blob: 5c78d6ce058c2978560715e282256ad2fdd06c9c [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}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001325
1326/*
1327 * "virtcol2col({winid}, {lnum}, {col})" function
1328 */
1329 void
1330f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1331{
1332 win_T *wp;
1333 linenr_T lnum;
1334 int screencol;
1335 int error = FALSE;
1336
1337 rettv->vval.v_number = -1;
1338
1339 if (check_for_number_arg(argvars, 0) == FAIL
1340 || check_for_number_arg(argvars, 1) == FAIL
1341 || check_for_number_arg(argvars, 2) == FAIL)
1342 return;
1343
1344 wp = find_win_by_nr_or_id(&argvars[0]);
1345 if (wp == NULL)
1346 return;
1347
1348 lnum = tv_get_number_chk(&argvars[1], &error);
1349 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1350 return;
1351
1352 screencol = tv_get_number_chk(&argvars[2], &error);
1353 if (error || screencol < 0)
1354 return;
1355
1356 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1357}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001358#endif
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360/*
1361 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001364scrolldown(
1365 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001366 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001368 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 int wrow;
1370 int moved = FALSE;
1371
1372#ifdef FEAT_FOLDING
1373 linenr_T first;
1374
Bram Moolenaar85a20022019-12-21 18:25:54 +01001375 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1377#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001378 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 while (line_count-- > 0)
1380 {
1381#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001382 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1383 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
1385 ++curwin->w_topfill;
1386 ++done;
1387 }
1388 else
1389#endif
1390 {
1391 if (curwin->w_topline == 1)
1392 break;
1393 --curwin->w_topline;
1394#ifdef FEAT_DIFF
1395 curwin->w_topfill = 0;
1396#endif
1397#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001398 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (hasFolding(curwin->w_topline, &first, NULL))
1400 {
1401 ++done;
1402 if (!byfold)
1403 line_count -= curwin->w_topline - first - 1;
1404 curwin->w_botline -= curwin->w_topline - first;
1405 curwin->w_topline = first;
1406 }
1407 else
1408#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001409 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001411 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 invalidate_botline();
1413 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001414 curwin->w_wrow += done; // keep w_wrow updated
1415 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417#ifdef FEAT_DIFF
1418 if (curwin->w_cursor.lnum == curwin->w_topline)
1419 curwin->w_cline_row = 0;
1420 check_topfill(curwin, TRUE);
1421#endif
1422
1423 /*
1424 * Compute the row number of the last row of the cursor line
1425 * and move the cursor onto the displayed part of the window.
1426 */
1427 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001428 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
1430 validate_virtcol();
1431 validate_cheight();
1432 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001433 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1436 {
1437#ifdef FEAT_FOLDING
1438 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1439 {
1440 --wrow;
1441 if (first == 1)
1442 curwin->w_cursor.lnum = 1;
1443 else
1444 curwin->w_cursor.lnum = first - 1;
1445 }
1446 else
1447#endif
1448 wrow -= plines(curwin->w_cursor.lnum--);
1449 curwin->w_valid &=
1450 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1451 moved = TRUE;
1452 }
1453 if (moved)
1454 {
1455#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001456 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 foldAdjustCursor();
1458#endif
1459 coladvance(curwin->w_curswant);
1460 }
1461}
1462
1463/*
1464 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001467scrollup(
1468 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001469 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1472 linenr_T lnum;
1473
1474 if (
1475# ifdef FEAT_FOLDING
1476 (byfold && hasAnyFolding(curwin))
1477# ifdef FEAT_DIFF
1478 ||
1479# endif
1480# endif
1481# ifdef FEAT_DIFF
1482 curwin->w_p_diff
1483# endif
1484 )
1485 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001486 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 lnum = curwin->w_topline;
1488 while (line_count--)
1489 {
1490# ifdef FEAT_DIFF
1491 if (curwin->w_topfill > 0)
1492 --curwin->w_topfill;
1493 else
1494# endif
1495 {
1496# ifdef FEAT_FOLDING
1497 if (byfold)
1498 (void)hasFolding(lnum, NULL, &lnum);
1499# endif
1500 if (lnum >= curbuf->b_ml.ml_line_count)
1501 break;
1502 ++lnum;
1503# ifdef FEAT_DIFF
1504 curwin->w_topfill = diff_check_fill(curwin, lnum);
1505# endif
1506 }
1507 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001508 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 curwin->w_botline += lnum - curwin->w_topline;
1510 curwin->w_topline = lnum;
1511 }
1512 else
1513#endif
1514 {
1515 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001516 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518
1519 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1520 curwin->w_topline = curbuf->b_ml.ml_line_count;
1521 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1522 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1523
1524#ifdef FEAT_DIFF
1525 check_topfill(curwin, FALSE);
1526#endif
1527
1528#ifdef FEAT_FOLDING
1529 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001530 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1532#endif
1533
1534 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1535 if (curwin->w_cursor.lnum < curwin->w_topline)
1536 {
1537 curwin->w_cursor.lnum = curwin->w_topline;
1538 curwin->w_valid &=
1539 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1540 coladvance(curwin->w_curswant);
1541 }
1542}
1543
1544#ifdef FEAT_DIFF
1545/*
1546 * Don't end up with too many filler lines in the window.
1547 */
1548 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001549check_topfill(
1550 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001551 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 int n;
1554
1555 if (wp->w_topfill > 0)
1556 {
1557 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1558 if (wp->w_topfill + n > wp->w_height)
1559 {
1560 if (down && wp->w_topline > 1)
1561 {
1562 --wp->w_topline;
1563 wp->w_topfill = 0;
1564 }
1565 else
1566 {
1567 wp->w_topfill = wp->w_height - n;
1568 if (wp->w_topfill < 0)
1569 wp->w_topfill = 0;
1570 }
1571 }
1572 }
1573}
1574
1575/*
1576 * Use as many filler lines as possible for w_topline. Make sure w_topline
1577 * is still visible.
1578 */
1579 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001580max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 int n;
1583
1584 n = plines_nofill(curwin->w_topline);
1585 if (n >= curwin->w_height)
1586 curwin->w_topfill = 0;
1587 else
1588 {
1589 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1590 if (curwin->w_topfill + n > curwin->w_height)
1591 curwin->w_topfill = curwin->w_height - n;
1592 }
1593}
1594#endif
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596/*
1597 * Scroll the screen one line down, but don't do it if it would move the
1598 * cursor off the screen.
1599 */
1600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001601scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 int end_row;
1604#ifdef FEAT_DIFF
1605 int can_fill = (curwin->w_topfill
1606 < diff_check_fill(curwin, curwin->w_topline));
1607#endif
1608
1609 if (curwin->w_topline <= 1
1610#ifdef FEAT_DIFF
1611 && !can_fill
1612#endif
1613 )
1614 return;
1615
Bram Moolenaar85a20022019-12-21 18:25:54 +01001616 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618 /*
1619 * Compute the row number of the last row of the cursor line
1620 * and make sure it doesn't go off the screen. Make sure the cursor
1621 * doesn't go past 'scrolloff' lines from the screen end.
1622 */
1623 end_row = curwin->w_wrow;
1624#ifdef FEAT_DIFF
1625 if (can_fill)
1626 ++end_row;
1627 else
1628 end_row += plines_nofill(curwin->w_topline - 1);
1629#else
1630 end_row += plines(curwin->w_topline - 1);
1631#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001632 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 validate_cheight();
1635 validate_virtcol();
1636 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001637 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001639 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
1641#ifdef FEAT_DIFF
1642 if (can_fill)
1643 {
1644 ++curwin->w_topfill;
1645 check_topfill(curwin, TRUE);
1646 }
1647 else
1648 {
1649 --curwin->w_topline;
1650 curwin->w_topfill = 0;
1651 }
1652#else
1653 --curwin->w_topline;
1654#endif
1655#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001656 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001658 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1660 }
1661}
1662
1663/*
1664 * Scroll the screen one line up, but don't do it if it would move the cursor
1665 * off the screen.
1666 */
1667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001668scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 int start_row;
1671
1672 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1673#ifdef FEAT_DIFF
1674 && curwin->w_topfill == 0
1675#endif
1676 )
1677 return;
1678
Bram Moolenaar85a20022019-12-21 18:25:54 +01001679 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 /*
1682 * Compute the row number of the first row of the cursor line
1683 * and make sure it doesn't go off the screen. Make sure the cursor
1684 * doesn't go before 'scrolloff' lines from the screen start.
1685 */
1686#ifdef FEAT_DIFF
1687 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1688 - curwin->w_topfill;
1689#else
1690 start_row = curwin->w_wrow - plines(curwin->w_topline);
1691#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001692 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001695 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001697 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 {
1699#ifdef FEAT_DIFF
1700 if (curwin->w_topfill > 0)
1701 --curwin->w_topfill;
1702 else
1703#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001704 {
1705#ifdef FEAT_FOLDING
1706 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001709 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001710 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1712 }
1713}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715/*
1716 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1717 * a (wrapped) text line. Uses and sets "lp->fill".
1718 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001719 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 */
1721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001722topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724#ifdef FEAT_DIFF
1725 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1726 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001727 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 ++lp->fill;
1729 lp->height = 1;
1730 }
1731 else
1732#endif
1733 {
1734 --lp->lnum;
1735#ifdef FEAT_DIFF
1736 lp->fill = 0;
1737#endif
1738 if (lp->lnum < 1)
1739 lp->height = MAXCOL;
1740 else
1741#ifdef FEAT_FOLDING
1742 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001743 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 lp->height = 1;
1745 else
1746#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001747 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749}
1750
1751/*
1752 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1753 * a (wrapped) text line. Uses and sets "lp->fill".
1754 * Returns the height of the added line in "lp->height".
1755 * Lines below the last one are incredibly high.
1756 */
1757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001758botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760#ifdef FEAT_DIFF
1761 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1762 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001763 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 ++lp->fill;
1765 lp->height = 1;
1766 }
1767 else
1768#endif
1769 {
1770 ++lp->lnum;
1771#ifdef FEAT_DIFF
1772 lp->fill = 0;
1773#endif
1774 if (lp->lnum > curbuf->b_ml.ml_line_count)
1775 lp->height = MAXCOL;
1776 else
1777#ifdef FEAT_FOLDING
1778 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 lp->height = 1;
1781 else
1782#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001783 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
1785}
1786
1787#ifdef FEAT_DIFF
1788/*
1789 * Switch from including filler lines below lp->lnum to including filler
1790 * lines above loff.lnum + 1. This keeps pointing to the same line.
1791 * When there are no filler lines nothing changes.
1792 */
1793 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001794botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
1796 if (lp->fill > 0)
1797 {
1798 ++lp->lnum;
1799 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1800 }
1801}
1802
1803/*
1804 * Switch from including filler lines above lp->lnum to including filler
1805 * lines below loff.lnum - 1. This keeps pointing to the same line.
1806 * When there are no filler lines nothing changes.
1807 */
1808 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001809topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 if (lp->fill > 0)
1812 {
1813 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1814 --lp->lnum;
1815 }
1816}
1817#endif
1818
1819/*
1820 * Recompute topline to put the cursor at the top of the window.
1821 * Scroll at least "min_scroll" lines.
1822 * If "always" is TRUE, always set topline (for "zt").
1823 */
1824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001825scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 int scrolled = 0;
1828 int extra = 0;
1829 int used;
1830 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001831 linenr_T top; // just above displayed lines
1832 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 linenr_T old_topline = curwin->w_topline;
1834#ifdef FEAT_DIFF
1835 linenr_T old_topfill = curwin->w_topfill;
1836#endif
1837 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001838 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (mouse_dragging > 0)
1841 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843 /*
1844 * Decrease topline until:
1845 * - it has become 1
1846 * - (part of) the cursor line is moved off the screen or
1847 * - moved at least 'scrolljump' lines and
1848 * - at least 'scrolloff' lines above and below the cursor
1849 */
1850 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001851 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (curwin->w_cursor.lnum < curwin->w_topline)
1853 scrolled = used;
1854
1855#ifdef FEAT_FOLDING
1856 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1857 {
1858 --top;
1859 ++bot;
1860 }
1861 else
1862#endif
1863 {
1864 top = curwin->w_cursor.lnum - 1;
1865 bot = curwin->w_cursor.lnum + 1;
1866 }
1867 new_topline = top + 1;
1868
1869#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001870 // "used" already contains the number of filler lines above, don't add it
1871 // again.
1872 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001873 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874#endif
1875
1876 /*
1877 * Check if the lines from "top" to "bot" fit in the window. If they do,
1878 * set new_topline and advance "top" and "bot" to include more lines.
1879 */
1880 while (top > 0)
1881 {
1882#ifdef FEAT_FOLDING
1883 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001884 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 i = 1;
1886 else
1887#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001888 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 used += i;
1890 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1891 {
1892#ifdef FEAT_FOLDING
1893 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001894 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 ++used;
1896 else
1897#endif
1898 used += plines(bot);
1899 }
1900 if (used > curwin->w_height)
1901 break;
1902 if (top < curwin->w_topline)
1903 scrolled += i;
1904
1905 /*
1906 * If scrolling is needed, scroll at least 'sj' lines.
1907 */
1908 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1909 && extra >= off)
1910 break;
1911
1912 extra += i;
1913 new_topline = top;
1914 --top;
1915 ++bot;
1916 }
1917
1918 /*
1919 * If we don't have enough space, put cursor in the middle.
1920 * This makes sure we get the same position when using "k" and "j"
1921 * in a small window.
1922 */
1923 if (used > curwin->w_height)
1924 scroll_cursor_halfway(FALSE);
1925 else
1926 {
1927 /*
1928 * If "always" is FALSE, only adjust topline to a lower value, higher
1929 * value may happen with wrapping lines
1930 */
1931 if (new_topline < curwin->w_topline || always)
1932 curwin->w_topline = new_topline;
1933 if (curwin->w_topline > curwin->w_cursor.lnum)
1934 curwin->w_topline = curwin->w_cursor.lnum;
1935#ifdef FEAT_DIFF
1936 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1937 if (curwin->w_topfill > 0 && extra > off)
1938 {
1939 curwin->w_topfill -= extra - off;
1940 if (curwin->w_topfill < 0)
1941 curwin->w_topfill = 0;
1942 }
1943 check_topfill(curwin, FALSE);
1944#endif
1945 if (curwin->w_topline != old_topline
1946#ifdef FEAT_DIFF
1947 || curwin->w_topfill != old_topfill
1948#endif
1949 )
1950 curwin->w_valid &=
1951 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1952 curwin->w_valid |= VALID_TOPLINE;
1953 }
1954}
1955
1956/*
1957 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1958 * screen lines for text lines.
1959 */
1960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001961set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963#ifdef FEAT_DIFF
1964 wp->w_filler_rows = 0;
1965#endif
1966 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001967 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 else
1969 {
1970 wp->w_empty_rows = wp->w_height - used;
1971#ifdef FEAT_DIFF
1972 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1973 {
1974 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1975 if (wp->w_empty_rows > wp->w_filler_rows)
1976 wp->w_empty_rows -= wp->w_filler_rows;
1977 else
1978 {
1979 wp->w_filler_rows = wp->w_empty_rows;
1980 wp->w_empty_rows = 0;
1981 }
1982 }
1983#endif
1984 }
1985}
1986
1987/*
1988 * Recompute topline to put the cursor at the bottom of the window.
1989 * Scroll at least "min_scroll" lines.
1990 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1991 * This is messy stuff!!!
1992 */
1993 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001994scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 int used;
1997 int scrolled = 0;
1998 int extra = 0;
1999 int i;
2000 linenr_T line_count;
2001 linenr_T old_topline = curwin->w_topline;
2002 lineoff_T loff;
2003 lineoff_T boff;
2004#ifdef FEAT_DIFF
2005 int old_topfill = curwin->w_topfill;
2006 int fill_below_window;
2007#endif
2008 linenr_T old_botline = curwin->w_botline;
2009 linenr_T old_valid = curwin->w_valid;
2010 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002011 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002012 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 cln = curwin->w_cursor.lnum;
2015 if (set_topbot)
2016 {
2017 used = 0;
2018 curwin->w_botline = cln + 1;
2019#ifdef FEAT_DIFF
2020 loff.fill = 0;
2021#endif
2022 for (curwin->w_topline = curwin->w_botline;
2023 curwin->w_topline > 1;
2024 curwin->w_topline = loff.lnum)
2025 {
2026 loff.lnum = curwin->w_topline;
2027 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002028 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 break;
2030 used += loff.height;
2031#ifdef FEAT_DIFF
2032 curwin->w_topfill = loff.fill;
2033#endif
2034 }
2035 set_empty_rows(curwin, used);
2036 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2037 if (curwin->w_topline != old_topline
2038#ifdef FEAT_DIFF
2039 || curwin->w_topfill != old_topfill
2040#endif
2041 )
2042 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2043 }
2044 else
2045 validate_botline();
2046
Bram Moolenaar85a20022019-12-21 18:25:54 +01002047 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048#ifdef FEAT_DIFF
2049 used = plines_nofill(cln);
2050#else
2051 validate_cheight();
2052 used = curwin->w_cline_height;
2053#endif
2054
Bram Moolenaar85a20022019-12-21 18:25:54 +01002055 // If the cursor is below botline, we will at least scroll by the height
2056 // of the cursor line. Correct for empty lines, which are really part of
2057 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (cln >= curwin->w_botline)
2059 {
2060 scrolled = used;
2061 if (cln == curwin->w_botline)
2062 scrolled -= curwin->w_empty_rows;
2063 }
2064
2065 /*
2066 * Stop counting lines to scroll when
2067 * - hitting start of the file
2068 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002069 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 * - lines between botline and cursor have been counted
2071 */
2072#ifdef FEAT_FOLDING
2073 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2074#endif
2075 {
2076 loff.lnum = cln;
2077 boff.lnum = cln;
2078 }
2079#ifdef FEAT_DIFF
2080 loff.fill = 0;
2081 boff.fill = 0;
2082 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2083 - curwin->w_filler_rows;
2084#endif
2085
2086 while (loff.lnum > 1)
2087 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002088 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2089 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002091 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2093 && loff.lnum <= curwin->w_botline
2094#ifdef FEAT_DIFF
2095 && (loff.lnum < curwin->w_botline
2096 || loff.fill >= fill_below_window)
2097#endif
2098 )
2099 break;
2100
Bram Moolenaar85a20022019-12-21 18:25:54 +01002101 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002103 if (loff.height == MAXCOL)
2104 used = MAXCOL;
2105 else
2106 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (used > curwin->w_height)
2108 break;
2109 if (loff.lnum >= curwin->w_botline
2110#ifdef FEAT_DIFF
2111 && (loff.lnum > curwin->w_botline
2112 || loff.fill <= fill_below_window)
2113#endif
2114 )
2115 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002116 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 scrolled += loff.height;
2118 if (loff.lnum == curwin->w_botline
2119#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002120 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121#endif
2122 )
2123 scrolled -= curwin->w_empty_rows;
2124 }
2125
2126 if (boff.lnum < curbuf->b_ml.ml_line_count)
2127 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002128 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 botline_forw(&boff);
2130 used += boff.height;
2131 if (used > curwin->w_height)
2132 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002133 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2134 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 extra += boff.height;
2137 if (boff.lnum >= curwin->w_botline
2138#ifdef FEAT_DIFF
2139 || (boff.lnum + 1 == curwin->w_botline
2140 && boff.fill > curwin->w_filler_rows)
2141#endif
2142 )
2143 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 scrolled += boff.height;
2146 if (boff.lnum == curwin->w_botline
2147#ifdef FEAT_DIFF
2148 && boff.fill == 0
2149#endif
2150 )
2151 scrolled -= curwin->w_empty_rows;
2152 }
2153 }
2154 }
2155 }
2156
Bram Moolenaar85a20022019-12-21 18:25:54 +01002157 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (scrolled <= 0)
2159 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002160 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 else if (used > curwin->w_height)
2162 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002163 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 else
2165 {
2166 line_count = 0;
2167#ifdef FEAT_DIFF
2168 boff.fill = curwin->w_topfill;
2169#endif
2170 boff.lnum = curwin->w_topline - 1;
2171 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2172 {
2173 botline_forw(&boff);
2174 i += boff.height;
2175 ++line_count;
2176 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002177 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 line_count = 9999;
2179 }
2180
2181 /*
2182 * Scroll up if the cursor is off the bottom of the screen a bit.
2183 * Otherwise put it at 1/2 of the screen.
2184 */
2185 if (line_count >= curwin->w_height && line_count > min_scroll)
2186 scroll_cursor_halfway(FALSE);
2187 else
2188 scrollup(line_count, TRUE);
2189
2190 /*
2191 * If topline didn't change we need to restore w_botline and w_empty_rows
2192 * (we changed them).
2193 * If topline did change, update_screen() will set botline.
2194 */
2195 if (curwin->w_topline == old_topline && set_topbot)
2196 {
2197 curwin->w_botline = old_botline;
2198 curwin->w_empty_rows = old_empty_rows;
2199 curwin->w_valid = old_valid;
2200 }
2201 curwin->w_valid |= VALID_TOPLINE;
2202}
2203
2204/*
2205 * Recompute topline to put the cursor halfway the window
2206 * If "atend" is TRUE, also put it halfway at the end of the file.
2207 */
2208 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002209scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
2211 int above = 0;
2212 linenr_T topline;
2213#ifdef FEAT_DIFF
2214 int topfill = 0;
2215#endif
2216 int below = 0;
2217 int used;
2218 lineoff_T loff;
2219 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002220#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002221 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002224#ifdef FEAT_PROP_POPUP
2225 // if the width changed this needs to be updated first
2226 may_update_popup_position();
2227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2229#ifdef FEAT_FOLDING
2230 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2231#endif
2232#ifdef FEAT_DIFF
2233 used = plines_nofill(loff.lnum);
2234 loff.fill = 0;
2235 boff.fill = 0;
2236#else
2237 used = plines(loff.lnum);
2238#endif
2239 topline = loff.lnum;
2240 while (topline > 1)
2241 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 if (boff.lnum < curbuf->b_ml.ml_line_count)
2245 {
2246 botline_forw(&boff);
2247 used += boff.height;
2248 if (used > curwin->w_height)
2249 break;
2250 below += boff.height;
2251 }
2252 else
2253 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (atend)
2256 ++used;
2257 }
2258 }
2259
Bram Moolenaar85a20022019-12-21 18:25:54 +01002260 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
2262 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002263 if (loff.height == MAXCOL)
2264 used = MAXCOL;
2265 else
2266 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (used > curwin->w_height)
2268 break;
2269 above += loff.height;
2270 topline = loff.lnum;
2271#ifdef FEAT_DIFF
2272 topfill = loff.fill;
2273#endif
2274 }
2275 }
2276#ifdef FEAT_FOLDING
2277 if (!hasFolding(topline, &curwin->w_topline, NULL))
2278#endif
2279 curwin->w_topline = topline;
2280#ifdef FEAT_DIFF
2281 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002282 if (old_topline > curwin->w_topline + curwin->w_height)
2283 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 check_topfill(curwin, FALSE);
2285#endif
2286 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2287 curwin->w_valid |= VALID_TOPLINE;
2288}
2289
2290/*
2291 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002292 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 * If not possible, put it at the same position as scroll_cursor_halfway().
2294 * When called topline must be valid!
2295 */
2296 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002297cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002299 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002301 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 linenr_T botline;
2303 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002304 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002306 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 /*
2309 * How many lines we would like to have above/below the cursor depends on
2310 * whether the first/last line of the file is on screen.
2311 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002312 above_wanted = so;
2313 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002314 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 above_wanted = mouse_dragging - 1;
2317 below_wanted = mouse_dragging - 1;
2318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (curwin->w_topline == 1)
2320 {
2321 above_wanted = 0;
2322 max_off = curwin->w_height / 2;
2323 if (below_wanted > max_off)
2324 below_wanted = max_off;
2325 }
2326 validate_botline();
2327 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002328 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 below_wanted = 0;
2331 max_off = (curwin->w_height - 1) / 2;
2332 if (above_wanted > max_off)
2333 above_wanted = max_off;
2334 }
2335
2336 /*
2337 * If there are sufficient file-lines above and below the cursor, we can
2338 * return now.
2339 */
2340 cln = curwin->w_cursor.lnum;
2341 if (cln >= curwin->w_topline + above_wanted
2342 && cln < curwin->w_botline - below_wanted
2343#ifdef FEAT_FOLDING
2344 && !hasAnyFolding(curwin)
2345#endif
2346 )
2347 return;
2348
2349 /*
2350 * Narrow down the area where the cursor can be put by taking lines from
2351 * the top and the bottom until:
2352 * - the desired context lines are found
2353 * - the lines from the top is past the lines from the bottom
2354 */
2355 topline = curwin->w_topline;
2356 botline = curwin->w_botline - 1;
2357#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002358 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 above = curwin->w_topfill;
2360 below = curwin->w_filler_rows;
2361#endif
2362 while ((above < above_wanted || below < below_wanted) && topline < botline)
2363 {
2364 if (below < below_wanted && (below <= above || above >= above_wanted))
2365 {
2366#ifdef FEAT_FOLDING
2367 if (hasFolding(botline, &botline, NULL))
2368 ++below;
2369 else
2370#endif
2371 below += plines(botline);
2372 --botline;
2373 }
2374 if (above < above_wanted && (above < below || below >= below_wanted))
2375 {
2376#ifdef FEAT_FOLDING
2377 if (hasFolding(topline, NULL, &topline))
2378 ++above;
2379 else
2380#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002381 above += PLINES_NOFILL(topline);
2382#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002383 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (topline < botline)
2385 above += diff_check_fill(curwin, topline + 1);
2386#endif
2387 ++topline;
2388 }
2389 }
2390 if (topline == botline || botline == 0)
2391 curwin->w_cursor.lnum = topline;
2392 else if (topline > botline)
2393 curwin->w_cursor.lnum = botline;
2394 else
2395 {
2396 if (cln < topline && curwin->w_topline > 1)
2397 {
2398 curwin->w_cursor.lnum = topline;
2399 curwin->w_valid &=
2400 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2401 }
2402 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2403 {
2404 curwin->w_cursor.lnum = botline;
2405 curwin->w_valid &=
2406 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2407 }
2408 }
2409 curwin->w_valid |= VALID_TOPLINE;
2410}
2411
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002412static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414/*
2415 * move screen 'count' pages up or down and update screen
2416 *
2417 * return FAIL for failure, OK otherwise
2418 */
2419 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002420onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 long n;
2423 int retval = OK;
2424 lineoff_T loff;
2425 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002426 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
Bram Moolenaar85a20022019-12-21 18:25:54 +01002428 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 beep_flush();
2431 return FAIL;
2432 }
2433
2434 for ( ; count > 0; --count)
2435 {
2436 validate_botline();
2437 /*
2438 * It's an error to move a page up when the first line is already on
2439 * the screen. It's an error to move a page down when the last line
2440 * is on the screen and the topline is 'scrolloff' lines from the
2441 * last line.
2442 */
2443 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002444 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2446 : (curwin->w_topline == 1
2447#ifdef FEAT_DIFF
2448 && curwin->w_topfill ==
2449 diff_check_fill(curwin, curwin->w_topline)
2450#endif
2451 ))
2452 {
2453 beep_flush();
2454 retval = FAIL;
2455 break;
2456 }
2457
2458#ifdef FEAT_DIFF
2459 loff.fill = 0;
2460#endif
2461 if (dir == FORWARD)
2462 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002463 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002465 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002466 if (p_window <= 2)
2467 ++curwin->w_topline;
2468 else
2469 curwin->w_topline += p_window - 2;
2470 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2471 curwin->w_topline = curbuf->b_ml.ml_line_count;
2472 curwin->w_cursor.lnum = curwin->w_topline;
2473 }
2474 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2475 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002476 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 curwin->w_topline = curbuf->b_ml.ml_line_count;
2478#ifdef FEAT_DIFF
2479 curwin->w_topfill = 0;
2480#endif
2481 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2482 }
2483 else
2484 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002485 // For the overlap, start with the line just below the window
2486 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 loff.lnum = curwin->w_botline;
2488#ifdef FEAT_DIFF
2489 loff.fill = diff_check_fill(curwin, loff.lnum)
2490 - curwin->w_filler_rows;
2491#endif
2492 get_scroll_overlap(&loff, -1);
2493 curwin->w_topline = loff.lnum;
2494#ifdef FEAT_DIFF
2495 curwin->w_topfill = loff.fill;
2496 check_topfill(curwin, FALSE);
2497#endif
2498 curwin->w_cursor.lnum = curwin->w_topline;
2499 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2500 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2501 }
2502 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002503 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505#ifdef FEAT_DIFF
2506 if (curwin->w_topline == 1)
2507 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002508 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 max_topfill();
2510 continue;
2511 }
2512#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002513 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002514 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002515 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002516 if (p_window <= 2)
2517 --curwin->w_topline;
2518 else
2519 curwin->w_topline -= p_window - 2;
2520 if (curwin->w_topline < 1)
2521 curwin->w_topline = 1;
2522 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2523 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2524 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2525 continue;
2526 }
2527
Bram Moolenaar85a20022019-12-21 18:25:54 +01002528 // Find the line at the top of the window that is going to be the
2529 // line at the bottom of the window. Make sure this results in
2530 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 loff.lnum = curwin->w_topline - 1;
2532#ifdef FEAT_DIFF
2533 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2534 - curwin->w_topfill;
2535#endif
2536 get_scroll_overlap(&loff, 1);
2537
2538 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2539 {
2540 loff.lnum = curbuf->b_ml.ml_line_count;
2541#ifdef FEAT_DIFF
2542 loff.fill = 0;
2543 }
2544 else
2545 {
2546 botline_topline(&loff);
2547#endif
2548 }
2549 curwin->w_cursor.lnum = loff.lnum;
2550
Bram Moolenaar85a20022019-12-21 18:25:54 +01002551 // Find the line just above the new topline to get the right line
2552 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 n = 0;
2554 while (n <= curwin->w_height && loff.lnum >= 1)
2555 {
2556 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002557 if (loff.height == MAXCOL)
2558 n = MAXCOL;
2559 else
2560 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002562 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
2564 curwin->w_topline = 1;
2565#ifdef FEAT_DIFF
2566 max_topfill();
2567#endif
2568 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2569 }
2570 else
2571 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002572 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#ifdef FEAT_DIFF
2574 topline_botline(&loff);
2575#endif
2576 botline_forw(&loff);
2577 botline_forw(&loff);
2578#ifdef FEAT_DIFF
2579 botline_topline(&loff);
2580#endif
2581#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002582 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2584#endif
2585
Bram Moolenaar85a20022019-12-21 18:25:54 +01002586 // Always scroll at least one line. Avoid getting stuck on
2587 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (loff.lnum >= curwin->w_topline
2589#ifdef FEAT_DIFF
2590 && (loff.lnum > curwin->w_topline
2591 || loff.fill >= curwin->w_topfill)
2592#endif
2593 )
2594 {
2595#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002596 // First try using the maximum number of filler lines. If
2597 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 loff.fill = curwin->w_topfill;
2599 if (curwin->w_topfill < diff_check_fill(curwin,
2600 curwin->w_topline))
2601 max_topfill();
2602 if (curwin->w_topfill == loff.fill)
2603#endif
2604 {
2605 --curwin->w_topline;
2606#ifdef FEAT_DIFF
2607 curwin->w_topfill = 0;
2608#endif
2609 }
2610 comp_botline(curwin);
2611 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002612 curwin->w_valid &=
2613 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615 else
2616 {
2617 curwin->w_topline = loff.lnum;
2618#ifdef FEAT_DIFF
2619 curwin->w_topfill = loff.fill;
2620 check_topfill(curwin, FALSE);
2621#endif
2622 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2623 }
2624 }
2625 }
2626 }
2627#ifdef FEAT_FOLDING
2628 foldAdjustCursor();
2629#endif
2630 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002631 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002632 if (retval == OK)
2633 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2635
Bram Moolenaar907dad72018-07-10 15:07:15 +02002636 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002638 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2639 // But make sure we scroll at least one line (happens with mix of long
2640 // wrapping lines and non-wrapping line).
2641 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002643 scroll_cursor_top(1, FALSE);
2644 if (curwin->w_topline <= old_topline
2645 && old_topline < curbuf->b_ml.ml_line_count)
2646 {
2647 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002649 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2650#endif
2651 }
2652 }
2653#ifdef FEAT_FOLDING
2654 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658
2659 redraw_later(VALID);
2660 return retval;
2661}
2662
2663/*
2664 * Decide how much overlap to use for page-up or page-down scrolling.
2665 * This is symmetric, so that doing both keeps the same lines displayed.
2666 * Three lines are examined:
2667 *
2668 * before CTRL-F after CTRL-F / before CTRL-B
2669 * etc. l1
2670 * l1 last but one line ------------
2671 * l2 last text line l2 top text line
2672 * ------------- l3 second text line
2673 * l3 etc.
2674 */
2675 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002676get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 int h1, h2, h3, h4;
2679 int min_height = curwin->w_height - 2;
2680 lineoff_T loff0, loff1, loff2;
2681
2682#ifdef FEAT_DIFF
2683 if (lp->fill > 0)
2684 lp->height = 1;
2685 else
2686 lp->height = plines_nofill(lp->lnum);
2687#else
2688 lp->height = plines(lp->lnum);
2689#endif
2690 h1 = lp->height;
2691 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002692 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 loff0 = *lp;
2695 if (dir > 0)
2696 botline_forw(lp);
2697 else
2698 topline_back(lp);
2699 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002700 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002702 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 return;
2704 }
2705
2706 loff1 = *lp;
2707 if (dir > 0)
2708 botline_forw(lp);
2709 else
2710 topline_back(lp);
2711 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002712 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002714 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return;
2716 }
2717
2718 loff2 = *lp;
2719 if (dir > 0)
2720 botline_forw(lp);
2721 else
2722 topline_back(lp);
2723 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002724 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002725 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002727 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728}
2729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730/*
2731 * Scroll 'scroll' lines up or down.
2732 */
2733 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002734halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
2736 long scrolled = 0;
2737 int i;
2738 int n;
2739 int room;
2740
2741 if (Prenum)
2742 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2743 curwin->w_height : Prenum;
2744 n = (curwin->w_p_scr <= curwin->w_height) ?
2745 curwin->w_p_scr : curwin->w_height;
2746
Bram Moolenaard5d37532017-03-27 23:02:07 +02002747 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 validate_botline();
2749 room = curwin->w_empty_rows;
2750#ifdef FEAT_DIFF
2751 room += curwin->w_filler_rows;
2752#endif
2753 if (flag)
2754 {
2755 /*
2756 * scroll the text up
2757 */
2758 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2759 {
2760#ifdef FEAT_DIFF
2761 if (curwin->w_topfill > 0)
2762 {
2763 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002764 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 --curwin->w_topfill;
2766 }
2767 else
2768#endif
2769 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002770 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 n -= i;
2772 if (n < 0 && scrolled > 0)
2773 break;
2774#ifdef FEAT_FOLDING
2775 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2776#endif
2777 ++curwin->w_topline;
2778#ifdef FEAT_DIFF
2779 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2780#endif
2781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2783 {
2784 ++curwin->w_cursor.lnum;
2785 curwin->w_valid &=
2786 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2790 scrolled += i;
2791
2792 /*
2793 * Correct w_botline for changed w_topline.
2794 * Won't work when there are filler lines.
2795 */
2796#ifdef FEAT_DIFF
2797 if (curwin->w_p_diff)
2798 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2799 else
2800#endif
2801 {
2802 room += i;
2803 do
2804 {
2805 i = plines(curwin->w_botline);
2806 if (i > room)
2807 break;
2808#ifdef FEAT_FOLDING
2809 (void)hasFolding(curwin->w_botline, NULL,
2810 &curwin->w_botline);
2811#endif
2812 ++curwin->w_botline;
2813 room -= i;
2814 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2815 }
2816 }
2817
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 /*
2819 * When hit bottom of the file: move cursor down.
2820 */
2821 if (n > 0)
2822 {
2823# ifdef FEAT_FOLDING
2824 if (hasAnyFolding(curwin))
2825 {
2826 while (--n >= 0
2827 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2828 {
2829 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2830 &curwin->w_cursor.lnum);
2831 ++curwin->w_cursor.lnum;
2832 }
2833 }
2834 else
2835# endif
2836 curwin->w_cursor.lnum += n;
2837 check_cursor_lnum();
2838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840 else
2841 {
2842 /*
2843 * scroll the text down
2844 */
2845 while (n > 0 && curwin->w_topline > 1)
2846 {
2847#ifdef FEAT_DIFF
2848 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2849 {
2850 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002851 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 ++curwin->w_topfill;
2853 }
2854 else
2855#endif
2856 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002857 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 n -= i;
2859 if (n < 0 && scrolled > 0)
2860 break;
2861 --curwin->w_topline;
2862#ifdef FEAT_FOLDING
2863 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2864#endif
2865#ifdef FEAT_DIFF
2866 curwin->w_topfill = 0;
2867#endif
2868 }
2869 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2870 VALID_BOTLINE|VALID_BOTLINE_AP);
2871 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (curwin->w_cursor.lnum > 1)
2873 {
2874 --curwin->w_cursor.lnum;
2875 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002878
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 /*
2880 * When hit top of the file: move cursor up.
2881 */
2882 if (n > 0)
2883 {
2884 if (curwin->w_cursor.lnum <= (linenr_T)n)
2885 curwin->w_cursor.lnum = 1;
2886 else
2887# ifdef FEAT_FOLDING
2888 if (hasAnyFolding(curwin))
2889 {
2890 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2891 {
2892 --curwin->w_cursor.lnum;
2893 (void)hasFolding(curwin->w_cursor.lnum,
2894 &curwin->w_cursor.lnum, NULL);
2895 }
2896 }
2897 else
2898# endif
2899 curwin->w_cursor.lnum -= n;
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002903 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 foldAdjustCursor();
2905# endif
2906#ifdef FEAT_DIFF
2907 check_topfill(curwin, !flag);
2908#endif
2909 cursor_correct();
2910 beginline(BL_SOL | BL_FIX);
2911 redraw_later(VALID);
2912}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002913
Bram Moolenaar860cae12010-06-05 23:22:07 +02002914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002915do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002916{
2917 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002918 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002919 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002920 colnr_T curswant = curwin->w_curswant;
2921 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002922 win_T *old_curwin = curwin;
2923 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002924 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002925 int old_VIsual_select = VIsual_select;
2926 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002927
2928 /*
2929 * loop through the cursorbound windows
2930 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002931 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002932 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002933 {
2934 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002935 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002936 if (curwin != old_curwin && curwin->w_p_crb)
2937 {
2938# ifdef FEAT_DIFF
2939 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002940 curwin->w_cursor.lnum =
2941 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002942 else
2943# endif
2944 curwin->w_cursor.lnum = line;
2945 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002946 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002947 curwin->w_curswant = curswant;
2948 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002949
Bram Moolenaar85a20022019-12-21 18:25:54 +01002950 // Make sure the cursor is in a valid position. Temporarily set
2951 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002952 restart_edit_save = restart_edit;
2953 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002954 check_cursor();
Christian Brabandt2c645e82022-04-20 14:52:01 +01002955 validate_cursor();
Bram Moolenaar61452852011-02-01 18:01:11 +01002956 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002957 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002958 if (has_mbyte)
2959 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002960 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002961
Bram Moolenaar85a20022019-12-21 18:25:54 +01002962 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002963 if (!curwin->w_p_scb)
2964 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002965 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002966 }
2967 }
2968
2969 /*
2970 * reset current-window
2971 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002972 VIsual_select = old_VIsual_select;
2973 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002974 curwin = old_curwin;
2975 curbuf = old_curbuf;
2976}