blob: 3a5f806b5dcc5adc7c7aa6bb6d21dde702e4700f [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 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001130 // w_skipcol excludes win_col_off(). Include it here, since w_wcol
1131 // counts actual screen columns.
1132 if (curwin->w_skipcol <= width1)
1133 curwin->w_wcol -= curwin->w_width;
1134 else
1135 curwin->w_wcol -= curwin->w_width
1136 * (((curwin->w_skipcol - width1) / width2) + 1);
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001137 did_sub_skipcol = TRUE;
1138 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001139
Bram Moolenaar85a20022019-12-21 18:25:54 +01001140 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001141 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001143 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001144 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1145 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 curwin->w_wrow += n;
1147
1148#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001149 // When cursor wraps to first char of next line in Insert
1150 // mode, the 'showbreak' string isn't shown, backup to first
1151 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001152 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001153 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001154 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 curwin->w_wcol = 0;
1156#endif
1157 }
1158 }
1159
Bram Moolenaar85a20022019-12-21 18:25:54 +01001160 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1161 // is not folded.
1162 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001163 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164#ifdef FEAT_FOLDING
1165 && !curwin->w_cline_folded
1166#endif
1167 )
1168 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001169#ifdef FEAT_PROP_POPUP
1170 if (curwin->w_virtcol_first_char > 0)
1171 {
1172 int cols = (curwin->w_width - extra);
1173 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1174
1175 // each "above" text prop shifts the text one row down
1176 curwin->w_wrow += rows;
1177 curwin->w_wcol -= rows * cols;
1178 endcol -= rows * cols;
1179 curwin->w_cline_height = rows + 1;
1180 }
1181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 /*
1183 * If Cursor is left of the screen, scroll rightwards.
1184 * If Cursor is right of the screen, scroll leftwards
1185 * If we get closer to the edge than 'sidescrolloff', scroll a little
1186 * extra
1187 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001188 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001189 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001190 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (off_left < 0 || off_right > 0)
1192 {
1193 if (off_left < 0)
1194 diff = -off_left;
1195 else
1196 diff = off_right;
1197
Bram Moolenaar85a20022019-12-21 18:25:54 +01001198 // When far off or not enough room on either side, put cursor in
1199 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001200 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1201 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 else
1203 {
1204 if (diff < p_ss)
1205 diff = p_ss;
1206 if (off_left < 0)
1207 new_leftcol = curwin->w_leftcol - diff;
1208 else
1209 new_leftcol = curwin->w_leftcol + diff;
1210 }
1211 if (new_leftcol < 0)
1212 new_leftcol = 0;
1213 if (new_leftcol != (int)curwin->w_leftcol)
1214 {
1215 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001216 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001217 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
1220 curwin->w_wcol -= curwin->w_leftcol;
1221 }
1222 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1223 curwin->w_wcol -= curwin->w_leftcol;
1224 else
1225 curwin->w_wcol = 0;
1226
1227#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001228 // Skip over filler lines. At the top use w_topfill, there
1229 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (curwin->w_cursor.lnum == curwin->w_topline)
1231 curwin->w_wrow += curwin->w_topfill;
1232 else
1233 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1234#endif
1235
1236 prev_skipcol = curwin->w_skipcol;
1237
1238 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if ((curwin->w_wrow >= curwin->w_height
1241 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001242 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 && (p_lines =
1244#ifdef FEAT_DIFF
1245 plines_win_nofill
1246#else
1247 plines_win
1248#endif
1249 (curwin, curwin->w_cursor.lnum, FALSE))
1250 - 1 >= curwin->w_height))
1251 && curwin->w_height != 0
1252 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001253 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001254 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001256 // Cursor past end of screen. Happens with a single line that does
1257 // not fit on screen. Find a skipcol to show the text around the
1258 // cursor. Avoid scrolling all the time. compute value of "extra":
1259 // 1: Less than 'scrolloff' lines above
1260 // 2: Less than 'scrolloff' lines below
1261 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001263 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001265 // Compute last display line of the buffer line that we want at the
1266 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (p_lines == 0)
1268 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1269 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001270 if (p_lines > curwin->w_wrow + so)
1271 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 else
1273 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001274 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 extra += 2;
1276
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001277 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001279 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001280 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (n > curwin->w_height / 2)
1282 n -= curwin->w_height / 2;
1283 else
1284 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001285 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (n > p_lines - curwin->w_height + 1)
1287 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001288 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 else if (extra == 1)
1291 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001292 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001293 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1294 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (extra > 0)
1296 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001297 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1298 extra = curwin->w_skipcol / width2;
1299 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301 }
1302 else if (extra == 2)
1303 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001304 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001305 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001307 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (endcol > curwin->w_skipcol)
1309 curwin->w_skipcol = endcol;
1310 }
1311
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001312 // adjust w_wrow for the changed w_skipcol
1313 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001314 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001315 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001316 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (curwin->w_wrow >= curwin->w_height)
1319 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001320 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001322 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 curwin->w_wrow -= extra;
1324 }
1325
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001326 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (extra > 0)
1328 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1329 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001330 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001332 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 curwin->w_skipcol = 0;
1334 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001335 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001337#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001338 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001339#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001340#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1341 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1342 {
1343 curwin->w_wrow += popup_top_extra(curwin);
1344 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001345 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001346 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001347 else
1348 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001349#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001350
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001351 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1352 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001353 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001354 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1357}
1358
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001359#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001360/*
1361 * Compute the screen position of text character at "pos" in window "wp"
1362 * The resulting values are one-based, zero when character is not visible.
1363 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001364 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001365textpos2screenpos(
1366 win_T *wp,
1367 pos_T *pos,
1368 int *rowp, // screen row
1369 int *scolp, // start screen column
1370 int *ccolp, // cursor screen column
1371 int *ecolp) // end screen column
1372{
1373 colnr_T scol = 0, ccol = 0, ecol = 0;
1374 int row = 0;
1375 int rowoff = 0;
1376 colnr_T coloff = 0;
1377
Bram Moolenaar189663b2021-07-21 18:04:56 +02001378 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001379 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001380 colnr_T off;
1381 colnr_T col;
1382 int width;
1383 linenr_T lnum = pos->lnum;
1384#ifdef FEAT_FOLDING
1385 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001386
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001387 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1388#endif
1389 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1390#ifdef FEAT_FOLDING
1391 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001392 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001393 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001394 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001395 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001396 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001397#endif
1398 {
1399 getvcol(wp, pos, &scol, &ccol, &ecol);
1400
1401 // similar to what is done in validate_cursor_col()
1402 col = scol;
1403 off = win_col_off(wp);
1404 col += off;
1405 width = wp->w_width - off + win_col_off2(wp);
1406
1407 // long line wrapping, adjust row
1408 if (wp->w_p_wrap
1409 && col >= (colnr_T)wp->w_width
1410 && width > 0)
1411 {
1412 // use same formula as what is used in curs_columns()
1413 rowoff = ((col - wp->w_width) / width + 1);
1414 col -= rowoff * width;
1415 }
1416 col -= wp->w_leftcol;
1417 if (col >= wp->w_width)
1418 col = -1;
1419 if (col >= 0 && row + rowoff <= wp->w_height)
1420 {
1421 coloff = col - scol + wp->w_wincol + 1;
1422 row += W_WINROW(wp);
1423 }
1424 else
1425 // character is left, right or below of the window
1426 row = rowoff = scol = ccol = ecol = 0;
1427 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001428 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001429 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001430 *scolp = scol + coloff;
1431 *ccolp = ccol + coloff;
1432 *ecolp = ecol + coloff;
1433}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001434#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001435
Bram Moolenaar12034e22019-08-25 22:25:02 +02001436#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001437/*
1438 * "screenpos({winid}, {lnum}, {col})" function
1439 */
1440 void
1441f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1442{
1443 dict_T *dict;
1444 win_T *wp;
1445 pos_T pos;
1446 int row = 0;
1447 int scol = 0, ccol = 0, ecol = 0;
1448
Bram Moolenaar93a10962022-06-16 11:42:09 +01001449 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450 return;
1451 dict = rettv->vval.v_dict;
1452
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001453 if (in_vim9script()
1454 && (check_for_number_arg(argvars, 0) == FAIL
1455 || check_for_number_arg(argvars, 1) == FAIL
1456 || check_for_number_arg(argvars, 2) == FAIL))
1457 return;
1458
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001459 wp = find_win_by_nr_or_id(&argvars[0]);
1460 if (wp == NULL)
1461 return;
1462
1463 pos.lnum = tv_get_number(&argvars[1]);
1464 pos.col = tv_get_number(&argvars[2]) - 1;
1465 pos.coladd = 0;
1466 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1467
1468 dict_add_number(dict, "row", row);
1469 dict_add_number(dict, "col", scol);
1470 dict_add_number(dict, "curscol", ccol);
1471 dict_add_number(dict, "endcol", ecol);
1472}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001473
1474/*
1475 * "virtcol2col({winid}, {lnum}, {col})" function
1476 */
1477 void
1478f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1479{
1480 win_T *wp;
1481 linenr_T lnum;
1482 int screencol;
1483 int error = FALSE;
1484
1485 rettv->vval.v_number = -1;
1486
1487 if (check_for_number_arg(argvars, 0) == FAIL
1488 || check_for_number_arg(argvars, 1) == FAIL
1489 || check_for_number_arg(argvars, 2) == FAIL)
1490 return;
1491
1492 wp = find_win_by_nr_or_id(&argvars[0]);
1493 if (wp == NULL)
1494 return;
1495
1496 lnum = tv_get_number_chk(&argvars[1], &error);
1497 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1498 return;
1499
1500 screencol = tv_get_number_chk(&argvars[2], &error);
1501 if (error || screencol < 0)
1502 return;
1503
1504 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1505}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001506#endif
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508/*
1509 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001512scrolldown(
1513 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001514 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001516 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 int wrow;
1518 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001519 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001520 int width1 = 0;
1521 int width2 = 0;
1522
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001523 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001524 {
1525 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001526 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
1529#ifdef FEAT_FOLDING
1530 linenr_T first;
1531
Bram Moolenaar85a20022019-12-21 18:25:54 +01001532 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1534#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001535 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001536 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001539 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1540 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 {
1542 ++curwin->w_topfill;
1543 ++done;
1544 }
1545 else
1546#endif
1547 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001548 // break when at the very top
1549 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001550 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001552 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001554 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001555 if (curwin->w_skipcol >= width1 + width2)
1556 curwin->w_skipcol -= width2;
1557 else
1558 curwin->w_skipcol -= width1;
1559 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001563 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001564 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001565 --curwin->w_topline;
1566 curwin->w_skipcol = 0;
1567#ifdef FEAT_DIFF
1568 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001570#ifdef FEAT_FOLDING
1571 // A sequence of folded lines only counts for one logical line
1572 if (hasFolding(curwin->w_topline, &first, NULL))
1573 {
1574 ++done;
1575 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001576 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001577 curwin->w_botline -= curwin->w_topline - first;
1578 curwin->w_topline = first;
1579 }
1580 else
1581#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001582 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001583 {
1584 int size = win_linetabsize(curwin, curwin->w_topline,
1585 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1586 if (size > width1)
1587 {
1588 curwin->w_skipcol = width1;
1589 size -= width1;
1590 redraw_later(UPD_NOT_VALID);
1591 }
1592 while (size > width2)
1593 {
1594 curwin->w_skipcol += width2;
1595 size -= width2;
1596 }
1597 ++done;
1598 }
1599 else
1600 done += PLINES_NOFILL(curwin->w_topline);
1601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 invalidate_botline();
1605 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001606 curwin->w_wrow += done; // keep w_wrow updated
1607 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609#ifdef FEAT_DIFF
1610 if (curwin->w_cursor.lnum == curwin->w_topline)
1611 curwin->w_cline_row = 0;
1612 check_topfill(curwin, TRUE);
1613#endif
1614
1615 /*
1616 * Compute the row number of the last row of the cursor line
1617 * and move the cursor onto the displayed part of the window.
1618 */
1619 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001620 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 validate_virtcol();
1623 validate_cheight();
1624 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001625 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1628 {
1629#ifdef FEAT_FOLDING
1630 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1631 {
1632 --wrow;
1633 if (first == 1)
1634 curwin->w_cursor.lnum = 1;
1635 else
1636 curwin->w_cursor.lnum = first - 1;
1637 }
1638 else
1639#endif
1640 wrow -= plines(curwin->w_cursor.lnum--);
1641 curwin->w_valid &=
1642 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1643 moved = TRUE;
1644 }
1645 if (moved)
1646 {
1647#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001648 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 foldAdjustCursor();
1650#endif
1651 coladvance(curwin->w_curswant);
1652 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001653
1654 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1655 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001656 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001657 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1658
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001659 // make sure the cursor is in the visible text
1660 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001661 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001662 int row = 0;
1663 if (col >= width1)
1664 {
1665 col -= width1;
1666 ++row;
1667 }
1668 if (col > width2)
1669 {
1670 row += col / width2;
1671 col = col % width2;
1672 }
1673 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001674 {
1675 curwin->w_curswant = curwin->w_virtcol
1676 - (row - curwin->w_height + 1) * width2;
1677 coladvance(curwin->w_curswant);
1678 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680}
1681
1682/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001683 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1684 */
1685 static int
1686scrolling_screenlines(int byfold UNUSED)
1687{
1688 return (curwin->w_p_wrap && curwin->w_p_sms)
1689# ifdef FEAT_FOLDING
1690 || (byfold && hasAnyFolding(curwin))
1691# endif
1692# ifdef FEAT_DIFF
1693 || curwin->w_p_diff
1694# endif
1695 ;
1696}
1697
1698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001702scrollup(
1703 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001704 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001706 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001708 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001710 int width1 = curwin->w_width - curwin_col_off();
1711 int width2 = width1 + curwin_col_off2();
1712 int size = 0;
1713 linenr_T prev_topline = curwin->w_topline;
1714
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001715 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001716 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001717
1718 // diff mode: first consume "topfill"
1719 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1720 // the line, then advance to the next line.
1721 // folding: count each sequence of folded lines as one logical line.
1722 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 {
1724# ifdef FEAT_DIFF
1725 if (curwin->w_topfill > 0)
1726 --curwin->w_topfill;
1727 else
1728# endif
1729 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001730 linenr_T lnum = curwin->w_topline;
1731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732# ifdef FEAT_FOLDING
1733 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001734 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 (void)hasFolding(lnum, NULL, &lnum);
1736# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001737 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001738 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001739 // 'smoothscroll': increase "w_skipcol" until it goes over
1740 // the end of the line, then advance to the next line.
1741 int add = curwin->w_skipcol > 0 ? width2 : width1;
1742 curwin->w_skipcol += add;
1743 if (curwin->w_skipcol >= size)
1744 {
1745 if (lnum == curbuf->b_ml.ml_line_count)
1746 {
1747 // at the last screen line, can't scroll further
1748 curwin->w_skipcol -= add;
1749 break;
1750 }
1751 ++lnum;
1752 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001753 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001754 else
1755 {
1756 if (lnum >= curbuf->b_ml.ml_line_count)
1757 break;
1758 ++lnum;
1759 }
1760
1761 if (lnum > curwin->w_topline)
1762 {
1763 // approximate w_botline
1764 curwin->w_botline += lnum - curwin->w_topline;
1765 curwin->w_topline = lnum;
1766# ifdef FEAT_DIFF
1767 curwin->w_topfill = diff_check_fill(curwin, lnum);
1768# endif
1769 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001770 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001771 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001772 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001773 }
1774 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001775
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001776 if (curwin->w_topline == prev_topline)
1777 // need to redraw even though w_topline didn't change
1778 redraw_later(UPD_NOT_VALID);
1779 }
1780 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 {
1782 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001783 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
1785
1786 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1787 curwin->w_topline = curbuf->b_ml.ml_line_count;
1788 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1789 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1790
1791#ifdef FEAT_DIFF
1792 check_topfill(curwin, FALSE);
1793#endif
1794
1795#ifdef FEAT_FOLDING
1796 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001797 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1799#endif
1800
1801 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1802 if (curwin->w_cursor.lnum < curwin->w_topline)
1803 {
1804 curwin->w_cursor.lnum = curwin->w_topline;
1805 curwin->w_valid &=
1806 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1807 coladvance(curwin->w_curswant);
1808 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001809 if (curwin->w_cursor.lnum == curwin->w_topline
1810 && do_sms && curwin->w_skipcol > 0)
1811 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001812 int width1 = curwin->w_width - curwin_col_off();
1813 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001814 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001815 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001816 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001817
1818 // Make sure the cursor is in a visible part of the line, taking
1819 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001820 // If there are not enough screen lines put the cursor in the middle.
1821 if (scrolloff_cols > space_cols / 2)
1822 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001823 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001824 if (curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001825 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001826 colnr_T col = curwin->w_virtcol;
1827
1828 if (col < width1)
1829 col += width1;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001830 while (col < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001831 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001832 curwin->w_curswant = col;
1833 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001834
1835 // validate_virtcol() marked various things as valid, but after
1836 // moving the cursor they need to be recomputed
1837 curwin->w_valid &=
1838 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001839 }
1840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841}
1842
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001843/*
1844 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1845 * valid for 'smoothscroll'.
1846 */
1847 void
1848adjust_skipcol(void)
1849{
1850 if (!curwin->w_p_wrap
1851 || !curwin->w_p_sms
1852 || curwin->w_cursor.lnum != curwin->w_topline)
1853 return;
1854
1855 int width1 = curwin->w_width - curwin_col_off();
1856 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001857 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001858 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1859 int scrolled = FALSE;
1860
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001861 validate_cheight();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001862 if (curwin->w_cline_height == curwin->w_height)
1863 {
1864 // the line just fits in the window, don't scroll
Bram Moolenaarf32fb932022-11-17 11:34:38 +00001865 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001866 return;
1867 }
1868
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001869 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001870 while (curwin->w_skipcol > 0
1871 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1872 {
1873 // scroll a screen line down
1874 if (curwin->w_skipcol >= width1 + width2)
1875 curwin->w_skipcol -= width2;
1876 else
1877 curwin->w_skipcol -= width1;
1878 redraw_later(UPD_NOT_VALID);
1879 scrolled = TRUE;
1880 validate_virtcol();
1881 }
1882 if (scrolled)
1883 return; // don't scroll in the other direction now
1884
1885 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1886 int row = 0;
1887 if (col >= width1)
1888 {
1889 col -= width1;
1890 ++row;
1891 }
1892 if (col > width2)
1893 {
1894 row += col / width2;
1895 col = col % width2;
1896 }
1897 if (row >= curwin->w_height)
1898 {
1899 if (curwin->w_skipcol == 0)
1900 {
1901 curwin->w_skipcol += width1;
1902 --row;
1903 }
1904 if (row >= curwin->w_height)
1905 curwin->w_skipcol += (row - curwin->w_height) * width2;
1906 redraw_later(UPD_NOT_VALID);
1907 }
1908}
1909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910#ifdef FEAT_DIFF
1911/*
1912 * Don't end up with too many filler lines in the window.
1913 */
1914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001915check_topfill(
1916 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001917 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918{
1919 int n;
1920
1921 if (wp->w_topfill > 0)
1922 {
1923 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1924 if (wp->w_topfill + n > wp->w_height)
1925 {
1926 if (down && wp->w_topline > 1)
1927 {
1928 --wp->w_topline;
1929 wp->w_topfill = 0;
1930 }
1931 else
1932 {
1933 wp->w_topfill = wp->w_height - n;
1934 if (wp->w_topfill < 0)
1935 wp->w_topfill = 0;
1936 }
1937 }
1938 }
1939}
1940
1941/*
1942 * Use as many filler lines as possible for w_topline. Make sure w_topline
1943 * is still visible.
1944 */
1945 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001946max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
1948 int n;
1949
1950 n = plines_nofill(curwin->w_topline);
1951 if (n >= curwin->w_height)
1952 curwin->w_topfill = 0;
1953 else
1954 {
1955 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1956 if (curwin->w_topfill + n > curwin->w_height)
1957 curwin->w_topfill = curwin->w_height - n;
1958 }
1959}
1960#endif
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962/*
1963 * Scroll the screen one line down, but don't do it if it would move the
1964 * cursor off the screen.
1965 */
1966 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001967scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968{
1969 int end_row;
1970#ifdef FEAT_DIFF
1971 int can_fill = (curwin->w_topfill
1972 < diff_check_fill(curwin, curwin->w_topline));
1973#endif
1974
1975 if (curwin->w_topline <= 1
1976#ifdef FEAT_DIFF
1977 && !can_fill
1978#endif
1979 )
1980 return;
1981
Bram Moolenaar85a20022019-12-21 18:25:54 +01001982 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
1984 /*
1985 * Compute the row number of the last row of the cursor line
1986 * and make sure it doesn't go off the screen. Make sure the cursor
1987 * doesn't go past 'scrolloff' lines from the screen end.
1988 */
1989 end_row = curwin->w_wrow;
1990#ifdef FEAT_DIFF
1991 if (can_fill)
1992 ++end_row;
1993 else
1994 end_row += plines_nofill(curwin->w_topline - 1);
1995#else
1996 end_row += plines(curwin->w_topline - 1);
1997#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001998 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
2000 validate_cheight();
2001 validate_virtcol();
2002 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02002003 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002005 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
2007#ifdef FEAT_DIFF
2008 if (can_fill)
2009 {
2010 ++curwin->w_topfill;
2011 check_topfill(curwin, TRUE);
2012 }
2013 else
2014 {
2015 --curwin->w_topline;
2016 curwin->w_topfill = 0;
2017 }
2018#else
2019 --curwin->w_topline;
2020#endif
2021#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002022 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002024 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2026 }
2027}
2028
2029/*
2030 * Scroll the screen one line up, but don't do it if it would move the cursor
2031 * off the screen.
2032 */
2033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002034scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 int start_row;
2037
2038 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2039#ifdef FEAT_DIFF
2040 && curwin->w_topfill == 0
2041#endif
2042 )
2043 return;
2044
Bram Moolenaar85a20022019-12-21 18:25:54 +01002045 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 /*
2048 * Compute the row number of the first row of the cursor line
2049 * and make sure it doesn't go off the screen. Make sure the cursor
2050 * doesn't go before 'scrolloff' lines from the screen start.
2051 */
2052#ifdef FEAT_DIFF
2053 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2054 - curwin->w_topfill;
2055#else
2056 start_row = curwin->w_wrow - plines(curwin->w_topline);
2057#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002058 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
2060 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002061 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002063 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065#ifdef FEAT_DIFF
2066 if (curwin->w_topfill > 0)
2067 --curwin->w_topfill;
2068 else
2069#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002070 {
2071#ifdef FEAT_FOLDING
2072 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002075 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002076 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2078 }
2079}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081/*
2082 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2083 * a (wrapped) text line. Uses and sets "lp->fill".
2084 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002085 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 */
2087 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002088topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089{
2090#ifdef FEAT_DIFF
2091 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2092 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002093 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 ++lp->fill;
2095 lp->height = 1;
2096 }
2097 else
2098#endif
2099 {
2100 --lp->lnum;
2101#ifdef FEAT_DIFF
2102 lp->fill = 0;
2103#endif
2104 if (lp->lnum < 1)
2105 lp->height = MAXCOL;
2106 else
2107#ifdef FEAT_FOLDING
2108 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002109 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 lp->height = 1;
2111 else
2112#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002113 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
2115}
2116
2117/*
2118 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2119 * a (wrapped) text line. Uses and sets "lp->fill".
2120 * Returns the height of the added line in "lp->height".
2121 * Lines below the last one are incredibly high.
2122 */
2123 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126#ifdef FEAT_DIFF
2127 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2128 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002129 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 ++lp->fill;
2131 lp->height = 1;
2132 }
2133 else
2134#endif
2135 {
2136 ++lp->lnum;
2137#ifdef FEAT_DIFF
2138 lp->fill = 0;
2139#endif
2140 if (lp->lnum > curbuf->b_ml.ml_line_count)
2141 lp->height = MAXCOL;
2142 else
2143#ifdef FEAT_FOLDING
2144 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002145 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 lp->height = 1;
2147 else
2148#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002149 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151}
2152
2153#ifdef FEAT_DIFF
2154/*
2155 * Switch from including filler lines below lp->lnum to including filler
2156 * lines above loff.lnum + 1. This keeps pointing to the same line.
2157 * When there are no filler lines nothing changes.
2158 */
2159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002160botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162 if (lp->fill > 0)
2163 {
2164 ++lp->lnum;
2165 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2166 }
2167}
2168
2169/*
2170 * Switch from including filler lines above lp->lnum to including filler
2171 * lines below loff.lnum - 1. This keeps pointing to the same line.
2172 * When there are no filler lines nothing changes.
2173 */
2174 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002175topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 if (lp->fill > 0)
2178 {
2179 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2180 --lp->lnum;
2181 }
2182}
2183#endif
2184
2185/*
2186 * Recompute topline to put the cursor at the top of the window.
2187 * Scroll at least "min_scroll" lines.
2188 * If "always" is TRUE, always set topline (for "zt").
2189 */
2190 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002191scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 int scrolled = 0;
2194 int extra = 0;
2195 int used;
2196 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002197 linenr_T top; // just above displayed lines
2198 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002200 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#ifdef FEAT_DIFF
2202 linenr_T old_topfill = curwin->w_topfill;
2203#endif
2204 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002205 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 if (mouse_dragging > 0)
2208 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 /*
2211 * Decrease topline until:
2212 * - it has become 1
2213 * - (part of) the cursor line is moved off the screen or
2214 * - moved at least 'scrolljump' lines and
2215 * - at least 'scrolloff' lines above and below the cursor
2216 */
2217 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002218 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (curwin->w_cursor.lnum < curwin->w_topline)
2220 scrolled = used;
2221
2222#ifdef FEAT_FOLDING
2223 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2224 {
2225 --top;
2226 ++bot;
2227 }
2228 else
2229#endif
2230 {
2231 top = curwin->w_cursor.lnum - 1;
2232 bot = curwin->w_cursor.lnum + 1;
2233 }
2234 new_topline = top + 1;
2235
2236#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002237 // "used" already contains the number of filler lines above, don't add it
2238 // again.
2239 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002240 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#endif
2242
2243 /*
2244 * Check if the lines from "top" to "bot" fit in the window. If they do,
2245 * set new_topline and advance "top" and "bot" to include more lines.
2246 */
2247 while (top > 0)
2248 {
2249#ifdef FEAT_FOLDING
2250 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002251 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 i = 1;
2253 else
2254#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002255 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 used += i;
2257 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2258 {
2259#ifdef FEAT_FOLDING
2260 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002261 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 ++used;
2263 else
2264#endif
2265 used += plines(bot);
2266 }
2267 if (used > curwin->w_height)
2268 break;
2269 if (top < curwin->w_topline)
2270 scrolled += i;
2271
2272 /*
2273 * If scrolling is needed, scroll at least 'sj' lines.
2274 */
2275 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2276 && extra >= off)
2277 break;
2278
2279 extra += i;
2280 new_topline = top;
2281 --top;
2282 ++bot;
2283 }
2284
2285 /*
2286 * If we don't have enough space, put cursor in the middle.
2287 * This makes sure we get the same position when using "k" and "j"
2288 * in a small window.
2289 */
2290 if (used > curwin->w_height)
2291 scroll_cursor_halfway(FALSE);
2292 else
2293 {
2294 /*
2295 * If "always" is FALSE, only adjust topline to a lower value, higher
2296 * value may happen with wrapping lines
2297 */
2298 if (new_topline < curwin->w_topline || always)
2299 curwin->w_topline = new_topline;
2300 if (curwin->w_topline > curwin->w_cursor.lnum)
2301 curwin->w_topline = curwin->w_cursor.lnum;
2302#ifdef FEAT_DIFF
2303 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2304 if (curwin->w_topfill > 0 && extra > off)
2305 {
2306 curwin->w_topfill -= extra - off;
2307 if (curwin->w_topfill < 0)
2308 curwin->w_topfill = 0;
2309 }
2310 check_topfill(curwin, FALSE);
2311#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002312 // TODO: if the line doesn't fit may optimize w_skipcol
2313 if (curwin->w_topline == curwin->w_cursor.lnum)
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002314 reset_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002316 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#ifdef FEAT_DIFF
2318 || curwin->w_topfill != old_topfill
2319#endif
2320 )
2321 curwin->w_valid &=
2322 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2323 curwin->w_valid |= VALID_TOPLINE;
2324 }
2325}
2326
2327/*
2328 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2329 * screen lines for text lines.
2330 */
2331 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002332set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334#ifdef FEAT_DIFF
2335 wp->w_filler_rows = 0;
2336#endif
2337 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002338 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
2340 {
2341 wp->w_empty_rows = wp->w_height - used;
2342#ifdef FEAT_DIFF
2343 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2344 {
2345 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2346 if (wp->w_empty_rows > wp->w_filler_rows)
2347 wp->w_empty_rows -= wp->w_filler_rows;
2348 else
2349 {
2350 wp->w_filler_rows = wp->w_empty_rows;
2351 wp->w_empty_rows = 0;
2352 }
2353 }
2354#endif
2355 }
2356}
2357
2358/*
2359 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002360 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2362 * This is messy stuff!!!
2363 */
2364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002365scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 int used;
2368 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002369 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 int extra = 0;
2371 int i;
2372 linenr_T line_count;
2373 linenr_T old_topline = curwin->w_topline;
2374 lineoff_T loff;
2375 lineoff_T boff;
2376#ifdef FEAT_DIFF
2377 int old_topfill = curwin->w_topfill;
2378 int fill_below_window;
2379#endif
2380 linenr_T old_botline = curwin->w_botline;
2381 linenr_T old_valid = curwin->w_valid;
2382 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002383 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002384 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 cln = curwin->w_cursor.lnum;
2387 if (set_topbot)
2388 {
2389 used = 0;
2390 curwin->w_botline = cln + 1;
2391#ifdef FEAT_DIFF
2392 loff.fill = 0;
2393#endif
2394 for (curwin->w_topline = curwin->w_botline;
2395 curwin->w_topline > 1;
2396 curwin->w_topline = loff.lnum)
2397 {
2398 loff.lnum = curwin->w_topline;
2399 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002400 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 break;
2402 used += loff.height;
2403#ifdef FEAT_DIFF
2404 curwin->w_topfill = loff.fill;
2405#endif
2406 }
2407 set_empty_rows(curwin, used);
2408 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2409 if (curwin->w_topline != old_topline
2410#ifdef FEAT_DIFF
2411 || curwin->w_topfill != old_topfill
2412#endif
2413 )
2414 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2415 }
2416 else
2417 validate_botline();
2418
Bram Moolenaar85a20022019-12-21 18:25:54 +01002419 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#ifdef FEAT_DIFF
2421 used = plines_nofill(cln);
2422#else
2423 validate_cheight();
2424 used = curwin->w_cline_height;
2425#endif
2426
Bram Moolenaar85a20022019-12-21 18:25:54 +01002427 // If the cursor is below botline, we will at least scroll by the height
2428 // of the cursor line. Correct for empty lines, which are really part of
2429 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (cln >= curwin->w_botline)
2431 {
2432 scrolled = used;
2433 if (cln == curwin->w_botline)
2434 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002435 min_scrolled = scrolled;
2436 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2437 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
Bram Moolenaar4d31b482022-10-06 16:03:09 +01002438 min_scrolled += PLINES_NOFILL(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440
2441 /*
2442 * Stop counting lines to scroll when
2443 * - hitting start of the file
2444 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002445 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 * - lines between botline and cursor have been counted
2447 */
2448#ifdef FEAT_FOLDING
2449 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2450#endif
2451 {
2452 loff.lnum = cln;
2453 boff.lnum = cln;
2454 }
2455#ifdef FEAT_DIFF
2456 loff.fill = 0;
2457 boff.fill = 0;
2458 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2459 - curwin->w_filler_rows;
2460#endif
2461
2462 while (loff.lnum > 1)
2463 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002464 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2465 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002467 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2469 && loff.lnum <= curwin->w_botline
2470#ifdef FEAT_DIFF
2471 && (loff.lnum < curwin->w_botline
2472 || loff.fill >= fill_below_window)
2473#endif
2474 )
2475 break;
2476
Bram Moolenaar85a20022019-12-21 18:25:54 +01002477 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002479 if (loff.height == MAXCOL)
2480 used = MAXCOL;
2481 else
2482 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 if (used > curwin->w_height)
2484 break;
2485 if (loff.lnum >= curwin->w_botline
2486#ifdef FEAT_DIFF
2487 && (loff.lnum > curwin->w_botline
2488 || loff.fill <= fill_below_window)
2489#endif
2490 )
2491 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002492 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 scrolled += loff.height;
2494 if (loff.lnum == curwin->w_botline
2495#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002496 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497#endif
2498 )
2499 scrolled -= curwin->w_empty_rows;
2500 }
2501
2502 if (boff.lnum < curbuf->b_ml.ml_line_count)
2503 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002504 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 botline_forw(&boff);
2506 used += boff.height;
2507 if (used > curwin->w_height)
2508 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002509 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2510 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 extra += boff.height;
2513 if (boff.lnum >= curwin->w_botline
2514#ifdef FEAT_DIFF
2515 || (boff.lnum + 1 == curwin->w_botline
2516 && boff.fill > curwin->w_filler_rows)
2517#endif
2518 )
2519 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002520 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 scrolled += boff.height;
2522 if (boff.lnum == curwin->w_botline
2523#ifdef FEAT_DIFF
2524 && boff.fill == 0
2525#endif
2526 )
2527 scrolled -= curwin->w_empty_rows;
2528 }
2529 }
2530 }
2531 }
2532
Bram Moolenaar85a20022019-12-21 18:25:54 +01002533 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (scrolled <= 0)
2535 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002536 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 else if (used > curwin->w_height)
2538 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002539 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 else
2541 {
2542 line_count = 0;
2543#ifdef FEAT_DIFF
2544 boff.fill = curwin->w_topfill;
2545#endif
2546 boff.lnum = curwin->w_topline - 1;
2547 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2548 {
2549 botline_forw(&boff);
2550 i += boff.height;
2551 ++line_count;
2552 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002553 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 line_count = 9999;
2555 }
2556
2557 /*
2558 * Scroll up if the cursor is off the bottom of the screen a bit.
2559 * Otherwise put it at 1/2 of the screen.
2560 */
2561 if (line_count >= curwin->w_height && line_count > min_scroll)
2562 scroll_cursor_halfway(FALSE);
2563 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002564 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002565 // With 'smoothscroll' scroll at least the height of the cursor line,
2566 // unless it would move the cursor.
2567 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2568 && (curwin->w_cursor.lnum < curwin->w_topline
2569 || (curwin->w_virtcol - curwin->w_skipcol >=
2570 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002571 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002572 if (line_count > 0)
2573 {
2574 if (scrolling_screenlines(TRUE))
2575 scrollup(scrolled, TRUE); // TODO
2576 else
2577 scrollup(line_count, TRUE);
2578 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 /*
2582 * If topline didn't change we need to restore w_botline and w_empty_rows
2583 * (we changed them).
2584 * If topline did change, update_screen() will set botline.
2585 */
2586 if (curwin->w_topline == old_topline && set_topbot)
2587 {
2588 curwin->w_botline = old_botline;
2589 curwin->w_empty_rows = old_empty_rows;
2590 curwin->w_valid = old_valid;
2591 }
2592 curwin->w_valid |= VALID_TOPLINE;
2593}
2594
2595/*
2596 * Recompute topline to put the cursor halfway the window
2597 * If "atend" is TRUE, also put it halfway at the end of the file.
2598 */
2599 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002600scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
2602 int above = 0;
2603 linenr_T topline;
2604#ifdef FEAT_DIFF
2605 int topfill = 0;
2606#endif
2607 int below = 0;
2608 int used;
2609 lineoff_T loff;
2610 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002611#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002612 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002615#ifdef FEAT_PROP_POPUP
2616 // if the width changed this needs to be updated first
2617 may_update_popup_position();
2618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2620#ifdef FEAT_FOLDING
2621 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2622#endif
2623#ifdef FEAT_DIFF
2624 used = plines_nofill(loff.lnum);
2625 loff.fill = 0;
2626 boff.fill = 0;
2627#else
2628 used = plines(loff.lnum);
2629#endif
2630 topline = loff.lnum;
2631 while (topline > 1)
2632 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002633 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 if (boff.lnum < curbuf->b_ml.ml_line_count)
2636 {
2637 botline_forw(&boff);
2638 used += boff.height;
2639 if (used > curwin->w_height)
2640 break;
2641 below += boff.height;
2642 }
2643 else
2644 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002645 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (atend)
2647 ++used;
2648 }
2649 }
2650
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002654 if (loff.height == MAXCOL)
2655 used = MAXCOL;
2656 else
2657 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (used > curwin->w_height)
2659 break;
2660 above += loff.height;
2661 topline = loff.lnum;
2662#ifdef FEAT_DIFF
2663 topfill = loff.fill;
2664#endif
2665 }
2666 }
2667#ifdef FEAT_FOLDING
2668 if (!hasFolding(topline, &curwin->w_topline, NULL))
2669#endif
2670 curwin->w_topline = topline;
2671#ifdef FEAT_DIFF
2672 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002673 if (old_topline > curwin->w_topline + curwin->w_height)
2674 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 check_topfill(curwin, FALSE);
2676#endif
2677 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2678 curwin->w_valid |= VALID_TOPLINE;
2679}
2680
2681/*
2682 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002683 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 * If not possible, put it at the same position as scroll_cursor_halfway().
2685 * When called topline must be valid!
2686 */
2687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002688cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002690 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002692 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 linenr_T botline;
2694 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002695 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002697 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 /*
2700 * How many lines we would like to have above/below the cursor depends on
2701 * whether the first/last line of the file is on screen.
2702 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002703 above_wanted = so;
2704 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002705 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
2707 above_wanted = mouse_dragging - 1;
2708 below_wanted = mouse_dragging - 1;
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (curwin->w_topline == 1)
2711 {
2712 above_wanted = 0;
2713 max_off = curwin->w_height / 2;
2714 if (below_wanted > max_off)
2715 below_wanted = max_off;
2716 }
2717 validate_botline();
2718 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002719 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
2721 below_wanted = 0;
2722 max_off = (curwin->w_height - 1) / 2;
2723 if (above_wanted > max_off)
2724 above_wanted = max_off;
2725 }
2726
2727 /*
2728 * If there are sufficient file-lines above and below the cursor, we can
2729 * return now.
2730 */
2731 cln = curwin->w_cursor.lnum;
2732 if (cln >= curwin->w_topline + above_wanted
2733 && cln < curwin->w_botline - below_wanted
2734#ifdef FEAT_FOLDING
2735 && !hasAnyFolding(curwin)
2736#endif
2737 )
2738 return;
2739
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002740 if (curwin->w_p_sms && !curwin->w_p_wrap)
2741 {
2742 // 'smoothscroll is active
2743 if (curwin->w_cline_height == curwin->w_height)
2744 {
2745 // The cursor line just fits in the window, don't scroll.
Bram Moolenaarf32fb932022-11-17 11:34:38 +00002746 reset_skipcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002747 return;
2748 }
2749 // TODO: If the cursor line doesn't fit in the window then only adjust
2750 // w_skipcol.
2751 }
2752
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 /*
2754 * Narrow down the area where the cursor can be put by taking lines from
2755 * the top and the bottom until:
2756 * - the desired context lines are found
2757 * - the lines from the top is past the lines from the bottom
2758 */
2759 topline = curwin->w_topline;
2760 botline = curwin->w_botline - 1;
2761#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002762 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 above = curwin->w_topfill;
2764 below = curwin->w_filler_rows;
2765#endif
2766 while ((above < above_wanted || below < below_wanted) && topline < botline)
2767 {
2768 if (below < below_wanted && (below <= above || above >= above_wanted))
2769 {
2770#ifdef FEAT_FOLDING
2771 if (hasFolding(botline, &botline, NULL))
2772 ++below;
2773 else
2774#endif
2775 below += plines(botline);
2776 --botline;
2777 }
2778 if (above < above_wanted && (above < below || below >= below_wanted))
2779 {
2780#ifdef FEAT_FOLDING
2781 if (hasFolding(topline, NULL, &topline))
2782 ++above;
2783 else
2784#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002785 above += PLINES_NOFILL(topline);
2786#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002787 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (topline < botline)
2789 above += diff_check_fill(curwin, topline + 1);
2790#endif
2791 ++topline;
2792 }
2793 }
2794 if (topline == botline || botline == 0)
2795 curwin->w_cursor.lnum = topline;
2796 else if (topline > botline)
2797 curwin->w_cursor.lnum = botline;
2798 else
2799 {
2800 if (cln < topline && curwin->w_topline > 1)
2801 {
2802 curwin->w_cursor.lnum = topline;
2803 curwin->w_valid &=
2804 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2805 }
2806 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2807 {
2808 curwin->w_cursor.lnum = botline;
2809 curwin->w_valid &=
2810 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2811 }
2812 }
2813 curwin->w_valid |= VALID_TOPLINE;
2814}
2815
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002816static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
2818/*
2819 * move screen 'count' pages up or down and update screen
2820 *
2821 * return FAIL for failure, OK otherwise
2822 */
2823 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002824onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
2826 long n;
2827 int retval = OK;
2828 lineoff_T loff;
2829 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002830 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaar85a20022019-12-21 18:25:54 +01002832 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 beep_flush();
2835 return FAIL;
2836 }
2837
2838 for ( ; count > 0; --count)
2839 {
2840 validate_botline();
2841 /*
2842 * It's an error to move a page up when the first line is already on
2843 * the screen. It's an error to move a page down when the last line
2844 * is on the screen and the topline is 'scrolloff' lines from the
2845 * last line.
2846 */
2847 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002848 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2850 : (curwin->w_topline == 1
2851#ifdef FEAT_DIFF
2852 && curwin->w_topfill ==
2853 diff_check_fill(curwin, curwin->w_topline)
2854#endif
2855 ))
2856 {
2857 beep_flush();
2858 retval = FAIL;
2859 break;
2860 }
2861
2862#ifdef FEAT_DIFF
2863 loff.fill = 0;
2864#endif
2865 if (dir == FORWARD)
2866 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002867 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002869 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002870 if (p_window <= 2)
2871 ++curwin->w_topline;
2872 else
2873 curwin->w_topline += p_window - 2;
2874 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2875 curwin->w_topline = curbuf->b_ml.ml_line_count;
2876 curwin->w_cursor.lnum = curwin->w_topline;
2877 }
2878 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2879 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002880 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 curwin->w_topline = curbuf->b_ml.ml_line_count;
2882#ifdef FEAT_DIFF
2883 curwin->w_topfill = 0;
2884#endif
2885 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2886 }
2887 else
2888 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002889 // For the overlap, start with the line just below the window
2890 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 loff.lnum = curwin->w_botline;
2892#ifdef FEAT_DIFF
2893 loff.fill = diff_check_fill(curwin, loff.lnum)
2894 - curwin->w_filler_rows;
2895#endif
2896 get_scroll_overlap(&loff, -1);
2897 curwin->w_topline = loff.lnum;
2898#ifdef FEAT_DIFF
2899 curwin->w_topfill = loff.fill;
2900 check_topfill(curwin, FALSE);
2901#endif
2902 curwin->w_cursor.lnum = curwin->w_topline;
2903 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2904 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2905 }
2906 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002907 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
2909#ifdef FEAT_DIFF
2910 if (curwin->w_topline == 1)
2911 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002912 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 max_topfill();
2914 continue;
2915 }
2916#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002917 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002918 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002920 if (p_window <= 2)
2921 --curwin->w_topline;
2922 else
2923 curwin->w_topline -= p_window - 2;
2924 if (curwin->w_topline < 1)
2925 curwin->w_topline = 1;
2926 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2927 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2928 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2929 continue;
2930 }
2931
Bram Moolenaar85a20022019-12-21 18:25:54 +01002932 // Find the line at the top of the window that is going to be the
2933 // line at the bottom of the window. Make sure this results in
2934 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 loff.lnum = curwin->w_topline - 1;
2936#ifdef FEAT_DIFF
2937 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2938 - curwin->w_topfill;
2939#endif
2940 get_scroll_overlap(&loff, 1);
2941
2942 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2943 {
2944 loff.lnum = curbuf->b_ml.ml_line_count;
2945#ifdef FEAT_DIFF
2946 loff.fill = 0;
2947 }
2948 else
2949 {
2950 botline_topline(&loff);
2951#endif
2952 }
2953 curwin->w_cursor.lnum = loff.lnum;
2954
Bram Moolenaar85a20022019-12-21 18:25:54 +01002955 // Find the line just above the new topline to get the right line
2956 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 n = 0;
2958 while (n <= curwin->w_height && loff.lnum >= 1)
2959 {
2960 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002961 if (loff.height == MAXCOL)
2962 n = MAXCOL;
2963 else
2964 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002966 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
2968 curwin->w_topline = 1;
2969#ifdef FEAT_DIFF
2970 max_topfill();
2971#endif
2972 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2973 }
2974 else
2975 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002976 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977#ifdef FEAT_DIFF
2978 topline_botline(&loff);
2979#endif
2980 botline_forw(&loff);
2981 botline_forw(&loff);
2982#ifdef FEAT_DIFF
2983 botline_topline(&loff);
2984#endif
2985#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002986 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2988#endif
2989
Bram Moolenaar85a20022019-12-21 18:25:54 +01002990 // Always scroll at least one line. Avoid getting stuck on
2991 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (loff.lnum >= curwin->w_topline
2993#ifdef FEAT_DIFF
2994 && (loff.lnum > curwin->w_topline
2995 || loff.fill >= curwin->w_topfill)
2996#endif
2997 )
2998 {
2999#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01003000 // First try using the maximum number of filler lines. If
3001 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 loff.fill = curwin->w_topfill;
3003 if (curwin->w_topfill < diff_check_fill(curwin,
3004 curwin->w_topline))
3005 max_topfill();
3006 if (curwin->w_topfill == loff.fill)
3007#endif
3008 {
3009 --curwin->w_topline;
3010#ifdef FEAT_DIFF
3011 curwin->w_topfill = 0;
3012#endif
3013 }
3014 comp_botline(curwin);
3015 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003016 curwin->w_valid &=
3017 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
3019 else
3020 {
3021 curwin->w_topline = loff.lnum;
3022#ifdef FEAT_DIFF
3023 curwin->w_topfill = loff.fill;
3024 check_topfill(curwin, FALSE);
3025#endif
3026 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3027 }
3028 }
3029 }
3030 }
3031#ifdef FEAT_FOLDING
3032 foldAdjustCursor();
3033#endif
3034 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003035 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003036 if (retval == OK)
3037 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3039
Bram Moolenaar907dad72018-07-10 15:07:15 +02003040 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003042 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3043 // But make sure we scroll at least one line (happens with mix of long
3044 // wrapping lines and non-wrapping line).
3045 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003047 scroll_cursor_top(1, FALSE);
3048 if (curwin->w_topline <= old_topline
3049 && old_topline < curbuf->b_ml.ml_line_count)
3050 {
3051 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003053 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3054#endif
3055 }
3056 }
3057#ifdef FEAT_FOLDING
3058 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
3062
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003063 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 return retval;
3065}
3066
3067/*
3068 * Decide how much overlap to use for page-up or page-down scrolling.
3069 * This is symmetric, so that doing both keeps the same lines displayed.
3070 * Three lines are examined:
3071 *
3072 * before CTRL-F after CTRL-F / before CTRL-B
3073 * etc. l1
3074 * l1 last but one line ------------
3075 * l2 last text line l2 top text line
3076 * ------------- l3 second text line
3077 * l3 etc.
3078 */
3079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003080get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 int h1, h2, h3, h4;
3083 int min_height = curwin->w_height - 2;
3084 lineoff_T loff0, loff1, loff2;
3085
3086#ifdef FEAT_DIFF
3087 if (lp->fill > 0)
3088 lp->height = 1;
3089 else
3090 lp->height = plines_nofill(lp->lnum);
3091#else
3092 lp->height = plines(lp->lnum);
3093#endif
3094 h1 = lp->height;
3095 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003096 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 loff0 = *lp;
3099 if (dir > 0)
3100 botline_forw(lp);
3101 else
3102 topline_back(lp);
3103 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003104 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003106 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 return;
3108 }
3109
3110 loff1 = *lp;
3111 if (dir > 0)
3112 botline_forw(lp);
3113 else
3114 topline_back(lp);
3115 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003116 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003118 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 return;
3120 }
3121
3122 loff2 = *lp;
3123 if (dir > 0)
3124 botline_forw(lp);
3125 else
3126 topline_back(lp);
3127 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003128 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003129 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003131 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132}
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134/*
3135 * Scroll 'scroll' lines up or down.
3136 */
3137 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003138halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 long scrolled = 0;
3141 int i;
3142 int n;
3143 int room;
3144
3145 if (Prenum)
3146 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3147 curwin->w_height : Prenum;
3148 n = (curwin->w_p_scr <= curwin->w_height) ?
3149 curwin->w_p_scr : curwin->w_height;
3150
Bram Moolenaard5d37532017-03-27 23:02:07 +02003151 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 validate_botline();
3153 room = curwin->w_empty_rows;
3154#ifdef FEAT_DIFF
3155 room += curwin->w_filler_rows;
3156#endif
3157 if (flag)
3158 {
3159 /*
3160 * scroll the text up
3161 */
3162 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3163 {
3164#ifdef FEAT_DIFF
3165 if (curwin->w_topfill > 0)
3166 {
3167 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003168 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 --curwin->w_topfill;
3170 }
3171 else
3172#endif
3173 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003174 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 n -= i;
3176 if (n < 0 && scrolled > 0)
3177 break;
3178#ifdef FEAT_FOLDING
3179 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3180#endif
3181 ++curwin->w_topline;
3182#ifdef FEAT_DIFF
3183 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3184#endif
3185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3187 {
3188 ++curwin->w_cursor.lnum;
3189 curwin->w_valid &=
3190 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3194 scrolled += i;
3195
3196 /*
3197 * Correct w_botline for changed w_topline.
3198 * Won't work when there are filler lines.
3199 */
3200#ifdef FEAT_DIFF
3201 if (curwin->w_p_diff)
3202 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3203 else
3204#endif
3205 {
3206 room += i;
3207 do
3208 {
3209 i = plines(curwin->w_botline);
3210 if (i > room)
3211 break;
3212#ifdef FEAT_FOLDING
3213 (void)hasFolding(curwin->w_botline, NULL,
3214 &curwin->w_botline);
3215#endif
3216 ++curwin->w_botline;
3217 room -= i;
3218 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3219 }
3220 }
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 /*
3223 * When hit bottom of the file: move cursor down.
3224 */
3225 if (n > 0)
3226 {
3227# ifdef FEAT_FOLDING
3228 if (hasAnyFolding(curwin))
3229 {
3230 while (--n >= 0
3231 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3232 {
3233 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3234 &curwin->w_cursor.lnum);
3235 ++curwin->w_cursor.lnum;
3236 }
3237 }
3238 else
3239# endif
3240 curwin->w_cursor.lnum += n;
3241 check_cursor_lnum();
3242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244 else
3245 {
3246 /*
3247 * scroll the text down
3248 */
3249 while (n > 0 && curwin->w_topline > 1)
3250 {
3251#ifdef FEAT_DIFF
3252 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3253 {
3254 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003255 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 ++curwin->w_topfill;
3257 }
3258 else
3259#endif
3260 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003261 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 n -= i;
3263 if (n < 0 && scrolled > 0)
3264 break;
3265 --curwin->w_topline;
3266#ifdef FEAT_FOLDING
3267 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3268#endif
3269#ifdef FEAT_DIFF
3270 curwin->w_topfill = 0;
3271#endif
3272 }
3273 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3274 VALID_BOTLINE|VALID_BOTLINE_AP);
3275 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (curwin->w_cursor.lnum > 1)
3277 {
3278 --curwin->w_cursor.lnum;
3279 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 /*
3284 * When hit top of the file: move cursor up.
3285 */
3286 if (n > 0)
3287 {
3288 if (curwin->w_cursor.lnum <= (linenr_T)n)
3289 curwin->w_cursor.lnum = 1;
3290 else
3291# ifdef FEAT_FOLDING
3292 if (hasAnyFolding(curwin))
3293 {
3294 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3295 {
3296 --curwin->w_cursor.lnum;
3297 (void)hasFolding(curwin->w_cursor.lnum,
3298 &curwin->w_cursor.lnum, NULL);
3299 }
3300 }
3301 else
3302# endif
3303 curwin->w_cursor.lnum -= n;
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003307 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 foldAdjustCursor();
3309# endif
3310#ifdef FEAT_DIFF
3311 check_topfill(curwin, !flag);
3312#endif
3313 cursor_correct();
3314 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003315 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003317
Bram Moolenaar860cae12010-06-05 23:22:07 +02003318 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003319do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003320{
3321 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003322 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003323 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003324 colnr_T curswant = curwin->w_curswant;
3325 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003326 win_T *old_curwin = curwin;
3327 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003328 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003329 int old_VIsual_select = VIsual_select;
3330 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003331
3332 /*
3333 * loop through the cursorbound windows
3334 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003335 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003336 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003337 {
3338 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003339 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003340 if (curwin != old_curwin && curwin->w_p_crb)
3341 {
3342# ifdef FEAT_DIFF
3343 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003344 curwin->w_cursor.lnum =
3345 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003346 else
3347# endif
3348 curwin->w_cursor.lnum = line;
3349 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003350 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003351 curwin->w_curswant = curswant;
3352 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003353
Bram Moolenaar85a20022019-12-21 18:25:54 +01003354 // Make sure the cursor is in a valid position. Temporarily set
3355 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003356 restart_edit_save = restart_edit;
3357 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003358 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003359
3360 // Avoid a scroll here for the cursor position, 'scrollbind' is
3361 // more important.
3362 if (!curwin->w_p_scb)
3363 validate_cursor();
3364
Bram Moolenaar61452852011-02-01 18:01:11 +01003365 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003366 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003367 if (has_mbyte)
3368 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003369 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003370
Bram Moolenaar85a20022019-12-21 18:25:54 +01003371 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003372 if (!curwin->w_p_scb)
3373 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003374 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003375 }
3376 }
3377
3378 /*
3379 * reset current-window
3380 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003381 VIsual_select = old_VIsual_select;
3382 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003383 curwin = old_curwin;
3384 curbuf = old_curbuf;
3385}