blob: 5bf2592269eb375c3fd101144dc01d9739603da1 [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 Moolenaar8088ae92022-06-20 11:38:17 +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 Moolenaar8088ae92022-06-20 11:38:17 +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/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100597 * Display of line has changed for "buf", invalidate cursor position and
598 * w_botline.
599 */
600 void
601changed_line_display_buf(buf_T *buf)
602{
603 win_T *wp;
604
605 FOR_ALL_WINDOWS(wp)
606 if (wp->w_buffer == buf)
607 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
608 |VALID_CROW|VALID_CHEIGHT
609 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
610}
611
612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * Make sure the value of curwin->w_botline is valid.
614 */
615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100616validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100618 validate_botline_win(curwin);
619}
620
621/*
622 * Make sure the value of wp->w_botline is valid.
623 */
624 void
625validate_botline_win(win_T *wp)
626{
627 if (!(wp->w_valid & VALID_BOTLINE))
628 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629}
630
631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 * Mark curwin->w_botline as invalid (because of some change in the buffer).
633 */
634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100635invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
638}
639
640 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100641invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
644}
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100647approximate_botline_win(
648 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649{
650 wp->w_valid &= ~VALID_BOTLINE;
651}
652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653/*
654 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
655 */
656 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 check_cursor_moved(curwin);
660 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
661 (VALID_WROW|VALID_WCOL));
662}
663
664/*
665 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
666 * w_topline must be valid, you may need to call update_topline() first!
667 */
668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100669validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 check_cursor_moved(curwin);
672 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
673 curs_columns(TRUE);
674}
675
676#if defined(FEAT_GUI) || defined(PROTO)
677/*
678 * validate w_cline_row.
679 */
680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100681validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 /*
684 * First make sure that w_topline is valid (after moving the cursor).
685 */
686 update_topline();
687 check_cursor_moved(curwin);
688 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100689 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691#endif
692
693/*
694 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200695 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 */
697 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100698curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
700 linenr_T lnum;
701 int i;
702 int all_invalid;
703 int valid;
704#ifdef FEAT_FOLDING
705 long fold_count;
706#endif
707
Bram Moolenaar85a20022019-12-21 18:25:54 +0100708 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 all_invalid = (!redrawing()
710 || wp->w_lines_valid == 0
711 || wp->w_lines[0].wl_lnum > wp->w_topline);
712 i = 0;
713 wp->w_cline_row = 0;
714 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
715 {
716 valid = FALSE;
717 if (!all_invalid && i < wp->w_lines_valid)
718 {
719 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100720 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (wp->w_lines[i].wl_lnum == lnum)
722 {
723#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100724 // Check for newly inserted lines below this row, in which
725 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (!wp->w_buffer->b_mod_set
727 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
728 || wp->w_buffer->b_mod_top
729 > wp->w_lines[i].wl_lastlnum + 1)
730#endif
731 valid = TRUE;
732 }
733 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100734 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 }
736 if (valid
737#ifdef FEAT_DIFF
738 && (lnum != wp->w_topline || !wp->w_p_diff)
739#endif
740 )
741 {
742#ifdef FEAT_FOLDING
743 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100744 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 if (lnum > wp->w_cursor.lnum)
746 break;
747#else
748 ++lnum;
749#endif
750 wp->w_cline_row += wp->w_lines[i].wl_size;
751 }
752 else
753 {
754#ifdef FEAT_FOLDING
755 fold_count = foldedCount(wp, lnum, NULL);
756 if (fold_count)
757 {
758 lnum += fold_count;
759 if (lnum > wp->w_cursor.lnum)
760 break;
761 ++wp->w_cline_row;
762 }
763 else
764#endif
765#ifdef FEAT_DIFF
766 if (lnum == wp->w_topline)
767 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
768 + wp->w_topfill;
769 else
770#endif
771 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
772 }
773 }
774
775 check_cursor_moved(wp);
776 if (!(wp->w_valid & VALID_CHEIGHT))
777 {
778 if (all_invalid
779 || i == wp->w_lines_valid
780 || (i < wp->w_lines_valid
781 && (!wp->w_lines[i].wl_valid
782 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
783 {
784#ifdef FEAT_DIFF
785 if (wp->w_cursor.lnum == wp->w_topline)
786 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
787 TRUE) + wp->w_topfill;
788 else
789#endif
790 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
791#ifdef FEAT_FOLDING
792 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
793 NULL, NULL, TRUE, NULL);
794#endif
795 }
796 else if (i > wp->w_lines_valid)
797 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100798 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 wp->w_cline_height = 0;
800#ifdef FEAT_FOLDING
801 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
802 NULL, NULL, TRUE, NULL);
803#endif
804 }
805 else
806 {
807 wp->w_cline_height = wp->w_lines[i].wl_size;
808#ifdef FEAT_FOLDING
809 wp->w_cline_folded = wp->w_lines[i].wl_folded;
810#endif
811 }
812 }
813
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100814 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817}
818
819/*
820 * Validate curwin->w_virtcol only.
821 */
822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 validate_virtcol_win(curwin);
826}
827
828/*
829 * Validate wp->w_virtcol only.
830 */
831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100832validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
834 check_cursor_moved(wp);
835 if (!(wp->w_valid & VALID_VIRTCOL))
836 {
837 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000838#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100839 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000840#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100841 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843}
844
845/*
846 * Validate curwin->w_cline_height only.
847 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100849validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 check_cursor_moved(curwin);
852 if (!(curwin->w_valid & VALID_CHEIGHT))
853 {
854#ifdef FEAT_DIFF
855 if (curwin->w_cursor.lnum == curwin->w_topline)
856 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
857 + curwin->w_topfill;
858 else
859#endif
860 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
861#ifdef FEAT_FOLDING
862 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
863#endif
864 curwin->w_valid |= VALID_CHEIGHT;
865 }
866}
867
868/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000869 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 */
871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100872validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 colnr_T off;
875 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100876 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
878 validate_virtcol();
879 if (!(curwin->w_valid & VALID_WCOL))
880 {
881 col = curwin->w_virtcol;
882 off = curwin_col_off();
883 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200884 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaar85a20022019-12-21 18:25:54 +0100886 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000887 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200888 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100889 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100890 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200891 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000892 if (col > (int)curwin->w_leftcol)
893 col -= curwin->w_leftcol;
894 else
895 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000897
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100899#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100900 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903}
904
905/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200906 * Compute offset of a window, occupied by absolute or relative line number,
907 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 */
909 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100910win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
Bram Moolenaar64486672010-05-16 15:46:46 +0200912 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#ifdef FEAT_CMDWIN
914 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
915#endif
916#ifdef FEAT_FOLDING
917 + wp->w_p_fdc
918#endif
919#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200920 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921#endif
922 );
923}
924
925 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 return win_col_off(curwin);
929}
930
931/*
932 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200933 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
934 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
936 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100937win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
Bram Moolenaar64486672010-05-16 15:46:46 +0200939 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000940 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 return 0;
942}
943
944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100945curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 return win_col_off2(curwin);
948}
949
950/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100951 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 * Also updates curwin->w_wrow and curwin->w_cline_row.
953 * Also updates curwin->w_leftcol.
954 */
955 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100956curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100957 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958{
959 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100960 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 int off_left, off_right;
962 int n;
963 int p_lines;
964 int width = 0;
965 int textwidth;
966 int new_leftcol;
967 colnr_T startcol;
968 colnr_T endcol;
969 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100970 long so = get_scrolloff_value();
971 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973 /*
974 * First make sure that w_topline is valid (after moving the cursor).
975 */
976 update_topline();
977
978 /*
979 * Next make sure that w_cline_row is valid.
980 */
981 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100982 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 /*
985 * Compute the number of virtual columns.
986 */
987#ifdef FEAT_FOLDING
988 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100989 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
991 else
992#endif
993 getvvcol(curwin, &curwin->w_cursor,
994 &startcol, &(curwin->w_virtcol), &endcol);
995
Bram Moolenaar85a20022019-12-21 18:25:54 +0100996 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100998 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000 extra = curwin_col_off();
1001 curwin->w_wcol = curwin->w_virtcol + extra;
1002 endcol += extra;
1003
1004 /*
1005 * Now compute w_wrow, counting screen lines from w_cline_row.
1006 */
1007 curwin->w_wrow = curwin->w_cline_row;
1008
Bram Moolenaar02631462017-09-22 15:20:32 +02001009 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (textwidth <= 0)
1011 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001012 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001013 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001014 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001015 if (curwin->w_p_wrap)
1016 curwin->w_wrow = curwin->w_height - 1;
1017 else
1018 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001020 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
1022 width = textwidth + curwin_col_off2();
1023
Bram Moolenaar85a20022019-12-21 18:25:54 +01001024 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001025 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001027#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001028 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001029#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001030
Bram Moolenaar85a20022019-12-21 18:25:54 +01001031 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001032 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 curwin->w_wcol -= n * width;
1034 curwin->w_wrow += n;
1035
1036#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001037 // When cursor wraps to first char of next line in Insert
1038 // mode, the 'showbreak' string isn't shown, backup to first
1039 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001040 sbr = get_showbreak_value(curwin);
1041 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001042 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 curwin->w_wcol = 0;
1044#endif
1045 }
1046 }
1047
Bram Moolenaar85a20022019-12-21 18:25:54 +01001048 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1049 // is not folded.
1050 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001051 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052#ifdef FEAT_FOLDING
1053 && !curwin->w_cline_folded
1054#endif
1055 )
1056 {
1057 /*
1058 * If Cursor is left of the screen, scroll rightwards.
1059 * If Cursor is right of the screen, scroll leftwards
1060 * If we get closer to the edge than 'sidescrolloff', scroll a little
1061 * extra
1062 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001063 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001064 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001065 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (off_left < 0 || off_right > 0)
1067 {
1068 if (off_left < 0)
1069 diff = -off_left;
1070 else
1071 diff = off_right;
1072
Bram Moolenaar85a20022019-12-21 18:25:54 +01001073 // When far off or not enough room on either side, put cursor in
1074 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1076 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1077 else
1078 {
1079 if (diff < p_ss)
1080 diff = p_ss;
1081 if (off_left < 0)
1082 new_leftcol = curwin->w_leftcol - diff;
1083 else
1084 new_leftcol = curwin->w_leftcol + diff;
1085 }
1086 if (new_leftcol < 0)
1087 new_leftcol = 0;
1088 if (new_leftcol != (int)curwin->w_leftcol)
1089 {
1090 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 redraw_later(NOT_VALID);
1093 }
1094 }
1095 curwin->w_wcol -= curwin->w_leftcol;
1096 }
1097 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1098 curwin->w_wcol -= curwin->w_leftcol;
1099 else
1100 curwin->w_wcol = 0;
1101
1102#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001103 // Skip over filler lines. At the top use w_topfill, there
1104 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (curwin->w_cursor.lnum == curwin->w_topline)
1106 curwin->w_wrow += curwin->w_topfill;
1107 else
1108 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1109#endif
1110
1111 prev_skipcol = curwin->w_skipcol;
1112
1113 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 if ((curwin->w_wrow >= curwin->w_height
1116 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001117 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 && (p_lines =
1119#ifdef FEAT_DIFF
1120 plines_win_nofill
1121#else
1122 plines_win
1123#endif
1124 (curwin, curwin->w_cursor.lnum, FALSE))
1125 - 1 >= curwin->w_height))
1126 && curwin->w_height != 0
1127 && curwin->w_cursor.lnum == curwin->w_topline
1128 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001129 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001131 // Cursor past end of screen. Happens with a single line that does
1132 // not fit on screen. Find a skipcol to show the text around the
1133 // cursor. Avoid scrolling all the time. compute value of "extra":
1134 // 1: Less than 'scrolloff' lines above
1135 // 2: Less than 'scrolloff' lines below
1136 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001138 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001140 // Compute last display line of the buffer line that we want at the
1141 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (p_lines == 0)
1143 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1144 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001145 if (p_lines > curwin->w_wrow + so)
1146 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 else
1148 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001149 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 extra += 2;
1151
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001152 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 n = curwin->w_virtcol / width;
1156 if (n > curwin->w_height / 2)
1157 n -= curwin->w_height / 2;
1158 else
1159 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001160 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (n > p_lines - curwin->w_height + 1)
1162 n = p_lines - curwin->w_height + 1;
1163 curwin->w_skipcol = n * width;
1164 }
1165 else if (extra == 1)
1166 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001167 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001168 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 + width - 1) / width;
1170 if (extra > 0)
1171 {
1172 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1173 extra = curwin->w_skipcol / width;
1174 curwin->w_skipcol -= extra * width;
1175 }
1176 }
1177 else if (extra == 2)
1178 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001179 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 endcol = (n - curwin->w_height + 1) * width;
1181 while (endcol > curwin->w_virtcol)
1182 endcol -= width;
1183 if (endcol > curwin->w_skipcol)
1184 curwin->w_skipcol = endcol;
1185 }
1186
1187 curwin->w_wrow -= curwin->w_skipcol / width;
1188 if (curwin->w_wrow >= curwin->w_height)
1189 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001190 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 extra = curwin->w_wrow - curwin->w_height + 1;
1192 curwin->w_skipcol += extra * width;
1193 curwin->w_wrow -= extra;
1194 }
1195
1196 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1197 if (extra > 0)
1198 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1199 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001200 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202 else
1203 curwin->w_skipcol = 0;
1204 if (prev_skipcol != curwin->w_skipcol)
1205 redraw_later(NOT_VALID);
1206
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001207#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001208 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001209#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001210#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1211 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1212 {
1213 curwin->w_wrow += popup_top_extra(curwin);
1214 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001215 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001216 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001217 else
1218 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001219#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001220
Bram Moolenaar08f23632019-11-16 14:19:33 +01001221 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1222 curwin->w_valid_leftcol = curwin->w_leftcol;
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1225}
1226
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001227#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001228/*
1229 * Compute the screen position of text character at "pos" in window "wp"
1230 * The resulting values are one-based, zero when character is not visible.
1231 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001232 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001233textpos2screenpos(
1234 win_T *wp,
1235 pos_T *pos,
1236 int *rowp, // screen row
1237 int *scolp, // start screen column
1238 int *ccolp, // cursor screen column
1239 int *ecolp) // end screen column
1240{
1241 colnr_T scol = 0, ccol = 0, ecol = 0;
1242 int row = 0;
1243 int rowoff = 0;
1244 colnr_T coloff = 0;
1245
Bram Moolenaar189663b2021-07-21 18:04:56 +02001246 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001247 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001248 colnr_T off;
1249 colnr_T col;
1250 int width;
1251 linenr_T lnum = pos->lnum;
1252#ifdef FEAT_FOLDING
1253 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001254
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001255 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1256#endif
1257 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1258#ifdef FEAT_FOLDING
1259 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001260 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001261 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001262 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001263 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001264 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001265#endif
1266 {
1267 getvcol(wp, pos, &scol, &ccol, &ecol);
1268
1269 // similar to what is done in validate_cursor_col()
1270 col = scol;
1271 off = win_col_off(wp);
1272 col += off;
1273 width = wp->w_width - off + win_col_off2(wp);
1274
1275 // long line wrapping, adjust row
1276 if (wp->w_p_wrap
1277 && col >= (colnr_T)wp->w_width
1278 && width > 0)
1279 {
1280 // use same formula as what is used in curs_columns()
1281 rowoff = ((col - wp->w_width) / width + 1);
1282 col -= rowoff * width;
1283 }
1284 col -= wp->w_leftcol;
1285 if (col >= wp->w_width)
1286 col = -1;
1287 if (col >= 0 && row + rowoff <= wp->w_height)
1288 {
1289 coloff = col - scol + wp->w_wincol + 1;
1290 row += W_WINROW(wp);
1291 }
1292 else
1293 // character is left, right or below of the window
1294 row = rowoff = scol = ccol = ecol = 0;
1295 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001296 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001297 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001298 *scolp = scol + coloff;
1299 *ccolp = ccol + coloff;
1300 *ecolp = ecol + coloff;
1301}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001302#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001303
Bram Moolenaar12034e22019-08-25 22:25:02 +02001304#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001305/*
1306 * "screenpos({winid}, {lnum}, {col})" function
1307 */
1308 void
1309f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1310{
1311 dict_T *dict;
1312 win_T *wp;
1313 pos_T pos;
1314 int row = 0;
1315 int scol = 0, ccol = 0, ecol = 0;
1316
Bram Moolenaar93a10962022-06-16 11:42:09 +01001317 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001318 return;
1319 dict = rettv->vval.v_dict;
1320
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001321 if (in_vim9script()
1322 && (check_for_number_arg(argvars, 0) == FAIL
1323 || check_for_number_arg(argvars, 1) == FAIL
1324 || check_for_number_arg(argvars, 2) == FAIL))
1325 return;
1326
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001327 wp = find_win_by_nr_or_id(&argvars[0]);
1328 if (wp == NULL)
1329 return;
1330
1331 pos.lnum = tv_get_number(&argvars[1]);
1332 pos.col = tv_get_number(&argvars[2]) - 1;
1333 pos.coladd = 0;
1334 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1335
1336 dict_add_number(dict, "row", row);
1337 dict_add_number(dict, "col", scol);
1338 dict_add_number(dict, "curscol", ccol);
1339 dict_add_number(dict, "endcol", ecol);
1340}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001341
1342/*
1343 * "virtcol2col({winid}, {lnum}, {col})" function
1344 */
1345 void
1346f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1347{
1348 win_T *wp;
1349 linenr_T lnum;
1350 int screencol;
1351 int error = FALSE;
1352
1353 rettv->vval.v_number = -1;
1354
1355 if (check_for_number_arg(argvars, 0) == FAIL
1356 || check_for_number_arg(argvars, 1) == FAIL
1357 || check_for_number_arg(argvars, 2) == FAIL)
1358 return;
1359
1360 wp = find_win_by_nr_or_id(&argvars[0]);
1361 if (wp == NULL)
1362 return;
1363
1364 lnum = tv_get_number_chk(&argvars[1], &error);
1365 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1366 return;
1367
1368 screencol = tv_get_number_chk(&argvars[2], &error);
1369 if (error || screencol < 0)
1370 return;
1371
1372 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1373}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001374#endif
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376/*
1377 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001380scrolldown(
1381 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001382 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001384 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int wrow;
1386 int moved = FALSE;
1387
1388#ifdef FEAT_FOLDING
1389 linenr_T first;
1390
Bram Moolenaar85a20022019-12-21 18:25:54 +01001391 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1393#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001394 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 while (line_count-- > 0)
1396 {
1397#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001398 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1399 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 {
1401 ++curwin->w_topfill;
1402 ++done;
1403 }
1404 else
1405#endif
1406 {
1407 if (curwin->w_topline == 1)
1408 break;
1409 --curwin->w_topline;
1410#ifdef FEAT_DIFF
1411 curwin->w_topfill = 0;
1412#endif
1413#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001414 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (hasFolding(curwin->w_topline, &first, NULL))
1416 {
1417 ++done;
1418 if (!byfold)
1419 line_count -= curwin->w_topline - first - 1;
1420 curwin->w_botline -= curwin->w_topline - first;
1421 curwin->w_topline = first;
1422 }
1423 else
1424#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001425 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001427 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 invalidate_botline();
1429 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001430 curwin->w_wrow += done; // keep w_wrow updated
1431 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
1433#ifdef FEAT_DIFF
1434 if (curwin->w_cursor.lnum == curwin->w_topline)
1435 curwin->w_cline_row = 0;
1436 check_topfill(curwin, TRUE);
1437#endif
1438
1439 /*
1440 * Compute the row number of the last row of the cursor line
1441 * and move the cursor onto the displayed part of the window.
1442 */
1443 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001444 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
1446 validate_virtcol();
1447 validate_cheight();
1448 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001449 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1452 {
1453#ifdef FEAT_FOLDING
1454 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1455 {
1456 --wrow;
1457 if (first == 1)
1458 curwin->w_cursor.lnum = 1;
1459 else
1460 curwin->w_cursor.lnum = first - 1;
1461 }
1462 else
1463#endif
1464 wrow -= plines(curwin->w_cursor.lnum--);
1465 curwin->w_valid &=
1466 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1467 moved = TRUE;
1468 }
1469 if (moved)
1470 {
1471#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001472 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 foldAdjustCursor();
1474#endif
1475 coladvance(curwin->w_curswant);
1476 }
1477}
1478
1479/*
1480 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001483scrollup(
1484 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001485 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1488 linenr_T lnum;
1489
1490 if (
1491# ifdef FEAT_FOLDING
1492 (byfold && hasAnyFolding(curwin))
1493# ifdef FEAT_DIFF
1494 ||
1495# endif
1496# endif
1497# ifdef FEAT_DIFF
1498 curwin->w_p_diff
1499# endif
1500 )
1501 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001502 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 lnum = curwin->w_topline;
1504 while (line_count--)
1505 {
1506# ifdef FEAT_DIFF
1507 if (curwin->w_topfill > 0)
1508 --curwin->w_topfill;
1509 else
1510# endif
1511 {
1512# ifdef FEAT_FOLDING
1513 if (byfold)
1514 (void)hasFolding(lnum, NULL, &lnum);
1515# endif
1516 if (lnum >= curbuf->b_ml.ml_line_count)
1517 break;
1518 ++lnum;
1519# ifdef FEAT_DIFF
1520 curwin->w_topfill = diff_check_fill(curwin, lnum);
1521# endif
1522 }
1523 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001524 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 curwin->w_botline += lnum - curwin->w_topline;
1526 curwin->w_topline = lnum;
1527 }
1528 else
1529#endif
1530 {
1531 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001532 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534
1535 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1536 curwin->w_topline = curbuf->b_ml.ml_line_count;
1537 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1538 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1539
1540#ifdef FEAT_DIFF
1541 check_topfill(curwin, FALSE);
1542#endif
1543
1544#ifdef FEAT_FOLDING
1545 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001546 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1548#endif
1549
1550 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1551 if (curwin->w_cursor.lnum < curwin->w_topline)
1552 {
1553 curwin->w_cursor.lnum = curwin->w_topline;
1554 curwin->w_valid &=
1555 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1556 coladvance(curwin->w_curswant);
1557 }
1558}
1559
1560#ifdef FEAT_DIFF
1561/*
1562 * Don't end up with too many filler lines in the window.
1563 */
1564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001565check_topfill(
1566 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001567 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 int n;
1570
1571 if (wp->w_topfill > 0)
1572 {
1573 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1574 if (wp->w_topfill + n > wp->w_height)
1575 {
1576 if (down && wp->w_topline > 1)
1577 {
1578 --wp->w_topline;
1579 wp->w_topfill = 0;
1580 }
1581 else
1582 {
1583 wp->w_topfill = wp->w_height - n;
1584 if (wp->w_topfill < 0)
1585 wp->w_topfill = 0;
1586 }
1587 }
1588 }
1589}
1590
1591/*
1592 * Use as many filler lines as possible for w_topline. Make sure w_topline
1593 * is still visible.
1594 */
1595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001596max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 int n;
1599
1600 n = plines_nofill(curwin->w_topline);
1601 if (n >= curwin->w_height)
1602 curwin->w_topfill = 0;
1603 else
1604 {
1605 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1606 if (curwin->w_topfill + n > curwin->w_height)
1607 curwin->w_topfill = curwin->w_height - n;
1608 }
1609}
1610#endif
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612/*
1613 * Scroll the screen one line down, but don't do it if it would move the
1614 * cursor off the screen.
1615 */
1616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001617scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 int end_row;
1620#ifdef FEAT_DIFF
1621 int can_fill = (curwin->w_topfill
1622 < diff_check_fill(curwin, curwin->w_topline));
1623#endif
1624
1625 if (curwin->w_topline <= 1
1626#ifdef FEAT_DIFF
1627 && !can_fill
1628#endif
1629 )
1630 return;
1631
Bram Moolenaar85a20022019-12-21 18:25:54 +01001632 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634 /*
1635 * Compute the row number of the last row of the cursor line
1636 * and make sure it doesn't go off the screen. Make sure the cursor
1637 * doesn't go past 'scrolloff' lines from the screen end.
1638 */
1639 end_row = curwin->w_wrow;
1640#ifdef FEAT_DIFF
1641 if (can_fill)
1642 ++end_row;
1643 else
1644 end_row += plines_nofill(curwin->w_topline - 1);
1645#else
1646 end_row += plines(curwin->w_topline - 1);
1647#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001648 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 validate_cheight();
1651 validate_virtcol();
1652 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001653 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001655 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657#ifdef FEAT_DIFF
1658 if (can_fill)
1659 {
1660 ++curwin->w_topfill;
1661 check_topfill(curwin, TRUE);
1662 }
1663 else
1664 {
1665 --curwin->w_topline;
1666 curwin->w_topfill = 0;
1667 }
1668#else
1669 --curwin->w_topline;
1670#endif
1671#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001672 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001674 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1676 }
1677}
1678
1679/*
1680 * Scroll the screen one line up, but don't do it if it would move the cursor
1681 * off the screen.
1682 */
1683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001684scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 int start_row;
1687
1688 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1689#ifdef FEAT_DIFF
1690 && curwin->w_topfill == 0
1691#endif
1692 )
1693 return;
1694
Bram Moolenaar85a20022019-12-21 18:25:54 +01001695 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
1697 /*
1698 * Compute the row number of the first row of the cursor line
1699 * and make sure it doesn't go off the screen. Make sure the cursor
1700 * doesn't go before 'scrolloff' lines from the screen start.
1701 */
1702#ifdef FEAT_DIFF
1703 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1704 - curwin->w_topfill;
1705#else
1706 start_row = curwin->w_wrow - plines(curwin->w_topline);
1707#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001708 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
1710 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001711 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001713 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
1715#ifdef FEAT_DIFF
1716 if (curwin->w_topfill > 0)
1717 --curwin->w_topfill;
1718 else
1719#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001720 {
1721#ifdef FEAT_FOLDING
1722 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001725 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001726 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1728 }
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731/*
1732 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1733 * a (wrapped) text line. Uses and sets "lp->fill".
1734 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001735 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 */
1737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001738topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740#ifdef FEAT_DIFF
1741 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1742 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001743 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 ++lp->fill;
1745 lp->height = 1;
1746 }
1747 else
1748#endif
1749 {
1750 --lp->lnum;
1751#ifdef FEAT_DIFF
1752 lp->fill = 0;
1753#endif
1754 if (lp->lnum < 1)
1755 lp->height = MAXCOL;
1756 else
1757#ifdef FEAT_FOLDING
1758 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001759 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 lp->height = 1;
1761 else
1762#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001763 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
1765}
1766
1767/*
1768 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1769 * a (wrapped) text line. Uses and sets "lp->fill".
1770 * Returns the height of the added line in "lp->height".
1771 * Lines below the last one are incredibly high.
1772 */
1773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001774botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776#ifdef FEAT_DIFF
1777 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1778 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 ++lp->fill;
1781 lp->height = 1;
1782 }
1783 else
1784#endif
1785 {
1786 ++lp->lnum;
1787#ifdef FEAT_DIFF
1788 lp->fill = 0;
1789#endif
1790 if (lp->lnum > curbuf->b_ml.ml_line_count)
1791 lp->height = MAXCOL;
1792 else
1793#ifdef FEAT_FOLDING
1794 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001795 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 lp->height = 1;
1797 else
1798#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001799 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 }
1801}
1802
1803#ifdef FEAT_DIFF
1804/*
1805 * Switch from including filler lines below lp->lnum to including filler
1806 * lines above loff.lnum + 1. This keeps pointing to the same line.
1807 * When there are no filler lines nothing changes.
1808 */
1809 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001810botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812 if (lp->fill > 0)
1813 {
1814 ++lp->lnum;
1815 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1816 }
1817}
1818
1819/*
1820 * Switch from including filler lines above lp->lnum to including filler
1821 * lines below loff.lnum - 1. This keeps pointing to the same line.
1822 * When there are no filler lines nothing changes.
1823 */
1824 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001825topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 if (lp->fill > 0)
1828 {
1829 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1830 --lp->lnum;
1831 }
1832}
1833#endif
1834
1835/*
1836 * Recompute topline to put the cursor at the top of the window.
1837 * Scroll at least "min_scroll" lines.
1838 * If "always" is TRUE, always set topline (for "zt").
1839 */
1840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001841scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
1843 int scrolled = 0;
1844 int extra = 0;
1845 int used;
1846 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001847 linenr_T top; // just above displayed lines
1848 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 linenr_T old_topline = curwin->w_topline;
1850#ifdef FEAT_DIFF
1851 linenr_T old_topfill = curwin->w_topfill;
1852#endif
1853 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001854 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (mouse_dragging > 0)
1857 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
1859 /*
1860 * Decrease topline until:
1861 * - it has become 1
1862 * - (part of) the cursor line is moved off the screen or
1863 * - moved at least 'scrolljump' lines and
1864 * - at least 'scrolloff' lines above and below the cursor
1865 */
1866 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001867 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (curwin->w_cursor.lnum < curwin->w_topline)
1869 scrolled = used;
1870
1871#ifdef FEAT_FOLDING
1872 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1873 {
1874 --top;
1875 ++bot;
1876 }
1877 else
1878#endif
1879 {
1880 top = curwin->w_cursor.lnum - 1;
1881 bot = curwin->w_cursor.lnum + 1;
1882 }
1883 new_topline = top + 1;
1884
1885#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001886 // "used" already contains the number of filler lines above, don't add it
1887 // again.
1888 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001889 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890#endif
1891
1892 /*
1893 * Check if the lines from "top" to "bot" fit in the window. If they do,
1894 * set new_topline and advance "top" and "bot" to include more lines.
1895 */
1896 while (top > 0)
1897 {
1898#ifdef FEAT_FOLDING
1899 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001900 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 i = 1;
1902 else
1903#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001904 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 used += i;
1906 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1907 {
1908#ifdef FEAT_FOLDING
1909 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001910 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 ++used;
1912 else
1913#endif
1914 used += plines(bot);
1915 }
1916 if (used > curwin->w_height)
1917 break;
1918 if (top < curwin->w_topline)
1919 scrolled += i;
1920
1921 /*
1922 * If scrolling is needed, scroll at least 'sj' lines.
1923 */
1924 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1925 && extra >= off)
1926 break;
1927
1928 extra += i;
1929 new_topline = top;
1930 --top;
1931 ++bot;
1932 }
1933
1934 /*
1935 * If we don't have enough space, put cursor in the middle.
1936 * This makes sure we get the same position when using "k" and "j"
1937 * in a small window.
1938 */
1939 if (used > curwin->w_height)
1940 scroll_cursor_halfway(FALSE);
1941 else
1942 {
1943 /*
1944 * If "always" is FALSE, only adjust topline to a lower value, higher
1945 * value may happen with wrapping lines
1946 */
1947 if (new_topline < curwin->w_topline || always)
1948 curwin->w_topline = new_topline;
1949 if (curwin->w_topline > curwin->w_cursor.lnum)
1950 curwin->w_topline = curwin->w_cursor.lnum;
1951#ifdef FEAT_DIFF
1952 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1953 if (curwin->w_topfill > 0 && extra > off)
1954 {
1955 curwin->w_topfill -= extra - off;
1956 if (curwin->w_topfill < 0)
1957 curwin->w_topfill = 0;
1958 }
1959 check_topfill(curwin, FALSE);
1960#endif
1961 if (curwin->w_topline != old_topline
1962#ifdef FEAT_DIFF
1963 || curwin->w_topfill != old_topfill
1964#endif
1965 )
1966 curwin->w_valid &=
1967 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1968 curwin->w_valid |= VALID_TOPLINE;
1969 }
1970}
1971
1972/*
1973 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1974 * screen lines for text lines.
1975 */
1976 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001977set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
1979#ifdef FEAT_DIFF
1980 wp->w_filler_rows = 0;
1981#endif
1982 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001983 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 else
1985 {
1986 wp->w_empty_rows = wp->w_height - used;
1987#ifdef FEAT_DIFF
1988 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1989 {
1990 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1991 if (wp->w_empty_rows > wp->w_filler_rows)
1992 wp->w_empty_rows -= wp->w_filler_rows;
1993 else
1994 {
1995 wp->w_filler_rows = wp->w_empty_rows;
1996 wp->w_empty_rows = 0;
1997 }
1998 }
1999#endif
2000 }
2001}
2002
2003/*
2004 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002005 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2007 * This is messy stuff!!!
2008 */
2009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002010scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 int used;
2013 int scrolled = 0;
2014 int extra = 0;
2015 int i;
2016 linenr_T line_count;
2017 linenr_T old_topline = curwin->w_topline;
2018 lineoff_T loff;
2019 lineoff_T boff;
2020#ifdef FEAT_DIFF
2021 int old_topfill = curwin->w_topfill;
2022 int fill_below_window;
2023#endif
2024 linenr_T old_botline = curwin->w_botline;
2025 linenr_T old_valid = curwin->w_valid;
2026 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002027 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002028 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
2030 cln = curwin->w_cursor.lnum;
2031 if (set_topbot)
2032 {
2033 used = 0;
2034 curwin->w_botline = cln + 1;
2035#ifdef FEAT_DIFF
2036 loff.fill = 0;
2037#endif
2038 for (curwin->w_topline = curwin->w_botline;
2039 curwin->w_topline > 1;
2040 curwin->w_topline = loff.lnum)
2041 {
2042 loff.lnum = curwin->w_topline;
2043 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002044 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 break;
2046 used += loff.height;
2047#ifdef FEAT_DIFF
2048 curwin->w_topfill = loff.fill;
2049#endif
2050 }
2051 set_empty_rows(curwin, used);
2052 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2053 if (curwin->w_topline != old_topline
2054#ifdef FEAT_DIFF
2055 || curwin->w_topfill != old_topfill
2056#endif
2057 )
2058 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2059 }
2060 else
2061 validate_botline();
2062
Bram Moolenaar85a20022019-12-21 18:25:54 +01002063 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064#ifdef FEAT_DIFF
2065 used = plines_nofill(cln);
2066#else
2067 validate_cheight();
2068 used = curwin->w_cline_height;
2069#endif
2070
Bram Moolenaar85a20022019-12-21 18:25:54 +01002071 // If the cursor is below botline, we will at least scroll by the height
2072 // of the cursor line. Correct for empty lines, which are really part of
2073 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (cln >= curwin->w_botline)
2075 {
2076 scrolled = used;
2077 if (cln == curwin->w_botline)
2078 scrolled -= curwin->w_empty_rows;
2079 }
2080
2081 /*
2082 * Stop counting lines to scroll when
2083 * - hitting start of the file
2084 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002085 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 * - lines between botline and cursor have been counted
2087 */
2088#ifdef FEAT_FOLDING
2089 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2090#endif
2091 {
2092 loff.lnum = cln;
2093 boff.lnum = cln;
2094 }
2095#ifdef FEAT_DIFF
2096 loff.fill = 0;
2097 boff.fill = 0;
2098 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2099 - curwin->w_filler_rows;
2100#endif
2101
2102 while (loff.lnum > 1)
2103 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002104 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2105 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002107 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2109 && 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 break;
2116
Bram Moolenaar85a20022019-12-21 18:25:54 +01002117 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002119 if (loff.height == MAXCOL)
2120 used = MAXCOL;
2121 else
2122 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (used > curwin->w_height)
2124 break;
2125 if (loff.lnum >= curwin->w_botline
2126#ifdef FEAT_DIFF
2127 && (loff.lnum > curwin->w_botline
2128 || loff.fill <= fill_below_window)
2129#endif
2130 )
2131 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002132 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 scrolled += loff.height;
2134 if (loff.lnum == curwin->w_botline
2135#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002136 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137#endif
2138 )
2139 scrolled -= curwin->w_empty_rows;
2140 }
2141
2142 if (boff.lnum < curbuf->b_ml.ml_line_count)
2143 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002144 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 botline_forw(&boff);
2146 used += boff.height;
2147 if (used > curwin->w_height)
2148 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002149 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2150 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 extra += boff.height;
2153 if (boff.lnum >= curwin->w_botline
2154#ifdef FEAT_DIFF
2155 || (boff.lnum + 1 == curwin->w_botline
2156 && boff.fill > curwin->w_filler_rows)
2157#endif
2158 )
2159 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002160 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 scrolled += boff.height;
2162 if (boff.lnum == curwin->w_botline
2163#ifdef FEAT_DIFF
2164 && boff.fill == 0
2165#endif
2166 )
2167 scrolled -= curwin->w_empty_rows;
2168 }
2169 }
2170 }
2171 }
2172
Bram Moolenaar85a20022019-12-21 18:25:54 +01002173 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (scrolled <= 0)
2175 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002176 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 else if (used > curwin->w_height)
2178 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002179 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 else
2181 {
2182 line_count = 0;
2183#ifdef FEAT_DIFF
2184 boff.fill = curwin->w_topfill;
2185#endif
2186 boff.lnum = curwin->w_topline - 1;
2187 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2188 {
2189 botline_forw(&boff);
2190 i += boff.height;
2191 ++line_count;
2192 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002193 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 line_count = 9999;
2195 }
2196
2197 /*
2198 * Scroll up if the cursor is off the bottom of the screen a bit.
2199 * Otherwise put it at 1/2 of the screen.
2200 */
2201 if (line_count >= curwin->w_height && line_count > min_scroll)
2202 scroll_cursor_halfway(FALSE);
2203 else
2204 scrollup(line_count, TRUE);
2205
2206 /*
2207 * If topline didn't change we need to restore w_botline and w_empty_rows
2208 * (we changed them).
2209 * If topline did change, update_screen() will set botline.
2210 */
2211 if (curwin->w_topline == old_topline && set_topbot)
2212 {
2213 curwin->w_botline = old_botline;
2214 curwin->w_empty_rows = old_empty_rows;
2215 curwin->w_valid = old_valid;
2216 }
2217 curwin->w_valid |= VALID_TOPLINE;
2218}
2219
2220/*
2221 * Recompute topline to put the cursor halfway the window
2222 * If "atend" is TRUE, also put it halfway at the end of the file.
2223 */
2224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002225scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227 int above = 0;
2228 linenr_T topline;
2229#ifdef FEAT_DIFF
2230 int topfill = 0;
2231#endif
2232 int below = 0;
2233 int used;
2234 lineoff_T loff;
2235 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002236#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002237 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002240#ifdef FEAT_PROP_POPUP
2241 // if the width changed this needs to be updated first
2242 may_update_popup_position();
2243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2245#ifdef FEAT_FOLDING
2246 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2247#endif
2248#ifdef FEAT_DIFF
2249 used = plines_nofill(loff.lnum);
2250 loff.fill = 0;
2251 boff.fill = 0;
2252#else
2253 used = plines(loff.lnum);
2254#endif
2255 topline = loff.lnum;
2256 while (topline > 1)
2257 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002258 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
2260 if (boff.lnum < curbuf->b_ml.ml_line_count)
2261 {
2262 botline_forw(&boff);
2263 used += boff.height;
2264 if (used > curwin->w_height)
2265 break;
2266 below += boff.height;
2267 }
2268 else
2269 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002270 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (atend)
2272 ++used;
2273 }
2274 }
2275
Bram Moolenaar85a20022019-12-21 18:25:54 +01002276 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002279 if (loff.height == MAXCOL)
2280 used = MAXCOL;
2281 else
2282 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (used > curwin->w_height)
2284 break;
2285 above += loff.height;
2286 topline = loff.lnum;
2287#ifdef FEAT_DIFF
2288 topfill = loff.fill;
2289#endif
2290 }
2291 }
2292#ifdef FEAT_FOLDING
2293 if (!hasFolding(topline, &curwin->w_topline, NULL))
2294#endif
2295 curwin->w_topline = topline;
2296#ifdef FEAT_DIFF
2297 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002298 if (old_topline > curwin->w_topline + curwin->w_height)
2299 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 check_topfill(curwin, FALSE);
2301#endif
2302 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2303 curwin->w_valid |= VALID_TOPLINE;
2304}
2305
2306/*
2307 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002308 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * If not possible, put it at the same position as scroll_cursor_halfway().
2310 * When called topline must be valid!
2311 */
2312 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002313cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002315 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002317 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 linenr_T botline;
2319 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002320 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002322 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324 /*
2325 * How many lines we would like to have above/below the cursor depends on
2326 * whether the first/last line of the file is on screen.
2327 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002328 above_wanted = so;
2329 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002330 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332 above_wanted = mouse_dragging - 1;
2333 below_wanted = mouse_dragging - 1;
2334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (curwin->w_topline == 1)
2336 {
2337 above_wanted = 0;
2338 max_off = curwin->w_height / 2;
2339 if (below_wanted > max_off)
2340 below_wanted = max_off;
2341 }
2342 validate_botline();
2343 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002344 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 below_wanted = 0;
2347 max_off = (curwin->w_height - 1) / 2;
2348 if (above_wanted > max_off)
2349 above_wanted = max_off;
2350 }
2351
2352 /*
2353 * If there are sufficient file-lines above and below the cursor, we can
2354 * return now.
2355 */
2356 cln = curwin->w_cursor.lnum;
2357 if (cln >= curwin->w_topline + above_wanted
2358 && cln < curwin->w_botline - below_wanted
2359#ifdef FEAT_FOLDING
2360 && !hasAnyFolding(curwin)
2361#endif
2362 )
2363 return;
2364
2365 /*
2366 * Narrow down the area where the cursor can be put by taking lines from
2367 * the top and the bottom until:
2368 * - the desired context lines are found
2369 * - the lines from the top is past the lines from the bottom
2370 */
2371 topline = curwin->w_topline;
2372 botline = curwin->w_botline - 1;
2373#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002374 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 above = curwin->w_topfill;
2376 below = curwin->w_filler_rows;
2377#endif
2378 while ((above < above_wanted || below < below_wanted) && topline < botline)
2379 {
2380 if (below < below_wanted && (below <= above || above >= above_wanted))
2381 {
2382#ifdef FEAT_FOLDING
2383 if (hasFolding(botline, &botline, NULL))
2384 ++below;
2385 else
2386#endif
2387 below += plines(botline);
2388 --botline;
2389 }
2390 if (above < above_wanted && (above < below || below >= below_wanted))
2391 {
2392#ifdef FEAT_FOLDING
2393 if (hasFolding(topline, NULL, &topline))
2394 ++above;
2395 else
2396#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002397 above += PLINES_NOFILL(topline);
2398#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002399 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (topline < botline)
2401 above += diff_check_fill(curwin, topline + 1);
2402#endif
2403 ++topline;
2404 }
2405 }
2406 if (topline == botline || botline == 0)
2407 curwin->w_cursor.lnum = topline;
2408 else if (topline > botline)
2409 curwin->w_cursor.lnum = botline;
2410 else
2411 {
2412 if (cln < topline && curwin->w_topline > 1)
2413 {
2414 curwin->w_cursor.lnum = topline;
2415 curwin->w_valid &=
2416 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2417 }
2418 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2419 {
2420 curwin->w_cursor.lnum = botline;
2421 curwin->w_valid &=
2422 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2423 }
2424 }
2425 curwin->w_valid |= VALID_TOPLINE;
2426}
2427
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002428static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430/*
2431 * move screen 'count' pages up or down and update screen
2432 *
2433 * return FAIL for failure, OK otherwise
2434 */
2435 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002436onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 long n;
2439 int retval = OK;
2440 lineoff_T loff;
2441 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002442 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
Bram Moolenaar85a20022019-12-21 18:25:54 +01002444 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 beep_flush();
2447 return FAIL;
2448 }
2449
2450 for ( ; count > 0; --count)
2451 {
2452 validate_botline();
2453 /*
2454 * It's an error to move a page up when the first line is already on
2455 * the screen. It's an error to move a page down when the last line
2456 * is on the screen and the topline is 'scrolloff' lines from the
2457 * last line.
2458 */
2459 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002460 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2462 : (curwin->w_topline == 1
2463#ifdef FEAT_DIFF
2464 && curwin->w_topfill ==
2465 diff_check_fill(curwin, curwin->w_topline)
2466#endif
2467 ))
2468 {
2469 beep_flush();
2470 retval = FAIL;
2471 break;
2472 }
2473
2474#ifdef FEAT_DIFF
2475 loff.fill = 0;
2476#endif
2477 if (dir == FORWARD)
2478 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002479 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002481 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002482 if (p_window <= 2)
2483 ++curwin->w_topline;
2484 else
2485 curwin->w_topline += p_window - 2;
2486 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2487 curwin->w_topline = curbuf->b_ml.ml_line_count;
2488 curwin->w_cursor.lnum = curwin->w_topline;
2489 }
2490 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2491 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002492 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 curwin->w_topline = curbuf->b_ml.ml_line_count;
2494#ifdef FEAT_DIFF
2495 curwin->w_topfill = 0;
2496#endif
2497 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2498 }
2499 else
2500 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002501 // For the overlap, start with the line just below the window
2502 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 loff.lnum = curwin->w_botline;
2504#ifdef FEAT_DIFF
2505 loff.fill = diff_check_fill(curwin, loff.lnum)
2506 - curwin->w_filler_rows;
2507#endif
2508 get_scroll_overlap(&loff, -1);
2509 curwin->w_topline = loff.lnum;
2510#ifdef FEAT_DIFF
2511 curwin->w_topfill = loff.fill;
2512 check_topfill(curwin, FALSE);
2513#endif
2514 curwin->w_cursor.lnum = curwin->w_topline;
2515 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2516 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2517 }
2518 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002519 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521#ifdef FEAT_DIFF
2522 if (curwin->w_topline == 1)
2523 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002524 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 max_topfill();
2526 continue;
2527 }
2528#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002529 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002530 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002531 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002532 if (p_window <= 2)
2533 --curwin->w_topline;
2534 else
2535 curwin->w_topline -= p_window - 2;
2536 if (curwin->w_topline < 1)
2537 curwin->w_topline = 1;
2538 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2539 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2540 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2541 continue;
2542 }
2543
Bram Moolenaar85a20022019-12-21 18:25:54 +01002544 // Find the line at the top of the window that is going to be the
2545 // line at the bottom of the window. Make sure this results in
2546 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 loff.lnum = curwin->w_topline - 1;
2548#ifdef FEAT_DIFF
2549 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2550 - curwin->w_topfill;
2551#endif
2552 get_scroll_overlap(&loff, 1);
2553
2554 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2555 {
2556 loff.lnum = curbuf->b_ml.ml_line_count;
2557#ifdef FEAT_DIFF
2558 loff.fill = 0;
2559 }
2560 else
2561 {
2562 botline_topline(&loff);
2563#endif
2564 }
2565 curwin->w_cursor.lnum = loff.lnum;
2566
Bram Moolenaar85a20022019-12-21 18:25:54 +01002567 // Find the line just above the new topline to get the right line
2568 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 n = 0;
2570 while (n <= curwin->w_height && loff.lnum >= 1)
2571 {
2572 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002573 if (loff.height == MAXCOL)
2574 n = MAXCOL;
2575 else
2576 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002578 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
2580 curwin->w_topline = 1;
2581#ifdef FEAT_DIFF
2582 max_topfill();
2583#endif
2584 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2585 }
2586 else
2587 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002588 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589#ifdef FEAT_DIFF
2590 topline_botline(&loff);
2591#endif
2592 botline_forw(&loff);
2593 botline_forw(&loff);
2594#ifdef FEAT_DIFF
2595 botline_topline(&loff);
2596#endif
2597#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002598 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2600#endif
2601
Bram Moolenaar85a20022019-12-21 18:25:54 +01002602 // Always scroll at least one line. Avoid getting stuck on
2603 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (loff.lnum >= curwin->w_topline
2605#ifdef FEAT_DIFF
2606 && (loff.lnum > curwin->w_topline
2607 || loff.fill >= curwin->w_topfill)
2608#endif
2609 )
2610 {
2611#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002612 // First try using the maximum number of filler lines. If
2613 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 loff.fill = curwin->w_topfill;
2615 if (curwin->w_topfill < diff_check_fill(curwin,
2616 curwin->w_topline))
2617 max_topfill();
2618 if (curwin->w_topfill == loff.fill)
2619#endif
2620 {
2621 --curwin->w_topline;
2622#ifdef FEAT_DIFF
2623 curwin->w_topfill = 0;
2624#endif
2625 }
2626 comp_botline(curwin);
2627 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002628 curwin->w_valid &=
2629 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 else
2632 {
2633 curwin->w_topline = loff.lnum;
2634#ifdef FEAT_DIFF
2635 curwin->w_topfill = loff.fill;
2636 check_topfill(curwin, FALSE);
2637#endif
2638 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2639 }
2640 }
2641 }
2642 }
2643#ifdef FEAT_FOLDING
2644 foldAdjustCursor();
2645#endif
2646 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002647 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002648 if (retval == OK)
2649 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2651
Bram Moolenaar907dad72018-07-10 15:07:15 +02002652 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002654 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2655 // But make sure we scroll at least one line (happens with mix of long
2656 // wrapping lines and non-wrapping line).
2657 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002659 scroll_cursor_top(1, FALSE);
2660 if (curwin->w_topline <= old_topline
2661 && old_topline < curbuf->b_ml.ml_line_count)
2662 {
2663 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002665 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2666#endif
2667 }
2668 }
2669#ifdef FEAT_FOLDING
2670 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
2674
2675 redraw_later(VALID);
2676 return retval;
2677}
2678
2679/*
2680 * Decide how much overlap to use for page-up or page-down scrolling.
2681 * This is symmetric, so that doing both keeps the same lines displayed.
2682 * Three lines are examined:
2683 *
2684 * before CTRL-F after CTRL-F / before CTRL-B
2685 * etc. l1
2686 * l1 last but one line ------------
2687 * l2 last text line l2 top text line
2688 * ------------- l3 second text line
2689 * l3 etc.
2690 */
2691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002692get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 int h1, h2, h3, h4;
2695 int min_height = curwin->w_height - 2;
2696 lineoff_T loff0, loff1, loff2;
2697
2698#ifdef FEAT_DIFF
2699 if (lp->fill > 0)
2700 lp->height = 1;
2701 else
2702 lp->height = plines_nofill(lp->lnum);
2703#else
2704 lp->height = plines(lp->lnum);
2705#endif
2706 h1 = lp->height;
2707 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002708 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 loff0 = *lp;
2711 if (dir > 0)
2712 botline_forw(lp);
2713 else
2714 topline_back(lp);
2715 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002716 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002718 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 return;
2720 }
2721
2722 loff1 = *lp;
2723 if (dir > 0)
2724 botline_forw(lp);
2725 else
2726 topline_back(lp);
2727 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002728 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002730 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return;
2732 }
2733
2734 loff2 = *lp;
2735 if (dir > 0)
2736 botline_forw(lp);
2737 else
2738 topline_back(lp);
2739 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002740 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002741 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002743 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744}
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746/*
2747 * Scroll 'scroll' lines up or down.
2748 */
2749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002750halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 long scrolled = 0;
2753 int i;
2754 int n;
2755 int room;
2756
2757 if (Prenum)
2758 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2759 curwin->w_height : Prenum;
2760 n = (curwin->w_p_scr <= curwin->w_height) ?
2761 curwin->w_p_scr : curwin->w_height;
2762
Bram Moolenaard5d37532017-03-27 23:02:07 +02002763 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 validate_botline();
2765 room = curwin->w_empty_rows;
2766#ifdef FEAT_DIFF
2767 room += curwin->w_filler_rows;
2768#endif
2769 if (flag)
2770 {
2771 /*
2772 * scroll the text up
2773 */
2774 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2775 {
2776#ifdef FEAT_DIFF
2777 if (curwin->w_topfill > 0)
2778 {
2779 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002780 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 --curwin->w_topfill;
2782 }
2783 else
2784#endif
2785 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002786 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 n -= i;
2788 if (n < 0 && scrolled > 0)
2789 break;
2790#ifdef FEAT_FOLDING
2791 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2792#endif
2793 ++curwin->w_topline;
2794#ifdef FEAT_DIFF
2795 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2796#endif
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2799 {
2800 ++curwin->w_cursor.lnum;
2801 curwin->w_valid &=
2802 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2806 scrolled += i;
2807
2808 /*
2809 * Correct w_botline for changed w_topline.
2810 * Won't work when there are filler lines.
2811 */
2812#ifdef FEAT_DIFF
2813 if (curwin->w_p_diff)
2814 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2815 else
2816#endif
2817 {
2818 room += i;
2819 do
2820 {
2821 i = plines(curwin->w_botline);
2822 if (i > room)
2823 break;
2824#ifdef FEAT_FOLDING
2825 (void)hasFolding(curwin->w_botline, NULL,
2826 &curwin->w_botline);
2827#endif
2828 ++curwin->w_botline;
2829 room -= i;
2830 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2831 }
2832 }
2833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 /*
2835 * When hit bottom of the file: move cursor down.
2836 */
2837 if (n > 0)
2838 {
2839# ifdef FEAT_FOLDING
2840 if (hasAnyFolding(curwin))
2841 {
2842 while (--n >= 0
2843 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2844 {
2845 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2846 &curwin->w_cursor.lnum);
2847 ++curwin->w_cursor.lnum;
2848 }
2849 }
2850 else
2851# endif
2852 curwin->w_cursor.lnum += n;
2853 check_cursor_lnum();
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
2856 else
2857 {
2858 /*
2859 * scroll the text down
2860 */
2861 while (n > 0 && curwin->w_topline > 1)
2862 {
2863#ifdef FEAT_DIFF
2864 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2865 {
2866 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002867 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 ++curwin->w_topfill;
2869 }
2870 else
2871#endif
2872 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002873 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 n -= i;
2875 if (n < 0 && scrolled > 0)
2876 break;
2877 --curwin->w_topline;
2878#ifdef FEAT_FOLDING
2879 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2880#endif
2881#ifdef FEAT_DIFF
2882 curwin->w_topfill = 0;
2883#endif
2884 }
2885 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2886 VALID_BOTLINE|VALID_BOTLINE_AP);
2887 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (curwin->w_cursor.lnum > 1)
2889 {
2890 --curwin->w_cursor.lnum;
2891 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 /*
2896 * When hit top of the file: move cursor up.
2897 */
2898 if (n > 0)
2899 {
2900 if (curwin->w_cursor.lnum <= (linenr_T)n)
2901 curwin->w_cursor.lnum = 1;
2902 else
2903# ifdef FEAT_FOLDING
2904 if (hasAnyFolding(curwin))
2905 {
2906 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2907 {
2908 --curwin->w_cursor.lnum;
2909 (void)hasFolding(curwin->w_cursor.lnum,
2910 &curwin->w_cursor.lnum, NULL);
2911 }
2912 }
2913 else
2914# endif
2915 curwin->w_cursor.lnum -= n;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 foldAdjustCursor();
2921# endif
2922#ifdef FEAT_DIFF
2923 check_topfill(curwin, !flag);
2924#endif
2925 cursor_correct();
2926 beginline(BL_SOL | BL_FIX);
2927 redraw_later(VALID);
2928}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002929
Bram Moolenaar860cae12010-06-05 23:22:07 +02002930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002931do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002932{
2933 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002934 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002935 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002936 colnr_T curswant = curwin->w_curswant;
2937 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002938 win_T *old_curwin = curwin;
2939 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002940 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002941 int old_VIsual_select = VIsual_select;
2942 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002943
2944 /*
2945 * loop through the cursorbound windows
2946 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002947 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002948 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002949 {
2950 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002951 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002952 if (curwin != old_curwin && curwin->w_p_crb)
2953 {
2954# ifdef FEAT_DIFF
2955 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002956 curwin->w_cursor.lnum =
2957 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002958 else
2959# endif
2960 curwin->w_cursor.lnum = line;
2961 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002962 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002963 curwin->w_curswant = curswant;
2964 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002965
Bram Moolenaar85a20022019-12-21 18:25:54 +01002966 // Make sure the cursor is in a valid position. Temporarily set
2967 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002968 restart_edit_save = restart_edit;
2969 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002970 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01002971
2972 // Avoid a scroll here for the cursor position, 'scrollbind' is
2973 // more important.
2974 if (!curwin->w_p_scb)
2975 validate_cursor();
2976
Bram Moolenaar61452852011-02-01 18:01:11 +01002977 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002978 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002979 if (has_mbyte)
2980 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002981 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002982
Bram Moolenaar85a20022019-12-21 18:25:54 +01002983 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002984 if (!curwin->w_p_scb)
2985 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002986 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002987 }
2988 }
2989
2990 /*
2991 * reset current-window
2992 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002993 VIsual_select = old_VIsual_select;
2994 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002995 curwin = old_curwin;
2996 curbuf = old_curbuf;
2997}