blob: d3648df8b62ec387e80eb464907b5efc49fbeec9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
41 static int
42adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +010065 * Return how many lines "lnum" will take on the screen, taking into account
66 * whether it is the first line, whether w_skipcol is non-zero and limiting to
67 * the window height.
68 */
69 static int
70plines_correct_topline(win_T *wp, linenr_T lnum)
71{
72 int n;
73#ifdef FEAT_DIFF
74 if (lnum == wp->w_topline)
75 n = plines_win_nofill(wp, lnum, FALSE) + wp->w_topfill;
76 else
77#endif
78 n = plines_win(wp, lnum, FALSE);
79 if (lnum == wp->w_topline)
80 n = adjust_plines_for_skipcol(wp, n);
81 if (n > wp->w_height)
82 n = wp->w_height;
83 return n;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Compute wp->w_botline for the current wp->w_topline. Can be called after
88 * wp->w_topline changed.
89 */
90 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010091comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000092{
93 int n;
94 linenr_T lnum;
95 int done;
96#ifdef FEAT_FOLDING
97 linenr_T last;
98 int folded;
99#endif
100
101 /*
102 * If w_cline_row is valid, start there.
103 * Otherwise have to start at w_topline.
104 */
105 check_cursor_moved(wp);
106 if (wp->w_valid & VALID_CROW)
107 {
108 lnum = wp->w_cursor.lnum;
109 done = wp->w_cline_row;
110 }
111 else
112 {
113 lnum = wp->w_topline;
114 done = 0;
115 }
116
117 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
118 {
119#ifdef FEAT_FOLDING
120 last = lnum;
121 folded = FALSE;
122 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
123 {
124 n = 1;
125 folded = TRUE;
126 }
127 else
128#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100129 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100130 n = plines_correct_topline(wp, lnum);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 if (
133#ifdef FEAT_FOLDING
134 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
135#else
136 lnum == wp->w_cursor.lnum
137#endif
138 )
139 {
140 wp->w_cline_row = done;
141 wp->w_cline_height = n;
142#ifdef FEAT_FOLDING
143 wp->w_cline_folded = folded;
144#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100145 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
147 }
148 if (done + n > wp->w_height)
149 break;
150 done += n;
151#ifdef FEAT_FOLDING
152 lnum = last;
153#endif
154 }
155
Bram Moolenaar85a20022019-12-21 18:25:54 +0100156 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 wp->w_botline = lnum;
158 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
159
160 set_empty_rows(wp, done);
161}
162
163/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100164 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
165 * set.
166 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100168redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100169{
170 if ((wp->w_p_rnu
171#ifdef FEAT_SYN_HL
172 || wp->w_p_cul
173#endif
174 )
175 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200176 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200177 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000178 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100179 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200180 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100181}
182
zeertzjq3e559cd2022-03-27 19:26:55 +0100183#ifdef FEAT_SYN_HL
184/*
185 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
186 * contains "screenline".
187 */
188 static void
189redraw_for_cursorcolumn(win_T *wp)
190{
191 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
192 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100193 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100194 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100195 redraw_win_later(wp, UPD_SOME_VALID);
196 // When 'cursorlineopt' contains "screenline" need to redraw with
197 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100198 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100199 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100200 }
201}
202#endif
203
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100204/*
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000205 * Calculates how much overlap the smoothscroll marker "<<<" overlaps with
206 * buffer text for curwin.
207 * Parameter "extra2" should be the padding on the 2nd line, not the first
208 * line.
209 * Returns the number of columns of overlap with buffer text, excluding the
210 * extra padding on the ledge.
211 */
212 static int
213smoothscroll_marker_overlap(int extra2)
214{
215#if defined(FEAT_LINEBREAK)
216 // We don't draw the <<< marker when in showbreak mode, thus no need to
217 // account for it. See wlv_screen_line().
218 if (*get_showbreak_value(curwin) != NUL)
219 return 0;
220#endif
221 return extra2 > 3 ? 0 : 3 - extra2;
222}
223
224/*
Bram Moolenaardb4d88c2022-12-31 15:13:22 +0000225 * Calculates the skipcol offset for window "wp" given how many
226 * physical lines we want to scroll down.
227 */
228 static int
229skipcol_from_plines(win_T *wp, int plines_off)
230{
231 int width1 = wp->w_width - win_col_off(wp);
232
233 int skipcol = 0;
234 if (plines_off > 0)
235 skipcol += width1;
236 if (plines_off > 1)
237 skipcol += (width1 + win_col_off2(wp)) * (plines_off - 1);
238 return skipcol;
239}
240
241/*
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000242 * Set curwin->s_skipcol to zero and redraw later if needed.
243 */
244 static void
245reset_skipcol(void)
246{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000247 if (curwin->w_skipcol == 0)
248 return;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000249
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000250 curwin->w_skipcol = 0;
251
252 // Should use the least expensive way that displays all that changed.
253 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
254 // enough when the top line gets another screen line.
255 redraw_later(UPD_SOME_VALID);
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000256}
257
258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 * Update curwin->w_topline and redraw if necessary.
260 * Used to update the screen before printing a message.
261 */
262 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100263update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264{
265 update_topline();
266 if (must_redraw)
267 update_screen(0);
268}
269
270/*
271 * Update curwin->w_topline to move the cursor onto the screen.
272 */
273 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100274update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275{
276 long line_count;
277 int halfheight;
278 int n;
279 linenr_T old_topline;
280#ifdef FEAT_DIFF
281 int old_topfill;
282#endif
283#ifdef FEAT_FOLDING
284 linenr_T lnum;
285#endif
286 int check_topline = FALSE;
287 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100288 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100289 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100291 // Cursor is updated instead when this is TRUE for 'splitkeep'.
292 if (skip_update_topline)
293 return;
294
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // If there is no valid screen and when the window height is zero just use
296 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200297 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200298 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200300 curwin->w_topline = curwin->w_cursor.lnum;
301 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200302 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 check_cursor_moved(curwin);
307 if (curwin->w_valid & VALID_TOPLINE)
308 return;
309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000311 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100312 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
314 old_topline = curwin->w_topline;
315#ifdef FEAT_DIFF
316 old_topfill = curwin->w_topfill;
317#endif
318
319 /*
320 * If the buffer is empty, always set topline to 1.
321 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100325 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 curwin->w_botline = 2;
328 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 }
331
332 /*
333 * If the cursor is above or near the top of the window, scroll the window
334 * to show the line the cursor is in, with 'scrolloff' context.
335 */
336 else
337 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100338 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // If the cursor is above topline, scrolling is always needed.
341 // If the cursor is far below topline and there is no folding,
342 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (curwin->w_cursor.lnum < curwin->w_topline)
344 check_topline = TRUE;
345 else if (check_top_offset())
346 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100347 else if (curwin->w_cursor.lnum == curwin->w_topline)
348 {
349 colnr_T vcol;
350
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000351 // Check that the cursor position is visible. Add columns for
352 // the smoothscroll marker "<<<" displayed in the top-left if
353 // needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100354 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000355 int smoothscroll_overlap = smoothscroll_marker_overlap(
356 curwin_col_off() - curwin_col_off2());
357 if (curwin->w_skipcol + smoothscroll_overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100358 check_topline = TRUE;
359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 }
361#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100362 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
364 curwin->w_topline))
365 check_topline = TRUE;
366#endif
367
368 if (check_topline)
369 {
370 halfheight = curwin->w_height / 2 - 1;
371 if (halfheight < 2)
372 halfheight = 2;
373
374#ifdef FEAT_FOLDING
375 if (hasAnyFolding(curwin))
376 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // Count the number of logical lines between the cursor and
378 // topline + scrolloff (approximation of how much will be
379 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 n = 0;
381 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100382 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100385 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
387 break;
388 (void)hasFolding(lnum, NULL, &lnum);
389 }
390 }
391 else
392#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100393 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar85a20022019-12-21 18:25:54 +0100395 // If we weren't very close to begin with, we scroll to put the
396 // cursor in the middle of the window. Otherwise put the cursor
397 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (n >= halfheight)
399 scroll_cursor_halfway(FALSE);
400 else
401 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 check_botline = TRUE;
404 }
405 }
406
407 else
408 {
409#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100410 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
412#endif
413 check_botline = TRUE;
414 }
415 }
416
417 /*
418 * If the cursor is below the bottom of the window, scroll the window
419 * to put the cursor on the window.
420 * When w_botline is invalid, recompute it first, to avoid a redraw later.
421 * If w_botline was approximated, we might need a redraw later in a few
422 * cases, but we don't want to spend (a lot of) time recomputing w_botline
423 * for every small change.
424 */
425 if (check_botline)
426 {
427 if (!(curwin->w_valid & VALID_BOTLINE_AP))
428 validate_botline();
429
430 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
431 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000432 if (curwin->w_cursor.lnum < curwin->w_botline)
433 {
434 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100435 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#ifdef FEAT_FOLDING
437 || hasAnyFolding(curwin)
438#endif
439 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 lineoff_T loff;
442
Bram Moolenaar85a20022019-12-21 18:25:54 +0100443 // Cursor is (a few lines) above botline, check if there are
444 // 'scrolloff' window lines below the cursor. If not, need to
445 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 n = curwin->w_empty_rows;
447 loff.lnum = curwin->w_cursor.lnum;
448#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100449 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
451#endif
452#ifdef FEAT_DIFF
453 loff.fill = 0;
454 n += curwin->w_filler_rows;
455#endif
456 loff.height = 0;
457 while (loff.lnum < curwin->w_botline
458#ifdef FEAT_DIFF
459 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
460#endif
461 )
462 {
463 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100464 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 break;
466 botline_forw(&loff);
467 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100468 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100469 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000471 }
472 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100473 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000474 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 if (check_botline)
477 {
478#ifdef FEAT_FOLDING
479 if (hasAnyFolding(curwin))
480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 // Count the number of logical lines between the cursor and
482 // botline - scrolloff (approximation of how much will be
483 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 line_count = 0;
485 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100486 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
488 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100489 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 if (lnum <= 0 || line_count > curwin->w_height + 1)
491 break;
492 (void)hasFolding(lnum, &lnum, NULL);
493 }
494 }
495 else
496#endif
497 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100498 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000500 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 else
502 scroll_cursor_halfway(FALSE);
503 }
504 }
505 }
506 curwin->w_valid |= VALID_TOPLINE;
507
508 /*
509 * Need to redraw when topline changed.
510 */
511 if (curwin->w_topline != old_topline
512#ifdef FEAT_DIFF
513 || curwin->w_topfill != old_topfill
514#endif
515 )
516 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100517 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000518 redraw_later(UPD_VALID);
519 reset_skipcol();
520
Bram Moolenaar85a20022019-12-21 18:25:54 +0100521 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (curwin->w_cursor.lnum == curwin->w_topline)
523 validate_cursor();
524 }
525
Bram Moolenaar375e3392019-01-31 18:26:10 +0100526 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527}
528
529/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000530 * Return the scrolljump value to use for the current window.
531 * When 'scrolljump' is positive use it as-is.
532 * When 'scrolljump' is negative use it as a percentage of the window height.
533 */
534 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000536{
537 if (p_sj >= 0)
538 return (int)p_sj;
539 return (curwin->w_height * -p_sj) / 100;
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
544 * current window.
545 */
546 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100547check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 lineoff_T loff;
550 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100551 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar375e3392019-01-31 18:26:10 +0100553 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#ifdef FEAT_FOLDING
555 || hasAnyFolding(curwin)
556#endif
557 )
558 {
559 loff.lnum = curwin->w_cursor.lnum;
560#ifdef FEAT_DIFF
561 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100562 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#else
564 n = 0;
565#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100567 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100570 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (loff.lnum < curwin->w_topline
572#ifdef FEAT_DIFF
573 || (loff.lnum == curwin->w_topline && loff.fill > 0)
574#endif
575 )
576 break;
577 n += loff.height;
578 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100579 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return TRUE;
581 }
582 return FALSE;
583}
584
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100585/*
586 * Update w_curswant.
587 */
588 void
589update_curswant_force(void)
590{
591 validate_virtcol();
592 curwin->w_curswant = curwin->w_virtcol
593#ifdef FEAT_PROP_POPUP
594 - curwin->w_virtcol_first_char
595#endif
596 ;
597 curwin->w_set_curswant = FALSE;
598}
599
600/*
601 * Update w_curswant if w_set_curswant is set.
602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100607 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609
610/*
611 * Check if the cursor has moved. Set the w_valid flag accordingly.
612 */
613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100614check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
617 {
618 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100619 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
620 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 wp->w_valid_cursor = wp->w_cursor;
622 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100623 wp->w_valid_skipcol = wp->w_skipcol;
624 }
625 else if (wp->w_skipcol != wp->w_valid_skipcol)
626 {
627 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
628 |VALID_CHEIGHT|VALID_CROW
629 |VALID_BOTLINE|VALID_BOTLINE_AP);
630 wp->w_valid_cursor = wp->w_cursor;
631 wp->w_valid_leftcol = wp->w_leftcol;
632 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
634 else if (wp->w_cursor.col != wp->w_valid_cursor.col
635 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100636 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {
638 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
639 wp->w_valid_cursor.col = wp->w_cursor.col;
640 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643}
644
645/*
646 * Call this function when some window settings have changed, which require
647 * the cursor position, botline and topline to be recomputed and the window to
648 * be redrawn. E.g, when changing the 'wrap' option or folding.
649 */
650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100651changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 changed_window_setting_win(curwin);
654}
655
656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 wp->w_lines_valid = 0;
660 changed_line_abv_curs_win(wp);
661 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100662 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663}
664
665/*
Bram Moolenaar89469d12022-12-02 20:46:26 +0000666 * Call changed_window_setting_win() for every window containing "buf".
667 */
668 void
669changed_window_setting_buf(buf_T *buf)
670{
671 tabpage_T *tp;
672 win_T *wp;
673
674 FOR_ALL_TAB_WINDOWS(tp, wp)
675 if (wp->w_buffer == buf)
676 changed_window_setting_win(wp);
677}
678
679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * Set wp->w_topline to a certain number.
681 */
682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100683set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200685#ifdef FEAT_DIFF
686 linenr_T prev_topline = wp->w_topline;
687#endif
688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100690 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
692#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100693 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100695 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
696 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000698 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200700 if (lnum != prev_topline)
701 // Keep the filler lines when the topline didn't change.
702 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703#endif
704 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100705 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100706 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707}
708
709/*
710 * Call this function when the length of the cursor line (in screen
711 * characters) has changed, and the change is before the cursor.
712 * Need to take care of w_botline separately!
713 */
714 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100715changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
717 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
718 |VALID_CHEIGHT|VALID_TOPLINE);
719}
720
721 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100722changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723{
724 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
725 |VALID_CHEIGHT|VALID_TOPLINE);
726}
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728/*
729 * Call this function when the length of a line (in screen characters) above
730 * the cursor have changed.
731 * Need to take care of w_botline separately!
732 */
733 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100734changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
737 |VALID_CHEIGHT|VALID_TOPLINE);
738}
739
740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100741changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
743 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
744 |VALID_CHEIGHT|VALID_TOPLINE);
745}
746
747/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100748 * Display of line has changed for "buf", invalidate cursor position and
749 * w_botline.
750 */
751 void
752changed_line_display_buf(buf_T *buf)
753{
754 win_T *wp;
755
756 FOR_ALL_WINDOWS(wp)
757 if (wp->w_buffer == buf)
758 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
759 |VALID_CROW|VALID_CHEIGHT
760 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
761}
762
763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * Make sure the value of curwin->w_botline is valid.
765 */
766 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100767validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100769 validate_botline_win(curwin);
770}
771
772/*
773 * Make sure the value of wp->w_botline is valid.
774 */
775 void
776validate_botline_win(win_T *wp)
777{
778 if (!(wp->w_valid & VALID_BOTLINE))
779 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * Mark curwin->w_botline as invalid (because of some change in the buffer).
784 */
785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100786invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
789}
790
791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100792invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
795}
796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100798approximate_botline_win(
799 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 wp->w_valid &= ~VALID_BOTLINE;
802}
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804/*
805 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
806 */
807 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100808cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809{
810 check_cursor_moved(curwin);
811 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
812 (VALID_WROW|VALID_WCOL));
813}
814
815/*
816 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
817 * w_topline must be valid, you may need to call update_topline() first!
818 */
819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100820validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100822 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 check_cursor_moved(curwin);
824 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
825 curs_columns(TRUE);
826}
827
828#if defined(FEAT_GUI) || defined(PROTO)
829/*
830 * validate w_cline_row.
831 */
832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100833validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 /*
836 * First make sure that w_topline is valid (after moving the cursor).
837 */
838 update_topline();
839 check_cursor_moved(curwin);
840 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100841 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843#endif
844
845/*
846 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200847 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 */
849 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100850curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
852 linenr_T lnum;
853 int i;
854 int all_invalid;
855 int valid;
856#ifdef FEAT_FOLDING
857 long fold_count;
858#endif
859
Bram Moolenaar85a20022019-12-21 18:25:54 +0100860 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 all_invalid = (!redrawing()
862 || wp->w_lines_valid == 0
863 || wp->w_lines[0].wl_lnum > wp->w_topline);
864 i = 0;
865 wp->w_cline_row = 0;
866 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
867 {
868 valid = FALSE;
869 if (!all_invalid && i < wp->w_lines_valid)
870 {
871 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100872 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (wp->w_lines[i].wl_lnum == lnum)
874 {
875#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100876 // Check for newly inserted lines below this row, in which
877 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (!wp->w_buffer->b_mod_set
879 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
880 || wp->w_buffer->b_mod_top
881 > wp->w_lines[i].wl_lastlnum + 1)
882#endif
883 valid = TRUE;
884 }
885 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100886 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
888 if (valid
889#ifdef FEAT_DIFF
890 && (lnum != wp->w_topline || !wp->w_p_diff)
891#endif
892 )
893 {
894#ifdef FEAT_FOLDING
895 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100896 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (lnum > wp->w_cursor.lnum)
898 break;
899#else
900 ++lnum;
901#endif
902 wp->w_cline_row += wp->w_lines[i].wl_size;
903 }
904 else
905 {
906#ifdef FEAT_FOLDING
907 fold_count = foldedCount(wp, lnum, NULL);
908 if (fold_count)
909 {
910 lnum += fold_count;
911 if (lnum > wp->w_cursor.lnum)
912 break;
913 ++wp->w_cline_row;
914 }
915 else
916#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100917 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100918 wp->w_cline_row += plines_correct_topline(wp, lnum);
919 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922 }
923
924 check_cursor_moved(wp);
925 if (!(wp->w_valid & VALID_CHEIGHT))
926 {
927 if (all_invalid
928 || i == wp->w_lines_valid
929 || (i < wp->w_lines_valid
930 && (!wp->w_lines[i].wl_valid
931 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
932 {
933#ifdef FEAT_DIFF
934 if (wp->w_cursor.lnum == wp->w_topline)
935 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
936 TRUE) + wp->w_topfill;
937 else
938#endif
939 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
940#ifdef FEAT_FOLDING
941 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
942 NULL, NULL, TRUE, NULL);
943#endif
944 }
945 else if (i > wp->w_lines_valid)
946 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100947 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 wp->w_cline_height = 0;
949#ifdef FEAT_FOLDING
950 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
951 NULL, NULL, TRUE, NULL);
952#endif
953 }
954 else
955 {
956 wp->w_cline_height = wp->w_lines[i].wl_size;
957#ifdef FEAT_FOLDING
958 wp->w_cline_folded = wp->w_lines[i].wl_folded;
959#endif
960 }
961 }
962
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100963 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966
967/*
968 * Validate curwin->w_virtcol only.
969 */
970 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100971validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 validate_virtcol_win(curwin);
974}
975
976/*
977 * Validate wp->w_virtcol only.
978 */
979 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100980validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
982 check_cursor_moved(wp);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000983
984 if (wp->w_valid & VALID_VIRTCOL)
985 return;
986
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100987#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000988 wp->w_virtcol_first_char = 0;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100989#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000990 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000991#ifdef FEAT_SYN_HL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000992 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000993#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000994 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995}
996
997/*
998 * Validate curwin->w_cline_height only.
999 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +01001000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001001validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 check_cursor_moved(curwin);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001004
1005 if (curwin->w_valid & VALID_CHEIGHT)
1006 return;
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#ifdef FEAT_DIFF
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001009 if (curwin->w_cursor.lnum == curwin->w_topline)
1010 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
1011 + curwin->w_topfill;
1012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001014 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#ifdef FEAT_FOLDING
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001016 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 curwin->w_valid |= VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019}
1020
1021/*
Bram Moolenaarc236c162008-07-13 17:41:49 +00001022 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
1024 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001025validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
1027 colnr_T off;
1028 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +01001029 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
1031 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001033 if (curwin->w_valid & VALID_WCOL)
1034 return;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001035
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001036 col = curwin->w_virtcol;
1037 off = curwin_col_off();
1038 col += off;
1039 width = curwin->w_width - off + curwin_col_off2();
1040
1041 // long line wrapping, adjust curwin->w_wrow
1042 if (curwin->w_p_wrap
1043 && col >= (colnr_T)curwin->w_width
1044 && width > 0)
1045 // use same formula as what is used in curs_columns()
1046 col -= ((col - curwin->w_width) / width + 1) * width;
1047 if (col > (int)curwin->w_leftcol)
1048 col -= curwin->w_leftcol;
1049 else
1050 col = 0;
1051 curwin->w_wcol = col;
1052
1053 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001054#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001055 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057}
1058
1059/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001060 * Compute offset of a window, occupied by absolute or relative line number,
1061 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 */
1063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001064win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaar64486672010-05-16 15:46:46 +02001066 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#ifdef FEAT_FOLDING
1069 + wp->w_p_fdc
1070#endif
1071#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001072 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#endif
1074 );
1075}
1076
1077 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001078curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
1080 return win_col_off(curwin);
1081}
1082
1083/*
1084 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001085 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1086 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 */
1088 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001089win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
Bram Moolenaar64486672010-05-16 15:46:46 +02001091 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001092 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 return 0;
1094}
1095
1096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001097curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
1099 return win_col_off2(curwin);
1100}
1101
1102/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001103 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 * Also updates curwin->w_wrow and curwin->w_cline_row.
1105 * Also updates curwin->w_leftcol.
1106 */
1107 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001108curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001109 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001112 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 int off_left, off_right;
1114 int n;
1115 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001116 int width1; // text width for first screen line
1117 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 int new_leftcol;
1119 colnr_T startcol;
1120 colnr_T endcol;
1121 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001122 long so = get_scrolloff_value();
1123 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001124 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
1126 /*
1127 * First make sure that w_topline is valid (after moving the cursor).
1128 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001129 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
1131 /*
1132 * Next make sure that w_cline_row is valid.
1133 */
1134 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001135 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001137#ifdef FEAT_PROP_POPUP
1138 // will be set by getvvcol() but not reset
1139 curwin->w_virtcol_first_char = 0;
1140#endif
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 /*
1143 * Compute the number of virtual columns.
1144 */
1145#ifdef FEAT_FOLDING
1146 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001147 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1149 else
1150#endif
1151 getvvcol(curwin, &curwin->w_cursor,
1152 &startcol, &(curwin->w_virtcol), &endcol);
1153
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001156 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 extra = curwin_col_off();
1159 curwin->w_wcol = curwin->w_virtcol + extra;
1160 endcol += extra;
1161
1162 /*
1163 * Now compute w_wrow, counting screen lines from w_cline_row.
1164 */
1165 curwin->w_wrow = curwin->w_cline_row;
1166
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001167 width1 = curwin->w_width - extra;
1168 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001170 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001171 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001172 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001173 if (curwin->w_p_wrap)
1174 curwin->w_wrow = curwin->w_height - 1;
1175 else
1176 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001178 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001180 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001182 // skip columns that are not visible
1183 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001184 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001185 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001186 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001187 // Deduct by multiples of width2. This allows the long line
1188 // wrapping formula below to correctly calculate the w_wcol value
1189 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001190 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001191 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001192 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001193 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001194 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001195
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001196 did_sub_skipcol = TRUE;
1197 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001198
Bram Moolenaar85a20022019-12-21 18:25:54 +01001199 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001200 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001203 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1204 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 curwin->w_wrow += n;
1206
1207#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // When cursor wraps to first char of next line in Insert
1209 // mode, the 'showbreak' string isn't shown, backup to first
1210 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001211 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001212 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001213 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 curwin->w_wcol = 0;
1215#endif
1216 }
1217 }
1218
Bram Moolenaar85a20022019-12-21 18:25:54 +01001219 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1220 // is not folded.
1221 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001222 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#ifdef FEAT_FOLDING
1224 && !curwin->w_cline_folded
1225#endif
1226 )
1227 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001228#ifdef FEAT_PROP_POPUP
1229 if (curwin->w_virtcol_first_char > 0)
1230 {
1231 int cols = (curwin->w_width - extra);
1232 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1233
1234 // each "above" text prop shifts the text one row down
1235 curwin->w_wrow += rows;
1236 curwin->w_wcol -= rows * cols;
1237 endcol -= rows * cols;
1238 curwin->w_cline_height = rows + 1;
1239 }
1240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 /*
1242 * If Cursor is left of the screen, scroll rightwards.
1243 * If Cursor is right of the screen, scroll leftwards
1244 * If we get closer to the edge than 'sidescrolloff', scroll a little
1245 * extra
1246 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001247 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001248 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001249 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (off_left < 0 || off_right > 0)
1251 {
1252 if (off_left < 0)
1253 diff = -off_left;
1254 else
1255 diff = off_right;
1256
Bram Moolenaar85a20022019-12-21 18:25:54 +01001257 // When far off or not enough room on either side, put cursor in
1258 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001259 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1260 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 else
1262 {
1263 if (diff < p_ss)
1264 diff = p_ss;
1265 if (off_left < 0)
1266 new_leftcol = curwin->w_leftcol - diff;
1267 else
1268 new_leftcol = curwin->w_leftcol + diff;
1269 }
1270 if (new_leftcol < 0)
1271 new_leftcol = 0;
1272 if (new_leftcol != (int)curwin->w_leftcol)
1273 {
1274 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001275 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001276 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278 }
1279 curwin->w_wcol -= curwin->w_leftcol;
1280 }
1281 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1282 curwin->w_wcol -= curwin->w_leftcol;
1283 else
1284 curwin->w_wcol = 0;
1285
1286#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001287 // Skip over filler lines. At the top use w_topfill, there
1288 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (curwin->w_cursor.lnum == curwin->w_topline)
1290 curwin->w_wrow += curwin->w_topfill;
1291 else
1292 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1293#endif
1294
1295 prev_skipcol = curwin->w_skipcol;
1296
1297 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001298
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if ((curwin->w_wrow >= curwin->w_height
1300 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001301 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && (p_lines =
1303#ifdef FEAT_DIFF
1304 plines_win_nofill
1305#else
1306 plines_win
1307#endif
1308 (curwin, curwin->w_cursor.lnum, FALSE))
1309 - 1 >= curwin->w_height))
1310 && curwin->w_height != 0
1311 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001312 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001313 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001315 // Cursor past end of screen. Happens with a single line that does
1316 // not fit on screen. Find a skipcol to show the text around the
1317 // cursor. Avoid scrolling all the time. compute value of "extra":
1318 // 1: Less than 'scrolloff' lines above
1319 // 2: Less than 'scrolloff' lines below
1320 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001322 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001324 // Compute last display line of the buffer line that we want at the
1325 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (p_lines == 0)
1327 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1328 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001329 if (p_lines > curwin->w_wrow + so)
1330 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 else
1332 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001333 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 extra += 2;
1335
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001336 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001339 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (n > curwin->w_height / 2)
1341 n -= curwin->w_height / 2;
1342 else
1343 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001344 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (n > p_lines - curwin->w_height + 1)
1346 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001347 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 else if (extra == 1)
1350 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001351 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001352 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1353 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (extra > 0)
1355 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001356 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1357 extra = curwin->w_skipcol / width2;
1358 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360 }
1361 else if (extra == 2)
1362 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001363 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001364 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001366 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (endcol > curwin->w_skipcol)
1368 curwin->w_skipcol = endcol;
1369 }
1370
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001371 // adjust w_wrow for the changed w_skipcol
1372 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001373 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001374 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001375 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (curwin->w_wrow >= curwin->w_height)
1378 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001379 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001381 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 curwin->w_wrow -= extra;
1383 }
1384
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001385 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (extra > 0)
1387 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1388 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001389 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001391 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 curwin->w_skipcol = 0;
1393 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001394 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001396#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001397 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001398#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001399#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1400 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1401 {
1402 curwin->w_wrow += popup_top_extra(curwin);
1403 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001404 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001405 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001406 else
1407 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001408#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001409
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001410 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1411 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001412 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001413 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1416}
1417
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001418#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001419/*
1420 * Compute the screen position of text character at "pos" in window "wp"
1421 * The resulting values are one-based, zero when character is not visible.
1422 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001423 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001424textpos2screenpos(
1425 win_T *wp,
1426 pos_T *pos,
1427 int *rowp, // screen row
1428 int *scolp, // start screen column
1429 int *ccolp, // cursor screen column
1430 int *ecolp) // end screen column
1431{
1432 colnr_T scol = 0, ccol = 0, ecol = 0;
1433 int row = 0;
1434 int rowoff = 0;
1435 colnr_T coloff = 0;
1436
Bram Moolenaar189663b2021-07-21 18:04:56 +02001437 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001438 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001439 colnr_T col;
1440 int width;
1441 linenr_T lnum = pos->lnum;
1442#ifdef FEAT_FOLDING
1443 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001444
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001445 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1446#endif
1447 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
Bram Moolenaar1cb16c32022-12-05 22:26:44 +00001448
1449#ifdef FEAT_DIFF
1450 // Add filler lines above this buffer line.
1451 row += diff_check_fill(wp, lnum);
1452#endif
1453
zeertzjqba2d1912022-12-18 12:28:59 +00001454 colnr_T off = win_col_off(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001455#ifdef FEAT_FOLDING
1456 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001457 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001458 row += W_WINROW(wp);
zeertzjqba2d1912022-12-18 12:28:59 +00001459 coloff = wp->w_wincol + 1 + off;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001460 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001461 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001462#endif
1463 {
1464 getvcol(wp, pos, &scol, &ccol, &ecol);
1465
1466 // similar to what is done in validate_cursor_col()
1467 col = scol;
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001468 col += off;
1469 width = wp->w_width - off + win_col_off2(wp);
1470
1471 // long line wrapping, adjust row
1472 if (wp->w_p_wrap
1473 && col >= (colnr_T)wp->w_width
1474 && width > 0)
1475 {
1476 // use same formula as what is used in curs_columns()
1477 rowoff = ((col - wp->w_width) / width + 1);
1478 col -= rowoff * width;
1479 }
1480 col -= wp->w_leftcol;
1481 if (col >= wp->w_width)
1482 col = -1;
1483 if (col >= 0 && row + rowoff <= wp->w_height)
1484 {
1485 coloff = col - scol + wp->w_wincol + 1;
1486 row += W_WINROW(wp);
1487 }
1488 else
1489 // character is left, right or below of the window
1490 row = rowoff = scol = ccol = ecol = 0;
1491 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001492 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001493 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001494 *scolp = scol + coloff;
1495 *ccolp = ccol + coloff;
1496 *ecolp = ecol + coloff;
1497}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001498#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001499
Bram Moolenaar12034e22019-08-25 22:25:02 +02001500#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001501/*
1502 * "screenpos({winid}, {lnum}, {col})" function
1503 */
1504 void
1505f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1506{
1507 dict_T *dict;
1508 win_T *wp;
1509 pos_T pos;
1510 int row = 0;
1511 int scol = 0, ccol = 0, ecol = 0;
1512
Bram Moolenaar93a10962022-06-16 11:42:09 +01001513 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001514 return;
1515 dict = rettv->vval.v_dict;
1516
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001517 if (in_vim9script()
1518 && (check_for_number_arg(argvars, 0) == FAIL
1519 || check_for_number_arg(argvars, 1) == FAIL
1520 || check_for_number_arg(argvars, 2) == FAIL))
1521 return;
1522
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001523 wp = find_win_by_nr_or_id(&argvars[0]);
1524 if (wp == NULL)
1525 return;
1526
1527 pos.lnum = tv_get_number(&argvars[1]);
Bram Moolenaar99d19432022-12-05 16:23:24 +00001528 if (pos.lnum > wp->w_buffer->b_ml.ml_line_count)
1529 {
1530 semsg(_(e_invalid_line_number_nr), pos.lnum);
1531 return;
1532 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001533 pos.col = tv_get_number(&argvars[2]) - 1;
1534 pos.coladd = 0;
1535 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1536
1537 dict_add_number(dict, "row", row);
1538 dict_add_number(dict, "col", scol);
1539 dict_add_number(dict, "curscol", ccol);
1540 dict_add_number(dict, "endcol", ecol);
1541}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001542
1543/*
1544 * "virtcol2col({winid}, {lnum}, {col})" function
1545 */
1546 void
1547f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1548{
1549 win_T *wp;
1550 linenr_T lnum;
1551 int screencol;
1552 int error = FALSE;
1553
1554 rettv->vval.v_number = -1;
1555
1556 if (check_for_number_arg(argvars, 0) == FAIL
1557 || check_for_number_arg(argvars, 1) == FAIL
1558 || check_for_number_arg(argvars, 2) == FAIL)
1559 return;
1560
1561 wp = find_win_by_nr_or_id(&argvars[0]);
1562 if (wp == NULL)
1563 return;
1564
1565 lnum = tv_get_number_chk(&argvars[1], &error);
1566 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1567 return;
1568
1569 screencol = tv_get_number_chk(&argvars[2], &error);
1570 if (error || screencol < 0)
1571 return;
1572
1573 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1574}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001575#endif
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577/*
1578 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001581scrolldown(
1582 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001583 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001585 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 int wrow;
1587 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001588 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001589 int width1 = 0;
1590 int width2 = 0;
1591
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001592 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001593 {
1594 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001595 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
1598#ifdef FEAT_FOLDING
1599 linenr_T first;
1600
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1603#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001604 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001605 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001608 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1609 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 ++curwin->w_topfill;
1612 ++done;
1613 }
1614 else
1615#endif
1616 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001617 // break when at the very top
1618 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001619 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001621 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001623 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001624 if (curwin->w_skipcol >= width1 + width2)
1625 curwin->w_skipcol -= width2;
1626 else
1627 curwin->w_skipcol -= width1;
1628 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 }
1631 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001632 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001633 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001634 --curwin->w_topline;
1635 curwin->w_skipcol = 0;
1636#ifdef FEAT_DIFF
1637 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001639#ifdef FEAT_FOLDING
1640 // A sequence of folded lines only counts for one logical line
1641 if (hasFolding(curwin->w_topline, &first, NULL))
1642 {
1643 ++done;
1644 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001645 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001646 curwin->w_botline -= curwin->w_topline - first;
1647 curwin->w_topline = first;
1648 }
1649 else
1650#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001651 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001652 {
1653 int size = win_linetabsize(curwin, curwin->w_topline,
1654 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1655 if (size > width1)
1656 {
1657 curwin->w_skipcol = width1;
1658 size -= width1;
1659 redraw_later(UPD_NOT_VALID);
1660 }
1661 while (size > width2)
1662 {
1663 curwin->w_skipcol += width2;
1664 size -= width2;
1665 }
1666 ++done;
1667 }
1668 else
1669 done += PLINES_NOFILL(curwin->w_topline);
1670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001672 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 invalidate_botline();
1674 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001675 curwin->w_wrow += done; // keep w_wrow updated
1676 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678#ifdef FEAT_DIFF
1679 if (curwin->w_cursor.lnum == curwin->w_topline)
1680 curwin->w_cline_row = 0;
1681 check_topfill(curwin, TRUE);
1682#endif
1683
1684 /*
1685 * Compute the row number of the last row of the cursor line
1686 * and move the cursor onto the displayed part of the window.
1687 */
1688 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001689 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 validate_virtcol();
1692 validate_cheight();
1693 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001694 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
1696 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1697 {
1698#ifdef FEAT_FOLDING
1699 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1700 {
1701 --wrow;
1702 if (first == 1)
1703 curwin->w_cursor.lnum = 1;
1704 else
1705 curwin->w_cursor.lnum = first - 1;
1706 }
1707 else
1708#endif
1709 wrow -= plines(curwin->w_cursor.lnum--);
1710 curwin->w_valid &=
1711 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1712 moved = TRUE;
1713 }
1714 if (moved)
1715 {
1716#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001717 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 foldAdjustCursor();
1719#endif
1720 coladvance(curwin->w_curswant);
1721 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001722
1723 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1724 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001725 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001726 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1727
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001728 // make sure the cursor is in the visible text
1729 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001730 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001731 int row = 0;
1732 if (col >= width1)
1733 {
1734 col -= width1;
1735 ++row;
1736 }
1737 if (col > width2)
1738 {
1739 row += col / width2;
1740 col = col % width2;
1741 }
1742 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001743 {
1744 curwin->w_curswant = curwin->w_virtcol
1745 - (row - curwin->w_height + 1) * width2;
1746 coladvance(curwin->w_curswant);
1747 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749}
1750
1751/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001752 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1753 */
1754 static int
1755scrolling_screenlines(int byfold UNUSED)
1756{
1757 return (curwin->w_p_wrap && curwin->w_p_sms)
1758# ifdef FEAT_FOLDING
1759 || (byfold && hasAnyFolding(curwin))
1760# endif
1761# ifdef FEAT_DIFF
1762 || curwin->w_p_diff
1763# endif
1764 ;
1765}
1766
1767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001771scrollup(
1772 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001773 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001775 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001777 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001779 int width1 = curwin->w_width - curwin_col_off();
1780 int width2 = width1 + curwin_col_off2();
1781 int size = 0;
1782 linenr_T prev_topline = curwin->w_topline;
1783
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001784 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001785 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001786
1787 // diff mode: first consume "topfill"
1788 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1789 // the line, then advance to the next line.
1790 // folding: count each sequence of folded lines as one logical line.
1791 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
1793# ifdef FEAT_DIFF
1794 if (curwin->w_topfill > 0)
1795 --curwin->w_topfill;
1796 else
1797# endif
1798 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001799 linenr_T lnum = curwin->w_topline;
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801# ifdef FEAT_FOLDING
1802 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001803 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 (void)hasFolding(lnum, NULL, &lnum);
1805# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001806 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001807 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001808 // 'smoothscroll': increase "w_skipcol" until it goes over
1809 // the end of the line, then advance to the next line.
1810 int add = curwin->w_skipcol > 0 ? width2 : width1;
1811 curwin->w_skipcol += add;
1812 if (curwin->w_skipcol >= size)
1813 {
1814 if (lnum == curbuf->b_ml.ml_line_count)
1815 {
1816 // at the last screen line, can't scroll further
1817 curwin->w_skipcol -= add;
1818 break;
1819 }
1820 ++lnum;
1821 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001822 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001823 else
1824 {
1825 if (lnum >= curbuf->b_ml.ml_line_count)
1826 break;
1827 ++lnum;
1828 }
1829
1830 if (lnum > curwin->w_topline)
1831 {
1832 // approximate w_botline
1833 curwin->w_botline += lnum - curwin->w_topline;
1834 curwin->w_topline = lnum;
1835# ifdef FEAT_DIFF
1836 curwin->w_topfill = diff_check_fill(curwin, lnum);
1837# endif
1838 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001839 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001840 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001841 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001842 }
1843 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001844
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001845 if (curwin->w_topline == prev_topline)
1846 // need to redraw even though w_topline didn't change
1847 redraw_later(UPD_NOT_VALID);
1848 }
1849 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
1851 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001852 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854
1855 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1856 curwin->w_topline = curbuf->b_ml.ml_line_count;
1857 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1858 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1859
1860#ifdef FEAT_DIFF
1861 check_topfill(curwin, FALSE);
1862#endif
1863
1864#ifdef FEAT_FOLDING
1865 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001866 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1868#endif
1869
1870 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1871 if (curwin->w_cursor.lnum < curwin->w_topline)
1872 {
1873 curwin->w_cursor.lnum = curwin->w_topline;
1874 curwin->w_valid &=
1875 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1876 coladvance(curwin->w_curswant);
1877 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001878 if (curwin->w_cursor.lnum == curwin->w_topline
1879 && do_sms && curwin->w_skipcol > 0)
1880 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001881 int col_off = curwin_col_off();
1882 int col_off2 = curwin_col_off2();
1883
1884 int width1 = curwin->w_width - col_off;
1885 int width2 = width1 + col_off2;
1886 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001887 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001888 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001889 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001890
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001891 // If we have non-zero scrolloff, just ignore the <<< marker as we are
1892 // going past it anyway.
1893 int smoothscroll_overlap = scrolloff_cols != 0 ? 0 :
1894 smoothscroll_marker_overlap(extra2);
1895
Bram Moolenaar118c2352022-10-09 17:19:27 +01001896 // Make sure the cursor is in a visible part of the line, taking
1897 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001898 // If there are not enough screen lines put the cursor in the middle.
1899 if (scrolloff_cols > space_cols / 2)
1900 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001901 validate_virtcol();
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001902 if (curwin->w_virtcol < curwin->w_skipcol
1903 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001904 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001905 colnr_T col = curwin->w_virtcol;
1906
1907 if (col < width1)
1908 col += width1;
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001909 while (col < curwin->w_skipcol
1910 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001911 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001912 curwin->w_curswant = col;
1913 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001914
1915 // validate_virtcol() marked various things as valid, but after
1916 // moving the cursor they need to be recomputed
1917 curwin->w_valid &=
1918 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001919 }
1920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921}
1922
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001923/*
1924 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1925 * valid for 'smoothscroll'.
1926 */
1927 void
1928adjust_skipcol(void)
1929{
1930 if (!curwin->w_p_wrap
1931 || !curwin->w_p_sms
1932 || curwin->w_cursor.lnum != curwin->w_topline)
1933 return;
1934
1935 int width1 = curwin->w_width - curwin_col_off();
1936 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001937 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001938 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1939 int scrolled = FALSE;
1940
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001941 validate_cheight();
Bram Moolenaarb21b8e92022-12-03 18:35:07 +00001942 if (curwin->w_cline_height == curwin->w_height
1943 // w_cline_height may be capped at w_height, check there aren't
1944 // actually more lines.
1945 && plines_win(curwin, curwin->w_cursor.lnum, FALSE)
1946 <= curwin->w_height)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001947 {
1948 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001949 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001950 return;
1951 }
1952
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001953 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001954 while (curwin->w_skipcol > 0
1955 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1956 {
1957 // scroll a screen line down
1958 if (curwin->w_skipcol >= width1 + width2)
1959 curwin->w_skipcol -= width2;
1960 else
1961 curwin->w_skipcol -= width1;
1962 redraw_later(UPD_NOT_VALID);
1963 scrolled = TRUE;
1964 validate_virtcol();
1965 }
1966 if (scrolled)
1967 return; // don't scroll in the other direction now
1968
1969 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1970 int row = 0;
1971 if (col >= width1)
1972 {
1973 col -= width1;
1974 ++row;
1975 }
1976 if (col > width2)
1977 {
1978 row += col / width2;
1979 col = col % width2;
1980 }
1981 if (row >= curwin->w_height)
1982 {
1983 if (curwin->w_skipcol == 0)
1984 {
1985 curwin->w_skipcol += width1;
1986 --row;
1987 }
1988 if (row >= curwin->w_height)
1989 curwin->w_skipcol += (row - curwin->w_height) * width2;
1990 redraw_later(UPD_NOT_VALID);
1991 }
1992}
1993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994#ifdef FEAT_DIFF
1995/*
1996 * Don't end up with too many filler lines in the window.
1997 */
1998 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001999check_topfill(
2000 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002001 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003 int n;
2004
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002005 if (wp->w_topfill <= 0)
2006 return;
2007
2008 n = plines_win_nofill(wp, wp->w_topline, TRUE);
2009 if (wp->w_topfill + n > wp->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002011 if (down && wp->w_topline > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002013 --wp->w_topline;
2014 wp->w_topfill = 0;
2015 }
2016 else
2017 {
2018 wp->w_topfill = wp->w_height - n;
2019 if (wp->w_topfill < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 }
2023}
2024
2025/*
2026 * Use as many filler lines as possible for w_topline. Make sure w_topline
2027 * is still visible.
2028 */
2029 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002030max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 int n;
2033
2034 n = plines_nofill(curwin->w_topline);
2035 if (n >= curwin->w_height)
2036 curwin->w_topfill = 0;
2037 else
2038 {
2039 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2040 if (curwin->w_topfill + n > curwin->w_height)
2041 curwin->w_topfill = curwin->w_height - n;
2042 }
2043}
2044#endif
2045
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046/*
2047 * Scroll the screen one line down, but don't do it if it would move the
2048 * cursor off the screen.
2049 */
2050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002051scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052{
2053 int end_row;
2054#ifdef FEAT_DIFF
2055 int can_fill = (curwin->w_topfill
2056 < diff_check_fill(curwin, curwin->w_topline));
2057#endif
2058
2059 if (curwin->w_topline <= 1
2060#ifdef FEAT_DIFF
2061 && !can_fill
2062#endif
2063 )
2064 return;
2065
Bram Moolenaar85a20022019-12-21 18:25:54 +01002066 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 /*
2069 * Compute the row number of the last row of the cursor line
2070 * and make sure it doesn't go off the screen. Make sure the cursor
2071 * doesn't go past 'scrolloff' lines from the screen end.
2072 */
2073 end_row = curwin->w_wrow;
2074#ifdef FEAT_DIFF
2075 if (can_fill)
2076 ++end_row;
2077 else
2078 end_row += plines_nofill(curwin->w_topline - 1);
2079#else
2080 end_row += plines(curwin->w_topline - 1);
2081#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002082 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 validate_cheight();
2085 validate_virtcol();
2086 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002087 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002089 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091#ifdef FEAT_DIFF
2092 if (can_fill)
2093 {
2094 ++curwin->w_topfill;
2095 check_topfill(curwin, TRUE);
2096 }
2097 else
2098 {
2099 --curwin->w_topline;
2100 curwin->w_topfill = 0;
2101 }
2102#else
2103 --curwin->w_topline;
2104#endif
2105#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002106 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002108 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2110 }
2111}
2112
2113/*
2114 * Scroll the screen one line up, but don't do it if it would move the cursor
2115 * off the screen.
2116 */
2117 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 int start_row;
2121
2122 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2123#ifdef FEAT_DIFF
2124 && curwin->w_topfill == 0
2125#endif
2126 )
2127 return;
2128
Bram Moolenaar85a20022019-12-21 18:25:54 +01002129 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
2131 /*
2132 * Compute the row number of the first row of the cursor line
2133 * and make sure it doesn't go off the screen. Make sure the cursor
2134 * doesn't go before 'scrolloff' lines from the screen start.
2135 */
2136#ifdef FEAT_DIFF
2137 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2138 - curwin->w_topfill;
2139#else
2140 start_row = curwin->w_wrow - plines(curwin->w_topline);
2141#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002142 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002145 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002147 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
2149#ifdef FEAT_DIFF
2150 if (curwin->w_topfill > 0)
2151 --curwin->w_topfill;
2152 else
2153#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002154 {
2155#ifdef FEAT_FOLDING
2156 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002159 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002160 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2162 }
2163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165/*
2166 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2167 * a (wrapped) text line. Uses and sets "lp->fill".
2168 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002169 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 */
2171 static void
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002172topline_back_winheight(
2173 lineoff_T *lp,
2174 int winheight) // when TRUE limit to window height
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176#ifdef FEAT_DIFF
2177 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2178 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002179 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 ++lp->fill;
2181 lp->height = 1;
2182 }
2183 else
2184#endif
2185 {
2186 --lp->lnum;
2187#ifdef FEAT_DIFF
2188 lp->fill = 0;
2189#endif
2190 if (lp->lnum < 1)
2191 lp->height = MAXCOL;
2192 else
2193#ifdef FEAT_FOLDING
2194 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002195 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 lp->height = 1;
2197 else
2198#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002199 lp->height = PLINES_WIN_NOFILL(curwin, lp->lnum, winheight);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
2201}
2202
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002203 static void
2204topline_back(lineoff_T *lp)
2205{
2206 topline_back_winheight(lp, TRUE);
2207}
2208
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210/*
2211 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2212 * a (wrapped) text line. Uses and sets "lp->fill".
2213 * Returns the height of the added line in "lp->height".
2214 * Lines below the last one are incredibly high.
2215 */
2216 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002217botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218{
2219#ifdef FEAT_DIFF
2220 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002222 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 ++lp->fill;
2224 lp->height = 1;
2225 }
2226 else
2227#endif
2228 {
2229 ++lp->lnum;
2230#ifdef FEAT_DIFF
2231 lp->fill = 0;
2232#endif
2233 if (lp->lnum > curbuf->b_ml.ml_line_count)
2234 lp->height = MAXCOL;
2235 else
2236#ifdef FEAT_FOLDING
2237 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002238 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 lp->height = 1;
2240 else
2241#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002242 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
2244}
2245
2246#ifdef FEAT_DIFF
2247/*
2248 * Switch from including filler lines below lp->lnum to including filler
2249 * lines above loff.lnum + 1. This keeps pointing to the same line.
2250 * When there are no filler lines nothing changes.
2251 */
2252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002253botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 if (lp->fill > 0)
2256 {
2257 ++lp->lnum;
2258 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2259 }
2260}
2261
2262/*
2263 * Switch from including filler lines above lp->lnum to including filler
2264 * lines below loff.lnum - 1. This keeps pointing to the same line.
2265 * When there are no filler lines nothing changes.
2266 */
2267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002268topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270 if (lp->fill > 0)
2271 {
2272 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2273 --lp->lnum;
2274 }
2275}
2276#endif
2277
2278/*
2279 * Recompute topline to put the cursor at the top of the window.
2280 * Scroll at least "min_scroll" lines.
2281 * If "always" is TRUE, always set topline (for "zt").
2282 */
2283 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 int scrolled = 0;
2287 int extra = 0;
2288 int used;
2289 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002290 linenr_T top; // just above displayed lines
2291 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002293 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294#ifdef FEAT_DIFF
2295 linenr_T old_topfill = curwin->w_topfill;
2296#endif
2297 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002298 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (mouse_dragging > 0)
2301 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
2303 /*
2304 * Decrease topline until:
2305 * - it has become 1
2306 * - (part of) the cursor line is moved off the screen or
2307 * - moved at least 'scrolljump' lines and
2308 * - at least 'scrolloff' lines above and below the cursor
2309 */
2310 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002311 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (curwin->w_cursor.lnum < curwin->w_topline)
2313 scrolled = used;
2314
2315#ifdef FEAT_FOLDING
2316 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2317 {
2318 --top;
2319 ++bot;
2320 }
2321 else
2322#endif
2323 {
2324 top = curwin->w_cursor.lnum - 1;
2325 bot = curwin->w_cursor.lnum + 1;
2326 }
2327 new_topline = top + 1;
2328
2329#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002330 // "used" already contains the number of filler lines above, don't add it
2331 // again.
2332 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002333 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#endif
2335
2336 /*
2337 * Check if the lines from "top" to "bot" fit in the window. If they do,
2338 * set new_topline and advance "top" and "bot" to include more lines.
2339 */
2340 while (top > 0)
2341 {
2342#ifdef FEAT_FOLDING
2343 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002344 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 i = 1;
2346 else
2347#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002348 i = PLINES_NOFILL(top);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002349 if (top < curwin->w_topline)
2350 scrolled += i;
2351
2352 // If scrolling is needed, scroll at least 'sj' lines.
2353 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2354 && extra >= off)
2355 break;
2356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 used += i;
2358 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2359 {
2360#ifdef FEAT_FOLDING
2361 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002362 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 ++used;
2364 else
2365#endif
2366 used += plines(bot);
2367 }
2368 if (used > curwin->w_height)
2369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 extra += i;
2372 new_topline = top;
2373 --top;
2374 ++bot;
2375 }
2376
2377 /*
2378 * If we don't have enough space, put cursor in the middle.
2379 * This makes sure we get the same position when using "k" and "j"
2380 * in a small window.
2381 */
2382 if (used > curwin->w_height)
2383 scroll_cursor_halfway(FALSE);
2384 else
2385 {
2386 /*
2387 * If "always" is FALSE, only adjust topline to a lower value, higher
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002388 * value may happen with wrapping lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 */
2390 if (new_topline < curwin->w_topline || always)
2391 curwin->w_topline = new_topline;
2392 if (curwin->w_topline > curwin->w_cursor.lnum)
2393 curwin->w_topline = curwin->w_cursor.lnum;
2394#ifdef FEAT_DIFF
2395 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2396 if (curwin->w_topfill > 0 && extra > off)
2397 {
2398 curwin->w_topfill -= extra - off;
2399 if (curwin->w_topfill < 0)
2400 curwin->w_topfill = 0;
2401 }
2402 check_topfill(curwin, FALSE);
2403#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002404 // TODO: if the line doesn't fit may optimize w_skipcol
Bram Moolenaar1b73edd2022-12-03 11:51:54 +00002405 if (curwin->w_topline == curwin->w_cursor.lnum
2406 && curwin->w_skipcol >= curwin->w_cursor.col)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002407 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002409 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410#ifdef FEAT_DIFF
2411 || curwin->w_topfill != old_topfill
2412#endif
2413 )
2414 curwin->w_valid &=
2415 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2416 curwin->w_valid |= VALID_TOPLINE;
2417 }
2418}
2419
2420/*
2421 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2422 * screen lines for text lines.
2423 */
2424 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002425set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427#ifdef FEAT_DIFF
2428 wp->w_filler_rows = 0;
2429#endif
2430 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 else
2433 {
2434 wp->w_empty_rows = wp->w_height - used;
2435#ifdef FEAT_DIFF
2436 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2437 {
2438 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2439 if (wp->w_empty_rows > wp->w_filler_rows)
2440 wp->w_empty_rows -= wp->w_filler_rows;
2441 else
2442 {
2443 wp->w_filler_rows = wp->w_empty_rows;
2444 wp->w_empty_rows = 0;
2445 }
2446 }
2447#endif
2448 }
2449}
2450
2451/*
2452 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002453 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2455 * This is messy stuff!!!
2456 */
2457 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002458scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
2460 int used;
2461 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002462 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 int extra = 0;
2464 int i;
2465 linenr_T line_count;
2466 linenr_T old_topline = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002467 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 lineoff_T loff;
2469 lineoff_T boff;
2470#ifdef FEAT_DIFF
2471 int old_topfill = curwin->w_topfill;
2472 int fill_below_window;
2473#endif
2474 linenr_T old_botline = curwin->w_botline;
2475 linenr_T old_valid = curwin->w_valid;
2476 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002477 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002478 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 cln = curwin->w_cursor.lnum;
2481 if (set_topbot)
2482 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002483 int set_skipcol = FALSE;
2484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 used = 0;
2486 curwin->w_botline = cln + 1;
2487#ifdef FEAT_DIFF
2488 loff.fill = 0;
2489#endif
2490 for (curwin->w_topline = curwin->w_botline;
2491 curwin->w_topline > 1;
2492 curwin->w_topline = loff.lnum)
2493 {
2494 loff.lnum = curwin->w_topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002495 topline_back_winheight(&loff, FALSE);
2496 if (loff.height == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 break;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002498 if (used + loff.height > curwin->w_height)
2499 {
2500 if (curwin->w_p_sms && curwin->w_p_wrap)
2501 {
2502 // 'smoothscroll' and 'wrap' are set. The above line is
2503 // too long to show in its entirety, so we show just a part
2504 // of it.
2505 if (used < curwin->w_height)
2506 {
2507 int plines_offset = used + loff.height
2508 - curwin->w_height;
2509 used = curwin->w_height;
2510#ifdef FEAT_DIFF
2511 curwin->w_topfill = loff.fill;
2512#endif
2513 curwin->w_topline = loff.lnum;
2514 curwin->w_skipcol = skipcol_from_plines(
2515 curwin, plines_offset);
2516 set_skipcol = TRUE;
2517 }
2518 }
2519 break;
2520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 used += loff.height;
2522#ifdef FEAT_DIFF
2523 curwin->w_topfill = loff.fill;
2524#endif
2525 }
2526 set_empty_rows(curwin, used);
2527 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2528 if (curwin->w_topline != old_topline
2529#ifdef FEAT_DIFF
2530 || curwin->w_topfill != old_topfill
2531#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002532 || set_skipcol
2533 || curwin->w_skipcol != 0)
2534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002536 if (set_skipcol)
2537 redraw_later(UPD_NOT_VALID);
2538 else
2539 reset_skipcol();
2540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542 else
2543 validate_botline();
2544
Bram Moolenaar85a20022019-12-21 18:25:54 +01002545 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#ifdef FEAT_DIFF
2547 used = plines_nofill(cln);
2548#else
2549 validate_cheight();
2550 used = curwin->w_cline_height;
2551#endif
2552
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002553 // If the cursor is on or below botline, we will at least scroll by the
2554 // height of the cursor line, which is "used". Correct for empty lines,
2555 // which are really part of botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 if (cln >= curwin->w_botline)
2557 {
2558 scrolled = used;
2559 if (cln == curwin->w_botline)
2560 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002561 min_scrolled = scrolled;
Yee Cheng Chin361895d2022-11-19 12:25:16 +00002562 if (curwin->w_p_sms && curwin->w_p_wrap)
2563 {
2564 // 'smoothscroll' and 'wrap' are set
2565 if (cln > curwin->w_botline)
2566 // add screen lines below w_botline
2567 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
2568 min_scrolled += PLINES_NOFILL(lnum);
2569
2570 // Calculate how many screen lines the current top line of window
2571 // occupies. If it is occupying more than the entire window, we
2572 // need to scroll the additional clipped lines to scroll past the
2573 // top line before we can move on to the other lines.
2574 int top_plines =
2575#ifdef FEAT_DIFF
2576 plines_win_nofill
2577#else
2578 plines_win
2579#endif
2580 (curwin, curwin->w_topline, FALSE);
2581 int skip_lines = 0;
2582 int width1 = curwin->w_width - curwin_col_off();
2583 int width2 = width1 + curwin_col_off2();
2584 // similar formula is used in curs_columns()
2585 if (curwin->w_skipcol > width1)
2586 skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
2587 else if (curwin->w_skipcol > 0)
2588 skip_lines = 1;
2589
2590 top_plines -= skip_lines;
2591 if (top_plines > curwin->w_height)
2592 {
2593 scrolled += (top_plines - curwin->w_height);
2594 min_scrolled += (top_plines - curwin->w_height);
2595 }
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598
2599 /*
2600 * Stop counting lines to scroll when
2601 * - hitting start of the file
2602 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002603 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 * - lines between botline and cursor have been counted
2605 */
2606#ifdef FEAT_FOLDING
2607 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2608#endif
2609 {
2610 loff.lnum = cln;
2611 boff.lnum = cln;
2612 }
2613#ifdef FEAT_DIFF
2614 loff.fill = 0;
2615 boff.fill = 0;
2616 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2617 - curwin->w_filler_rows;
2618#endif
2619
2620 while (loff.lnum > 1)
2621 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002622 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2623 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002625 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2627 && loff.lnum <= curwin->w_botline
2628#ifdef FEAT_DIFF
2629 && (loff.lnum < curwin->w_botline
2630 || loff.fill >= fill_below_window)
2631#endif
2632 )
2633 break;
2634
Bram Moolenaar85a20022019-12-21 18:25:54 +01002635 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002637 if (loff.height == MAXCOL)
2638 used = MAXCOL;
2639 else
2640 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (used > curwin->w_height)
2642 break;
2643 if (loff.lnum >= curwin->w_botline
2644#ifdef FEAT_DIFF
2645 && (loff.lnum > curwin->w_botline
2646 || loff.fill <= fill_below_window)
2647#endif
2648 )
2649 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002650 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 scrolled += loff.height;
2652 if (loff.lnum == curwin->w_botline
2653#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002654 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655#endif
2656 )
2657 scrolled -= curwin->w_empty_rows;
2658 }
2659
2660 if (boff.lnum < curbuf->b_ml.ml_line_count)
2661 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 botline_forw(&boff);
2664 used += boff.height;
2665 if (used > curwin->w_height)
2666 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002667 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2668 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 extra += boff.height;
2671 if (boff.lnum >= curwin->w_botline
2672#ifdef FEAT_DIFF
2673 || (boff.lnum + 1 == curwin->w_botline
2674 && boff.fill > curwin->w_filler_rows)
2675#endif
2676 )
2677 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002678 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 scrolled += boff.height;
2680 if (boff.lnum == curwin->w_botline
2681#ifdef FEAT_DIFF
2682 && boff.fill == 0
2683#endif
2684 )
2685 scrolled -= curwin->w_empty_rows;
2686 }
2687 }
2688 }
2689 }
2690
Bram Moolenaar85a20022019-12-21 18:25:54 +01002691 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (scrolled <= 0)
2693 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 else if (used > curwin->w_height)
2696 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 else
2699 {
2700 line_count = 0;
2701#ifdef FEAT_DIFF
2702 boff.fill = curwin->w_topfill;
2703#endif
2704 boff.lnum = curwin->w_topline - 1;
2705 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2706 {
2707 botline_forw(&boff);
2708 i += boff.height;
2709 ++line_count;
2710 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002711 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 line_count = 9999;
2713 }
2714
2715 /*
2716 * Scroll up if the cursor is off the bottom of the screen a bit.
2717 * Otherwise put it at 1/2 of the screen.
2718 */
2719 if (line_count >= curwin->w_height && line_count > min_scroll)
2720 scroll_cursor_halfway(FALSE);
2721 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002722 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002723 // With 'smoothscroll' scroll at least the height of the cursor line,
2724 // unless it would move the cursor.
2725 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2726 && (curwin->w_cursor.lnum < curwin->w_topline
2727 || (curwin->w_virtcol - curwin->w_skipcol >=
2728 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002729 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002730 if (line_count > 0)
2731 {
2732 if (scrolling_screenlines(TRUE))
2733 scrollup(scrolled, TRUE); // TODO
2734 else
2735 scrollup(line_count, TRUE);
2736 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 /*
2740 * If topline didn't change we need to restore w_botline and w_empty_rows
2741 * (we changed them).
2742 * If topline did change, update_screen() will set botline.
2743 */
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002744 if (curwin->w_topline == old_topline
2745 && curwin->w_skipcol == old_skipcol
2746 && set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
2748 curwin->w_botline = old_botline;
2749 curwin->w_empty_rows = old_empty_rows;
2750 curwin->w_valid = old_valid;
2751 }
2752 curwin->w_valid |= VALID_TOPLINE;
2753}
2754
2755/*
2756 * Recompute topline to put the cursor halfway the window
2757 * If "atend" is TRUE, also put it halfway at the end of the file.
2758 */
2759 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002760scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
2762 int above = 0;
2763 linenr_T topline;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002764 colnr_T skipcol = 0;
2765 int set_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766#ifdef FEAT_DIFF
2767 int topfill = 0;
2768#endif
2769 int below = 0;
2770 int used;
2771 lineoff_T loff;
2772 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002773#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002774 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002777#ifdef FEAT_PROP_POPUP
2778 // if the width changed this needs to be updated first
2779 may_update_popup_position();
2780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2782#ifdef FEAT_FOLDING
2783 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2784#endif
2785#ifdef FEAT_DIFF
2786 used = plines_nofill(loff.lnum);
2787 loff.fill = 0;
2788 boff.fill = 0;
2789#else
2790 used = plines(loff.lnum);
2791#endif
2792 topline = loff.lnum;
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002793
2794 int half_height = 0;
2795 int smooth_scroll = FALSE;
2796 if (curwin->w_p_sms && curwin->w_p_wrap)
2797 {
2798 // 'smoothscroll' and 'wrap' are set
2799 smooth_scroll = TRUE;
2800 half_height = (curwin->w_height - used) / 2;
2801 used = 0;
2802 }
2803
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 while (topline > 1)
2805 {
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002806 // If using smoothscroll, we can precisely scroll to the
2807 // exact point where the cursor is halfway down the screen.
2808 if (smooth_scroll)
2809 {
2810 topline_back_winheight(&loff, FALSE);
2811 if (loff.height == MAXCOL)
2812 break;
2813 else
2814 used += loff.height;
2815 if (used > half_height)
2816 {
2817 if (used - loff.height < half_height)
2818 {
2819 int plines_offset = used - half_height;
2820 loff.height -= plines_offset;
2821 used = half_height;
2822
2823 topline = loff.lnum;
2824#ifdef FEAT_DIFF
2825 topfill = loff.fill;
2826#endif
2827 skipcol = skipcol_from_plines(curwin, plines_offset);
2828 set_skipcol = TRUE;
2829 }
2830 break;
2831 }
2832 topline = loff.lnum;
2833#ifdef FEAT_DIFF
2834 topfill = loff.fill;
2835#endif
2836 continue;
2837 }
2838
2839 // If not using smoothscroll, we have to iteratively find how many
2840 // lines to scroll down to roughly fit the cursor.
2841 // This may not be right in the middle if the lines' physical height >
2842 // 1 (e.g. 'wrap' is on).
2843
Bram Moolenaar85a20022019-12-21 18:25:54 +01002844 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
2846 if (boff.lnum < curbuf->b_ml.ml_line_count)
2847 {
2848 botline_forw(&boff);
2849 used += boff.height;
2850 if (used > curwin->w_height)
2851 break;
2852 below += boff.height;
2853 }
2854 else
2855 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002856 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (atend)
2858 ++used;
2859 }
2860 }
2861
Bram Moolenaar85a20022019-12-21 18:25:54 +01002862 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
2864 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002865 if (loff.height == MAXCOL)
2866 used = MAXCOL;
2867 else
2868 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 if (used > curwin->w_height)
2870 break;
2871 above += loff.height;
2872 topline = loff.lnum;
2873#ifdef FEAT_DIFF
2874 topfill = loff.fill;
2875#endif
2876 }
2877 }
2878#ifdef FEAT_FOLDING
2879 if (!hasFolding(topline, &curwin->w_topline, NULL))
2880#endif
Bram Moolenaardb4d88c2022-12-31 15:13:22 +00002881 {
2882 if (curwin->w_topline != topline
2883 || set_skipcol
2884 || curwin->w_skipcol != 0)
2885 {
2886 curwin->w_topline = topline;
2887 if (set_skipcol)
2888 {
2889 curwin->w_skipcol = skipcol;
2890 redraw_later(UPD_NOT_VALID);
2891 }
2892 else
2893 reset_skipcol();
2894 }
2895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896#ifdef FEAT_DIFF
2897 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002898 if (old_topline > curwin->w_topline + curwin->w_height)
2899 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 check_topfill(curwin, FALSE);
2901#endif
2902 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2903 curwin->w_valid |= VALID_TOPLINE;
2904}
2905
2906/*
2907 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002908 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 * If not possible, put it at the same position as scroll_cursor_halfway().
2910 * When called topline must be valid!
2911 */
2912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002913cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002917 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 linenr_T botline;
2919 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002920 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002922 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
2924 /*
2925 * How many lines we would like to have above/below the cursor depends on
2926 * whether the first/last line of the file is on screen.
2927 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002928 above_wanted = so;
2929 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002930 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 {
2932 above_wanted = mouse_dragging - 1;
2933 below_wanted = mouse_dragging - 1;
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (curwin->w_topline == 1)
2936 {
2937 above_wanted = 0;
2938 max_off = curwin->w_height / 2;
2939 if (below_wanted > max_off)
2940 below_wanted = max_off;
2941 }
2942 validate_botline();
2943 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002944 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 {
2946 below_wanted = 0;
2947 max_off = (curwin->w_height - 1) / 2;
2948 if (above_wanted > max_off)
2949 above_wanted = max_off;
2950 }
2951
2952 /*
2953 * If there are sufficient file-lines above and below the cursor, we can
2954 * return now.
2955 */
2956 cln = curwin->w_cursor.lnum;
2957 if (cln >= curwin->w_topline + above_wanted
2958 && cln < curwin->w_botline - below_wanted
2959#ifdef FEAT_FOLDING
2960 && !hasAnyFolding(curwin)
2961#endif
2962 )
2963 return;
2964
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002965 if (curwin->w_p_sms && !curwin->w_p_wrap)
2966 {
2967 // 'smoothscroll is active
2968 if (curwin->w_cline_height == curwin->w_height)
2969 {
2970 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002971 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002972 return;
2973 }
2974 // TODO: If the cursor line doesn't fit in the window then only adjust
2975 // w_skipcol.
2976 }
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /*
2979 * Narrow down the area where the cursor can be put by taking lines from
2980 * the top and the bottom until:
2981 * - the desired context lines are found
2982 * - the lines from the top is past the lines from the bottom
2983 */
2984 topline = curwin->w_topline;
2985 botline = curwin->w_botline - 1;
2986#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002987 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 above = curwin->w_topfill;
2989 below = curwin->w_filler_rows;
2990#endif
2991 while ((above < above_wanted || below < below_wanted) && topline < botline)
2992 {
2993 if (below < below_wanted && (below <= above || above >= above_wanted))
2994 {
2995#ifdef FEAT_FOLDING
2996 if (hasFolding(botline, &botline, NULL))
2997 ++below;
2998 else
2999#endif
3000 below += plines(botline);
3001 --botline;
3002 }
3003 if (above < above_wanted && (above < below || below >= below_wanted))
3004 {
3005#ifdef FEAT_FOLDING
3006 if (hasFolding(topline, NULL, &topline))
3007 ++above;
3008 else
3009#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003010 above += PLINES_NOFILL(topline);
3011#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003012 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (topline < botline)
3014 above += diff_check_fill(curwin, topline + 1);
3015#endif
3016 ++topline;
3017 }
3018 }
3019 if (topline == botline || botline == 0)
3020 curwin->w_cursor.lnum = topline;
3021 else if (topline > botline)
3022 curwin->w_cursor.lnum = botline;
3023 else
3024 {
3025 if (cln < topline && curwin->w_topline > 1)
3026 {
3027 curwin->w_cursor.lnum = topline;
3028 curwin->w_valid &=
3029 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3030 }
3031 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3032 {
3033 curwin->w_cursor.lnum = botline;
3034 curwin->w_valid &=
3035 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
3036 }
3037 }
3038 curwin->w_valid |= VALID_TOPLINE;
3039}
3040
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003041static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00003044 * Move screen "count" pages up ("dir" is BACKWARD) or down ("dir" is FORWARD)
3045 * and update the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00003047 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 */
3049 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003050onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 long n;
3053 int retval = OK;
3054 lineoff_T loff;
3055 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01003056 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaar85a20022019-12-21 18:25:54 +01003058 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
3060 beep_flush();
3061 return FAIL;
3062 }
3063
3064 for ( ; count > 0; --count)
3065 {
3066 validate_botline();
3067 /*
3068 * It's an error to move a page up when the first line is already on
3069 * the screen. It's an error to move a page down when the last line
3070 * is on the screen and the topline is 'scrolloff' lines from the
3071 * last line.
3072 */
3073 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01003074 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 && curwin->w_botline > curbuf->b_ml.ml_line_count)
3076 : (curwin->w_topline == 1
3077#ifdef FEAT_DIFF
3078 && curwin->w_topfill ==
3079 diff_check_fill(curwin, curwin->w_topline)
3080#endif
3081 ))
3082 {
3083 beep_flush();
3084 retval = FAIL;
3085 break;
3086 }
3087
3088#ifdef FEAT_DIFF
3089 loff.fill = 0;
3090#endif
3091 if (dir == FORWARD)
3092 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003093 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003095 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003096 if (p_window <= 2)
3097 ++curwin->w_topline;
3098 else
3099 curwin->w_topline += p_window - 2;
3100 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
3101 curwin->w_topline = curbuf->b_ml.ml_line_count;
3102 curwin->w_cursor.lnum = curwin->w_topline;
3103 }
3104 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3105 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003106 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 curwin->w_topline = curbuf->b_ml.ml_line_count;
3108#ifdef FEAT_DIFF
3109 curwin->w_topfill = 0;
3110#endif
3111 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
3112 }
3113 else
3114 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003115 // For the overlap, start with the line just below the window
3116 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 loff.lnum = curwin->w_botline;
3118#ifdef FEAT_DIFF
3119 loff.fill = diff_check_fill(curwin, loff.lnum)
3120 - curwin->w_filler_rows;
3121#endif
3122 get_scroll_overlap(&loff, -1);
3123 curwin->w_topline = loff.lnum;
3124#ifdef FEAT_DIFF
3125 curwin->w_topfill = loff.fill;
3126 check_topfill(curwin, FALSE);
3127#endif
3128 curwin->w_cursor.lnum = curwin->w_topline;
3129 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
3130 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3131 }
3132 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003133 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 {
3135#ifdef FEAT_DIFF
3136 if (curwin->w_topline == 1)
3137 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003138 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 max_topfill();
3140 continue;
3141 }
3142#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01003143 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003144 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003145 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003146 if (p_window <= 2)
3147 --curwin->w_topline;
3148 else
3149 curwin->w_topline -= p_window - 2;
3150 if (curwin->w_topline < 1)
3151 curwin->w_topline = 1;
3152 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
3153 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3154 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3155 continue;
3156 }
3157
Bram Moolenaar85a20022019-12-21 18:25:54 +01003158 // Find the line at the top of the window that is going to be the
3159 // line at the bottom of the window. Make sure this results in
3160 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 loff.lnum = curwin->w_topline - 1;
3162#ifdef FEAT_DIFF
3163 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
3164 - curwin->w_topfill;
3165#endif
3166 get_scroll_overlap(&loff, 1);
3167
3168 if (loff.lnum >= curbuf->b_ml.ml_line_count)
3169 {
3170 loff.lnum = curbuf->b_ml.ml_line_count;
3171#ifdef FEAT_DIFF
3172 loff.fill = 0;
3173 }
3174 else
3175 {
3176 botline_topline(&loff);
3177#endif
3178 }
3179 curwin->w_cursor.lnum = loff.lnum;
3180
Bram Moolenaar85a20022019-12-21 18:25:54 +01003181 // Find the line just above the new topline to get the right line
3182 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 n = 0;
3184 while (n <= curwin->w_height && loff.lnum >= 1)
3185 {
3186 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01003187 if (loff.height == MAXCOL)
3188 n = MAXCOL;
3189 else
3190 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003192 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
3194 curwin->w_topline = 1;
3195#ifdef FEAT_DIFF
3196 max_topfill();
3197#endif
3198 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3199 }
3200 else
3201 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003202 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#ifdef FEAT_DIFF
3204 topline_botline(&loff);
3205#endif
3206 botline_forw(&loff);
3207 botline_forw(&loff);
3208#ifdef FEAT_DIFF
3209 botline_topline(&loff);
3210#endif
3211#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003212 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3214#endif
3215
Bram Moolenaar85a20022019-12-21 18:25:54 +01003216 // Always scroll at least one line. Avoid getting stuck on
3217 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (loff.lnum >= curwin->w_topline
3219#ifdef FEAT_DIFF
3220 && (loff.lnum > curwin->w_topline
3221 || loff.fill >= curwin->w_topfill)
3222#endif
3223 )
3224 {
3225#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003226 // First try using the maximum number of filler lines. If
3227 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 loff.fill = curwin->w_topfill;
3229 if (curwin->w_topfill < diff_check_fill(curwin,
3230 curwin->w_topline))
3231 max_topfill();
3232 if (curwin->w_topfill == loff.fill)
3233#endif
3234 {
3235 --curwin->w_topline;
3236#ifdef FEAT_DIFF
3237 curwin->w_topfill = 0;
3238#endif
3239 }
3240 comp_botline(curwin);
3241 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003242 curwin->w_valid &=
3243 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245 else
3246 {
3247 curwin->w_topline = loff.lnum;
3248#ifdef FEAT_DIFF
3249 curwin->w_topfill = loff.fill;
3250 check_topfill(curwin, FALSE);
3251#endif
3252 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3253 }
3254 }
3255 }
3256 }
3257#ifdef FEAT_FOLDING
3258 foldAdjustCursor();
3259#endif
3260 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003261 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003262 if (retval == OK)
3263 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3265
Bram Moolenaar907dad72018-07-10 15:07:15 +02003266 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003268 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3269 // But make sure we scroll at least one line (happens with mix of long
3270 // wrapping lines and non-wrapping line).
3271 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003273 scroll_cursor_top(1, FALSE);
3274 if (curwin->w_topline <= old_topline
3275 && old_topline < curbuf->b_ml.ml_line_count)
3276 {
3277 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003279 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3280#endif
3281 }
3282 }
3283#ifdef FEAT_FOLDING
3284 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003289 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 return retval;
3291}
3292
3293/*
3294 * Decide how much overlap to use for page-up or page-down scrolling.
3295 * This is symmetric, so that doing both keeps the same lines displayed.
3296 * Three lines are examined:
3297 *
3298 * before CTRL-F after CTRL-F / before CTRL-B
3299 * etc. l1
3300 * l1 last but one line ------------
3301 * l2 last text line l2 top text line
3302 * ------------- l3 second text line
3303 * l3 etc.
3304 */
3305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003306get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 int h1, h2, h3, h4;
3309 int min_height = curwin->w_height - 2;
3310 lineoff_T loff0, loff1, loff2;
3311
3312#ifdef FEAT_DIFF
3313 if (lp->fill > 0)
3314 lp->height = 1;
3315 else
3316 lp->height = plines_nofill(lp->lnum);
3317#else
3318 lp->height = plines(lp->lnum);
3319#endif
3320 h1 = lp->height;
3321 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003322 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
3324 loff0 = *lp;
3325 if (dir > 0)
3326 botline_forw(lp);
3327 else
3328 topline_back(lp);
3329 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003330 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003332 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 return;
3334 }
3335
3336 loff1 = *lp;
3337 if (dir > 0)
3338 botline_forw(lp);
3339 else
3340 topline_back(lp);
3341 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003342 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003344 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 return;
3346 }
3347
3348 loff2 = *lp;
3349 if (dir > 0)
3350 botline_forw(lp);
3351 else
3352 topline_back(lp);
3353 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003354 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003355 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003357 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358}
3359
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360/*
3361 * Scroll 'scroll' lines up or down.
3362 */
3363 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003364halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 long scrolled = 0;
3367 int i;
3368 int n;
3369 int room;
3370
3371 if (Prenum)
3372 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3373 curwin->w_height : Prenum;
3374 n = (curwin->w_p_scr <= curwin->w_height) ?
3375 curwin->w_p_scr : curwin->w_height;
3376
Bram Moolenaard5d37532017-03-27 23:02:07 +02003377 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 validate_botline();
3379 room = curwin->w_empty_rows;
3380#ifdef FEAT_DIFF
3381 room += curwin->w_filler_rows;
3382#endif
3383 if (flag)
3384 {
3385 /*
3386 * scroll the text up
3387 */
3388 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3389 {
3390#ifdef FEAT_DIFF
3391 if (curwin->w_topfill > 0)
3392 {
3393 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003394 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 --curwin->w_topfill;
3396 }
3397 else
3398#endif
3399 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003400 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 n -= i;
3402 if (n < 0 && scrolled > 0)
3403 break;
3404#ifdef FEAT_FOLDING
3405 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3406#endif
3407 ++curwin->w_topline;
3408#ifdef FEAT_DIFF
3409 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3410#endif
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3413 {
3414 ++curwin->w_cursor.lnum;
3415 curwin->w_valid &=
3416 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
3419 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3420 scrolled += i;
3421
3422 /*
3423 * Correct w_botline for changed w_topline.
3424 * Won't work when there are filler lines.
3425 */
3426#ifdef FEAT_DIFF
3427 if (curwin->w_p_diff)
3428 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3429 else
3430#endif
3431 {
3432 room += i;
3433 do
3434 {
3435 i = plines(curwin->w_botline);
3436 if (i > room)
3437 break;
3438#ifdef FEAT_FOLDING
3439 (void)hasFolding(curwin->w_botline, NULL,
3440 &curwin->w_botline);
3441#endif
3442 ++curwin->w_botline;
3443 room -= i;
3444 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3445 }
3446 }
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 /*
3449 * When hit bottom of the file: move cursor down.
3450 */
3451 if (n > 0)
3452 {
3453# ifdef FEAT_FOLDING
3454 if (hasAnyFolding(curwin))
3455 {
3456 while (--n >= 0
3457 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3458 {
3459 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3460 &curwin->w_cursor.lnum);
3461 ++curwin->w_cursor.lnum;
3462 }
3463 }
3464 else
3465# endif
3466 curwin->w_cursor.lnum += n;
3467 check_cursor_lnum();
3468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 else
3471 {
3472 /*
3473 * scroll the text down
3474 */
3475 while (n > 0 && curwin->w_topline > 1)
3476 {
3477#ifdef FEAT_DIFF
3478 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3479 {
3480 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003481 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 ++curwin->w_topfill;
3483 }
3484 else
3485#endif
3486 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003487 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 n -= i;
3489 if (n < 0 && scrolled > 0)
3490 break;
3491 --curwin->w_topline;
3492#ifdef FEAT_FOLDING
3493 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3494#endif
3495#ifdef FEAT_DIFF
3496 curwin->w_topfill = 0;
3497#endif
3498 }
3499 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3500 VALID_BOTLINE|VALID_BOTLINE_AP);
3501 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (curwin->w_cursor.lnum > 1)
3503 {
3504 --curwin->w_cursor.lnum;
3505 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 /*
3510 * When hit top of the file: move cursor up.
3511 */
3512 if (n > 0)
3513 {
3514 if (curwin->w_cursor.lnum <= (linenr_T)n)
3515 curwin->w_cursor.lnum = 1;
3516 else
3517# ifdef FEAT_FOLDING
3518 if (hasAnyFolding(curwin))
3519 {
3520 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3521 {
3522 --curwin->w_cursor.lnum;
3523 (void)hasFolding(curwin->w_cursor.lnum,
3524 &curwin->w_cursor.lnum, NULL);
3525 }
3526 }
3527 else
3528# endif
3529 curwin->w_cursor.lnum -= n;
3530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003533 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 foldAdjustCursor();
3535# endif
3536#ifdef FEAT_DIFF
3537 check_topfill(curwin, !flag);
3538#endif
3539 cursor_correct();
3540 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003541 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003543
Bram Moolenaar860cae12010-06-05 23:22:07 +02003544 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003545do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003546{
3547 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003548 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003549 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003550 colnr_T curswant = curwin->w_curswant;
3551 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003552 win_T *old_curwin = curwin;
3553 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003554 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003555 int old_VIsual_select = VIsual_select;
3556 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003557
3558 /*
3559 * loop through the cursorbound windows
3560 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003561 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003562 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563 {
3564 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003565 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003566 if (curwin != old_curwin && curwin->w_p_crb)
3567 {
3568# ifdef FEAT_DIFF
3569 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003570 curwin->w_cursor.lnum =
3571 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003572 else
3573# endif
3574 curwin->w_cursor.lnum = line;
3575 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003576 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003577 curwin->w_curswant = curswant;
3578 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003579
Bram Moolenaar85a20022019-12-21 18:25:54 +01003580 // Make sure the cursor is in a valid position. Temporarily set
3581 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003582 restart_edit_save = restart_edit;
3583 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003584 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003585
3586 // Avoid a scroll here for the cursor position, 'scrollbind' is
3587 // more important.
3588 if (!curwin->w_p_scb)
3589 validate_cursor();
3590
Bram Moolenaar61452852011-02-01 18:01:11 +01003591 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003592 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593 if (has_mbyte)
3594 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003595 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003596
Bram Moolenaar85a20022019-12-21 18:25:54 +01003597 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003598 if (!curwin->w_p_scb)
3599 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003601 }
3602 }
3603
3604 /*
3605 * reset current-window
3606 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003607 VIsual_select = old_VIsual_select;
3608 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003609 curwin = old_curwin;
3610 curbuf = old_curbuf;
3611}