blob: 3c359468e5bad85e8a5393890ddafc8d99721f86 [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/*
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000205 * Set curwin->s_skipcol to zero and redraw later if needed.
206 */
207 static void
208reset_skipcol(void)
209{
210 if (curwin->w_skipcol != 0)
211 {
212 curwin->w_skipcol = 0;
213
214 // Should use the least expensive way that displays all that changed.
215 // UPD_NOT_VALID is too expensive, UPD_REDRAW_TOP does not redraw
216 // enough when the top line gets another screen line.
217 redraw_later(UPD_SOME_VALID);
218 }
219}
220
221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 * Update curwin->w_topline and redraw if necessary.
223 * Used to update the screen before printing a message.
224 */
225 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100226update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227{
228 update_topline();
229 if (must_redraw)
230 update_screen(0);
231}
232
233/*
234 * Update curwin->w_topline to move the cursor onto the screen.
235 */
236 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100237update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 long line_count;
240 int halfheight;
241 int n;
242 linenr_T old_topline;
243#ifdef FEAT_DIFF
244 int old_topfill;
245#endif
246#ifdef FEAT_FOLDING
247 linenr_T lnum;
248#endif
249 int check_topline = FALSE;
250 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100251 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100252 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100254 // Cursor is updated instead when this is TRUE for 'splitkeep'.
255 if (skip_update_topline)
256 return;
257
Bram Moolenaar85a20022019-12-21 18:25:54 +0100258 // If there is no valid screen and when the window height is zero just use
259 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200260 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200261 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100262 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200263 curwin->w_topline = curwin->w_cursor.lnum;
264 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200265 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200266 return;
267 }
268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 check_cursor_moved(curwin);
270 if (curwin->w_valid & VALID_TOPLINE)
271 return;
272
Bram Moolenaar85a20022019-12-21 18:25:54 +0100273 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000274 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100275 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276
277 old_topline = curwin->w_topline;
278#ifdef FEAT_DIFF
279 old_topfill = curwin->w_topfill;
280#endif
281
282 /*
283 * If the buffer is empty, always set topline to 1.
284 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100285 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 {
287 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100288 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 curwin->w_botline = 2;
291 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 }
294
295 /*
296 * If the cursor is above or near the top of the window, scroll the window
297 * to show the line the cursor is in, with 'scrolloff' context.
298 */
299 else
300 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100301 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100303 // If the cursor is above topline, scrolling is always needed.
304 // If the cursor is far below topline and there is no folding,
305 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 if (curwin->w_cursor.lnum < curwin->w_topline)
307 check_topline = TRUE;
308 else if (check_top_offset())
309 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100310 else if (curwin->w_cursor.lnum == curwin->w_topline)
311 {
312 colnr_T vcol;
313
314 // check the cursor position is visible. Add 3 for the ">>>"
315 // displayed in the top-left.
316 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
317 if (curwin->w_skipcol + 3 >= vcol)
318 check_topline = TRUE;
319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
324 curwin->w_topline))
325 check_topline = TRUE;
326#endif
327
328 if (check_topline)
329 {
330 halfheight = curwin->w_height / 2 - 1;
331 if (halfheight < 2)
332 halfheight = 2;
333
334#ifdef FEAT_FOLDING
335 if (hasAnyFolding(curwin))
336 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100337 // Count the number of logical lines between the cursor and
338 // topline + scrolloff (approximation of how much will be
339 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 n = 0;
341 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100342 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
344 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100345 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
347 break;
348 (void)hasFolding(lnum, NULL, &lnum);
349 }
350 }
351 else
352#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100353 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaar85a20022019-12-21 18:25:54 +0100355 // If we weren't very close to begin with, we scroll to put the
356 // cursor in the middle of the window. Otherwise put the cursor
357 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 if (n >= halfheight)
359 scroll_cursor_halfway(FALSE);
360 else
361 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000362 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 check_botline = TRUE;
364 }
365 }
366
367 else
368 {
369#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100370 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
372#endif
373 check_botline = TRUE;
374 }
375 }
376
377 /*
378 * If the cursor is below the bottom of the window, scroll the window
379 * to put the cursor on the window.
380 * When w_botline is invalid, recompute it first, to avoid a redraw later.
381 * If w_botline was approximated, we might need a redraw later in a few
382 * cases, but we don't want to spend (a lot of) time recomputing w_botline
383 * for every small change.
384 */
385 if (check_botline)
386 {
387 if (!(curwin->w_valid & VALID_BOTLINE_AP))
388 validate_botline();
389
390 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
391 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000392 if (curwin->w_cursor.lnum < curwin->w_botline)
393 {
394 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100395 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_FOLDING
397 || hasAnyFolding(curwin)
398#endif
399 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 lineoff_T loff;
402
Bram Moolenaar85a20022019-12-21 18:25:54 +0100403 // Cursor is (a few lines) above botline, check if there are
404 // 'scrolloff' window lines below the cursor. If not, need to
405 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 n = curwin->w_empty_rows;
407 loff.lnum = curwin->w_cursor.lnum;
408#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
411#endif
412#ifdef FEAT_DIFF
413 loff.fill = 0;
414 n += curwin->w_filler_rows;
415#endif
416 loff.height = 0;
417 while (loff.lnum < curwin->w_botline
418#ifdef FEAT_DIFF
419 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
420#endif
421 )
422 {
423 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100424 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 break;
426 botline_forw(&loff);
427 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100428 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100429 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000431 }
432 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100433 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000434 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 }
436 if (check_botline)
437 {
438#ifdef FEAT_FOLDING
439 if (hasAnyFolding(curwin))
440 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100441 // Count the number of logical lines between the cursor and
442 // botline - scrolloff (approximation of how much will be
443 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 line_count = 0;
445 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100446 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {
448 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100449 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (lnum <= 0 || line_count > curwin->w_height + 1)
451 break;
452 (void)hasFolding(lnum, &lnum, NULL);
453 }
454 }
455 else
456#endif
457 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100458 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000460 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 else
462 scroll_cursor_halfway(FALSE);
463 }
464 }
465 }
466 curwin->w_valid |= VALID_TOPLINE;
467
468 /*
469 * Need to redraw when topline changed.
470 */
471 if (curwin->w_topline != old_topline
472#ifdef FEAT_DIFF
473 || curwin->w_topfill != old_topfill
474#endif
475 )
476 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100477 dollar_vcol = -1;
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000478 redraw_later(UPD_VALID);
479 reset_skipcol();
480
Bram Moolenaar85a20022019-12-21 18:25:54 +0100481 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 if (curwin->w_cursor.lnum == curwin->w_topline)
483 validate_cursor();
484 }
485
Bram Moolenaar375e3392019-01-31 18:26:10 +0100486 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487}
488
489/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000490 * Return the scrolljump value to use for the current window.
491 * When 'scrolljump' is positive use it as-is.
492 * When 'scrolljump' is negative use it as a percentage of the window height.
493 */
494 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100495scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000496{
497 if (p_sj >= 0)
498 return (int)p_sj;
499 return (curwin->w_height * -p_sj) / 100;
500}
501
502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
504 * current window.
505 */
506 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100507check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508{
509 lineoff_T loff;
510 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100511 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar375e3392019-01-31 18:26:10 +0100513 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514#ifdef FEAT_FOLDING
515 || hasAnyFolding(curwin)
516#endif
517 )
518 {
519 loff.lnum = curwin->w_cursor.lnum;
520#ifdef FEAT_DIFF
521 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100522 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523#else
524 n = 0;
525#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100526 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100527 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
529 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100530 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 if (loff.lnum < curwin->w_topline
532#ifdef FEAT_DIFF
533 || (loff.lnum == curwin->w_topline && loff.fill > 0)
534#endif
535 )
536 break;
537 n += loff.height;
538 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100539 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 return TRUE;
541 }
542 return FALSE;
543}
544
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100545/*
546 * Update w_curswant.
547 */
548 void
549update_curswant_force(void)
550{
551 validate_virtcol();
552 curwin->w_curswant = curwin->w_virtcol
553#ifdef FEAT_PROP_POPUP
554 - curwin->w_virtcol_first_char
555#endif
556 ;
557 curwin->w_set_curswant = FALSE;
558}
559
560/*
561 * Update w_curswant if w_set_curswant is set.
562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100564update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565{
566 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100567 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568}
569
570/*
571 * Check if the cursor has moved. Set the w_valid flag accordingly.
572 */
573 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100574check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
576 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
577 {
578 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100579 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
580 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 wp->w_valid_cursor = wp->w_cursor;
582 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100583 wp->w_valid_skipcol = wp->w_skipcol;
584 }
585 else if (wp->w_skipcol != wp->w_valid_skipcol)
586 {
587 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
588 |VALID_CHEIGHT|VALID_CROW
589 |VALID_BOTLINE|VALID_BOTLINE_AP);
590 wp->w_valid_cursor = wp->w_cursor;
591 wp->w_valid_leftcol = wp->w_leftcol;
592 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594 else if (wp->w_cursor.col != wp->w_valid_cursor.col
595 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100596 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 {
598 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
599 wp->w_valid_cursor.col = wp->w_cursor.col;
600 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603}
604
605/*
606 * Call this function when some window settings have changed, which require
607 * the cursor position, botline and topline to be recomputed and the window to
608 * be redrawn. E.g, when changing the 'wrap' option or folding.
609 */
610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100611changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 changed_window_setting_win(curwin);
614}
615
616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100617changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 wp->w_lines_valid = 0;
620 changed_line_abv_curs_win(wp);
621 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100622 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623}
624
625/*
626 * Set wp->w_topline to a certain number.
627 */
628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100629set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200631#ifdef FEAT_DIFF
632 linenr_T prev_topline = wp->w_topline;
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100636 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
638#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100639 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100641 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
642 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000644 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200646 if (lnum != prev_topline)
647 // Keep the filler lines when the topline didn't change.
648 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#endif
650 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100651 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100652 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654
655/*
656 * Call this function when the length of the cursor line (in screen
657 * characters) has changed, and the change is before the cursor.
658 * Need to take care of w_botline separately!
659 */
660 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100661changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
664 |VALID_CHEIGHT|VALID_TOPLINE);
665}
666
667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100668changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
671 |VALID_CHEIGHT|VALID_TOPLINE);
672}
673
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674/*
675 * Call this function when the length of a line (in screen characters) above
676 * the cursor have changed.
677 * Need to take care of w_botline separately!
678 */
679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
683 |VALID_CHEIGHT|VALID_TOPLINE);
684}
685
686 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100687changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
689 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
690 |VALID_CHEIGHT|VALID_TOPLINE);
691}
692
693/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100694 * Display of line has changed for "buf", invalidate cursor position and
695 * w_botline.
696 */
697 void
698changed_line_display_buf(buf_T *buf)
699{
700 win_T *wp;
701
702 FOR_ALL_WINDOWS(wp)
703 if (wp->w_buffer == buf)
704 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
705 |VALID_CROW|VALID_CHEIGHT
706 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
707}
708
709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 * Make sure the value of curwin->w_botline is valid.
711 */
712 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100713validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100715 validate_botline_win(curwin);
716}
717
718/*
719 * Make sure the value of wp->w_botline is valid.
720 */
721 void
722validate_botline_win(win_T *wp)
723{
724 if (!(wp->w_valid & VALID_BOTLINE))
725 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726}
727
728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 * Mark curwin->w_botline as invalid (because of some change in the buffer).
730 */
731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100732invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
734 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
735}
736
737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100738invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
740 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
741}
742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100744approximate_botline_win(
745 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 wp->w_valid &= ~VALID_BOTLINE;
748}
749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750/*
751 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
752 */
753 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100754cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
756 check_cursor_moved(curwin);
757 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
758 (VALID_WROW|VALID_WCOL));
759}
760
761/*
762 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
763 * w_topline must be valid, you may need to call update_topline() first!
764 */
765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100766validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100768 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 check_cursor_moved(curwin);
770 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
771 curs_columns(TRUE);
772}
773
774#if defined(FEAT_GUI) || defined(PROTO)
775/*
776 * validate w_cline_row.
777 */
778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100779validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
781 /*
782 * First make sure that w_topline is valid (after moving the cursor).
783 */
784 update_topline();
785 check_cursor_moved(curwin);
786 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100787 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789#endif
790
791/*
792 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200793 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 */
795 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100796curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 linenr_T lnum;
799 int i;
800 int all_invalid;
801 int valid;
802#ifdef FEAT_FOLDING
803 long fold_count;
804#endif
805
Bram Moolenaar85a20022019-12-21 18:25:54 +0100806 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 all_invalid = (!redrawing()
808 || wp->w_lines_valid == 0
809 || wp->w_lines[0].wl_lnum > wp->w_topline);
810 i = 0;
811 wp->w_cline_row = 0;
812 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
813 {
814 valid = FALSE;
815 if (!all_invalid && i < wp->w_lines_valid)
816 {
817 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100818 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (wp->w_lines[i].wl_lnum == lnum)
820 {
821#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100822 // Check for newly inserted lines below this row, in which
823 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (!wp->w_buffer->b_mod_set
825 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
826 || wp->w_buffer->b_mod_top
827 > wp->w_lines[i].wl_lastlnum + 1)
828#endif
829 valid = TRUE;
830 }
831 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100832 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 if (valid
835#ifdef FEAT_DIFF
836 && (lnum != wp->w_topline || !wp->w_p_diff)
837#endif
838 )
839 {
840#ifdef FEAT_FOLDING
841 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100842 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (lnum > wp->w_cursor.lnum)
844 break;
845#else
846 ++lnum;
847#endif
848 wp->w_cline_row += wp->w_lines[i].wl_size;
849 }
850 else
851 {
852#ifdef FEAT_FOLDING
853 fold_count = foldedCount(wp, lnum, NULL);
854 if (fold_count)
855 {
856 lnum += fold_count;
857 if (lnum > wp->w_cursor.lnum)
858 break;
859 ++wp->w_cline_row;
860 }
861 else
862#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100863 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100864 wp->w_cline_row += plines_correct_topline(wp, lnum);
865 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868 }
869
870 check_cursor_moved(wp);
871 if (!(wp->w_valid & VALID_CHEIGHT))
872 {
873 if (all_invalid
874 || i == wp->w_lines_valid
875 || (i < wp->w_lines_valid
876 && (!wp->w_lines[i].wl_valid
877 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
878 {
879#ifdef FEAT_DIFF
880 if (wp->w_cursor.lnum == wp->w_topline)
881 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
882 TRUE) + wp->w_topfill;
883 else
884#endif
885 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
886#ifdef FEAT_FOLDING
887 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
888 NULL, NULL, TRUE, NULL);
889#endif
890 }
891 else if (i > wp->w_lines_valid)
892 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100893 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 wp->w_cline_height = 0;
895#ifdef FEAT_FOLDING
896 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
897 NULL, NULL, TRUE, NULL);
898#endif
899 }
900 else
901 {
902 wp->w_cline_height = wp->w_lines[i].wl_size;
903#ifdef FEAT_FOLDING
904 wp->w_cline_folded = wp->w_lines[i].wl_folded;
905#endif
906 }
907 }
908
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100909 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911}
912
913/*
914 * Validate curwin->w_virtcol only.
915 */
916 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100917validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 validate_virtcol_win(curwin);
920}
921
922/*
923 * Validate wp->w_virtcol only.
924 */
925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 check_cursor_moved(wp);
929 if (!(wp->w_valid & VALID_VIRTCOL))
930 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100931#ifdef FEAT_PROP_POPUP
932 wp->w_virtcol_first_char = 0;
933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000935#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100936 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000937#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100938 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940}
941
942/*
943 * Validate curwin->w_cline_height only.
944 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100945 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100946validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 check_cursor_moved(curwin);
949 if (!(curwin->w_valid & VALID_CHEIGHT))
950 {
951#ifdef FEAT_DIFF
952 if (curwin->w_cursor.lnum == curwin->w_topline)
953 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
954 + curwin->w_topfill;
955 else
956#endif
957 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
958#ifdef FEAT_FOLDING
959 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
960#endif
961 curwin->w_valid |= VALID_CHEIGHT;
962 }
963}
964
965/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000966 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 */
968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100969validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 colnr_T off;
972 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100973 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 validate_virtcol();
976 if (!(curwin->w_valid & VALID_WCOL))
977 {
978 col = curwin->w_virtcol;
979 off = curwin_col_off();
980 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200981 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar85a20022019-12-21 18:25:54 +0100983 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000984 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200985 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100986 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100987 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200988 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000989 if (col > (int)curwin->w_leftcol)
990 col -= curwin->w_leftcol;
991 else
992 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100996#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100997 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 }
1000}
1001
1002/*
Bram Moolenaar64486672010-05-16 15:46:46 +02001003 * Compute offset of a window, occupied by absolute or relative line number,
1004 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 */
1006 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001007win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
Bram Moolenaar64486672010-05-16 15:46:46 +02001009 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef FEAT_FOLDING
1012 + wp->w_p_fdc
1013#endif
1014#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001015 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#endif
1017 );
1018}
1019
1020 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001021curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 return win_col_off(curwin);
1024}
1025
1026/*
1027 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001028 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1029 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 */
1031 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001032win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar64486672010-05-16 15:46:46 +02001034 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001035 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 return 0;
1037}
1038
1039 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001040curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 return win_col_off2(curwin);
1043}
1044
1045/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001046 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Also updates curwin->w_wrow and curwin->w_cline_row.
1048 * Also updates curwin->w_leftcol.
1049 */
1050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001051curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001052 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001055 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 int off_left, off_right;
1057 int n;
1058 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001059 int width1; // text width for first screen line
1060 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 int new_leftcol;
1062 colnr_T startcol;
1063 colnr_T endcol;
1064 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001065 long so = get_scrolloff_value();
1066 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001067 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069 /*
1070 * First make sure that w_topline is valid (after moving the cursor).
1071 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001072 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
1074 /*
1075 * Next make sure that w_cline_row is valid.
1076 */
1077 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001078 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001080#ifdef FEAT_PROP_POPUP
1081 // will be set by getvvcol() but not reset
1082 curwin->w_virtcol_first_char = 0;
1083#endif
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 /*
1086 * Compute the number of virtual columns.
1087 */
1088#ifdef FEAT_FOLDING
1089 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001090 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1092 else
1093#endif
1094 getvvcol(curwin, &curwin->w_cursor,
1095 &startcol, &(curwin->w_virtcol), &endcol);
1096
Bram Moolenaar85a20022019-12-21 18:25:54 +01001097 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001099 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
1101 extra = curwin_col_off();
1102 curwin->w_wcol = curwin->w_virtcol + extra;
1103 endcol += extra;
1104
1105 /*
1106 * Now compute w_wrow, counting screen lines from w_cline_row.
1107 */
1108 curwin->w_wrow = curwin->w_cline_row;
1109
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001110 width1 = curwin->w_width - extra;
1111 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001114 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001115 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001116 if (curwin->w_p_wrap)
1117 curwin->w_wrow = curwin->w_height - 1;
1118 else
1119 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001121 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001123 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001125 // skip columns that are not visible
1126 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001127 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001128 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001129 {
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001130 // Deduct by multiples of width2. This allows the long line
1131 // wrapping formula below to correctly calculate the w_wcol value
1132 // when wrapping.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001133 if (curwin->w_skipcol <= width1)
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001134 curwin->w_wcol -= width2;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001135 else
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001136 curwin->w_wcol -= width2
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001137 * (((curwin->w_skipcol - width1) / width2) + 1);
Yee Cheng Chin01ee52b2022-11-17 12:41:42 +00001138
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001139 did_sub_skipcol = TRUE;
1140 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001141
Bram Moolenaar85a20022019-12-21 18:25:54 +01001142 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001143 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001145 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001146 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1147 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 curwin->w_wrow += n;
1149
1150#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001151 // When cursor wraps to first char of next line in Insert
1152 // mode, the 'showbreak' string isn't shown, backup to first
1153 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001154 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001155 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001156 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 curwin->w_wcol = 0;
1158#endif
1159 }
1160 }
1161
Bram Moolenaar85a20022019-12-21 18:25:54 +01001162 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1163 // is not folded.
1164 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001165 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166#ifdef FEAT_FOLDING
1167 && !curwin->w_cline_folded
1168#endif
1169 )
1170 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001171#ifdef FEAT_PROP_POPUP
1172 if (curwin->w_virtcol_first_char > 0)
1173 {
1174 int cols = (curwin->w_width - extra);
1175 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1176
1177 // each "above" text prop shifts the text one row down
1178 curwin->w_wrow += rows;
1179 curwin->w_wcol -= rows * cols;
1180 endcol -= rows * cols;
1181 curwin->w_cline_height = rows + 1;
1182 }
1183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 /*
1185 * If Cursor is left of the screen, scroll rightwards.
1186 * If Cursor is right of the screen, scroll leftwards
1187 * If we get closer to the edge than 'sidescrolloff', scroll a little
1188 * extra
1189 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001190 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001191 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001192 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (off_left < 0 || off_right > 0)
1194 {
1195 if (off_left < 0)
1196 diff = -off_left;
1197 else
1198 diff = off_right;
1199
Bram Moolenaar85a20022019-12-21 18:25:54 +01001200 // When far off or not enough room on either side, put cursor in
1201 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001202 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1203 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 else
1205 {
1206 if (diff < p_ss)
1207 diff = p_ss;
1208 if (off_left < 0)
1209 new_leftcol = curwin->w_leftcol - diff;
1210 else
1211 new_leftcol = curwin->w_leftcol + diff;
1212 }
1213 if (new_leftcol < 0)
1214 new_leftcol = 0;
1215 if (new_leftcol != (int)curwin->w_leftcol)
1216 {
1217 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001218 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001219 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221 }
1222 curwin->w_wcol -= curwin->w_leftcol;
1223 }
1224 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1225 curwin->w_wcol -= curwin->w_leftcol;
1226 else
1227 curwin->w_wcol = 0;
1228
1229#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001230 // Skip over filler lines. At the top use w_topfill, there
1231 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (curwin->w_cursor.lnum == curwin->w_topline)
1233 curwin->w_wrow += curwin->w_topfill;
1234 else
1235 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1236#endif
1237
1238 prev_skipcol = curwin->w_skipcol;
1239
1240 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if ((curwin->w_wrow >= curwin->w_height
1243 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001244 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 && (p_lines =
1246#ifdef FEAT_DIFF
1247 plines_win_nofill
1248#else
1249 plines_win
1250#endif
1251 (curwin, curwin->w_cursor.lnum, FALSE))
1252 - 1 >= curwin->w_height))
1253 && curwin->w_height != 0
1254 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001255 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001256 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001258 // Cursor past end of screen. Happens with a single line that does
1259 // not fit on screen. Find a skipcol to show the text around the
1260 // cursor. Avoid scrolling all the time. compute value of "extra":
1261 // 1: Less than 'scrolloff' lines above
1262 // 2: Less than 'scrolloff' lines below
1263 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001265 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001267 // Compute last display line of the buffer line that we want at the
1268 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (p_lines == 0)
1270 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1271 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001272 if (p_lines > curwin->w_wrow + so)
1273 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
1275 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001276 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 extra += 2;
1278
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001279 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001281 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001282 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (n > curwin->w_height / 2)
1284 n -= curwin->w_height / 2;
1285 else
1286 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001287 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (n > p_lines - curwin->w_height + 1)
1289 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001290 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 else if (extra == 1)
1293 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001294 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001295 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1296 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 if (extra > 0)
1298 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001299 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1300 extra = curwin->w_skipcol / width2;
1301 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 }
1303 }
1304 else if (extra == 2)
1305 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001306 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001307 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001309 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if (endcol > curwin->w_skipcol)
1311 curwin->w_skipcol = endcol;
1312 }
1313
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001314 // adjust w_wrow for the changed w_skipcol
1315 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001316 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001317 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001318 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (curwin->w_wrow >= curwin->w_height)
1321 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001322 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001324 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 curwin->w_wrow -= extra;
1326 }
1327
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001328 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (extra > 0)
1330 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1331 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001332 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001334 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 curwin->w_skipcol = 0;
1336 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001337 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001339#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001340 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001341#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001342#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1343 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1344 {
1345 curwin->w_wrow += popup_top_extra(curwin);
1346 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001347 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001348 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001349 else
1350 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001351#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001352
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001353 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1354 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001355 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001356 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1359}
1360
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001361#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001362/*
1363 * Compute the screen position of text character at "pos" in window "wp"
1364 * The resulting values are one-based, zero when character is not visible.
1365 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001366 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001367textpos2screenpos(
1368 win_T *wp,
1369 pos_T *pos,
1370 int *rowp, // screen row
1371 int *scolp, // start screen column
1372 int *ccolp, // cursor screen column
1373 int *ecolp) // end screen column
1374{
1375 colnr_T scol = 0, ccol = 0, ecol = 0;
1376 int row = 0;
1377 int rowoff = 0;
1378 colnr_T coloff = 0;
1379
Bram Moolenaar189663b2021-07-21 18:04:56 +02001380 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001381 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001382 colnr_T off;
1383 colnr_T col;
1384 int width;
1385 linenr_T lnum = pos->lnum;
1386#ifdef FEAT_FOLDING
1387 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001388
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001389 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1390#endif
1391 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1392#ifdef FEAT_FOLDING
1393 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001394 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001395 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001396 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001397 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001398 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001399#endif
1400 {
1401 getvcol(wp, pos, &scol, &ccol, &ecol);
1402
1403 // similar to what is done in validate_cursor_col()
1404 col = scol;
1405 off = win_col_off(wp);
1406 col += off;
1407 width = wp->w_width - off + win_col_off2(wp);
1408
1409 // long line wrapping, adjust row
1410 if (wp->w_p_wrap
1411 && col >= (colnr_T)wp->w_width
1412 && width > 0)
1413 {
1414 // use same formula as what is used in curs_columns()
1415 rowoff = ((col - wp->w_width) / width + 1);
1416 col -= rowoff * width;
1417 }
1418 col -= wp->w_leftcol;
1419 if (col >= wp->w_width)
1420 col = -1;
1421 if (col >= 0 && row + rowoff <= wp->w_height)
1422 {
1423 coloff = col - scol + wp->w_wincol + 1;
1424 row += W_WINROW(wp);
1425 }
1426 else
1427 // character is left, right or below of the window
1428 row = rowoff = scol = ccol = ecol = 0;
1429 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001430 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001431 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001432 *scolp = scol + coloff;
1433 *ccolp = ccol + coloff;
1434 *ecolp = ecol + coloff;
1435}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001436#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001437
Bram Moolenaar12034e22019-08-25 22:25:02 +02001438#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001439/*
1440 * "screenpos({winid}, {lnum}, {col})" function
1441 */
1442 void
1443f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1444{
1445 dict_T *dict;
1446 win_T *wp;
1447 pos_T pos;
1448 int row = 0;
1449 int scol = 0, ccol = 0, ecol = 0;
1450
Bram Moolenaar93a10962022-06-16 11:42:09 +01001451 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001452 return;
1453 dict = rettv->vval.v_dict;
1454
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001455 if (in_vim9script()
1456 && (check_for_number_arg(argvars, 0) == FAIL
1457 || check_for_number_arg(argvars, 1) == FAIL
1458 || check_for_number_arg(argvars, 2) == FAIL))
1459 return;
1460
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001461 wp = find_win_by_nr_or_id(&argvars[0]);
1462 if (wp == NULL)
1463 return;
1464
1465 pos.lnum = tv_get_number(&argvars[1]);
1466 pos.col = tv_get_number(&argvars[2]) - 1;
1467 pos.coladd = 0;
1468 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1469
1470 dict_add_number(dict, "row", row);
1471 dict_add_number(dict, "col", scol);
1472 dict_add_number(dict, "curscol", ccol);
1473 dict_add_number(dict, "endcol", ecol);
1474}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001475
1476/*
1477 * "virtcol2col({winid}, {lnum}, {col})" function
1478 */
1479 void
1480f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1481{
1482 win_T *wp;
1483 linenr_T lnum;
1484 int screencol;
1485 int error = FALSE;
1486
1487 rettv->vval.v_number = -1;
1488
1489 if (check_for_number_arg(argvars, 0) == FAIL
1490 || check_for_number_arg(argvars, 1) == FAIL
1491 || check_for_number_arg(argvars, 2) == FAIL)
1492 return;
1493
1494 wp = find_win_by_nr_or_id(&argvars[0]);
1495 if (wp == NULL)
1496 return;
1497
1498 lnum = tv_get_number_chk(&argvars[1], &error);
1499 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1500 return;
1501
1502 screencol = tv_get_number_chk(&argvars[2], &error);
1503 if (error || screencol < 0)
1504 return;
1505
1506 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1507}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001508#endif
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510/*
1511 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001514scrolldown(
1515 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001516 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001518 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 int wrow;
1520 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001521 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001522 int width1 = 0;
1523 int width2 = 0;
1524
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001525 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001526 {
1527 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001528 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
1531#ifdef FEAT_FOLDING
1532 linenr_T first;
1533
Bram Moolenaar85a20022019-12-21 18:25:54 +01001534 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1536#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001537 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001538 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
1540#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001541 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1542 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 ++curwin->w_topfill;
1545 ++done;
1546 }
1547 else
1548#endif
1549 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001550 // break when at the very top
1551 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001552 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001554 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001556 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001557 if (curwin->w_skipcol >= width1 + width2)
1558 curwin->w_skipcol -= width2;
1559 else
1560 curwin->w_skipcol -= width1;
1561 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001565 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001566 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001567 --curwin->w_topline;
1568 curwin->w_skipcol = 0;
1569#ifdef FEAT_DIFF
1570 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001572#ifdef FEAT_FOLDING
1573 // A sequence of folded lines only counts for one logical line
1574 if (hasFolding(curwin->w_topline, &first, NULL))
1575 {
1576 ++done;
1577 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001578 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001579 curwin->w_botline -= curwin->w_topline - first;
1580 curwin->w_topline = first;
1581 }
1582 else
1583#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001584 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001585 {
1586 int size = win_linetabsize(curwin, curwin->w_topline,
1587 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1588 if (size > width1)
1589 {
1590 curwin->w_skipcol = width1;
1591 size -= width1;
1592 redraw_later(UPD_NOT_VALID);
1593 }
1594 while (size > width2)
1595 {
1596 curwin->w_skipcol += width2;
1597 size -= width2;
1598 }
1599 ++done;
1600 }
1601 else
1602 done += PLINES_NOFILL(curwin->w_topline);
1603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001605 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 invalidate_botline();
1607 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001608 curwin->w_wrow += done; // keep w_wrow updated
1609 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611#ifdef FEAT_DIFF
1612 if (curwin->w_cursor.lnum == curwin->w_topline)
1613 curwin->w_cline_row = 0;
1614 check_topfill(curwin, TRUE);
1615#endif
1616
1617 /*
1618 * Compute the row number of the last row of the cursor line
1619 * and move the cursor onto the displayed part of the window.
1620 */
1621 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001622 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 validate_virtcol();
1625 validate_cheight();
1626 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001627 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1630 {
1631#ifdef FEAT_FOLDING
1632 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1633 {
1634 --wrow;
1635 if (first == 1)
1636 curwin->w_cursor.lnum = 1;
1637 else
1638 curwin->w_cursor.lnum = first - 1;
1639 }
1640 else
1641#endif
1642 wrow -= plines(curwin->w_cursor.lnum--);
1643 curwin->w_valid &=
1644 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1645 moved = TRUE;
1646 }
1647 if (moved)
1648 {
1649#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001650 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 foldAdjustCursor();
1652#endif
1653 coladvance(curwin->w_curswant);
1654 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001655
1656 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1657 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001658 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001659 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1660
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001661 // make sure the cursor is in the visible text
1662 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001663 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001664 int row = 0;
1665 if (col >= width1)
1666 {
1667 col -= width1;
1668 ++row;
1669 }
1670 if (col > width2)
1671 {
1672 row += col / width2;
1673 col = col % width2;
1674 }
1675 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001676 {
1677 curwin->w_curswant = curwin->w_virtcol
1678 - (row - curwin->w_height + 1) * width2;
1679 coladvance(curwin->w_curswant);
1680 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682}
1683
1684/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001685 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1686 */
1687 static int
1688scrolling_screenlines(int byfold UNUSED)
1689{
1690 return (curwin->w_p_wrap && curwin->w_p_sms)
1691# ifdef FEAT_FOLDING
1692 || (byfold && hasAnyFolding(curwin))
1693# endif
1694# ifdef FEAT_DIFF
1695 || curwin->w_p_diff
1696# endif
1697 ;
1698}
1699
1700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001704scrollup(
1705 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001706 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001708 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001710 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001712 int width1 = curwin->w_width - curwin_col_off();
1713 int width2 = width1 + curwin_col_off2();
1714 int size = 0;
1715 linenr_T prev_topline = curwin->w_topline;
1716
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001717 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001718 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001719
1720 // diff mode: first consume "topfill"
1721 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1722 // the line, then advance to the next line.
1723 // folding: count each sequence of folded lines as one logical line.
1724 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 {
1726# ifdef FEAT_DIFF
1727 if (curwin->w_topfill > 0)
1728 --curwin->w_topfill;
1729 else
1730# endif
1731 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001732 linenr_T lnum = curwin->w_topline;
1733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734# ifdef FEAT_FOLDING
1735 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001736 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 (void)hasFolding(lnum, NULL, &lnum);
1738# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001739 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001740 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001741 // 'smoothscroll': increase "w_skipcol" until it goes over
1742 // the end of the line, then advance to the next line.
1743 int add = curwin->w_skipcol > 0 ? width2 : width1;
1744 curwin->w_skipcol += add;
1745 if (curwin->w_skipcol >= size)
1746 {
1747 if (lnum == curbuf->b_ml.ml_line_count)
1748 {
1749 // at the last screen line, can't scroll further
1750 curwin->w_skipcol -= add;
1751 break;
1752 }
1753 ++lnum;
1754 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001755 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001756 else
1757 {
1758 if (lnum >= curbuf->b_ml.ml_line_count)
1759 break;
1760 ++lnum;
1761 }
1762
1763 if (lnum > curwin->w_topline)
1764 {
1765 // approximate w_botline
1766 curwin->w_botline += lnum - curwin->w_topline;
1767 curwin->w_topline = lnum;
1768# ifdef FEAT_DIFF
1769 curwin->w_topfill = diff_check_fill(curwin, lnum);
1770# endif
1771 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001772 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001773 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001774 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001775 }
1776 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001777
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001778 if (curwin->w_topline == prev_topline)
1779 // need to redraw even though w_topline didn't change
1780 redraw_later(UPD_NOT_VALID);
1781 }
1782 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
1784 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001785 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
1787
1788 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1789 curwin->w_topline = curbuf->b_ml.ml_line_count;
1790 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1791 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1792
1793#ifdef FEAT_DIFF
1794 check_topfill(curwin, FALSE);
1795#endif
1796
1797#ifdef FEAT_FOLDING
1798 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001799 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1801#endif
1802
1803 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1804 if (curwin->w_cursor.lnum < curwin->w_topline)
1805 {
1806 curwin->w_cursor.lnum = curwin->w_topline;
1807 curwin->w_valid &=
1808 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1809 coladvance(curwin->w_curswant);
1810 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001811 if (curwin->w_cursor.lnum == curwin->w_topline
1812 && do_sms && curwin->w_skipcol > 0)
1813 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001814 int width1 = curwin->w_width - curwin_col_off();
1815 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001816 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001817 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001818 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001819
1820 // Make sure the cursor is in a visible part of the line, taking
1821 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001822 // If there are not enough screen lines put the cursor in the middle.
1823 if (scrolloff_cols > space_cols / 2)
1824 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001825 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001826 if (curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001827 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001828 colnr_T col = curwin->w_virtcol;
1829
1830 if (col < width1)
1831 col += width1;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001832 while (col < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001833 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001834 curwin->w_curswant = col;
1835 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001836
1837 // validate_virtcol() marked various things as valid, but after
1838 // moving the cursor they need to be recomputed
1839 curwin->w_valid &=
1840 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001841 }
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843}
1844
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001845/*
1846 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1847 * valid for 'smoothscroll'.
1848 */
1849 void
1850adjust_skipcol(void)
1851{
1852 if (!curwin->w_p_wrap
1853 || !curwin->w_p_sms
1854 || curwin->w_cursor.lnum != curwin->w_topline)
1855 return;
1856
1857 int width1 = curwin->w_width - curwin_col_off();
1858 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001859 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001860 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1861 int scrolled = FALSE;
1862
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001863 validate_cheight();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001864 if (curwin->w_cline_height == curwin->w_height)
1865 {
1866 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001867 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001868 return;
1869 }
1870
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001871 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001872 while (curwin->w_skipcol > 0
1873 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1874 {
1875 // scroll a screen line down
1876 if (curwin->w_skipcol >= width1 + width2)
1877 curwin->w_skipcol -= width2;
1878 else
1879 curwin->w_skipcol -= width1;
1880 redraw_later(UPD_NOT_VALID);
1881 scrolled = TRUE;
1882 validate_virtcol();
1883 }
1884 if (scrolled)
1885 return; // don't scroll in the other direction now
1886
1887 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1888 int row = 0;
1889 if (col >= width1)
1890 {
1891 col -= width1;
1892 ++row;
1893 }
1894 if (col > width2)
1895 {
1896 row += col / width2;
1897 col = col % width2;
1898 }
1899 if (row >= curwin->w_height)
1900 {
1901 if (curwin->w_skipcol == 0)
1902 {
1903 curwin->w_skipcol += width1;
1904 --row;
1905 }
1906 if (row >= curwin->w_height)
1907 curwin->w_skipcol += (row - curwin->w_height) * width2;
1908 redraw_later(UPD_NOT_VALID);
1909 }
1910}
1911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912#ifdef FEAT_DIFF
1913/*
1914 * Don't end up with too many filler lines in the window.
1915 */
1916 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001917check_topfill(
1918 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001919 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920{
1921 int n;
1922
1923 if (wp->w_topfill > 0)
1924 {
1925 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1926 if (wp->w_topfill + n > wp->w_height)
1927 {
1928 if (down && wp->w_topline > 1)
1929 {
1930 --wp->w_topline;
1931 wp->w_topfill = 0;
1932 }
1933 else
1934 {
1935 wp->w_topfill = wp->w_height - n;
1936 if (wp->w_topfill < 0)
1937 wp->w_topfill = 0;
1938 }
1939 }
1940 }
1941}
1942
1943/*
1944 * Use as many filler lines as possible for w_topline. Make sure w_topline
1945 * is still visible.
1946 */
1947 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001948max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949{
1950 int n;
1951
1952 n = plines_nofill(curwin->w_topline);
1953 if (n >= curwin->w_height)
1954 curwin->w_topfill = 0;
1955 else
1956 {
1957 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1958 if (curwin->w_topfill + n > curwin->w_height)
1959 curwin->w_topfill = curwin->w_height - n;
1960 }
1961}
1962#endif
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964/*
1965 * Scroll the screen one line down, but don't do it if it would move the
1966 * cursor off the screen.
1967 */
1968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001969scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
1971 int end_row;
1972#ifdef FEAT_DIFF
1973 int can_fill = (curwin->w_topfill
1974 < diff_check_fill(curwin, curwin->w_topline));
1975#endif
1976
1977 if (curwin->w_topline <= 1
1978#ifdef FEAT_DIFF
1979 && !can_fill
1980#endif
1981 )
1982 return;
1983
Bram Moolenaar85a20022019-12-21 18:25:54 +01001984 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
1986 /*
1987 * Compute the row number of the last row of the cursor line
1988 * and make sure it doesn't go off the screen. Make sure the cursor
1989 * doesn't go past 'scrolloff' lines from the screen end.
1990 */
1991 end_row = curwin->w_wrow;
1992#ifdef FEAT_DIFF
1993 if (can_fill)
1994 ++end_row;
1995 else
1996 end_row += plines_nofill(curwin->w_topline - 1);
1997#else
1998 end_row += plines(curwin->w_topline - 1);
1999#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002000 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 validate_cheight();
2003 validate_virtcol();
2004 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002005 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002007 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
2009#ifdef FEAT_DIFF
2010 if (can_fill)
2011 {
2012 ++curwin->w_topfill;
2013 check_topfill(curwin, TRUE);
2014 }
2015 else
2016 {
2017 --curwin->w_topline;
2018 curwin->w_topfill = 0;
2019 }
2020#else
2021 --curwin->w_topline;
2022#endif
2023#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002024 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002026 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2028 }
2029}
2030
2031/*
2032 * Scroll the screen one line up, but don't do it if it would move the cursor
2033 * off the screen.
2034 */
2035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002036scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
2038 int start_row;
2039
2040 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2041#ifdef FEAT_DIFF
2042 && curwin->w_topfill == 0
2043#endif
2044 )
2045 return;
2046
Bram Moolenaar85a20022019-12-21 18:25:54 +01002047 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 /*
2050 * Compute the row number of the first row of the cursor line
2051 * and make sure it doesn't go off the screen. Make sure the cursor
2052 * doesn't go before 'scrolloff' lines from the screen start.
2053 */
2054#ifdef FEAT_DIFF
2055 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2056 - curwin->w_topfill;
2057#else
2058 start_row = curwin->w_wrow - plines(curwin->w_topline);
2059#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002060 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002063 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002065 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
2067#ifdef FEAT_DIFF
2068 if (curwin->w_topfill > 0)
2069 --curwin->w_topfill;
2070 else
2071#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002072 {
2073#ifdef FEAT_FOLDING
2074 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002077 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002078 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2080 }
2081}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083/*
2084 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2085 * a (wrapped) text line. Uses and sets "lp->fill".
2086 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002087 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 */
2089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002090topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
2092#ifdef FEAT_DIFF
2093 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002095 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 ++lp->fill;
2097 lp->height = 1;
2098 }
2099 else
2100#endif
2101 {
2102 --lp->lnum;
2103#ifdef FEAT_DIFF
2104 lp->fill = 0;
2105#endif
2106 if (lp->lnum < 1)
2107 lp->height = MAXCOL;
2108 else
2109#ifdef FEAT_FOLDING
2110 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002111 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 lp->height = 1;
2113 else
2114#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002115 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117}
2118
2119/*
2120 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2121 * a (wrapped) text line. Uses and sets "lp->fill".
2122 * Returns the height of the added line in "lp->height".
2123 * Lines below the last one are incredibly high.
2124 */
2125 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002126botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128#ifdef FEAT_DIFF
2129 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2130 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002131 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ++lp->fill;
2133 lp->height = 1;
2134 }
2135 else
2136#endif
2137 {
2138 ++lp->lnum;
2139#ifdef FEAT_DIFF
2140 lp->fill = 0;
2141#endif
2142 if (lp->lnum > curbuf->b_ml.ml_line_count)
2143 lp->height = MAXCOL;
2144 else
2145#ifdef FEAT_FOLDING
2146 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002147 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 lp->height = 1;
2149 else
2150#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002151 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153}
2154
2155#ifdef FEAT_DIFF
2156/*
2157 * Switch from including filler lines below lp->lnum to including filler
2158 * lines above loff.lnum + 1. This keeps pointing to the same line.
2159 * When there are no filler lines nothing changes.
2160 */
2161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002162botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
2164 if (lp->fill > 0)
2165 {
2166 ++lp->lnum;
2167 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2168 }
2169}
2170
2171/*
2172 * Switch from including filler lines above lp->lnum to including filler
2173 * lines below loff.lnum - 1. This keeps pointing to the same line.
2174 * When there are no filler lines nothing changes.
2175 */
2176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002177topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 if (lp->fill > 0)
2180 {
2181 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2182 --lp->lnum;
2183 }
2184}
2185#endif
2186
2187/*
2188 * Recompute topline to put the cursor at the top of the window.
2189 * Scroll at least "min_scroll" lines.
2190 * If "always" is TRUE, always set topline (for "zt").
2191 */
2192 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002193scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 int scrolled = 0;
2196 int extra = 0;
2197 int used;
2198 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002199 linenr_T top; // just above displayed lines
2200 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002202 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203#ifdef FEAT_DIFF
2204 linenr_T old_topfill = curwin->w_topfill;
2205#endif
2206 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002207 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (mouse_dragging > 0)
2210 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 /*
2213 * Decrease topline until:
2214 * - it has become 1
2215 * - (part of) the cursor line is moved off the screen or
2216 * - moved at least 'scrolljump' lines and
2217 * - at least 'scrolloff' lines above and below the cursor
2218 */
2219 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (curwin->w_cursor.lnum < curwin->w_topline)
2222 scrolled = used;
2223
2224#ifdef FEAT_FOLDING
2225 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2226 {
2227 --top;
2228 ++bot;
2229 }
2230 else
2231#endif
2232 {
2233 top = curwin->w_cursor.lnum - 1;
2234 bot = curwin->w_cursor.lnum + 1;
2235 }
2236 new_topline = top + 1;
2237
2238#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002239 // "used" already contains the number of filler lines above, don't add it
2240 // again.
2241 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002242 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#endif
2244
2245 /*
2246 * Check if the lines from "top" to "bot" fit in the window. If they do,
2247 * set new_topline and advance "top" and "bot" to include more lines.
2248 */
2249 while (top > 0)
2250 {
2251#ifdef FEAT_FOLDING
2252 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002253 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 i = 1;
2255 else
2256#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002257 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 used += i;
2259 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2260 {
2261#ifdef FEAT_FOLDING
2262 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 ++used;
2265 else
2266#endif
2267 used += plines(bot);
2268 }
2269 if (used > curwin->w_height)
2270 break;
2271 if (top < curwin->w_topline)
2272 scrolled += i;
2273
2274 /*
2275 * If scrolling is needed, scroll at least 'sj' lines.
2276 */
2277 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2278 && extra >= off)
2279 break;
2280
2281 extra += i;
2282 new_topline = top;
2283 --top;
2284 ++bot;
2285 }
2286
2287 /*
2288 * If we don't have enough space, put cursor in the middle.
2289 * This makes sure we get the same position when using "k" and "j"
2290 * in a small window.
2291 */
2292 if (used > curwin->w_height)
2293 scroll_cursor_halfway(FALSE);
2294 else
2295 {
2296 /*
2297 * If "always" is FALSE, only adjust topline to a lower value, higher
2298 * value may happen with wrapping lines
2299 */
2300 if (new_topline < curwin->w_topline || always)
2301 curwin->w_topline = new_topline;
2302 if (curwin->w_topline > curwin->w_cursor.lnum)
2303 curwin->w_topline = curwin->w_cursor.lnum;
2304#ifdef FEAT_DIFF
2305 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2306 if (curwin->w_topfill > 0 && extra > off)
2307 {
2308 curwin->w_topfill -= extra - off;
2309 if (curwin->w_topfill < 0)
2310 curwin->w_topfill = 0;
2311 }
2312 check_topfill(curwin, FALSE);
2313#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002314 // TODO: if the line doesn't fit may optimize w_skipcol
2315 if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002316 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002318 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#ifdef FEAT_DIFF
2320 || curwin->w_topfill != old_topfill
2321#endif
2322 )
2323 curwin->w_valid &=
2324 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2325 curwin->w_valid |= VALID_TOPLINE;
2326 }
2327}
2328
2329/*
2330 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2331 * screen lines for text lines.
2332 */
2333 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002334set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336#ifdef FEAT_DIFF
2337 wp->w_filler_rows = 0;
2338#endif
2339 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002340 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 else
2342 {
2343 wp->w_empty_rows = wp->w_height - used;
2344#ifdef FEAT_DIFF
2345 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2346 {
2347 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2348 if (wp->w_empty_rows > wp->w_filler_rows)
2349 wp->w_empty_rows -= wp->w_filler_rows;
2350 else
2351 {
2352 wp->w_filler_rows = wp->w_empty_rows;
2353 wp->w_empty_rows = 0;
2354 }
2355 }
2356#endif
2357 }
2358}
2359
2360/*
2361 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002362 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2364 * This is messy stuff!!!
2365 */
2366 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002367scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369 int used;
2370 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002371 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 int extra = 0;
2373 int i;
2374 linenr_T line_count;
2375 linenr_T old_topline = curwin->w_topline;
2376 lineoff_T loff;
2377 lineoff_T boff;
2378#ifdef FEAT_DIFF
2379 int old_topfill = curwin->w_topfill;
2380 int fill_below_window;
2381#endif
2382 linenr_T old_botline = curwin->w_botline;
2383 linenr_T old_valid = curwin->w_valid;
2384 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002385 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002386 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 cln = curwin->w_cursor.lnum;
2389 if (set_topbot)
2390 {
2391 used = 0;
2392 curwin->w_botline = cln + 1;
2393#ifdef FEAT_DIFF
2394 loff.fill = 0;
2395#endif
2396 for (curwin->w_topline = curwin->w_botline;
2397 curwin->w_topline > 1;
2398 curwin->w_topline = loff.lnum)
2399 {
2400 loff.lnum = curwin->w_topline;
2401 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002402 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 break;
2404 used += loff.height;
2405#ifdef FEAT_DIFF
2406 curwin->w_topfill = loff.fill;
2407#endif
2408 }
2409 set_empty_rows(curwin, used);
2410 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2411 if (curwin->w_topline != old_topline
2412#ifdef FEAT_DIFF
2413 || curwin->w_topfill != old_topfill
2414#endif
2415 )
2416 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2417 }
2418 else
2419 validate_botline();
2420
Bram Moolenaar85a20022019-12-21 18:25:54 +01002421 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#ifdef FEAT_DIFF
2423 used = plines_nofill(cln);
2424#else
2425 validate_cheight();
2426 used = curwin->w_cline_height;
2427#endif
2428
Bram Moolenaar85a20022019-12-21 18:25:54 +01002429 // If the cursor is below botline, we will at least scroll by the height
2430 // of the cursor line. Correct for empty lines, which are really part of
2431 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (cln >= curwin->w_botline)
2433 {
2434 scrolled = used;
2435 if (cln == curwin->w_botline)
2436 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002437 min_scrolled = scrolled;
2438 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2439 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
Bram Moolenaar4d31b482022-10-06 16:03:09 +01002440 min_scrolled += PLINES_NOFILL(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442
2443 /*
2444 * Stop counting lines to scroll when
2445 * - hitting start of the file
2446 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002447 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 * - lines between botline and cursor have been counted
2449 */
2450#ifdef FEAT_FOLDING
2451 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2452#endif
2453 {
2454 loff.lnum = cln;
2455 boff.lnum = cln;
2456 }
2457#ifdef FEAT_DIFF
2458 loff.fill = 0;
2459 boff.fill = 0;
2460 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2461 - curwin->w_filler_rows;
2462#endif
2463
2464 while (loff.lnum > 1)
2465 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002466 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2467 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002469 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2471 && loff.lnum <= curwin->w_botline
2472#ifdef FEAT_DIFF
2473 && (loff.lnum < curwin->w_botline
2474 || loff.fill >= fill_below_window)
2475#endif
2476 )
2477 break;
2478
Bram Moolenaar85a20022019-12-21 18:25:54 +01002479 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002481 if (loff.height == MAXCOL)
2482 used = MAXCOL;
2483 else
2484 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (used > curwin->w_height)
2486 break;
2487 if (loff.lnum >= curwin->w_botline
2488#ifdef FEAT_DIFF
2489 && (loff.lnum > curwin->w_botline
2490 || loff.fill <= fill_below_window)
2491#endif
2492 )
2493 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002494 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 scrolled += loff.height;
2496 if (loff.lnum == curwin->w_botline
2497#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002498 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499#endif
2500 )
2501 scrolled -= curwin->w_empty_rows;
2502 }
2503
2504 if (boff.lnum < curbuf->b_ml.ml_line_count)
2505 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002506 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 botline_forw(&boff);
2508 used += boff.height;
2509 if (used > curwin->w_height)
2510 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002511 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2512 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 extra += boff.height;
2515 if (boff.lnum >= curwin->w_botline
2516#ifdef FEAT_DIFF
2517 || (boff.lnum + 1 == curwin->w_botline
2518 && boff.fill > curwin->w_filler_rows)
2519#endif
2520 )
2521 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002522 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 scrolled += boff.height;
2524 if (boff.lnum == curwin->w_botline
2525#ifdef FEAT_DIFF
2526 && boff.fill == 0
2527#endif
2528 )
2529 scrolled -= curwin->w_empty_rows;
2530 }
2531 }
2532 }
2533 }
2534
Bram Moolenaar85a20022019-12-21 18:25:54 +01002535 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (scrolled <= 0)
2537 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002538 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 else if (used > curwin->w_height)
2540 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002541 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 else
2543 {
2544 line_count = 0;
2545#ifdef FEAT_DIFF
2546 boff.fill = curwin->w_topfill;
2547#endif
2548 boff.lnum = curwin->w_topline - 1;
2549 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2550 {
2551 botline_forw(&boff);
2552 i += boff.height;
2553 ++line_count;
2554 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002555 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 line_count = 9999;
2557 }
2558
2559 /*
2560 * Scroll up if the cursor is off the bottom of the screen a bit.
2561 * Otherwise put it at 1/2 of the screen.
2562 */
2563 if (line_count >= curwin->w_height && line_count > min_scroll)
2564 scroll_cursor_halfway(FALSE);
2565 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002566 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002567 // With 'smoothscroll' scroll at least the height of the cursor line,
2568 // unless it would move the cursor.
2569 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2570 && (curwin->w_cursor.lnum < curwin->w_topline
2571 || (curwin->w_virtcol - curwin->w_skipcol >=
2572 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002573 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002574 if (line_count > 0)
2575 {
2576 if (scrolling_screenlines(TRUE))
2577 scrollup(scrolled, TRUE); // TODO
2578 else
2579 scrollup(line_count, TRUE);
2580 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 /*
2584 * If topline didn't change we need to restore w_botline and w_empty_rows
2585 * (we changed them).
2586 * If topline did change, update_screen() will set botline.
2587 */
2588 if (curwin->w_topline == old_topline && set_topbot)
2589 {
2590 curwin->w_botline = old_botline;
2591 curwin->w_empty_rows = old_empty_rows;
2592 curwin->w_valid = old_valid;
2593 }
2594 curwin->w_valid |= VALID_TOPLINE;
2595}
2596
2597/*
2598 * Recompute topline to put the cursor halfway the window
2599 * If "atend" is TRUE, also put it halfway at the end of the file.
2600 */
2601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002602scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604 int above = 0;
2605 linenr_T topline;
2606#ifdef FEAT_DIFF
2607 int topfill = 0;
2608#endif
2609 int below = 0;
2610 int used;
2611 lineoff_T loff;
2612 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002613#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002614 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002617#ifdef FEAT_PROP_POPUP
2618 // if the width changed this needs to be updated first
2619 may_update_popup_position();
2620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2622#ifdef FEAT_FOLDING
2623 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2624#endif
2625#ifdef FEAT_DIFF
2626 used = plines_nofill(loff.lnum);
2627 loff.fill = 0;
2628 boff.fill = 0;
2629#else
2630 used = plines(loff.lnum);
2631#endif
2632 topline = loff.lnum;
2633 while (topline > 1)
2634 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002635 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
2637 if (boff.lnum < curbuf->b_ml.ml_line_count)
2638 {
2639 botline_forw(&boff);
2640 used += boff.height;
2641 if (used > curwin->w_height)
2642 break;
2643 below += boff.height;
2644 }
2645 else
2646 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002647 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if (atend)
2649 ++used;
2650 }
2651 }
2652
Bram Moolenaar85a20022019-12-21 18:25:54 +01002653 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002656 if (loff.height == MAXCOL)
2657 used = MAXCOL;
2658 else
2659 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (used > curwin->w_height)
2661 break;
2662 above += loff.height;
2663 topline = loff.lnum;
2664#ifdef FEAT_DIFF
2665 topfill = loff.fill;
2666#endif
2667 }
2668 }
2669#ifdef FEAT_FOLDING
2670 if (!hasFolding(topline, &curwin->w_topline, NULL))
2671#endif
2672 curwin->w_topline = topline;
2673#ifdef FEAT_DIFF
2674 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002675 if (old_topline > curwin->w_topline + curwin->w_height)
2676 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 check_topfill(curwin, FALSE);
2678#endif
2679 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2680 curwin->w_valid |= VALID_TOPLINE;
2681}
2682
2683/*
2684 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002685 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 * If not possible, put it at the same position as scroll_cursor_halfway().
2687 * When called topline must be valid!
2688 */
2689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002692 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 linenr_T botline;
2696 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002699 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 /*
2702 * How many lines we would like to have above/below the cursor depends on
2703 * whether the first/last line of the file is on screen.
2704 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002705 above_wanted = so;
2706 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002707 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
2709 above_wanted = mouse_dragging - 1;
2710 below_wanted = mouse_dragging - 1;
2711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 if (curwin->w_topline == 1)
2713 {
2714 above_wanted = 0;
2715 max_off = curwin->w_height / 2;
2716 if (below_wanted > max_off)
2717 below_wanted = max_off;
2718 }
2719 validate_botline();
2720 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002721 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
2723 below_wanted = 0;
2724 max_off = (curwin->w_height - 1) / 2;
2725 if (above_wanted > max_off)
2726 above_wanted = max_off;
2727 }
2728
2729 /*
2730 * If there are sufficient file-lines above and below the cursor, we can
2731 * return now.
2732 */
2733 cln = curwin->w_cursor.lnum;
2734 if (cln >= curwin->w_topline + above_wanted
2735 && cln < curwin->w_botline - below_wanted
2736#ifdef FEAT_FOLDING
2737 && !hasAnyFolding(curwin)
2738#endif
2739 )
2740 return;
2741
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002742 if (curwin->w_p_sms && !curwin->w_p_wrap)
2743 {
2744 // 'smoothscroll is active
2745 if (curwin->w_cline_height == curwin->w_height)
2746 {
2747 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002748 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002749 return;
2750 }
2751 // TODO: If the cursor line doesn't fit in the window then only adjust
2752 // w_skipcol.
2753 }
2754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 /*
2756 * Narrow down the area where the cursor can be put by taking lines from
2757 * the top and the bottom until:
2758 * - the desired context lines are found
2759 * - the lines from the top is past the lines from the bottom
2760 */
2761 topline = curwin->w_topline;
2762 botline = curwin->w_botline - 1;
2763#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002764 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 above = curwin->w_topfill;
2766 below = curwin->w_filler_rows;
2767#endif
2768 while ((above < above_wanted || below < below_wanted) && topline < botline)
2769 {
2770 if (below < below_wanted && (below <= above || above >= above_wanted))
2771 {
2772#ifdef FEAT_FOLDING
2773 if (hasFolding(botline, &botline, NULL))
2774 ++below;
2775 else
2776#endif
2777 below += plines(botline);
2778 --botline;
2779 }
2780 if (above < above_wanted && (above < below || below >= below_wanted))
2781 {
2782#ifdef FEAT_FOLDING
2783 if (hasFolding(topline, NULL, &topline))
2784 ++above;
2785 else
2786#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002787 above += PLINES_NOFILL(topline);
2788#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002789 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (topline < botline)
2791 above += diff_check_fill(curwin, topline + 1);
2792#endif
2793 ++topline;
2794 }
2795 }
2796 if (topline == botline || botline == 0)
2797 curwin->w_cursor.lnum = topline;
2798 else if (topline > botline)
2799 curwin->w_cursor.lnum = botline;
2800 else
2801 {
2802 if (cln < topline && curwin->w_topline > 1)
2803 {
2804 curwin->w_cursor.lnum = topline;
2805 curwin->w_valid &=
2806 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2807 }
2808 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2809 {
2810 curwin->w_cursor.lnum = botline;
2811 curwin->w_valid &=
2812 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2813 }
2814 }
2815 curwin->w_valid |= VALID_TOPLINE;
2816}
2817
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002818static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
2820/*
2821 * move screen 'count' pages up or down and update screen
2822 *
2823 * return FAIL for failure, OK otherwise
2824 */
2825 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002826onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 long n;
2829 int retval = OK;
2830 lineoff_T loff;
2831 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002832 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
Bram Moolenaar85a20022019-12-21 18:25:54 +01002834 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
2836 beep_flush();
2837 return FAIL;
2838 }
2839
2840 for ( ; count > 0; --count)
2841 {
2842 validate_botline();
2843 /*
2844 * It's an error to move a page up when the first line is already on
2845 * the screen. It's an error to move a page down when the last line
2846 * is on the screen and the topline is 'scrolloff' lines from the
2847 * last line.
2848 */
2849 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002850 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2852 : (curwin->w_topline == 1
2853#ifdef FEAT_DIFF
2854 && curwin->w_topfill ==
2855 diff_check_fill(curwin, curwin->w_topline)
2856#endif
2857 ))
2858 {
2859 beep_flush();
2860 retval = FAIL;
2861 break;
2862 }
2863
2864#ifdef FEAT_DIFF
2865 loff.fill = 0;
2866#endif
2867 if (dir == FORWARD)
2868 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002869 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002871 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002872 if (p_window <= 2)
2873 ++curwin->w_topline;
2874 else
2875 curwin->w_topline += p_window - 2;
2876 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2877 curwin->w_topline = curbuf->b_ml.ml_line_count;
2878 curwin->w_cursor.lnum = curwin->w_topline;
2879 }
2880 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2881 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002882 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 curwin->w_topline = curbuf->b_ml.ml_line_count;
2884#ifdef FEAT_DIFF
2885 curwin->w_topfill = 0;
2886#endif
2887 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2888 }
2889 else
2890 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002891 // For the overlap, start with the line just below the window
2892 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 loff.lnum = curwin->w_botline;
2894#ifdef FEAT_DIFF
2895 loff.fill = diff_check_fill(curwin, loff.lnum)
2896 - curwin->w_filler_rows;
2897#endif
2898 get_scroll_overlap(&loff, -1);
2899 curwin->w_topline = loff.lnum;
2900#ifdef FEAT_DIFF
2901 curwin->w_topfill = loff.fill;
2902 check_topfill(curwin, FALSE);
2903#endif
2904 curwin->w_cursor.lnum = curwin->w_topline;
2905 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2906 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2907 }
2908 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002909 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911#ifdef FEAT_DIFF
2912 if (curwin->w_topline == 1)
2913 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002914 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 max_topfill();
2916 continue;
2917 }
2918#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002919 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002920 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002921 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002922 if (p_window <= 2)
2923 --curwin->w_topline;
2924 else
2925 curwin->w_topline -= p_window - 2;
2926 if (curwin->w_topline < 1)
2927 curwin->w_topline = 1;
2928 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2929 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2930 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2931 continue;
2932 }
2933
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 // Find the line at the top of the window that is going to be the
2935 // line at the bottom of the window. Make sure this results in
2936 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 loff.lnum = curwin->w_topline - 1;
2938#ifdef FEAT_DIFF
2939 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2940 - curwin->w_topfill;
2941#endif
2942 get_scroll_overlap(&loff, 1);
2943
2944 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2945 {
2946 loff.lnum = curbuf->b_ml.ml_line_count;
2947#ifdef FEAT_DIFF
2948 loff.fill = 0;
2949 }
2950 else
2951 {
2952 botline_topline(&loff);
2953#endif
2954 }
2955 curwin->w_cursor.lnum = loff.lnum;
2956
Bram Moolenaar85a20022019-12-21 18:25:54 +01002957 // Find the line just above the new topline to get the right line
2958 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 n = 0;
2960 while (n <= curwin->w_height && loff.lnum >= 1)
2961 {
2962 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002963 if (loff.height == MAXCOL)
2964 n = MAXCOL;
2965 else
2966 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002968 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {
2970 curwin->w_topline = 1;
2971#ifdef FEAT_DIFF
2972 max_topfill();
2973#endif
2974 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2975 }
2976 else
2977 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002978 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979#ifdef FEAT_DIFF
2980 topline_botline(&loff);
2981#endif
2982 botline_forw(&loff);
2983 botline_forw(&loff);
2984#ifdef FEAT_DIFF
2985 botline_topline(&loff);
2986#endif
2987#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002988 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2990#endif
2991
Bram Moolenaar85a20022019-12-21 18:25:54 +01002992 // Always scroll at least one line. Avoid getting stuck on
2993 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 if (loff.lnum >= curwin->w_topline
2995#ifdef FEAT_DIFF
2996 && (loff.lnum > curwin->w_topline
2997 || loff.fill >= curwin->w_topfill)
2998#endif
2999 )
3000 {
3001#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003002 // First try using the maximum number of filler lines. If
3003 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 loff.fill = curwin->w_topfill;
3005 if (curwin->w_topfill < diff_check_fill(curwin,
3006 curwin->w_topline))
3007 max_topfill();
3008 if (curwin->w_topfill == loff.fill)
3009#endif
3010 {
3011 --curwin->w_topline;
3012#ifdef FEAT_DIFF
3013 curwin->w_topfill = 0;
3014#endif
3015 }
3016 comp_botline(curwin);
3017 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003018 curwin->w_valid &=
3019 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021 else
3022 {
3023 curwin->w_topline = loff.lnum;
3024#ifdef FEAT_DIFF
3025 curwin->w_topfill = loff.fill;
3026 check_topfill(curwin, FALSE);
3027#endif
3028 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3029 }
3030 }
3031 }
3032 }
3033#ifdef FEAT_FOLDING
3034 foldAdjustCursor();
3035#endif
3036 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003037 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003038 if (retval == OK)
3039 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3041
Bram Moolenaar907dad72018-07-10 15:07:15 +02003042 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003044 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3045 // But make sure we scroll at least one line (happens with mix of long
3046 // wrapping lines and non-wrapping line).
3047 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003049 scroll_cursor_top(1, FALSE);
3050 if (curwin->w_topline <= old_topline
3051 && old_topline < curbuf->b_ml.ml_line_count)
3052 {
3053 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003055 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3056#endif
3057 }
3058 }
3059#ifdef FEAT_FOLDING
3060 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003065 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 return retval;
3067}
3068
3069/*
3070 * Decide how much overlap to use for page-up or page-down scrolling.
3071 * This is symmetric, so that doing both keeps the same lines displayed.
3072 * Three lines are examined:
3073 *
3074 * before CTRL-F after CTRL-F / before CTRL-B
3075 * etc. l1
3076 * l1 last but one line ------------
3077 * l2 last text line l2 top text line
3078 * ------------- l3 second text line
3079 * l3 etc.
3080 */
3081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003082get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 int h1, h2, h3, h4;
3085 int min_height = curwin->w_height - 2;
3086 lineoff_T loff0, loff1, loff2;
3087
3088#ifdef FEAT_DIFF
3089 if (lp->fill > 0)
3090 lp->height = 1;
3091 else
3092 lp->height = plines_nofill(lp->lnum);
3093#else
3094 lp->height = plines(lp->lnum);
3095#endif
3096 h1 = lp->height;
3097 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003098 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
3100 loff0 = *lp;
3101 if (dir > 0)
3102 botline_forw(lp);
3103 else
3104 topline_back(lp);
3105 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003106 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003108 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 return;
3110 }
3111
3112 loff1 = *lp;
3113 if (dir > 0)
3114 botline_forw(lp);
3115 else
3116 topline_back(lp);
3117 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003118 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003120 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 return;
3122 }
3123
3124 loff2 = *lp;
3125 if (dir > 0)
3126 botline_forw(lp);
3127 else
3128 topline_back(lp);
3129 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003130 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003131 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003133 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134}
3135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136/*
3137 * Scroll 'scroll' lines up or down.
3138 */
3139 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003140halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 long scrolled = 0;
3143 int i;
3144 int n;
3145 int room;
3146
3147 if (Prenum)
3148 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3149 curwin->w_height : Prenum;
3150 n = (curwin->w_p_scr <= curwin->w_height) ?
3151 curwin->w_p_scr : curwin->w_height;
3152
Bram Moolenaard5d37532017-03-27 23:02:07 +02003153 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 validate_botline();
3155 room = curwin->w_empty_rows;
3156#ifdef FEAT_DIFF
3157 room += curwin->w_filler_rows;
3158#endif
3159 if (flag)
3160 {
3161 /*
3162 * scroll the text up
3163 */
3164 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3165 {
3166#ifdef FEAT_DIFF
3167 if (curwin->w_topfill > 0)
3168 {
3169 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003170 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 --curwin->w_topfill;
3172 }
3173 else
3174#endif
3175 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003176 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 n -= i;
3178 if (n < 0 && scrolled > 0)
3179 break;
3180#ifdef FEAT_FOLDING
3181 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3182#endif
3183 ++curwin->w_topline;
3184#ifdef FEAT_DIFF
3185 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3186#endif
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3189 {
3190 ++curwin->w_cursor.lnum;
3191 curwin->w_valid &=
3192 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3196 scrolled += i;
3197
3198 /*
3199 * Correct w_botline for changed w_topline.
3200 * Won't work when there are filler lines.
3201 */
3202#ifdef FEAT_DIFF
3203 if (curwin->w_p_diff)
3204 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3205 else
3206#endif
3207 {
3208 room += i;
3209 do
3210 {
3211 i = plines(curwin->w_botline);
3212 if (i > room)
3213 break;
3214#ifdef FEAT_FOLDING
3215 (void)hasFolding(curwin->w_botline, NULL,
3216 &curwin->w_botline);
3217#endif
3218 ++curwin->w_botline;
3219 room -= i;
3220 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3221 }
3222 }
3223
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 /*
3225 * When hit bottom of the file: move cursor down.
3226 */
3227 if (n > 0)
3228 {
3229# ifdef FEAT_FOLDING
3230 if (hasAnyFolding(curwin))
3231 {
3232 while (--n >= 0
3233 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3234 {
3235 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3236 &curwin->w_cursor.lnum);
3237 ++curwin->w_cursor.lnum;
3238 }
3239 }
3240 else
3241# endif
3242 curwin->w_cursor.lnum += n;
3243 check_cursor_lnum();
3244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
3246 else
3247 {
3248 /*
3249 * scroll the text down
3250 */
3251 while (n > 0 && curwin->w_topline > 1)
3252 {
3253#ifdef FEAT_DIFF
3254 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3255 {
3256 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003257 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 ++curwin->w_topfill;
3259 }
3260 else
3261#endif
3262 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003263 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 n -= i;
3265 if (n < 0 && scrolled > 0)
3266 break;
3267 --curwin->w_topline;
3268#ifdef FEAT_FOLDING
3269 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3270#endif
3271#ifdef FEAT_DIFF
3272 curwin->w_topfill = 0;
3273#endif
3274 }
3275 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3276 VALID_BOTLINE|VALID_BOTLINE_AP);
3277 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (curwin->w_cursor.lnum > 1)
3279 {
3280 --curwin->w_cursor.lnum;
3281 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 /*
3286 * When hit top of the file: move cursor up.
3287 */
3288 if (n > 0)
3289 {
3290 if (curwin->w_cursor.lnum <= (linenr_T)n)
3291 curwin->w_cursor.lnum = 1;
3292 else
3293# ifdef FEAT_FOLDING
3294 if (hasAnyFolding(curwin))
3295 {
3296 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3297 {
3298 --curwin->w_cursor.lnum;
3299 (void)hasFolding(curwin->w_cursor.lnum,
3300 &curwin->w_cursor.lnum, NULL);
3301 }
3302 }
3303 else
3304# endif
3305 curwin->w_cursor.lnum -= n;
3306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003309 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 foldAdjustCursor();
3311# endif
3312#ifdef FEAT_DIFF
3313 check_topfill(curwin, !flag);
3314#endif
3315 cursor_correct();
3316 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003317 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003319
Bram Moolenaar860cae12010-06-05 23:22:07 +02003320 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003321do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003322{
3323 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003324 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003325 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003326 colnr_T curswant = curwin->w_curswant;
3327 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003328 win_T *old_curwin = curwin;
3329 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003330 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003331 int old_VIsual_select = VIsual_select;
3332 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003333
3334 /*
3335 * loop through the cursorbound windows
3336 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003337 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003338 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003339 {
3340 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003342 if (curwin != old_curwin && curwin->w_p_crb)
3343 {
3344# ifdef FEAT_DIFF
3345 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003346 curwin->w_cursor.lnum =
3347 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003348 else
3349# endif
3350 curwin->w_cursor.lnum = line;
3351 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003352 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003353 curwin->w_curswant = curswant;
3354 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003355
Bram Moolenaar85a20022019-12-21 18:25:54 +01003356 // Make sure the cursor is in a valid position. Temporarily set
3357 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003358 restart_edit_save = restart_edit;
3359 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003360 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003361
3362 // Avoid a scroll here for the cursor position, 'scrollbind' is
3363 // more important.
3364 if (!curwin->w_p_scb)
3365 validate_cursor();
3366
Bram Moolenaar61452852011-02-01 18:01:11 +01003367 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003368 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003369 if (has_mbyte)
3370 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003371 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003372
Bram Moolenaar85a20022019-12-21 18:25:54 +01003373 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003374 if (!curwin->w_p_scb)
3375 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003376 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003377 }
3378 }
3379
3380 /*
3381 * reset current-window
3382 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003383 VIsual_select = old_VIsual_select;
3384 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003385 curwin = old_curwin;
3386 curbuf = old_curbuf;
3387}