blob: 58bd97fe0402e7fb18e9435426f19bd997f2e648 [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.
134 redraw_win_later(wp, 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 {
148 // When 'cursorcolumn' is set need to redraw with SOME_VALID.
149 if (wp->w_p_cuc)
Bram Moolenaar471b3ae2022-03-28 12:41:19 +0100150 redraw_win_later(wp, SOME_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100151 // When 'cursorlineopt' contains "screenline" need to redraw with VALID.
152 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaar471b3ae2022-03-28 12:41:19 +0100153 redraw_win_later(wp, VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100154 }
155}
156#endif
157
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 * Update curwin->w_topline and redraw if necessary.
160 * Used to update the screen before printing a message.
161 */
162 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100163update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164{
165 update_topline();
166 if (must_redraw)
167 update_screen(0);
168}
169
170/*
171 * Update curwin->w_topline to move the cursor onto the screen.
172 */
173 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100174update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175{
176 long line_count;
177 int halfheight;
178 int n;
179 linenr_T old_topline;
180#ifdef FEAT_DIFF
181 int old_topfill;
182#endif
183#ifdef FEAT_FOLDING
184 linenr_T lnum;
185#endif
186 int check_topline = FALSE;
187 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100188 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100189 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar85a20022019-12-21 18:25:54 +0100191 // If there is no valid screen and when the window height is zero just use
192 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200193 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200194 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100195 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200196 curwin->w_topline = curwin->w_cursor.lnum;
197 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200198 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200199 return;
200 }
201
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 check_cursor_moved(curwin);
203 if (curwin->w_valid & VALID_TOPLINE)
204 return;
205
Bram Moolenaar85a20022019-12-21 18:25:54 +0100206 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000207 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100208 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
210 old_topline = curwin->w_topline;
211#ifdef FEAT_DIFF
212 old_topfill = curwin->w_topfill;
213#endif
214
215 /*
216 * If the buffer is empty, always set topline to 1.
217 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100218 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 {
220 if (curwin->w_topline != 1)
221 redraw_later(NOT_VALID);
222 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 curwin->w_botline = 2;
224 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 }
227
228 /*
229 * If the cursor is above or near the top of the window, scroll the window
230 * to show the line the cursor is in, with 'scrolloff' context.
231 */
232 else
233 {
234 if (curwin->w_topline > 1)
235 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100236 // If the cursor is above topline, scrolling is always needed.
237 // If the cursor is far below topline and there is no folding,
238 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (curwin->w_cursor.lnum < curwin->w_topline)
240 check_topline = TRUE;
241 else if (check_top_offset())
242 check_topline = TRUE;
243 }
244#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100245 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
247 curwin->w_topline))
248 check_topline = TRUE;
249#endif
250
251 if (check_topline)
252 {
253 halfheight = curwin->w_height / 2 - 1;
254 if (halfheight < 2)
255 halfheight = 2;
256
257#ifdef FEAT_FOLDING
258 if (hasAnyFolding(curwin))
259 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100260 // Count the number of logical lines between the cursor and
261 // topline + scrolloff (approximation of how much will be
262 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 n = 0;
264 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100265 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 {
267 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100268 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
270 break;
271 (void)hasFolding(lnum, NULL, &lnum);
272 }
273 }
274 else
275#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100276 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 // If we weren't very close to begin with, we scroll to put the
279 // cursor in the middle of the window. Otherwise put the cursor
280 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (n >= halfheight)
282 scroll_cursor_halfway(FALSE);
283 else
284 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000285 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 check_botline = TRUE;
287 }
288 }
289
290 else
291 {
292#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100293 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
295#endif
296 check_botline = TRUE;
297 }
298 }
299
300 /*
301 * If the cursor is below the bottom of the window, scroll the window
302 * to put the cursor on the window.
303 * When w_botline is invalid, recompute it first, to avoid a redraw later.
304 * If w_botline was approximated, we might need a redraw later in a few
305 * cases, but we don't want to spend (a lot of) time recomputing w_botline
306 * for every small change.
307 */
308 if (check_botline)
309 {
310 if (!(curwin->w_valid & VALID_BOTLINE_AP))
311 validate_botline();
312
313 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
314 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000315 if (curwin->w_cursor.lnum < curwin->w_botline)
316 {
317 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100318 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifdef FEAT_FOLDING
320 || hasAnyFolding(curwin)
321#endif
322 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 lineoff_T loff;
325
Bram Moolenaar85a20022019-12-21 18:25:54 +0100326 // Cursor is (a few lines) above botline, check if there are
327 // 'scrolloff' window lines below the cursor. If not, need to
328 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 n = curwin->w_empty_rows;
330 loff.lnum = curwin->w_cursor.lnum;
331#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100332 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
334#endif
335#ifdef FEAT_DIFF
336 loff.fill = 0;
337 n += curwin->w_filler_rows;
338#endif
339 loff.height = 0;
340 while (loff.lnum < curwin->w_botline
341#ifdef FEAT_DIFF
342 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
343#endif
344 )
345 {
346 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100347 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 break;
349 botline_forw(&loff);
350 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100351 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100352 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000354 }
355 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000357 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (check_botline)
360 {
361#ifdef FEAT_FOLDING
362 if (hasAnyFolding(curwin))
363 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100364 // Count the number of logical lines between the cursor and
365 // botline - scrolloff (approximation of how much will be
366 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 line_count = 0;
368 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100369 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
371 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100372 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 if (lnum <= 0 || line_count > curwin->w_height + 1)
374 break;
375 (void)hasFolding(lnum, &lnum, NULL);
376 }
377 }
378 else
379#endif
380 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100381 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000383 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 else
385 scroll_cursor_halfway(FALSE);
386 }
387 }
388 }
389 curwin->w_valid |= VALID_TOPLINE;
390
391 /*
392 * Need to redraw when topline changed.
393 */
394 if (curwin->w_topline != old_topline
395#ifdef FEAT_DIFF
396 || curwin->w_topfill != old_topfill
397#endif
398 )
399 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100400 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000401 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 {
403 curwin->w_skipcol = 0;
404 redraw_later(NOT_VALID);
405 }
406 else
407 redraw_later(VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100408 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 if (curwin->w_cursor.lnum == curwin->w_topline)
410 validate_cursor();
411 }
412
Bram Moolenaar375e3392019-01-31 18:26:10 +0100413 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414}
415
416/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000417 * Return the scrolljump value to use for the current window.
418 * When 'scrolljump' is positive use it as-is.
419 * When 'scrolljump' is negative use it as a percentage of the window height.
420 */
421 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100422scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000423{
424 if (p_sj >= 0)
425 return (int)p_sj;
426 return (curwin->w_height * -p_sj) / 100;
427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
431 * current window.
432 */
433 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100434check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
436 lineoff_T loff;
437 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100438 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439
Bram Moolenaar375e3392019-01-31 18:26:10 +0100440 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#ifdef FEAT_FOLDING
442 || hasAnyFolding(curwin)
443#endif
444 )
445 {
446 loff.lnum = curwin->w_cursor.lnum;
447#ifdef FEAT_DIFF
448 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100449 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#else
451 n = 0;
452#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100453 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100454 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 {
456 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100457 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 if (loff.lnum < curwin->w_topline
459#ifdef FEAT_DIFF
460 || (loff.lnum == curwin->w_topline && loff.fill > 0)
461#endif
462 )
463 break;
464 n += loff.height;
465 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100466 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 return TRUE;
468 }
469 return FALSE;
470}
471
472 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100473update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
475 if (curwin->w_set_curswant)
476 {
477 validate_virtcol();
478 curwin->w_curswant = curwin->w_virtcol;
479 curwin->w_set_curswant = FALSE;
480 }
481}
482
483/*
484 * Check if the cursor has moved. Set the w_valid flag accordingly.
485 */
486 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100487check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
489 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
490 {
491 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100492 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
493 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 wp->w_valid_cursor = wp->w_cursor;
495 wp->w_valid_leftcol = wp->w_leftcol;
496 }
497 else if (wp->w_cursor.col != wp->w_valid_cursor.col
498 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100499 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
502 wp->w_valid_cursor.col = wp->w_cursor.col;
503 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506}
507
508/*
509 * Call this function when some window settings have changed, which require
510 * the cursor position, botline and topline to be recomputed and the window to
511 * be redrawn. E.g, when changing the 'wrap' option or folding.
512 */
513 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100514changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515{
516 changed_window_setting_win(curwin);
517}
518
519 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100520changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
522 wp->w_lines_valid = 0;
523 changed_line_abv_curs_win(wp);
524 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
525 redraw_win_later(wp, NOT_VALID);
526}
527
528/*
529 * Set wp->w_topline to a certain number.
530 */
531 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100532set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200534#ifdef FEAT_DIFF
535 linenr_T prev_topline = wp->w_topline;
536#endif
537
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100539 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
541#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100542 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100544 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
545 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000547 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200549 if (lnum != prev_topline)
550 // Keep the filler lines when the topline didn't change.
551 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552#endif
553 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100554 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 redraw_later(VALID);
556}
557
558/*
559 * Call this function when the length of the cursor line (in screen
560 * characters) has changed, and the change is before the cursor.
561 * Need to take care of w_botline separately!
562 */
563 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100564changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565{
566 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
567 |VALID_CHEIGHT|VALID_TOPLINE);
568}
569
570 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100571changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572{
573 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
574 |VALID_CHEIGHT|VALID_TOPLINE);
575}
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577/*
578 * Call this function when the length of a line (in screen characters) above
579 * the cursor have changed.
580 * Need to take care of w_botline separately!
581 */
582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100583changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
586 |VALID_CHEIGHT|VALID_TOPLINE);
587}
588
589 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100590changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
593 |VALID_CHEIGHT|VALID_TOPLINE);
594}
595
596/*
597 * Make sure the value of curwin->w_botline is valid.
598 */
599 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100600validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100602 validate_botline_win(curwin);
603}
604
605/*
606 * Make sure the value of wp->w_botline is valid.
607 */
608 void
609validate_botline_win(win_T *wp)
610{
611 if (!(wp->w_valid & VALID_BOTLINE))
612 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613}
614
615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 * Mark curwin->w_botline as invalid (because of some change in the buffer).
617 */
618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
622}
623
624 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100625invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
628}
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100631approximate_botline_win(
632 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 wp->w_valid &= ~VALID_BOTLINE;
635}
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637/*
638 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
639 */
640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100641cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 check_cursor_moved(curwin);
644 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
645 (VALID_WROW|VALID_WCOL));
646}
647
648/*
649 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
650 * w_topline must be valid, you may need to call update_topline() first!
651 */
652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100653validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 check_cursor_moved(curwin);
656 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
657 curs_columns(TRUE);
658}
659
660#if defined(FEAT_GUI) || defined(PROTO)
661/*
662 * validate w_cline_row.
663 */
664 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100665validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
667 /*
668 * First make sure that w_topline is valid (after moving the cursor).
669 */
670 update_topline();
671 check_cursor_moved(curwin);
672 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100673 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674}
675#endif
676
677/*
678 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200679 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 */
681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100682curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 linenr_T lnum;
685 int i;
686 int all_invalid;
687 int valid;
688#ifdef FEAT_FOLDING
689 long fold_count;
690#endif
691
Bram Moolenaar85a20022019-12-21 18:25:54 +0100692 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 all_invalid = (!redrawing()
694 || wp->w_lines_valid == 0
695 || wp->w_lines[0].wl_lnum > wp->w_topline);
696 i = 0;
697 wp->w_cline_row = 0;
698 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
699 {
700 valid = FALSE;
701 if (!all_invalid && i < wp->w_lines_valid)
702 {
703 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100704 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 if (wp->w_lines[i].wl_lnum == lnum)
706 {
707#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100708 // Check for newly inserted lines below this row, in which
709 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 if (!wp->w_buffer->b_mod_set
711 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
712 || wp->w_buffer->b_mod_top
713 > wp->w_lines[i].wl_lastlnum + 1)
714#endif
715 valid = TRUE;
716 }
717 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100718 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720 if (valid
721#ifdef FEAT_DIFF
722 && (lnum != wp->w_topline || !wp->w_p_diff)
723#endif
724 )
725 {
726#ifdef FEAT_FOLDING
727 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100728 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (lnum > wp->w_cursor.lnum)
730 break;
731#else
732 ++lnum;
733#endif
734 wp->w_cline_row += wp->w_lines[i].wl_size;
735 }
736 else
737 {
738#ifdef FEAT_FOLDING
739 fold_count = foldedCount(wp, lnum, NULL);
740 if (fold_count)
741 {
742 lnum += fold_count;
743 if (lnum > wp->w_cursor.lnum)
744 break;
745 ++wp->w_cline_row;
746 }
747 else
748#endif
749#ifdef FEAT_DIFF
750 if (lnum == wp->w_topline)
751 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
752 + wp->w_topfill;
753 else
754#endif
755 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
756 }
757 }
758
759 check_cursor_moved(wp);
760 if (!(wp->w_valid & VALID_CHEIGHT))
761 {
762 if (all_invalid
763 || i == wp->w_lines_valid
764 || (i < wp->w_lines_valid
765 && (!wp->w_lines[i].wl_valid
766 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
767 {
768#ifdef FEAT_DIFF
769 if (wp->w_cursor.lnum == wp->w_topline)
770 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
771 TRUE) + wp->w_topfill;
772 else
773#endif
774 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
775#ifdef FEAT_FOLDING
776 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
777 NULL, NULL, TRUE, NULL);
778#endif
779 }
780 else if (i > wp->w_lines_valid)
781 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100782 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 wp->w_cline_height = 0;
784#ifdef FEAT_FOLDING
785 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
786 NULL, NULL, TRUE, NULL);
787#endif
788 }
789 else
790 {
791 wp->w_cline_height = wp->w_lines[i].wl_size;
792#ifdef FEAT_FOLDING
793 wp->w_cline_folded = wp->w_lines[i].wl_folded;
794#endif
795 }
796 }
797
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100798 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801}
802
803/*
804 * Validate curwin->w_virtcol only.
805 */
806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100807validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808{
809 validate_virtcol_win(curwin);
810}
811
812/*
813 * Validate wp->w_virtcol only.
814 */
815 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100816validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 check_cursor_moved(wp);
819 if (!(wp->w_valid & VALID_VIRTCOL))
820 {
821 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000822#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100823 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000824#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100825 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 }
827}
828
829/*
830 * Validate curwin->w_cline_height only.
831 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100833validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 check_cursor_moved(curwin);
836 if (!(curwin->w_valid & VALID_CHEIGHT))
837 {
838#ifdef FEAT_DIFF
839 if (curwin->w_cursor.lnum == curwin->w_topline)
840 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
841 + curwin->w_topfill;
842 else
843#endif
844 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
845#ifdef FEAT_FOLDING
846 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
847#endif
848 curwin->w_valid |= VALID_CHEIGHT;
849 }
850}
851
852/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000853 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 */
855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100856validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857{
858 colnr_T off;
859 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100860 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862 validate_virtcol();
863 if (!(curwin->w_valid & VALID_WCOL))
864 {
865 col = curwin->w_virtcol;
866 off = curwin_col_off();
867 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200868 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
Bram Moolenaar85a20022019-12-21 18:25:54 +0100870 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000871 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200872 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100873 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100874 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200875 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000876 if (col > (int)curwin->w_leftcol)
877 col -= curwin->w_leftcol;
878 else
879 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100883#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100884 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887}
888
889/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200890 * Compute offset of a window, occupied by absolute or relative line number,
891 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 */
893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100894win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
Bram Moolenaar64486672010-05-16 15:46:46 +0200896 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#ifdef FEAT_CMDWIN
898 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
899#endif
900#ifdef FEAT_FOLDING
901 + wp->w_p_fdc
902#endif
903#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200904 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905#endif
906 );
907}
908
909 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100910curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
912 return win_col_off(curwin);
913}
914
915/*
916 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200917 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
918 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 */
920 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100921win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
Bram Moolenaar64486672010-05-16 15:46:46 +0200923 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000924 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 return 0;
926}
927
928 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100929curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 return win_col_off2(curwin);
932}
933
934/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100935 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Also updates curwin->w_wrow and curwin->w_cline_row.
937 * Also updates curwin->w_leftcol.
938 */
939 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100940curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100941 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
943 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100944 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 int off_left, off_right;
946 int n;
947 int p_lines;
948 int width = 0;
949 int textwidth;
950 int new_leftcol;
951 colnr_T startcol;
952 colnr_T endcol;
953 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100954 long so = get_scrolloff_value();
955 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 /*
958 * First make sure that w_topline is valid (after moving the cursor).
959 */
960 update_topline();
961
962 /*
963 * Next make sure that w_cline_row is valid.
964 */
965 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100966 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968 /*
969 * Compute the number of virtual columns.
970 */
971#ifdef FEAT_FOLDING
972 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100973 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
975 else
976#endif
977 getvvcol(curwin, &curwin->w_cursor,
978 &startcol, &(curwin->w_virtcol), &endcol);
979
Bram Moolenaar85a20022019-12-21 18:25:54 +0100980 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100982 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 extra = curwin_col_off();
985 curwin->w_wcol = curwin->w_virtcol + extra;
986 endcol += extra;
987
988 /*
989 * Now compute w_wrow, counting screen lines from w_cline_row.
990 */
991 curwin->w_wrow = curwin->w_cline_row;
992
Bram Moolenaar02631462017-09-22 15:20:32 +0200993 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (textwidth <= 0)
995 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100996 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +0200997 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +0200998 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +0200999 if (curwin->w_p_wrap)
1000 curwin->w_wrow = curwin->w_height - 1;
1001 else
1002 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001004 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
1006 width = textwidth + curwin_col_off2();
1007
Bram Moolenaar85a20022019-12-21 18:25:54 +01001008 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001009 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001011#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001012 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001013#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001014
Bram Moolenaar85a20022019-12-21 18:25:54 +01001015 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001016 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 curwin->w_wcol -= n * width;
1018 curwin->w_wrow += n;
1019
1020#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001021 // When cursor wraps to first char of next line in Insert
1022 // mode, the 'showbreak' string isn't shown, backup to first
1023 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001024 sbr = get_showbreak_value(curwin);
1025 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001026 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 curwin->w_wcol = 0;
1028#endif
1029 }
1030 }
1031
Bram Moolenaar85a20022019-12-21 18:25:54 +01001032 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1033 // is not folded.
1034 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001035 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036#ifdef FEAT_FOLDING
1037 && !curwin->w_cline_folded
1038#endif
1039 )
1040 {
1041 /*
1042 * If Cursor is left of the screen, scroll rightwards.
1043 * If Cursor is right of the screen, scroll leftwards
1044 * If we get closer to the edge than 'sidescrolloff', scroll a little
1045 * extra
1046 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001047 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001048 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001049 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (off_left < 0 || off_right > 0)
1051 {
1052 if (off_left < 0)
1053 diff = -off_left;
1054 else
1055 diff = off_right;
1056
Bram Moolenaar85a20022019-12-21 18:25:54 +01001057 // When far off or not enough room on either side, put cursor in
1058 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1060 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1061 else
1062 {
1063 if (diff < p_ss)
1064 diff = p_ss;
1065 if (off_left < 0)
1066 new_leftcol = curwin->w_leftcol - diff;
1067 else
1068 new_leftcol = curwin->w_leftcol + diff;
1069 }
1070 if (new_leftcol < 0)
1071 new_leftcol = 0;
1072 if (new_leftcol != (int)curwin->w_leftcol)
1073 {
1074 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001075 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 redraw_later(NOT_VALID);
1077 }
1078 }
1079 curwin->w_wcol -= curwin->w_leftcol;
1080 }
1081 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1082 curwin->w_wcol -= curwin->w_leftcol;
1083 else
1084 curwin->w_wcol = 0;
1085
1086#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001087 // Skip over filler lines. At the top use w_topfill, there
1088 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (curwin->w_cursor.lnum == curwin->w_topline)
1090 curwin->w_wrow += curwin->w_topfill;
1091 else
1092 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1093#endif
1094
1095 prev_skipcol = curwin->w_skipcol;
1096
1097 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if ((curwin->w_wrow >= curwin->w_height
1100 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001101 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 && (p_lines =
1103#ifdef FEAT_DIFF
1104 plines_win_nofill
1105#else
1106 plines_win
1107#endif
1108 (curwin, curwin->w_cursor.lnum, FALSE))
1109 - 1 >= curwin->w_height))
1110 && curwin->w_height != 0
1111 && curwin->w_cursor.lnum == curwin->w_topline
1112 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001113 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001115 // Cursor past end of screen. Happens with a single line that does
1116 // not fit on screen. Find a skipcol to show the text around the
1117 // cursor. Avoid scrolling all the time. compute value of "extra":
1118 // 1: Less than 'scrolloff' lines above
1119 // 2: Less than 'scrolloff' lines below
1120 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001122 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 // Compute last display line of the buffer line that we want at the
1125 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (p_lines == 0)
1127 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1128 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001129 if (p_lines > curwin->w_wrow + so)
1130 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 else
1132 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001133 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 extra += 2;
1135
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001136 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001138 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 n = curwin->w_virtcol / width;
1140 if (n > curwin->w_height / 2)
1141 n -= curwin->w_height / 2;
1142 else
1143 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001144 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (n > p_lines - curwin->w_height + 1)
1146 n = p_lines - curwin->w_height + 1;
1147 curwin->w_skipcol = n * width;
1148 }
1149 else if (extra == 1)
1150 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001151 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001152 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 + width - 1) / width;
1154 if (extra > 0)
1155 {
1156 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1157 extra = curwin->w_skipcol / width;
1158 curwin->w_skipcol -= extra * width;
1159 }
1160 }
1161 else if (extra == 2)
1162 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001163 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 endcol = (n - curwin->w_height + 1) * width;
1165 while (endcol > curwin->w_virtcol)
1166 endcol -= width;
1167 if (endcol > curwin->w_skipcol)
1168 curwin->w_skipcol = endcol;
1169 }
1170
1171 curwin->w_wrow -= curwin->w_skipcol / width;
1172 if (curwin->w_wrow >= curwin->w_height)
1173 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 extra = curwin->w_wrow - curwin->w_height + 1;
1176 curwin->w_skipcol += extra * width;
1177 curwin->w_wrow -= extra;
1178 }
1179
1180 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1181 if (extra > 0)
1182 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1183 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001184 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 }
1186 else
1187 curwin->w_skipcol = 0;
1188 if (prev_skipcol != curwin->w_skipcol)
1189 redraw_later(NOT_VALID);
1190
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001191#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001192 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001193#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001194#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1195 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1196 {
1197 curwin->w_wrow += popup_top_extra(curwin);
1198 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001199 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001200 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001201 else
1202 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001203#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001204
Bram Moolenaar08f23632019-11-16 14:19:33 +01001205 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1206 curwin->w_valid_leftcol = curwin->w_leftcol;
1207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1209}
1210
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001211#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001212/*
1213 * Compute the screen position of text character at "pos" in window "wp"
1214 * The resulting values are one-based, zero when character is not visible.
1215 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001216 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001217textpos2screenpos(
1218 win_T *wp,
1219 pos_T *pos,
1220 int *rowp, // screen row
1221 int *scolp, // start screen column
1222 int *ccolp, // cursor screen column
1223 int *ecolp) // end screen column
1224{
1225 colnr_T scol = 0, ccol = 0, ecol = 0;
1226 int row = 0;
1227 int rowoff = 0;
1228 colnr_T coloff = 0;
1229
Bram Moolenaar189663b2021-07-21 18:04:56 +02001230 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001231 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001232 colnr_T off;
1233 colnr_T col;
1234 int width;
1235 linenr_T lnum = pos->lnum;
1236#ifdef FEAT_FOLDING
1237 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001238
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001239 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1240#endif
1241 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1242#ifdef FEAT_FOLDING
1243 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001244 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001245 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001246 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001247 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001248 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001249#endif
1250 {
1251 getvcol(wp, pos, &scol, &ccol, &ecol);
1252
1253 // similar to what is done in validate_cursor_col()
1254 col = scol;
1255 off = win_col_off(wp);
1256 col += off;
1257 width = wp->w_width - off + win_col_off2(wp);
1258
1259 // long line wrapping, adjust row
1260 if (wp->w_p_wrap
1261 && col >= (colnr_T)wp->w_width
1262 && width > 0)
1263 {
1264 // use same formula as what is used in curs_columns()
1265 rowoff = ((col - wp->w_width) / width + 1);
1266 col -= rowoff * width;
1267 }
1268 col -= wp->w_leftcol;
1269 if (col >= wp->w_width)
1270 col = -1;
1271 if (col >= 0 && row + rowoff <= wp->w_height)
1272 {
1273 coloff = col - scol + wp->w_wincol + 1;
1274 row += W_WINROW(wp);
1275 }
1276 else
1277 // character is left, right or below of the window
1278 row = rowoff = scol = ccol = ecol = 0;
1279 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001280 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001281 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001282 *scolp = scol + coloff;
1283 *ccolp = ccol + coloff;
1284 *ecolp = ecol + coloff;
1285}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001286#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001287
Bram Moolenaar12034e22019-08-25 22:25:02 +02001288#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001289/*
1290 * "screenpos({winid}, {lnum}, {col})" function
1291 */
1292 void
1293f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1294{
1295 dict_T *dict;
1296 win_T *wp;
1297 pos_T pos;
1298 int row = 0;
1299 int scol = 0, ccol = 0, ecol = 0;
1300
1301 if (rettv_dict_alloc(rettv) != OK)
1302 return;
1303 dict = rettv->vval.v_dict;
1304
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001305 if (in_vim9script()
1306 && (check_for_number_arg(argvars, 0) == FAIL
1307 || check_for_number_arg(argvars, 1) == FAIL
1308 || check_for_number_arg(argvars, 2) == FAIL))
1309 return;
1310
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001311 wp = find_win_by_nr_or_id(&argvars[0]);
1312 if (wp == NULL)
1313 return;
1314
1315 pos.lnum = tv_get_number(&argvars[1]);
1316 pos.col = tv_get_number(&argvars[2]) - 1;
1317 pos.coladd = 0;
1318 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1319
1320 dict_add_number(dict, "row", row);
1321 dict_add_number(dict, "col", scol);
1322 dict_add_number(dict, "curscol", ccol);
1323 dict_add_number(dict, "endcol", ecol);
1324}
1325#endif
1326
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327/*
1328 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001331scrolldown(
1332 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001333 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001335 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 int wrow;
1337 int moved = FALSE;
1338
1339#ifdef FEAT_FOLDING
1340 linenr_T first;
1341
Bram Moolenaar85a20022019-12-21 18:25:54 +01001342 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1344#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001345 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 while (line_count-- > 0)
1347 {
1348#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001349 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1350 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
1352 ++curwin->w_topfill;
1353 ++done;
1354 }
1355 else
1356#endif
1357 {
1358 if (curwin->w_topline == 1)
1359 break;
1360 --curwin->w_topline;
1361#ifdef FEAT_DIFF
1362 curwin->w_topfill = 0;
1363#endif
1364#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001365 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (hasFolding(curwin->w_topline, &first, NULL))
1367 {
1368 ++done;
1369 if (!byfold)
1370 line_count -= curwin->w_topline - first - 1;
1371 curwin->w_botline -= curwin->w_topline - first;
1372 curwin->w_topline = first;
1373 }
1374 else
1375#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001376 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001378 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 invalidate_botline();
1380 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001381 curwin->w_wrow += done; // keep w_wrow updated
1382 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
1384#ifdef FEAT_DIFF
1385 if (curwin->w_cursor.lnum == curwin->w_topline)
1386 curwin->w_cline_row = 0;
1387 check_topfill(curwin, TRUE);
1388#endif
1389
1390 /*
1391 * Compute the row number of the last row of the cursor line
1392 * and move the cursor onto the displayed part of the window.
1393 */
1394 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001395 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 {
1397 validate_virtcol();
1398 validate_cheight();
1399 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001400 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1403 {
1404#ifdef FEAT_FOLDING
1405 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1406 {
1407 --wrow;
1408 if (first == 1)
1409 curwin->w_cursor.lnum = 1;
1410 else
1411 curwin->w_cursor.lnum = first - 1;
1412 }
1413 else
1414#endif
1415 wrow -= plines(curwin->w_cursor.lnum--);
1416 curwin->w_valid &=
1417 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1418 moved = TRUE;
1419 }
1420 if (moved)
1421 {
1422#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001423 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 foldAdjustCursor();
1425#endif
1426 coladvance(curwin->w_curswant);
1427 }
1428}
1429
1430/*
1431 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001434scrollup(
1435 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001436 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1439 linenr_T lnum;
1440
1441 if (
1442# ifdef FEAT_FOLDING
1443 (byfold && hasAnyFolding(curwin))
1444# ifdef FEAT_DIFF
1445 ||
1446# endif
1447# endif
1448# ifdef FEAT_DIFF
1449 curwin->w_p_diff
1450# endif
1451 )
1452 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001453 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 lnum = curwin->w_topline;
1455 while (line_count--)
1456 {
1457# ifdef FEAT_DIFF
1458 if (curwin->w_topfill > 0)
1459 --curwin->w_topfill;
1460 else
1461# endif
1462 {
1463# ifdef FEAT_FOLDING
1464 if (byfold)
1465 (void)hasFolding(lnum, NULL, &lnum);
1466# endif
1467 if (lnum >= curbuf->b_ml.ml_line_count)
1468 break;
1469 ++lnum;
1470# ifdef FEAT_DIFF
1471 curwin->w_topfill = diff_check_fill(curwin, lnum);
1472# endif
1473 }
1474 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001475 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 curwin->w_botline += lnum - curwin->w_topline;
1477 curwin->w_topline = lnum;
1478 }
1479 else
1480#endif
1481 {
1482 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001483 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485
1486 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1487 curwin->w_topline = curbuf->b_ml.ml_line_count;
1488 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1489 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1490
1491#ifdef FEAT_DIFF
1492 check_topfill(curwin, FALSE);
1493#endif
1494
1495#ifdef FEAT_FOLDING
1496 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001497 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1499#endif
1500
1501 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1502 if (curwin->w_cursor.lnum < curwin->w_topline)
1503 {
1504 curwin->w_cursor.lnum = curwin->w_topline;
1505 curwin->w_valid &=
1506 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1507 coladvance(curwin->w_curswant);
1508 }
1509}
1510
1511#ifdef FEAT_DIFF
1512/*
1513 * Don't end up with too many filler lines in the window.
1514 */
1515 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001516check_topfill(
1517 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001518 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
1520 int n;
1521
1522 if (wp->w_topfill > 0)
1523 {
1524 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1525 if (wp->w_topfill + n > wp->w_height)
1526 {
1527 if (down && wp->w_topline > 1)
1528 {
1529 --wp->w_topline;
1530 wp->w_topfill = 0;
1531 }
1532 else
1533 {
1534 wp->w_topfill = wp->w_height - n;
1535 if (wp->w_topfill < 0)
1536 wp->w_topfill = 0;
1537 }
1538 }
1539 }
1540}
1541
1542/*
1543 * Use as many filler lines as possible for w_topline. Make sure w_topline
1544 * is still visible.
1545 */
1546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001547max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 int n;
1550
1551 n = plines_nofill(curwin->w_topline);
1552 if (n >= curwin->w_height)
1553 curwin->w_topfill = 0;
1554 else
1555 {
1556 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1557 if (curwin->w_topfill + n > curwin->w_height)
1558 curwin->w_topfill = curwin->w_height - n;
1559 }
1560}
1561#endif
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563/*
1564 * Scroll the screen one line down, but don't do it if it would move the
1565 * cursor off the screen.
1566 */
1567 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001568scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570 int end_row;
1571#ifdef FEAT_DIFF
1572 int can_fill = (curwin->w_topfill
1573 < diff_check_fill(curwin, curwin->w_topline));
1574#endif
1575
1576 if (curwin->w_topline <= 1
1577#ifdef FEAT_DIFF
1578 && !can_fill
1579#endif
1580 )
1581 return;
1582
Bram Moolenaar85a20022019-12-21 18:25:54 +01001583 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 /*
1586 * Compute the row number of the last row of the cursor line
1587 * and make sure it doesn't go off the screen. Make sure the cursor
1588 * doesn't go past 'scrolloff' lines from the screen end.
1589 */
1590 end_row = curwin->w_wrow;
1591#ifdef FEAT_DIFF
1592 if (can_fill)
1593 ++end_row;
1594 else
1595 end_row += plines_nofill(curwin->w_topline - 1);
1596#else
1597 end_row += plines(curwin->w_topline - 1);
1598#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001599 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 validate_cheight();
1602 validate_virtcol();
1603 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001604 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001606 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608#ifdef FEAT_DIFF
1609 if (can_fill)
1610 {
1611 ++curwin->w_topfill;
1612 check_topfill(curwin, TRUE);
1613 }
1614 else
1615 {
1616 --curwin->w_topline;
1617 curwin->w_topfill = 0;
1618 }
1619#else
1620 --curwin->w_topline;
1621#endif
1622#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001623 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001625 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1627 }
1628}
1629
1630/*
1631 * Scroll the screen one line up, but don't do it if it would move the cursor
1632 * off the screen.
1633 */
1634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001635scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 int start_row;
1638
1639 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1640#ifdef FEAT_DIFF
1641 && curwin->w_topfill == 0
1642#endif
1643 )
1644 return;
1645
Bram Moolenaar85a20022019-12-21 18:25:54 +01001646 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 /*
1649 * Compute the row number of the first row of the cursor line
1650 * and make sure it doesn't go off the screen. Make sure the cursor
1651 * doesn't go before 'scrolloff' lines from the screen start.
1652 */
1653#ifdef FEAT_DIFF
1654 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1655 - curwin->w_topfill;
1656#else
1657 start_row = curwin->w_wrow - plines(curwin->w_topline);
1658#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001659 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
1661 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001662 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001664 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666#ifdef FEAT_DIFF
1667 if (curwin->w_topfill > 0)
1668 --curwin->w_topfill;
1669 else
1670#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001671 {
1672#ifdef FEAT_FOLDING
1673 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001676 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001677 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1679 }
1680}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
1682/*
1683 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1684 * a (wrapped) text line. Uses and sets "lp->fill".
1685 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001686 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001689topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691#ifdef FEAT_DIFF
1692 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1693 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001694 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 ++lp->fill;
1696 lp->height = 1;
1697 }
1698 else
1699#endif
1700 {
1701 --lp->lnum;
1702#ifdef FEAT_DIFF
1703 lp->fill = 0;
1704#endif
1705 if (lp->lnum < 1)
1706 lp->height = MAXCOL;
1707 else
1708#ifdef FEAT_FOLDING
1709 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001710 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 lp->height = 1;
1712 else
1713#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001714 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 }
1716}
1717
1718/*
1719 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1720 * a (wrapped) text line. Uses and sets "lp->fill".
1721 * Returns the height of the added line in "lp->height".
1722 * Lines below the last one are incredibly high.
1723 */
1724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001725botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
1727#ifdef FEAT_DIFF
1728 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1729 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001730 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 ++lp->fill;
1732 lp->height = 1;
1733 }
1734 else
1735#endif
1736 {
1737 ++lp->lnum;
1738#ifdef FEAT_DIFF
1739 lp->fill = 0;
1740#endif
1741 if (lp->lnum > curbuf->b_ml.ml_line_count)
1742 lp->height = MAXCOL;
1743 else
1744#ifdef FEAT_FOLDING
1745 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001746 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 lp->height = 1;
1748 else
1749#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001750 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752}
1753
1754#ifdef FEAT_DIFF
1755/*
1756 * Switch from including filler lines below lp->lnum to including filler
1757 * lines above loff.lnum + 1. This keeps pointing to the same line.
1758 * When there are no filler lines nothing changes.
1759 */
1760 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001761botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 if (lp->fill > 0)
1764 {
1765 ++lp->lnum;
1766 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1767 }
1768}
1769
1770/*
1771 * Switch from including filler lines above lp->lnum to including filler
1772 * lines below loff.lnum - 1. This keeps pointing to the same line.
1773 * When there are no filler lines nothing changes.
1774 */
1775 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001776topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 if (lp->fill > 0)
1779 {
1780 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1781 --lp->lnum;
1782 }
1783}
1784#endif
1785
1786/*
1787 * Recompute topline to put the cursor at the top of the window.
1788 * Scroll at least "min_scroll" lines.
1789 * If "always" is TRUE, always set topline (for "zt").
1790 */
1791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001792scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
1794 int scrolled = 0;
1795 int extra = 0;
1796 int used;
1797 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001798 linenr_T top; // just above displayed lines
1799 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 linenr_T old_topline = curwin->w_topline;
1801#ifdef FEAT_DIFF
1802 linenr_T old_topfill = curwin->w_topfill;
1803#endif
1804 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001805 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (mouse_dragging > 0)
1808 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810 /*
1811 * Decrease topline until:
1812 * - it has become 1
1813 * - (part of) the cursor line is moved off the screen or
1814 * - moved at least 'scrolljump' lines and
1815 * - at least 'scrolloff' lines above and below the cursor
1816 */
1817 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001818 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (curwin->w_cursor.lnum < curwin->w_topline)
1820 scrolled = used;
1821
1822#ifdef FEAT_FOLDING
1823 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1824 {
1825 --top;
1826 ++bot;
1827 }
1828 else
1829#endif
1830 {
1831 top = curwin->w_cursor.lnum - 1;
1832 bot = curwin->w_cursor.lnum + 1;
1833 }
1834 new_topline = top + 1;
1835
1836#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001837 // "used" already contains the number of filler lines above, don't add it
1838 // again.
1839 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001840 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#endif
1842
1843 /*
1844 * Check if the lines from "top" to "bot" fit in the window. If they do,
1845 * set new_topline and advance "top" and "bot" to include more lines.
1846 */
1847 while (top > 0)
1848 {
1849#ifdef FEAT_FOLDING
1850 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001851 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 i = 1;
1853 else
1854#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001855 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 used += i;
1857 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1858 {
1859#ifdef FEAT_FOLDING
1860 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001861 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 ++used;
1863 else
1864#endif
1865 used += plines(bot);
1866 }
1867 if (used > curwin->w_height)
1868 break;
1869 if (top < curwin->w_topline)
1870 scrolled += i;
1871
1872 /*
1873 * If scrolling is needed, scroll at least 'sj' lines.
1874 */
1875 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1876 && extra >= off)
1877 break;
1878
1879 extra += i;
1880 new_topline = top;
1881 --top;
1882 ++bot;
1883 }
1884
1885 /*
1886 * If we don't have enough space, put cursor in the middle.
1887 * This makes sure we get the same position when using "k" and "j"
1888 * in a small window.
1889 */
1890 if (used > curwin->w_height)
1891 scroll_cursor_halfway(FALSE);
1892 else
1893 {
1894 /*
1895 * If "always" is FALSE, only adjust topline to a lower value, higher
1896 * value may happen with wrapping lines
1897 */
1898 if (new_topline < curwin->w_topline || always)
1899 curwin->w_topline = new_topline;
1900 if (curwin->w_topline > curwin->w_cursor.lnum)
1901 curwin->w_topline = curwin->w_cursor.lnum;
1902#ifdef FEAT_DIFF
1903 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1904 if (curwin->w_topfill > 0 && extra > off)
1905 {
1906 curwin->w_topfill -= extra - off;
1907 if (curwin->w_topfill < 0)
1908 curwin->w_topfill = 0;
1909 }
1910 check_topfill(curwin, FALSE);
1911#endif
1912 if (curwin->w_topline != old_topline
1913#ifdef FEAT_DIFF
1914 || curwin->w_topfill != old_topfill
1915#endif
1916 )
1917 curwin->w_valid &=
1918 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1919 curwin->w_valid |= VALID_TOPLINE;
1920 }
1921}
1922
1923/*
1924 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1925 * screen lines for text lines.
1926 */
1927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001928set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930#ifdef FEAT_DIFF
1931 wp->w_filler_rows = 0;
1932#endif
1933 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001934 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 else
1936 {
1937 wp->w_empty_rows = wp->w_height - used;
1938#ifdef FEAT_DIFF
1939 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1940 {
1941 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1942 if (wp->w_empty_rows > wp->w_filler_rows)
1943 wp->w_empty_rows -= wp->w_filler_rows;
1944 else
1945 {
1946 wp->w_filler_rows = wp->w_empty_rows;
1947 wp->w_empty_rows = 0;
1948 }
1949 }
1950#endif
1951 }
1952}
1953
1954/*
1955 * Recompute topline to put the cursor at the bottom of the window.
1956 * Scroll at least "min_scroll" lines.
1957 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1958 * This is messy stuff!!!
1959 */
1960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001961scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 int used;
1964 int scrolled = 0;
1965 int extra = 0;
1966 int i;
1967 linenr_T line_count;
1968 linenr_T old_topline = curwin->w_topline;
1969 lineoff_T loff;
1970 lineoff_T boff;
1971#ifdef FEAT_DIFF
1972 int old_topfill = curwin->w_topfill;
1973 int fill_below_window;
1974#endif
1975 linenr_T old_botline = curwin->w_botline;
1976 linenr_T old_valid = curwin->w_valid;
1977 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001978 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001979 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 cln = curwin->w_cursor.lnum;
1982 if (set_topbot)
1983 {
1984 used = 0;
1985 curwin->w_botline = cln + 1;
1986#ifdef FEAT_DIFF
1987 loff.fill = 0;
1988#endif
1989 for (curwin->w_topline = curwin->w_botline;
1990 curwin->w_topline > 1;
1991 curwin->w_topline = loff.lnum)
1992 {
1993 loff.lnum = curwin->w_topline;
1994 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001995 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 break;
1997 used += loff.height;
1998#ifdef FEAT_DIFF
1999 curwin->w_topfill = loff.fill;
2000#endif
2001 }
2002 set_empty_rows(curwin, used);
2003 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2004 if (curwin->w_topline != old_topline
2005#ifdef FEAT_DIFF
2006 || curwin->w_topfill != old_topfill
2007#endif
2008 )
2009 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2010 }
2011 else
2012 validate_botline();
2013
Bram Moolenaar85a20022019-12-21 18:25:54 +01002014 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#ifdef FEAT_DIFF
2016 used = plines_nofill(cln);
2017#else
2018 validate_cheight();
2019 used = curwin->w_cline_height;
2020#endif
2021
Bram Moolenaar85a20022019-12-21 18:25:54 +01002022 // If the cursor is below botline, we will at least scroll by the height
2023 // of the cursor line. Correct for empty lines, which are really part of
2024 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (cln >= curwin->w_botline)
2026 {
2027 scrolled = used;
2028 if (cln == curwin->w_botline)
2029 scrolled -= curwin->w_empty_rows;
2030 }
2031
2032 /*
2033 * Stop counting lines to scroll when
2034 * - hitting start of the file
2035 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002036 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 * - lines between botline and cursor have been counted
2038 */
2039#ifdef FEAT_FOLDING
2040 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2041#endif
2042 {
2043 loff.lnum = cln;
2044 boff.lnum = cln;
2045 }
2046#ifdef FEAT_DIFF
2047 loff.fill = 0;
2048 boff.fill = 0;
2049 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2050 - curwin->w_filler_rows;
2051#endif
2052
2053 while (loff.lnum > 1)
2054 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002055 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2056 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002058 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2060 && loff.lnum <= curwin->w_botline
2061#ifdef FEAT_DIFF
2062 && (loff.lnum < curwin->w_botline
2063 || loff.fill >= fill_below_window)
2064#endif
2065 )
2066 break;
2067
Bram Moolenaar85a20022019-12-21 18:25:54 +01002068 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002070 if (loff.height == MAXCOL)
2071 used = MAXCOL;
2072 else
2073 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (used > curwin->w_height)
2075 break;
2076 if (loff.lnum >= curwin->w_botline
2077#ifdef FEAT_DIFF
2078 && (loff.lnum > curwin->w_botline
2079 || loff.fill <= fill_below_window)
2080#endif
2081 )
2082 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002083 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 scrolled += loff.height;
2085 if (loff.lnum == curwin->w_botline
2086#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002087 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#endif
2089 )
2090 scrolled -= curwin->w_empty_rows;
2091 }
2092
2093 if (boff.lnum < curbuf->b_ml.ml_line_count)
2094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002095 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 botline_forw(&boff);
2097 used += boff.height;
2098 if (used > curwin->w_height)
2099 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002100 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2101 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 extra += boff.height;
2104 if (boff.lnum >= curwin->w_botline
2105#ifdef FEAT_DIFF
2106 || (boff.lnum + 1 == curwin->w_botline
2107 && boff.fill > curwin->w_filler_rows)
2108#endif
2109 )
2110 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002111 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 scrolled += boff.height;
2113 if (boff.lnum == curwin->w_botline
2114#ifdef FEAT_DIFF
2115 && boff.fill == 0
2116#endif
2117 )
2118 scrolled -= curwin->w_empty_rows;
2119 }
2120 }
2121 }
2122 }
2123
Bram Moolenaar85a20022019-12-21 18:25:54 +01002124 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (scrolled <= 0)
2126 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002127 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 else if (used > curwin->w_height)
2129 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002130 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else
2132 {
2133 line_count = 0;
2134#ifdef FEAT_DIFF
2135 boff.fill = curwin->w_topfill;
2136#endif
2137 boff.lnum = curwin->w_topline - 1;
2138 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2139 {
2140 botline_forw(&boff);
2141 i += boff.height;
2142 ++line_count;
2143 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 line_count = 9999;
2146 }
2147
2148 /*
2149 * Scroll up if the cursor is off the bottom of the screen a bit.
2150 * Otherwise put it at 1/2 of the screen.
2151 */
2152 if (line_count >= curwin->w_height && line_count > min_scroll)
2153 scroll_cursor_halfway(FALSE);
2154 else
2155 scrollup(line_count, TRUE);
2156
2157 /*
2158 * If topline didn't change we need to restore w_botline and w_empty_rows
2159 * (we changed them).
2160 * If topline did change, update_screen() will set botline.
2161 */
2162 if (curwin->w_topline == old_topline && set_topbot)
2163 {
2164 curwin->w_botline = old_botline;
2165 curwin->w_empty_rows = old_empty_rows;
2166 curwin->w_valid = old_valid;
2167 }
2168 curwin->w_valid |= VALID_TOPLINE;
2169}
2170
2171/*
2172 * Recompute topline to put the cursor halfway the window
2173 * If "atend" is TRUE, also put it halfway at the end of the file.
2174 */
2175 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002176scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178 int above = 0;
2179 linenr_T topline;
2180#ifdef FEAT_DIFF
2181 int topfill = 0;
2182#endif
2183 int below = 0;
2184 int used;
2185 lineoff_T loff;
2186 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002187#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002188 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002191#ifdef FEAT_PROP_POPUP
2192 // if the width changed this needs to be updated first
2193 may_update_popup_position();
2194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2196#ifdef FEAT_FOLDING
2197 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2198#endif
2199#ifdef FEAT_DIFF
2200 used = plines_nofill(loff.lnum);
2201 loff.fill = 0;
2202 boff.fill = 0;
2203#else
2204 used = plines(loff.lnum);
2205#endif
2206 topline = loff.lnum;
2207 while (topline > 1)
2208 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002209 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
2211 if (boff.lnum < curbuf->b_ml.ml_line_count)
2212 {
2213 botline_forw(&boff);
2214 used += boff.height;
2215 if (used > curwin->w_height)
2216 break;
2217 below += boff.height;
2218 }
2219 else
2220 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002221 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 if (atend)
2223 ++used;
2224 }
2225 }
2226
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
2229 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002230 if (loff.height == MAXCOL)
2231 used = MAXCOL;
2232 else
2233 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (used > curwin->w_height)
2235 break;
2236 above += loff.height;
2237 topline = loff.lnum;
2238#ifdef FEAT_DIFF
2239 topfill = loff.fill;
2240#endif
2241 }
2242 }
2243#ifdef FEAT_FOLDING
2244 if (!hasFolding(topline, &curwin->w_topline, NULL))
2245#endif
2246 curwin->w_topline = topline;
2247#ifdef FEAT_DIFF
2248 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002249 if (old_topline > curwin->w_topline + curwin->w_height)
2250 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 check_topfill(curwin, FALSE);
2252#endif
2253 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2254 curwin->w_valid |= VALID_TOPLINE;
2255}
2256
2257/*
2258 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002259 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 * If not possible, put it at the same position as scroll_cursor_halfway().
2261 * When called topline must be valid!
2262 */
2263 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002264cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002266 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002268 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 linenr_T botline;
2270 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002271 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002273 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275 /*
2276 * How many lines we would like to have above/below the cursor depends on
2277 * whether the first/last line of the file is on screen.
2278 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002279 above_wanted = so;
2280 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002281 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 above_wanted = mouse_dragging - 1;
2284 below_wanted = mouse_dragging - 1;
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (curwin->w_topline == 1)
2287 {
2288 above_wanted = 0;
2289 max_off = curwin->w_height / 2;
2290 if (below_wanted > max_off)
2291 below_wanted = max_off;
2292 }
2293 validate_botline();
2294 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002295 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 below_wanted = 0;
2298 max_off = (curwin->w_height - 1) / 2;
2299 if (above_wanted > max_off)
2300 above_wanted = max_off;
2301 }
2302
2303 /*
2304 * If there are sufficient file-lines above and below the cursor, we can
2305 * return now.
2306 */
2307 cln = curwin->w_cursor.lnum;
2308 if (cln >= curwin->w_topline + above_wanted
2309 && cln < curwin->w_botline - below_wanted
2310#ifdef FEAT_FOLDING
2311 && !hasAnyFolding(curwin)
2312#endif
2313 )
2314 return;
2315
2316 /*
2317 * Narrow down the area where the cursor can be put by taking lines from
2318 * the top and the bottom until:
2319 * - the desired context lines are found
2320 * - the lines from the top is past the lines from the bottom
2321 */
2322 topline = curwin->w_topline;
2323 botline = curwin->w_botline - 1;
2324#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002325 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 above = curwin->w_topfill;
2327 below = curwin->w_filler_rows;
2328#endif
2329 while ((above < above_wanted || below < below_wanted) && topline < botline)
2330 {
2331 if (below < below_wanted && (below <= above || above >= above_wanted))
2332 {
2333#ifdef FEAT_FOLDING
2334 if (hasFolding(botline, &botline, NULL))
2335 ++below;
2336 else
2337#endif
2338 below += plines(botline);
2339 --botline;
2340 }
2341 if (above < above_wanted && (above < below || below >= below_wanted))
2342 {
2343#ifdef FEAT_FOLDING
2344 if (hasFolding(topline, NULL, &topline))
2345 ++above;
2346 else
2347#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002348 above += PLINES_NOFILL(topline);
2349#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002350 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (topline < botline)
2352 above += diff_check_fill(curwin, topline + 1);
2353#endif
2354 ++topline;
2355 }
2356 }
2357 if (topline == botline || botline == 0)
2358 curwin->w_cursor.lnum = topline;
2359 else if (topline > botline)
2360 curwin->w_cursor.lnum = botline;
2361 else
2362 {
2363 if (cln < topline && curwin->w_topline > 1)
2364 {
2365 curwin->w_cursor.lnum = topline;
2366 curwin->w_valid &=
2367 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2368 }
2369 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2370 {
2371 curwin->w_cursor.lnum = botline;
2372 curwin->w_valid &=
2373 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2374 }
2375 }
2376 curwin->w_valid |= VALID_TOPLINE;
2377}
2378
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002379static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381/*
2382 * move screen 'count' pages up or down and update screen
2383 *
2384 * return FAIL for failure, OK otherwise
2385 */
2386 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002387onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
2389 long n;
2390 int retval = OK;
2391 lineoff_T loff;
2392 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002393 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
Bram Moolenaar85a20022019-12-21 18:25:54 +01002395 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
2397 beep_flush();
2398 return FAIL;
2399 }
2400
2401 for ( ; count > 0; --count)
2402 {
2403 validate_botline();
2404 /*
2405 * It's an error to move a page up when the first line is already on
2406 * the screen. It's an error to move a page down when the last line
2407 * is on the screen and the topline is 'scrolloff' lines from the
2408 * last line.
2409 */
2410 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002411 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2413 : (curwin->w_topline == 1
2414#ifdef FEAT_DIFF
2415 && curwin->w_topfill ==
2416 diff_check_fill(curwin, curwin->w_topline)
2417#endif
2418 ))
2419 {
2420 beep_flush();
2421 retval = FAIL;
2422 break;
2423 }
2424
2425#ifdef FEAT_DIFF
2426 loff.fill = 0;
2427#endif
2428 if (dir == FORWARD)
2429 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002430 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002432 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002433 if (p_window <= 2)
2434 ++curwin->w_topline;
2435 else
2436 curwin->w_topline += p_window - 2;
2437 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2438 curwin->w_topline = curbuf->b_ml.ml_line_count;
2439 curwin->w_cursor.lnum = curwin->w_topline;
2440 }
2441 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2442 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002443 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 curwin->w_topline = curbuf->b_ml.ml_line_count;
2445#ifdef FEAT_DIFF
2446 curwin->w_topfill = 0;
2447#endif
2448 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2449 }
2450 else
2451 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002452 // For the overlap, start with the line just below the window
2453 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 loff.lnum = curwin->w_botline;
2455#ifdef FEAT_DIFF
2456 loff.fill = diff_check_fill(curwin, loff.lnum)
2457 - curwin->w_filler_rows;
2458#endif
2459 get_scroll_overlap(&loff, -1);
2460 curwin->w_topline = loff.lnum;
2461#ifdef FEAT_DIFF
2462 curwin->w_topfill = loff.fill;
2463 check_topfill(curwin, FALSE);
2464#endif
2465 curwin->w_cursor.lnum = curwin->w_topline;
2466 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2467 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2468 }
2469 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002470 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472#ifdef FEAT_DIFF
2473 if (curwin->w_topline == 1)
2474 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002475 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 max_topfill();
2477 continue;
2478 }
2479#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002480 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002481 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002482 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002483 if (p_window <= 2)
2484 --curwin->w_topline;
2485 else
2486 curwin->w_topline -= p_window - 2;
2487 if (curwin->w_topline < 1)
2488 curwin->w_topline = 1;
2489 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2490 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2491 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2492 continue;
2493 }
2494
Bram Moolenaar85a20022019-12-21 18:25:54 +01002495 // Find the line at the top of the window that is going to be the
2496 // line at the bottom of the window. Make sure this results in
2497 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 loff.lnum = curwin->w_topline - 1;
2499#ifdef FEAT_DIFF
2500 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2501 - curwin->w_topfill;
2502#endif
2503 get_scroll_overlap(&loff, 1);
2504
2505 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2506 {
2507 loff.lnum = curbuf->b_ml.ml_line_count;
2508#ifdef FEAT_DIFF
2509 loff.fill = 0;
2510 }
2511 else
2512 {
2513 botline_topline(&loff);
2514#endif
2515 }
2516 curwin->w_cursor.lnum = loff.lnum;
2517
Bram Moolenaar85a20022019-12-21 18:25:54 +01002518 // Find the line just above the new topline to get the right line
2519 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 n = 0;
2521 while (n <= curwin->w_height && loff.lnum >= 1)
2522 {
2523 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002524 if (loff.height == MAXCOL)
2525 n = MAXCOL;
2526 else
2527 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002529 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 curwin->w_topline = 1;
2532#ifdef FEAT_DIFF
2533 max_topfill();
2534#endif
2535 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2536 }
2537 else
2538 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002539 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540#ifdef FEAT_DIFF
2541 topline_botline(&loff);
2542#endif
2543 botline_forw(&loff);
2544 botline_forw(&loff);
2545#ifdef FEAT_DIFF
2546 botline_topline(&loff);
2547#endif
2548#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002549 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2551#endif
2552
Bram Moolenaar85a20022019-12-21 18:25:54 +01002553 // Always scroll at least one line. Avoid getting stuck on
2554 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (loff.lnum >= curwin->w_topline
2556#ifdef FEAT_DIFF
2557 && (loff.lnum > curwin->w_topline
2558 || loff.fill >= curwin->w_topfill)
2559#endif
2560 )
2561 {
2562#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002563 // First try using the maximum number of filler lines. If
2564 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 loff.fill = curwin->w_topfill;
2566 if (curwin->w_topfill < diff_check_fill(curwin,
2567 curwin->w_topline))
2568 max_topfill();
2569 if (curwin->w_topfill == loff.fill)
2570#endif
2571 {
2572 --curwin->w_topline;
2573#ifdef FEAT_DIFF
2574 curwin->w_topfill = 0;
2575#endif
2576 }
2577 comp_botline(curwin);
2578 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002579 curwin->w_valid &=
2580 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582 else
2583 {
2584 curwin->w_topline = loff.lnum;
2585#ifdef FEAT_DIFF
2586 curwin->w_topfill = loff.fill;
2587 check_topfill(curwin, FALSE);
2588#endif
2589 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2590 }
2591 }
2592 }
2593 }
2594#ifdef FEAT_FOLDING
2595 foldAdjustCursor();
2596#endif
2597 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002598 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002599 if (retval == OK)
2600 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2602
Bram Moolenaar907dad72018-07-10 15:07:15 +02002603 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002605 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2606 // But make sure we scroll at least one line (happens with mix of long
2607 // wrapping lines and non-wrapping line).
2608 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002610 scroll_cursor_top(1, FALSE);
2611 if (curwin->w_topline <= old_topline
2612 && old_topline < curbuf->b_ml.ml_line_count)
2613 {
2614 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002616 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2617#endif
2618 }
2619 }
2620#ifdef FEAT_FOLDING
2621 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625
2626 redraw_later(VALID);
2627 return retval;
2628}
2629
2630/*
2631 * Decide how much overlap to use for page-up or page-down scrolling.
2632 * This is symmetric, so that doing both keeps the same lines displayed.
2633 * Three lines are examined:
2634 *
2635 * before CTRL-F after CTRL-F / before CTRL-B
2636 * etc. l1
2637 * l1 last but one line ------------
2638 * l2 last text line l2 top text line
2639 * ------------- l3 second text line
2640 * l3 etc.
2641 */
2642 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002643get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 int h1, h2, h3, h4;
2646 int min_height = curwin->w_height - 2;
2647 lineoff_T loff0, loff1, loff2;
2648
2649#ifdef FEAT_DIFF
2650 if (lp->fill > 0)
2651 lp->height = 1;
2652 else
2653 lp->height = plines_nofill(lp->lnum);
2654#else
2655 lp->height = plines(lp->lnum);
2656#endif
2657 h1 = lp->height;
2658 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002659 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 loff0 = *lp;
2662 if (dir > 0)
2663 botline_forw(lp);
2664 else
2665 topline_back(lp);
2666 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002667 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002669 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 return;
2671 }
2672
2673 loff1 = *lp;
2674 if (dir > 0)
2675 botline_forw(lp);
2676 else
2677 topline_back(lp);
2678 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002679 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002681 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 return;
2683 }
2684
2685 loff2 = *lp;
2686 if (dir > 0)
2687 botline_forw(lp);
2688 else
2689 topline_back(lp);
2690 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002691 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002692 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695}
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * Scroll 'scroll' lines up or down.
2699 */
2700 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002701halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 long scrolled = 0;
2704 int i;
2705 int n;
2706 int room;
2707
2708 if (Prenum)
2709 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2710 curwin->w_height : Prenum;
2711 n = (curwin->w_p_scr <= curwin->w_height) ?
2712 curwin->w_p_scr : curwin->w_height;
2713
Bram Moolenaard5d37532017-03-27 23:02:07 +02002714 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 validate_botline();
2716 room = curwin->w_empty_rows;
2717#ifdef FEAT_DIFF
2718 room += curwin->w_filler_rows;
2719#endif
2720 if (flag)
2721 {
2722 /*
2723 * scroll the text up
2724 */
2725 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2726 {
2727#ifdef FEAT_DIFF
2728 if (curwin->w_topfill > 0)
2729 {
2730 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002731 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 --curwin->w_topfill;
2733 }
2734 else
2735#endif
2736 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002737 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 n -= i;
2739 if (n < 0 && scrolled > 0)
2740 break;
2741#ifdef FEAT_FOLDING
2742 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2743#endif
2744 ++curwin->w_topline;
2745#ifdef FEAT_DIFF
2746 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2747#endif
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2750 {
2751 ++curwin->w_cursor.lnum;
2752 curwin->w_valid &=
2753 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
2756 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2757 scrolled += i;
2758
2759 /*
2760 * Correct w_botline for changed w_topline.
2761 * Won't work when there are filler lines.
2762 */
2763#ifdef FEAT_DIFF
2764 if (curwin->w_p_diff)
2765 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2766 else
2767#endif
2768 {
2769 room += i;
2770 do
2771 {
2772 i = plines(curwin->w_botline);
2773 if (i > room)
2774 break;
2775#ifdef FEAT_FOLDING
2776 (void)hasFolding(curwin->w_botline, NULL,
2777 &curwin->w_botline);
2778#endif
2779 ++curwin->w_botline;
2780 room -= i;
2781 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2782 }
2783 }
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 /*
2786 * When hit bottom of the file: move cursor down.
2787 */
2788 if (n > 0)
2789 {
2790# ifdef FEAT_FOLDING
2791 if (hasAnyFolding(curwin))
2792 {
2793 while (--n >= 0
2794 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2795 {
2796 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2797 &curwin->w_cursor.lnum);
2798 ++curwin->w_cursor.lnum;
2799 }
2800 }
2801 else
2802# endif
2803 curwin->w_cursor.lnum += n;
2804 check_cursor_lnum();
2805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 else
2808 {
2809 /*
2810 * scroll the text down
2811 */
2812 while (n > 0 && curwin->w_topline > 1)
2813 {
2814#ifdef FEAT_DIFF
2815 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2816 {
2817 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002818 --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 - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 n -= i;
2826 if (n < 0 && scrolled > 0)
2827 break;
2828 --curwin->w_topline;
2829#ifdef FEAT_FOLDING
2830 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2831#endif
2832#ifdef FEAT_DIFF
2833 curwin->w_topfill = 0;
2834#endif
2835 }
2836 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2837 VALID_BOTLINE|VALID_BOTLINE_AP);
2838 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (curwin->w_cursor.lnum > 1)
2840 {
2841 --curwin->w_cursor.lnum;
2842 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002845
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 /*
2847 * When hit top of the file: move cursor up.
2848 */
2849 if (n > 0)
2850 {
2851 if (curwin->w_cursor.lnum <= (linenr_T)n)
2852 curwin->w_cursor.lnum = 1;
2853 else
2854# ifdef FEAT_FOLDING
2855 if (hasAnyFolding(curwin))
2856 {
2857 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2858 {
2859 --curwin->w_cursor.lnum;
2860 (void)hasFolding(curwin->w_cursor.lnum,
2861 &curwin->w_cursor.lnum, NULL);
2862 }
2863 }
2864 else
2865# endif
2866 curwin->w_cursor.lnum -= n;
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002870 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 foldAdjustCursor();
2872# endif
2873#ifdef FEAT_DIFF
2874 check_topfill(curwin, !flag);
2875#endif
2876 cursor_correct();
2877 beginline(BL_SOL | BL_FIX);
2878 redraw_later(VALID);
2879}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002880
Bram Moolenaar860cae12010-06-05 23:22:07 +02002881 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002882do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002883{
2884 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002885 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002886 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002887 colnr_T curswant = curwin->w_curswant;
2888 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002889 win_T *old_curwin = curwin;
2890 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002891 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002892 int old_VIsual_select = VIsual_select;
2893 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002894
2895 /*
2896 * loop through the cursorbound windows
2897 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002898 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002899 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002900 {
2901 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002902 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002903 if (curwin != old_curwin && curwin->w_p_crb)
2904 {
2905# ifdef FEAT_DIFF
2906 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002907 curwin->w_cursor.lnum =
2908 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002909 else
2910# endif
2911 curwin->w_cursor.lnum = line;
2912 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002913 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002914 curwin->w_curswant = curswant;
2915 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002916
Bram Moolenaar85a20022019-12-21 18:25:54 +01002917 // Make sure the cursor is in a valid position. Temporarily set
2918 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002919 restart_edit_save = restart_edit;
2920 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002921 check_cursor();
Christian Brabandt2c645e82022-04-20 14:52:01 +01002922 validate_cursor();
Bram Moolenaar61452852011-02-01 18:01:11 +01002923 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002925 if (has_mbyte)
2926 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002927 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002928
Bram Moolenaar85a20022019-12-21 18:25:54 +01002929 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002930 if (!curwin->w_p_scb)
2931 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002932 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002933 }
2934 }
2935
2936 /*
2937 * reset current-window
2938 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002939 VIsual_select = old_VIsual_select;
2940 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002941 curwin = old_curwin;
2942 curbuf = old_curbuf;
2943}