blob: 3d8356fe95863cef8aaa97decf1826dc78943f1d [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)
150 redraw_later(SOME_VALID);
151 // When 'cursorlineopt' contains "screenline" need to redraw with VALID.
152 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
153 redraw_later(VALID);
154 }
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 Moolenaar375e3392019-01-31 18:26:10 +0100188 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100189 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar85a20022019-12-21 18:25:54 +0100191 // If there is no valid screen and when the window height is zero just use
192 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200193 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200194 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100195 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200196 curwin->w_topline = curwin->w_cursor.lnum;
197 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200198 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200199 return;
200 }
201
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 check_cursor_moved(curwin);
203 if (curwin->w_valid & VALID_TOPLINE)
204 return;
205
Bram Moolenaar85a20022019-12-21 18:25:54 +0100206 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000207 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100208 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
210 old_topline = curwin->w_topline;
211#ifdef FEAT_DIFF
212 old_topfill = curwin->w_topfill;
213#endif
214
215 /*
216 * If the buffer is empty, always set topline to 1.
217 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100218 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 {
220 if (curwin->w_topline != 1)
221 redraw_later(NOT_VALID);
222 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 curwin->w_botline = 2;
224 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 }
227
228 /*
229 * If the cursor is above or near the top of the window, scroll the window
230 * to show the line the cursor is in, with 'scrolloff' context.
231 */
232 else
233 {
234 if (curwin->w_topline > 1)
235 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100236 // If the cursor is above topline, scrolling is always needed.
237 // If the cursor is far below topline and there is no folding,
238 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (curwin->w_cursor.lnum < curwin->w_topline)
240 check_topline = TRUE;
241 else if (check_top_offset())
242 check_topline = TRUE;
243 }
244#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100245 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
247 curwin->w_topline))
248 check_topline = TRUE;
249#endif
250
251 if (check_topline)
252 {
253 halfheight = curwin->w_height / 2 - 1;
254 if (halfheight < 2)
255 halfheight = 2;
256
257#ifdef FEAT_FOLDING
258 if (hasAnyFolding(curwin))
259 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100260 // Count the number of logical lines between the cursor and
261 // topline + scrolloff (approximation of how much will be
262 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 n = 0;
264 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100265 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 {
267 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100268 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
270 break;
271 (void)hasFolding(lnum, NULL, &lnum);
272 }
273 }
274 else
275#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100276 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
Bram Moolenaar85a20022019-12-21 18:25:54 +0100278 // If we weren't very close to begin with, we scroll to put the
279 // cursor in the middle of the window. Otherwise put the cursor
280 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (n >= halfheight)
282 scroll_cursor_halfway(FALSE);
283 else
284 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000285 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 check_botline = TRUE;
287 }
288 }
289
290 else
291 {
292#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100293 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
295#endif
296 check_botline = TRUE;
297 }
298 }
299
300 /*
301 * If the cursor is below the bottom of the window, scroll the window
302 * to put the cursor on the window.
303 * When w_botline is invalid, recompute it first, to avoid a redraw later.
304 * If w_botline was approximated, we might need a redraw later in a few
305 * cases, but we don't want to spend (a lot of) time recomputing w_botline
306 * for every small change.
307 */
308 if (check_botline)
309 {
310 if (!(curwin->w_valid & VALID_BOTLINE_AP))
311 validate_botline();
312
313 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
314 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000315 if (curwin->w_cursor.lnum < curwin->w_botline)
316 {
317 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100318 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifdef FEAT_FOLDING
320 || hasAnyFolding(curwin)
321#endif
322 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 lineoff_T loff;
325
Bram Moolenaar85a20022019-12-21 18:25:54 +0100326 // Cursor is (a few lines) above botline, check if there are
327 // 'scrolloff' window lines below the cursor. If not, need to
328 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 n = curwin->w_empty_rows;
330 loff.lnum = curwin->w_cursor.lnum;
331#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100332 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
334#endif
335#ifdef FEAT_DIFF
336 loff.fill = 0;
337 n += curwin->w_filler_rows;
338#endif
339 loff.height = 0;
340 while (loff.lnum < curwin->w_botline
341#ifdef FEAT_DIFF
342 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
343#endif
344 )
345 {
346 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100347 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 break;
349 botline_forw(&loff);
350 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100351 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100352 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000354 }
355 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000357 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (check_botline)
360 {
361#ifdef FEAT_FOLDING
362 if (hasAnyFolding(curwin))
363 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100364 // Count the number of logical lines between the cursor and
365 // botline - scrolloff (approximation of how much will be
366 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 line_count = 0;
368 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100369 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
371 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100372 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 if (lnum <= 0 || line_count > curwin->w_height + 1)
374 break;
375 (void)hasFolding(lnum, &lnum, NULL);
376 }
377 }
378 else
379#endif
380 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100381 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000383 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 else
385 scroll_cursor_halfway(FALSE);
386 }
387 }
388 }
389 curwin->w_valid |= VALID_TOPLINE;
390
391 /*
392 * Need to redraw when topline changed.
393 */
394 if (curwin->w_topline != old_topline
395#ifdef FEAT_DIFF
396 || curwin->w_topfill != old_topfill
397#endif
398 )
399 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100400 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000401 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 {
403 curwin->w_skipcol = 0;
404 redraw_later(NOT_VALID);
405 }
406 else
407 redraw_later(VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100408 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 if (curwin->w_cursor.lnum == curwin->w_topline)
410 validate_cursor();
411 }
412
Bram Moolenaar375e3392019-01-31 18:26:10 +0100413 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414}
415
416/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000417 * Return the scrolljump value to use for the current window.
418 * When 'scrolljump' is positive use it as-is.
419 * When 'scrolljump' is negative use it as a percentage of the window height.
420 */
421 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100422scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000423{
424 if (p_sj >= 0)
425 return (int)p_sj;
426 return (curwin->w_height * -p_sj) / 100;
427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
431 * current window.
432 */
433 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100434check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
436 lineoff_T loff;
437 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +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
492 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
493 wp->w_valid_cursor = wp->w_cursor;
494 wp->w_valid_leftcol = wp->w_leftcol;
495 }
496 else if (wp->w_cursor.col != wp->w_valid_cursor.col
497 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100498 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {
500 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
501 wp->w_valid_cursor.col = wp->w_cursor.col;
502 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 }
505}
506
507/*
508 * Call this function when some window settings have changed, which require
509 * the cursor position, botline and topline to be recomputed and the window to
510 * be redrawn. E.g, when changing the 'wrap' option or folding.
511 */
512 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100513changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514{
515 changed_window_setting_win(curwin);
516}
517
518 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100519changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521 wp->w_lines_valid = 0;
522 changed_line_abv_curs_win(wp);
523 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
524 redraw_win_later(wp, NOT_VALID);
525}
526
527/*
528 * Set wp->w_topline to a certain number.
529 */
530 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100531set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200533#ifdef FEAT_DIFF
534 linenr_T prev_topline = wp->w_topline;
535#endif
536
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100538 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
540#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100541 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100543 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
544 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000546 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200548 if (lnum != prev_topline)
549 // Keep the filler lines when the topline didn't change.
550 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#endif
552 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100553 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 redraw_later(VALID);
555}
556
557/*
558 * Call this function when the length of the cursor line (in screen
559 * characters) has changed, and the change is before the cursor.
560 * Need to take care of w_botline separately!
561 */
562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100563changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
566 |VALID_CHEIGHT|VALID_TOPLINE);
567}
568
569 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100570changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571{
572 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
573 |VALID_CHEIGHT|VALID_TOPLINE);
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Call this function when the length of a line (in screen characters) above
578 * the cursor have changed.
579 * Need to take care of w_botline separately!
580 */
581 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100582changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
584 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
585 |VALID_CHEIGHT|VALID_TOPLINE);
586}
587
588 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100589changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
592 |VALID_CHEIGHT|VALID_TOPLINE);
593}
594
595/*
596 * Make sure the value of curwin->w_botline is valid.
597 */
598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100599validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100601 validate_botline_win(curwin);
602}
603
604/*
605 * Make sure the value of wp->w_botline is valid.
606 */
607 void
608validate_botline_win(win_T *wp)
609{
610 if (!(wp->w_valid & VALID_BOTLINE))
611 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612}
613
614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 * Mark curwin->w_botline as invalid (because of some change in the buffer).
616 */
617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100618invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
620 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
621}
622
623 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100624invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
627}
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100630approximate_botline_win(
631 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 wp->w_valid &= ~VALID_BOTLINE;
634}
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636/*
637 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
638 */
639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 check_cursor_moved(curwin);
643 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
644 (VALID_WROW|VALID_WCOL));
645}
646
647/*
648 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
649 * w_topline must be valid, you may need to call update_topline() first!
650 */
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 check_cursor_moved(curwin);
655 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
656 curs_columns(TRUE);
657}
658
659#if defined(FEAT_GUI) || defined(PROTO)
660/*
661 * validate w_cline_row.
662 */
663 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100664validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665{
666 /*
667 * First make sure that w_topline is valid (after moving the cursor).
668 */
669 update_topline();
670 check_cursor_moved(curwin);
671 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100672 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673}
674#endif
675
676/*
677 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200678 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 */
680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100681curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 linenr_T lnum;
684 int i;
685 int all_invalid;
686 int valid;
687#ifdef FEAT_FOLDING
688 long fold_count;
689#endif
690
Bram Moolenaar85a20022019-12-21 18:25:54 +0100691 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 all_invalid = (!redrawing()
693 || wp->w_lines_valid == 0
694 || wp->w_lines[0].wl_lnum > wp->w_topline);
695 i = 0;
696 wp->w_cline_row = 0;
697 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
698 {
699 valid = FALSE;
700 if (!all_invalid && i < wp->w_lines_valid)
701 {
702 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100703 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 if (wp->w_lines[i].wl_lnum == lnum)
705 {
706#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100707 // Check for newly inserted lines below this row, in which
708 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (!wp->w_buffer->b_mod_set
710 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
711 || wp->w_buffer->b_mod_top
712 > wp->w_lines[i].wl_lastlnum + 1)
713#endif
714 valid = TRUE;
715 }
716 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100717 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 if (valid
720#ifdef FEAT_DIFF
721 && (lnum != wp->w_topline || !wp->w_p_diff)
722#endif
723 )
724 {
725#ifdef FEAT_FOLDING
726 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100727 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (lnum > wp->w_cursor.lnum)
729 break;
730#else
731 ++lnum;
732#endif
733 wp->w_cline_row += wp->w_lines[i].wl_size;
734 }
735 else
736 {
737#ifdef FEAT_FOLDING
738 fold_count = foldedCount(wp, lnum, NULL);
739 if (fold_count)
740 {
741 lnum += fold_count;
742 if (lnum > wp->w_cursor.lnum)
743 break;
744 ++wp->w_cline_row;
745 }
746 else
747#endif
748#ifdef FEAT_DIFF
749 if (lnum == wp->w_topline)
750 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
751 + wp->w_topfill;
752 else
753#endif
754 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
755 }
756 }
757
758 check_cursor_moved(wp);
759 if (!(wp->w_valid & VALID_CHEIGHT))
760 {
761 if (all_invalid
762 || i == wp->w_lines_valid
763 || (i < wp->w_lines_valid
764 && (!wp->w_lines[i].wl_valid
765 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
766 {
767#ifdef FEAT_DIFF
768 if (wp->w_cursor.lnum == wp->w_topline)
769 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
770 TRUE) + wp->w_topfill;
771 else
772#endif
773 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
774#ifdef FEAT_FOLDING
775 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
776 NULL, NULL, TRUE, NULL);
777#endif
778 }
779 else if (i > wp->w_lines_valid)
780 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100781 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 wp->w_cline_height = 0;
783#ifdef FEAT_FOLDING
784 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
785 NULL, NULL, TRUE, NULL);
786#endif
787 }
788 else
789 {
790 wp->w_cline_height = wp->w_lines[i].wl_size;
791#ifdef FEAT_FOLDING
792 wp->w_cline_folded = wp->w_lines[i].wl_folded;
793#endif
794 }
795 }
796
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100797 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
802/*
803 * Validate curwin->w_virtcol only.
804 */
805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100806validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 validate_virtcol_win(curwin);
809}
810
811/*
812 * Validate wp->w_virtcol only.
813 */
814 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100815validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 check_cursor_moved(wp);
818 if (!(wp->w_valid & VALID_VIRTCOL))
819 {
820 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000821#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100822 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000823#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100824 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 }
826}
827
828/*
829 * Validate curwin->w_cline_height only.
830 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100832validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
834 check_cursor_moved(curwin);
835 if (!(curwin->w_valid & VALID_CHEIGHT))
836 {
837#ifdef FEAT_DIFF
838 if (curwin->w_cursor.lnum == curwin->w_topline)
839 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
840 + curwin->w_topfill;
841 else
842#endif
843 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
844#ifdef FEAT_FOLDING
845 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
846#endif
847 curwin->w_valid |= VALID_CHEIGHT;
848 }
849}
850
851/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000852 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 */
854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100855validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856{
857 colnr_T off;
858 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100859 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861 validate_virtcol();
862 if (!(curwin->w_valid & VALID_WCOL))
863 {
864 col = curwin->w_virtcol;
865 off = curwin_col_off();
866 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200867 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar85a20022019-12-21 18:25:54 +0100869 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000870 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200871 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100872 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100873 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200874 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000875 if (col > (int)curwin->w_leftcol)
876 col -= curwin->w_leftcol;
877 else
878 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100882#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100883 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886}
887
888/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200889 * Compute offset of a window, occupied by absolute or relative line number,
890 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 */
892 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100893win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
Bram Moolenaar64486672010-05-16 15:46:46 +0200895 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896#ifdef FEAT_CMDWIN
897 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
898#endif
899#ifdef FEAT_FOLDING
900 + wp->w_p_fdc
901#endif
902#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200903 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905 );
906}
907
908 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100909curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 return win_col_off(curwin);
912}
913
914/*
915 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200916 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
917 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 */
919 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100920win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
Bram Moolenaar64486672010-05-16 15:46:46 +0200922 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000923 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 return 0;
925}
926
927 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100928curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 return win_col_off2(curwin);
931}
932
933/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100934 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 * Also updates curwin->w_wrow and curwin->w_cline_row.
936 * Also updates curwin->w_leftcol.
937 */
938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100939curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100940 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
942 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100943 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 int off_left, off_right;
945 int n;
946 int p_lines;
947 int width = 0;
948 int textwidth;
949 int new_leftcol;
950 colnr_T startcol;
951 colnr_T endcol;
952 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100953 long so = get_scrolloff_value();
954 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 /*
957 * First make sure that w_topline is valid (after moving the cursor).
958 */
959 update_topline();
960
961 /*
962 * Next make sure that w_cline_row is valid.
963 */
964 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100965 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
967 /*
968 * Compute the number of virtual columns.
969 */
970#ifdef FEAT_FOLDING
971 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100972 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
974 else
975#endif
976 getvvcol(curwin, &curwin->w_cursor,
977 &startcol, &(curwin->w_virtcol), &endcol);
978
Bram Moolenaar85a20022019-12-21 18:25:54 +0100979 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100981 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 extra = curwin_col_off();
984 curwin->w_wcol = curwin->w_virtcol + extra;
985 endcol += extra;
986
987 /*
988 * Now compute w_wrow, counting screen lines from w_cline_row.
989 */
990 curwin->w_wrow = curwin->w_cline_row;
991
Bram Moolenaar02631462017-09-22 15:20:32 +0200992 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (textwidth <= 0)
994 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100995 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +0200996 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +0200997 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +0200998 if (curwin->w_p_wrap)
999 curwin->w_wrow = curwin->w_height - 1;
1000 else
1001 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001003 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 {
1005 width = textwidth + curwin_col_off2();
1006
Bram Moolenaar85a20022019-12-21 18:25:54 +01001007 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001008 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001010#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001011 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001012#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001013
Bram Moolenaar85a20022019-12-21 18:25:54 +01001014 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001015 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 curwin->w_wcol -= n * width;
1017 curwin->w_wrow += n;
1018
1019#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001020 // When cursor wraps to first char of next line in Insert
1021 // mode, the 'showbreak' string isn't shown, backup to first
1022 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001023 sbr = get_showbreak_value(curwin);
1024 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001025 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 curwin->w_wcol = 0;
1027#endif
1028 }
1029 }
1030
Bram Moolenaar85a20022019-12-21 18:25:54 +01001031 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1032 // is not folded.
1033 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001034 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#ifdef FEAT_FOLDING
1036 && !curwin->w_cline_folded
1037#endif
1038 )
1039 {
1040 /*
1041 * If Cursor is left of the screen, scroll rightwards.
1042 * If Cursor is right of the screen, scroll leftwards
1043 * If we get closer to the edge than 'sidescrolloff', scroll a little
1044 * extra
1045 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001046 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001047 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001048 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (off_left < 0 || off_right > 0)
1050 {
1051 if (off_left < 0)
1052 diff = -off_left;
1053 else
1054 diff = off_right;
1055
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 // When far off or not enough room on either side, put cursor in
1057 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1059 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1060 else
1061 {
1062 if (diff < p_ss)
1063 diff = p_ss;
1064 if (off_left < 0)
1065 new_leftcol = curwin->w_leftcol - diff;
1066 else
1067 new_leftcol = curwin->w_leftcol + diff;
1068 }
1069 if (new_leftcol < 0)
1070 new_leftcol = 0;
1071 if (new_leftcol != (int)curwin->w_leftcol)
1072 {
1073 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001074 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 redraw_later(NOT_VALID);
1076 }
1077 }
1078 curwin->w_wcol -= curwin->w_leftcol;
1079 }
1080 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1081 curwin->w_wcol -= curwin->w_leftcol;
1082 else
1083 curwin->w_wcol = 0;
1084
1085#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001086 // Skip over filler lines. At the top use w_topfill, there
1087 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (curwin->w_cursor.lnum == curwin->w_topline)
1089 curwin->w_wrow += curwin->w_topfill;
1090 else
1091 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1092#endif
1093
1094 prev_skipcol = curwin->w_skipcol;
1095
1096 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if ((curwin->w_wrow >= curwin->w_height
1099 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001100 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 && (p_lines =
1102#ifdef FEAT_DIFF
1103 plines_win_nofill
1104#else
1105 plines_win
1106#endif
1107 (curwin, curwin->w_cursor.lnum, FALSE))
1108 - 1 >= curwin->w_height))
1109 && curwin->w_height != 0
1110 && curwin->w_cursor.lnum == curwin->w_topline
1111 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001112 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001114 // Cursor past end of screen. Happens with a single line that does
1115 // not fit on screen. Find a skipcol to show the text around the
1116 // cursor. Avoid scrolling all the time. compute value of "extra":
1117 // 1: Less than 'scrolloff' lines above
1118 // 2: Less than 'scrolloff' lines below
1119 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001121 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001123 // Compute last display line of the buffer line that we want at the
1124 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (p_lines == 0)
1126 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1127 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001128 if (p_lines > curwin->w_wrow + so)
1129 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 else
1131 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001132 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 extra += 2;
1134
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001135 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001137 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 n = curwin->w_virtcol / width;
1139 if (n > curwin->w_height / 2)
1140 n -= curwin->w_height / 2;
1141 else
1142 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001143 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (n > p_lines - curwin->w_height + 1)
1145 n = p_lines - curwin->w_height + 1;
1146 curwin->w_skipcol = n * width;
1147 }
1148 else if (extra == 1)
1149 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001150 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001151 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 + width - 1) / width;
1153 if (extra > 0)
1154 {
1155 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1156 extra = curwin->w_skipcol / width;
1157 curwin->w_skipcol -= extra * width;
1158 }
1159 }
1160 else if (extra == 2)
1161 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001162 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 endcol = (n - curwin->w_height + 1) * width;
1164 while (endcol > curwin->w_virtcol)
1165 endcol -= width;
1166 if (endcol > curwin->w_skipcol)
1167 curwin->w_skipcol = endcol;
1168 }
1169
1170 curwin->w_wrow -= curwin->w_skipcol / width;
1171 if (curwin->w_wrow >= curwin->w_height)
1172 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 extra = curwin->w_wrow - curwin->w_height + 1;
1175 curwin->w_skipcol += extra * width;
1176 curwin->w_wrow -= extra;
1177 }
1178
1179 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1180 if (extra > 0)
1181 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1182 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001183 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 else
1186 curwin->w_skipcol = 0;
1187 if (prev_skipcol != curwin->w_skipcol)
1188 redraw_later(NOT_VALID);
1189
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001190#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001191 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001192#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001193#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1194 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1195 {
1196 curwin->w_wrow += popup_top_extra(curwin);
1197 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001198 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001199 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001200 else
1201 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001202#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001203
Bram Moolenaar08f23632019-11-16 14:19:33 +01001204 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1205 curwin->w_valid_leftcol = curwin->w_leftcol;
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1208}
1209
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001210#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001211/*
1212 * Compute the screen position of text character at "pos" in window "wp"
1213 * The resulting values are one-based, zero when character is not visible.
1214 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001215 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001216textpos2screenpos(
1217 win_T *wp,
1218 pos_T *pos,
1219 int *rowp, // screen row
1220 int *scolp, // start screen column
1221 int *ccolp, // cursor screen column
1222 int *ecolp) // end screen column
1223{
1224 colnr_T scol = 0, ccol = 0, ecol = 0;
1225 int row = 0;
1226 int rowoff = 0;
1227 colnr_T coloff = 0;
1228
Bram Moolenaar189663b2021-07-21 18:04:56 +02001229 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001230 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001231 colnr_T off;
1232 colnr_T col;
1233 int width;
1234 linenr_T lnum = pos->lnum;
1235#ifdef FEAT_FOLDING
1236 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001237
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001238 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1239#endif
1240 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1241#ifdef FEAT_FOLDING
1242 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001243 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001244 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001245 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001246 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001247 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001248#endif
1249 {
1250 getvcol(wp, pos, &scol, &ccol, &ecol);
1251
1252 // similar to what is done in validate_cursor_col()
1253 col = scol;
1254 off = win_col_off(wp);
1255 col += off;
1256 width = wp->w_width - off + win_col_off2(wp);
1257
1258 // long line wrapping, adjust row
1259 if (wp->w_p_wrap
1260 && col >= (colnr_T)wp->w_width
1261 && width > 0)
1262 {
1263 // use same formula as what is used in curs_columns()
1264 rowoff = ((col - wp->w_width) / width + 1);
1265 col -= rowoff * width;
1266 }
1267 col -= wp->w_leftcol;
1268 if (col >= wp->w_width)
1269 col = -1;
1270 if (col >= 0 && row + rowoff <= wp->w_height)
1271 {
1272 coloff = col - scol + wp->w_wincol + 1;
1273 row += W_WINROW(wp);
1274 }
1275 else
1276 // character is left, right or below of the window
1277 row = rowoff = scol = ccol = ecol = 0;
1278 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001279 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001280 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001281 *scolp = scol + coloff;
1282 *ccolp = ccol + coloff;
1283 *ecolp = ecol + coloff;
1284}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001285#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001286
Bram Moolenaar12034e22019-08-25 22:25:02 +02001287#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001288/*
1289 * "screenpos({winid}, {lnum}, {col})" function
1290 */
1291 void
1292f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1293{
1294 dict_T *dict;
1295 win_T *wp;
1296 pos_T pos;
1297 int row = 0;
1298 int scol = 0, ccol = 0, ecol = 0;
1299
1300 if (rettv_dict_alloc(rettv) != OK)
1301 return;
1302 dict = rettv->vval.v_dict;
1303
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001304 if (in_vim9script()
1305 && (check_for_number_arg(argvars, 0) == FAIL
1306 || check_for_number_arg(argvars, 1) == FAIL
1307 || check_for_number_arg(argvars, 2) == FAIL))
1308 return;
1309
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001310 wp = find_win_by_nr_or_id(&argvars[0]);
1311 if (wp == NULL)
1312 return;
1313
1314 pos.lnum = tv_get_number(&argvars[1]);
1315 pos.col = tv_get_number(&argvars[2]) - 1;
1316 pos.coladd = 0;
1317 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1318
1319 dict_add_number(dict, "row", row);
1320 dict_add_number(dict, "col", scol);
1321 dict_add_number(dict, "curscol", ccol);
1322 dict_add_number(dict, "endcol", ecol);
1323}
1324#endif
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326/*
1327 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001330scrolldown(
1331 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001332 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001334 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 int wrow;
1336 int moved = FALSE;
1337
1338#ifdef FEAT_FOLDING
1339 linenr_T first;
1340
Bram Moolenaar85a20022019-12-21 18:25:54 +01001341 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1343#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001344 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 while (line_count-- > 0)
1346 {
1347#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001348 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1349 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 ++curwin->w_topfill;
1352 ++done;
1353 }
1354 else
1355#endif
1356 {
1357 if (curwin->w_topline == 1)
1358 break;
1359 --curwin->w_topline;
1360#ifdef FEAT_DIFF
1361 curwin->w_topfill = 0;
1362#endif
1363#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001364 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (hasFolding(curwin->w_topline, &first, NULL))
1366 {
1367 ++done;
1368 if (!byfold)
1369 line_count -= curwin->w_topline - first - 1;
1370 curwin->w_botline -= curwin->w_topline - first;
1371 curwin->w_topline = first;
1372 }
1373 else
1374#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001375 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001377 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 invalidate_botline();
1379 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001380 curwin->w_wrow += done; // keep w_wrow updated
1381 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
1383#ifdef FEAT_DIFF
1384 if (curwin->w_cursor.lnum == curwin->w_topline)
1385 curwin->w_cline_row = 0;
1386 check_topfill(curwin, TRUE);
1387#endif
1388
1389 /*
1390 * Compute the row number of the last row of the cursor line
1391 * and move the cursor onto the displayed part of the window.
1392 */
1393 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001394 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 validate_virtcol();
1397 validate_cheight();
1398 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001399 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1402 {
1403#ifdef FEAT_FOLDING
1404 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1405 {
1406 --wrow;
1407 if (first == 1)
1408 curwin->w_cursor.lnum = 1;
1409 else
1410 curwin->w_cursor.lnum = first - 1;
1411 }
1412 else
1413#endif
1414 wrow -= plines(curwin->w_cursor.lnum--);
1415 curwin->w_valid &=
1416 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1417 moved = TRUE;
1418 }
1419 if (moved)
1420 {
1421#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001422 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 foldAdjustCursor();
1424#endif
1425 coladvance(curwin->w_curswant);
1426 }
1427}
1428
1429/*
1430 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001433scrollup(
1434 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001435 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
1437#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1438 linenr_T lnum;
1439
1440 if (
1441# ifdef FEAT_FOLDING
1442 (byfold && hasAnyFolding(curwin))
1443# ifdef FEAT_DIFF
1444 ||
1445# endif
1446# endif
1447# ifdef FEAT_DIFF
1448 curwin->w_p_diff
1449# endif
1450 )
1451 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001452 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 lnum = curwin->w_topline;
1454 while (line_count--)
1455 {
1456# ifdef FEAT_DIFF
1457 if (curwin->w_topfill > 0)
1458 --curwin->w_topfill;
1459 else
1460# endif
1461 {
1462# ifdef FEAT_FOLDING
1463 if (byfold)
1464 (void)hasFolding(lnum, NULL, &lnum);
1465# endif
1466 if (lnum >= curbuf->b_ml.ml_line_count)
1467 break;
1468 ++lnum;
1469# ifdef FEAT_DIFF
1470 curwin->w_topfill = diff_check_fill(curwin, lnum);
1471# endif
1472 }
1473 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001474 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 curwin->w_botline += lnum - curwin->w_topline;
1476 curwin->w_topline = lnum;
1477 }
1478 else
1479#endif
1480 {
1481 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001482 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484
1485 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1486 curwin->w_topline = curbuf->b_ml.ml_line_count;
1487 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1488 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1489
1490#ifdef FEAT_DIFF
1491 check_topfill(curwin, FALSE);
1492#endif
1493
1494#ifdef FEAT_FOLDING
1495 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001496 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1498#endif
1499
1500 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1501 if (curwin->w_cursor.lnum < curwin->w_topline)
1502 {
1503 curwin->w_cursor.lnum = curwin->w_topline;
1504 curwin->w_valid &=
1505 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1506 coladvance(curwin->w_curswant);
1507 }
1508}
1509
1510#ifdef FEAT_DIFF
1511/*
1512 * Don't end up with too many filler lines in the window.
1513 */
1514 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001515check_topfill(
1516 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001517 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 int n;
1520
1521 if (wp->w_topfill > 0)
1522 {
1523 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1524 if (wp->w_topfill + n > wp->w_height)
1525 {
1526 if (down && wp->w_topline > 1)
1527 {
1528 --wp->w_topline;
1529 wp->w_topfill = 0;
1530 }
1531 else
1532 {
1533 wp->w_topfill = wp->w_height - n;
1534 if (wp->w_topfill < 0)
1535 wp->w_topfill = 0;
1536 }
1537 }
1538 }
1539}
1540
1541/*
1542 * Use as many filler lines as possible for w_topline. Make sure w_topline
1543 * is still visible.
1544 */
1545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001546max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 int n;
1549
1550 n = plines_nofill(curwin->w_topline);
1551 if (n >= curwin->w_height)
1552 curwin->w_topfill = 0;
1553 else
1554 {
1555 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1556 if (curwin->w_topfill + n > curwin->w_height)
1557 curwin->w_topfill = curwin->w_height - n;
1558 }
1559}
1560#endif
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562/*
1563 * Scroll the screen one line down, but don't do it if it would move the
1564 * cursor off the screen.
1565 */
1566 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001567scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 int end_row;
1570#ifdef FEAT_DIFF
1571 int can_fill = (curwin->w_topfill
1572 < diff_check_fill(curwin, curwin->w_topline));
1573#endif
1574
1575 if (curwin->w_topline <= 1
1576#ifdef FEAT_DIFF
1577 && !can_fill
1578#endif
1579 )
1580 return;
1581
Bram Moolenaar85a20022019-12-21 18:25:54 +01001582 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 /*
1585 * Compute the row number of the last row of the cursor line
1586 * and make sure it doesn't go off the screen. Make sure the cursor
1587 * doesn't go past 'scrolloff' lines from the screen end.
1588 */
1589 end_row = curwin->w_wrow;
1590#ifdef FEAT_DIFF
1591 if (can_fill)
1592 ++end_row;
1593 else
1594 end_row += plines_nofill(curwin->w_topline - 1);
1595#else
1596 end_row += plines(curwin->w_topline - 1);
1597#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001598 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 validate_cheight();
1601 validate_virtcol();
1602 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001603 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001605 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607#ifdef FEAT_DIFF
1608 if (can_fill)
1609 {
1610 ++curwin->w_topfill;
1611 check_topfill(curwin, TRUE);
1612 }
1613 else
1614 {
1615 --curwin->w_topline;
1616 curwin->w_topfill = 0;
1617 }
1618#else
1619 --curwin->w_topline;
1620#endif
1621#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001622 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001624 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1626 }
1627}
1628
1629/*
1630 * Scroll the screen one line up, but don't do it if it would move the cursor
1631 * off the screen.
1632 */
1633 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001634scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
1636 int start_row;
1637
1638 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1639#ifdef FEAT_DIFF
1640 && curwin->w_topfill == 0
1641#endif
1642 )
1643 return;
1644
Bram Moolenaar85a20022019-12-21 18:25:54 +01001645 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 /*
1648 * Compute the row number of the first row of the cursor line
1649 * and make sure it doesn't go off the screen. Make sure the cursor
1650 * doesn't go before 'scrolloff' lines from the screen start.
1651 */
1652#ifdef FEAT_DIFF
1653 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1654 - curwin->w_topfill;
1655#else
1656 start_row = curwin->w_wrow - plines(curwin->w_topline);
1657#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001658 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 {
1660 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001661 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001663 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
1665#ifdef FEAT_DIFF
1666 if (curwin->w_topfill > 0)
1667 --curwin->w_topfill;
1668 else
1669#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001670 {
1671#ifdef FEAT_FOLDING
1672 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001675 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001676 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1678 }
1679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681/*
1682 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1683 * a (wrapped) text line. Uses and sets "lp->fill".
1684 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001685 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 */
1687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001688topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690#ifdef FEAT_DIFF
1691 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1692 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001693 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 ++lp->fill;
1695 lp->height = 1;
1696 }
1697 else
1698#endif
1699 {
1700 --lp->lnum;
1701#ifdef FEAT_DIFF
1702 lp->fill = 0;
1703#endif
1704 if (lp->lnum < 1)
1705 lp->height = MAXCOL;
1706 else
1707#ifdef FEAT_FOLDING
1708 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001709 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 lp->height = 1;
1711 else
1712#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001713 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715}
1716
1717/*
1718 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1719 * a (wrapped) text line. Uses and sets "lp->fill".
1720 * Returns the height of the added line in "lp->height".
1721 * Lines below the last one are incredibly high.
1722 */
1723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001724botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726#ifdef FEAT_DIFF
1727 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1728 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001729 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 ++lp->fill;
1731 lp->height = 1;
1732 }
1733 else
1734#endif
1735 {
1736 ++lp->lnum;
1737#ifdef FEAT_DIFF
1738 lp->fill = 0;
1739#endif
1740 if (lp->lnum > curbuf->b_ml.ml_line_count)
1741 lp->height = MAXCOL;
1742 else
1743#ifdef FEAT_FOLDING
1744 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001745 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 lp->height = 1;
1747 else
1748#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001749 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
1751}
1752
1753#ifdef FEAT_DIFF
1754/*
1755 * Switch from including filler lines below lp->lnum to including filler
1756 * lines above loff.lnum + 1. This keeps pointing to the same line.
1757 * When there are no filler lines nothing changes.
1758 */
1759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 if (lp->fill > 0)
1763 {
1764 ++lp->lnum;
1765 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1766 }
1767}
1768
1769/*
1770 * Switch from including filler lines above lp->lnum to including filler
1771 * lines below loff.lnum - 1. This keeps pointing to the same line.
1772 * When there are no filler lines nothing changes.
1773 */
1774 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 if (lp->fill > 0)
1778 {
1779 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1780 --lp->lnum;
1781 }
1782}
1783#endif
1784
1785/*
1786 * Recompute topline to put the cursor at the top of the window.
1787 * Scroll at least "min_scroll" lines.
1788 * If "always" is TRUE, always set topline (for "zt").
1789 */
1790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001791scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 int scrolled = 0;
1794 int extra = 0;
1795 int used;
1796 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001797 linenr_T top; // just above displayed lines
1798 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 linenr_T old_topline = curwin->w_topline;
1800#ifdef FEAT_DIFF
1801 linenr_T old_topfill = curwin->w_topfill;
1802#endif
1803 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001804 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (mouse_dragging > 0)
1807 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 /*
1810 * Decrease topline until:
1811 * - it has become 1
1812 * - (part of) the cursor line is moved off the screen or
1813 * - moved at least 'scrolljump' lines and
1814 * - at least 'scrolloff' lines above and below the cursor
1815 */
1816 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001817 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (curwin->w_cursor.lnum < curwin->w_topline)
1819 scrolled = used;
1820
1821#ifdef FEAT_FOLDING
1822 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1823 {
1824 --top;
1825 ++bot;
1826 }
1827 else
1828#endif
1829 {
1830 top = curwin->w_cursor.lnum - 1;
1831 bot = curwin->w_cursor.lnum + 1;
1832 }
1833 new_topline = top + 1;
1834
1835#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001836 // "used" already contains the number of filler lines above, don't add it
1837 // again.
1838 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001839 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#endif
1841
1842 /*
1843 * Check if the lines from "top" to "bot" fit in the window. If they do,
1844 * set new_topline and advance "top" and "bot" to include more lines.
1845 */
1846 while (top > 0)
1847 {
1848#ifdef FEAT_FOLDING
1849 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001850 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 i = 1;
1852 else
1853#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001854 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 used += i;
1856 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1857 {
1858#ifdef FEAT_FOLDING
1859 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001860 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 ++used;
1862 else
1863#endif
1864 used += plines(bot);
1865 }
1866 if (used > curwin->w_height)
1867 break;
1868 if (top < curwin->w_topline)
1869 scrolled += i;
1870
1871 /*
1872 * If scrolling is needed, scroll at least 'sj' lines.
1873 */
1874 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1875 && extra >= off)
1876 break;
1877
1878 extra += i;
1879 new_topline = top;
1880 --top;
1881 ++bot;
1882 }
1883
1884 /*
1885 * If we don't have enough space, put cursor in the middle.
1886 * This makes sure we get the same position when using "k" and "j"
1887 * in a small window.
1888 */
1889 if (used > curwin->w_height)
1890 scroll_cursor_halfway(FALSE);
1891 else
1892 {
1893 /*
1894 * If "always" is FALSE, only adjust topline to a lower value, higher
1895 * value may happen with wrapping lines
1896 */
1897 if (new_topline < curwin->w_topline || always)
1898 curwin->w_topline = new_topline;
1899 if (curwin->w_topline > curwin->w_cursor.lnum)
1900 curwin->w_topline = curwin->w_cursor.lnum;
1901#ifdef FEAT_DIFF
1902 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1903 if (curwin->w_topfill > 0 && extra > off)
1904 {
1905 curwin->w_topfill -= extra - off;
1906 if (curwin->w_topfill < 0)
1907 curwin->w_topfill = 0;
1908 }
1909 check_topfill(curwin, FALSE);
1910#endif
1911 if (curwin->w_topline != old_topline
1912#ifdef FEAT_DIFF
1913 || curwin->w_topfill != old_topfill
1914#endif
1915 )
1916 curwin->w_valid &=
1917 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1918 curwin->w_valid |= VALID_TOPLINE;
1919 }
1920}
1921
1922/*
1923 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1924 * screen lines for text lines.
1925 */
1926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001927set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929#ifdef FEAT_DIFF
1930 wp->w_filler_rows = 0;
1931#endif
1932 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001933 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 else
1935 {
1936 wp->w_empty_rows = wp->w_height - used;
1937#ifdef FEAT_DIFF
1938 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1939 {
1940 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1941 if (wp->w_empty_rows > wp->w_filler_rows)
1942 wp->w_empty_rows -= wp->w_filler_rows;
1943 else
1944 {
1945 wp->w_filler_rows = wp->w_empty_rows;
1946 wp->w_empty_rows = 0;
1947 }
1948 }
1949#endif
1950 }
1951}
1952
1953/*
1954 * Recompute topline to put the cursor at the bottom of the window.
1955 * Scroll at least "min_scroll" lines.
1956 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1957 * This is messy stuff!!!
1958 */
1959 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001960scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961{
1962 int used;
1963 int scrolled = 0;
1964 int extra = 0;
1965 int i;
1966 linenr_T line_count;
1967 linenr_T old_topline = curwin->w_topline;
1968 lineoff_T loff;
1969 lineoff_T boff;
1970#ifdef FEAT_DIFF
1971 int old_topfill = curwin->w_topfill;
1972 int fill_below_window;
1973#endif
1974 linenr_T old_botline = curwin->w_botline;
1975 linenr_T old_valid = curwin->w_valid;
1976 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001977 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001978 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 cln = curwin->w_cursor.lnum;
1981 if (set_topbot)
1982 {
1983 used = 0;
1984 curwin->w_botline = cln + 1;
1985#ifdef FEAT_DIFF
1986 loff.fill = 0;
1987#endif
1988 for (curwin->w_topline = curwin->w_botline;
1989 curwin->w_topline > 1;
1990 curwin->w_topline = loff.lnum)
1991 {
1992 loff.lnum = curwin->w_topline;
1993 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001994 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 break;
1996 used += loff.height;
1997#ifdef FEAT_DIFF
1998 curwin->w_topfill = loff.fill;
1999#endif
2000 }
2001 set_empty_rows(curwin, used);
2002 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2003 if (curwin->w_topline != old_topline
2004#ifdef FEAT_DIFF
2005 || curwin->w_topfill != old_topfill
2006#endif
2007 )
2008 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2009 }
2010 else
2011 validate_botline();
2012
Bram Moolenaar85a20022019-12-21 18:25:54 +01002013 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014#ifdef FEAT_DIFF
2015 used = plines_nofill(cln);
2016#else
2017 validate_cheight();
2018 used = curwin->w_cline_height;
2019#endif
2020
Bram Moolenaar85a20022019-12-21 18:25:54 +01002021 // If the cursor is below botline, we will at least scroll by the height
2022 // of the cursor line. Correct for empty lines, which are really part of
2023 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (cln >= curwin->w_botline)
2025 {
2026 scrolled = used;
2027 if (cln == curwin->w_botline)
2028 scrolled -= curwin->w_empty_rows;
2029 }
2030
2031 /*
2032 * Stop counting lines to scroll when
2033 * - hitting start of the file
2034 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002035 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 * - lines between botline and cursor have been counted
2037 */
2038#ifdef FEAT_FOLDING
2039 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2040#endif
2041 {
2042 loff.lnum = cln;
2043 boff.lnum = cln;
2044 }
2045#ifdef FEAT_DIFF
2046 loff.fill = 0;
2047 boff.fill = 0;
2048 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2049 - curwin->w_filler_rows;
2050#endif
2051
2052 while (loff.lnum > 1)
2053 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002054 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2055 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002057 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2059 && loff.lnum <= curwin->w_botline
2060#ifdef FEAT_DIFF
2061 && (loff.lnum < curwin->w_botline
2062 || loff.fill >= fill_below_window)
2063#endif
2064 )
2065 break;
2066
Bram Moolenaar85a20022019-12-21 18:25:54 +01002067 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002069 if (loff.height == MAXCOL)
2070 used = MAXCOL;
2071 else
2072 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (used > curwin->w_height)
2074 break;
2075 if (loff.lnum >= curwin->w_botline
2076#ifdef FEAT_DIFF
2077 && (loff.lnum > curwin->w_botline
2078 || loff.fill <= fill_below_window)
2079#endif
2080 )
2081 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002082 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 scrolled += loff.height;
2084 if (loff.lnum == curwin->w_botline
2085#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002086 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087#endif
2088 )
2089 scrolled -= curwin->w_empty_rows;
2090 }
2091
2092 if (boff.lnum < curbuf->b_ml.ml_line_count)
2093 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002094 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 botline_forw(&boff);
2096 used += boff.height;
2097 if (used > curwin->w_height)
2098 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002099 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2100 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
2102 extra += boff.height;
2103 if (boff.lnum >= curwin->w_botline
2104#ifdef FEAT_DIFF
2105 || (boff.lnum + 1 == curwin->w_botline
2106 && boff.fill > curwin->w_filler_rows)
2107#endif
2108 )
2109 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002110 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 scrolled += boff.height;
2112 if (boff.lnum == curwin->w_botline
2113#ifdef FEAT_DIFF
2114 && boff.fill == 0
2115#endif
2116 )
2117 scrolled -= curwin->w_empty_rows;
2118 }
2119 }
2120 }
2121 }
2122
Bram Moolenaar85a20022019-12-21 18:25:54 +01002123 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (scrolled <= 0)
2125 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002126 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 else if (used > curwin->w_height)
2128 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002129 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 else
2131 {
2132 line_count = 0;
2133#ifdef FEAT_DIFF
2134 boff.fill = curwin->w_topfill;
2135#endif
2136 boff.lnum = curwin->w_topline - 1;
2137 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2138 {
2139 botline_forw(&boff);
2140 i += boff.height;
2141 ++line_count;
2142 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002143 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 line_count = 9999;
2145 }
2146
2147 /*
2148 * Scroll up if the cursor is off the bottom of the screen a bit.
2149 * Otherwise put it at 1/2 of the screen.
2150 */
2151 if (line_count >= curwin->w_height && line_count > min_scroll)
2152 scroll_cursor_halfway(FALSE);
2153 else
2154 scrollup(line_count, TRUE);
2155
2156 /*
2157 * If topline didn't change we need to restore w_botline and w_empty_rows
2158 * (we changed them).
2159 * If topline did change, update_screen() will set botline.
2160 */
2161 if (curwin->w_topline == old_topline && set_topbot)
2162 {
2163 curwin->w_botline = old_botline;
2164 curwin->w_empty_rows = old_empty_rows;
2165 curwin->w_valid = old_valid;
2166 }
2167 curwin->w_valid |= VALID_TOPLINE;
2168}
2169
2170/*
2171 * Recompute topline to put the cursor halfway the window
2172 * If "atend" is TRUE, also put it halfway at the end of the file.
2173 */
2174 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002175scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 int above = 0;
2178 linenr_T topline;
2179#ifdef FEAT_DIFF
2180 int topfill = 0;
2181#endif
2182 int below = 0;
2183 int used;
2184 lineoff_T loff;
2185 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002186#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002187 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002190#ifdef FEAT_PROP_POPUP
2191 // if the width changed this needs to be updated first
2192 may_update_popup_position();
2193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2195#ifdef FEAT_FOLDING
2196 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2197#endif
2198#ifdef FEAT_DIFF
2199 used = plines_nofill(loff.lnum);
2200 loff.fill = 0;
2201 boff.fill = 0;
2202#else
2203 used = plines(loff.lnum);
2204#endif
2205 topline = loff.lnum;
2206 while (topline > 1)
2207 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002208 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 if (boff.lnum < curbuf->b_ml.ml_line_count)
2211 {
2212 botline_forw(&boff);
2213 used += boff.height;
2214 if (used > curwin->w_height)
2215 break;
2216 below += boff.height;
2217 }
2218 else
2219 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002220 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (atend)
2222 ++used;
2223 }
2224 }
2225
Bram Moolenaar85a20022019-12-21 18:25:54 +01002226 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002229 if (loff.height == MAXCOL)
2230 used = MAXCOL;
2231 else
2232 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (used > curwin->w_height)
2234 break;
2235 above += loff.height;
2236 topline = loff.lnum;
2237#ifdef FEAT_DIFF
2238 topfill = loff.fill;
2239#endif
2240 }
2241 }
2242#ifdef FEAT_FOLDING
2243 if (!hasFolding(topline, &curwin->w_topline, NULL))
2244#endif
2245 curwin->w_topline = topline;
2246#ifdef FEAT_DIFF
2247 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002248 if (old_topline > curwin->w_topline + curwin->w_height)
2249 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 check_topfill(curwin, FALSE);
2251#endif
2252 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2253 curwin->w_valid |= VALID_TOPLINE;
2254}
2255
2256/*
2257 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002258 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 * If not possible, put it at the same position as scroll_cursor_halfway().
2260 * When called topline must be valid!
2261 */
2262 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002263cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002265 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002267 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 linenr_T botline;
2269 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002270 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002272 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274 /*
2275 * How many lines we would like to have above/below the cursor depends on
2276 * whether the first/last line of the file is on screen.
2277 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002278 above_wanted = so;
2279 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002280 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
2282 above_wanted = mouse_dragging - 1;
2283 below_wanted = mouse_dragging - 1;
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (curwin->w_topline == 1)
2286 {
2287 above_wanted = 0;
2288 max_off = curwin->w_height / 2;
2289 if (below_wanted > max_off)
2290 below_wanted = max_off;
2291 }
2292 validate_botline();
2293 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002294 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 below_wanted = 0;
2297 max_off = (curwin->w_height - 1) / 2;
2298 if (above_wanted > max_off)
2299 above_wanted = max_off;
2300 }
2301
2302 /*
2303 * If there are sufficient file-lines above and below the cursor, we can
2304 * return now.
2305 */
2306 cln = curwin->w_cursor.lnum;
2307 if (cln >= curwin->w_topline + above_wanted
2308 && cln < curwin->w_botline - below_wanted
2309#ifdef FEAT_FOLDING
2310 && !hasAnyFolding(curwin)
2311#endif
2312 )
2313 return;
2314
2315 /*
2316 * Narrow down the area where the cursor can be put by taking lines from
2317 * the top and the bottom until:
2318 * - the desired context lines are found
2319 * - the lines from the top is past the lines from the bottom
2320 */
2321 topline = curwin->w_topline;
2322 botline = curwin->w_botline - 1;
2323#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002324 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 above = curwin->w_topfill;
2326 below = curwin->w_filler_rows;
2327#endif
2328 while ((above < above_wanted || below < below_wanted) && topline < botline)
2329 {
2330 if (below < below_wanted && (below <= above || above >= above_wanted))
2331 {
2332#ifdef FEAT_FOLDING
2333 if (hasFolding(botline, &botline, NULL))
2334 ++below;
2335 else
2336#endif
2337 below += plines(botline);
2338 --botline;
2339 }
2340 if (above < above_wanted && (above < below || below >= below_wanted))
2341 {
2342#ifdef FEAT_FOLDING
2343 if (hasFolding(topline, NULL, &topline))
2344 ++above;
2345 else
2346#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002347 above += PLINES_NOFILL(topline);
2348#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002349 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 if (topline < botline)
2351 above += diff_check_fill(curwin, topline + 1);
2352#endif
2353 ++topline;
2354 }
2355 }
2356 if (topline == botline || botline == 0)
2357 curwin->w_cursor.lnum = topline;
2358 else if (topline > botline)
2359 curwin->w_cursor.lnum = botline;
2360 else
2361 {
2362 if (cln < topline && curwin->w_topline > 1)
2363 {
2364 curwin->w_cursor.lnum = topline;
2365 curwin->w_valid &=
2366 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2367 }
2368 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2369 {
2370 curwin->w_cursor.lnum = botline;
2371 curwin->w_valid &=
2372 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2373 }
2374 }
2375 curwin->w_valid |= VALID_TOPLINE;
2376}
2377
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002378static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380/*
2381 * move screen 'count' pages up or down and update screen
2382 *
2383 * return FAIL for failure, OK otherwise
2384 */
2385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002386onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
2388 long n;
2389 int retval = OK;
2390 lineoff_T loff;
2391 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002392 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
Bram Moolenaar85a20022019-12-21 18:25:54 +01002394 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 beep_flush();
2397 return FAIL;
2398 }
2399
2400 for ( ; count > 0; --count)
2401 {
2402 validate_botline();
2403 /*
2404 * It's an error to move a page up when the first line is already on
2405 * the screen. It's an error to move a page down when the last line
2406 * is on the screen and the topline is 'scrolloff' lines from the
2407 * last line.
2408 */
2409 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002410 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2412 : (curwin->w_topline == 1
2413#ifdef FEAT_DIFF
2414 && curwin->w_topfill ==
2415 diff_check_fill(curwin, curwin->w_topline)
2416#endif
2417 ))
2418 {
2419 beep_flush();
2420 retval = FAIL;
2421 break;
2422 }
2423
2424#ifdef FEAT_DIFF
2425 loff.fill = 0;
2426#endif
2427 if (dir == FORWARD)
2428 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002429 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002432 if (p_window <= 2)
2433 ++curwin->w_topline;
2434 else
2435 curwin->w_topline += p_window - 2;
2436 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2437 curwin->w_topline = curbuf->b_ml.ml_line_count;
2438 curwin->w_cursor.lnum = curwin->w_topline;
2439 }
2440 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2441 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002442 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 curwin->w_topline = curbuf->b_ml.ml_line_count;
2444#ifdef FEAT_DIFF
2445 curwin->w_topfill = 0;
2446#endif
2447 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2448 }
2449 else
2450 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002451 // For the overlap, start with the line just below the window
2452 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 loff.lnum = curwin->w_botline;
2454#ifdef FEAT_DIFF
2455 loff.fill = diff_check_fill(curwin, loff.lnum)
2456 - curwin->w_filler_rows;
2457#endif
2458 get_scroll_overlap(&loff, -1);
2459 curwin->w_topline = loff.lnum;
2460#ifdef FEAT_DIFF
2461 curwin->w_topfill = loff.fill;
2462 check_topfill(curwin, FALSE);
2463#endif
2464 curwin->w_cursor.lnum = curwin->w_topline;
2465 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2466 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2467 }
2468 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002469 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
2471#ifdef FEAT_DIFF
2472 if (curwin->w_topline == 1)
2473 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002474 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 max_topfill();
2476 continue;
2477 }
2478#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002479 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002481 // Vi compatible scrolling (sort of)
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 < 1)
2487 curwin->w_topline = 1;
2488 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2489 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2490 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2491 continue;
2492 }
2493
Bram Moolenaar85a20022019-12-21 18:25:54 +01002494 // Find the line at the top of the window that is going to be the
2495 // line at the bottom of the window. Make sure this results in
2496 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 loff.lnum = curwin->w_topline - 1;
2498#ifdef FEAT_DIFF
2499 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2500 - curwin->w_topfill;
2501#endif
2502 get_scroll_overlap(&loff, 1);
2503
2504 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2505 {
2506 loff.lnum = curbuf->b_ml.ml_line_count;
2507#ifdef FEAT_DIFF
2508 loff.fill = 0;
2509 }
2510 else
2511 {
2512 botline_topline(&loff);
2513#endif
2514 }
2515 curwin->w_cursor.lnum = loff.lnum;
2516
Bram Moolenaar85a20022019-12-21 18:25:54 +01002517 // Find the line just above the new topline to get the right line
2518 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 n = 0;
2520 while (n <= curwin->w_height && loff.lnum >= 1)
2521 {
2522 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002523 if (loff.height == MAXCOL)
2524 n = MAXCOL;
2525 else
2526 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002528 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 curwin->w_topline = 1;
2531#ifdef FEAT_DIFF
2532 max_topfill();
2533#endif
2534 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2535 }
2536 else
2537 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002538 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539#ifdef FEAT_DIFF
2540 topline_botline(&loff);
2541#endif
2542 botline_forw(&loff);
2543 botline_forw(&loff);
2544#ifdef FEAT_DIFF
2545 botline_topline(&loff);
2546#endif
2547#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002548 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2550#endif
2551
Bram Moolenaar85a20022019-12-21 18:25:54 +01002552 // Always scroll at least one line. Avoid getting stuck on
2553 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (loff.lnum >= curwin->w_topline
2555#ifdef FEAT_DIFF
2556 && (loff.lnum > curwin->w_topline
2557 || loff.fill >= curwin->w_topfill)
2558#endif
2559 )
2560 {
2561#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002562 // First try using the maximum number of filler lines. If
2563 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 loff.fill = curwin->w_topfill;
2565 if (curwin->w_topfill < diff_check_fill(curwin,
2566 curwin->w_topline))
2567 max_topfill();
2568 if (curwin->w_topfill == loff.fill)
2569#endif
2570 {
2571 --curwin->w_topline;
2572#ifdef FEAT_DIFF
2573 curwin->w_topfill = 0;
2574#endif
2575 }
2576 comp_botline(curwin);
2577 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002578 curwin->w_valid &=
2579 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581 else
2582 {
2583 curwin->w_topline = loff.lnum;
2584#ifdef FEAT_DIFF
2585 curwin->w_topfill = loff.fill;
2586 check_topfill(curwin, FALSE);
2587#endif
2588 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2589 }
2590 }
2591 }
2592 }
2593#ifdef FEAT_FOLDING
2594 foldAdjustCursor();
2595#endif
2596 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002597 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002598 if (retval == OK)
2599 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2601
Bram Moolenaar907dad72018-07-10 15:07:15 +02002602 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002604 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2605 // But make sure we scroll at least one line (happens with mix of long
2606 // wrapping lines and non-wrapping line).
2607 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002609 scroll_cursor_top(1, FALSE);
2610 if (curwin->w_topline <= old_topline
2611 && old_topline < curbuf->b_ml.ml_line_count)
2612 {
2613 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002615 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2616#endif
2617 }
2618 }
2619#ifdef FEAT_FOLDING
2620 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624
2625 redraw_later(VALID);
2626 return retval;
2627}
2628
2629/*
2630 * Decide how much overlap to use for page-up or page-down scrolling.
2631 * This is symmetric, so that doing both keeps the same lines displayed.
2632 * Three lines are examined:
2633 *
2634 * before CTRL-F after CTRL-F / before CTRL-B
2635 * etc. l1
2636 * l1 last but one line ------------
2637 * l2 last text line l2 top text line
2638 * ------------- l3 second text line
2639 * l3 etc.
2640 */
2641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002642get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 int h1, h2, h3, h4;
2645 int min_height = curwin->w_height - 2;
2646 lineoff_T loff0, loff1, loff2;
2647
2648#ifdef FEAT_DIFF
2649 if (lp->fill > 0)
2650 lp->height = 1;
2651 else
2652 lp->height = plines_nofill(lp->lnum);
2653#else
2654 lp->height = plines(lp->lnum);
2655#endif
2656 h1 = lp->height;
2657 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002658 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 loff0 = *lp;
2661 if (dir > 0)
2662 botline_forw(lp);
2663 else
2664 topline_back(lp);
2665 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002666 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002668 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 return;
2670 }
2671
2672 loff1 = *lp;
2673 if (dir > 0)
2674 botline_forw(lp);
2675 else
2676 topline_back(lp);
2677 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002678 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002680 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 return;
2682 }
2683
2684 loff2 = *lp;
2685 if (dir > 0)
2686 botline_forw(lp);
2687 else
2688 topline_back(lp);
2689 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002690 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002691 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002693 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694}
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696/*
2697 * Scroll 'scroll' lines up or down.
2698 */
2699 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002700halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 long scrolled = 0;
2703 int i;
2704 int n;
2705 int room;
2706
2707 if (Prenum)
2708 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2709 curwin->w_height : Prenum;
2710 n = (curwin->w_p_scr <= curwin->w_height) ?
2711 curwin->w_p_scr : curwin->w_height;
2712
Bram Moolenaard5d37532017-03-27 23:02:07 +02002713 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 validate_botline();
2715 room = curwin->w_empty_rows;
2716#ifdef FEAT_DIFF
2717 room += curwin->w_filler_rows;
2718#endif
2719 if (flag)
2720 {
2721 /*
2722 * scroll the text up
2723 */
2724 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2725 {
2726#ifdef FEAT_DIFF
2727 if (curwin->w_topfill > 0)
2728 {
2729 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002730 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 --curwin->w_topfill;
2732 }
2733 else
2734#endif
2735 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002736 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 n -= i;
2738 if (n < 0 && scrolled > 0)
2739 break;
2740#ifdef FEAT_FOLDING
2741 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2742#endif
2743 ++curwin->w_topline;
2744#ifdef FEAT_DIFF
2745 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2746#endif
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2749 {
2750 ++curwin->w_cursor.lnum;
2751 curwin->w_valid &=
2752 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2756 scrolled += i;
2757
2758 /*
2759 * Correct w_botline for changed w_topline.
2760 * Won't work when there are filler lines.
2761 */
2762#ifdef FEAT_DIFF
2763 if (curwin->w_p_diff)
2764 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2765 else
2766#endif
2767 {
2768 room += i;
2769 do
2770 {
2771 i = plines(curwin->w_botline);
2772 if (i > room)
2773 break;
2774#ifdef FEAT_FOLDING
2775 (void)hasFolding(curwin->w_botline, NULL,
2776 &curwin->w_botline);
2777#endif
2778 ++curwin->w_botline;
2779 room -= i;
2780 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2781 }
2782 }
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 /*
2785 * When hit bottom of the file: move cursor down.
2786 */
2787 if (n > 0)
2788 {
2789# ifdef FEAT_FOLDING
2790 if (hasAnyFolding(curwin))
2791 {
2792 while (--n >= 0
2793 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2794 {
2795 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2796 &curwin->w_cursor.lnum);
2797 ++curwin->w_cursor.lnum;
2798 }
2799 }
2800 else
2801# endif
2802 curwin->w_cursor.lnum += n;
2803 check_cursor_lnum();
2804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806 else
2807 {
2808 /*
2809 * scroll the text down
2810 */
2811 while (n > 0 && curwin->w_topline > 1)
2812 {
2813#ifdef FEAT_DIFF
2814 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2815 {
2816 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002817 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 ++curwin->w_topfill;
2819 }
2820 else
2821#endif
2822 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002823 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 n -= i;
2825 if (n < 0 && scrolled > 0)
2826 break;
2827 --curwin->w_topline;
2828#ifdef FEAT_FOLDING
2829 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2830#endif
2831#ifdef FEAT_DIFF
2832 curwin->w_topfill = 0;
2833#endif
2834 }
2835 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2836 VALID_BOTLINE|VALID_BOTLINE_AP);
2837 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 if (curwin->w_cursor.lnum > 1)
2839 {
2840 --curwin->w_cursor.lnum;
2841 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 /*
2846 * When hit top of the file: move cursor up.
2847 */
2848 if (n > 0)
2849 {
2850 if (curwin->w_cursor.lnum <= (linenr_T)n)
2851 curwin->w_cursor.lnum = 1;
2852 else
2853# ifdef FEAT_FOLDING
2854 if (hasAnyFolding(curwin))
2855 {
2856 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2857 {
2858 --curwin->w_cursor.lnum;
2859 (void)hasFolding(curwin->w_cursor.lnum,
2860 &curwin->w_cursor.lnum, NULL);
2861 }
2862 }
2863 else
2864# endif
2865 curwin->w_cursor.lnum -= n;
2866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002869 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 foldAdjustCursor();
2871# endif
2872#ifdef FEAT_DIFF
2873 check_topfill(curwin, !flag);
2874#endif
2875 cursor_correct();
2876 beginline(BL_SOL | BL_FIX);
2877 redraw_later(VALID);
2878}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002879
Bram Moolenaar860cae12010-06-05 23:22:07 +02002880 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002881do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002882{
2883 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002884 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002885 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002886 colnr_T curswant = curwin->w_curswant;
2887 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002888 win_T *old_curwin = curwin;
2889 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002890 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002891 int old_VIsual_select = VIsual_select;
2892 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002893
2894 /*
2895 * loop through the cursorbound windows
2896 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002897 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002898 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002899 {
2900 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002901 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002902 if (curwin != old_curwin && curwin->w_p_crb)
2903 {
2904# ifdef FEAT_DIFF
2905 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002906 curwin->w_cursor.lnum =
2907 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002908 else
2909# endif
2910 curwin->w_cursor.lnum = line;
2911 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002912 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002913 curwin->w_curswant = curswant;
2914 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002915
Bram Moolenaar85a20022019-12-21 18:25:54 +01002916 // Make sure the cursor is in a valid position. Temporarily set
2917 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002918 restart_edit_save = restart_edit;
2919 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002920 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002921# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002922 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002923 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002924# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002925 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002926 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002927 if (has_mbyte)
2928 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002929 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002930
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002932 if (!curwin->w_p_scb)
2933 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002934 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002935 }
2936 }
2937
2938 /*
2939 * reset current-window
2940 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002941 VIsual_select = old_VIsual_select;
2942 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002943 curwin = old_curwin;
2944 curbuf = old_curbuf;
2945}