blob: e255bd68e36ef119d15498e1bdbbf7a819aaecda [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 Moolenaar071d4272004-06-13 20:20:40 +0000205 * Update curwin->w_topline and redraw if necessary.
206 * Used to update the screen before printing a message.
207 */
208 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100209update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210{
211 update_topline();
212 if (must_redraw)
213 update_screen(0);
214}
215
216/*
217 * Update curwin->w_topline to move the cursor onto the screen.
218 */
219 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100220update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
222 long line_count;
223 int halfheight;
224 int n;
225 linenr_T old_topline;
226#ifdef FEAT_DIFF
227 int old_topfill;
228#endif
229#ifdef FEAT_FOLDING
230 linenr_T lnum;
231#endif
232 int check_topline = FALSE;
233 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100234 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100235 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100237 // Cursor is updated instead when this is TRUE for 'splitkeep'.
238 if (skip_update_topline)
239 return;
240
Bram Moolenaar85a20022019-12-21 18:25:54 +0100241 // If there is no valid screen and when the window height is zero just use
242 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200243 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200244 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100245 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200246 curwin->w_topline = curwin->w_cursor.lnum;
247 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200248 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200249 return;
250 }
251
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 check_cursor_moved(curwin);
253 if (curwin->w_valid & VALID_TOPLINE)
254 return;
255
Bram Moolenaar85a20022019-12-21 18:25:54 +0100256 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000257 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100258 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260 old_topline = curwin->w_topline;
261#ifdef FEAT_DIFF
262 old_topfill = curwin->w_topfill;
263#endif
264
265 /*
266 * If the buffer is empty, always set topline to 1.
267 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100268 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
270 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100271 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 curwin->w_botline = 2;
274 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
277
278 /*
279 * If the cursor is above or near the top of the window, scroll the window
280 * to show the line the cursor is in, with 'scrolloff' context.
281 */
282 else
283 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100284 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100286 // If the cursor is above topline, scrolling is always needed.
287 // If the cursor is far below topline and there is no folding,
288 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 if (curwin->w_cursor.lnum < curwin->w_topline)
290 check_topline = TRUE;
291 else if (check_top_offset())
292 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100293 else if (curwin->w_cursor.lnum == curwin->w_topline)
294 {
295 colnr_T vcol;
296
297 // check the cursor position is visible. Add 3 for the ">>>"
298 // displayed in the top-left.
299 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
300 if (curwin->w_skipcol + 3 >= vcol)
301 check_topline = TRUE;
302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 }
304#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100305 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
307 curwin->w_topline))
308 check_topline = TRUE;
309#endif
310
311 if (check_topline)
312 {
313 halfheight = curwin->w_height / 2 - 1;
314 if (halfheight < 2)
315 halfheight = 2;
316
317#ifdef FEAT_FOLDING
318 if (hasAnyFolding(curwin))
319 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100320 // Count the number of logical lines between the cursor and
321 // topline + scrolloff (approximation of how much will be
322 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 n = 0;
324 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100325 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 {
327 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100328 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
330 break;
331 (void)hasFolding(lnum, NULL, &lnum);
332 }
333 }
334 else
335#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100336 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar85a20022019-12-21 18:25:54 +0100338 // If we weren't very close to begin with, we scroll to put the
339 // cursor in the middle of the window. Otherwise put the cursor
340 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 if (n >= halfheight)
342 scroll_cursor_halfway(FALSE);
343 else
344 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000345 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 check_botline = TRUE;
347 }
348 }
349
350 else
351 {
352#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100353 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
355#endif
356 check_botline = TRUE;
357 }
358 }
359
360 /*
361 * If the cursor is below the bottom of the window, scroll the window
362 * to put the cursor on the window.
363 * When w_botline is invalid, recompute it first, to avoid a redraw later.
364 * If w_botline was approximated, we might need a redraw later in a few
365 * cases, but we don't want to spend (a lot of) time recomputing w_botline
366 * for every small change.
367 */
368 if (check_botline)
369 {
370 if (!(curwin->w_valid & VALID_BOTLINE_AP))
371 validate_botline();
372
373 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
374 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000375 if (curwin->w_cursor.lnum < curwin->w_botline)
376 {
377 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100378 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379#ifdef FEAT_FOLDING
380 || hasAnyFolding(curwin)
381#endif
382 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 lineoff_T loff;
385
Bram Moolenaar85a20022019-12-21 18:25:54 +0100386 // Cursor is (a few lines) above botline, check if there are
387 // 'scrolloff' window lines below the cursor. If not, need to
388 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 n = curwin->w_empty_rows;
390 loff.lnum = curwin->w_cursor.lnum;
391#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100392 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
394#endif
395#ifdef FEAT_DIFF
396 loff.fill = 0;
397 n += curwin->w_filler_rows;
398#endif
399 loff.height = 0;
400 while (loff.lnum < curwin->w_botline
401#ifdef FEAT_DIFF
402 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
403#endif
404 )
405 {
406 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100407 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 break;
409 botline_forw(&loff);
410 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100411 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100412 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000414 }
415 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100416 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000417 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 }
419 if (check_botline)
420 {
421#ifdef FEAT_FOLDING
422 if (hasAnyFolding(curwin))
423 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100424 // Count the number of logical lines between the cursor and
425 // botline - scrolloff (approximation of how much will be
426 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 line_count = 0;
428 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100429 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {
431 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100432 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 if (lnum <= 0 || line_count > curwin->w_height + 1)
434 break;
435 (void)hasFolding(lnum, &lnum, NULL);
436 }
437 }
438 else
439#endif
440 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100441 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000443 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 else
445 scroll_cursor_halfway(FALSE);
446 }
447 }
448 }
449 curwin->w_valid |= VALID_TOPLINE;
450
451 /*
452 * Need to redraw when topline changed.
453 */
454 if (curwin->w_topline != old_topline
455#ifdef FEAT_DIFF
456 || curwin->w_topfill != old_topfill
457#endif
458 )
459 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100460 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000461 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 {
463 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100464 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
466 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100467 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100468 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 if (curwin->w_cursor.lnum == curwin->w_topline)
470 validate_cursor();
471 }
472
Bram Moolenaar375e3392019-01-31 18:26:10 +0100473 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474}
475
476/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000477 * Return the scrolljump value to use for the current window.
478 * When 'scrolljump' is positive use it as-is.
479 * When 'scrolljump' is negative use it as a percentage of the window height.
480 */
481 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100482scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000483{
484 if (p_sj >= 0)
485 return (int)p_sj;
486 return (curwin->w_height * -p_sj) / 100;
487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
491 * current window.
492 */
493 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100494check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496 lineoff_T loff;
497 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100498 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar375e3392019-01-31 18:26:10 +0100500 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501#ifdef FEAT_FOLDING
502 || hasAnyFolding(curwin)
503#endif
504 )
505 {
506 loff.lnum = curwin->w_cursor.lnum;
507#ifdef FEAT_DIFF
508 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100509 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510#else
511 n = 0;
512#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100513 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100514 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
516 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100517 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (loff.lnum < curwin->w_topline
519#ifdef FEAT_DIFF
520 || (loff.lnum == curwin->w_topline && loff.fill > 0)
521#endif
522 )
523 break;
524 n += loff.height;
525 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100526 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 return TRUE;
528 }
529 return FALSE;
530}
531
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100532/*
533 * Update w_curswant.
534 */
535 void
536update_curswant_force(void)
537{
538 validate_virtcol();
539 curwin->w_curswant = curwin->w_virtcol
540#ifdef FEAT_PROP_POPUP
541 - curwin->w_virtcol_first_char
542#endif
543 ;
544 curwin->w_set_curswant = FALSE;
545}
546
547/*
548 * Update w_curswant if w_set_curswant is set.
549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100551update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552{
553 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100554 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555}
556
557/*
558 * Check if the cursor has moved. Set the w_valid flag accordingly.
559 */
560 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100561check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562{
563 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
564 {
565 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100566 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
567 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 wp->w_valid_cursor = wp->w_cursor;
569 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100570 wp->w_valid_skipcol = wp->w_skipcol;
571 }
572 else if (wp->w_skipcol != wp->w_valid_skipcol)
573 {
574 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
575 |VALID_CHEIGHT|VALID_CROW
576 |VALID_BOTLINE|VALID_BOTLINE_AP);
577 wp->w_valid_cursor = wp->w_cursor;
578 wp->w_valid_leftcol = wp->w_leftcol;
579 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
581 else if (wp->w_cursor.col != wp->w_valid_cursor.col
582 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100583 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {
585 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
586 wp->w_valid_cursor.col = wp->w_cursor.col;
587 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 }
590}
591
592/*
593 * Call this function when some window settings have changed, which require
594 * the cursor position, botline and topline to be recomputed and the window to
595 * be redrawn. E.g, when changing the 'wrap' option or folding.
596 */
597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 changed_window_setting_win(curwin);
601}
602
603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 wp->w_lines_valid = 0;
607 changed_line_abv_curs_win(wp);
608 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100609 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610}
611
612/*
613 * Set wp->w_topline to a certain number.
614 */
615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100616set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200618#ifdef FEAT_DIFF
619 linenr_T prev_topline = wp->w_topline;
620#endif
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100623 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
625#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100626 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100628 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
629 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000631 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200633 if (lnum != prev_topline)
634 // Keep the filler lines when the topline didn't change.
635 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#endif
637 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100638 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100639 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640}
641
642/*
643 * Call this function when the length of the cursor line (in screen
644 * characters) has changed, and the change is before the cursor.
645 * Need to take care of w_botline separately!
646 */
647 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100648changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
650 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
651 |VALID_CHEIGHT|VALID_TOPLINE);
652}
653
654 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100655changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
657 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
658 |VALID_CHEIGHT|VALID_TOPLINE);
659}
660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661/*
662 * Call this function when the length of a line (in screen characters) above
663 * the cursor have changed.
664 * Need to take care of w_botline separately!
665 */
666 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100667changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
669 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
670 |VALID_CHEIGHT|VALID_TOPLINE);
671}
672
673 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100674changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
677 |VALID_CHEIGHT|VALID_TOPLINE);
678}
679
680/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100681 * Display of line has changed for "buf", invalidate cursor position and
682 * w_botline.
683 */
684 void
685changed_line_display_buf(buf_T *buf)
686{
687 win_T *wp;
688
689 FOR_ALL_WINDOWS(wp)
690 if (wp->w_buffer == buf)
691 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
692 |VALID_CROW|VALID_CHEIGHT
693 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
694}
695
696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 * Make sure the value of curwin->w_botline is valid.
698 */
699 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100700validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100702 validate_botline_win(curwin);
703}
704
705/*
706 * Make sure the value of wp->w_botline is valid.
707 */
708 void
709validate_botline_win(win_T *wp)
710{
711 if (!(wp->w_valid & VALID_BOTLINE))
712 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713}
714
715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 * Mark curwin->w_botline as invalid (because of some change in the buffer).
717 */
718 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100719invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
722}
723
724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100725invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726{
727 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
728}
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731approximate_botline_win(
732 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
734 wp->w_valid &= ~VALID_BOTLINE;
735}
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
738 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
739 */
740 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100741cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
743 check_cursor_moved(curwin);
744 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
745 (VALID_WROW|VALID_WCOL));
746}
747
748/*
749 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
750 * w_topline must be valid, you may need to call update_topline() first!
751 */
752 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100753validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100755 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 check_cursor_moved(curwin);
757 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
758 curs_columns(TRUE);
759}
760
761#if defined(FEAT_GUI) || defined(PROTO)
762/*
763 * validate w_cline_row.
764 */
765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100766validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768 /*
769 * First make sure that w_topline is valid (after moving the cursor).
770 */
771 update_topline();
772 check_cursor_moved(curwin);
773 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100774 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775}
776#endif
777
778/*
779 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200780 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 */
782 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100783curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
785 linenr_T lnum;
786 int i;
787 int all_invalid;
788 int valid;
789#ifdef FEAT_FOLDING
790 long fold_count;
791#endif
792
Bram Moolenaar85a20022019-12-21 18:25:54 +0100793 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 all_invalid = (!redrawing()
795 || wp->w_lines_valid == 0
796 || wp->w_lines[0].wl_lnum > wp->w_topline);
797 i = 0;
798 wp->w_cline_row = 0;
799 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
800 {
801 valid = FALSE;
802 if (!all_invalid && i < wp->w_lines_valid)
803 {
804 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100805 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (wp->w_lines[i].wl_lnum == lnum)
807 {
808#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100809 // Check for newly inserted lines below this row, in which
810 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (!wp->w_buffer->b_mod_set
812 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
813 || wp->w_buffer->b_mod_top
814 > wp->w_lines[i].wl_lastlnum + 1)
815#endif
816 valid = TRUE;
817 }
818 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100819 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 }
821 if (valid
822#ifdef FEAT_DIFF
823 && (lnum != wp->w_topline || !wp->w_p_diff)
824#endif
825 )
826 {
827#ifdef FEAT_FOLDING
828 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100829 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (lnum > wp->w_cursor.lnum)
831 break;
832#else
833 ++lnum;
834#endif
835 wp->w_cline_row += wp->w_lines[i].wl_size;
836 }
837 else
838 {
839#ifdef FEAT_FOLDING
840 fold_count = foldedCount(wp, lnum, NULL);
841 if (fold_count)
842 {
843 lnum += fold_count;
844 if (lnum > wp->w_cursor.lnum)
845 break;
846 ++wp->w_cline_row;
847 }
848 else
849#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100850 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +0100851 wp->w_cline_row += plines_correct_topline(wp, lnum);
852 ++lnum;
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 }
856
857 check_cursor_moved(wp);
858 if (!(wp->w_valid & VALID_CHEIGHT))
859 {
860 if (all_invalid
861 || i == wp->w_lines_valid
862 || (i < wp->w_lines_valid
863 && (!wp->w_lines[i].wl_valid
864 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
865 {
866#ifdef FEAT_DIFF
867 if (wp->w_cursor.lnum == wp->w_topline)
868 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
869 TRUE) + wp->w_topfill;
870 else
871#endif
872 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
873#ifdef FEAT_FOLDING
874 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
875 NULL, NULL, TRUE, NULL);
876#endif
877 }
878 else if (i > wp->w_lines_valid)
879 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100880 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 wp->w_cline_height = 0;
882#ifdef FEAT_FOLDING
883 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
884 NULL, NULL, TRUE, NULL);
885#endif
886 }
887 else
888 {
889 wp->w_cline_height = wp->w_lines[i].wl_size;
890#ifdef FEAT_FOLDING
891 wp->w_cline_folded = wp->w_lines[i].wl_folded;
892#endif
893 }
894 }
895
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100896 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898}
899
900/*
901 * Validate curwin->w_virtcol only.
902 */
903 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100904validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906 validate_virtcol_win(curwin);
907}
908
909/*
910 * Validate wp->w_virtcol only.
911 */
912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100913validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
915 check_cursor_moved(wp);
916 if (!(wp->w_valid & VALID_VIRTCOL))
917 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100918#ifdef FEAT_PROP_POPUP
919 wp->w_virtcol_first_char = 0;
920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000922#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100923 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000924#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100925 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
927}
928
929/*
930 * Validate curwin->w_cline_height only.
931 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100932 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100933validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934{
935 check_cursor_moved(curwin);
936 if (!(curwin->w_valid & VALID_CHEIGHT))
937 {
938#ifdef FEAT_DIFF
939 if (curwin->w_cursor.lnum == curwin->w_topline)
940 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
941 + curwin->w_topfill;
942 else
943#endif
944 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
945#ifdef FEAT_FOLDING
946 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
947#endif
948 curwin->w_valid |= VALID_CHEIGHT;
949 }
950}
951
952/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000953 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 */
955 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100956validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
958 colnr_T off;
959 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100960 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961
962 validate_virtcol();
963 if (!(curwin->w_valid & VALID_WCOL))
964 {
965 col = curwin->w_virtcol;
966 off = curwin_col_off();
967 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200968 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
Bram Moolenaar85a20022019-12-21 18:25:54 +0100970 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000971 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200972 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100973 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100974 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200975 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000976 if (col > (int)curwin->w_leftcol)
977 col -= curwin->w_leftcol;
978 else
979 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100983#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100984 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987}
988
989/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200990 * Compute offset of a window, occupied by absolute or relative line number,
991 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 */
993 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100994win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
Bram Moolenaar64486672010-05-16 15:46:46 +0200996 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#ifdef FEAT_FOLDING
999 + wp->w_p_fdc
1000#endif
1001#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02001002 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#endif
1004 );
1005}
1006
1007 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001008curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 return win_col_off(curwin);
1011}
1012
1013/*
1014 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001015 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1016 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
1018 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001019win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar64486672010-05-16 15:46:46 +02001021 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001022 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 return 0;
1024}
1025
1026 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001027curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 return win_col_off2(curwin);
1030}
1031
1032/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001033 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 * Also updates curwin->w_wrow and curwin->w_cline_row.
1035 * Also updates curwin->w_leftcol.
1036 */
1037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001038curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001039 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001042 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 int off_left, off_right;
1044 int n;
1045 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001046 int width1; // text width for first screen line
1047 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 int new_leftcol;
1049 colnr_T startcol;
1050 colnr_T endcol;
1051 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001052 long so = get_scrolloff_value();
1053 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001054 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 /*
1057 * First make sure that w_topline is valid (after moving the cursor).
1058 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001059 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
1061 /*
1062 * Next make sure that w_cline_row is valid.
1063 */
1064 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001065 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001067#ifdef FEAT_PROP_POPUP
1068 // will be set by getvvcol() but not reset
1069 curwin->w_virtcol_first_char = 0;
1070#endif
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 /*
1073 * Compute the number of virtual columns.
1074 */
1075#ifdef FEAT_FOLDING
1076 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001077 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1079 else
1080#endif
1081 getvvcol(curwin, &curwin->w_cursor,
1082 &startcol, &(curwin->w_virtcol), &endcol);
1083
Bram Moolenaar85a20022019-12-21 18:25:54 +01001084 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001086 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 extra = curwin_col_off();
1089 curwin->w_wcol = curwin->w_virtcol + extra;
1090 endcol += extra;
1091
1092 /*
1093 * Now compute w_wrow, counting screen lines from w_cline_row.
1094 */
1095 curwin->w_wrow = curwin->w_cline_row;
1096
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001097 width1 = curwin->w_width - extra;
1098 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001100 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001101 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001102 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001103 if (curwin->w_p_wrap)
1104 curwin->w_wrow = curwin->w_height - 1;
1105 else
1106 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001108 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001110 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001112 // skip columns that are not visible
1113 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001114 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001115 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001116 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001117 // w_skipcol excludes win_col_off(). Include it here, since w_wcol
1118 // counts actual screen columns.
1119 if (curwin->w_skipcol <= width1)
1120 curwin->w_wcol -= curwin->w_width;
1121 else
1122 curwin->w_wcol -= curwin->w_width
1123 * (((curwin->w_skipcol - width1) / width2) + 1);
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001124 did_sub_skipcol = TRUE;
1125 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001126
Bram Moolenaar85a20022019-12-21 18:25:54 +01001127 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001128 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001130 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001131 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1132 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 curwin->w_wrow += n;
1134
1135#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001136 // When cursor wraps to first char of next line in Insert
1137 // mode, the 'showbreak' string isn't shown, backup to first
1138 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001139 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001140 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001141 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 curwin->w_wcol = 0;
1143#endif
1144 }
1145 }
1146
Bram Moolenaar85a20022019-12-21 18:25:54 +01001147 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1148 // is not folded.
1149 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001150 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151#ifdef FEAT_FOLDING
1152 && !curwin->w_cline_folded
1153#endif
1154 )
1155 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001156#ifdef FEAT_PROP_POPUP
1157 if (curwin->w_virtcol_first_char > 0)
1158 {
1159 int cols = (curwin->w_width - extra);
1160 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1161
1162 // each "above" text prop shifts the text one row down
1163 curwin->w_wrow += rows;
1164 curwin->w_wcol -= rows * cols;
1165 endcol -= rows * cols;
1166 curwin->w_cline_height = rows + 1;
1167 }
1168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 /*
1170 * If Cursor is left of the screen, scroll rightwards.
1171 * If Cursor is right of the screen, scroll leftwards
1172 * If we get closer to the edge than 'sidescrolloff', scroll a little
1173 * extra
1174 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001175 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001176 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001177 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (off_left < 0 || off_right > 0)
1179 {
1180 if (off_left < 0)
1181 diff = -off_left;
1182 else
1183 diff = off_right;
1184
Bram Moolenaar85a20022019-12-21 18:25:54 +01001185 // When far off or not enough room on either side, put cursor in
1186 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001187 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1188 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 else
1190 {
1191 if (diff < p_ss)
1192 diff = p_ss;
1193 if (off_left < 0)
1194 new_leftcol = curwin->w_leftcol - diff;
1195 else
1196 new_leftcol = curwin->w_leftcol + diff;
1197 }
1198 if (new_leftcol < 0)
1199 new_leftcol = 0;
1200 if (new_leftcol != (int)curwin->w_leftcol)
1201 {
1202 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001203 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001204 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 }
1207 curwin->w_wcol -= curwin->w_leftcol;
1208 }
1209 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1210 curwin->w_wcol -= curwin->w_leftcol;
1211 else
1212 curwin->w_wcol = 0;
1213
1214#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001215 // Skip over filler lines. At the top use w_topfill, there
1216 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (curwin->w_cursor.lnum == curwin->w_topline)
1218 curwin->w_wrow += curwin->w_topfill;
1219 else
1220 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1221#endif
1222
1223 prev_skipcol = curwin->w_skipcol;
1224
1225 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if ((curwin->w_wrow >= curwin->w_height
1228 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001229 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 && (p_lines =
1231#ifdef FEAT_DIFF
1232 plines_win_nofill
1233#else
1234 plines_win
1235#endif
1236 (curwin, curwin->w_cursor.lnum, FALSE))
1237 - 1 >= curwin->w_height))
1238 && curwin->w_height != 0
1239 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001240 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001241 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001243 // Cursor past end of screen. Happens with a single line that does
1244 // not fit on screen. Find a skipcol to show the text around the
1245 // cursor. Avoid scrolling all the time. compute value of "extra":
1246 // 1: Less than 'scrolloff' lines above
1247 // 2: Less than 'scrolloff' lines below
1248 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001250 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001252 // Compute last display line of the buffer line that we want at the
1253 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (p_lines == 0)
1255 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1256 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001257 if (p_lines > curwin->w_wrow + so)
1258 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 else
1260 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001261 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 extra += 2;
1263
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001264 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001266 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001267 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 if (n > curwin->w_height / 2)
1269 n -= curwin->w_height / 2;
1270 else
1271 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001272 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (n > p_lines - curwin->w_height + 1)
1274 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001275 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 else if (extra == 1)
1278 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001279 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001280 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1281 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (extra > 0)
1283 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001284 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1285 extra = curwin->w_skipcol / width2;
1286 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 }
1289 else if (extra == 2)
1290 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001291 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001292 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001294 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (endcol > curwin->w_skipcol)
1296 curwin->w_skipcol = endcol;
1297 }
1298
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001299 // adjust w_wrow for the changed w_skipcol
1300 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001301 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001302 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001303 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001304
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (curwin->w_wrow >= curwin->w_height)
1306 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001307 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001309 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 curwin->w_wrow -= extra;
1311 }
1312
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001313 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (extra > 0)
1315 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1316 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001317 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001319 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 curwin->w_skipcol = 0;
1321 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001322 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001324#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001325 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001326#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001327#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1328 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1329 {
1330 curwin->w_wrow += popup_top_extra(curwin);
1331 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001332 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001333 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001334 else
1335 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001336#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001337
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001338 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1339 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001340 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001341 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1344}
1345
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001346#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001347/*
1348 * Compute the screen position of text character at "pos" in window "wp"
1349 * The resulting values are one-based, zero when character is not visible.
1350 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001351 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001352textpos2screenpos(
1353 win_T *wp,
1354 pos_T *pos,
1355 int *rowp, // screen row
1356 int *scolp, // start screen column
1357 int *ccolp, // cursor screen column
1358 int *ecolp) // end screen column
1359{
1360 colnr_T scol = 0, ccol = 0, ecol = 0;
1361 int row = 0;
1362 int rowoff = 0;
1363 colnr_T coloff = 0;
1364
Bram Moolenaar189663b2021-07-21 18:04:56 +02001365 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001366 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001367 colnr_T off;
1368 colnr_T col;
1369 int width;
1370 linenr_T lnum = pos->lnum;
1371#ifdef FEAT_FOLDING
1372 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001373
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001374 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1375#endif
1376 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1377#ifdef FEAT_FOLDING
1378 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001379 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001380 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001381 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001382 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001383 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001384#endif
1385 {
1386 getvcol(wp, pos, &scol, &ccol, &ecol);
1387
1388 // similar to what is done in validate_cursor_col()
1389 col = scol;
1390 off = win_col_off(wp);
1391 col += off;
1392 width = wp->w_width - off + win_col_off2(wp);
1393
1394 // long line wrapping, adjust row
1395 if (wp->w_p_wrap
1396 && col >= (colnr_T)wp->w_width
1397 && width > 0)
1398 {
1399 // use same formula as what is used in curs_columns()
1400 rowoff = ((col - wp->w_width) / width + 1);
1401 col -= rowoff * width;
1402 }
1403 col -= wp->w_leftcol;
1404 if (col >= wp->w_width)
1405 col = -1;
1406 if (col >= 0 && row + rowoff <= wp->w_height)
1407 {
1408 coloff = col - scol + wp->w_wincol + 1;
1409 row += W_WINROW(wp);
1410 }
1411 else
1412 // character is left, right or below of the window
1413 row = rowoff = scol = ccol = ecol = 0;
1414 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001415 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001416 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001417 *scolp = scol + coloff;
1418 *ccolp = ccol + coloff;
1419 *ecolp = ecol + coloff;
1420}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001421#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001422
Bram Moolenaar12034e22019-08-25 22:25:02 +02001423#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001424/*
1425 * "screenpos({winid}, {lnum}, {col})" function
1426 */
1427 void
1428f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1429{
1430 dict_T *dict;
1431 win_T *wp;
1432 pos_T pos;
1433 int row = 0;
1434 int scol = 0, ccol = 0, ecol = 0;
1435
Bram Moolenaar93a10962022-06-16 11:42:09 +01001436 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001437 return;
1438 dict = rettv->vval.v_dict;
1439
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001440 if (in_vim9script()
1441 && (check_for_number_arg(argvars, 0) == FAIL
1442 || check_for_number_arg(argvars, 1) == FAIL
1443 || check_for_number_arg(argvars, 2) == FAIL))
1444 return;
1445
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001446 wp = find_win_by_nr_or_id(&argvars[0]);
1447 if (wp == NULL)
1448 return;
1449
1450 pos.lnum = tv_get_number(&argvars[1]);
1451 pos.col = tv_get_number(&argvars[2]) - 1;
1452 pos.coladd = 0;
1453 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1454
1455 dict_add_number(dict, "row", row);
1456 dict_add_number(dict, "col", scol);
1457 dict_add_number(dict, "curscol", ccol);
1458 dict_add_number(dict, "endcol", ecol);
1459}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001460
1461/*
1462 * "virtcol2col({winid}, {lnum}, {col})" function
1463 */
1464 void
1465f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1466{
1467 win_T *wp;
1468 linenr_T lnum;
1469 int screencol;
1470 int error = FALSE;
1471
1472 rettv->vval.v_number = -1;
1473
1474 if (check_for_number_arg(argvars, 0) == FAIL
1475 || check_for_number_arg(argvars, 1) == FAIL
1476 || check_for_number_arg(argvars, 2) == FAIL)
1477 return;
1478
1479 wp = find_win_by_nr_or_id(&argvars[0]);
1480 if (wp == NULL)
1481 return;
1482
1483 lnum = tv_get_number_chk(&argvars[1], &error);
1484 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1485 return;
1486
1487 screencol = tv_get_number_chk(&argvars[2], &error);
1488 if (error || screencol < 0)
1489 return;
1490
1491 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1492}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001493#endif
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495/*
1496 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001499scrolldown(
1500 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001501 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001503 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 int wrow;
1505 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001506 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001507 int width1 = 0;
1508 int width2 = 0;
1509
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001510 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001511 {
1512 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001513 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
1516#ifdef FEAT_FOLDING
1517 linenr_T first;
1518
Bram Moolenaar85a20022019-12-21 18:25:54 +01001519 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1521#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001522 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001523 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
1525#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001526 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1527 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 ++curwin->w_topfill;
1530 ++done;
1531 }
1532 else
1533#endif
1534 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001535 // break when at the very top
1536 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001537 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001539 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001541 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001542 if (curwin->w_skipcol >= width1 + width2)
1543 curwin->w_skipcol -= width2;
1544 else
1545 curwin->w_skipcol -= width1;
1546 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 }
1549 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001550 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001551 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001552 --curwin->w_topline;
1553 curwin->w_skipcol = 0;
1554#ifdef FEAT_DIFF
1555 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001557#ifdef FEAT_FOLDING
1558 // A sequence of folded lines only counts for one logical line
1559 if (hasFolding(curwin->w_topline, &first, NULL))
1560 {
1561 ++done;
1562 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001563 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001564 curwin->w_botline -= curwin->w_topline - first;
1565 curwin->w_topline = first;
1566 }
1567 else
1568#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001569 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001570 {
1571 int size = win_linetabsize(curwin, curwin->w_topline,
1572 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1573 if (size > width1)
1574 {
1575 curwin->w_skipcol = width1;
1576 size -= width1;
1577 redraw_later(UPD_NOT_VALID);
1578 }
1579 while (size > width2)
1580 {
1581 curwin->w_skipcol += width2;
1582 size -= width2;
1583 }
1584 ++done;
1585 }
1586 else
1587 done += PLINES_NOFILL(curwin->w_topline);
1588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001590 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 invalidate_botline();
1592 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001593 curwin->w_wrow += done; // keep w_wrow updated
1594 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
1596#ifdef FEAT_DIFF
1597 if (curwin->w_cursor.lnum == curwin->w_topline)
1598 curwin->w_cline_row = 0;
1599 check_topfill(curwin, TRUE);
1600#endif
1601
1602 /*
1603 * Compute the row number of the last row of the cursor line
1604 * and move the cursor onto the displayed part of the window.
1605 */
1606 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001607 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
1609 validate_virtcol();
1610 validate_cheight();
1611 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001612 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1615 {
1616#ifdef FEAT_FOLDING
1617 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1618 {
1619 --wrow;
1620 if (first == 1)
1621 curwin->w_cursor.lnum = 1;
1622 else
1623 curwin->w_cursor.lnum = first - 1;
1624 }
1625 else
1626#endif
1627 wrow -= plines(curwin->w_cursor.lnum--);
1628 curwin->w_valid &=
1629 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1630 moved = TRUE;
1631 }
1632 if (moved)
1633 {
1634#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001635 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 foldAdjustCursor();
1637#endif
1638 coladvance(curwin->w_curswant);
1639 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001640
1641 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1642 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001643 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001644 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1645
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001646 // make sure the cursor is in the visible text
1647 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001648 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001649 int row = 0;
1650 if (col >= width1)
1651 {
1652 col -= width1;
1653 ++row;
1654 }
1655 if (col > width2)
1656 {
1657 row += col / width2;
1658 col = col % width2;
1659 }
1660 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001661 {
1662 curwin->w_curswant = curwin->w_virtcol
1663 - (row - curwin->w_height + 1) * width2;
1664 coladvance(curwin->w_curswant);
1665 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
1669/*
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001670 * Return TRUE if scrollup() will scroll by screen line rather than text line.
1671 */
1672 static int
1673scrolling_screenlines(int byfold UNUSED)
1674{
1675 return (curwin->w_p_wrap && curwin->w_p_sms)
1676# ifdef FEAT_FOLDING
1677 || (byfold && hasAnyFolding(curwin))
1678# endif
1679# ifdef FEAT_DIFF
1680 || curwin->w_p_diff
1681# endif
1682 ;
1683}
1684
1685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001689scrollup(
1690 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001691 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001693 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001695 if (scrolling_screenlines(byfold))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001697 int width1 = curwin->w_width - curwin_col_off();
1698 int width2 = width1 + curwin_col_off2();
1699 int size = 0;
1700 linenr_T prev_topline = curwin->w_topline;
1701
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001702 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001703 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001704
1705 // diff mode: first consume "topfill"
1706 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1707 // the line, then advance to the next line.
1708 // folding: count each sequence of folded lines as one logical line.
1709 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
1711# ifdef FEAT_DIFF
1712 if (curwin->w_topfill > 0)
1713 --curwin->w_topfill;
1714 else
1715# endif
1716 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001717 linenr_T lnum = curwin->w_topline;
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719# ifdef FEAT_FOLDING
1720 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001721 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 (void)hasFolding(lnum, NULL, &lnum);
1723# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001724 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001725 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001726 // 'smoothscroll': increase "w_skipcol" until it goes over
1727 // the end of the line, then advance to the next line.
1728 int add = curwin->w_skipcol > 0 ? width2 : width1;
1729 curwin->w_skipcol += add;
1730 if (curwin->w_skipcol >= size)
1731 {
1732 if (lnum == curbuf->b_ml.ml_line_count)
1733 {
1734 // at the last screen line, can't scroll further
1735 curwin->w_skipcol -= add;
1736 break;
1737 }
1738 ++lnum;
1739 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001740 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001741 else
1742 {
1743 if (lnum >= curbuf->b_ml.ml_line_count)
1744 break;
1745 ++lnum;
1746 }
1747
1748 if (lnum > curwin->w_topline)
1749 {
1750 // approximate w_botline
1751 curwin->w_botline += lnum - curwin->w_topline;
1752 curwin->w_topline = lnum;
1753# ifdef FEAT_DIFF
1754 curwin->w_topfill = diff_check_fill(curwin, lnum);
1755# endif
1756 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001757 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001758 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001759 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001760 }
1761 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001762
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001763 if (curwin->w_topline == prev_topline)
1764 // need to redraw even though w_topline didn't change
1765 redraw_later(UPD_NOT_VALID);
1766 }
1767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 {
1769 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001770 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 }
1772
1773 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1774 curwin->w_topline = curbuf->b_ml.ml_line_count;
1775 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1776 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1777
1778#ifdef FEAT_DIFF
1779 check_topfill(curwin, FALSE);
1780#endif
1781
1782#ifdef FEAT_FOLDING
1783 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001784 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1786#endif
1787
1788 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1789 if (curwin->w_cursor.lnum < curwin->w_topline)
1790 {
1791 curwin->w_cursor.lnum = curwin->w_topline;
1792 curwin->w_valid &=
1793 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1794 coladvance(curwin->w_curswant);
1795 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001796 if (curwin->w_cursor.lnum == curwin->w_topline
1797 && do_sms && curwin->w_skipcol > 0)
1798 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001799 int width1 = curwin->w_width - curwin_col_off();
1800 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001801 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001802 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001803 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001804
1805 // Make sure the cursor is in a visible part of the line, taking
1806 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001807 // If there are not enough screen lines put the cursor in the middle.
1808 if (scrolloff_cols > space_cols / 2)
1809 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001810 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001811 if (curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001812 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001813 colnr_T col = curwin->w_virtcol;
1814
1815 if (col < width1)
1816 col += width1;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001817 while (col < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001818 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001819 curwin->w_curswant = col;
1820 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001821
1822 // validate_virtcol() marked various things as valid, but after
1823 // moving the cursor they need to be recomputed
1824 curwin->w_valid &=
1825 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001826 }
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828}
1829
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001830/*
1831 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1832 * valid for 'smoothscroll'.
1833 */
1834 void
1835adjust_skipcol(void)
1836{
1837 if (!curwin->w_p_wrap
1838 || !curwin->w_p_sms
1839 || curwin->w_cursor.lnum != curwin->w_topline)
1840 return;
1841
1842 int width1 = curwin->w_width - curwin_col_off();
1843 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001844 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001845 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1846 int scrolled = FALSE;
1847
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001848 validate_cheight();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001849 if (curwin->w_cline_height == curwin->w_height)
1850 {
1851 // the line just fits in the window, don't scroll
1852 if (curwin->w_skipcol != 0)
1853 {
1854 curwin->w_skipcol = 0;
1855 redraw_later(UPD_NOT_VALID);
1856 }
1857 return;
1858 }
1859
Bram Moolenaard5337ef2022-10-20 20:15:47 +01001860 validate_virtcol();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001861 while (curwin->w_skipcol > 0
1862 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1863 {
1864 // scroll a screen line down
1865 if (curwin->w_skipcol >= width1 + width2)
1866 curwin->w_skipcol -= width2;
1867 else
1868 curwin->w_skipcol -= width1;
1869 redraw_later(UPD_NOT_VALID);
1870 scrolled = TRUE;
1871 validate_virtcol();
1872 }
1873 if (scrolled)
1874 return; // don't scroll in the other direction now
1875
1876 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1877 int row = 0;
1878 if (col >= width1)
1879 {
1880 col -= width1;
1881 ++row;
1882 }
1883 if (col > width2)
1884 {
1885 row += col / width2;
1886 col = col % width2;
1887 }
1888 if (row >= curwin->w_height)
1889 {
1890 if (curwin->w_skipcol == 0)
1891 {
1892 curwin->w_skipcol += width1;
1893 --row;
1894 }
1895 if (row >= curwin->w_height)
1896 curwin->w_skipcol += (row - curwin->w_height) * width2;
1897 redraw_later(UPD_NOT_VALID);
1898 }
1899}
1900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#ifdef FEAT_DIFF
1902/*
1903 * Don't end up with too many filler lines in the window.
1904 */
1905 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001906check_topfill(
1907 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001908 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 int n;
1911
1912 if (wp->w_topfill > 0)
1913 {
1914 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1915 if (wp->w_topfill + n > wp->w_height)
1916 {
1917 if (down && wp->w_topline > 1)
1918 {
1919 --wp->w_topline;
1920 wp->w_topfill = 0;
1921 }
1922 else
1923 {
1924 wp->w_topfill = wp->w_height - n;
1925 if (wp->w_topfill < 0)
1926 wp->w_topfill = 0;
1927 }
1928 }
1929 }
1930}
1931
1932/*
1933 * Use as many filler lines as possible for w_topline. Make sure w_topline
1934 * is still visible.
1935 */
1936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001937max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938{
1939 int n;
1940
1941 n = plines_nofill(curwin->w_topline);
1942 if (n >= curwin->w_height)
1943 curwin->w_topfill = 0;
1944 else
1945 {
1946 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1947 if (curwin->w_topfill + n > curwin->w_height)
1948 curwin->w_topfill = curwin->w_height - n;
1949 }
1950}
1951#endif
1952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953/*
1954 * Scroll the screen one line down, but don't do it if it would move the
1955 * cursor off the screen.
1956 */
1957 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001958scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
1960 int end_row;
1961#ifdef FEAT_DIFF
1962 int can_fill = (curwin->w_topfill
1963 < diff_check_fill(curwin, curwin->w_topline));
1964#endif
1965
1966 if (curwin->w_topline <= 1
1967#ifdef FEAT_DIFF
1968 && !can_fill
1969#endif
1970 )
1971 return;
1972
Bram Moolenaar85a20022019-12-21 18:25:54 +01001973 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975 /*
1976 * Compute the row number of the last row of the cursor line
1977 * and make sure it doesn't go off the screen. Make sure the cursor
1978 * doesn't go past 'scrolloff' lines from the screen end.
1979 */
1980 end_row = curwin->w_wrow;
1981#ifdef FEAT_DIFF
1982 if (can_fill)
1983 ++end_row;
1984 else
1985 end_row += plines_nofill(curwin->w_topline - 1);
1986#else
1987 end_row += plines(curwin->w_topline - 1);
1988#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001989 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
1991 validate_cheight();
1992 validate_virtcol();
1993 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001994 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001996 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998#ifdef FEAT_DIFF
1999 if (can_fill)
2000 {
2001 ++curwin->w_topfill;
2002 check_topfill(curwin, TRUE);
2003 }
2004 else
2005 {
2006 --curwin->w_topline;
2007 curwin->w_topfill = 0;
2008 }
2009#else
2010 --curwin->w_topline;
2011#endif
2012#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002013 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002015 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2017 }
2018}
2019
2020/*
2021 * Scroll the screen one line up, but don't do it if it would move the cursor
2022 * off the screen.
2023 */
2024 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002025scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
2027 int start_row;
2028
2029 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2030#ifdef FEAT_DIFF
2031 && curwin->w_topfill == 0
2032#endif
2033 )
2034 return;
2035
Bram Moolenaar85a20022019-12-21 18:25:54 +01002036 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038 /*
2039 * Compute the row number of the first row of the cursor line
2040 * and make sure it doesn't go off the screen. Make sure the cursor
2041 * doesn't go before 'scrolloff' lines from the screen start.
2042 */
2043#ifdef FEAT_DIFF
2044 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2045 - curwin->w_topfill;
2046#else
2047 start_row = curwin->w_wrow - plines(curwin->w_topline);
2048#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002049 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
2051 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002052 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002054 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056#ifdef FEAT_DIFF
2057 if (curwin->w_topfill > 0)
2058 --curwin->w_topfill;
2059 else
2060#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002061 {
2062#ifdef FEAT_FOLDING
2063 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002066 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002067 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2069 }
2070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072/*
2073 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2074 * a (wrapped) text line. Uses and sets "lp->fill".
2075 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002076 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 */
2078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002079topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081#ifdef FEAT_DIFF
2082 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2083 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002084 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 ++lp->fill;
2086 lp->height = 1;
2087 }
2088 else
2089#endif
2090 {
2091 --lp->lnum;
2092#ifdef FEAT_DIFF
2093 lp->fill = 0;
2094#endif
2095 if (lp->lnum < 1)
2096 lp->height = MAXCOL;
2097 else
2098#ifdef FEAT_FOLDING
2099 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002100 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 lp->height = 1;
2102 else
2103#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002104 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106}
2107
2108/*
2109 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2110 * a (wrapped) text line. Uses and sets "lp->fill".
2111 * Returns the height of the added line in "lp->height".
2112 * Lines below the last one are incredibly high.
2113 */
2114 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002115botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117#ifdef FEAT_DIFF
2118 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2119 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002120 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 ++lp->fill;
2122 lp->height = 1;
2123 }
2124 else
2125#endif
2126 {
2127 ++lp->lnum;
2128#ifdef FEAT_DIFF
2129 lp->fill = 0;
2130#endif
2131 if (lp->lnum > curbuf->b_ml.ml_line_count)
2132 lp->height = MAXCOL;
2133 else
2134#ifdef FEAT_FOLDING
2135 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 lp->height = 1;
2138 else
2139#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002140 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142}
2143
2144#ifdef FEAT_DIFF
2145/*
2146 * Switch from including filler lines below lp->lnum to including filler
2147 * lines above loff.lnum + 1. This keeps pointing to the same line.
2148 * When there are no filler lines nothing changes.
2149 */
2150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002151botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 if (lp->fill > 0)
2154 {
2155 ++lp->lnum;
2156 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2157 }
2158}
2159
2160/*
2161 * Switch from including filler lines above lp->lnum to including filler
2162 * lines below loff.lnum - 1. This keeps pointing to the same line.
2163 * When there are no filler lines nothing changes.
2164 */
2165 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002166topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 if (lp->fill > 0)
2169 {
2170 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2171 --lp->lnum;
2172 }
2173}
2174#endif
2175
2176/*
2177 * Recompute topline to put the cursor at the top of the window.
2178 * Scroll at least "min_scroll" lines.
2179 * If "always" is TRUE, always set topline (for "zt").
2180 */
2181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002182scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
2184 int scrolled = 0;
2185 int extra = 0;
2186 int used;
2187 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002188 linenr_T top; // just above displayed lines
2189 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002191 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#ifdef FEAT_DIFF
2193 linenr_T old_topfill = curwin->w_topfill;
2194#endif
2195 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002196 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (mouse_dragging > 0)
2199 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201 /*
2202 * Decrease topline until:
2203 * - it has become 1
2204 * - (part of) the cursor line is moved off the screen or
2205 * - moved at least 'scrolljump' lines and
2206 * - at least 'scrolloff' lines above and below the cursor
2207 */
2208 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002209 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (curwin->w_cursor.lnum < curwin->w_topline)
2211 scrolled = used;
2212
2213#ifdef FEAT_FOLDING
2214 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2215 {
2216 --top;
2217 ++bot;
2218 }
2219 else
2220#endif
2221 {
2222 top = curwin->w_cursor.lnum - 1;
2223 bot = curwin->w_cursor.lnum + 1;
2224 }
2225 new_topline = top + 1;
2226
2227#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 // "used" already contains the number of filler lines above, don't add it
2229 // again.
2230 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002231 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#endif
2233
2234 /*
2235 * Check if the lines from "top" to "bot" fit in the window. If they do,
2236 * set new_topline and advance "top" and "bot" to include more lines.
2237 */
2238 while (top > 0)
2239 {
2240#ifdef FEAT_FOLDING
2241 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 i = 1;
2244 else
2245#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002246 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 used += i;
2248 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2249 {
2250#ifdef FEAT_FOLDING
2251 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002252 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 ++used;
2254 else
2255#endif
2256 used += plines(bot);
2257 }
2258 if (used > curwin->w_height)
2259 break;
2260 if (top < curwin->w_topline)
2261 scrolled += i;
2262
2263 /*
2264 * If scrolling is needed, scroll at least 'sj' lines.
2265 */
2266 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2267 && extra >= off)
2268 break;
2269
2270 extra += i;
2271 new_topline = top;
2272 --top;
2273 ++bot;
2274 }
2275
2276 /*
2277 * If we don't have enough space, put cursor in the middle.
2278 * This makes sure we get the same position when using "k" and "j"
2279 * in a small window.
2280 */
2281 if (used > curwin->w_height)
2282 scroll_cursor_halfway(FALSE);
2283 else
2284 {
2285 /*
2286 * If "always" is FALSE, only adjust topline to a lower value, higher
2287 * value may happen with wrapping lines
2288 */
2289 if (new_topline < curwin->w_topline || always)
2290 curwin->w_topline = new_topline;
2291 if (curwin->w_topline > curwin->w_cursor.lnum)
2292 curwin->w_topline = curwin->w_cursor.lnum;
2293#ifdef FEAT_DIFF
2294 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2295 if (curwin->w_topfill > 0 && extra > off)
2296 {
2297 curwin->w_topfill -= extra - off;
2298 if (curwin->w_topfill < 0)
2299 curwin->w_topfill = 0;
2300 }
2301 check_topfill(curwin, FALSE);
2302#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002303 // TODO: if the line doesn't fit may optimize w_skipcol
2304 if (curwin->w_topline == curwin->w_cursor.lnum)
2305 {
2306 curwin->w_skipcol = 0;
2307 redraw_later(UPD_NOT_VALID);
2308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002310 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#ifdef FEAT_DIFF
2312 || curwin->w_topfill != old_topfill
2313#endif
2314 )
2315 curwin->w_valid &=
2316 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2317 curwin->w_valid |= VALID_TOPLINE;
2318 }
2319}
2320
2321/*
2322 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2323 * screen lines for text lines.
2324 */
2325 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002326set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328#ifdef FEAT_DIFF
2329 wp->w_filler_rows = 0;
2330#endif
2331 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002332 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 else
2334 {
2335 wp->w_empty_rows = wp->w_height - used;
2336#ifdef FEAT_DIFF
2337 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2338 {
2339 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2340 if (wp->w_empty_rows > wp->w_filler_rows)
2341 wp->w_empty_rows -= wp->w_filler_rows;
2342 else
2343 {
2344 wp->w_filler_rows = wp->w_empty_rows;
2345 wp->w_empty_rows = 0;
2346 }
2347 }
2348#endif
2349 }
2350}
2351
2352/*
2353 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002354 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2356 * This is messy stuff!!!
2357 */
2358 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002359scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361 int used;
2362 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002363 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 int extra = 0;
2365 int i;
2366 linenr_T line_count;
2367 linenr_T old_topline = curwin->w_topline;
2368 lineoff_T loff;
2369 lineoff_T boff;
2370#ifdef FEAT_DIFF
2371 int old_topfill = curwin->w_topfill;
2372 int fill_below_window;
2373#endif
2374 linenr_T old_botline = curwin->w_botline;
2375 linenr_T old_valid = curwin->w_valid;
2376 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002377 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002378 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 cln = curwin->w_cursor.lnum;
2381 if (set_topbot)
2382 {
2383 used = 0;
2384 curwin->w_botline = cln + 1;
2385#ifdef FEAT_DIFF
2386 loff.fill = 0;
2387#endif
2388 for (curwin->w_topline = curwin->w_botline;
2389 curwin->w_topline > 1;
2390 curwin->w_topline = loff.lnum)
2391 {
2392 loff.lnum = curwin->w_topline;
2393 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002394 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 break;
2396 used += loff.height;
2397#ifdef FEAT_DIFF
2398 curwin->w_topfill = loff.fill;
2399#endif
2400 }
2401 set_empty_rows(curwin, used);
2402 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2403 if (curwin->w_topline != old_topline
2404#ifdef FEAT_DIFF
2405 || curwin->w_topfill != old_topfill
2406#endif
2407 )
2408 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2409 }
2410 else
2411 validate_botline();
2412
Bram Moolenaar85a20022019-12-21 18:25:54 +01002413 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef FEAT_DIFF
2415 used = plines_nofill(cln);
2416#else
2417 validate_cheight();
2418 used = curwin->w_cline_height;
2419#endif
2420
Bram Moolenaar85a20022019-12-21 18:25:54 +01002421 // If the cursor is below botline, we will at least scroll by the height
2422 // of the cursor line. Correct for empty lines, which are really part of
2423 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (cln >= curwin->w_botline)
2425 {
2426 scrolled = used;
2427 if (cln == curwin->w_botline)
2428 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002429 min_scrolled = scrolled;
2430 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2431 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
Bram Moolenaar4d31b482022-10-06 16:03:09 +01002432 min_scrolled += PLINES_NOFILL(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 }
2434
2435 /*
2436 * Stop counting lines to scroll when
2437 * - hitting start of the file
2438 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002439 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 * - lines between botline and cursor have been counted
2441 */
2442#ifdef FEAT_FOLDING
2443 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2444#endif
2445 {
2446 loff.lnum = cln;
2447 boff.lnum = cln;
2448 }
2449#ifdef FEAT_DIFF
2450 loff.fill = 0;
2451 boff.fill = 0;
2452 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2453 - curwin->w_filler_rows;
2454#endif
2455
2456 while (loff.lnum > 1)
2457 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002458 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2459 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002461 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2463 && loff.lnum <= curwin->w_botline
2464#ifdef FEAT_DIFF
2465 && (loff.lnum < curwin->w_botline
2466 || loff.fill >= fill_below_window)
2467#endif
2468 )
2469 break;
2470
Bram Moolenaar85a20022019-12-21 18:25:54 +01002471 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002473 if (loff.height == MAXCOL)
2474 used = MAXCOL;
2475 else
2476 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (used > curwin->w_height)
2478 break;
2479 if (loff.lnum >= curwin->w_botline
2480#ifdef FEAT_DIFF
2481 && (loff.lnum > curwin->w_botline
2482 || loff.fill <= fill_below_window)
2483#endif
2484 )
2485 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002486 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 scrolled += loff.height;
2488 if (loff.lnum == curwin->w_botline
2489#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002490 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491#endif
2492 )
2493 scrolled -= curwin->w_empty_rows;
2494 }
2495
2496 if (boff.lnum < curbuf->b_ml.ml_line_count)
2497 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002498 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 botline_forw(&boff);
2500 used += boff.height;
2501 if (used > curwin->w_height)
2502 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002503 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2504 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
2506 extra += boff.height;
2507 if (boff.lnum >= curwin->w_botline
2508#ifdef FEAT_DIFF
2509 || (boff.lnum + 1 == curwin->w_botline
2510 && boff.fill > curwin->w_filler_rows)
2511#endif
2512 )
2513 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002514 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 scrolled += boff.height;
2516 if (boff.lnum == curwin->w_botline
2517#ifdef FEAT_DIFF
2518 && boff.fill == 0
2519#endif
2520 )
2521 scrolled -= curwin->w_empty_rows;
2522 }
2523 }
2524 }
2525 }
2526
Bram Moolenaar85a20022019-12-21 18:25:54 +01002527 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (scrolled <= 0)
2529 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002530 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 else if (used > curwin->w_height)
2532 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002533 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 else
2535 {
2536 line_count = 0;
2537#ifdef FEAT_DIFF
2538 boff.fill = curwin->w_topfill;
2539#endif
2540 boff.lnum = curwin->w_topline - 1;
2541 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2542 {
2543 botline_forw(&boff);
2544 i += boff.height;
2545 ++line_count;
2546 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002547 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 line_count = 9999;
2549 }
2550
2551 /*
2552 * Scroll up if the cursor is off the bottom of the screen a bit.
2553 * Otherwise put it at 1/2 of the screen.
2554 */
2555 if (line_count >= curwin->w_height && line_count > min_scroll)
2556 scroll_cursor_halfway(FALSE);
2557 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002558 {
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002559 // With 'smoothscroll' scroll at least the height of the cursor line,
2560 // unless it would move the cursor.
2561 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled
2562 && (curwin->w_cursor.lnum < curwin->w_topline
2563 || (curwin->w_virtcol - curwin->w_skipcol >=
2564 curwin->w_width - curwin_col_off())))
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002565 line_count = min_scrolled;
Bram Moolenaard5337ef2022-10-20 20:15:47 +01002566 if (line_count > 0)
2567 {
2568 if (scrolling_screenlines(TRUE))
2569 scrollup(scrolled, TRUE); // TODO
2570 else
2571 scrollup(line_count, TRUE);
2572 }
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 /*
2576 * If topline didn't change we need to restore w_botline and w_empty_rows
2577 * (we changed them).
2578 * If topline did change, update_screen() will set botline.
2579 */
2580 if (curwin->w_topline == old_topline && set_topbot)
2581 {
2582 curwin->w_botline = old_botline;
2583 curwin->w_empty_rows = old_empty_rows;
2584 curwin->w_valid = old_valid;
2585 }
2586 curwin->w_valid |= VALID_TOPLINE;
2587}
2588
2589/*
2590 * Recompute topline to put the cursor halfway the window
2591 * If "atend" is TRUE, also put it halfway at the end of the file.
2592 */
2593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002594scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 int above = 0;
2597 linenr_T topline;
2598#ifdef FEAT_DIFF
2599 int topfill = 0;
2600#endif
2601 int below = 0;
2602 int used;
2603 lineoff_T loff;
2604 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002605#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002606 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002609#ifdef FEAT_PROP_POPUP
2610 // if the width changed this needs to be updated first
2611 may_update_popup_position();
2612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2614#ifdef FEAT_FOLDING
2615 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2616#endif
2617#ifdef FEAT_DIFF
2618 used = plines_nofill(loff.lnum);
2619 loff.fill = 0;
2620 boff.fill = 0;
2621#else
2622 used = plines(loff.lnum);
2623#endif
2624 topline = loff.lnum;
2625 while (topline > 1)
2626 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002627 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
2629 if (boff.lnum < curbuf->b_ml.ml_line_count)
2630 {
2631 botline_forw(&boff);
2632 used += boff.height;
2633 if (used > curwin->w_height)
2634 break;
2635 below += boff.height;
2636 }
2637 else
2638 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002639 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (atend)
2641 ++used;
2642 }
2643 }
2644
Bram Moolenaar85a20022019-12-21 18:25:54 +01002645 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002648 if (loff.height == MAXCOL)
2649 used = MAXCOL;
2650 else
2651 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (used > curwin->w_height)
2653 break;
2654 above += loff.height;
2655 topline = loff.lnum;
2656#ifdef FEAT_DIFF
2657 topfill = loff.fill;
2658#endif
2659 }
2660 }
2661#ifdef FEAT_FOLDING
2662 if (!hasFolding(topline, &curwin->w_topline, NULL))
2663#endif
2664 curwin->w_topline = topline;
2665#ifdef FEAT_DIFF
2666 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002667 if (old_topline > curwin->w_topline + curwin->w_height)
2668 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 check_topfill(curwin, FALSE);
2670#endif
2671 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2672 curwin->w_valid |= VALID_TOPLINE;
2673}
2674
2675/*
2676 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002677 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 * If not possible, put it at the same position as scroll_cursor_halfway().
2679 * When called topline must be valid!
2680 */
2681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002682cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002684 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002686 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 linenr_T botline;
2688 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002689 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002691 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 /*
2694 * How many lines we would like to have above/below the cursor depends on
2695 * whether the first/last line of the file is on screen.
2696 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002697 above_wanted = so;
2698 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002699 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 above_wanted = mouse_dragging - 1;
2702 below_wanted = mouse_dragging - 1;
2703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (curwin->w_topline == 1)
2705 {
2706 above_wanted = 0;
2707 max_off = curwin->w_height / 2;
2708 if (below_wanted > max_off)
2709 below_wanted = max_off;
2710 }
2711 validate_botline();
2712 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002713 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
2715 below_wanted = 0;
2716 max_off = (curwin->w_height - 1) / 2;
2717 if (above_wanted > max_off)
2718 above_wanted = max_off;
2719 }
2720
2721 /*
2722 * If there are sufficient file-lines above and below the cursor, we can
2723 * return now.
2724 */
2725 cln = curwin->w_cursor.lnum;
2726 if (cln >= curwin->w_topline + above_wanted
2727 && cln < curwin->w_botline - below_wanted
2728#ifdef FEAT_FOLDING
2729 && !hasAnyFolding(curwin)
2730#endif
2731 )
2732 return;
2733
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002734 if (curwin->w_p_sms && !curwin->w_p_wrap)
2735 {
2736 // 'smoothscroll is active
2737 if (curwin->w_cline_height == curwin->w_height)
2738 {
2739 // The cursor line just fits in the window, don't scroll.
2740 curwin->w_skipcol = 0;
2741 return;
2742 }
2743 // TODO: If the cursor line doesn't fit in the window then only adjust
2744 // w_skipcol.
2745 }
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 /*
2748 * Narrow down the area where the cursor can be put by taking lines from
2749 * the top and the bottom until:
2750 * - the desired context lines are found
2751 * - the lines from the top is past the lines from the bottom
2752 */
2753 topline = curwin->w_topline;
2754 botline = curwin->w_botline - 1;
2755#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002756 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 above = curwin->w_topfill;
2758 below = curwin->w_filler_rows;
2759#endif
2760 while ((above < above_wanted || below < below_wanted) && topline < botline)
2761 {
2762 if (below < below_wanted && (below <= above || above >= above_wanted))
2763 {
2764#ifdef FEAT_FOLDING
2765 if (hasFolding(botline, &botline, NULL))
2766 ++below;
2767 else
2768#endif
2769 below += plines(botline);
2770 --botline;
2771 }
2772 if (above < above_wanted && (above < below || below >= below_wanted))
2773 {
2774#ifdef FEAT_FOLDING
2775 if (hasFolding(topline, NULL, &topline))
2776 ++above;
2777 else
2778#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002779 above += PLINES_NOFILL(topline);
2780#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002781 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (topline < botline)
2783 above += diff_check_fill(curwin, topline + 1);
2784#endif
2785 ++topline;
2786 }
2787 }
2788 if (topline == botline || botline == 0)
2789 curwin->w_cursor.lnum = topline;
2790 else if (topline > botline)
2791 curwin->w_cursor.lnum = botline;
2792 else
2793 {
2794 if (cln < topline && curwin->w_topline > 1)
2795 {
2796 curwin->w_cursor.lnum = topline;
2797 curwin->w_valid &=
2798 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2799 }
2800 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2801 {
2802 curwin->w_cursor.lnum = botline;
2803 curwin->w_valid &=
2804 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2805 }
2806 }
2807 curwin->w_valid |= VALID_TOPLINE;
2808}
2809
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002810static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
2812/*
2813 * move screen 'count' pages up or down and update screen
2814 *
2815 * return FAIL for failure, OK otherwise
2816 */
2817 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002818onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 long n;
2821 int retval = OK;
2822 lineoff_T loff;
2823 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002824 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
Bram Moolenaar85a20022019-12-21 18:25:54 +01002826 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
2828 beep_flush();
2829 return FAIL;
2830 }
2831
2832 for ( ; count > 0; --count)
2833 {
2834 validate_botline();
2835 /*
2836 * It's an error to move a page up when the first line is already on
2837 * the screen. It's an error to move a page down when the last line
2838 * is on the screen and the topline is 'scrolloff' lines from the
2839 * last line.
2840 */
2841 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002842 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2844 : (curwin->w_topline == 1
2845#ifdef FEAT_DIFF
2846 && curwin->w_topfill ==
2847 diff_check_fill(curwin, curwin->w_topline)
2848#endif
2849 ))
2850 {
2851 beep_flush();
2852 retval = FAIL;
2853 break;
2854 }
2855
2856#ifdef FEAT_DIFF
2857 loff.fill = 0;
2858#endif
2859 if (dir == FORWARD)
2860 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002861 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002863 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002864 if (p_window <= 2)
2865 ++curwin->w_topline;
2866 else
2867 curwin->w_topline += p_window - 2;
2868 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2869 curwin->w_topline = curbuf->b_ml.ml_line_count;
2870 curwin->w_cursor.lnum = curwin->w_topline;
2871 }
2872 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2873 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002874 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 curwin->w_topline = curbuf->b_ml.ml_line_count;
2876#ifdef FEAT_DIFF
2877 curwin->w_topfill = 0;
2878#endif
2879 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2880 }
2881 else
2882 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002883 // For the overlap, start with the line just below the window
2884 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 loff.lnum = curwin->w_botline;
2886#ifdef FEAT_DIFF
2887 loff.fill = diff_check_fill(curwin, loff.lnum)
2888 - curwin->w_filler_rows;
2889#endif
2890 get_scroll_overlap(&loff, -1);
2891 curwin->w_topline = loff.lnum;
2892#ifdef FEAT_DIFF
2893 curwin->w_topfill = loff.fill;
2894 check_topfill(curwin, FALSE);
2895#endif
2896 curwin->w_cursor.lnum = curwin->w_topline;
2897 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2898 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2899 }
2900 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002901 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903#ifdef FEAT_DIFF
2904 if (curwin->w_topline == 1)
2905 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002906 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 max_topfill();
2908 continue;
2909 }
2910#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002911 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002912 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002913 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002914 if (p_window <= 2)
2915 --curwin->w_topline;
2916 else
2917 curwin->w_topline -= p_window - 2;
2918 if (curwin->w_topline < 1)
2919 curwin->w_topline = 1;
2920 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2921 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2922 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2923 continue;
2924 }
2925
Bram Moolenaar85a20022019-12-21 18:25:54 +01002926 // Find the line at the top of the window that is going to be the
2927 // line at the bottom of the window. Make sure this results in
2928 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 loff.lnum = curwin->w_topline - 1;
2930#ifdef FEAT_DIFF
2931 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2932 - curwin->w_topfill;
2933#endif
2934 get_scroll_overlap(&loff, 1);
2935
2936 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2937 {
2938 loff.lnum = curbuf->b_ml.ml_line_count;
2939#ifdef FEAT_DIFF
2940 loff.fill = 0;
2941 }
2942 else
2943 {
2944 botline_topline(&loff);
2945#endif
2946 }
2947 curwin->w_cursor.lnum = loff.lnum;
2948
Bram Moolenaar85a20022019-12-21 18:25:54 +01002949 // Find the line just above the new topline to get the right line
2950 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 n = 0;
2952 while (n <= curwin->w_height && loff.lnum >= 1)
2953 {
2954 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002955 if (loff.height == MAXCOL)
2956 n = MAXCOL;
2957 else
2958 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002960 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
2962 curwin->w_topline = 1;
2963#ifdef FEAT_DIFF
2964 max_topfill();
2965#endif
2966 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2967 }
2968 else
2969 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002970 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#ifdef FEAT_DIFF
2972 topline_botline(&loff);
2973#endif
2974 botline_forw(&loff);
2975 botline_forw(&loff);
2976#ifdef FEAT_DIFF
2977 botline_topline(&loff);
2978#endif
2979#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002980 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2982#endif
2983
Bram Moolenaar85a20022019-12-21 18:25:54 +01002984 // Always scroll at least one line. Avoid getting stuck on
2985 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 if (loff.lnum >= curwin->w_topline
2987#ifdef FEAT_DIFF
2988 && (loff.lnum > curwin->w_topline
2989 || loff.fill >= curwin->w_topfill)
2990#endif
2991 )
2992 {
2993#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002994 // First try using the maximum number of filler lines. If
2995 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 loff.fill = curwin->w_topfill;
2997 if (curwin->w_topfill < diff_check_fill(curwin,
2998 curwin->w_topline))
2999 max_topfill();
3000 if (curwin->w_topfill == loff.fill)
3001#endif
3002 {
3003 --curwin->w_topline;
3004#ifdef FEAT_DIFF
3005 curwin->w_topfill = 0;
3006#endif
3007 }
3008 comp_botline(curwin);
3009 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01003010 curwin->w_valid &=
3011 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013 else
3014 {
3015 curwin->w_topline = loff.lnum;
3016#ifdef FEAT_DIFF
3017 curwin->w_topfill = loff.fill;
3018 check_topfill(curwin, FALSE);
3019#endif
3020 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
3021 }
3022 }
3023 }
3024 }
3025#ifdef FEAT_FOLDING
3026 foldAdjustCursor();
3027#endif
3028 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003029 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003030 if (retval == OK)
3031 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3033
Bram Moolenaar907dad72018-07-10 15:07:15 +02003034 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003036 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3037 // But make sure we scroll at least one line (happens with mix of long
3038 // wrapping lines and non-wrapping line).
3039 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003041 scroll_cursor_top(1, FALSE);
3042 if (curwin->w_topline <= old_topline
3043 && old_topline < curbuf->b_ml.ml_line_count)
3044 {
3045 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003047 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3048#endif
3049 }
3050 }
3051#ifdef FEAT_FOLDING
3052 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003057 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 return retval;
3059}
3060
3061/*
3062 * Decide how much overlap to use for page-up or page-down scrolling.
3063 * This is symmetric, so that doing both keeps the same lines displayed.
3064 * Three lines are examined:
3065 *
3066 * before CTRL-F after CTRL-F / before CTRL-B
3067 * etc. l1
3068 * l1 last but one line ------------
3069 * l2 last text line l2 top text line
3070 * ------------- l3 second text line
3071 * l3 etc.
3072 */
3073 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003074get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
3076 int h1, h2, h3, h4;
3077 int min_height = curwin->w_height - 2;
3078 lineoff_T loff0, loff1, loff2;
3079
3080#ifdef FEAT_DIFF
3081 if (lp->fill > 0)
3082 lp->height = 1;
3083 else
3084 lp->height = plines_nofill(lp->lnum);
3085#else
3086 lp->height = plines(lp->lnum);
3087#endif
3088 h1 = lp->height;
3089 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003090 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 loff0 = *lp;
3093 if (dir > 0)
3094 botline_forw(lp);
3095 else
3096 topline_back(lp);
3097 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003098 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003100 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 return;
3102 }
3103
3104 loff1 = *lp;
3105 if (dir > 0)
3106 botline_forw(lp);
3107 else
3108 topline_back(lp);
3109 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003110 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003112 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 return;
3114 }
3115
3116 loff2 = *lp;
3117 if (dir > 0)
3118 botline_forw(lp);
3119 else
3120 topline_back(lp);
3121 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003122 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003123 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003125 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126}
3127
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128/*
3129 * Scroll 'scroll' lines up or down.
3130 */
3131 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003132halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 long scrolled = 0;
3135 int i;
3136 int n;
3137 int room;
3138
3139 if (Prenum)
3140 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3141 curwin->w_height : Prenum;
3142 n = (curwin->w_p_scr <= curwin->w_height) ?
3143 curwin->w_p_scr : curwin->w_height;
3144
Bram Moolenaard5d37532017-03-27 23:02:07 +02003145 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 validate_botline();
3147 room = curwin->w_empty_rows;
3148#ifdef FEAT_DIFF
3149 room += curwin->w_filler_rows;
3150#endif
3151 if (flag)
3152 {
3153 /*
3154 * scroll the text up
3155 */
3156 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3157 {
3158#ifdef FEAT_DIFF
3159 if (curwin->w_topfill > 0)
3160 {
3161 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003162 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 --curwin->w_topfill;
3164 }
3165 else
3166#endif
3167 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003168 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 n -= i;
3170 if (n < 0 && scrolled > 0)
3171 break;
3172#ifdef FEAT_FOLDING
3173 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3174#endif
3175 ++curwin->w_topline;
3176#ifdef FEAT_DIFF
3177 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3178#endif
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3181 {
3182 ++curwin->w_cursor.lnum;
3183 curwin->w_valid &=
3184 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3188 scrolled += i;
3189
3190 /*
3191 * Correct w_botline for changed w_topline.
3192 * Won't work when there are filler lines.
3193 */
3194#ifdef FEAT_DIFF
3195 if (curwin->w_p_diff)
3196 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3197 else
3198#endif
3199 {
3200 room += i;
3201 do
3202 {
3203 i = plines(curwin->w_botline);
3204 if (i > room)
3205 break;
3206#ifdef FEAT_FOLDING
3207 (void)hasFolding(curwin->w_botline, NULL,
3208 &curwin->w_botline);
3209#endif
3210 ++curwin->w_botline;
3211 room -= i;
3212 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3213 }
3214 }
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 /*
3217 * When hit bottom of the file: move cursor down.
3218 */
3219 if (n > 0)
3220 {
3221# ifdef FEAT_FOLDING
3222 if (hasAnyFolding(curwin))
3223 {
3224 while (--n >= 0
3225 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3226 {
3227 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3228 &curwin->w_cursor.lnum);
3229 ++curwin->w_cursor.lnum;
3230 }
3231 }
3232 else
3233# endif
3234 curwin->w_cursor.lnum += n;
3235 check_cursor_lnum();
3236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
3238 else
3239 {
3240 /*
3241 * scroll the text down
3242 */
3243 while (n > 0 && curwin->w_topline > 1)
3244 {
3245#ifdef FEAT_DIFF
3246 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3247 {
3248 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003249 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 ++curwin->w_topfill;
3251 }
3252 else
3253#endif
3254 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003255 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 n -= i;
3257 if (n < 0 && scrolled > 0)
3258 break;
3259 --curwin->w_topline;
3260#ifdef FEAT_FOLDING
3261 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3262#endif
3263#ifdef FEAT_DIFF
3264 curwin->w_topfill = 0;
3265#endif
3266 }
3267 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3268 VALID_BOTLINE|VALID_BOTLINE_AP);
3269 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 if (curwin->w_cursor.lnum > 1)
3271 {
3272 --curwin->w_cursor.lnum;
3273 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003276
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 /*
3278 * When hit top of the file: move cursor up.
3279 */
3280 if (n > 0)
3281 {
3282 if (curwin->w_cursor.lnum <= (linenr_T)n)
3283 curwin->w_cursor.lnum = 1;
3284 else
3285# ifdef FEAT_FOLDING
3286 if (hasAnyFolding(curwin))
3287 {
3288 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3289 {
3290 --curwin->w_cursor.lnum;
3291 (void)hasFolding(curwin->w_cursor.lnum,
3292 &curwin->w_cursor.lnum, NULL);
3293 }
3294 }
3295 else
3296# endif
3297 curwin->w_cursor.lnum -= n;
3298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003301 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 foldAdjustCursor();
3303# endif
3304#ifdef FEAT_DIFF
3305 check_topfill(curwin, !flag);
3306#endif
3307 cursor_correct();
3308 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003309 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003311
Bram Moolenaar860cae12010-06-05 23:22:07 +02003312 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003313do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003314{
3315 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003316 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003317 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003318 colnr_T curswant = curwin->w_curswant;
3319 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003320 win_T *old_curwin = curwin;
3321 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003322 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003323 int old_VIsual_select = VIsual_select;
3324 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003325
3326 /*
3327 * loop through the cursorbound windows
3328 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003329 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003330 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003331 {
3332 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003333 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003334 if (curwin != old_curwin && curwin->w_p_crb)
3335 {
3336# ifdef FEAT_DIFF
3337 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003338 curwin->w_cursor.lnum =
3339 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003340 else
3341# endif
3342 curwin->w_cursor.lnum = line;
3343 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003344 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003345 curwin->w_curswant = curswant;
3346 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003347
Bram Moolenaar85a20022019-12-21 18:25:54 +01003348 // Make sure the cursor is in a valid position. Temporarily set
3349 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003350 restart_edit_save = restart_edit;
3351 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003352 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003353
3354 // Avoid a scroll here for the cursor position, 'scrollbind' is
3355 // more important.
3356 if (!curwin->w_p_scb)
3357 validate_cursor();
3358
Bram Moolenaar61452852011-02-01 18:01:11 +01003359 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003360 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003361 if (has_mbyte)
3362 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003363 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003364
Bram Moolenaar85a20022019-12-21 18:25:54 +01003365 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003366 if (!curwin->w_p_scb)
3367 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003368 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003369 }
3370 }
3371
3372 /*
3373 * reset current-window
3374 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003375 VIsual_select = old_VIsual_select;
3376 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003377 curwin = old_curwin;
3378 curbuf = old_curbuf;
3379}