blob: 8a8af9a4e9b93202f48c3eb0b987f388d1e7a279 [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/*
39 * Compute wp->w_botline for the current wp->w_topline. Can be called after
40 * wp->w_topline changed.
41 */
42 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010043comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int n;
46 linenr_T lnum;
47 int done;
48#ifdef FEAT_FOLDING
49 linenr_T last;
50 int folded;
51#endif
52
53 /*
54 * If w_cline_row is valid, start there.
55 * Otherwise have to start at w_topline.
56 */
57 check_cursor_moved(wp);
58 if (wp->w_valid & VALID_CROW)
59 {
60 lnum = wp->w_cursor.lnum;
61 done = wp->w_cline_row;
62 }
63 else
64 {
65 lnum = wp->w_topline;
66 done = 0;
67 }
68
69 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
70 {
71#ifdef FEAT_FOLDING
72 last = lnum;
73 folded = FALSE;
74 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
75 {
76 n = 1;
77 folded = TRUE;
78 }
79 else
80#endif
81#ifdef FEAT_DIFF
82 if (lnum == wp->w_topline)
83 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
84 else
85#endif
86 n = plines_win(wp, lnum, TRUE);
87 if (
88#ifdef FEAT_FOLDING
89 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
90#else
91 lnum == wp->w_cursor.lnum
92#endif
93 )
94 {
95 wp->w_cline_row = done;
96 wp->w_cline_height = n;
97#ifdef FEAT_FOLDING
98 wp->w_cline_folded = folded;
99#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100100 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
102 }
103 if (done + n > wp->w_height)
104 break;
105 done += n;
106#ifdef FEAT_FOLDING
107 lnum = last;
108#endif
109 }
110
Bram Moolenaar85a20022019-12-21 18:25:54 +0100111 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
118/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100119 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
120 * set.
121 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100123redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100124{
125 if ((wp->w_p_rnu
126#ifdef FEAT_SYN_HL
127 || wp->w_p_cul
128#endif
129 )
130 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200131 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200132 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000133 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100134 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200135 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100136}
137
zeertzjq3e559cd2022-03-27 19:26:55 +0100138#ifdef FEAT_SYN_HL
139/*
140 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
141 * contains "screenline".
142 */
143 static void
144redraw_for_cursorcolumn(win_T *wp)
145{
146 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
147 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100148 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100149 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100150 redraw_win_later(wp, UPD_SOME_VALID);
151 // When 'cursorlineopt' contains "screenline" need to redraw with
152 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100153 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100154 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100155 }
156}
157#endif
158
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 * Update curwin->w_topline and redraw if necessary.
161 * Used to update the screen before printing a message.
162 */
163 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100164update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165{
166 update_topline();
167 if (must_redraw)
168 update_screen(0);
169}
170
171/*
172 * Update curwin->w_topline to move the cursor onto the screen.
173 */
174 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100175update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176{
177 long line_count;
178 int halfheight;
179 int n;
180 linenr_T old_topline;
181#ifdef FEAT_DIFF
182 int old_topfill;
183#endif
184#ifdef FEAT_FOLDING
185 linenr_T lnum;
186#endif
187 int check_topline = FALSE;
188 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100189 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100190 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar85a20022019-12-21 18:25:54 +0100192 // If there is no valid screen and when the window height is zero just use
193 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200194 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200195 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100196 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200197 curwin->w_topline = curwin->w_cursor.lnum;
198 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200199 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200200 return;
201 }
202
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 check_cursor_moved(curwin);
204 if (curwin->w_valid & VALID_TOPLINE)
205 return;
206
Bram Moolenaar85a20022019-12-21 18:25:54 +0100207 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000208 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100209 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
211 old_topline = curwin->w_topline;
212#ifdef FEAT_DIFF
213 old_topfill = curwin->w_topfill;
214#endif
215
216 /*
217 * If the buffer is empty, always set topline to 1.
218 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100219 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 {
221 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100222 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 curwin->w_botline = 2;
225 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 }
228
229 /*
230 * If the cursor is above or near the top of the window, scroll the window
231 * to show the line the cursor is in, with 'scrolloff' context.
232 */
233 else
234 {
235 if (curwin->w_topline > 1)
236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100237 // If the cursor is above topline, scrolling is always needed.
238 // If the cursor is far below topline and there is no folding,
239 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 if (curwin->w_cursor.lnum < curwin->w_topline)
241 check_topline = TRUE;
242 else if (check_top_offset())
243 check_topline = TRUE;
244 }
245#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100246 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
248 curwin->w_topline))
249 check_topline = TRUE;
250#endif
251
252 if (check_topline)
253 {
254 halfheight = curwin->w_height / 2 - 1;
255 if (halfheight < 2)
256 halfheight = 2;
257
258#ifdef FEAT_FOLDING
259 if (hasAnyFolding(curwin))
260 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100261 // Count the number of logical lines between the cursor and
262 // topline + scrolloff (approximation of how much will be
263 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 n = 0;
265 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100266 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 {
268 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100269 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
271 break;
272 (void)hasFolding(lnum, NULL, &lnum);
273 }
274 }
275 else
276#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100277 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
Bram Moolenaar85a20022019-12-21 18:25:54 +0100279 // If we weren't very close to begin with, we scroll to put the
280 // cursor in the middle of the window. Otherwise put the cursor
281 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 if (n >= halfheight)
283 scroll_cursor_halfway(FALSE);
284 else
285 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000286 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 check_botline = TRUE;
288 }
289 }
290
291 else
292 {
293#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
296#endif
297 check_botline = TRUE;
298 }
299 }
300
301 /*
302 * If the cursor is below the bottom of the window, scroll the window
303 * to put the cursor on the window.
304 * When w_botline is invalid, recompute it first, to avoid a redraw later.
305 * If w_botline was approximated, we might need a redraw later in a few
306 * cases, but we don't want to spend (a lot of) time recomputing w_botline
307 * for every small change.
308 */
309 if (check_botline)
310 {
311 if (!(curwin->w_valid & VALID_BOTLINE_AP))
312 validate_botline();
313
314 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
315 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000316 if (curwin->w_cursor.lnum < curwin->w_botline)
317 {
318 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100319 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifdef FEAT_FOLDING
321 || hasAnyFolding(curwin)
322#endif
323 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 lineoff_T loff;
326
Bram Moolenaar85a20022019-12-21 18:25:54 +0100327 // Cursor is (a few lines) above botline, check if there are
328 // 'scrolloff' window lines below the cursor. If not, need to
329 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 n = curwin->w_empty_rows;
331 loff.lnum = curwin->w_cursor.lnum;
332#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100333 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
335#endif
336#ifdef FEAT_DIFF
337 loff.fill = 0;
338 n += curwin->w_filler_rows;
339#endif
340 loff.height = 0;
341 while (loff.lnum < curwin->w_botline
342#ifdef FEAT_DIFF
343 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
344#endif
345 )
346 {
347 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100348 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 break;
350 botline_forw(&loff);
351 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100352 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100353 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000355 }
356 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000358 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360 if (check_botline)
361 {
362#ifdef FEAT_FOLDING
363 if (hasAnyFolding(curwin))
364 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100365 // Count the number of logical lines between the cursor and
366 // botline - scrolloff (approximation of how much will be
367 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 line_count = 0;
369 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100370 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
372 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (lnum <= 0 || line_count > curwin->w_height + 1)
375 break;
376 (void)hasFolding(lnum, &lnum, NULL);
377 }
378 }
379 else
380#endif
381 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100382 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000384 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 else
386 scroll_cursor_halfway(FALSE);
387 }
388 }
389 }
390 curwin->w_valid |= VALID_TOPLINE;
391
392 /*
393 * Need to redraw when topline changed.
394 */
395 if (curwin->w_topline != old_topline
396#ifdef FEAT_DIFF
397 || curwin->w_topfill != old_topfill
398#endif
399 )
400 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100401 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000402 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {
404 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100405 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100408 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 if (curwin->w_cursor.lnum == curwin->w_topline)
411 validate_cursor();
412 }
413
Bram Moolenaar375e3392019-01-31 18:26:10 +0100414 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415}
416
417/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000418 * Return the scrolljump value to use for the current window.
419 * When 'scrolljump' is positive use it as-is.
420 * When 'scrolljump' is negative use it as a percentage of the window height.
421 */
422 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100423scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000424{
425 if (p_sj >= 0)
426 return (int)p_sj;
427 return (curwin->w_height * -p_sj) / 100;
428}
429
430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
432 * current window.
433 */
434 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100435check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 lineoff_T loff;
438 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100439 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
Bram Moolenaar375e3392019-01-31 18:26:10 +0100441 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442#ifdef FEAT_FOLDING
443 || hasAnyFolding(curwin)
444#endif
445 )
446 {
447 loff.lnum = curwin->w_cursor.lnum;
448#ifdef FEAT_DIFF
449 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100450 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#else
452 n = 0;
453#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100454 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100455 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {
457 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100458 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 if (loff.lnum < curwin->w_topline
460#ifdef FEAT_DIFF
461 || (loff.lnum == curwin->w_topline && loff.fill > 0)
462#endif
463 )
464 break;
465 n += loff.height;
466 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100467 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 return TRUE;
469 }
470 return FALSE;
471}
472
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100473/*
474 * Update w_curswant.
475 */
476 void
477update_curswant_force(void)
478{
479 validate_virtcol();
480 curwin->w_curswant = curwin->w_virtcol
481#ifdef FEAT_PROP_POPUP
482 - curwin->w_virtcol_first_char
483#endif
484 ;
485 curwin->w_set_curswant = FALSE;
486}
487
488/*
489 * Update w_curswant if w_set_curswant is set.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100495 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496}
497
498/*
499 * Check if the cursor has moved. Set the w_valid flag accordingly.
500 */
501 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100502check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
504 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
505 {
506 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100507 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
508 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 wp->w_valid_cursor = wp->w_cursor;
510 wp->w_valid_leftcol = wp->w_leftcol;
511 }
512 else if (wp->w_cursor.col != wp->w_valid_cursor.col
513 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100514 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
516 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
517 wp->w_valid_cursor.col = wp->w_cursor.col;
518 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521}
522
523/*
524 * Call this function when some window settings have changed, which require
525 * the cursor position, botline and topline to be recomputed and the window to
526 * be redrawn. E.g, when changing the 'wrap' option or folding.
527 */
528 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100529changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530{
531 changed_window_setting_win(curwin);
532}
533
534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537 wp->w_lines_valid = 0;
538 changed_line_abv_curs_win(wp);
539 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100540 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541}
542
543/*
544 * Set wp->w_topline to a certain number.
545 */
546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100547set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200549#ifdef FEAT_DIFF
550 linenr_T prev_topline = wp->w_topline;
551#endif
552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100554 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
556#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100557 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100559 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
560 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000562 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200564 if (lnum != prev_topline)
565 // Keep the filler lines when the topline didn't change.
566 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
568 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100569 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100570 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571}
572
573/*
574 * Call this function when the length of the cursor line (in screen
575 * characters) has changed, and the change is before the cursor.
576 * Need to take care of w_botline separately!
577 */
578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100579changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
581 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
582 |VALID_CHEIGHT|VALID_TOPLINE);
583}
584
585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100586changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
589 |VALID_CHEIGHT|VALID_TOPLINE);
590}
591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592/*
593 * Call this function when the length of a line (in screen characters) above
594 * the cursor have changed.
595 * Need to take care of w_botline separately!
596 */
597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
601 |VALID_CHEIGHT|VALID_TOPLINE);
602}
603
604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100605changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
608 |VALID_CHEIGHT|VALID_TOPLINE);
609}
610
611/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100612 * Display of line has changed for "buf", invalidate cursor position and
613 * w_botline.
614 */
615 void
616changed_line_display_buf(buf_T *buf)
617{
618 win_T *wp;
619
620 FOR_ALL_WINDOWS(wp)
621 if (wp->w_buffer == buf)
622 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
623 |VALID_CROW|VALID_CHEIGHT
624 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
625}
626
627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 * Make sure the value of curwin->w_botline is valid.
629 */
630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100631validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100633 validate_botline_win(curwin);
634}
635
636/*
637 * Make sure the value of wp->w_botline is valid.
638 */
639 void
640validate_botline_win(win_T *wp)
641{
642 if (!(wp->w_valid & VALID_BOTLINE))
643 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644}
645
646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 * Mark curwin->w_botline as invalid (because of some change in the buffer).
648 */
649 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100650invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
653}
654
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
659}
660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100662approximate_botline_win(
663 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664{
665 wp->w_valid &= ~VALID_BOTLINE;
666}
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668/*
669 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
670 */
671 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100672cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 check_cursor_moved(curwin);
675 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
676 (VALID_WROW|VALID_WCOL));
677}
678
679/*
680 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
681 * w_topline must be valid, you may need to call update_topline() first!
682 */
683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100684validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100686 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 check_cursor_moved(curwin);
688 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
689 curs_columns(TRUE);
690}
691
692#if defined(FEAT_GUI) || defined(PROTO)
693/*
694 * validate w_cline_row.
695 */
696 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100697validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
699 /*
700 * First make sure that w_topline is valid (after moving the cursor).
701 */
702 update_topline();
703 check_cursor_moved(curwin);
704 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100705 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706}
707#endif
708
709/*
710 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200711 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 */
713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100714curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 linenr_T lnum;
717 int i;
718 int all_invalid;
719 int valid;
720#ifdef FEAT_FOLDING
721 long fold_count;
722#endif
723
Bram Moolenaar85a20022019-12-21 18:25:54 +0100724 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 all_invalid = (!redrawing()
726 || wp->w_lines_valid == 0
727 || wp->w_lines[0].wl_lnum > wp->w_topline);
728 i = 0;
729 wp->w_cline_row = 0;
730 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
731 {
732 valid = FALSE;
733 if (!all_invalid && i < wp->w_lines_valid)
734 {
735 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100736 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (wp->w_lines[i].wl_lnum == lnum)
738 {
739#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 // Check for newly inserted lines below this row, in which
741 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 if (!wp->w_buffer->b_mod_set
743 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
744 || wp->w_buffer->b_mod_top
745 > wp->w_lines[i].wl_lastlnum + 1)
746#endif
747 valid = TRUE;
748 }
749 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100750 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752 if (valid
753#ifdef FEAT_DIFF
754 && (lnum != wp->w_topline || !wp->w_p_diff)
755#endif
756 )
757 {
758#ifdef FEAT_FOLDING
759 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100760 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (lnum > wp->w_cursor.lnum)
762 break;
763#else
764 ++lnum;
765#endif
766 wp->w_cline_row += wp->w_lines[i].wl_size;
767 }
768 else
769 {
770#ifdef FEAT_FOLDING
771 fold_count = foldedCount(wp, lnum, NULL);
772 if (fold_count)
773 {
774 lnum += fold_count;
775 if (lnum > wp->w_cursor.lnum)
776 break;
777 ++wp->w_cline_row;
778 }
779 else
780#endif
781#ifdef FEAT_DIFF
782 if (lnum == wp->w_topline)
783 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
784 + wp->w_topfill;
785 else
786#endif
787 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
788 }
789 }
790
791 check_cursor_moved(wp);
792 if (!(wp->w_valid & VALID_CHEIGHT))
793 {
794 if (all_invalid
795 || i == wp->w_lines_valid
796 || (i < wp->w_lines_valid
797 && (!wp->w_lines[i].wl_valid
798 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
799 {
800#ifdef FEAT_DIFF
801 if (wp->w_cursor.lnum == wp->w_topline)
802 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
803 TRUE) + wp->w_topfill;
804 else
805#endif
806 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
807#ifdef FEAT_FOLDING
808 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
809 NULL, NULL, TRUE, NULL);
810#endif
811 }
812 else if (i > wp->w_lines_valid)
813 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100814 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 wp->w_cline_height = 0;
816#ifdef FEAT_FOLDING
817 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
818 NULL, NULL, TRUE, NULL);
819#endif
820 }
821 else
822 {
823 wp->w_cline_height = wp->w_lines[i].wl_size;
824#ifdef FEAT_FOLDING
825 wp->w_cline_folded = wp->w_lines[i].wl_folded;
826#endif
827 }
828 }
829
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100830 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834
835/*
836 * Validate curwin->w_virtcol only.
837 */
838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100839validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841 validate_virtcol_win(curwin);
842}
843
844/*
845 * Validate wp->w_virtcol only.
846 */
847 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100848validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
850 check_cursor_moved(wp);
851 if (!(wp->w_valid & VALID_VIRTCOL))
852 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100853#ifdef FEAT_PROP_POPUP
854 wp->w_virtcol_first_char = 0;
855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000857#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100858 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000859#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100860 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
862}
863
864/*
865 * Validate curwin->w_cline_height only.
866 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100868validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 check_cursor_moved(curwin);
871 if (!(curwin->w_valid & VALID_CHEIGHT))
872 {
873#ifdef FEAT_DIFF
874 if (curwin->w_cursor.lnum == curwin->w_topline)
875 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
876 + curwin->w_topfill;
877 else
878#endif
879 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
880#ifdef FEAT_FOLDING
881 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
882#endif
883 curwin->w_valid |= VALID_CHEIGHT;
884 }
885}
886
887/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000888 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 */
890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100891validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 colnr_T off;
894 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100895 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 validate_virtcol();
898 if (!(curwin->w_valid & VALID_WCOL))
899 {
900 col = curwin->w_virtcol;
901 off = curwin_col_off();
902 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200903 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar85a20022019-12-21 18:25:54 +0100905 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000906 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200907 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100908 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100909 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200910 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000911 if (col > (int)curwin->w_leftcol)
912 col -= curwin->w_leftcol;
913 else
914 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100918#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100919 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922}
923
924/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200925 * Compute offset of a window, occupied by absolute or relative line number,
926 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 */
928 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100929win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
Bram Moolenaar64486672010-05-16 15:46:46 +0200931 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932#ifdef FEAT_CMDWIN
933 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
934#endif
935#ifdef FEAT_FOLDING
936 + wp->w_p_fdc
937#endif
938#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200939 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#endif
941 );
942}
943
944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100945curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 return win_col_off(curwin);
948}
949
950/*
951 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200952 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
953 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 */
955 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100956win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
Bram Moolenaar64486672010-05-16 15:46:46 +0200958 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000959 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 return 0;
961}
962
963 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100964curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 return win_col_off2(curwin);
967}
968
969/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100970 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 * Also updates curwin->w_wrow and curwin->w_cline_row.
972 * Also updates curwin->w_leftcol.
973 */
974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100975curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100976 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977{
978 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100979 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 int off_left, off_right;
981 int n;
982 int p_lines;
983 int width = 0;
984 int textwidth;
985 int new_leftcol;
986 colnr_T startcol;
987 colnr_T endcol;
988 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100989 long so = get_scrolloff_value();
990 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 /*
993 * First make sure that w_topline is valid (after moving the cursor).
994 */
Luuk van Baalfaf1d412022-09-19 16:45:29 +0100995 if (!skip_update_topline)
Luuk van Baal29ab5242022-09-11 16:59:53 +0100996 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 /*
999 * Next make sure that w_cline_row is valid.
1000 */
1001 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001002 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001004#ifdef FEAT_PROP_POPUP
1005 // will be set by getvvcol() but not reset
1006 curwin->w_virtcol_first_char = 0;
1007#endif
1008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /*
1010 * Compute the number of virtual columns.
1011 */
1012#ifdef FEAT_FOLDING
1013 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001014 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1016 else
1017#endif
1018 getvvcol(curwin, &curwin->w_cursor,
1019 &startcol, &(curwin->w_virtcol), &endcol);
1020
Bram Moolenaar85a20022019-12-21 18:25:54 +01001021 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001023 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
1025 extra = curwin_col_off();
1026 curwin->w_wcol = curwin->w_virtcol + extra;
1027 endcol += extra;
1028
1029 /*
1030 * Now compute w_wrow, counting screen lines from w_cline_row.
1031 */
1032 curwin->w_wrow = curwin->w_cline_row;
1033
Bram Moolenaar02631462017-09-22 15:20:32 +02001034 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (textwidth <= 0)
1036 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001037 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001038 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001039 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001040 if (curwin->w_p_wrap)
1041 curwin->w_wrow = curwin->w_height - 1;
1042 else
1043 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001045 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
1047 width = textwidth + curwin_col_off2();
1048
Bram Moolenaar85a20022019-12-21 18:25:54 +01001049 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001050 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001052#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001053 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001054#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001055
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001057 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 curwin->w_wcol -= n * width;
1059 curwin->w_wrow += n;
1060
1061#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001062 // When cursor wraps to first char of next line in Insert
1063 // mode, the 'showbreak' string isn't shown, backup to first
1064 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001065 sbr = get_showbreak_value(curwin);
1066 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001067 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 curwin->w_wcol = 0;
1069#endif
1070 }
1071 }
1072
Bram Moolenaar85a20022019-12-21 18:25:54 +01001073 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1074 // is not folded.
1075 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001076 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#ifdef FEAT_FOLDING
1078 && !curwin->w_cline_folded
1079#endif
1080 )
1081 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001082#ifdef FEAT_PROP_POPUP
1083 if (curwin->w_virtcol_first_char > 0)
1084 {
1085 int cols = (curwin->w_width - extra);
1086 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1087
1088 // each "above" text prop shifts the text one row down
1089 curwin->w_wrow += rows;
1090 curwin->w_wcol -= rows * cols;
1091 endcol -= rows * cols;
1092 curwin->w_cline_height = rows + 1;
1093 }
1094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 /*
1096 * If Cursor is left of the screen, scroll rightwards.
1097 * If Cursor is right of the screen, scroll leftwards
1098 * If we get closer to the edge than 'sidescrolloff', scroll a little
1099 * extra
1100 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001101 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001102 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001103 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (off_left < 0 || off_right > 0)
1105 {
1106 if (off_left < 0)
1107 diff = -off_left;
1108 else
1109 diff = off_right;
1110
Bram Moolenaar85a20022019-12-21 18:25:54 +01001111 // When far off or not enough room on either side, put cursor in
1112 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1114 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1115 else
1116 {
1117 if (diff < p_ss)
1118 diff = p_ss;
1119 if (off_left < 0)
1120 new_leftcol = curwin->w_leftcol - diff;
1121 else
1122 new_leftcol = curwin->w_leftcol + diff;
1123 }
1124 if (new_leftcol < 0)
1125 new_leftcol = 0;
1126 if (new_leftcol != (int)curwin->w_leftcol)
1127 {
1128 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001129 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001130 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 }
1133 curwin->w_wcol -= curwin->w_leftcol;
1134 }
1135 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1136 curwin->w_wcol -= curwin->w_leftcol;
1137 else
1138 curwin->w_wcol = 0;
1139
1140#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001141 // Skip over filler lines. At the top use w_topfill, there
1142 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 if (curwin->w_cursor.lnum == curwin->w_topline)
1144 curwin->w_wrow += curwin->w_topfill;
1145 else
1146 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1147#endif
1148
1149 prev_skipcol = curwin->w_skipcol;
1150
1151 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if ((curwin->w_wrow >= curwin->w_height
1154 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001155 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 && (p_lines =
1157#ifdef FEAT_DIFF
1158 plines_win_nofill
1159#else
1160 plines_win
1161#endif
1162 (curwin, curwin->w_cursor.lnum, FALSE))
1163 - 1 >= curwin->w_height))
1164 && curwin->w_height != 0
1165 && curwin->w_cursor.lnum == curwin->w_topline
1166 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001167 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001169 // Cursor past end of screen. Happens with a single line that does
1170 // not fit on screen. Find a skipcol to show the text around the
1171 // cursor. Avoid scrolling all the time. compute value of "extra":
1172 // 1: Less than 'scrolloff' lines above
1173 // 2: Less than 'scrolloff' lines below
1174 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001176 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001178 // Compute last display line of the buffer line that we want at the
1179 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (p_lines == 0)
1181 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1182 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001183 if (p_lines > curwin->w_wrow + so)
1184 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 else
1186 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001187 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 extra += 2;
1189
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001190 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001192 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 n = curwin->w_virtcol / width;
1194 if (n > curwin->w_height / 2)
1195 n -= curwin->w_height / 2;
1196 else
1197 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001198 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (n > p_lines - curwin->w_height + 1)
1200 n = p_lines - curwin->w_height + 1;
1201 curwin->w_skipcol = n * width;
1202 }
1203 else if (extra == 1)
1204 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001205 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001206 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 + width - 1) / width;
1208 if (extra > 0)
1209 {
1210 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1211 extra = curwin->w_skipcol / width;
1212 curwin->w_skipcol -= extra * width;
1213 }
1214 }
1215 else if (extra == 2)
1216 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001217 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 endcol = (n - curwin->w_height + 1) * width;
1219 while (endcol > curwin->w_virtcol)
1220 endcol -= width;
1221 if (endcol > curwin->w_skipcol)
1222 curwin->w_skipcol = endcol;
1223 }
1224
1225 curwin->w_wrow -= curwin->w_skipcol / width;
1226 if (curwin->w_wrow >= curwin->w_height)
1227 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001228 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 extra = curwin->w_wrow - curwin->w_height + 1;
1230 curwin->w_skipcol += extra * width;
1231 curwin->w_wrow -= extra;
1232 }
1233
1234 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1235 if (extra > 0)
1236 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1237 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001238 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240 else
1241 curwin->w_skipcol = 0;
1242 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001243 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001245#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001246 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001247#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001248#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1249 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1250 {
1251 curwin->w_wrow += popup_top_extra(curwin);
1252 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001253 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001254 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001255 else
1256 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001257#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001258
Bram Moolenaar08f23632019-11-16 14:19:33 +01001259 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1260 curwin->w_valid_leftcol = curwin->w_leftcol;
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1263}
1264
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001265#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001266/*
1267 * Compute the screen position of text character at "pos" in window "wp"
1268 * The resulting values are one-based, zero when character is not visible.
1269 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001270 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001271textpos2screenpos(
1272 win_T *wp,
1273 pos_T *pos,
1274 int *rowp, // screen row
1275 int *scolp, // start screen column
1276 int *ccolp, // cursor screen column
1277 int *ecolp) // end screen column
1278{
1279 colnr_T scol = 0, ccol = 0, ecol = 0;
1280 int row = 0;
1281 int rowoff = 0;
1282 colnr_T coloff = 0;
1283
Bram Moolenaar189663b2021-07-21 18:04:56 +02001284 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001285 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001286 colnr_T off;
1287 colnr_T col;
1288 int width;
1289 linenr_T lnum = pos->lnum;
1290#ifdef FEAT_FOLDING
1291 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001292
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001293 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1294#endif
1295 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1296#ifdef FEAT_FOLDING
1297 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001298 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001299 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001300 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001301 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001302 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001303#endif
1304 {
1305 getvcol(wp, pos, &scol, &ccol, &ecol);
1306
1307 // similar to what is done in validate_cursor_col()
1308 col = scol;
1309 off = win_col_off(wp);
1310 col += off;
1311 width = wp->w_width - off + win_col_off2(wp);
1312
1313 // long line wrapping, adjust row
1314 if (wp->w_p_wrap
1315 && col >= (colnr_T)wp->w_width
1316 && width > 0)
1317 {
1318 // use same formula as what is used in curs_columns()
1319 rowoff = ((col - wp->w_width) / width + 1);
1320 col -= rowoff * width;
1321 }
1322 col -= wp->w_leftcol;
1323 if (col >= wp->w_width)
1324 col = -1;
1325 if (col >= 0 && row + rowoff <= wp->w_height)
1326 {
1327 coloff = col - scol + wp->w_wincol + 1;
1328 row += W_WINROW(wp);
1329 }
1330 else
1331 // character is left, right or below of the window
1332 row = rowoff = scol = ccol = ecol = 0;
1333 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001334 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001335 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001336 *scolp = scol + coloff;
1337 *ccolp = ccol + coloff;
1338 *ecolp = ecol + coloff;
1339}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001340#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001341
Bram Moolenaar12034e22019-08-25 22:25:02 +02001342#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001343/*
1344 * "screenpos({winid}, {lnum}, {col})" function
1345 */
1346 void
1347f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1348{
1349 dict_T *dict;
1350 win_T *wp;
1351 pos_T pos;
1352 int row = 0;
1353 int scol = 0, ccol = 0, ecol = 0;
1354
Bram Moolenaar93a10962022-06-16 11:42:09 +01001355 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001356 return;
1357 dict = rettv->vval.v_dict;
1358
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001359 if (in_vim9script()
1360 && (check_for_number_arg(argvars, 0) == FAIL
1361 || check_for_number_arg(argvars, 1) == FAIL
1362 || check_for_number_arg(argvars, 2) == FAIL))
1363 return;
1364
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001365 wp = find_win_by_nr_or_id(&argvars[0]);
1366 if (wp == NULL)
1367 return;
1368
1369 pos.lnum = tv_get_number(&argvars[1]);
1370 pos.col = tv_get_number(&argvars[2]) - 1;
1371 pos.coladd = 0;
1372 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1373
1374 dict_add_number(dict, "row", row);
1375 dict_add_number(dict, "col", scol);
1376 dict_add_number(dict, "curscol", ccol);
1377 dict_add_number(dict, "endcol", ecol);
1378}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001379
1380/*
1381 * "virtcol2col({winid}, {lnum}, {col})" function
1382 */
1383 void
1384f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1385{
1386 win_T *wp;
1387 linenr_T lnum;
1388 int screencol;
1389 int error = FALSE;
1390
1391 rettv->vval.v_number = -1;
1392
1393 if (check_for_number_arg(argvars, 0) == FAIL
1394 || check_for_number_arg(argvars, 1) == FAIL
1395 || check_for_number_arg(argvars, 2) == FAIL)
1396 return;
1397
1398 wp = find_win_by_nr_or_id(&argvars[0]);
1399 if (wp == NULL)
1400 return;
1401
1402 lnum = tv_get_number_chk(&argvars[1], &error);
1403 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1404 return;
1405
1406 screencol = tv_get_number_chk(&argvars[2], &error);
1407 if (error || screencol < 0)
1408 return;
1409
1410 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1411}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001412#endif
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414/*
1415 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001418scrolldown(
1419 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001420 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001422 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 int wrow;
1424 int moved = FALSE;
1425
1426#ifdef FEAT_FOLDING
1427 linenr_T first;
1428
Bram Moolenaar85a20022019-12-21 18:25:54 +01001429 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1431#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001432 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 while (line_count-- > 0)
1434 {
1435#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001436 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1437 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
1439 ++curwin->w_topfill;
1440 ++done;
1441 }
1442 else
1443#endif
1444 {
1445 if (curwin->w_topline == 1)
1446 break;
1447 --curwin->w_topline;
1448#ifdef FEAT_DIFF
1449 curwin->w_topfill = 0;
1450#endif
1451#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001452 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (hasFolding(curwin->w_topline, &first, NULL))
1454 {
1455 ++done;
1456 if (!byfold)
1457 line_count -= curwin->w_topline - first - 1;
1458 curwin->w_botline -= curwin->w_topline - first;
1459 curwin->w_topline = first;
1460 }
1461 else
1462#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001463 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001465 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 invalidate_botline();
1467 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001468 curwin->w_wrow += done; // keep w_wrow updated
1469 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
1471#ifdef FEAT_DIFF
1472 if (curwin->w_cursor.lnum == curwin->w_topline)
1473 curwin->w_cline_row = 0;
1474 check_topfill(curwin, TRUE);
1475#endif
1476
1477 /*
1478 * Compute the row number of the last row of the cursor line
1479 * and move the cursor onto the displayed part of the window.
1480 */
1481 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001482 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 validate_virtcol();
1485 validate_cheight();
1486 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001487 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
1489 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1490 {
1491#ifdef FEAT_FOLDING
1492 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1493 {
1494 --wrow;
1495 if (first == 1)
1496 curwin->w_cursor.lnum = 1;
1497 else
1498 curwin->w_cursor.lnum = first - 1;
1499 }
1500 else
1501#endif
1502 wrow -= plines(curwin->w_cursor.lnum--);
1503 curwin->w_valid &=
1504 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1505 moved = TRUE;
1506 }
1507 if (moved)
1508 {
1509#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001510 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 foldAdjustCursor();
1512#endif
1513 coladvance(curwin->w_curswant);
1514 }
1515}
1516
1517/*
1518 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001521scrollup(
1522 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001523 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1526 linenr_T lnum;
1527
1528 if (
1529# ifdef FEAT_FOLDING
1530 (byfold && hasAnyFolding(curwin))
1531# ifdef FEAT_DIFF
1532 ||
1533# endif
1534# endif
1535# ifdef FEAT_DIFF
1536 curwin->w_p_diff
1537# endif
1538 )
1539 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001540 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 lnum = curwin->w_topline;
1542 while (line_count--)
1543 {
1544# ifdef FEAT_DIFF
1545 if (curwin->w_topfill > 0)
1546 --curwin->w_topfill;
1547 else
1548# endif
1549 {
1550# ifdef FEAT_FOLDING
1551 if (byfold)
1552 (void)hasFolding(lnum, NULL, &lnum);
1553# endif
1554 if (lnum >= curbuf->b_ml.ml_line_count)
1555 break;
1556 ++lnum;
1557# ifdef FEAT_DIFF
1558 curwin->w_topfill = diff_check_fill(curwin, lnum);
1559# endif
1560 }
1561 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001562 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 curwin->w_botline += lnum - curwin->w_topline;
1564 curwin->w_topline = lnum;
1565 }
1566 else
1567#endif
1568 {
1569 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001570 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572
1573 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1574 curwin->w_topline = curbuf->b_ml.ml_line_count;
1575 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1576 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1577
1578#ifdef FEAT_DIFF
1579 check_topfill(curwin, FALSE);
1580#endif
1581
1582#ifdef FEAT_FOLDING
1583 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001584 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1586#endif
1587
1588 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1589 if (curwin->w_cursor.lnum < curwin->w_topline)
1590 {
1591 curwin->w_cursor.lnum = curwin->w_topline;
1592 curwin->w_valid &=
1593 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1594 coladvance(curwin->w_curswant);
1595 }
1596}
1597
1598#ifdef FEAT_DIFF
1599/*
1600 * Don't end up with too many filler lines in the window.
1601 */
1602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001603check_topfill(
1604 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001605 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 int n;
1608
1609 if (wp->w_topfill > 0)
1610 {
1611 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1612 if (wp->w_topfill + n > wp->w_height)
1613 {
1614 if (down && wp->w_topline > 1)
1615 {
1616 --wp->w_topline;
1617 wp->w_topfill = 0;
1618 }
1619 else
1620 {
1621 wp->w_topfill = wp->w_height - n;
1622 if (wp->w_topfill < 0)
1623 wp->w_topfill = 0;
1624 }
1625 }
1626 }
1627}
1628
1629/*
1630 * Use as many filler lines as possible for w_topline. Make sure w_topline
1631 * is still visible.
1632 */
1633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001634max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
1636 int n;
1637
1638 n = plines_nofill(curwin->w_topline);
1639 if (n >= curwin->w_height)
1640 curwin->w_topfill = 0;
1641 else
1642 {
1643 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1644 if (curwin->w_topfill + n > curwin->w_height)
1645 curwin->w_topfill = curwin->w_height - n;
1646 }
1647}
1648#endif
1649
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650/*
1651 * Scroll the screen one line down, but don't do it if it would move the
1652 * cursor off the screen.
1653 */
1654 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001655scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 int end_row;
1658#ifdef FEAT_DIFF
1659 int can_fill = (curwin->w_topfill
1660 < diff_check_fill(curwin, curwin->w_topline));
1661#endif
1662
1663 if (curwin->w_topline <= 1
1664#ifdef FEAT_DIFF
1665 && !can_fill
1666#endif
1667 )
1668 return;
1669
Bram Moolenaar85a20022019-12-21 18:25:54 +01001670 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672 /*
1673 * Compute the row number of the last row of the cursor line
1674 * and make sure it doesn't go off the screen. Make sure the cursor
1675 * doesn't go past 'scrolloff' lines from the screen end.
1676 */
1677 end_row = curwin->w_wrow;
1678#ifdef FEAT_DIFF
1679 if (can_fill)
1680 ++end_row;
1681 else
1682 end_row += plines_nofill(curwin->w_topline - 1);
1683#else
1684 end_row += plines(curwin->w_topline - 1);
1685#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001686 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 validate_cheight();
1689 validate_virtcol();
1690 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001691 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001693 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695#ifdef FEAT_DIFF
1696 if (can_fill)
1697 {
1698 ++curwin->w_topfill;
1699 check_topfill(curwin, TRUE);
1700 }
1701 else
1702 {
1703 --curwin->w_topline;
1704 curwin->w_topfill = 0;
1705 }
1706#else
1707 --curwin->w_topline;
1708#endif
1709#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001710 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001712 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1714 }
1715}
1716
1717/*
1718 * Scroll the screen one line up, but don't do it if it would move the cursor
1719 * off the screen.
1720 */
1721 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001722scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 int start_row;
1725
1726 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1727#ifdef FEAT_DIFF
1728 && curwin->w_topfill == 0
1729#endif
1730 )
1731 return;
1732
Bram Moolenaar85a20022019-12-21 18:25:54 +01001733 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
1735 /*
1736 * Compute the row number of the first row of the cursor line
1737 * and make sure it doesn't go off the screen. Make sure the cursor
1738 * doesn't go before 'scrolloff' lines from the screen start.
1739 */
1740#ifdef FEAT_DIFF
1741 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1742 - curwin->w_topfill;
1743#else
1744 start_row = curwin->w_wrow - plines(curwin->w_topline);
1745#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001746 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 {
1748 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001749 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001751 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
1753#ifdef FEAT_DIFF
1754 if (curwin->w_topfill > 0)
1755 --curwin->w_topfill;
1756 else
1757#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001758 {
1759#ifdef FEAT_FOLDING
1760 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001763 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001764 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1766 }
1767}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769/*
1770 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1771 * a (wrapped) text line. Uses and sets "lp->fill".
1772 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001773 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 */
1775 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001776topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778#ifdef FEAT_DIFF
1779 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1780 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001781 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 ++lp->fill;
1783 lp->height = 1;
1784 }
1785 else
1786#endif
1787 {
1788 --lp->lnum;
1789#ifdef FEAT_DIFF
1790 lp->fill = 0;
1791#endif
1792 if (lp->lnum < 1)
1793 lp->height = MAXCOL;
1794 else
1795#ifdef FEAT_FOLDING
1796 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001797 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 lp->height = 1;
1799 else
1800#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001801 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803}
1804
1805/*
1806 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1807 * a (wrapped) text line. Uses and sets "lp->fill".
1808 * Returns the height of the added line in "lp->height".
1809 * Lines below the last one are incredibly high.
1810 */
1811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001812botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
1814#ifdef FEAT_DIFF
1815 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1816 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001817 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 ++lp->fill;
1819 lp->height = 1;
1820 }
1821 else
1822#endif
1823 {
1824 ++lp->lnum;
1825#ifdef FEAT_DIFF
1826 lp->fill = 0;
1827#endif
1828 if (lp->lnum > curbuf->b_ml.ml_line_count)
1829 lp->height = MAXCOL;
1830 else
1831#ifdef FEAT_FOLDING
1832 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001833 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 lp->height = 1;
1835 else
1836#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001837 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 }
1839}
1840
1841#ifdef FEAT_DIFF
1842/*
1843 * Switch from including filler lines below lp->lnum to including filler
1844 * lines above loff.lnum + 1. This keeps pointing to the same line.
1845 * When there are no filler lines nothing changes.
1846 */
1847 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001848botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 if (lp->fill > 0)
1851 {
1852 ++lp->lnum;
1853 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1854 }
1855}
1856
1857/*
1858 * Switch from including filler lines above lp->lnum to including filler
1859 * lines below loff.lnum - 1. This keeps pointing to the same line.
1860 * When there are no filler lines nothing changes.
1861 */
1862 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001863topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
1865 if (lp->fill > 0)
1866 {
1867 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1868 --lp->lnum;
1869 }
1870}
1871#endif
1872
1873/*
1874 * Recompute topline to put the cursor at the top of the window.
1875 * Scroll at least "min_scroll" lines.
1876 * If "always" is TRUE, always set topline (for "zt").
1877 */
1878 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001879scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 int scrolled = 0;
1882 int extra = 0;
1883 int used;
1884 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001885 linenr_T top; // just above displayed lines
1886 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 linenr_T old_topline = curwin->w_topline;
1888#ifdef FEAT_DIFF
1889 linenr_T old_topfill = curwin->w_topfill;
1890#endif
1891 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001892 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (mouse_dragging > 0)
1895 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 /*
1898 * Decrease topline until:
1899 * - it has become 1
1900 * - (part of) the cursor line is moved off the screen or
1901 * - moved at least 'scrolljump' lines and
1902 * - at least 'scrolloff' lines above and below the cursor
1903 */
1904 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001905 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (curwin->w_cursor.lnum < curwin->w_topline)
1907 scrolled = used;
1908
1909#ifdef FEAT_FOLDING
1910 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1911 {
1912 --top;
1913 ++bot;
1914 }
1915 else
1916#endif
1917 {
1918 top = curwin->w_cursor.lnum - 1;
1919 bot = curwin->w_cursor.lnum + 1;
1920 }
1921 new_topline = top + 1;
1922
1923#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001924 // "used" already contains the number of filler lines above, don't add it
1925 // again.
1926 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001927 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#endif
1929
1930 /*
1931 * Check if the lines from "top" to "bot" fit in the window. If they do,
1932 * set new_topline and advance "top" and "bot" to include more lines.
1933 */
1934 while (top > 0)
1935 {
1936#ifdef FEAT_FOLDING
1937 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001938 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 i = 1;
1940 else
1941#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001942 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 used += i;
1944 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1945 {
1946#ifdef FEAT_FOLDING
1947 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001948 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 ++used;
1950 else
1951#endif
1952 used += plines(bot);
1953 }
1954 if (used > curwin->w_height)
1955 break;
1956 if (top < curwin->w_topline)
1957 scrolled += i;
1958
1959 /*
1960 * If scrolling is needed, scroll at least 'sj' lines.
1961 */
1962 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1963 && extra >= off)
1964 break;
1965
1966 extra += i;
1967 new_topline = top;
1968 --top;
1969 ++bot;
1970 }
1971
1972 /*
1973 * If we don't have enough space, put cursor in the middle.
1974 * This makes sure we get the same position when using "k" and "j"
1975 * in a small window.
1976 */
1977 if (used > curwin->w_height)
1978 scroll_cursor_halfway(FALSE);
1979 else
1980 {
1981 /*
1982 * If "always" is FALSE, only adjust topline to a lower value, higher
1983 * value may happen with wrapping lines
1984 */
1985 if (new_topline < curwin->w_topline || always)
1986 curwin->w_topline = new_topline;
1987 if (curwin->w_topline > curwin->w_cursor.lnum)
1988 curwin->w_topline = curwin->w_cursor.lnum;
1989#ifdef FEAT_DIFF
1990 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1991 if (curwin->w_topfill > 0 && extra > off)
1992 {
1993 curwin->w_topfill -= extra - off;
1994 if (curwin->w_topfill < 0)
1995 curwin->w_topfill = 0;
1996 }
1997 check_topfill(curwin, FALSE);
1998#endif
1999 if (curwin->w_topline != old_topline
2000#ifdef FEAT_DIFF
2001 || curwin->w_topfill != old_topfill
2002#endif
2003 )
2004 curwin->w_valid &=
2005 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2006 curwin->w_valid |= VALID_TOPLINE;
2007 }
2008}
2009
2010/*
2011 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2012 * screen lines for text lines.
2013 */
2014 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002015set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017#ifdef FEAT_DIFF
2018 wp->w_filler_rows = 0;
2019#endif
2020 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002021 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 else
2023 {
2024 wp->w_empty_rows = wp->w_height - used;
2025#ifdef FEAT_DIFF
2026 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2027 {
2028 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2029 if (wp->w_empty_rows > wp->w_filler_rows)
2030 wp->w_empty_rows -= wp->w_filler_rows;
2031 else
2032 {
2033 wp->w_filler_rows = wp->w_empty_rows;
2034 wp->w_empty_rows = 0;
2035 }
2036 }
2037#endif
2038 }
2039}
2040
2041/*
2042 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002043 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2045 * This is messy stuff!!!
2046 */
2047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002048scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 int used;
2051 int scrolled = 0;
2052 int extra = 0;
2053 int i;
2054 linenr_T line_count;
2055 linenr_T old_topline = curwin->w_topline;
2056 lineoff_T loff;
2057 lineoff_T boff;
2058#ifdef FEAT_DIFF
2059 int old_topfill = curwin->w_topfill;
2060 int fill_below_window;
2061#endif
2062 linenr_T old_botline = curwin->w_botline;
2063 linenr_T old_valid = curwin->w_valid;
2064 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002065 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002066 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 cln = curwin->w_cursor.lnum;
2069 if (set_topbot)
2070 {
2071 used = 0;
2072 curwin->w_botline = cln + 1;
2073#ifdef FEAT_DIFF
2074 loff.fill = 0;
2075#endif
2076 for (curwin->w_topline = curwin->w_botline;
2077 curwin->w_topline > 1;
2078 curwin->w_topline = loff.lnum)
2079 {
2080 loff.lnum = curwin->w_topline;
2081 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002082 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 break;
2084 used += loff.height;
2085#ifdef FEAT_DIFF
2086 curwin->w_topfill = loff.fill;
2087#endif
2088 }
2089 set_empty_rows(curwin, used);
2090 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2091 if (curwin->w_topline != old_topline
2092#ifdef FEAT_DIFF
2093 || curwin->w_topfill != old_topfill
2094#endif
2095 )
2096 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2097 }
2098 else
2099 validate_botline();
2100
Bram Moolenaar85a20022019-12-21 18:25:54 +01002101 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102#ifdef FEAT_DIFF
2103 used = plines_nofill(cln);
2104#else
2105 validate_cheight();
2106 used = curwin->w_cline_height;
2107#endif
2108
Bram Moolenaar85a20022019-12-21 18:25:54 +01002109 // If the cursor is below botline, we will at least scroll by the height
2110 // of the cursor line. Correct for empty lines, which are really part of
2111 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 if (cln >= curwin->w_botline)
2113 {
2114 scrolled = used;
2115 if (cln == curwin->w_botline)
2116 scrolled -= curwin->w_empty_rows;
2117 }
2118
2119 /*
2120 * Stop counting lines to scroll when
2121 * - hitting start of the file
2122 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002123 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 * - lines between botline and cursor have been counted
2125 */
2126#ifdef FEAT_FOLDING
2127 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2128#endif
2129 {
2130 loff.lnum = cln;
2131 boff.lnum = cln;
2132 }
2133#ifdef FEAT_DIFF
2134 loff.fill = 0;
2135 boff.fill = 0;
2136 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2137 - curwin->w_filler_rows;
2138#endif
2139
2140 while (loff.lnum > 1)
2141 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002142 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2143 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002145 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2147 && loff.lnum <= curwin->w_botline
2148#ifdef FEAT_DIFF
2149 && (loff.lnum < curwin->w_botline
2150 || loff.fill >= fill_below_window)
2151#endif
2152 )
2153 break;
2154
Bram Moolenaar85a20022019-12-21 18:25:54 +01002155 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002157 if (loff.height == MAXCOL)
2158 used = MAXCOL;
2159 else
2160 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (used > curwin->w_height)
2162 break;
2163 if (loff.lnum >= curwin->w_botline
2164#ifdef FEAT_DIFF
2165 && (loff.lnum > curwin->w_botline
2166 || loff.fill <= fill_below_window)
2167#endif
2168 )
2169 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002170 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 scrolled += loff.height;
2172 if (loff.lnum == curwin->w_botline
2173#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002174 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#endif
2176 )
2177 scrolled -= curwin->w_empty_rows;
2178 }
2179
2180 if (boff.lnum < curbuf->b_ml.ml_line_count)
2181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002182 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 botline_forw(&boff);
2184 used += boff.height;
2185 if (used > curwin->w_height)
2186 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002187 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2188 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
2190 extra += boff.height;
2191 if (boff.lnum >= curwin->w_botline
2192#ifdef FEAT_DIFF
2193 || (boff.lnum + 1 == curwin->w_botline
2194 && boff.fill > curwin->w_filler_rows)
2195#endif
2196 )
2197 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002198 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 scrolled += boff.height;
2200 if (boff.lnum == curwin->w_botline
2201#ifdef FEAT_DIFF
2202 && boff.fill == 0
2203#endif
2204 )
2205 scrolled -= curwin->w_empty_rows;
2206 }
2207 }
2208 }
2209 }
2210
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (scrolled <= 0)
2213 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002214 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 else if (used > curwin->w_height)
2216 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002217 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 else
2219 {
2220 line_count = 0;
2221#ifdef FEAT_DIFF
2222 boff.fill = curwin->w_topfill;
2223#endif
2224 boff.lnum = curwin->w_topline - 1;
2225 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2226 {
2227 botline_forw(&boff);
2228 i += boff.height;
2229 ++line_count;
2230 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002231 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 line_count = 9999;
2233 }
2234
2235 /*
2236 * Scroll up if the cursor is off the bottom of the screen a bit.
2237 * Otherwise put it at 1/2 of the screen.
2238 */
2239 if (line_count >= curwin->w_height && line_count > min_scroll)
2240 scroll_cursor_halfway(FALSE);
2241 else
2242 scrollup(line_count, TRUE);
2243
2244 /*
2245 * If topline didn't change we need to restore w_botline and w_empty_rows
2246 * (we changed them).
2247 * If topline did change, update_screen() will set botline.
2248 */
2249 if (curwin->w_topline == old_topline && set_topbot)
2250 {
2251 curwin->w_botline = old_botline;
2252 curwin->w_empty_rows = old_empty_rows;
2253 curwin->w_valid = old_valid;
2254 }
2255 curwin->w_valid |= VALID_TOPLINE;
2256}
2257
2258/*
2259 * Recompute topline to put the cursor halfway the window
2260 * If "atend" is TRUE, also put it halfway at the end of the file.
2261 */
2262 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002263scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 int above = 0;
2266 linenr_T topline;
2267#ifdef FEAT_DIFF
2268 int topfill = 0;
2269#endif
2270 int below = 0;
2271 int used;
2272 lineoff_T loff;
2273 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002274#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002275 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002278#ifdef FEAT_PROP_POPUP
2279 // if the width changed this needs to be updated first
2280 may_update_popup_position();
2281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2283#ifdef FEAT_FOLDING
2284 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2285#endif
2286#ifdef FEAT_DIFF
2287 used = plines_nofill(loff.lnum);
2288 loff.fill = 0;
2289 boff.fill = 0;
2290#else
2291 used = plines(loff.lnum);
2292#endif
2293 topline = loff.lnum;
2294 while (topline > 1)
2295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002296 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 if (boff.lnum < curbuf->b_ml.ml_line_count)
2299 {
2300 botline_forw(&boff);
2301 used += boff.height;
2302 if (used > curwin->w_height)
2303 break;
2304 below += boff.height;
2305 }
2306 else
2307 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002308 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (atend)
2310 ++used;
2311 }
2312 }
2313
Bram Moolenaar85a20022019-12-21 18:25:54 +01002314 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002317 if (loff.height == MAXCOL)
2318 used = MAXCOL;
2319 else
2320 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (used > curwin->w_height)
2322 break;
2323 above += loff.height;
2324 topline = loff.lnum;
2325#ifdef FEAT_DIFF
2326 topfill = loff.fill;
2327#endif
2328 }
2329 }
2330#ifdef FEAT_FOLDING
2331 if (!hasFolding(topline, &curwin->w_topline, NULL))
2332#endif
2333 curwin->w_topline = topline;
2334#ifdef FEAT_DIFF
2335 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002336 if (old_topline > curwin->w_topline + curwin->w_height)
2337 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 check_topfill(curwin, FALSE);
2339#endif
2340 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2341 curwin->w_valid |= VALID_TOPLINE;
2342}
2343
2344/*
2345 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002346 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 * If not possible, put it at the same position as scroll_cursor_halfway().
2348 * When called topline must be valid!
2349 */
2350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002351cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002353 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002355 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 linenr_T botline;
2357 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002358 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002360 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 /*
2363 * How many lines we would like to have above/below the cursor depends on
2364 * whether the first/last line of the file is on screen.
2365 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002366 above_wanted = so;
2367 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002368 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
2370 above_wanted = mouse_dragging - 1;
2371 below_wanted = mouse_dragging - 1;
2372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (curwin->w_topline == 1)
2374 {
2375 above_wanted = 0;
2376 max_off = curwin->w_height / 2;
2377 if (below_wanted > max_off)
2378 below_wanted = max_off;
2379 }
2380 validate_botline();
2381 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002382 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384 below_wanted = 0;
2385 max_off = (curwin->w_height - 1) / 2;
2386 if (above_wanted > max_off)
2387 above_wanted = max_off;
2388 }
2389
2390 /*
2391 * If there are sufficient file-lines above and below the cursor, we can
2392 * return now.
2393 */
2394 cln = curwin->w_cursor.lnum;
2395 if (cln >= curwin->w_topline + above_wanted
2396 && cln < curwin->w_botline - below_wanted
2397#ifdef FEAT_FOLDING
2398 && !hasAnyFolding(curwin)
2399#endif
2400 )
2401 return;
2402
2403 /*
2404 * Narrow down the area where the cursor can be put by taking lines from
2405 * the top and the bottom until:
2406 * - the desired context lines are found
2407 * - the lines from the top is past the lines from the bottom
2408 */
2409 topline = curwin->w_topline;
2410 botline = curwin->w_botline - 1;
2411#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002412 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 above = curwin->w_topfill;
2414 below = curwin->w_filler_rows;
2415#endif
2416 while ((above < above_wanted || below < below_wanted) && topline < botline)
2417 {
2418 if (below < below_wanted && (below <= above || above >= above_wanted))
2419 {
2420#ifdef FEAT_FOLDING
2421 if (hasFolding(botline, &botline, NULL))
2422 ++below;
2423 else
2424#endif
2425 below += plines(botline);
2426 --botline;
2427 }
2428 if (above < above_wanted && (above < below || below >= below_wanted))
2429 {
2430#ifdef FEAT_FOLDING
2431 if (hasFolding(topline, NULL, &topline))
2432 ++above;
2433 else
2434#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002435 above += PLINES_NOFILL(topline);
2436#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002437 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (topline < botline)
2439 above += diff_check_fill(curwin, topline + 1);
2440#endif
2441 ++topline;
2442 }
2443 }
2444 if (topline == botline || botline == 0)
2445 curwin->w_cursor.lnum = topline;
2446 else if (topline > botline)
2447 curwin->w_cursor.lnum = botline;
2448 else
2449 {
2450 if (cln < topline && curwin->w_topline > 1)
2451 {
2452 curwin->w_cursor.lnum = topline;
2453 curwin->w_valid &=
2454 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2455 }
2456 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2457 {
2458 curwin->w_cursor.lnum = botline;
2459 curwin->w_valid &=
2460 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2461 }
2462 }
2463 curwin->w_valid |= VALID_TOPLINE;
2464}
2465
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002466static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468/*
2469 * move screen 'count' pages up or down and update screen
2470 *
2471 * return FAIL for failure, OK otherwise
2472 */
2473 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002474onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
2476 long n;
2477 int retval = OK;
2478 lineoff_T loff;
2479 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002480 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar85a20022019-12-21 18:25:54 +01002482 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
2484 beep_flush();
2485 return FAIL;
2486 }
2487
2488 for ( ; count > 0; --count)
2489 {
2490 validate_botline();
2491 /*
2492 * It's an error to move a page up when the first line is already on
2493 * the screen. It's an error to move a page down when the last line
2494 * is on the screen and the topline is 'scrolloff' lines from the
2495 * last line.
2496 */
2497 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002498 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2500 : (curwin->w_topline == 1
2501#ifdef FEAT_DIFF
2502 && curwin->w_topfill ==
2503 diff_check_fill(curwin, curwin->w_topline)
2504#endif
2505 ))
2506 {
2507 beep_flush();
2508 retval = FAIL;
2509 break;
2510 }
2511
2512#ifdef FEAT_DIFF
2513 loff.fill = 0;
2514#endif
2515 if (dir == FORWARD)
2516 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002517 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002519 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002520 if (p_window <= 2)
2521 ++curwin->w_topline;
2522 else
2523 curwin->w_topline += p_window - 2;
2524 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2525 curwin->w_topline = curbuf->b_ml.ml_line_count;
2526 curwin->w_cursor.lnum = curwin->w_topline;
2527 }
2528 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2529 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002530 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 curwin->w_topline = curbuf->b_ml.ml_line_count;
2532#ifdef FEAT_DIFF
2533 curwin->w_topfill = 0;
2534#endif
2535 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2536 }
2537 else
2538 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002539 // For the overlap, start with the line just below the window
2540 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 loff.lnum = curwin->w_botline;
2542#ifdef FEAT_DIFF
2543 loff.fill = diff_check_fill(curwin, loff.lnum)
2544 - curwin->w_filler_rows;
2545#endif
2546 get_scroll_overlap(&loff, -1);
2547 curwin->w_topline = loff.lnum;
2548#ifdef FEAT_DIFF
2549 curwin->w_topfill = loff.fill;
2550 check_topfill(curwin, FALSE);
2551#endif
2552 curwin->w_cursor.lnum = curwin->w_topline;
2553 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2554 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2555 }
2556 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002557 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559#ifdef FEAT_DIFF
2560 if (curwin->w_topline == 1)
2561 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002562 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 max_topfill();
2564 continue;
2565 }
2566#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002567 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002568 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002569 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002570 if (p_window <= 2)
2571 --curwin->w_topline;
2572 else
2573 curwin->w_topline -= p_window - 2;
2574 if (curwin->w_topline < 1)
2575 curwin->w_topline = 1;
2576 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2577 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2578 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2579 continue;
2580 }
2581
Bram Moolenaar85a20022019-12-21 18:25:54 +01002582 // Find the line at the top of the window that is going to be the
2583 // line at the bottom of the window. Make sure this results in
2584 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 loff.lnum = curwin->w_topline - 1;
2586#ifdef FEAT_DIFF
2587 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2588 - curwin->w_topfill;
2589#endif
2590 get_scroll_overlap(&loff, 1);
2591
2592 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2593 {
2594 loff.lnum = curbuf->b_ml.ml_line_count;
2595#ifdef FEAT_DIFF
2596 loff.fill = 0;
2597 }
2598 else
2599 {
2600 botline_topline(&loff);
2601#endif
2602 }
2603 curwin->w_cursor.lnum = loff.lnum;
2604
Bram Moolenaar85a20022019-12-21 18:25:54 +01002605 // Find the line just above the new topline to get the right line
2606 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 n = 0;
2608 while (n <= curwin->w_height && loff.lnum >= 1)
2609 {
2610 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002611 if (loff.height == MAXCOL)
2612 n = MAXCOL;
2613 else
2614 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002616 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 curwin->w_topline = 1;
2619#ifdef FEAT_DIFF
2620 max_topfill();
2621#endif
2622 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2623 }
2624 else
2625 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002626 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627#ifdef FEAT_DIFF
2628 topline_botline(&loff);
2629#endif
2630 botline_forw(&loff);
2631 botline_forw(&loff);
2632#ifdef FEAT_DIFF
2633 botline_topline(&loff);
2634#endif
2635#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002636 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2638#endif
2639
Bram Moolenaar85a20022019-12-21 18:25:54 +01002640 // Always scroll at least one line. Avoid getting stuck on
2641 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (loff.lnum >= curwin->w_topline
2643#ifdef FEAT_DIFF
2644 && (loff.lnum > curwin->w_topline
2645 || loff.fill >= curwin->w_topfill)
2646#endif
2647 )
2648 {
2649#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002650 // First try using the maximum number of filler lines. If
2651 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 loff.fill = curwin->w_topfill;
2653 if (curwin->w_topfill < diff_check_fill(curwin,
2654 curwin->w_topline))
2655 max_topfill();
2656 if (curwin->w_topfill == loff.fill)
2657#endif
2658 {
2659 --curwin->w_topline;
2660#ifdef FEAT_DIFF
2661 curwin->w_topfill = 0;
2662#endif
2663 }
2664 comp_botline(curwin);
2665 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002666 curwin->w_valid &=
2667 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669 else
2670 {
2671 curwin->w_topline = loff.lnum;
2672#ifdef FEAT_DIFF
2673 curwin->w_topfill = loff.fill;
2674 check_topfill(curwin, FALSE);
2675#endif
2676 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2677 }
2678 }
2679 }
2680 }
2681#ifdef FEAT_FOLDING
2682 foldAdjustCursor();
2683#endif
2684 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002685 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002686 if (retval == OK)
2687 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2689
Bram Moolenaar907dad72018-07-10 15:07:15 +02002690 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002692 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2693 // But make sure we scroll at least one line (happens with mix of long
2694 // wrapping lines and non-wrapping line).
2695 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002697 scroll_cursor_top(1, FALSE);
2698 if (curwin->w_topline <= old_topline
2699 && old_topline < curbuf->b_ml.ml_line_count)
2700 {
2701 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002703 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2704#endif
2705 }
2706 }
2707#ifdef FEAT_FOLDING
2708 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002713 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 return retval;
2715}
2716
2717/*
2718 * Decide how much overlap to use for page-up or page-down scrolling.
2719 * This is symmetric, so that doing both keeps the same lines displayed.
2720 * Three lines are examined:
2721 *
2722 * before CTRL-F after CTRL-F / before CTRL-B
2723 * etc. l1
2724 * l1 last but one line ------------
2725 * l2 last text line l2 top text line
2726 * ------------- l3 second text line
2727 * l3 etc.
2728 */
2729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002730get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 int h1, h2, h3, h4;
2733 int min_height = curwin->w_height - 2;
2734 lineoff_T loff0, loff1, loff2;
2735
2736#ifdef FEAT_DIFF
2737 if (lp->fill > 0)
2738 lp->height = 1;
2739 else
2740 lp->height = plines_nofill(lp->lnum);
2741#else
2742 lp->height = plines(lp->lnum);
2743#endif
2744 h1 = lp->height;
2745 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002746 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 loff0 = *lp;
2749 if (dir > 0)
2750 botline_forw(lp);
2751 else
2752 topline_back(lp);
2753 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002754 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002756 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 return;
2758 }
2759
2760 loff1 = *lp;
2761 if (dir > 0)
2762 botline_forw(lp);
2763 else
2764 topline_back(lp);
2765 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002766 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002768 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 return;
2770 }
2771
2772 loff2 = *lp;
2773 if (dir > 0)
2774 botline_forw(lp);
2775 else
2776 topline_back(lp);
2777 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002778 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002779 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002781 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782}
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784/*
2785 * Scroll 'scroll' lines up or down.
2786 */
2787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002788halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 long scrolled = 0;
2791 int i;
2792 int n;
2793 int room;
2794
2795 if (Prenum)
2796 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2797 curwin->w_height : Prenum;
2798 n = (curwin->w_p_scr <= curwin->w_height) ?
2799 curwin->w_p_scr : curwin->w_height;
2800
Bram Moolenaard5d37532017-03-27 23:02:07 +02002801 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 validate_botline();
2803 room = curwin->w_empty_rows;
2804#ifdef FEAT_DIFF
2805 room += curwin->w_filler_rows;
2806#endif
2807 if (flag)
2808 {
2809 /*
2810 * scroll the text up
2811 */
2812 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2813 {
2814#ifdef FEAT_DIFF
2815 if (curwin->w_topfill > 0)
2816 {
2817 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002818 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 --curwin->w_topfill;
2820 }
2821 else
2822#endif
2823 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002824 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 n -= i;
2826 if (n < 0 && scrolled > 0)
2827 break;
2828#ifdef FEAT_FOLDING
2829 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2830#endif
2831 ++curwin->w_topline;
2832#ifdef FEAT_DIFF
2833 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2834#endif
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2837 {
2838 ++curwin->w_cursor.lnum;
2839 curwin->w_valid &=
2840 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2844 scrolled += i;
2845
2846 /*
2847 * Correct w_botline for changed w_topline.
2848 * Won't work when there are filler lines.
2849 */
2850#ifdef FEAT_DIFF
2851 if (curwin->w_p_diff)
2852 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2853 else
2854#endif
2855 {
2856 room += i;
2857 do
2858 {
2859 i = plines(curwin->w_botline);
2860 if (i > room)
2861 break;
2862#ifdef FEAT_FOLDING
2863 (void)hasFolding(curwin->w_botline, NULL,
2864 &curwin->w_botline);
2865#endif
2866 ++curwin->w_botline;
2867 room -= i;
2868 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2869 }
2870 }
2871
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 /*
2873 * When hit bottom of the file: move cursor down.
2874 */
2875 if (n > 0)
2876 {
2877# ifdef FEAT_FOLDING
2878 if (hasAnyFolding(curwin))
2879 {
2880 while (--n >= 0
2881 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2882 {
2883 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2884 &curwin->w_cursor.lnum);
2885 ++curwin->w_cursor.lnum;
2886 }
2887 }
2888 else
2889# endif
2890 curwin->w_cursor.lnum += n;
2891 check_cursor_lnum();
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894 else
2895 {
2896 /*
2897 * scroll the text down
2898 */
2899 while (n > 0 && curwin->w_topline > 1)
2900 {
2901#ifdef FEAT_DIFF
2902 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2903 {
2904 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002905 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 ++curwin->w_topfill;
2907 }
2908 else
2909#endif
2910 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002911 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 n -= i;
2913 if (n < 0 && scrolled > 0)
2914 break;
2915 --curwin->w_topline;
2916#ifdef FEAT_FOLDING
2917 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2918#endif
2919#ifdef FEAT_DIFF
2920 curwin->w_topfill = 0;
2921#endif
2922 }
2923 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2924 VALID_BOTLINE|VALID_BOTLINE_AP);
2925 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (curwin->w_cursor.lnum > 1)
2927 {
2928 --curwin->w_cursor.lnum;
2929 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 /*
2934 * When hit top of the file: move cursor up.
2935 */
2936 if (n > 0)
2937 {
2938 if (curwin->w_cursor.lnum <= (linenr_T)n)
2939 curwin->w_cursor.lnum = 1;
2940 else
2941# ifdef FEAT_FOLDING
2942 if (hasAnyFolding(curwin))
2943 {
2944 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2945 {
2946 --curwin->w_cursor.lnum;
2947 (void)hasFolding(curwin->w_cursor.lnum,
2948 &curwin->w_cursor.lnum, NULL);
2949 }
2950 }
2951 else
2952# endif
2953 curwin->w_cursor.lnum -= n;
2954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
2956# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002957 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 foldAdjustCursor();
2959# endif
2960#ifdef FEAT_DIFF
2961 check_topfill(curwin, !flag);
2962#endif
2963 cursor_correct();
2964 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002965 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002967
Bram Moolenaar860cae12010-06-05 23:22:07 +02002968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002969do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002970{
2971 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002972 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002973 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002974 colnr_T curswant = curwin->w_curswant;
2975 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002976 win_T *old_curwin = curwin;
2977 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002978 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002979 int old_VIsual_select = VIsual_select;
2980 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002981
2982 /*
2983 * loop through the cursorbound windows
2984 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002985 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002986 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002987 {
2988 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002989 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002990 if (curwin != old_curwin && curwin->w_p_crb)
2991 {
2992# ifdef FEAT_DIFF
2993 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002994 curwin->w_cursor.lnum =
2995 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002996 else
2997# endif
2998 curwin->w_cursor.lnum = line;
2999 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003000 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003001 curwin->w_curswant = curswant;
3002 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003003
Bram Moolenaar85a20022019-12-21 18:25:54 +01003004 // Make sure the cursor is in a valid position. Temporarily set
3005 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003006 restart_edit_save = restart_edit;
3007 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003008 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003009
3010 // Avoid a scroll here for the cursor position, 'scrollbind' is
3011 // more important.
3012 if (!curwin->w_p_scb)
3013 validate_cursor();
3014
Bram Moolenaar61452852011-02-01 18:01:11 +01003015 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003016 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003017 if (has_mbyte)
3018 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003019 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003020
Bram Moolenaar85a20022019-12-21 18:25:54 +01003021 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003022 if (!curwin->w_p_scb)
3023 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003024 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003025 }
3026 }
3027
3028 /*
3029 * reset current-window
3030 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003031 VIsual_select = old_VIsual_select;
3032 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003033 curwin = old_curwin;
3034 curbuf = old_curbuf;
3035}