blob: aac2c243d8c90d5fcb48eced434c760634feb045 [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 Moolenaarf32fb932022-11-17 11:34:38 +0000225 * Set curwin->s_skipcol to zero and redraw later if needed.
226 */
227 static void
228reset_skipcol(void)
229{
230 if (curwin->w_skipcol != 0)
231 {
232 curwin->w_skipcol = 0;
233
234 // Should use the least expensive way that displays all that changed.
235 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
236 // enough when the top line gets another screen line.
237 redraw_later(UPD_SOME_VALID);
238 }
239}
240
241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 * Update curwin->w_topline and redraw if necessary.
243 * Used to update the screen before printing a message.
244 */
245 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100246update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247{
248 update_topline();
249 if (must_redraw)
250 update_screen(0);
251}
252
253/*
254 * Update curwin->w_topline to move the cursor onto the screen.
255 */
256 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100257update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 long line_count;
260 int halfheight;
261 int n;
262 linenr_T old_topline;
263#ifdef FEAT_DIFF
264 int old_topfill;
265#endif
266#ifdef FEAT_FOLDING
267 linenr_T lnum;
268#endif
269 int check_topline = FALSE;
270 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100271 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100272 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100274 // Cursor is updated instead when this is TRUE for 'splitkeep'.
275 if (skip_update_topline)
276 return;
277
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 // If there is no valid screen and when the window height is zero just use
279 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200280 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200281 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100282 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200283 curwin->w_topline = curwin->w_cursor.lnum;
284 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200285 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200286 return;
287 }
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 check_cursor_moved(curwin);
290 if (curwin->w_valid & VALID_TOPLINE)
291 return;
292
Bram Moolenaar85a20022019-12-21 18:25:54 +0100293 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000294 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100295 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296
297 old_topline = curwin->w_topline;
298#ifdef FEAT_DIFF
299 old_topfill = curwin->w_topfill;
300#endif
301
302 /*
303 * If the buffer is empty, always set topline to 1.
304 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100305 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 {
307 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100308 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 curwin->w_botline = 2;
311 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 }
314
315 /*
316 * If the cursor is above or near the top of the window, scroll the window
317 * to show the line the cursor is in, with 'scrolloff' context.
318 */
319 else
320 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100321 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100323 // If the cursor is above topline, scrolling is always needed.
324 // If the cursor is far below topline and there is no folding,
325 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 if (curwin->w_cursor.lnum < curwin->w_topline)
327 check_topline = TRUE;
328 else if (check_top_offset())
329 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100330 else if (curwin->w_cursor.lnum == curwin->w_topline)
331 {
332 colnr_T vcol;
333
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000334 // Check that the cursor position is visible. Add columns for
335 // the smoothscroll marker "<<<" displayed in the top-left if
336 // needed.
Bram Moolenaar46b54742022-10-06 15:46:49 +0100337 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +0000338 int smoothscroll_overlap = smoothscroll_marker_overlap(
339 curwin_col_off() - curwin_col_off2());
340 if (curwin->w_skipcol + smoothscroll_overlap > vcol)
Bram Moolenaar46b54742022-10-06 15:46:49 +0100341 check_topline = TRUE;
342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
344#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100345 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
347 curwin->w_topline))
348 check_topline = TRUE;
349#endif
350
351 if (check_topline)
352 {
353 halfheight = curwin->w_height / 2 - 1;
354 if (halfheight < 2)
355 halfheight = 2;
356
357#ifdef FEAT_FOLDING
358 if (hasAnyFolding(curwin))
359 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100360 // Count the number of logical lines between the cursor and
361 // topline + scrolloff (approximation of how much will be
362 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 n = 0;
364 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100365 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 {
367 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100368 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
370 break;
371 (void)hasFolding(lnum, NULL, &lnum);
372 }
373 }
374 else
375#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100376 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaar85a20022019-12-21 18:25:54 +0100378 // If we weren't very close to begin with, we scroll to put the
379 // cursor in the middle of the window. Otherwise put the cursor
380 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 if (n >= halfheight)
382 scroll_cursor_halfway(FALSE);
383 else
384 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000385 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 check_botline = TRUE;
387 }
388 }
389
390 else
391 {
392#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100393 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
395#endif
396 check_botline = TRUE;
397 }
398 }
399
400 /*
401 * If the cursor is below the bottom of the window, scroll the window
402 * to put the cursor on the window.
403 * When w_botline is invalid, recompute it first, to avoid a redraw later.
404 * If w_botline was approximated, we might need a redraw later in a few
405 * cases, but we don't want to spend (a lot of) time recomputing w_botline
406 * for every small change.
407 */
408 if (check_botline)
409 {
410 if (!(curwin->w_valid & VALID_BOTLINE_AP))
411 validate_botline();
412
413 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
414 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000415 if (curwin->w_cursor.lnum < curwin->w_botline)
416 {
417 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100418 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#ifdef FEAT_FOLDING
420 || hasAnyFolding(curwin)
421#endif
422 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 lineoff_T loff;
425
Bram Moolenaar85a20022019-12-21 18:25:54 +0100426 // Cursor is (a few lines) above botline, check if there are
427 // 'scrolloff' window lines below the cursor. If not, need to
428 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 n = curwin->w_empty_rows;
430 loff.lnum = curwin->w_cursor.lnum;
431#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100432 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
434#endif
435#ifdef FEAT_DIFF
436 loff.fill = 0;
437 n += curwin->w_filler_rows;
438#endif
439 loff.height = 0;
440 while (loff.lnum < curwin->w_botline
441#ifdef FEAT_DIFF
442 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
443#endif
444 )
445 {
446 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100447 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 break;
449 botline_forw(&loff);
450 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100451 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100452 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000454 }
455 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100456 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000457 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 }
459 if (check_botline)
460 {
461#ifdef FEAT_FOLDING
462 if (hasAnyFolding(curwin))
463 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100464 // Count the number of logical lines between the cursor and
465 // botline - scrolloff (approximation of how much will be
466 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 line_count = 0;
468 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100469 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {
471 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100472 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 if (lnum <= 0 || line_count > curwin->w_height + 1)
474 break;
475 (void)hasFolding(lnum, &lnum, NULL);
476 }
477 }
478 else
479#endif
480 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100481 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000483 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 else
485 scroll_cursor_halfway(FALSE);
486 }
487 }
488 }
489 curwin->w_valid |= VALID_TOPLINE;
490
491 /*
492 * Need to redraw when topline changed.
493 */
494 if (curwin->w_topline != old_topline
495#ifdef FEAT_DIFF
496 || curwin->w_topfill != old_topfill
497#endif
498 )
499 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100500 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000501 redraw_later(UPD_VALID);
502 reset_skipcol();
503
Bram Moolenaar85a20022019-12-21 18:25:54 +0100504 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 if (curwin->w_cursor.lnum == curwin->w_topline)
506 validate_cursor();
507 }
508
Bram Moolenaar375e3392019-01-31 18:26:10 +0100509 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510}
511
512/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000513 * Return the scrolljump value to use for the current window.
514 * When 'scrolljump' is positive use it as-is.
515 * When 'scrolljump' is negative use it as a percentage of the window height.
516 */
517 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100518scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000519{
520 if (p_sj >= 0)
521 return (int)p_sj;
522 return (curwin->w_height * -p_sj) / 100;
523}
524
525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
527 * current window.
528 */
529 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100530check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 lineoff_T loff;
533 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100534 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
Bram Moolenaar375e3392019-01-31 18:26:10 +0100536 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537#ifdef FEAT_FOLDING
538 || hasAnyFolding(curwin)
539#endif
540 )
541 {
542 loff.lnum = curwin->w_cursor.lnum;
543#ifdef FEAT_DIFF
544 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100545 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546#else
547 n = 0;
548#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100549 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100550 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 {
552 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100553 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (loff.lnum < curwin->w_topline
555#ifdef FEAT_DIFF
556 || (loff.lnum == curwin->w_topline && loff.fill > 0)
557#endif
558 )
559 break;
560 n += loff.height;
561 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100562 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 return TRUE;
564 }
565 return FALSE;
566}
567
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100568/*
569 * Update w_curswant.
570 */
571 void
572update_curswant_force(void)
573{
574 validate_virtcol();
575 curwin->w_curswant = curwin->w_virtcol
576#ifdef FEAT_PROP_POPUP
577 - curwin->w_virtcol_first_char
578#endif
579 ;
580 curwin->w_set_curswant = FALSE;
581}
582
583/*
584 * Update w_curswant if w_set_curswant is set.
585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100587update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100590 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591}
592
593/*
594 * Check if the cursor has moved. Set the w_valid flag accordingly.
595 */
596 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100597check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
600 {
601 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100602 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
603 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 wp->w_valid_cursor = wp->w_cursor;
605 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100606 wp->w_valid_skipcol = wp->w_skipcol;
607 }
608 else if (wp->w_skipcol != wp->w_valid_skipcol)
609 {
610 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
611 |VALID_CHEIGHT|VALID_CROW
612 |VALID_BOTLINE|VALID_BOTLINE_AP);
613 wp->w_valid_cursor = wp->w_cursor;
614 wp->w_valid_leftcol = wp->w_leftcol;
615 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
617 else if (wp->w_cursor.col != wp->w_valid_cursor.col
618 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100619 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {
621 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
622 wp->w_valid_cursor.col = wp->w_cursor.col;
623 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
626}
627
628/*
629 * Call this function when some window settings have changed, which require
630 * the cursor position, botline and topline to be recomputed and the window to
631 * be redrawn. E.g, when changing the 'wrap' option or folding.
632 */
633 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100634changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 changed_window_setting_win(curwin);
637}
638
639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 wp->w_lines_valid = 0;
643 changed_line_abv_curs_win(wp);
644 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100645 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646}
647
648/*
649 * Set wp->w_topline to a certain number.
650 */
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200654#ifdef FEAT_DIFF
655 linenr_T prev_topline = wp->w_topline;
656#endif
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100659 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
661#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100662 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100664 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
665 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000667 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200669 if (lnum != prev_topline)
670 // Keep the filler lines when the topline didn't change.
671 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#endif
673 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100674 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100675 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676}
677
678/*
679 * Call this function when the length of the cursor line (in screen
680 * characters) has changed, and the change is before the cursor.
681 * Need to take care of w_botline separately!
682 */
683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100684changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
687 |VALID_CHEIGHT|VALID_TOPLINE);
688}
689
690 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100691changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
694 |VALID_CHEIGHT|VALID_TOPLINE);
695}
696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697/*
698 * Call this function when the length of a line (in screen characters) above
699 * the cursor have changed.
700 * Need to take care of w_botline separately!
701 */
702 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100703changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
705 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
706 |VALID_CHEIGHT|VALID_TOPLINE);
707}
708
709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100710changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
713 |VALID_CHEIGHT|VALID_TOPLINE);
714}
715
716/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100717 * Display of line has changed for "buf", invalidate cursor position and
718 * w_botline.
719 */
720 void
721changed_line_display_buf(buf_T *buf)
722{
723 win_T *wp;
724
725 FOR_ALL_WINDOWS(wp)
726 if (wp->w_buffer == buf)
727 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
728 |VALID_CROW|VALID_CHEIGHT
729 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
730}
731
732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 * Make sure the value of curwin->w_botline is valid.
734 */
735 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100736validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100738 validate_botline_win(curwin);
739}
740
741/*
742 * Make sure the value of wp->w_botline is valid.
743 */
744 void
745validate_botline_win(win_T *wp)
746{
747 if (!(wp->w_valid & VALID_BOTLINE))
748 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749}
750
751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 * Mark curwin->w_botline as invalid (because of some change in the buffer).
753 */
754 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100755invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756{
757 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
758}
759
760 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100761invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
763 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
764}
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100767approximate_botline_win(
768 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
770 wp->w_valid &= ~VALID_BOTLINE;
771}
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773/*
774 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
775 */
776 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100777cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
779 check_cursor_moved(curwin);
780 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
781 (VALID_WROW|VALID_WCOL));
782}
783
784/*
785 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
786 * w_topline must be valid, you may need to call update_topline() first!
787 */
788 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100789validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100791 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 check_cursor_moved(curwin);
793 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
794 curs_columns(TRUE);
795}
796
797#if defined(FEAT_GUI) || defined(PROTO)
798/*
799 * validate w_cline_row.
800 */
801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 /*
805 * First make sure that w_topline is valid (after moving the cursor).
806 */
807 update_topline();
808 check_cursor_moved(curwin);
809 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100810 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811}
812#endif
813
814/*
815 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200816 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 */
818 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 linenr_T lnum;
822 int i;
823 int all_invalid;
824 int valid;
825#ifdef FEAT_FOLDING
826 long fold_count;
827#endif
828
Bram Moolenaar85a20022019-12-21 18:25:54 +0100829 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 all_invalid = (!redrawing()
831 || wp->w_lines_valid == 0
832 || wp->w_lines[0].wl_lnum > wp->w_topline);
833 i = 0;
834 wp->w_cline_row = 0;
835 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
836 {
837 valid = FALSE;
838 if (!all_invalid && i < wp->w_lines_valid)
839 {
840 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100841 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (wp->w_lines[i].wl_lnum == lnum)
843 {
844#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100845 // Check for newly inserted lines below this row, in which
846 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (!wp->w_buffer->b_mod_set
848 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
849 || wp->w_buffer->b_mod_top
850 > wp->w_lines[i].wl_lastlnum + 1)
851#endif
852 valid = TRUE;
853 }
854 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100855 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857 if (valid
858#ifdef FEAT_DIFF
859 && (lnum != wp->w_topline || !wp->w_p_diff)
860#endif
861 )
862 {
863#ifdef FEAT_FOLDING
864 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100865 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if (lnum > wp->w_cursor.lnum)
867 break;
868#else
869 ++lnum;
870#endif
871 wp->w_cline_row += wp->w_lines[i].wl_size;
872 }
873 else
874 {
875#ifdef FEAT_FOLDING
876 fold_count = foldedCount(wp, lnum, NULL);
877 if (fold_count)
878 {
879 lnum += fold_count;
880 if (lnum > wp->w_cursor.lnum)
881 break;
882 ++wp->w_cline_row;
883 }
884 else
885#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100886 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100887 wp->w_cline_row += plines_correct_topline(wp, lnum);
888 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 }
891 }
892
893 check_cursor_moved(wp);
894 if (!(wp->w_valid & VALID_CHEIGHT))
895 {
896 if (all_invalid
897 || i == wp->w_lines_valid
898 || (i < wp->w_lines_valid
899 && (!wp->w_lines[i].wl_valid
900 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
901 {
902#ifdef FEAT_DIFF
903 if (wp->w_cursor.lnum == wp->w_topline)
904 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
905 TRUE) + wp->w_topfill;
906 else
907#endif
908 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
909#ifdef FEAT_FOLDING
910 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
911 NULL, NULL, TRUE, NULL);
912#endif
913 }
914 else if (i > wp->w_lines_valid)
915 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100916 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 wp->w_cline_height = 0;
918#ifdef FEAT_FOLDING
919 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
920 NULL, NULL, TRUE, NULL);
921#endif
922 }
923 else
924 {
925 wp->w_cline_height = wp->w_lines[i].wl_size;
926#ifdef FEAT_FOLDING
927 wp->w_cline_folded = wp->w_lines[i].wl_folded;
928#endif
929 }
930 }
931
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100932 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934}
935
936/*
937 * Validate curwin->w_virtcol only.
938 */
939 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100940validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
942 validate_virtcol_win(curwin);
943}
944
945/*
946 * Validate wp->w_virtcol only.
947 */
948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100949validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 check_cursor_moved(wp);
952 if (!(wp->w_valid & VALID_VIRTCOL))
953 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100954#ifdef FEAT_PROP_POPUP
955 wp->w_virtcol_first_char = 0;
956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000958#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100959 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000960#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100961 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963}
964
965/*
966 * Validate curwin->w_cline_height only.
967 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100969validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 check_cursor_moved(curwin);
972 if (!(curwin->w_valid & VALID_CHEIGHT))
973 {
974#ifdef FEAT_DIFF
975 if (curwin->w_cursor.lnum == curwin->w_topline)
976 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
977 + curwin->w_topfill;
978 else
979#endif
980 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
981#ifdef FEAT_FOLDING
982 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
983#endif
984 curwin->w_valid |= VALID_CHEIGHT;
985 }
986}
987
988/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000989 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 */
991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100992validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 colnr_T off;
995 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100996 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 validate_virtcol();
999 if (!(curwin->w_valid & VALID_WCOL))
1000 {
1001 col = curwin->w_virtcol;
1002 off = curwin_col_off();
1003 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +02001004 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar85a20022019-12-21 18:25:54 +01001006 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +00001007 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +02001008 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +01001009 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001010 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +02001011 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001012 if (col > (int)curwin->w_leftcol)
1013 col -= curwin->w_leftcol;
1014 else
1015 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +00001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001019#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +01001020 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +01001021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023}
1024
1025/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001026 * Compute offset of a window, occupied by absolute or relative line number,
1027 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 */
1029 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001030win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar64486672010-05-16 15:46:46 +02001032 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#ifdef FEAT_FOLDING
1035 + wp->w_p_fdc
1036#endif
1037#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001038 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039#endif
1040 );
1041}
1042
1043 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001044curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 return win_col_off(curwin);
1047}
1048
1049/*
1050 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001051 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1052 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 */
1054 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001055win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaar64486672010-05-16 15:46:46 +02001057 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001058 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 return 0;
1060}
1061
1062 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
1065 return win_col_off2(curwin);
1066}
1067
1068/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001069 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 * Also updates curwin->w_wrow and curwin->w_cline_row.
1071 * Also updates curwin->w_leftcol.
1072 */
1073 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001074curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001075 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001078 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 int off_left, off_right;
1080 int n;
1081 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001082 int width1; // text width for first screen line
1083 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 int new_leftcol;
1085 colnr_T startcol;
1086 colnr_T endcol;
1087 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001088 long so = get_scrolloff_value();
1089 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001090 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 /*
1093 * First make sure that w_topline is valid (after moving the cursor).
1094 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001095 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
1097 /*
1098 * Next make sure that w_cline_row is valid.
1099 */
1100 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001101 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001103#ifdef FEAT_PROP_POPUP
1104 // will be set by getvvcol() but not reset
1105 curwin->w_virtcol_first_char = 0;
1106#endif
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 /*
1109 * Compute the number of virtual columns.
1110 */
1111#ifdef FEAT_FOLDING
1112 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1115 else
1116#endif
1117 getvvcol(curwin, &curwin->w_cursor,
1118 &startcol, &(curwin->w_virtcol), &endcol);
1119
Bram Moolenaar85a20022019-12-21 18:25:54 +01001120 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001122 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
1124 extra = curwin_col_off();
1125 curwin->w_wcol = curwin->w_virtcol + extra;
1126 endcol += extra;
1127
1128 /*
1129 * Now compute w_wrow, counting screen lines from w_cline_row.
1130 */
1131 curwin->w_wrow = curwin->w_cline_row;
1132
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001133 width1 = curwin->w_width - extra;
1134 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001136 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001137 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001138 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001139 if (curwin->w_p_wrap)
1140 curwin->w_wrow = curwin->w_height - 1;
1141 else
1142 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001144 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001146 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001148 // skip columns that are not visible
1149 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001150 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001151 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001152 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001153 // Deduct by multiples of width2. This allows the long line
1154 // wrapping formula below to correctly calculate the w_wcol value
1155 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001156 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001157 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001158 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001159 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001160 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001161
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001162 did_sub_skipcol = TRUE;
1163 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001164
Bram Moolenaar85a20022019-12-21 18:25:54 +01001165 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001166 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001168 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001169 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1170 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 curwin->w_wrow += n;
1172
1173#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // When cursor wraps to first char of next line in Insert
1175 // mode, the 'showbreak' string isn't shown, backup to first
1176 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001177 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001178 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001179 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 curwin->w_wcol = 0;
1181#endif
1182 }
1183 }
1184
Bram Moolenaar85a20022019-12-21 18:25:54 +01001185 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1186 // is not folded.
1187 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001188 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#ifdef FEAT_FOLDING
1190 && !curwin->w_cline_folded
1191#endif
1192 )
1193 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001194#ifdef FEAT_PROP_POPUP
1195 if (curwin->w_virtcol_first_char > 0)
1196 {
1197 int cols = (curwin->w_width - extra);
1198 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1199
1200 // each "above" text prop shifts the text one row down
1201 curwin->w_wrow += rows;
1202 curwin->w_wcol -= rows * cols;
1203 endcol -= rows * cols;
1204 curwin->w_cline_height = rows + 1;
1205 }
1206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 /*
1208 * If Cursor is left of the screen, scroll rightwards.
1209 * If Cursor is right of the screen, scroll leftwards
1210 * If we get closer to the edge than 'sidescrolloff', scroll a little
1211 * extra
1212 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001213 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001214 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001215 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (off_left < 0 || off_right > 0)
1217 {
1218 if (off_left < 0)
1219 diff = -off_left;
1220 else
1221 diff = off_right;
1222
Bram Moolenaar85a20022019-12-21 18:25:54 +01001223 // When far off or not enough room on either side, put cursor in
1224 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001225 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1226 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else
1228 {
1229 if (diff < p_ss)
1230 diff = p_ss;
1231 if (off_left < 0)
1232 new_leftcol = curwin->w_leftcol - diff;
1233 else
1234 new_leftcol = curwin->w_leftcol + diff;
1235 }
1236 if (new_leftcol < 0)
1237 new_leftcol = 0;
1238 if (new_leftcol != (int)curwin->w_leftcol)
1239 {
1240 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001241 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001242 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
1244 }
1245 curwin->w_wcol -= curwin->w_leftcol;
1246 }
1247 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1248 curwin->w_wcol -= curwin->w_leftcol;
1249 else
1250 curwin->w_wcol = 0;
1251
1252#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001253 // Skip over filler lines. At the top use w_topfill, there
1254 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (curwin->w_cursor.lnum == curwin->w_topline)
1256 curwin->w_wrow += curwin->w_topfill;
1257 else
1258 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1259#endif
1260
1261 prev_skipcol = curwin->w_skipcol;
1262
1263 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001264
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if ((curwin->w_wrow >= curwin->w_height
1266 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001267 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 && (p_lines =
1269#ifdef FEAT_DIFF
1270 plines_win_nofill
1271#else
1272 plines_win
1273#endif
1274 (curwin, curwin->w_cursor.lnum, FALSE))
1275 - 1 >= curwin->w_height))
1276 && curwin->w_height != 0
1277 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001278 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001279 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001281 // Cursor past end of screen. Happens with a single line that does
1282 // not fit on screen. Find a skipcol to show the text around the
1283 // cursor. Avoid scrolling all the time. compute value of "extra":
1284 // 1: Less than 'scrolloff' lines above
1285 // 2: Less than 'scrolloff' lines below
1286 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001288 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001290 // Compute last display line of the buffer line that we want at the
1291 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (p_lines == 0)
1293 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1294 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001295 if (p_lines > curwin->w_wrow + so)
1296 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 else
1298 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001299 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 extra += 2;
1301
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001302 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001304 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001305 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (n > curwin->w_height / 2)
1307 n -= curwin->w_height / 2;
1308 else
1309 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001310 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (n > p_lines - curwin->w_height + 1)
1312 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001313 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 else if (extra == 1)
1316 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001317 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001318 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1319 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (extra > 0)
1321 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001322 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1323 extra = curwin->w_skipcol / width2;
1324 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 }
1327 else if (extra == 2)
1328 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001329 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001330 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001332 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (endcol > curwin->w_skipcol)
1334 curwin->w_skipcol = endcol;
1335 }
1336
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001337 // adjust w_wrow for the changed w_skipcol
1338 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001339 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001340 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001341 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (curwin->w_wrow >= curwin->w_height)
1344 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001345 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001347 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 curwin->w_wrow -= extra;
1349 }
1350
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001351 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (extra > 0)
1353 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1354 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001355 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001357 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 curwin->w_skipcol = 0;
1359 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001360 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001362#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001363 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001364#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001365#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1366 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1367 {
1368 curwin->w_wrow += popup_top_extra(curwin);
1369 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001370 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001371 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001372 else
1373 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001374#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001375
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001376 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1377 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001378 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001379 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1382}
1383
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001384#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001385/*
1386 * Compute the screen position of text character at "pos" in window "wp"
1387 * The resulting values are one-based, zero when character is not visible.
1388 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001389 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001390textpos2screenpos(
1391 win_T *wp,
1392 pos_T *pos,
1393 int *rowp, // screen row
1394 int *scolp, // start screen column
1395 int *ccolp, // cursor screen column
1396 int *ecolp) // end screen column
1397{
1398 colnr_T scol = 0, ccol = 0, ecol = 0;
1399 int row = 0;
1400 int rowoff = 0;
1401 colnr_T coloff = 0;
1402
Bram Moolenaar189663b2021-07-21 18:04:56 +02001403 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001404 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001405 colnr_T off;
1406 colnr_T col;
1407 int width;
1408 linenr_T lnum = pos->lnum;
1409#ifdef FEAT_FOLDING
1410 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001411
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001412 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1413#endif
1414 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1415#ifdef FEAT_FOLDING
1416 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001417 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001418 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001419 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001420 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001421 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001422#endif
1423 {
1424 getvcol(wp, pos, &scol, &ccol, &ecol);
1425
1426 // similar to what is done in validate_cursor_col()
1427 col = scol;
1428 off = win_col_off(wp);
1429 col += off;
1430 width = wp->w_width - off + win_col_off2(wp);
1431
1432 // long line wrapping, adjust row
1433 if (wp->w_p_wrap
1434 && col >= (colnr_T)wp->w_width
1435 && width > 0)
1436 {
1437 // use same formula as what is used in curs_columns()
1438 rowoff = ((col - wp->w_width) / width + 1);
1439 col -= rowoff * width;
1440 }
1441 col -= wp->w_leftcol;
1442 if (col >= wp->w_width)
1443 col = -1;
1444 if (col >= 0 && row + rowoff <= wp->w_height)
1445 {
1446 coloff = col - scol + wp->w_wincol + 1;
1447 row += W_WINROW(wp);
1448 }
1449 else
1450 // character is left, right or below of the window
1451 row = rowoff = scol = ccol = ecol = 0;
1452 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001453 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001454 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001455 *scolp = scol + coloff;
1456 *ccolp = ccol + coloff;
1457 *ecolp = ecol + coloff;
1458}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001459#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001460
Bram Moolenaar12034e22019-08-25 22:25:02 +02001461#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001462/*
1463 * "screenpos({winid}, {lnum}, {col})" function
1464 */
1465 void
1466f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1467{
1468 dict_T *dict;
1469 win_T *wp;
1470 pos_T pos;
1471 int row = 0;
1472 int scol = 0, ccol = 0, ecol = 0;
1473
Bram Moolenaar93a10962022-06-16 11:42:09 +01001474 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001475 return;
1476 dict = rettv->vval.v_dict;
1477
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001478 if (in_vim9script()
1479 && (check_for_number_arg(argvars, 0) == FAIL
1480 || check_for_number_arg(argvars, 1) == FAIL
1481 || check_for_number_arg(argvars, 2) == FAIL))
1482 return;
1483
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001484 wp = find_win_by_nr_or_id(&argvars[0]);
1485 if (wp == NULL)
1486 return;
1487
1488 pos.lnum = tv_get_number(&argvars[1]);
1489 pos.col = tv_get_number(&argvars[2]) - 1;
1490 pos.coladd = 0;
1491 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1492
1493 dict_add_number(dict, "row", row);
1494 dict_add_number(dict, "col", scol);
1495 dict_add_number(dict, "curscol", ccol);
1496 dict_add_number(dict, "endcol", ecol);
1497}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001498
1499/*
1500 * "virtcol2col({winid}, {lnum}, {col})" function
1501 */
1502 void
1503f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1504{
1505 win_T *wp;
1506 linenr_T lnum;
1507 int screencol;
1508 int error = FALSE;
1509
1510 rettv->vval.v_number = -1;
1511
1512 if (check_for_number_arg(argvars, 0) == FAIL
1513 || check_for_number_arg(argvars, 1) == FAIL
1514 || check_for_number_arg(argvars, 2) == FAIL)
1515 return;
1516
1517 wp = find_win_by_nr_or_id(&argvars[0]);
1518 if (wp == NULL)
1519 return;
1520
1521 lnum = tv_get_number_chk(&argvars[1], &error);
1522 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1523 return;
1524
1525 screencol = tv_get_number_chk(&argvars[2], &error);
1526 if (error || screencol < 0)
1527 return;
1528
1529 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1530}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001531#endif
1532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533/*
1534 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001537scrolldown(
1538 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001539 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001541 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 int wrow;
1543 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001544 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001545 int width1 = 0;
1546 int width2 = 0;
1547
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001548 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001549 {
1550 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001551 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554#ifdef FEAT_FOLDING
1555 linenr_T first;
1556
Bram Moolenaar85a20022019-12-21 18:25:54 +01001557 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1559#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001560 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001561 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001564 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1565 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
1567 ++curwin->w_topfill;
1568 ++done;
1569 }
1570 else
1571#endif
1572 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001573 // break when at the very top
1574 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001575 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001577 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001579 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001580 if (curwin->w_skipcol >= width1 + width2)
1581 curwin->w_skipcol -= width2;
1582 else
1583 curwin->w_skipcol -= width1;
1584 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
1587 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001588 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001589 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001590 --curwin->w_topline;
1591 curwin->w_skipcol = 0;
1592#ifdef FEAT_DIFF
1593 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001595#ifdef FEAT_FOLDING
1596 // A sequence of folded lines only counts for one logical line
1597 if (hasFolding(curwin->w_topline, &first, NULL))
1598 {
1599 ++done;
1600 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001601 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001602 curwin->w_botline -= curwin->w_topline - first;
1603 curwin->w_topline = first;
1604 }
1605 else
1606#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001607 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001608 {
1609 int size = win_linetabsize(curwin, curwin->w_topline,
1610 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1611 if (size > width1)
1612 {
1613 curwin->w_skipcol = width1;
1614 size -= width1;
1615 redraw_later(UPD_NOT_VALID);
1616 }
1617 while (size > width2)
1618 {
1619 curwin->w_skipcol += width2;
1620 size -= width2;
1621 }
1622 ++done;
1623 }
1624 else
1625 done += PLINES_NOFILL(curwin->w_topline);
1626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001628 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 invalidate_botline();
1630 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001631 curwin->w_wrow += done; // keep w_wrow updated
1632 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634#ifdef FEAT_DIFF
1635 if (curwin->w_cursor.lnum == curwin->w_topline)
1636 curwin->w_cline_row = 0;
1637 check_topfill(curwin, TRUE);
1638#endif
1639
1640 /*
1641 * Compute the row number of the last row of the cursor line
1642 * and move the cursor onto the displayed part of the window.
1643 */
1644 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001645 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
1647 validate_virtcol();
1648 validate_cheight();
1649 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001650 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 }
1652 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1653 {
1654#ifdef FEAT_FOLDING
1655 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1656 {
1657 --wrow;
1658 if (first == 1)
1659 curwin->w_cursor.lnum = 1;
1660 else
1661 curwin->w_cursor.lnum = first - 1;
1662 }
1663 else
1664#endif
1665 wrow -= plines(curwin->w_cursor.lnum--);
1666 curwin->w_valid &=
1667 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1668 moved = TRUE;
1669 }
1670 if (moved)
1671 {
1672#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001673 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 foldAdjustCursor();
1675#endif
1676 coladvance(curwin->w_curswant);
1677 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001678
1679 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1680 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001681 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001682 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1683
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001684 // make sure the cursor is in the visible text
1685 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001686 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001687 int row = 0;
1688 if (col >= width1)
1689 {
1690 col -= width1;
1691 ++row;
1692 }
1693 if (col > width2)
1694 {
1695 row += col / width2;
1696 col = col % width2;
1697 }
1698 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001699 {
1700 curwin->w_curswant = curwin->w_virtcol
1701 - (row - curwin->w_height + 1) * width2;
1702 coladvance(curwin->w_curswant);
1703 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705}
1706
1707/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001708 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1709 */
1710 static int
1711scrolling_screenlines(int byfold UNUSED)
1712{
1713 return (curwin->w_p_wrap && curwin->w_p_sms)
1714# ifdef FEAT_FOLDING
1715 || (byfold && hasAnyFolding(curwin))
1716# endif
1717# ifdef FEAT_DIFF
1718 || curwin->w_p_diff
1719# endif
1720 ;
1721}
1722
1723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001727scrollup(
1728 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001729 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001731 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001733 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001735 int width1 = curwin->w_width - curwin_col_off();
1736 int width2 = width1 + curwin_col_off2();
1737 int size = 0;
1738 linenr_T prev_topline = curwin->w_topline;
1739
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001740 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001741 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001742
1743 // diff mode: first consume "topfill"
1744 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1745 // the line, then advance to the next line.
1746 // folding: count each sequence of folded lines as one logical line.
1747 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 {
1749# ifdef FEAT_DIFF
1750 if (curwin->w_topfill > 0)
1751 --curwin->w_topfill;
1752 else
1753# endif
1754 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001755 linenr_T lnum = curwin->w_topline;
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757# ifdef FEAT_FOLDING
1758 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001759 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 (void)hasFolding(lnum, NULL, &lnum);
1761# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001762 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001763 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001764 // 'smoothscroll': increase "w_skipcol" until it goes over
1765 // the end of the line, then advance to the next line.
1766 int add = curwin->w_skipcol > 0 ? width2 : width1;
1767 curwin->w_skipcol += add;
1768 if (curwin->w_skipcol >= size)
1769 {
1770 if (lnum == curbuf->b_ml.ml_line_count)
1771 {
1772 // at the last screen line, can't scroll further
1773 curwin->w_skipcol -= add;
1774 break;
1775 }
1776 ++lnum;
1777 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001778 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001779 else
1780 {
1781 if (lnum >= curbuf->b_ml.ml_line_count)
1782 break;
1783 ++lnum;
1784 }
1785
1786 if (lnum > curwin->w_topline)
1787 {
1788 // approximate w_botline
1789 curwin->w_botline += lnum - curwin->w_topline;
1790 curwin->w_topline = lnum;
1791# ifdef FEAT_DIFF
1792 curwin->w_topfill = diff_check_fill(curwin, lnum);
1793# endif
1794 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001795 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001796 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001797 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001798 }
1799 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001800
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001801 if (curwin->w_topline == prev_topline)
1802 // need to redraw even though w_topline didn't change
1803 redraw_later(UPD_NOT_VALID);
1804 }
1805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
1807 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001808 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810
1811 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1812 curwin->w_topline = curbuf->b_ml.ml_line_count;
1813 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1814 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1815
1816#ifdef FEAT_DIFF
1817 check_topfill(curwin, FALSE);
1818#endif
1819
1820#ifdef FEAT_FOLDING
1821 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001822 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1824#endif
1825
1826 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1827 if (curwin->w_cursor.lnum < curwin->w_topline)
1828 {
1829 curwin->w_cursor.lnum = curwin->w_topline;
1830 curwin->w_valid &=
1831 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1832 coladvance(curwin->w_curswant);
1833 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001834 if (curwin->w_cursor.lnum == curwin->w_topline
1835 && do_sms && curwin->w_skipcol > 0)
1836 {
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001837 int col_off = curwin_col_off();
1838 int col_off2 = curwin_col_off2();
1839
1840 int width1 = curwin->w_width - col_off;
1841 int width2 = width1 + col_off2;
1842 int extra2 = col_off - col_off2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001843 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001844 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001845 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001846
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001847 // If we have non-zero scrolloff, just ignore the <<< marker as we are
1848 // going past it anyway.
1849 int smoothscroll_overlap = scrolloff_cols != 0 ? 0 :
1850 smoothscroll_marker_overlap(extra2);
1851
Bram Moolenaar118c2352022-10-09 17:19:27 +01001852 // Make sure the cursor is in a visible part of the line, taking
1853 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001854 // If there are not enough screen lines put the cursor in the middle.
1855 if (scrolloff_cols > space_cols / 2)
1856 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001857 validate_virtcol();
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001858 if (curwin->w_virtcol < curwin->w_skipcol
1859 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001860 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001861 colnr_T col = curwin->w_virtcol;
1862
1863 if (col < width1)
1864 col += width1;
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00001865 while (col < curwin->w_skipcol
1866 + smoothscroll_overlap + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001867 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001868 curwin->w_curswant = col;
1869 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001870
1871 // validate_virtcol() marked various things as valid, but after
1872 // moving the cursor they need to be recomputed
1873 curwin->w_valid &=
1874 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001875 }
1876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877}
1878
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001879/*
1880 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1881 * valid for 'smoothscroll'.
1882 */
1883 void
1884adjust_skipcol(void)
1885{
1886 if (!curwin->w_p_wrap
1887 || !curwin->w_p_sms
1888 || curwin->w_cursor.lnum != curwin->w_topline)
1889 return;
1890
1891 int width1 = curwin->w_width - curwin_col_off();
1892 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001893 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001894 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1895 int scrolled = FALSE;
1896
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001897 validate_cheight();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001898 if (curwin->w_cline_height == curwin->w_height)
1899 {
1900 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001901 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001902 return;
1903 }
1904
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001905 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001906 while (curwin->w_skipcol > 0
1907 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1908 {
1909 // scroll a screen line down
1910 if (curwin->w_skipcol >= width1 + width2)
1911 curwin->w_skipcol -= width2;
1912 else
1913 curwin->w_skipcol -= width1;
1914 redraw_later(UPD_NOT_VALID);
1915 scrolled = TRUE;
1916 validate_virtcol();
1917 }
1918 if (scrolled)
1919 return; // don't scroll in the other direction now
1920
1921 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1922 int row = 0;
1923 if (col >= width1)
1924 {
1925 col -= width1;
1926 ++row;
1927 }
1928 if (col > width2)
1929 {
1930 row += col / width2;
1931 col = col % width2;
1932 }
1933 if (row >= curwin->w_height)
1934 {
1935 if (curwin->w_skipcol == 0)
1936 {
1937 curwin->w_skipcol += width1;
1938 --row;
1939 }
1940 if (row >= curwin->w_height)
1941 curwin->w_skipcol += (row - curwin->w_height) * width2;
1942 redraw_later(UPD_NOT_VALID);
1943 }
1944}
1945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946#ifdef FEAT_DIFF
1947/*
1948 * Don't end up with too many filler lines in the window.
1949 */
1950 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001951check_topfill(
1952 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001953 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
1955 int n;
1956
1957 if (wp->w_topfill > 0)
1958 {
1959 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1960 if (wp->w_topfill + n > wp->w_height)
1961 {
1962 if (down && wp->w_topline > 1)
1963 {
1964 --wp->w_topline;
1965 wp->w_topfill = 0;
1966 }
1967 else
1968 {
1969 wp->w_topfill = wp->w_height - n;
1970 if (wp->w_topfill < 0)
1971 wp->w_topfill = 0;
1972 }
1973 }
1974 }
1975}
1976
1977/*
1978 * Use as many filler lines as possible for w_topline. Make sure w_topline
1979 * is still visible.
1980 */
1981 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001982max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983{
1984 int n;
1985
1986 n = plines_nofill(curwin->w_topline);
1987 if (n >= curwin->w_height)
1988 curwin->w_topfill = 0;
1989 else
1990 {
1991 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1992 if (curwin->w_topfill + n > curwin->w_height)
1993 curwin->w_topfill = curwin->w_height - n;
1994 }
1995}
1996#endif
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998/*
1999 * Scroll the screen one line down, but don't do it if it would move the
2000 * cursor off the screen.
2001 */
2002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002003scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 int end_row;
2006#ifdef FEAT_DIFF
2007 int can_fill = (curwin->w_topfill
2008 < diff_check_fill(curwin, curwin->w_topline));
2009#endif
2010
2011 if (curwin->w_topline <= 1
2012#ifdef FEAT_DIFF
2013 && !can_fill
2014#endif
2015 )
2016 return;
2017
Bram Moolenaar85a20022019-12-21 18:25:54 +01002018 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020 /*
2021 * Compute the row number of the last row of the cursor line
2022 * and make sure it doesn't go off the screen. Make sure the cursor
2023 * doesn't go past 'scrolloff' lines from the screen end.
2024 */
2025 end_row = curwin->w_wrow;
2026#ifdef FEAT_DIFF
2027 if (can_fill)
2028 ++end_row;
2029 else
2030 end_row += plines_nofill(curwin->w_topline - 1);
2031#else
2032 end_row += plines(curwin->w_topline - 1);
2033#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002034 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
2036 validate_cheight();
2037 validate_virtcol();
2038 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002039 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002041 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
2043#ifdef FEAT_DIFF
2044 if (can_fill)
2045 {
2046 ++curwin->w_topfill;
2047 check_topfill(curwin, TRUE);
2048 }
2049 else
2050 {
2051 --curwin->w_topline;
2052 curwin->w_topfill = 0;
2053 }
2054#else
2055 --curwin->w_topline;
2056#endif
2057#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002058 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002060 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2062 }
2063}
2064
2065/*
2066 * Scroll the screen one line up, but don't do it if it would move the cursor
2067 * off the screen.
2068 */
2069 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002070scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071{
2072 int start_row;
2073
2074 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2075#ifdef FEAT_DIFF
2076 && curwin->w_topfill == 0
2077#endif
2078 )
2079 return;
2080
Bram Moolenaar85a20022019-12-21 18:25:54 +01002081 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 /*
2084 * Compute the row number of the first row of the cursor line
2085 * and make sure it doesn't go off the screen. Make sure the cursor
2086 * doesn't go before 'scrolloff' lines from the screen start.
2087 */
2088#ifdef FEAT_DIFF
2089 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2090 - curwin->w_topfill;
2091#else
2092 start_row = curwin->w_wrow - plines(curwin->w_topline);
2093#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002094 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
2096 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002097 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002099 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
2101#ifdef FEAT_DIFF
2102 if (curwin->w_topfill > 0)
2103 --curwin->w_topfill;
2104 else
2105#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002106 {
2107#ifdef FEAT_FOLDING
2108 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002111 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002112 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2114 }
2115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117/*
2118 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2119 * a (wrapped) text line. Uses and sets "lp->fill".
2120 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002121 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 */
2123 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126#ifdef FEAT_DIFF
2127 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2128 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002129 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 ++lp->fill;
2131 lp->height = 1;
2132 }
2133 else
2134#endif
2135 {
2136 --lp->lnum;
2137#ifdef FEAT_DIFF
2138 lp->fill = 0;
2139#endif
2140 if (lp->lnum < 1)
2141 lp->height = MAXCOL;
2142 else
2143#ifdef FEAT_FOLDING
2144 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002145 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 lp->height = 1;
2147 else
2148#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002149 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151}
2152
2153/*
2154 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2155 * a (wrapped) text line. Uses and sets "lp->fill".
2156 * Returns the height of the added line in "lp->height".
2157 * Lines below the last one are incredibly high.
2158 */
2159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002160botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162#ifdef FEAT_DIFF
2163 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2164 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002165 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 ++lp->fill;
2167 lp->height = 1;
2168 }
2169 else
2170#endif
2171 {
2172 ++lp->lnum;
2173#ifdef FEAT_DIFF
2174 lp->fill = 0;
2175#endif
2176 if (lp->lnum > curbuf->b_ml.ml_line_count)
2177 lp->height = MAXCOL;
2178 else
2179#ifdef FEAT_FOLDING
2180 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002181 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 lp->height = 1;
2183 else
2184#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002185 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187}
2188
2189#ifdef FEAT_DIFF
2190/*
2191 * Switch from including filler lines below lp->lnum to including filler
2192 * lines above loff.lnum + 1. This keeps pointing to the same line.
2193 * When there are no filler lines nothing changes.
2194 */
2195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002196botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 if (lp->fill > 0)
2199 {
2200 ++lp->lnum;
2201 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2202 }
2203}
2204
2205/*
2206 * Switch from including filler lines above lp->lnum to including filler
2207 * lines below loff.lnum - 1. This keeps pointing to the same line.
2208 * When there are no filler lines nothing changes.
2209 */
2210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002211topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 if (lp->fill > 0)
2214 {
2215 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2216 --lp->lnum;
2217 }
2218}
2219#endif
2220
2221/*
2222 * Recompute topline to put the cursor at the top of the window.
2223 * Scroll at least "min_scroll" lines.
2224 * If "always" is TRUE, always set topline (for "zt").
2225 */
2226 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002227scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 int scrolled = 0;
2230 int extra = 0;
2231 int used;
2232 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002233 linenr_T top; // just above displayed lines
2234 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002236 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#ifdef FEAT_DIFF
2238 linenr_T old_topfill = curwin->w_topfill;
2239#endif
2240 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002241 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (mouse_dragging > 0)
2244 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 /*
2247 * Decrease topline until:
2248 * - it has become 1
2249 * - (part of) the cursor line is moved off the screen or
2250 * - moved at least 'scrolljump' lines and
2251 * - at least 'scrolloff' lines above and below the cursor
2252 */
2253 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (curwin->w_cursor.lnum < curwin->w_topline)
2256 scrolled = used;
2257
2258#ifdef FEAT_FOLDING
2259 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2260 {
2261 --top;
2262 ++bot;
2263 }
2264 else
2265#endif
2266 {
2267 top = curwin->w_cursor.lnum - 1;
2268 bot = curwin->w_cursor.lnum + 1;
2269 }
2270 new_topline = top + 1;
2271
2272#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002273 // "used" already contains the number of filler lines above, don't add it
2274 // again.
2275 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002276 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#endif
2278
2279 /*
2280 * Check if the lines from "top" to "bot" fit in the window. If they do,
2281 * set new_topline and advance "top" and "bot" to include more lines.
2282 */
2283 while (top > 0)
2284 {
2285#ifdef FEAT_FOLDING
2286 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002287 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 i = 1;
2289 else
2290#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002291 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 used += i;
2293 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2294 {
2295#ifdef FEAT_FOLDING
2296 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 ++used;
2299 else
2300#endif
2301 used += plines(bot);
2302 }
2303 if (used > curwin->w_height)
2304 break;
2305 if (top < curwin->w_topline)
2306 scrolled += i;
2307
2308 /*
2309 * If scrolling is needed, scroll at least 'sj' lines.
2310 */
2311 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2312 && extra >= off)
2313 break;
2314
2315 extra += i;
2316 new_topline = top;
2317 --top;
2318 ++bot;
2319 }
2320
2321 /*
2322 * If we don't have enough space, put cursor in the middle.
2323 * This makes sure we get the same position when using "k" and "j"
2324 * in a small window.
2325 */
2326 if (used > curwin->w_height)
2327 scroll_cursor_halfway(FALSE);
2328 else
2329 {
2330 /*
2331 * If "always" is FALSE, only adjust topline to a lower value, higher
2332 * value may happen with wrapping lines
2333 */
2334 if (new_topline < curwin->w_topline || always)
2335 curwin->w_topline = new_topline;
2336 if (curwin->w_topline > curwin->w_cursor.lnum)
2337 curwin->w_topline = curwin->w_cursor.lnum;
2338#ifdef FEAT_DIFF
2339 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2340 if (curwin->w_topfill > 0 && extra > off)
2341 {
2342 curwin->w_topfill -= extra - off;
2343 if (curwin->w_topfill < 0)
2344 curwin->w_topfill = 0;
2345 }
2346 check_topfill(curwin, FALSE);
2347#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002348 // TODO: if the line doesn't fit may optimize w_skipcol
2349 if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002350 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002352 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#ifdef FEAT_DIFF
2354 || curwin->w_topfill != old_topfill
2355#endif
2356 )
2357 curwin->w_valid &=
2358 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2359 curwin->w_valid |= VALID_TOPLINE;
2360 }
2361}
2362
2363/*
2364 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2365 * screen lines for text lines.
2366 */
2367 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002368set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370#ifdef FEAT_DIFF
2371 wp->w_filler_rows = 0;
2372#endif
2373 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002374 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 else
2376 {
2377 wp->w_empty_rows = wp->w_height - used;
2378#ifdef FEAT_DIFF
2379 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2380 {
2381 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2382 if (wp->w_empty_rows > wp->w_filler_rows)
2383 wp->w_empty_rows -= wp->w_filler_rows;
2384 else
2385 {
2386 wp->w_filler_rows = wp->w_empty_rows;
2387 wp->w_empty_rows = 0;
2388 }
2389 }
2390#endif
2391 }
2392}
2393
2394/*
2395 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002396 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2398 * This is messy stuff!!!
2399 */
2400 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002401scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
2403 int used;
2404 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002405 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 int extra = 0;
2407 int i;
2408 linenr_T line_count;
2409 linenr_T old_topline = curwin->w_topline;
2410 lineoff_T loff;
2411 lineoff_T boff;
2412#ifdef FEAT_DIFF
2413 int old_topfill = curwin->w_topfill;
2414 int fill_below_window;
2415#endif
2416 linenr_T old_botline = curwin->w_botline;
2417 linenr_T old_valid = curwin->w_valid;
2418 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002419 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002420 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 cln = curwin->w_cursor.lnum;
2423 if (set_topbot)
2424 {
2425 used = 0;
2426 curwin->w_botline = cln + 1;
2427#ifdef FEAT_DIFF
2428 loff.fill = 0;
2429#endif
2430 for (curwin->w_topline = curwin->w_botline;
2431 curwin->w_topline > 1;
2432 curwin->w_topline = loff.lnum)
2433 {
2434 loff.lnum = curwin->w_topline;
2435 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002436 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 break;
2438 used += loff.height;
2439#ifdef FEAT_DIFF
2440 curwin->w_topfill = loff.fill;
2441#endif
2442 }
2443 set_empty_rows(curwin, used);
2444 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2445 if (curwin->w_topline != old_topline
2446#ifdef FEAT_DIFF
2447 || curwin->w_topfill != old_topfill
2448#endif
2449 )
2450 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2451 }
2452 else
2453 validate_botline();
2454
Bram Moolenaar85a20022019-12-21 18:25:54 +01002455 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456#ifdef FEAT_DIFF
2457 used = plines_nofill(cln);
2458#else
2459 validate_cheight();
2460 used = curwin->w_cline_height;
2461#endif
2462
Bram Moolenaar85a20022019-12-21 18:25:54 +01002463 // If the cursor is below botline, we will at least scroll by the height
2464 // of the cursor line. Correct for empty lines, which are really part of
2465 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (cln >= curwin->w_botline)
2467 {
2468 scrolled = used;
2469 if (cln == curwin->w_botline)
2470 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002471 min_scrolled = scrolled;
2472 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2473 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
Bram Moolenaar4d31b482022-10-06 16:03:09 +01002474 min_scrolled += PLINES_NOFILL(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 }
2476
2477 /*
2478 * Stop counting lines to scroll when
2479 * - hitting start of the file
2480 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002481 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 * - lines between botline and cursor have been counted
2483 */
2484#ifdef FEAT_FOLDING
2485 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2486#endif
2487 {
2488 loff.lnum = cln;
2489 boff.lnum = cln;
2490 }
2491#ifdef FEAT_DIFF
2492 loff.fill = 0;
2493 boff.fill = 0;
2494 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2495 - curwin->w_filler_rows;
2496#endif
2497
2498 while (loff.lnum > 1)
2499 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002500 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2501 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002503 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2505 && loff.lnum <= curwin->w_botline
2506#ifdef FEAT_DIFF
2507 && (loff.lnum < curwin->w_botline
2508 || loff.fill >= fill_below_window)
2509#endif
2510 )
2511 break;
2512
Bram Moolenaar85a20022019-12-21 18:25:54 +01002513 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002515 if (loff.height == MAXCOL)
2516 used = MAXCOL;
2517 else
2518 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (used > curwin->w_height)
2520 break;
2521 if (loff.lnum >= curwin->w_botline
2522#ifdef FEAT_DIFF
2523 && (loff.lnum > curwin->w_botline
2524 || loff.fill <= fill_below_window)
2525#endif
2526 )
2527 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002528 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 scrolled += loff.height;
2530 if (loff.lnum == curwin->w_botline
2531#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002532 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#endif
2534 )
2535 scrolled -= curwin->w_empty_rows;
2536 }
2537
2538 if (boff.lnum < curbuf->b_ml.ml_line_count)
2539 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002540 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 botline_forw(&boff);
2542 used += boff.height;
2543 if (used > curwin->w_height)
2544 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002545 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2546 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 extra += boff.height;
2549 if (boff.lnum >= curwin->w_botline
2550#ifdef FEAT_DIFF
2551 || (boff.lnum + 1 == curwin->w_botline
2552 && boff.fill > curwin->w_filler_rows)
2553#endif
2554 )
2555 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002556 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 scrolled += boff.height;
2558 if (boff.lnum == curwin->w_botline
2559#ifdef FEAT_DIFF
2560 && boff.fill == 0
2561#endif
2562 )
2563 scrolled -= curwin->w_empty_rows;
2564 }
2565 }
2566 }
2567 }
2568
Bram Moolenaar85a20022019-12-21 18:25:54 +01002569 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (scrolled <= 0)
2571 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002572 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 else if (used > curwin->w_height)
2574 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002575 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 else
2577 {
2578 line_count = 0;
2579#ifdef FEAT_DIFF
2580 boff.fill = curwin->w_topfill;
2581#endif
2582 boff.lnum = curwin->w_topline - 1;
2583 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2584 {
2585 botline_forw(&boff);
2586 i += boff.height;
2587 ++line_count;
2588 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002589 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 line_count = 9999;
2591 }
2592
2593 /*
2594 * Scroll up if the cursor is off the bottom of the screen a bit.
2595 * Otherwise put it at 1/2 of the screen.
2596 */
2597 if (line_count >= curwin->w_height && line_count > min_scroll)
2598 scroll_cursor_halfway(FALSE);
2599 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002600 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002601 // With 'smoothscroll' scroll at least the height of the cursor line,
2602 // unless it would move the cursor.
2603 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2604 && (curwin->w_cursor.lnum < curwin->w_topline
2605 || (curwin->w_virtcol - curwin->w_skipcol >=
2606 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002607 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002608 if (line_count > 0)
2609 {
2610 if (scrolling_screenlines(TRUE))
2611 scrollup(scrolled, TRUE); // TODO
2612 else
2613 scrollup(line_count, TRUE);
2614 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 /*
2618 * If topline didn't change we need to restore w_botline and w_empty_rows
2619 * (we changed them).
2620 * If topline did change, update_screen() will set botline.
2621 */
2622 if (curwin->w_topline == old_topline && set_topbot)
2623 {
2624 curwin->w_botline = old_botline;
2625 curwin->w_empty_rows = old_empty_rows;
2626 curwin->w_valid = old_valid;
2627 }
2628 curwin->w_valid |= VALID_TOPLINE;
2629}
2630
2631/*
2632 * Recompute topline to put the cursor halfway the window
2633 * If "atend" is TRUE, also put it halfway at the end of the file.
2634 */
2635 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002636scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 int above = 0;
2639 linenr_T topline;
2640#ifdef FEAT_DIFF
2641 int topfill = 0;
2642#endif
2643 int below = 0;
2644 int used;
2645 lineoff_T loff;
2646 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002647#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002648 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002651#ifdef FEAT_PROP_POPUP
2652 // if the width changed this needs to be updated first
2653 may_update_popup_position();
2654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2656#ifdef FEAT_FOLDING
2657 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2658#endif
2659#ifdef FEAT_DIFF
2660 used = plines_nofill(loff.lnum);
2661 loff.fill = 0;
2662 boff.fill = 0;
2663#else
2664 used = plines(loff.lnum);
2665#endif
2666 topline = loff.lnum;
2667 while (topline > 1)
2668 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002669 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
2671 if (boff.lnum < curbuf->b_ml.ml_line_count)
2672 {
2673 botline_forw(&boff);
2674 used += boff.height;
2675 if (used > curwin->w_height)
2676 break;
2677 below += boff.height;
2678 }
2679 else
2680 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002681 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (atend)
2683 ++used;
2684 }
2685 }
2686
Bram Moolenaar85a20022019-12-21 18:25:54 +01002687 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
2689 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002690 if (loff.height == MAXCOL)
2691 used = MAXCOL;
2692 else
2693 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (used > curwin->w_height)
2695 break;
2696 above += loff.height;
2697 topline = loff.lnum;
2698#ifdef FEAT_DIFF
2699 topfill = loff.fill;
2700#endif
2701 }
2702 }
2703#ifdef FEAT_FOLDING
2704 if (!hasFolding(topline, &curwin->w_topline, NULL))
2705#endif
2706 curwin->w_topline = topline;
2707#ifdef FEAT_DIFF
2708 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002709 if (old_topline > curwin->w_topline + curwin->w_height)
2710 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 check_topfill(curwin, FALSE);
2712#endif
2713 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2714 curwin->w_valid |= VALID_TOPLINE;
2715}
2716
2717/*
2718 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002719 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 * If not possible, put it at the same position as scroll_cursor_halfway().
2721 * When called topline must be valid!
2722 */
2723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002724cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002726 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002728 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 linenr_T botline;
2730 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002731 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002733 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735 /*
2736 * How many lines we would like to have above/below the cursor depends on
2737 * whether the first/last line of the file is on screen.
2738 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002739 above_wanted = so;
2740 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002741 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
2743 above_wanted = mouse_dragging - 1;
2744 below_wanted = mouse_dragging - 1;
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (curwin->w_topline == 1)
2747 {
2748 above_wanted = 0;
2749 max_off = curwin->w_height / 2;
2750 if (below_wanted > max_off)
2751 below_wanted = max_off;
2752 }
2753 validate_botline();
2754 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002755 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 below_wanted = 0;
2758 max_off = (curwin->w_height - 1) / 2;
2759 if (above_wanted > max_off)
2760 above_wanted = max_off;
2761 }
2762
2763 /*
2764 * If there are sufficient file-lines above and below the cursor, we can
2765 * return now.
2766 */
2767 cln = curwin->w_cursor.lnum;
2768 if (cln >= curwin->w_topline + above_wanted
2769 && cln < curwin->w_botline - below_wanted
2770#ifdef FEAT_FOLDING
2771 && !hasAnyFolding(curwin)
2772#endif
2773 )
2774 return;
2775
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002776 if (curwin->w_p_sms && !curwin->w_p_wrap)
2777 {
2778 // 'smoothscroll is active
2779 if (curwin->w_cline_height == curwin->w_height)
2780 {
2781 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002782 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002783 return;
2784 }
2785 // TODO: If the cursor line doesn't fit in the window then only adjust
2786 // w_skipcol.
2787 }
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 /*
2790 * Narrow down the area where the cursor can be put by taking lines from
2791 * the top and the bottom until:
2792 * - the desired context lines are found
2793 * - the lines from the top is past the lines from the bottom
2794 */
2795 topline = curwin->w_topline;
2796 botline = curwin->w_botline - 1;
2797#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002798 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 above = curwin->w_topfill;
2800 below = curwin->w_filler_rows;
2801#endif
2802 while ((above < above_wanted || below < below_wanted) && topline < botline)
2803 {
2804 if (below < below_wanted && (below <= above || above >= above_wanted))
2805 {
2806#ifdef FEAT_FOLDING
2807 if (hasFolding(botline, &botline, NULL))
2808 ++below;
2809 else
2810#endif
2811 below += plines(botline);
2812 --botline;
2813 }
2814 if (above < above_wanted && (above < below || below >= below_wanted))
2815 {
2816#ifdef FEAT_FOLDING
2817 if (hasFolding(topline, NULL, &topline))
2818 ++above;
2819 else
2820#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002821 above += PLINES_NOFILL(topline);
2822#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002823 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 if (topline < botline)
2825 above += diff_check_fill(curwin, topline + 1);
2826#endif
2827 ++topline;
2828 }
2829 }
2830 if (topline == botline || botline == 0)
2831 curwin->w_cursor.lnum = topline;
2832 else if (topline > botline)
2833 curwin->w_cursor.lnum = botline;
2834 else
2835 {
2836 if (cln < topline && curwin->w_topline > 1)
2837 {
2838 curwin->w_cursor.lnum = topline;
2839 curwin->w_valid &=
2840 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2841 }
2842 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2843 {
2844 curwin->w_cursor.lnum = botline;
2845 curwin->w_valid &=
2846 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2847 }
2848 }
2849 curwin->w_valid |= VALID_TOPLINE;
2850}
2851
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002852static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854/*
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00002855 * Move screen "count" pages up or down and update screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 *
Yee Cheng Chin81ba26e2022-11-18 12:52:50 +00002857 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 */
2859 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002860onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861{
2862 long n;
2863 int retval = OK;
2864 lineoff_T loff;
2865 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002866 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaar85a20022019-12-21 18:25:54 +01002868 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
2870 beep_flush();
2871 return FAIL;
2872 }
2873
2874 for ( ; count > 0; --count)
2875 {
2876 validate_botline();
2877 /*
2878 * It's an error to move a page up when the first line is already on
2879 * the screen. It's an error to move a page down when the last line
2880 * is on the screen and the topline is 'scrolloff' lines from the
2881 * last line.
2882 */
2883 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002884 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2886 : (curwin->w_topline == 1
2887#ifdef FEAT_DIFF
2888 && curwin->w_topfill ==
2889 diff_check_fill(curwin, curwin->w_topline)
2890#endif
2891 ))
2892 {
2893 beep_flush();
2894 retval = FAIL;
2895 break;
2896 }
2897
2898#ifdef FEAT_DIFF
2899 loff.fill = 0;
2900#endif
2901 if (dir == FORWARD)
2902 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002903 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002905 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002906 if (p_window <= 2)
2907 ++curwin->w_topline;
2908 else
2909 curwin->w_topline += p_window - 2;
2910 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2911 curwin->w_topline = curbuf->b_ml.ml_line_count;
2912 curwin->w_cursor.lnum = curwin->w_topline;
2913 }
2914 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2915 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002916 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 curwin->w_topline = curbuf->b_ml.ml_line_count;
2918#ifdef FEAT_DIFF
2919 curwin->w_topfill = 0;
2920#endif
2921 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2922 }
2923 else
2924 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002925 // For the overlap, start with the line just below the window
2926 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 loff.lnum = curwin->w_botline;
2928#ifdef FEAT_DIFF
2929 loff.fill = diff_check_fill(curwin, loff.lnum)
2930 - curwin->w_filler_rows;
2931#endif
2932 get_scroll_overlap(&loff, -1);
2933 curwin->w_topline = loff.lnum;
2934#ifdef FEAT_DIFF
2935 curwin->w_topfill = loff.fill;
2936 check_topfill(curwin, FALSE);
2937#endif
2938 curwin->w_cursor.lnum = curwin->w_topline;
2939 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2940 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2941 }
2942 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002943 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {
2945#ifdef FEAT_DIFF
2946 if (curwin->w_topline == 1)
2947 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002948 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 max_topfill();
2950 continue;
2951 }
2952#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002953 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002954 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002955 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002956 if (p_window <= 2)
2957 --curwin->w_topline;
2958 else
2959 curwin->w_topline -= p_window - 2;
2960 if (curwin->w_topline < 1)
2961 curwin->w_topline = 1;
2962 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2963 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2964 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2965 continue;
2966 }
2967
Bram Moolenaar85a20022019-12-21 18:25:54 +01002968 // Find the line at the top of the window that is going to be the
2969 // line at the bottom of the window. Make sure this results in
2970 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 loff.lnum = curwin->w_topline - 1;
2972#ifdef FEAT_DIFF
2973 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2974 - curwin->w_topfill;
2975#endif
2976 get_scroll_overlap(&loff, 1);
2977
2978 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2979 {
2980 loff.lnum = curbuf->b_ml.ml_line_count;
2981#ifdef FEAT_DIFF
2982 loff.fill = 0;
2983 }
2984 else
2985 {
2986 botline_topline(&loff);
2987#endif
2988 }
2989 curwin->w_cursor.lnum = loff.lnum;
2990
Bram Moolenaar85a20022019-12-21 18:25:54 +01002991 // Find the line just above the new topline to get the right line
2992 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 n = 0;
2994 while (n <= curwin->w_height && loff.lnum >= 1)
2995 {
2996 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002997 if (loff.height == MAXCOL)
2998 n = MAXCOL;
2999 else
3000 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003002 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 {
3004 curwin->w_topline = 1;
3005#ifdef FEAT_DIFF
3006 max_topfill();
3007#endif
3008 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3009 }
3010 else
3011 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003012 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013#ifdef FEAT_DIFF
3014 topline_botline(&loff);
3015#endif
3016 botline_forw(&loff);
3017 botline_forw(&loff);
3018#ifdef FEAT_DIFF
3019 botline_topline(&loff);
3020#endif
3021#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003022 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
3024#endif
3025
Bram Moolenaar85a20022019-12-21 18:25:54 +01003026 // Always scroll at least one line. Avoid getting stuck on
3027 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 if (loff.lnum >= curwin->w_topline
3029#ifdef FEAT_DIFF
3030 && (loff.lnum > curwin->w_topline
3031 || loff.fill >= curwin->w_topfill)
3032#endif
3033 )
3034 {
3035#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003036 // First try using the maximum number of filler lines. If
3037 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 loff.fill = curwin->w_topfill;
3039 if (curwin->w_topfill < diff_check_fill(curwin,
3040 curwin->w_topline))
3041 max_topfill();
3042 if (curwin->w_topfill == loff.fill)
3043#endif
3044 {
3045 --curwin->w_topline;
3046#ifdef FEAT_DIFF
3047 curwin->w_topfill = 0;
3048#endif
3049 }
3050 comp_botline(curwin);
3051 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003052 curwin->w_valid &=
3053 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
3055 else
3056 {
3057 curwin->w_topline = loff.lnum;
3058#ifdef FEAT_DIFF
3059 curwin->w_topfill = loff.fill;
3060 check_topfill(curwin, FALSE);
3061#endif
3062 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3063 }
3064 }
3065 }
3066 }
3067#ifdef FEAT_FOLDING
3068 foldAdjustCursor();
3069#endif
3070 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003071 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003072 if (retval == OK)
3073 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3075
Bram Moolenaar907dad72018-07-10 15:07:15 +02003076 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003078 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3079 // But make sure we scroll at least one line (happens with mix of long
3080 // wrapping lines and non-wrapping line).
3081 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003083 scroll_cursor_top(1, FALSE);
3084 if (curwin->w_topline <= old_topline
3085 && old_topline < curbuf->b_ml.ml_line_count)
3086 {
3087 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003089 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3090#endif
3091 }
3092 }
3093#ifdef FEAT_FOLDING
3094 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003099 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 return retval;
3101}
3102
3103/*
3104 * Decide how much overlap to use for page-up or page-down scrolling.
3105 * This is symmetric, so that doing both keeps the same lines displayed.
3106 * Three lines are examined:
3107 *
3108 * before CTRL-F after CTRL-F / before CTRL-B
3109 * etc. l1
3110 * l1 last but one line ------------
3111 * l2 last text line l2 top text line
3112 * ------------- l3 second text line
3113 * l3 etc.
3114 */
3115 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003116get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 int h1, h2, h3, h4;
3119 int min_height = curwin->w_height - 2;
3120 lineoff_T loff0, loff1, loff2;
3121
3122#ifdef FEAT_DIFF
3123 if (lp->fill > 0)
3124 lp->height = 1;
3125 else
3126 lp->height = plines_nofill(lp->lnum);
3127#else
3128 lp->height = plines(lp->lnum);
3129#endif
3130 h1 = lp->height;
3131 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003132 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 loff0 = *lp;
3135 if (dir > 0)
3136 botline_forw(lp);
3137 else
3138 topline_back(lp);
3139 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003140 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003142 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 return;
3144 }
3145
3146 loff1 = *lp;
3147 if (dir > 0)
3148 botline_forw(lp);
3149 else
3150 topline_back(lp);
3151 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003152 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003154 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 return;
3156 }
3157
3158 loff2 = *lp;
3159 if (dir > 0)
3160 botline_forw(lp);
3161 else
3162 topline_back(lp);
3163 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003164 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003165 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003167 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168}
3169
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170/*
3171 * Scroll 'scroll' lines up or down.
3172 */
3173 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003174halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175{
3176 long scrolled = 0;
3177 int i;
3178 int n;
3179 int room;
3180
3181 if (Prenum)
3182 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3183 curwin->w_height : Prenum;
3184 n = (curwin->w_p_scr <= curwin->w_height) ?
3185 curwin->w_p_scr : curwin->w_height;
3186
Bram Moolenaard5d37532017-03-27 23:02:07 +02003187 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 validate_botline();
3189 room = curwin->w_empty_rows;
3190#ifdef FEAT_DIFF
3191 room += curwin->w_filler_rows;
3192#endif
3193 if (flag)
3194 {
3195 /*
3196 * scroll the text up
3197 */
3198 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3199 {
3200#ifdef FEAT_DIFF
3201 if (curwin->w_topfill > 0)
3202 {
3203 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003204 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 --curwin->w_topfill;
3206 }
3207 else
3208#endif
3209 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003210 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 n -= i;
3212 if (n < 0 && scrolled > 0)
3213 break;
3214#ifdef FEAT_FOLDING
3215 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3216#endif
3217 ++curwin->w_topline;
3218#ifdef FEAT_DIFF
3219 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3220#endif
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3223 {
3224 ++curwin->w_cursor.lnum;
3225 curwin->w_valid &=
3226 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3230 scrolled += i;
3231
3232 /*
3233 * Correct w_botline for changed w_topline.
3234 * Won't work when there are filler lines.
3235 */
3236#ifdef FEAT_DIFF
3237 if (curwin->w_p_diff)
3238 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3239 else
3240#endif
3241 {
3242 room += i;
3243 do
3244 {
3245 i = plines(curwin->w_botline);
3246 if (i > room)
3247 break;
3248#ifdef FEAT_FOLDING
3249 (void)hasFolding(curwin->w_botline, NULL,
3250 &curwin->w_botline);
3251#endif
3252 ++curwin->w_botline;
3253 room -= i;
3254 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3255 }
3256 }
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 /*
3259 * When hit bottom of the file: move cursor down.
3260 */
3261 if (n > 0)
3262 {
3263# ifdef FEAT_FOLDING
3264 if (hasAnyFolding(curwin))
3265 {
3266 while (--n >= 0
3267 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3268 {
3269 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3270 &curwin->w_cursor.lnum);
3271 ++curwin->w_cursor.lnum;
3272 }
3273 }
3274 else
3275# endif
3276 curwin->w_cursor.lnum += n;
3277 check_cursor_lnum();
3278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 else
3281 {
3282 /*
3283 * scroll the text down
3284 */
3285 while (n > 0 && curwin->w_topline > 1)
3286 {
3287#ifdef FEAT_DIFF
3288 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3289 {
3290 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003291 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 ++curwin->w_topfill;
3293 }
3294 else
3295#endif
3296 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003297 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 n -= i;
3299 if (n < 0 && scrolled > 0)
3300 break;
3301 --curwin->w_topline;
3302#ifdef FEAT_FOLDING
3303 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3304#endif
3305#ifdef FEAT_DIFF
3306 curwin->w_topfill = 0;
3307#endif
3308 }
3309 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3310 VALID_BOTLINE|VALID_BOTLINE_AP);
3311 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (curwin->w_cursor.lnum > 1)
3313 {
3314 --curwin->w_cursor.lnum;
3315 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 /*
3320 * When hit top of the file: move cursor up.
3321 */
3322 if (n > 0)
3323 {
3324 if (curwin->w_cursor.lnum <= (linenr_T)n)
3325 curwin->w_cursor.lnum = 1;
3326 else
3327# ifdef FEAT_FOLDING
3328 if (hasAnyFolding(curwin))
3329 {
3330 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3331 {
3332 --curwin->w_cursor.lnum;
3333 (void)hasFolding(curwin->w_cursor.lnum,
3334 &curwin->w_cursor.lnum, NULL);
3335 }
3336 }
3337 else
3338# endif
3339 curwin->w_cursor.lnum -= n;
3340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
3342# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003343 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 foldAdjustCursor();
3345# endif
3346#ifdef FEAT_DIFF
3347 check_topfill(curwin, !flag);
3348#endif
3349 cursor_correct();
3350 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003351 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003353
Bram Moolenaar860cae12010-06-05 23:22:07 +02003354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003355do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003356{
3357 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003358 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003359 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003360 colnr_T curswant = curwin->w_curswant;
3361 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003362 win_T *old_curwin = curwin;
3363 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003364 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003365 int old_VIsual_select = VIsual_select;
3366 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003367
3368 /*
3369 * loop through the cursorbound windows
3370 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003371 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003372 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003373 {
3374 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003375 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003376 if (curwin != old_curwin && curwin->w_p_crb)
3377 {
3378# ifdef FEAT_DIFF
3379 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003380 curwin->w_cursor.lnum =
3381 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003382 else
3383# endif
3384 curwin->w_cursor.lnum = line;
3385 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003386 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003387 curwin->w_curswant = curswant;
3388 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003389
Bram Moolenaar85a20022019-12-21 18:25:54 +01003390 // Make sure the cursor is in a valid position. Temporarily set
3391 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003392 restart_edit_save = restart_edit;
3393 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003394 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003395
3396 // Avoid a scroll here for the cursor position, 'scrollbind' is
3397 // more important.
3398 if (!curwin->w_p_scb)
3399 validate_cursor();
3400
Bram Moolenaar61452852011-02-01 18:01:11 +01003401 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003402 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003403 if (has_mbyte)
3404 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003405 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003406
Bram Moolenaar85a20022019-12-21 18:25:54 +01003407 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003408 if (!curwin->w_p_scb)
3409 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003410 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003411 }
3412 }
3413
3414 /*
3415 * reset current-window
3416 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003417 VIsual_select = old_VIsual_select;
3418 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003419 curwin = old_curwin;
3420 curbuf = old_curbuf;
3421}