blob: c3f144ef3e696a4326e6de3c269ddaad73d33b33 [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 Moolenaar071d4272004-06-13 20:20:40 +000065 * Compute wp->w_botline for the current wp->w_topline. Can be called after
66 * wp->w_topline changed.
67 */
68 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010069comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070{
71 int n;
72 linenr_T lnum;
73 int done;
74#ifdef FEAT_FOLDING
75 linenr_T last;
76 int folded;
77#endif
78
79 /*
80 * If w_cline_row is valid, start there.
81 * Otherwise have to start at w_topline.
82 */
83 check_cursor_moved(wp);
84 if (wp->w_valid & VALID_CROW)
85 {
86 lnum = wp->w_cursor.lnum;
87 done = wp->w_cline_row;
88 }
89 else
90 {
91 lnum = wp->w_topline;
92 done = 0;
93 }
94
95 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
96 {
97#ifdef FEAT_FOLDING
98 last = lnum;
99 folded = FALSE;
100 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
101 {
102 n = 1;
103 folded = TRUE;
104 }
105 else
106#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#ifdef FEAT_DIFF
109 if (lnum == wp->w_topline)
110 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
111 else
112#endif
113 n = plines_win(wp, lnum, TRUE);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100114 if (lnum == wp->w_topline)
115 n = adjust_plines_for_skipcol(wp, n);
116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 if (
118#ifdef FEAT_FOLDING
119 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
120#else
121 lnum == wp->w_cursor.lnum
122#endif
123 )
124 {
125 wp->w_cline_row = done;
126 wp->w_cline_height = n;
127#ifdef FEAT_FOLDING
128 wp->w_cline_folded = folded;
129#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100130 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
132 }
133 if (done + n > wp->w_height)
134 break;
135 done += n;
136#ifdef FEAT_FOLDING
137 lnum = last;
138#endif
139 }
140
Bram Moolenaar85a20022019-12-21 18:25:54 +0100141 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 wp->w_botline = lnum;
143 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
144
145 set_empty_rows(wp, done);
146}
147
148/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100149 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
150 * set.
151 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100152 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100153redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100154{
155 if ((wp->w_p_rnu
156#ifdef FEAT_SYN_HL
157 || wp->w_p_cul
158#endif
159 )
160 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200161 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200162 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000163 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100164 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200165 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100166}
167
zeertzjq3e559cd2022-03-27 19:26:55 +0100168#ifdef FEAT_SYN_HL
169/*
170 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
171 * contains "screenline".
172 */
173 static void
174redraw_for_cursorcolumn(win_T *wp)
175{
176 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
177 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100178 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100179 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100180 redraw_win_later(wp, UPD_SOME_VALID);
181 // When 'cursorlineopt' contains "screenline" need to redraw with
182 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100183 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100184 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100185 }
186}
187#endif
188
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 * Update curwin->w_topline and redraw if necessary.
191 * Used to update the screen before printing a message.
192 */
193 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100194update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 update_topline();
197 if (must_redraw)
198 update_screen(0);
199}
200
201/*
202 * Update curwin->w_topline to move the cursor onto the screen.
203 */
204 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100205update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206{
207 long line_count;
208 int halfheight;
209 int n;
210 linenr_T old_topline;
211#ifdef FEAT_DIFF
212 int old_topfill;
213#endif
214#ifdef FEAT_FOLDING
215 linenr_T lnum;
216#endif
217 int check_topline = FALSE;
218 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100219 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100220 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100222 // Cursor is updated instead when this is TRUE for 'splitkeep'.
223 if (skip_update_topline)
224 return;
225
Bram Moolenaar85a20022019-12-21 18:25:54 +0100226 // If there is no valid screen and when the window height is zero just use
227 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200228 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200229 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100230 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200231 curwin->w_topline = curwin->w_cursor.lnum;
232 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200233 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200234 return;
235 }
236
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 check_cursor_moved(curwin);
238 if (curwin->w_valid & VALID_TOPLINE)
239 return;
240
Bram Moolenaar85a20022019-12-21 18:25:54 +0100241 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000242 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100243 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245 old_topline = curwin->w_topline;
246#ifdef FEAT_DIFF
247 old_topfill = curwin->w_topfill;
248#endif
249
250 /*
251 * If the buffer is empty, always set topline to 1.
252 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100253 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {
255 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100256 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 curwin->w_botline = 2;
259 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
262
263 /*
264 * If the cursor is above or near the top of the window, scroll the window
265 * to show the line the cursor is in, with 'scrolloff' context.
266 */
267 else
268 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100269 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100271 // If the cursor is above topline, scrolling is always needed.
272 // If the cursor is far below topline and there is no folding,
273 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 if (curwin->w_cursor.lnum < curwin->w_topline)
275 check_topline = TRUE;
276 else if (check_top_offset())
277 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100278 else if (curwin->w_cursor.lnum == curwin->w_topline)
279 {
280 colnr_T vcol;
281
282 // check the cursor position is visible. Add 3 for the ">>>"
283 // displayed in the top-left.
284 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
285 if (curwin->w_skipcol + 3 >= vcol)
286 check_topline = TRUE;
287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
289#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
292 curwin->w_topline))
293 check_topline = TRUE;
294#endif
295
296 if (check_topline)
297 {
298 halfheight = curwin->w_height / 2 - 1;
299 if (halfheight < 2)
300 halfheight = 2;
301
302#ifdef FEAT_FOLDING
303 if (hasAnyFolding(curwin))
304 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100305 // Count the number of logical lines between the cursor and
306 // topline + scrolloff (approximation of how much will be
307 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 n = 0;
309 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 {
312 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100313 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
315 break;
316 (void)hasFolding(lnum, NULL, &lnum);
317 }
318 }
319 else
320#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100321 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaar85a20022019-12-21 18:25:54 +0100323 // If we weren't very close to begin with, we scroll to put the
324 // cursor in the middle of the window. Otherwise put the cursor
325 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 if (n >= halfheight)
327 scroll_cursor_halfway(FALSE);
328 else
329 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000330 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 check_botline = TRUE;
332 }
333 }
334
335 else
336 {
337#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100338 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
340#endif
341 check_botline = TRUE;
342 }
343 }
344
345 /*
346 * If the cursor is below the bottom of the window, scroll the window
347 * to put the cursor on the window.
348 * When w_botline is invalid, recompute it first, to avoid a redraw later.
349 * If w_botline was approximated, we might need a redraw later in a few
350 * cases, but we don't want to spend (a lot of) time recomputing w_botline
351 * for every small change.
352 */
353 if (check_botline)
354 {
355 if (!(curwin->w_valid & VALID_BOTLINE_AP))
356 validate_botline();
357
358 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
359 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000360 if (curwin->w_cursor.lnum < curwin->w_botline)
361 {
362 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100363 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364#ifdef FEAT_FOLDING
365 || hasAnyFolding(curwin)
366#endif
367 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 lineoff_T loff;
370
Bram Moolenaar85a20022019-12-21 18:25:54 +0100371 // Cursor is (a few lines) above botline, check if there are
372 // 'scrolloff' window lines below the cursor. If not, need to
373 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 n = curwin->w_empty_rows;
375 loff.lnum = curwin->w_cursor.lnum;
376#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
379#endif
380#ifdef FEAT_DIFF
381 loff.fill = 0;
382 n += curwin->w_filler_rows;
383#endif
384 loff.height = 0;
385 while (loff.lnum < curwin->w_botline
386#ifdef FEAT_DIFF
387 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
388#endif
389 )
390 {
391 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100392 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 break;
394 botline_forw(&loff);
395 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100396 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100397 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000399 }
400 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100401 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000402 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 }
404 if (check_botline)
405 {
406#ifdef FEAT_FOLDING
407 if (hasAnyFolding(curwin))
408 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // Count the number of logical lines between the cursor and
410 // botline - scrolloff (approximation of how much will be
411 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 line_count = 0;
413 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100414 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 {
416 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100417 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 if (lnum <= 0 || line_count > curwin->w_height + 1)
419 break;
420 (void)hasFolding(lnum, &lnum, NULL);
421 }
422 }
423 else
424#endif
425 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100426 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000428 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 else
430 scroll_cursor_halfway(FALSE);
431 }
432 }
433 }
434 curwin->w_valid |= VALID_TOPLINE;
435
436 /*
437 * Need to redraw when topline changed.
438 */
439 if (curwin->w_topline != old_topline
440#ifdef FEAT_DIFF
441 || curwin->w_topfill != old_topfill
442#endif
443 )
444 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100445 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000446 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {
448 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100449 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 }
451 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100452 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100453 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (curwin->w_cursor.lnum == curwin->w_topline)
455 validate_cursor();
456 }
457
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459}
460
461/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000462 * Return the scrolljump value to use for the current window.
463 * When 'scrolljump' is positive use it as-is.
464 * When 'scrolljump' is negative use it as a percentage of the window height.
465 */
466 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100467scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000468{
469 if (p_sj >= 0)
470 return (int)p_sj;
471 return (curwin->w_height * -p_sj) / 100;
472}
473
474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
476 * current window.
477 */
478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100479check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 lineoff_T loff;
482 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100483 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
Bram Moolenaar375e3392019-01-31 18:26:10 +0100485 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486#ifdef FEAT_FOLDING
487 || hasAnyFolding(curwin)
488#endif
489 )
490 {
491 loff.lnum = curwin->w_cursor.lnum;
492#ifdef FEAT_DIFF
493 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100494 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#else
496 n = 0;
497#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100498 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100499 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100502 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 if (loff.lnum < curwin->w_topline
504#ifdef FEAT_DIFF
505 || (loff.lnum == curwin->w_topline && loff.fill > 0)
506#endif
507 )
508 break;
509 n += loff.height;
510 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100511 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 return TRUE;
513 }
514 return FALSE;
515}
516
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100517/*
518 * Update w_curswant.
519 */
520 void
521update_curswant_force(void)
522{
523 validate_virtcol();
524 curwin->w_curswant = curwin->w_virtcol
525#ifdef FEAT_PROP_POPUP
526 - curwin->w_virtcol_first_char
527#endif
528 ;
529 curwin->w_set_curswant = FALSE;
530}
531
532/*
533 * Update w_curswant if w_set_curswant is set.
534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100539 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
541
542/*
543 * Check if the cursor has moved. Set the w_valid flag accordingly.
544 */
545 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100546check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
549 {
550 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100551 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
552 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 wp->w_valid_cursor = wp->w_cursor;
554 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100555 wp->w_valid_skipcol = wp->w_skipcol;
556 }
557 else if (wp->w_skipcol != wp->w_valid_skipcol)
558 {
559 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
560 |VALID_CHEIGHT|VALID_CROW
561 |VALID_BOTLINE|VALID_BOTLINE_AP);
562 wp->w_valid_cursor = wp->w_cursor;
563 wp->w_valid_leftcol = wp->w_leftcol;
564 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566 else if (wp->w_cursor.col != wp->w_valid_cursor.col
567 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100568 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
570 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
571 wp->w_valid_cursor.col = wp->w_cursor.col;
572 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575}
576
577/*
578 * Call this function when some window settings have changed, which require
579 * the cursor position, botline and topline to be recomputed and the window to
580 * be redrawn. E.g, when changing the 'wrap' option or folding.
581 */
582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100583changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 changed_window_setting_win(curwin);
586}
587
588 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100589changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 wp->w_lines_valid = 0;
592 changed_line_abv_curs_win(wp);
593 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100594 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595}
596
597/*
598 * Set wp->w_topline to a certain number.
599 */
600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100601set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200603#ifdef FEAT_DIFF
604 linenr_T prev_topline = wp->w_topline;
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100608 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
610#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100611 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100613 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
614 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000616 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200618 if (lnum != prev_topline)
619 // Keep the filler lines when the topline didn't change.
620 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#endif
622 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100623 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100624 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625}
626
627/*
628 * Call this function when the length of the cursor line (in screen
629 * characters) has changed, and the change is before the cursor.
630 * Need to take care of w_botline separately!
631 */
632 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100633changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
636 |VALID_CHEIGHT|VALID_TOPLINE);
637}
638
639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
643 |VALID_CHEIGHT|VALID_TOPLINE);
644}
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646/*
647 * Call this function when the length of a line (in screen characters) above
648 * the cursor have changed.
649 * Need to take care of w_botline separately!
650 */
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
655 |VALID_CHEIGHT|VALID_TOPLINE);
656}
657
658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100659changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
662 |VALID_CHEIGHT|VALID_TOPLINE);
663}
664
665/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100666 * Display of line has changed for "buf", invalidate cursor position and
667 * w_botline.
668 */
669 void
670changed_line_display_buf(buf_T *buf)
671{
672 win_T *wp;
673
674 FOR_ALL_WINDOWS(wp)
675 if (wp->w_buffer == buf)
676 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
677 |VALID_CROW|VALID_CHEIGHT
678 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
679}
680
681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 * Make sure the value of curwin->w_botline is valid.
683 */
684 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100685validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100687 validate_botline_win(curwin);
688}
689
690/*
691 * Make sure the value of wp->w_botline is valid.
692 */
693 void
694validate_botline_win(win_T *wp)
695{
696 if (!(wp->w_valid & VALID_BOTLINE))
697 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698}
699
700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 * Mark curwin->w_botline as invalid (because of some change in the buffer).
702 */
703 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100704invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
707}
708
709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100710invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
713}
714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100716approximate_botline_win(
717 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 wp->w_valid &= ~VALID_BOTLINE;
720}
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722/*
723 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
724 */
725 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100726cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 check_cursor_moved(curwin);
729 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
730 (VALID_WROW|VALID_WCOL));
731}
732
733/*
734 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
735 * w_topline must be valid, you may need to call update_topline() first!
736 */
737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100738validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100740 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 check_cursor_moved(curwin);
742 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
743 curs_columns(TRUE);
744}
745
746#if defined(FEAT_GUI) || defined(PROTO)
747/*
748 * validate w_cline_row.
749 */
750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 /*
754 * First make sure that w_topline is valid (after moving the cursor).
755 */
756 update_topline();
757 check_cursor_moved(curwin);
758 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100759 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760}
761#endif
762
763/*
764 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200765 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100768curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
770 linenr_T lnum;
771 int i;
772 int all_invalid;
773 int valid;
774#ifdef FEAT_FOLDING
775 long fold_count;
776#endif
777
Bram Moolenaar85a20022019-12-21 18:25:54 +0100778 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 all_invalid = (!redrawing()
780 || wp->w_lines_valid == 0
781 || wp->w_lines[0].wl_lnum > wp->w_topline);
782 i = 0;
783 wp->w_cline_row = 0;
784 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
785 {
786 valid = FALSE;
787 if (!all_invalid && i < wp->w_lines_valid)
788 {
789 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100790 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (wp->w_lines[i].wl_lnum == lnum)
792 {
793#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100794 // Check for newly inserted lines below this row, in which
795 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (!wp->w_buffer->b_mod_set
797 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
798 || wp->w_buffer->b_mod_top
799 > wp->w_lines[i].wl_lastlnum + 1)
800#endif
801 valid = TRUE;
802 }
803 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100804 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 if (valid
807#ifdef FEAT_DIFF
808 && (lnum != wp->w_topline || !wp->w_p_diff)
809#endif
810 )
811 {
812#ifdef FEAT_FOLDING
813 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100814 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (lnum > wp->w_cursor.lnum)
816 break;
817#else
818 ++lnum;
819#endif
820 wp->w_cline_row += wp->w_lines[i].wl_size;
821 }
822 else
823 {
824#ifdef FEAT_FOLDING
825 fold_count = foldedCount(wp, lnum, NULL);
826 if (fold_count)
827 {
828 lnum += fold_count;
829 if (lnum > wp->w_cursor.lnum)
830 break;
831 ++wp->w_cline_row;
832 }
833 else
834#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100835 {
836 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#ifdef FEAT_DIFF
838 if (lnum == wp->w_topline)
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100839 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 else
841#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100842 n = plines_win(wp, lnum, TRUE);
843 if (lnum++ == wp->w_topline)
844 n = adjust_plines_for_skipcol(wp, n);
845 wp->w_cline_row += n;
846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 }
849
850 check_cursor_moved(wp);
851 if (!(wp->w_valid & VALID_CHEIGHT))
852 {
853 if (all_invalid
854 || i == wp->w_lines_valid
855 || (i < wp->w_lines_valid
856 && (!wp->w_lines[i].wl_valid
857 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
858 {
859#ifdef FEAT_DIFF
860 if (wp->w_cursor.lnum == wp->w_topline)
861 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
862 TRUE) + wp->w_topfill;
863 else
864#endif
865 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
866#ifdef FEAT_FOLDING
867 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
868 NULL, NULL, TRUE, NULL);
869#endif
870 }
871 else if (i > wp->w_lines_valid)
872 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100873 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 wp->w_cline_height = 0;
875#ifdef FEAT_FOLDING
876 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
877 NULL, NULL, TRUE, NULL);
878#endif
879 }
880 else
881 {
882 wp->w_cline_height = wp->w_lines[i].wl_size;
883#ifdef FEAT_FOLDING
884 wp->w_cline_folded = wp->w_lines[i].wl_folded;
885#endif
886 }
887 }
888
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100889 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891}
892
893/*
894 * Validate curwin->w_virtcol only.
895 */
896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100897validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899 validate_virtcol_win(curwin);
900}
901
902/*
903 * Validate wp->w_virtcol only.
904 */
905 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100906validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
908 check_cursor_moved(wp);
909 if (!(wp->w_valid & VALID_VIRTCOL))
910 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100911#ifdef FEAT_PROP_POPUP
912 wp->w_virtcol_first_char = 0;
913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000915#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100916 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000917#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100918 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920}
921
922/*
923 * Validate curwin->w_cline_height only.
924 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 check_cursor_moved(curwin);
929 if (!(curwin->w_valid & VALID_CHEIGHT))
930 {
931#ifdef FEAT_DIFF
932 if (curwin->w_cursor.lnum == curwin->w_topline)
933 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
934 + curwin->w_topfill;
935 else
936#endif
937 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
938#ifdef FEAT_FOLDING
939 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
940#endif
941 curwin->w_valid |= VALID_CHEIGHT;
942 }
943}
944
945/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000946 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 */
948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100949validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 colnr_T off;
952 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100953 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 validate_virtcol();
956 if (!(curwin->w_valid & VALID_WCOL))
957 {
958 col = curwin->w_virtcol;
959 off = curwin_col_off();
960 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200961 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaar85a20022019-12-21 18:25:54 +0100963 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000964 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200965 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100966 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200968 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000969 if (col > (int)curwin->w_leftcol)
970 col -= curwin->w_leftcol;
971 else
972 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100976#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100977 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980}
981
982/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200983 * Compute offset of a window, occupied by absolute or relative line number,
984 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 */
986 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100987win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
Bram Moolenaar64486672010-05-16 15:46:46 +0200989 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_FOLDING
992 + wp->w_p_fdc
993#endif
994#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200995 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#endif
997 );
998}
999
1000 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001001curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 return win_col_off(curwin);
1004}
1005
1006/*
1007 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001008 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1009 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
1011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001012win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
Bram Moolenaar64486672010-05-16 15:46:46 +02001014 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001015 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 return 0;
1017}
1018
1019 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001020curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022 return win_col_off2(curwin);
1023}
1024
1025/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001026 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Also updates curwin->w_wrow and curwin->w_cline_row.
1028 * Also updates curwin->w_leftcol.
1029 */
1030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001031curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001032 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001035 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int off_left, off_right;
1037 int n;
1038 int p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001039 int width1; // text width for first screen line
1040 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 int new_leftcol;
1042 colnr_T startcol;
1043 colnr_T endcol;
1044 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001045 long so = get_scrolloff_value();
1046 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001047 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 /*
1050 * First make sure that w_topline is valid (after moving the cursor).
1051 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001052 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
1054 /*
1055 * Next make sure that w_cline_row is valid.
1056 */
1057 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001058 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001060#ifdef FEAT_PROP_POPUP
1061 // will be set by getvvcol() but not reset
1062 curwin->w_virtcol_first_char = 0;
1063#endif
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 /*
1066 * Compute the number of virtual columns.
1067 */
1068#ifdef FEAT_FOLDING
1069 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001070 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1072 else
1073#endif
1074 getvvcol(curwin, &curwin->w_cursor,
1075 &startcol, &(curwin->w_virtcol), &endcol);
1076
Bram Moolenaar85a20022019-12-21 18:25:54 +01001077 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001079 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
1081 extra = curwin_col_off();
1082 curwin->w_wcol = curwin->w_virtcol + extra;
1083 endcol += extra;
1084
1085 /*
1086 * Now compute w_wrow, counting screen lines from w_cline_row.
1087 */
1088 curwin->w_wrow = curwin->w_cline_row;
1089
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001090 width1 = curwin->w_width - extra;
1091 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001093 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001094 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001095 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001096 if (curwin->w_p_wrap)
1097 curwin->w_wrow = curwin->w_height - 1;
1098 else
1099 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001101 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001103 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001105 // skip columns that are not visible
1106 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001107 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001108 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001109 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001110 // w_skipcol excludes win_col_off(). Include it here, since w_wcol
1111 // counts actual screen columns.
1112 if (curwin->w_skipcol <= width1)
1113 curwin->w_wcol -= curwin->w_width;
1114 else
1115 curwin->w_wcol -= curwin->w_width
1116 * (((curwin->w_skipcol - width1) / width2) + 1);
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001117 did_sub_skipcol = TRUE;
1118 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001119
Bram Moolenaar85a20022019-12-21 18:25:54 +01001120 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001121 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001123 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001124 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1125 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 curwin->w_wrow += n;
1127
1128#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001129 // When cursor wraps to first char of next line in Insert
1130 // mode, the 'showbreak' string isn't shown, backup to first
1131 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001132 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001133 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001134 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 curwin->w_wcol = 0;
1136#endif
1137 }
1138 }
1139
Bram Moolenaar85a20022019-12-21 18:25:54 +01001140 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1141 // is not folded.
1142 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001143 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#ifdef FEAT_FOLDING
1145 && !curwin->w_cline_folded
1146#endif
1147 )
1148 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001149#ifdef FEAT_PROP_POPUP
1150 if (curwin->w_virtcol_first_char > 0)
1151 {
1152 int cols = (curwin->w_width - extra);
1153 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1154
1155 // each "above" text prop shifts the text one row down
1156 curwin->w_wrow += rows;
1157 curwin->w_wcol -= rows * cols;
1158 endcol -= rows * cols;
1159 curwin->w_cline_height = rows + 1;
1160 }
1161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 /*
1163 * If Cursor is left of the screen, scroll rightwards.
1164 * If Cursor is right of the screen, scroll leftwards
1165 * If we get closer to the edge than 'sidescrolloff', scroll a little
1166 * extra
1167 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001168 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001169 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001170 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (off_left < 0 || off_right > 0)
1172 {
1173 if (off_left < 0)
1174 diff = -off_left;
1175 else
1176 diff = off_right;
1177
Bram Moolenaar85a20022019-12-21 18:25:54 +01001178 // When far off or not enough room on either side, put cursor in
1179 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001180 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1181 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 else
1183 {
1184 if (diff < p_ss)
1185 diff = p_ss;
1186 if (off_left < 0)
1187 new_leftcol = curwin->w_leftcol - diff;
1188 else
1189 new_leftcol = curwin->w_leftcol + diff;
1190 }
1191 if (new_leftcol < 0)
1192 new_leftcol = 0;
1193 if (new_leftcol != (int)curwin->w_leftcol)
1194 {
1195 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001196 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001197 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199 }
1200 curwin->w_wcol -= curwin->w_leftcol;
1201 }
1202 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1203 curwin->w_wcol -= curwin->w_leftcol;
1204 else
1205 curwin->w_wcol = 0;
1206
1207#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // Skip over filler lines. At the top use w_topfill, there
1209 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (curwin->w_cursor.lnum == curwin->w_topline)
1211 curwin->w_wrow += curwin->w_topfill;
1212 else
1213 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1214#endif
1215
1216 prev_skipcol = curwin->w_skipcol;
1217
1218 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if ((curwin->w_wrow >= curwin->w_height
1221 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001222 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && (p_lines =
1224#ifdef FEAT_DIFF
1225 plines_win_nofill
1226#else
1227 plines_win
1228#endif
1229 (curwin, curwin->w_cursor.lnum, FALSE))
1230 - 1 >= curwin->w_height))
1231 && curwin->w_height != 0
1232 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001233 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001234 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001236 // Cursor past end of screen. Happens with a single line that does
1237 // not fit on screen. Find a skipcol to show the text around the
1238 // cursor. Avoid scrolling all the time. compute value of "extra":
1239 // 1: Less than 'scrolloff' lines above
1240 // 2: Less than 'scrolloff' lines below
1241 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001243 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001245 // Compute last display line of the buffer line that we want at the
1246 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (p_lines == 0)
1248 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1249 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001250 if (p_lines > curwin->w_wrow + so)
1251 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 else
1253 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001254 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 extra += 2;
1256
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001257 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001259 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001260 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (n > curwin->w_height / 2)
1262 n -= curwin->w_height / 2;
1263 else
1264 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001265 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 if (n > p_lines - curwin->w_height + 1)
1267 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001268 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 else if (extra == 1)
1271 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001272 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001273 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1274 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (extra > 0)
1276 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001277 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1278 extra = curwin->w_skipcol / width2;
1279 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 }
1282 else if (extra == 2)
1283 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001284 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001285 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001287 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (endcol > curwin->w_skipcol)
1289 curwin->w_skipcol = endcol;
1290 }
1291
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001292 // adjust w_wrow for the changed w_skipcol
1293 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001294 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001295 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001296 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (curwin->w_wrow >= curwin->w_height)
1299 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001300 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001302 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 curwin->w_wrow -= extra;
1304 }
1305
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001306 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 if (extra > 0)
1308 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1309 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001310 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001312 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 curwin->w_skipcol = 0;
1314 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001315 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001317#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001318 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001319#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001320#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1321 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1322 {
1323 curwin->w_wrow += popup_top_extra(curwin);
1324 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001325 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001326 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001327 else
1328 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001329#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001330
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001331 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1332 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001333 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001334 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1337}
1338
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001339#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001340/*
1341 * Compute the screen position of text character at "pos" in window "wp"
1342 * The resulting values are one-based, zero when character is not visible.
1343 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001344 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001345textpos2screenpos(
1346 win_T *wp,
1347 pos_T *pos,
1348 int *rowp, // screen row
1349 int *scolp, // start screen column
1350 int *ccolp, // cursor screen column
1351 int *ecolp) // end screen column
1352{
1353 colnr_T scol = 0, ccol = 0, ecol = 0;
1354 int row = 0;
1355 int rowoff = 0;
1356 colnr_T coloff = 0;
1357
Bram Moolenaar189663b2021-07-21 18:04:56 +02001358 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001359 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001360 colnr_T off;
1361 colnr_T col;
1362 int width;
1363 linenr_T lnum = pos->lnum;
1364#ifdef FEAT_FOLDING
1365 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001366
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001367 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1368#endif
1369 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1370#ifdef FEAT_FOLDING
1371 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001372 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001373 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001374 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001375 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001376 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001377#endif
1378 {
1379 getvcol(wp, pos, &scol, &ccol, &ecol);
1380
1381 // similar to what is done in validate_cursor_col()
1382 col = scol;
1383 off = win_col_off(wp);
1384 col += off;
1385 width = wp->w_width - off + win_col_off2(wp);
1386
1387 // long line wrapping, adjust row
1388 if (wp->w_p_wrap
1389 && col >= (colnr_T)wp->w_width
1390 && width > 0)
1391 {
1392 // use same formula as what is used in curs_columns()
1393 rowoff = ((col - wp->w_width) / width + 1);
1394 col -= rowoff * width;
1395 }
1396 col -= wp->w_leftcol;
1397 if (col >= wp->w_width)
1398 col = -1;
1399 if (col >= 0 && row + rowoff <= wp->w_height)
1400 {
1401 coloff = col - scol + wp->w_wincol + 1;
1402 row += W_WINROW(wp);
1403 }
1404 else
1405 // character is left, right or below of the window
1406 row = rowoff = scol = ccol = ecol = 0;
1407 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001408 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001409 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001410 *scolp = scol + coloff;
1411 *ccolp = ccol + coloff;
1412 *ecolp = ecol + coloff;
1413}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001414#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001415
Bram Moolenaar12034e22019-08-25 22:25:02 +02001416#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001417/*
1418 * "screenpos({winid}, {lnum}, {col})" function
1419 */
1420 void
1421f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1422{
1423 dict_T *dict;
1424 win_T *wp;
1425 pos_T pos;
1426 int row = 0;
1427 int scol = 0, ccol = 0, ecol = 0;
1428
Bram Moolenaar93a10962022-06-16 11:42:09 +01001429 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001430 return;
1431 dict = rettv->vval.v_dict;
1432
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001433 if (in_vim9script()
1434 && (check_for_number_arg(argvars, 0) == FAIL
1435 || check_for_number_arg(argvars, 1) == FAIL
1436 || check_for_number_arg(argvars, 2) == FAIL))
1437 return;
1438
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001439 wp = find_win_by_nr_or_id(&argvars[0]);
1440 if (wp == NULL)
1441 return;
1442
1443 pos.lnum = tv_get_number(&argvars[1]);
1444 pos.col = tv_get_number(&argvars[2]) - 1;
1445 pos.coladd = 0;
1446 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1447
1448 dict_add_number(dict, "row", row);
1449 dict_add_number(dict, "col", scol);
1450 dict_add_number(dict, "curscol", ccol);
1451 dict_add_number(dict, "endcol", ecol);
1452}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001453
1454/*
1455 * "virtcol2col({winid}, {lnum}, {col})" function
1456 */
1457 void
1458f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1459{
1460 win_T *wp;
1461 linenr_T lnum;
1462 int screencol;
1463 int error = FALSE;
1464
1465 rettv->vval.v_number = -1;
1466
1467 if (check_for_number_arg(argvars, 0) == FAIL
1468 || check_for_number_arg(argvars, 1) == FAIL
1469 || check_for_number_arg(argvars, 2) == FAIL)
1470 return;
1471
1472 wp = find_win_by_nr_or_id(&argvars[0]);
1473 if (wp == NULL)
1474 return;
1475
1476 lnum = tv_get_number_chk(&argvars[1], &error);
1477 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1478 return;
1479
1480 screencol = tv_get_number_chk(&argvars[2], &error);
1481 if (error || screencol < 0)
1482 return;
1483
1484 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1485}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488/*
1489 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001492scrolldown(
1493 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001494 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001496 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 int wrow;
1498 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001499 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001500 int width1 = 0;
1501 int width2 = 0;
1502
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001503 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001504 {
1505 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001506 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
1509#ifdef FEAT_FOLDING
1510 linenr_T first;
1511
Bram Moolenaar85a20022019-12-21 18:25:54 +01001512 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1514#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001515 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001516 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001519 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1520 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
1522 ++curwin->w_topfill;
1523 ++done;
1524 }
1525 else
1526#endif
1527 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001528 // break when at the very top
1529 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001530 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001532 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001534 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001535 if (curwin->w_skipcol >= width1 + width2)
1536 curwin->w_skipcol -= width2;
1537 else
1538 curwin->w_skipcol -= width1;
1539 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 }
1542 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001543 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001544 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001545 --curwin->w_topline;
1546 curwin->w_skipcol = 0;
1547#ifdef FEAT_DIFF
1548 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001550#ifdef FEAT_FOLDING
1551 // A sequence of folded lines only counts for one logical line
1552 if (hasFolding(curwin->w_topline, &first, NULL))
1553 {
1554 ++done;
1555 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001556 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001557 curwin->w_botline -= curwin->w_topline - first;
1558 curwin->w_topline = first;
1559 }
1560 else
1561#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001562 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001563 {
1564 int size = win_linetabsize(curwin, curwin->w_topline,
1565 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1566 if (size > width1)
1567 {
1568 curwin->w_skipcol = width1;
1569 size -= width1;
1570 redraw_later(UPD_NOT_VALID);
1571 }
1572 while (size > width2)
1573 {
1574 curwin->w_skipcol += width2;
1575 size -= width2;
1576 }
1577 ++done;
1578 }
1579 else
1580 done += PLINES_NOFILL(curwin->w_topline);
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001583 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 invalidate_botline();
1585 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001586 curwin->w_wrow += done; // keep w_wrow updated
1587 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589#ifdef FEAT_DIFF
1590 if (curwin->w_cursor.lnum == curwin->w_topline)
1591 curwin->w_cline_row = 0;
1592 check_topfill(curwin, TRUE);
1593#endif
1594
1595 /*
1596 * Compute the row number of the last row of the cursor line
1597 * and move the cursor onto the displayed part of the window.
1598 */
1599 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001600 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 validate_virtcol();
1603 validate_cheight();
1604 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001605 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1608 {
1609#ifdef FEAT_FOLDING
1610 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1611 {
1612 --wrow;
1613 if (first == 1)
1614 curwin->w_cursor.lnum = 1;
1615 else
1616 curwin->w_cursor.lnum = first - 1;
1617 }
1618 else
1619#endif
1620 wrow -= plines(curwin->w_cursor.lnum--);
1621 curwin->w_valid &=
1622 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1623 moved = TRUE;
1624 }
1625 if (moved)
1626 {
1627#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001628 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 foldAdjustCursor();
1630#endif
1631 coladvance(curwin->w_curswant);
1632 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001633
1634 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1635 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001636 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001637 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1638
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001639 // make sure the cursor is in the visible text
1640 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001641 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001642 int row = 0;
1643 if (col >= width1)
1644 {
1645 col -= width1;
1646 ++row;
1647 }
1648 if (col > width2)
1649 {
1650 row += col / width2;
1651 col = col % width2;
1652 }
1653 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001654 {
1655 curwin->w_curswant = curwin->w_virtcol
1656 - (row - curwin->w_height + 1) * width2;
1657 coladvance(curwin->w_curswant);
1658 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660}
1661
1662/*
1663 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001666scrollup(
1667 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001668 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001670 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001672 if (do_sms
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673# ifdef FEAT_FOLDING
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001674 || (byfold && hasAnyFolding(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675# endif
1676# ifdef FEAT_DIFF
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001677 || curwin->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678# endif
1679 )
1680 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001681 int width1 = curwin->w_width - curwin_col_off();
1682 int width2 = width1 + curwin_col_off2();
1683 int size = 0;
1684 linenr_T prev_topline = curwin->w_topline;
1685
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001686 if (do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001687 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001688
1689 // diff mode: first consume "topfill"
1690 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1691 // the line, then advance to the next line.
1692 // folding: count each sequence of folded lines as one logical line.
1693 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695# ifdef FEAT_DIFF
1696 if (curwin->w_topfill > 0)
1697 --curwin->w_topfill;
1698 else
1699# endif
1700 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001701 linenr_T lnum = curwin->w_topline;
1702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703# ifdef FEAT_FOLDING
1704 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001705 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 (void)hasFolding(lnum, NULL, &lnum);
1707# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001708 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001709 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001710 // 'smoothscroll': increase "w_skipcol" until it goes over
1711 // the end of the line, then advance to the next line.
1712 int add = curwin->w_skipcol > 0 ? width2 : width1;
1713 curwin->w_skipcol += add;
1714 if (curwin->w_skipcol >= size)
1715 {
1716 if (lnum == curbuf->b_ml.ml_line_count)
1717 {
1718 // at the last screen line, can't scroll further
1719 curwin->w_skipcol -= add;
1720 break;
1721 }
1722 ++lnum;
1723 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001724 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001725 else
1726 {
1727 if (lnum >= curbuf->b_ml.ml_line_count)
1728 break;
1729 ++lnum;
1730 }
1731
1732 if (lnum > curwin->w_topline)
1733 {
1734 // approximate w_botline
1735 curwin->w_botline += lnum - curwin->w_topline;
1736 curwin->w_topline = lnum;
1737# ifdef FEAT_DIFF
1738 curwin->w_topfill = diff_check_fill(curwin, lnum);
1739# endif
1740 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001741 if (todo > 1 && do_sms)
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001742 size = linetabsize(curwin, curwin->w_topline);
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001743 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001744 }
1745 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001746
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001747 if (curwin->w_topline == prev_topline)
1748 // need to redraw even though w_topline didn't change
1749 redraw_later(UPD_NOT_VALID);
1750 }
1751 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
1753 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001754 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756
1757 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1758 curwin->w_topline = curbuf->b_ml.ml_line_count;
1759 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1760 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1761
1762#ifdef FEAT_DIFF
1763 check_topfill(curwin, FALSE);
1764#endif
1765
1766#ifdef FEAT_FOLDING
1767 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001768 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1770#endif
1771
1772 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1773 if (curwin->w_cursor.lnum < curwin->w_topline)
1774 {
1775 curwin->w_cursor.lnum = curwin->w_topline;
1776 curwin->w_valid &=
1777 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1778 coladvance(curwin->w_curswant);
1779 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001780 if (curwin->w_cursor.lnum == curwin->w_topline
1781 && do_sms && curwin->w_skipcol > 0)
1782 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001783 int width1 = curwin->w_width - curwin_col_off();
1784 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001785 long so = get_scrolloff_value();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001786 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001787 int space_cols = (curwin->w_height - 1) * width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001788
1789 // Make sure the cursor is in a visible part of the line, taking
1790 // 'scrolloff' into account, but using screen lines.
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001791 // If there are not enough screen lines put the cursor in the middle.
1792 if (scrolloff_cols > space_cols / 2)
1793 scrolloff_cols = space_cols / 2;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001794 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001795 if (curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001796 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001797 colnr_T col = curwin->w_virtcol;
1798
1799 if (col < width1)
1800 col += width1;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001801 while (col < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001802 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001803 curwin->w_curswant = col;
1804 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001805
1806 // validate_virtcol() marked various things as valid, but after
1807 // moving the cursor they need to be recomputed
1808 curwin->w_valid &=
1809 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001810 }
1811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812}
1813
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001814/*
1815 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1816 * valid for 'smoothscroll'.
1817 */
1818 void
1819adjust_skipcol(void)
1820{
1821 if (!curwin->w_p_wrap
1822 || !curwin->w_p_sms
1823 || curwin->w_cursor.lnum != curwin->w_topline)
1824 return;
1825
1826 int width1 = curwin->w_width - curwin_col_off();
1827 int width2 = width1 + curwin_col_off2();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001828 long so = get_scrolloff_value();
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001829 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1830 int scrolled = FALSE;
1831
1832 validate_virtcol();
Bram Moolenaarc9121f72022-10-14 20:09:04 +01001833 if (curwin->w_cline_height == curwin->w_height)
1834 {
1835 // the line just fits in the window, don't scroll
1836 if (curwin->w_skipcol != 0)
1837 {
1838 curwin->w_skipcol = 0;
1839 redraw_later(UPD_NOT_VALID);
1840 }
1841 return;
1842 }
1843
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001844 while (curwin->w_skipcol > 0
1845 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1846 {
1847 // scroll a screen line down
1848 if (curwin->w_skipcol >= width1 + width2)
1849 curwin->w_skipcol -= width2;
1850 else
1851 curwin->w_skipcol -= width1;
1852 redraw_later(UPD_NOT_VALID);
1853 scrolled = TRUE;
1854 validate_virtcol();
1855 }
1856 if (scrolled)
1857 return; // don't scroll in the other direction now
1858
1859 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1860 int row = 0;
1861 if (col >= width1)
1862 {
1863 col -= width1;
1864 ++row;
1865 }
1866 if (col > width2)
1867 {
1868 row += col / width2;
1869 col = col % width2;
1870 }
1871 if (row >= curwin->w_height)
1872 {
1873 if (curwin->w_skipcol == 0)
1874 {
1875 curwin->w_skipcol += width1;
1876 --row;
1877 }
1878 if (row >= curwin->w_height)
1879 curwin->w_skipcol += (row - curwin->w_height) * width2;
1880 redraw_later(UPD_NOT_VALID);
1881 }
1882}
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884#ifdef FEAT_DIFF
1885/*
1886 * Don't end up with too many filler lines in the window.
1887 */
1888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001889check_topfill(
1890 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001891 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
1893 int n;
1894
1895 if (wp->w_topfill > 0)
1896 {
1897 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1898 if (wp->w_topfill + n > wp->w_height)
1899 {
1900 if (down && wp->w_topline > 1)
1901 {
1902 --wp->w_topline;
1903 wp->w_topfill = 0;
1904 }
1905 else
1906 {
1907 wp->w_topfill = wp->w_height - n;
1908 if (wp->w_topfill < 0)
1909 wp->w_topfill = 0;
1910 }
1911 }
1912 }
1913}
1914
1915/*
1916 * Use as many filler lines as possible for w_topline. Make sure w_topline
1917 * is still visible.
1918 */
1919 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001920max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921{
1922 int n;
1923
1924 n = plines_nofill(curwin->w_topline);
1925 if (n >= curwin->w_height)
1926 curwin->w_topfill = 0;
1927 else
1928 {
1929 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1930 if (curwin->w_topfill + n > curwin->w_height)
1931 curwin->w_topfill = curwin->w_height - n;
1932 }
1933}
1934#endif
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936/*
1937 * Scroll the screen one line down, but don't do it if it would move the
1938 * cursor off the screen.
1939 */
1940 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001941scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942{
1943 int end_row;
1944#ifdef FEAT_DIFF
1945 int can_fill = (curwin->w_topfill
1946 < diff_check_fill(curwin, curwin->w_topline));
1947#endif
1948
1949 if (curwin->w_topline <= 1
1950#ifdef FEAT_DIFF
1951 && !can_fill
1952#endif
1953 )
1954 return;
1955
Bram Moolenaar85a20022019-12-21 18:25:54 +01001956 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 /*
1959 * Compute the row number of the last row of the cursor line
1960 * and make sure it doesn't go off the screen. Make sure the cursor
1961 * doesn't go past 'scrolloff' lines from the screen end.
1962 */
1963 end_row = curwin->w_wrow;
1964#ifdef FEAT_DIFF
1965 if (can_fill)
1966 ++end_row;
1967 else
1968 end_row += plines_nofill(curwin->w_topline - 1);
1969#else
1970 end_row += plines(curwin->w_topline - 1);
1971#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001972 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 validate_cheight();
1975 validate_virtcol();
1976 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001977 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001979 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
1981#ifdef FEAT_DIFF
1982 if (can_fill)
1983 {
1984 ++curwin->w_topfill;
1985 check_topfill(curwin, TRUE);
1986 }
1987 else
1988 {
1989 --curwin->w_topline;
1990 curwin->w_topfill = 0;
1991 }
1992#else
1993 --curwin->w_topline;
1994#endif
1995#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001996 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001998 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2000 }
2001}
2002
2003/*
2004 * Scroll the screen one line up, but don't do it if it would move the cursor
2005 * off the screen.
2006 */
2007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 int start_row;
2011
2012 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2013#ifdef FEAT_DIFF
2014 && curwin->w_topfill == 0
2015#endif
2016 )
2017 return;
2018
Bram Moolenaar85a20022019-12-21 18:25:54 +01002019 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /*
2022 * Compute the row number of the first row of the cursor line
2023 * and make sure it doesn't go off the screen. Make sure the cursor
2024 * doesn't go before 'scrolloff' lines from the screen start.
2025 */
2026#ifdef FEAT_DIFF
2027 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2028 - curwin->w_topfill;
2029#else
2030 start_row = curwin->w_wrow - plines(curwin->w_topline);
2031#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002032 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
2034 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002035 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002037 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039#ifdef FEAT_DIFF
2040 if (curwin->w_topfill > 0)
2041 --curwin->w_topfill;
2042 else
2043#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002044 {
2045#ifdef FEAT_FOLDING
2046 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002049 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002050 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2052 }
2053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055/*
2056 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2057 * a (wrapped) text line. Uses and sets "lp->fill".
2058 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002059 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 */
2061 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002062topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063{
2064#ifdef FEAT_DIFF
2065 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2066 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002067 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 ++lp->fill;
2069 lp->height = 1;
2070 }
2071 else
2072#endif
2073 {
2074 --lp->lnum;
2075#ifdef FEAT_DIFF
2076 lp->fill = 0;
2077#endif
2078 if (lp->lnum < 1)
2079 lp->height = MAXCOL;
2080 else
2081#ifdef FEAT_FOLDING
2082 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002083 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 lp->height = 1;
2085 else
2086#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002087 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089}
2090
2091/*
2092 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2093 * a (wrapped) text line. Uses and sets "lp->fill".
2094 * Returns the height of the added line in "lp->height".
2095 * Lines below the last one are incredibly high.
2096 */
2097 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002098botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099{
2100#ifdef FEAT_DIFF
2101 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2102 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002103 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 ++lp->fill;
2105 lp->height = 1;
2106 }
2107 else
2108#endif
2109 {
2110 ++lp->lnum;
2111#ifdef FEAT_DIFF
2112 lp->fill = 0;
2113#endif
2114 if (lp->lnum > curbuf->b_ml.ml_line_count)
2115 lp->height = MAXCOL;
2116 else
2117#ifdef FEAT_FOLDING
2118 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002119 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 lp->height = 1;
2121 else
2122#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002123 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
2125}
2126
2127#ifdef FEAT_DIFF
2128/*
2129 * Switch from including filler lines below lp->lnum to including filler
2130 * lines above loff.lnum + 1. This keeps pointing to the same line.
2131 * When there are no filler lines nothing changes.
2132 */
2133 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 if (lp->fill > 0)
2137 {
2138 ++lp->lnum;
2139 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2140 }
2141}
2142
2143/*
2144 * Switch from including filler lines above lp->lnum to including filler
2145 * lines below loff.lnum - 1. This keeps pointing to the same line.
2146 * When there are no filler lines nothing changes.
2147 */
2148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002149topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150{
2151 if (lp->fill > 0)
2152 {
2153 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2154 --lp->lnum;
2155 }
2156}
2157#endif
2158
2159/*
2160 * Recompute topline to put the cursor at the top of the window.
2161 * Scroll at least "min_scroll" lines.
2162 * If "always" is TRUE, always set topline (for "zt").
2163 */
2164 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002165scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 int scrolled = 0;
2168 int extra = 0;
2169 int used;
2170 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002171 linenr_T top; // just above displayed lines
2172 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002174 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#ifdef FEAT_DIFF
2176 linenr_T old_topfill = curwin->w_topfill;
2177#endif
2178 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002179 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (mouse_dragging > 0)
2182 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184 /*
2185 * Decrease topline until:
2186 * - it has become 1
2187 * - (part of) the cursor line is moved off the screen or
2188 * - moved at least 'scrolljump' lines and
2189 * - at least 'scrolloff' lines above and below the cursor
2190 */
2191 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002192 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 if (curwin->w_cursor.lnum < curwin->w_topline)
2194 scrolled = used;
2195
2196#ifdef FEAT_FOLDING
2197 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2198 {
2199 --top;
2200 ++bot;
2201 }
2202 else
2203#endif
2204 {
2205 top = curwin->w_cursor.lnum - 1;
2206 bot = curwin->w_cursor.lnum + 1;
2207 }
2208 new_topline = top + 1;
2209
2210#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 // "used" already contains the number of filler lines above, don't add it
2212 // again.
2213 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002214 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#endif
2216
2217 /*
2218 * Check if the lines from "top" to "bot" fit in the window. If they do,
2219 * set new_topline and advance "top" and "bot" to include more lines.
2220 */
2221 while (top > 0)
2222 {
2223#ifdef FEAT_FOLDING
2224 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002225 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 i = 1;
2227 else
2228#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002229 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 used += i;
2231 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2232 {
2233#ifdef FEAT_FOLDING
2234 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002235 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 ++used;
2237 else
2238#endif
2239 used += plines(bot);
2240 }
2241 if (used > curwin->w_height)
2242 break;
2243 if (top < curwin->w_topline)
2244 scrolled += i;
2245
2246 /*
2247 * If scrolling is needed, scroll at least 'sj' lines.
2248 */
2249 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2250 && extra >= off)
2251 break;
2252
2253 extra += i;
2254 new_topline = top;
2255 --top;
2256 ++bot;
2257 }
2258
2259 /*
2260 * If we don't have enough space, put cursor in the middle.
2261 * This makes sure we get the same position when using "k" and "j"
2262 * in a small window.
2263 */
2264 if (used > curwin->w_height)
2265 scroll_cursor_halfway(FALSE);
2266 else
2267 {
2268 /*
2269 * If "always" is FALSE, only adjust topline to a lower value, higher
2270 * value may happen with wrapping lines
2271 */
2272 if (new_topline < curwin->w_topline || always)
2273 curwin->w_topline = new_topline;
2274 if (curwin->w_topline > curwin->w_cursor.lnum)
2275 curwin->w_topline = curwin->w_cursor.lnum;
2276#ifdef FEAT_DIFF
2277 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2278 if (curwin->w_topfill > 0 && extra > off)
2279 {
2280 curwin->w_topfill -= extra - off;
2281 if (curwin->w_topfill < 0)
2282 curwin->w_topfill = 0;
2283 }
2284 check_topfill(curwin, FALSE);
2285#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002286 // TODO: if the line doesn't fit may optimize w_skipcol
2287 if (curwin->w_topline == curwin->w_cursor.lnum)
2288 {
2289 curwin->w_skipcol = 0;
2290 redraw_later(UPD_NOT_VALID);
2291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002293 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294#ifdef FEAT_DIFF
2295 || curwin->w_topfill != old_topfill
2296#endif
2297 )
2298 curwin->w_valid &=
2299 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2300 curwin->w_valid |= VALID_TOPLINE;
2301 }
2302}
2303
2304/*
2305 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2306 * screen lines for text lines.
2307 */
2308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002309set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
2311#ifdef FEAT_DIFF
2312 wp->w_filler_rows = 0;
2313#endif
2314 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002315 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 else
2317 {
2318 wp->w_empty_rows = wp->w_height - used;
2319#ifdef FEAT_DIFF
2320 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2321 {
2322 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2323 if (wp->w_empty_rows > wp->w_filler_rows)
2324 wp->w_empty_rows -= wp->w_filler_rows;
2325 else
2326 {
2327 wp->w_filler_rows = wp->w_empty_rows;
2328 wp->w_empty_rows = 0;
2329 }
2330 }
2331#endif
2332 }
2333}
2334
2335/*
2336 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002337 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2339 * This is messy stuff!!!
2340 */
2341 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002342scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 int used;
2345 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002346 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 int extra = 0;
2348 int i;
2349 linenr_T line_count;
2350 linenr_T old_topline = curwin->w_topline;
2351 lineoff_T loff;
2352 lineoff_T boff;
2353#ifdef FEAT_DIFF
2354 int old_topfill = curwin->w_topfill;
2355 int fill_below_window;
2356#endif
2357 linenr_T old_botline = curwin->w_botline;
2358 linenr_T old_valid = curwin->w_valid;
2359 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002360 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002361 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363 cln = curwin->w_cursor.lnum;
2364 if (set_topbot)
2365 {
2366 used = 0;
2367 curwin->w_botline = cln + 1;
2368#ifdef FEAT_DIFF
2369 loff.fill = 0;
2370#endif
2371 for (curwin->w_topline = curwin->w_botline;
2372 curwin->w_topline > 1;
2373 curwin->w_topline = loff.lnum)
2374 {
2375 loff.lnum = curwin->w_topline;
2376 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002377 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 break;
2379 used += loff.height;
2380#ifdef FEAT_DIFF
2381 curwin->w_topfill = loff.fill;
2382#endif
2383 }
2384 set_empty_rows(curwin, used);
2385 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2386 if (curwin->w_topline != old_topline
2387#ifdef FEAT_DIFF
2388 || curwin->w_topfill != old_topfill
2389#endif
2390 )
2391 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2392 }
2393 else
2394 validate_botline();
2395
Bram Moolenaar85a20022019-12-21 18:25:54 +01002396 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#ifdef FEAT_DIFF
2398 used = plines_nofill(cln);
2399#else
2400 validate_cheight();
2401 used = curwin->w_cline_height;
2402#endif
2403
Bram Moolenaar85a20022019-12-21 18:25:54 +01002404 // If the cursor is below botline, we will at least scroll by the height
2405 // of the cursor line. Correct for empty lines, which are really part of
2406 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (cln >= curwin->w_botline)
2408 {
2409 scrolled = used;
2410 if (cln == curwin->w_botline)
2411 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002412 min_scrolled = scrolled;
2413 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2414 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
Bram Moolenaar4d31b482022-10-06 16:03:09 +01002415 min_scrolled += PLINES_NOFILL(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417
2418 /*
2419 * Stop counting lines to scroll when
2420 * - hitting start of the file
2421 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002422 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 * - lines between botline and cursor have been counted
2424 */
2425#ifdef FEAT_FOLDING
2426 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2427#endif
2428 {
2429 loff.lnum = cln;
2430 boff.lnum = cln;
2431 }
2432#ifdef FEAT_DIFF
2433 loff.fill = 0;
2434 boff.fill = 0;
2435 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2436 - curwin->w_filler_rows;
2437#endif
2438
2439 while (loff.lnum > 1)
2440 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002441 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2442 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002444 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2446 && loff.lnum <= curwin->w_botline
2447#ifdef FEAT_DIFF
2448 && (loff.lnum < curwin->w_botline
2449 || loff.fill >= fill_below_window)
2450#endif
2451 )
2452 break;
2453
Bram Moolenaar85a20022019-12-21 18:25:54 +01002454 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002456 if (loff.height == MAXCOL)
2457 used = MAXCOL;
2458 else
2459 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if (used > curwin->w_height)
2461 break;
2462 if (loff.lnum >= curwin->w_botline
2463#ifdef FEAT_DIFF
2464 && (loff.lnum > curwin->w_botline
2465 || loff.fill <= fill_below_window)
2466#endif
2467 )
2468 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002469 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 scrolled += loff.height;
2471 if (loff.lnum == curwin->w_botline
2472#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002473 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#endif
2475 )
2476 scrolled -= curwin->w_empty_rows;
2477 }
2478
2479 if (boff.lnum < curbuf->b_ml.ml_line_count)
2480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002481 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 botline_forw(&boff);
2483 used += boff.height;
2484 if (used > curwin->w_height)
2485 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002486 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2487 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 extra += boff.height;
2490 if (boff.lnum >= curwin->w_botline
2491#ifdef FEAT_DIFF
2492 || (boff.lnum + 1 == curwin->w_botline
2493 && boff.fill > curwin->w_filler_rows)
2494#endif
2495 )
2496 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002497 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 scrolled += boff.height;
2499 if (boff.lnum == curwin->w_botline
2500#ifdef FEAT_DIFF
2501 && boff.fill == 0
2502#endif
2503 )
2504 scrolled -= curwin->w_empty_rows;
2505 }
2506 }
2507 }
2508 }
2509
Bram Moolenaar85a20022019-12-21 18:25:54 +01002510 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 if (scrolled <= 0)
2512 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002513 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 else if (used > curwin->w_height)
2515 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002516 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 else
2518 {
2519 line_count = 0;
2520#ifdef FEAT_DIFF
2521 boff.fill = curwin->w_topfill;
2522#endif
2523 boff.lnum = curwin->w_topline - 1;
2524 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2525 {
2526 botline_forw(&boff);
2527 i += boff.height;
2528 ++line_count;
2529 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002530 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 line_count = 9999;
2532 }
2533
2534 /*
2535 * Scroll up if the cursor is off the bottom of the screen a bit.
2536 * Otherwise put it at 1/2 of the screen.
2537 */
2538 if (line_count >= curwin->w_height && line_count > min_scroll)
2539 scroll_cursor_halfway(FALSE);
2540 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002541 {
2542 // With 'smoothscroll' scroll at least the height of the cursor line.
2543 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled)
2544 line_count = min_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 /*
2549 * If topline didn't change we need to restore w_botline and w_empty_rows
2550 * (we changed them).
2551 * If topline did change, update_screen() will set botline.
2552 */
2553 if (curwin->w_topline == old_topline && set_topbot)
2554 {
2555 curwin->w_botline = old_botline;
2556 curwin->w_empty_rows = old_empty_rows;
2557 curwin->w_valid = old_valid;
2558 }
2559 curwin->w_valid |= VALID_TOPLINE;
2560}
2561
2562/*
2563 * Recompute topline to put the cursor halfway the window
2564 * If "atend" is TRUE, also put it halfway at the end of the file.
2565 */
2566 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002567scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
2569 int above = 0;
2570 linenr_T topline;
2571#ifdef FEAT_DIFF
2572 int topfill = 0;
2573#endif
2574 int below = 0;
2575 int used;
2576 lineoff_T loff;
2577 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002578#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002579 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002582#ifdef FEAT_PROP_POPUP
2583 // if the width changed this needs to be updated first
2584 may_update_popup_position();
2585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2587#ifdef FEAT_FOLDING
2588 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2589#endif
2590#ifdef FEAT_DIFF
2591 used = plines_nofill(loff.lnum);
2592 loff.fill = 0;
2593 boff.fill = 0;
2594#else
2595 used = plines(loff.lnum);
2596#endif
2597 topline = loff.lnum;
2598 while (topline > 1)
2599 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002600 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
2602 if (boff.lnum < curbuf->b_ml.ml_line_count)
2603 {
2604 botline_forw(&boff);
2605 used += boff.height;
2606 if (used > curwin->w_height)
2607 break;
2608 below += boff.height;
2609 }
2610 else
2611 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002612 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if (atend)
2614 ++used;
2615 }
2616 }
2617
Bram Moolenaar85a20022019-12-21 18:25:54 +01002618 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002621 if (loff.height == MAXCOL)
2622 used = MAXCOL;
2623 else
2624 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 if (used > curwin->w_height)
2626 break;
2627 above += loff.height;
2628 topline = loff.lnum;
2629#ifdef FEAT_DIFF
2630 topfill = loff.fill;
2631#endif
2632 }
2633 }
2634#ifdef FEAT_FOLDING
2635 if (!hasFolding(topline, &curwin->w_topline, NULL))
2636#endif
2637 curwin->w_topline = topline;
2638#ifdef FEAT_DIFF
2639 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002640 if (old_topline > curwin->w_topline + curwin->w_height)
2641 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 check_topfill(curwin, FALSE);
2643#endif
2644 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2645 curwin->w_valid |= VALID_TOPLINE;
2646}
2647
2648/*
2649 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002650 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 * If not possible, put it at the same position as scroll_cursor_halfway().
2652 * When called topline must be valid!
2653 */
2654 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002655cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002659 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 linenr_T botline;
2661 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002664 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 /*
2667 * How many lines we would like to have above/below the cursor depends on
2668 * whether the first/last line of the file is on screen.
2669 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002670 above_wanted = so;
2671 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002672 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
2674 above_wanted = mouse_dragging - 1;
2675 below_wanted = mouse_dragging - 1;
2676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (curwin->w_topline == 1)
2678 {
2679 above_wanted = 0;
2680 max_off = curwin->w_height / 2;
2681 if (below_wanted > max_off)
2682 below_wanted = max_off;
2683 }
2684 validate_botline();
2685 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002686 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
2688 below_wanted = 0;
2689 max_off = (curwin->w_height - 1) / 2;
2690 if (above_wanted > max_off)
2691 above_wanted = max_off;
2692 }
2693
2694 /*
2695 * If there are sufficient file-lines above and below the cursor, we can
2696 * return now.
2697 */
2698 cln = curwin->w_cursor.lnum;
2699 if (cln >= curwin->w_topline + above_wanted
2700 && cln < curwin->w_botline - below_wanted
2701#ifdef FEAT_FOLDING
2702 && !hasAnyFolding(curwin)
2703#endif
2704 )
2705 return;
2706
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002707 if (curwin->w_p_sms && !curwin->w_p_wrap)
2708 {
2709 // 'smoothscroll is active
2710 if (curwin->w_cline_height == curwin->w_height)
2711 {
2712 // The cursor line just fits in the window, don't scroll.
2713 curwin->w_skipcol = 0;
2714 return;
2715 }
2716 // TODO: If the cursor line doesn't fit in the window then only adjust
2717 // w_skipcol.
2718 }
2719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 /*
2721 * Narrow down the area where the cursor can be put by taking lines from
2722 * the top and the bottom until:
2723 * - the desired context lines are found
2724 * - the lines from the top is past the lines from the bottom
2725 */
2726 topline = curwin->w_topline;
2727 botline = curwin->w_botline - 1;
2728#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002729 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 above = curwin->w_topfill;
2731 below = curwin->w_filler_rows;
2732#endif
2733 while ((above < above_wanted || below < below_wanted) && topline < botline)
2734 {
2735 if (below < below_wanted && (below <= above || above >= above_wanted))
2736 {
2737#ifdef FEAT_FOLDING
2738 if (hasFolding(botline, &botline, NULL))
2739 ++below;
2740 else
2741#endif
2742 below += plines(botline);
2743 --botline;
2744 }
2745 if (above < above_wanted && (above < below || below >= below_wanted))
2746 {
2747#ifdef FEAT_FOLDING
2748 if (hasFolding(topline, NULL, &topline))
2749 ++above;
2750 else
2751#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002752 above += PLINES_NOFILL(topline);
2753#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002754 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (topline < botline)
2756 above += diff_check_fill(curwin, topline + 1);
2757#endif
2758 ++topline;
2759 }
2760 }
2761 if (topline == botline || botline == 0)
2762 curwin->w_cursor.lnum = topline;
2763 else if (topline > botline)
2764 curwin->w_cursor.lnum = botline;
2765 else
2766 {
2767 if (cln < topline && curwin->w_topline > 1)
2768 {
2769 curwin->w_cursor.lnum = topline;
2770 curwin->w_valid &=
2771 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2772 }
2773 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2774 {
2775 curwin->w_cursor.lnum = botline;
2776 curwin->w_valid &=
2777 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2778 }
2779 }
2780 curwin->w_valid |= VALID_TOPLINE;
2781}
2782
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002783static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785/*
2786 * move screen 'count' pages up or down and update screen
2787 *
2788 * return FAIL for failure, OK otherwise
2789 */
2790 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002791onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
2793 long n;
2794 int retval = OK;
2795 lineoff_T loff;
2796 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002797 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
Bram Moolenaar85a20022019-12-21 18:25:54 +01002799 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
2801 beep_flush();
2802 return FAIL;
2803 }
2804
2805 for ( ; count > 0; --count)
2806 {
2807 validate_botline();
2808 /*
2809 * It's an error to move a page up when the first line is already on
2810 * the screen. It's an error to move a page down when the last line
2811 * is on the screen and the topline is 'scrolloff' lines from the
2812 * last line.
2813 */
2814 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002815 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2817 : (curwin->w_topline == 1
2818#ifdef FEAT_DIFF
2819 && curwin->w_topfill ==
2820 diff_check_fill(curwin, curwin->w_topline)
2821#endif
2822 ))
2823 {
2824 beep_flush();
2825 retval = FAIL;
2826 break;
2827 }
2828
2829#ifdef FEAT_DIFF
2830 loff.fill = 0;
2831#endif
2832 if (dir == FORWARD)
2833 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002834 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002836 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002837 if (p_window <= 2)
2838 ++curwin->w_topline;
2839 else
2840 curwin->w_topline += p_window - 2;
2841 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2842 curwin->w_topline = curbuf->b_ml.ml_line_count;
2843 curwin->w_cursor.lnum = curwin->w_topline;
2844 }
2845 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2846 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002847 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 curwin->w_topline = curbuf->b_ml.ml_line_count;
2849#ifdef FEAT_DIFF
2850 curwin->w_topfill = 0;
2851#endif
2852 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2853 }
2854 else
2855 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002856 // For the overlap, start with the line just below the window
2857 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 loff.lnum = curwin->w_botline;
2859#ifdef FEAT_DIFF
2860 loff.fill = diff_check_fill(curwin, loff.lnum)
2861 - curwin->w_filler_rows;
2862#endif
2863 get_scroll_overlap(&loff, -1);
2864 curwin->w_topline = loff.lnum;
2865#ifdef FEAT_DIFF
2866 curwin->w_topfill = loff.fill;
2867 check_topfill(curwin, FALSE);
2868#endif
2869 curwin->w_cursor.lnum = curwin->w_topline;
2870 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2871 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2872 }
2873 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002874 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876#ifdef FEAT_DIFF
2877 if (curwin->w_topline == 1)
2878 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002879 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 max_topfill();
2881 continue;
2882 }
2883#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002884 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002885 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002886 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002887 if (p_window <= 2)
2888 --curwin->w_topline;
2889 else
2890 curwin->w_topline -= p_window - 2;
2891 if (curwin->w_topline < 1)
2892 curwin->w_topline = 1;
2893 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2894 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2895 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2896 continue;
2897 }
2898
Bram Moolenaar85a20022019-12-21 18:25:54 +01002899 // Find the line at the top of the window that is going to be the
2900 // line at the bottom of the window. Make sure this results in
2901 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 loff.lnum = curwin->w_topline - 1;
2903#ifdef FEAT_DIFF
2904 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2905 - curwin->w_topfill;
2906#endif
2907 get_scroll_overlap(&loff, 1);
2908
2909 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2910 {
2911 loff.lnum = curbuf->b_ml.ml_line_count;
2912#ifdef FEAT_DIFF
2913 loff.fill = 0;
2914 }
2915 else
2916 {
2917 botline_topline(&loff);
2918#endif
2919 }
2920 curwin->w_cursor.lnum = loff.lnum;
2921
Bram Moolenaar85a20022019-12-21 18:25:54 +01002922 // Find the line just above the new topline to get the right line
2923 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 n = 0;
2925 while (n <= curwin->w_height && loff.lnum >= 1)
2926 {
2927 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002928 if (loff.height == MAXCOL)
2929 n = MAXCOL;
2930 else
2931 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002933 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
2935 curwin->w_topline = 1;
2936#ifdef FEAT_DIFF
2937 max_topfill();
2938#endif
2939 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2940 }
2941 else
2942 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002943 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944#ifdef FEAT_DIFF
2945 topline_botline(&loff);
2946#endif
2947 botline_forw(&loff);
2948 botline_forw(&loff);
2949#ifdef FEAT_DIFF
2950 botline_topline(&loff);
2951#endif
2952#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002953 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2955#endif
2956
Bram Moolenaar85a20022019-12-21 18:25:54 +01002957 // Always scroll at least one line. Avoid getting stuck on
2958 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (loff.lnum >= curwin->w_topline
2960#ifdef FEAT_DIFF
2961 && (loff.lnum > curwin->w_topline
2962 || loff.fill >= curwin->w_topfill)
2963#endif
2964 )
2965 {
2966#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002967 // First try using the maximum number of filler lines. If
2968 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 loff.fill = curwin->w_topfill;
2970 if (curwin->w_topfill < diff_check_fill(curwin,
2971 curwin->w_topline))
2972 max_topfill();
2973 if (curwin->w_topfill == loff.fill)
2974#endif
2975 {
2976 --curwin->w_topline;
2977#ifdef FEAT_DIFF
2978 curwin->w_topfill = 0;
2979#endif
2980 }
2981 comp_botline(curwin);
2982 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002983 curwin->w_valid &=
2984 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 }
2986 else
2987 {
2988 curwin->w_topline = loff.lnum;
2989#ifdef FEAT_DIFF
2990 curwin->w_topfill = loff.fill;
2991 check_topfill(curwin, FALSE);
2992#endif
2993 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2994 }
2995 }
2996 }
2997 }
2998#ifdef FEAT_FOLDING
2999 foldAdjustCursor();
3000#endif
3001 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02003002 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00003003 if (retval == OK)
3004 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
3006
Bram Moolenaar907dad72018-07-10 15:07:15 +02003007 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003009 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
3010 // But make sure we scroll at least one line (happens with mix of long
3011 // wrapping lines and non-wrapping line).
3012 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02003014 scroll_cursor_top(1, FALSE);
3015 if (curwin->w_topline <= old_topline
3016 && old_topline < curbuf->b_ml.ml_line_count)
3017 {
3018 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02003020 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3021#endif
3022 }
3023 }
3024#ifdef FEAT_FOLDING
3025 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 }
3029
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003030 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 return retval;
3032}
3033
3034/*
3035 * Decide how much overlap to use for page-up or page-down scrolling.
3036 * This is symmetric, so that doing both keeps the same lines displayed.
3037 * Three lines are examined:
3038 *
3039 * before CTRL-F after CTRL-F / before CTRL-B
3040 * etc. l1
3041 * l1 last but one line ------------
3042 * l2 last text line l2 top text line
3043 * ------------- l3 second text line
3044 * l3 etc.
3045 */
3046 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003047get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 int h1, h2, h3, h4;
3050 int min_height = curwin->w_height - 2;
3051 lineoff_T loff0, loff1, loff2;
3052
3053#ifdef FEAT_DIFF
3054 if (lp->fill > 0)
3055 lp->height = 1;
3056 else
3057 lp->height = plines_nofill(lp->lnum);
3058#else
3059 lp->height = plines(lp->lnum);
3060#endif
3061 h1 = lp->height;
3062 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003063 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 loff0 = *lp;
3066 if (dir > 0)
3067 botline_forw(lp);
3068 else
3069 topline_back(lp);
3070 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003071 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003073 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 return;
3075 }
3076
3077 loff1 = *lp;
3078 if (dir > 0)
3079 botline_forw(lp);
3080 else
3081 topline_back(lp);
3082 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003083 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003085 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 return;
3087 }
3088
3089 loff2 = *lp;
3090 if (dir > 0)
3091 botline_forw(lp);
3092 else
3093 topline_back(lp);
3094 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003095 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003096 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003098 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099}
3100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101/*
3102 * Scroll 'scroll' lines up or down.
3103 */
3104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003105halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 long scrolled = 0;
3108 int i;
3109 int n;
3110 int room;
3111
3112 if (Prenum)
3113 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3114 curwin->w_height : Prenum;
3115 n = (curwin->w_p_scr <= curwin->w_height) ?
3116 curwin->w_p_scr : curwin->w_height;
3117
Bram Moolenaard5d37532017-03-27 23:02:07 +02003118 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 validate_botline();
3120 room = curwin->w_empty_rows;
3121#ifdef FEAT_DIFF
3122 room += curwin->w_filler_rows;
3123#endif
3124 if (flag)
3125 {
3126 /*
3127 * scroll the text up
3128 */
3129 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3130 {
3131#ifdef FEAT_DIFF
3132 if (curwin->w_topfill > 0)
3133 {
3134 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003135 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 --curwin->w_topfill;
3137 }
3138 else
3139#endif
3140 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003141 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 n -= i;
3143 if (n < 0 && scrolled > 0)
3144 break;
3145#ifdef FEAT_FOLDING
3146 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3147#endif
3148 ++curwin->w_topline;
3149#ifdef FEAT_DIFF
3150 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3151#endif
3152
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3154 {
3155 ++curwin->w_cursor.lnum;
3156 curwin->w_valid &=
3157 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
3160 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3161 scrolled += i;
3162
3163 /*
3164 * Correct w_botline for changed w_topline.
3165 * Won't work when there are filler lines.
3166 */
3167#ifdef FEAT_DIFF
3168 if (curwin->w_p_diff)
3169 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3170 else
3171#endif
3172 {
3173 room += i;
3174 do
3175 {
3176 i = plines(curwin->w_botline);
3177 if (i > room)
3178 break;
3179#ifdef FEAT_FOLDING
3180 (void)hasFolding(curwin->w_botline, NULL,
3181 &curwin->w_botline);
3182#endif
3183 ++curwin->w_botline;
3184 room -= i;
3185 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3186 }
3187 }
3188
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 /*
3190 * When hit bottom of the file: move cursor down.
3191 */
3192 if (n > 0)
3193 {
3194# ifdef FEAT_FOLDING
3195 if (hasAnyFolding(curwin))
3196 {
3197 while (--n >= 0
3198 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3199 {
3200 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3201 &curwin->w_cursor.lnum);
3202 ++curwin->w_cursor.lnum;
3203 }
3204 }
3205 else
3206# endif
3207 curwin->w_cursor.lnum += n;
3208 check_cursor_lnum();
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 else
3212 {
3213 /*
3214 * scroll the text down
3215 */
3216 while (n > 0 && curwin->w_topline > 1)
3217 {
3218#ifdef FEAT_DIFF
3219 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3220 {
3221 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003222 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 ++curwin->w_topfill;
3224 }
3225 else
3226#endif
3227 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003228 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 n -= i;
3230 if (n < 0 && scrolled > 0)
3231 break;
3232 --curwin->w_topline;
3233#ifdef FEAT_FOLDING
3234 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3235#endif
3236#ifdef FEAT_DIFF
3237 curwin->w_topfill = 0;
3238#endif
3239 }
3240 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3241 VALID_BOTLINE|VALID_BOTLINE_AP);
3242 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (curwin->w_cursor.lnum > 1)
3244 {
3245 --curwin->w_cursor.lnum;
3246 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 /*
3251 * When hit top of the file: move cursor up.
3252 */
3253 if (n > 0)
3254 {
3255 if (curwin->w_cursor.lnum <= (linenr_T)n)
3256 curwin->w_cursor.lnum = 1;
3257 else
3258# ifdef FEAT_FOLDING
3259 if (hasAnyFolding(curwin))
3260 {
3261 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3262 {
3263 --curwin->w_cursor.lnum;
3264 (void)hasFolding(curwin->w_cursor.lnum,
3265 &curwin->w_cursor.lnum, NULL);
3266 }
3267 }
3268 else
3269# endif
3270 curwin->w_cursor.lnum -= n;
3271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 }
3273# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003274 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 foldAdjustCursor();
3276# endif
3277#ifdef FEAT_DIFF
3278 check_topfill(curwin, !flag);
3279#endif
3280 cursor_correct();
3281 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003282 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003284
Bram Moolenaar860cae12010-06-05 23:22:07 +02003285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003286do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003287{
3288 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003289 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003290 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003291 colnr_T curswant = curwin->w_curswant;
3292 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003293 win_T *old_curwin = curwin;
3294 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003295 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003296 int old_VIsual_select = VIsual_select;
3297 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003298
3299 /*
3300 * loop through the cursorbound windows
3301 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003302 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003303 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003304 {
3305 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003306 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003307 if (curwin != old_curwin && curwin->w_p_crb)
3308 {
3309# ifdef FEAT_DIFF
3310 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003311 curwin->w_cursor.lnum =
3312 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003313 else
3314# endif
3315 curwin->w_cursor.lnum = line;
3316 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003317 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003318 curwin->w_curswant = curswant;
3319 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003320
Bram Moolenaar85a20022019-12-21 18:25:54 +01003321 // Make sure the cursor is in a valid position. Temporarily set
3322 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003323 restart_edit_save = restart_edit;
3324 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003325 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003326
3327 // Avoid a scroll here for the cursor position, 'scrollbind' is
3328 // more important.
3329 if (!curwin->w_p_scb)
3330 validate_cursor();
3331
Bram Moolenaar61452852011-02-01 18:01:11 +01003332 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003333 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003334 if (has_mbyte)
3335 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003336 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003337
Bram Moolenaar85a20022019-12-21 18:25:54 +01003338 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003339 if (!curwin->w_p_scb)
3340 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003341 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003342 }
3343 }
3344
3345 /*
3346 * reset current-window
3347 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003348 VIsual_select = old_VIsual_select;
3349 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003350 curwin = old_curwin;
3351 curbuf = old_curbuf;
3352}