blob: 571ec8c3174fcb5bb46782f92845bbd0379c0bde [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
138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 * Update curwin->w_topline and redraw if necessary.
140 * Used to update the screen before printing a message.
141 */
142 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100143update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 update_topline();
146 if (must_redraw)
147 update_screen(0);
148}
149
150/*
151 * Update curwin->w_topline to move the cursor onto the screen.
152 */
153 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100154update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
156 long line_count;
157 int halfheight;
158 int n;
159 linenr_T old_topline;
160#ifdef FEAT_DIFF
161 int old_topfill;
162#endif
163#ifdef FEAT_FOLDING
164 linenr_T lnum;
165#endif
166 int check_topline = FALSE;
167 int check_botline = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100168 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100169 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // If there is no valid screen and when the window height is zero just use
172 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200173 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200174 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100175 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200176 curwin->w_topline = curwin->w_cursor.lnum;
177 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200178 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200179 return;
180 }
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 check_cursor_moved(curwin);
183 if (curwin->w_valid & VALID_TOPLINE)
184 return;
185
Bram Moolenaar85a20022019-12-21 18:25:54 +0100186 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000187 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100188 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
190 old_topline = curwin->w_topline;
191#ifdef FEAT_DIFF
192 old_topfill = curwin->w_topfill;
193#endif
194
195 /*
196 * If the buffer is empty, always set topline to 1.
197 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100198 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 {
200 if (curwin->w_topline != 1)
201 redraw_later(NOT_VALID);
202 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 curwin->w_botline = 2;
204 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 }
207
208 /*
209 * If the cursor is above or near the top of the window, scroll the window
210 * to show the line the cursor is in, with 'scrolloff' context.
211 */
212 else
213 {
214 if (curwin->w_topline > 1)
215 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100216 // If the cursor is above topline, scrolling is always needed.
217 // If the cursor is far below topline and there is no folding,
218 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (curwin->w_cursor.lnum < curwin->w_topline)
220 check_topline = TRUE;
221 else if (check_top_offset())
222 check_topline = TRUE;
223 }
224#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100225 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
227 curwin->w_topline))
228 check_topline = TRUE;
229#endif
230
231 if (check_topline)
232 {
233 halfheight = curwin->w_height / 2 - 1;
234 if (halfheight < 2)
235 halfheight = 2;
236
237#ifdef FEAT_FOLDING
238 if (hasAnyFolding(curwin))
239 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100240 // Count the number of logical lines between the cursor and
241 // topline + scrolloff (approximation of how much will be
242 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 n = 0;
244 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100245 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {
247 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100248 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
250 break;
251 (void)hasFolding(lnum, NULL, &lnum);
252 }
253 }
254 else
255#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100256 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
Bram Moolenaar85a20022019-12-21 18:25:54 +0100258 // If we weren't very close to begin with, we scroll to put the
259 // cursor in the middle of the window. Otherwise put the cursor
260 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 if (n >= halfheight)
262 scroll_cursor_halfway(FALSE);
263 else
264 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000265 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 check_botline = TRUE;
267 }
268 }
269
270 else
271 {
272#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100273 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
275#endif
276 check_botline = TRUE;
277 }
278 }
279
280 /*
281 * If the cursor is below the bottom of the window, scroll the window
282 * to put the cursor on the window.
283 * When w_botline is invalid, recompute it first, to avoid a redraw later.
284 * If w_botline was approximated, we might need a redraw later in a few
285 * cases, but we don't want to spend (a lot of) time recomputing w_botline
286 * for every small change.
287 */
288 if (check_botline)
289 {
290 if (!(curwin->w_valid & VALID_BOTLINE_AP))
291 validate_botline();
292
293 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
294 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000295 if (curwin->w_cursor.lnum < curwin->w_botline)
296 {
297 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100298 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299#ifdef FEAT_FOLDING
300 || hasAnyFolding(curwin)
301#endif
302 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 lineoff_T loff;
305
Bram Moolenaar85a20022019-12-21 18:25:54 +0100306 // Cursor is (a few lines) above botline, check if there are
307 // 'scrolloff' window lines below the cursor. If not, need to
308 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 n = curwin->w_empty_rows;
310 loff.lnum = curwin->w_cursor.lnum;
311#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100312 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
314#endif
315#ifdef FEAT_DIFF
316 loff.fill = 0;
317 n += curwin->w_filler_rows;
318#endif
319 loff.height = 0;
320 while (loff.lnum < curwin->w_botline
321#ifdef FEAT_DIFF
322 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
323#endif
324 )
325 {
326 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100327 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 break;
329 botline_forw(&loff);
330 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100331 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100332 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000334 }
335 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100336 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000337 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 }
339 if (check_botline)
340 {
341#ifdef FEAT_FOLDING
342 if (hasAnyFolding(curwin))
343 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100344 // Count the number of logical lines between the cursor and
345 // botline - scrolloff (approximation of how much will be
346 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 line_count = 0;
348 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100349 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 {
351 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100352 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (lnum <= 0 || line_count > curwin->w_height + 1)
354 break;
355 (void)hasFolding(lnum, &lnum, NULL);
356 }
357 }
358 else
359#endif
360 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100361 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000363 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 else
365 scroll_cursor_halfway(FALSE);
366 }
367 }
368 }
369 curwin->w_valid |= VALID_TOPLINE;
370
371 /*
372 * Need to redraw when topline changed.
373 */
374 if (curwin->w_topline != old_topline
375#ifdef FEAT_DIFF
376 || curwin->w_topfill != old_topfill
377#endif
378 )
379 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100380 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000381 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
383 curwin->w_skipcol = 0;
384 redraw_later(NOT_VALID);
385 }
386 else
387 redraw_later(VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100388 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 if (curwin->w_cursor.lnum == curwin->w_topline)
390 validate_cursor();
391 }
392
Bram Moolenaar375e3392019-01-31 18:26:10 +0100393 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394}
395
396/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000397 * Return the scrolljump value to use for the current window.
398 * When 'scrolljump' is positive use it as-is.
399 * When 'scrolljump' is negative use it as a percentage of the window height.
400 */
401 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100402scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000403{
404 if (p_sj >= 0)
405 return (int)p_sj;
406 return (curwin->w_height * -p_sj) / 100;
407}
408
409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
411 * current window.
412 */
413 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100414check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
416 lineoff_T loff;
417 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100418 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419
Bram Moolenaar375e3392019-01-31 18:26:10 +0100420 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#ifdef FEAT_FOLDING
422 || hasAnyFolding(curwin)
423#endif
424 )
425 {
426 loff.lnum = curwin->w_cursor.lnum;
427#ifdef FEAT_DIFF
428 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100429 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430#else
431 n = 0;
432#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100433 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100434 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {
436 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100437 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 if (loff.lnum < curwin->w_topline
439#ifdef FEAT_DIFF
440 || (loff.lnum == curwin->w_topline && loff.fill > 0)
441#endif
442 )
443 break;
444 n += loff.height;
445 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100446 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 return TRUE;
448 }
449 return FALSE;
450}
451
452 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100453update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 if (curwin->w_set_curswant)
456 {
457 validate_virtcol();
458 curwin->w_curswant = curwin->w_virtcol;
459 curwin->w_set_curswant = FALSE;
460 }
461}
462
463/*
464 * Check if the cursor has moved. Set the w_valid flag accordingly.
465 */
466 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100467check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468{
469 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
470 {
471 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
472 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
473 wp->w_valid_cursor = wp->w_cursor;
474 wp->w_valid_leftcol = wp->w_leftcol;
475 }
476 else if (wp->w_cursor.col != wp->w_valid_cursor.col
477 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100478 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 {
480 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
481 wp->w_valid_cursor.col = wp->w_cursor.col;
482 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 }
485}
486
487/*
488 * Call this function when some window settings have changed, which require
489 * the cursor position, botline and topline to be recomputed and the window to
490 * be redrawn. E.g, when changing the 'wrap' option or folding.
491 */
492 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100493changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 changed_window_setting_win(curwin);
496}
497
498 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100499changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 wp->w_lines_valid = 0;
502 changed_line_abv_curs_win(wp);
503 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
504 redraw_win_later(wp, NOT_VALID);
505}
506
507/*
508 * Set wp->w_topline to a certain number.
509 */
510 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100511set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200513#ifdef FEAT_DIFF
514 linenr_T prev_topline = wp->w_topline;
515#endif
516
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100518 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
520#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100521 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100523 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
524 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000526 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200528 if (lnum != prev_topline)
529 // Keep the filler lines when the topline didn't change.
530 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531#endif
532 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100533 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 redraw_later(VALID);
535}
536
537/*
538 * Call this function when the length of the cursor line (in screen
539 * characters) has changed, and the change is before the cursor.
540 * Need to take care of w_botline separately!
541 */
542 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100543changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
546 |VALID_CHEIGHT|VALID_TOPLINE);
547}
548
549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100550changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
552 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
553 |VALID_CHEIGHT|VALID_TOPLINE);
554}
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556/*
557 * Call this function when the length of a line (in screen characters) above
558 * the cursor have changed.
559 * Need to take care of w_botline separately!
560 */
561 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100562changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563{
564 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
565 |VALID_CHEIGHT|VALID_TOPLINE);
566}
567
568 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100569changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
572 |VALID_CHEIGHT|VALID_TOPLINE);
573}
574
575/*
576 * Make sure the value of curwin->w_botline is valid.
577 */
578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100579validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100581 validate_botline_win(curwin);
582}
583
584/*
585 * Make sure the value of wp->w_botline is valid.
586 */
587 void
588validate_botline_win(win_T *wp)
589{
590 if (!(wp->w_valid & VALID_BOTLINE))
591 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592}
593
594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 * Mark curwin->w_botline as invalid (because of some change in the buffer).
596 */
597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
601}
602
603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
607}
608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100610approximate_botline_win(
611 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 wp->w_valid &= ~VALID_BOTLINE;
614}
615
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616/*
617 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
618 */
619 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100620cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 check_cursor_moved(curwin);
623 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
624 (VALID_WROW|VALID_WCOL));
625}
626
627/*
628 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
629 * w_topline must be valid, you may need to call update_topline() first!
630 */
631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100632validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 check_cursor_moved(curwin);
635 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
636 curs_columns(TRUE);
637}
638
639#if defined(FEAT_GUI) || defined(PROTO)
640/*
641 * validate w_cline_row.
642 */
643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 /*
647 * First make sure that w_topline is valid (after moving the cursor).
648 */
649 update_topline();
650 check_cursor_moved(curwin);
651 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100652 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654#endif
655
656/*
657 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200658 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 */
660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100661curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
663 linenr_T lnum;
664 int i;
665 int all_invalid;
666 int valid;
667#ifdef FEAT_FOLDING
668 long fold_count;
669#endif
670
Bram Moolenaar85a20022019-12-21 18:25:54 +0100671 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 all_invalid = (!redrawing()
673 || wp->w_lines_valid == 0
674 || wp->w_lines[0].wl_lnum > wp->w_topline);
675 i = 0;
676 wp->w_cline_row = 0;
677 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
678 {
679 valid = FALSE;
680 if (!all_invalid && i < wp->w_lines_valid)
681 {
682 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100683 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (wp->w_lines[i].wl_lnum == lnum)
685 {
686#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100687 // Check for newly inserted lines below this row, in which
688 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (!wp->w_buffer->b_mod_set
690 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
691 || wp->w_buffer->b_mod_top
692 > wp->w_lines[i].wl_lastlnum + 1)
693#endif
694 valid = TRUE;
695 }
696 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100697 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 }
699 if (valid
700#ifdef FEAT_DIFF
701 && (lnum != wp->w_topline || !wp->w_p_diff)
702#endif
703 )
704 {
705#ifdef FEAT_FOLDING
706 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100707 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (lnum > wp->w_cursor.lnum)
709 break;
710#else
711 ++lnum;
712#endif
713 wp->w_cline_row += wp->w_lines[i].wl_size;
714 }
715 else
716 {
717#ifdef FEAT_FOLDING
718 fold_count = foldedCount(wp, lnum, NULL);
719 if (fold_count)
720 {
721 lnum += fold_count;
722 if (lnum > wp->w_cursor.lnum)
723 break;
724 ++wp->w_cline_row;
725 }
726 else
727#endif
728#ifdef FEAT_DIFF
729 if (lnum == wp->w_topline)
730 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
731 + wp->w_topfill;
732 else
733#endif
734 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
735 }
736 }
737
738 check_cursor_moved(wp);
739 if (!(wp->w_valid & VALID_CHEIGHT))
740 {
741 if (all_invalid
742 || i == wp->w_lines_valid
743 || (i < wp->w_lines_valid
744 && (!wp->w_lines[i].wl_valid
745 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
746 {
747#ifdef FEAT_DIFF
748 if (wp->w_cursor.lnum == wp->w_topline)
749 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
750 TRUE) + wp->w_topfill;
751 else
752#endif
753 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
754#ifdef FEAT_FOLDING
755 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
756 NULL, NULL, TRUE, NULL);
757#endif
758 }
759 else if (i > wp->w_lines_valid)
760 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100761 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 wp->w_cline_height = 0;
763#ifdef FEAT_FOLDING
764 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
765 NULL, NULL, TRUE, NULL);
766#endif
767 }
768 else
769 {
770 wp->w_cline_height = wp->w_lines[i].wl_size;
771#ifdef FEAT_FOLDING
772 wp->w_cline_folded = wp->w_lines[i].wl_folded;
773#endif
774 }
775 }
776
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100777 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
782/*
783 * Validate curwin->w_virtcol only.
784 */
785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100786validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788 validate_virtcol_win(curwin);
789}
790
791/*
792 * Validate wp->w_virtcol only.
793 */
794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100795validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 check_cursor_moved(wp);
798 if (!(wp->w_valid & VALID_VIRTCOL))
799 {
800 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
801 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000802#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200803 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000804 redraw_win_later(wp, SOME_VALID);
805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
807}
808
809/*
810 * Validate curwin->w_cline_height only.
811 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100812 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100813validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 check_cursor_moved(curwin);
816 if (!(curwin->w_valid & VALID_CHEIGHT))
817 {
818#ifdef FEAT_DIFF
819 if (curwin->w_cursor.lnum == curwin->w_topline)
820 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
821 + curwin->w_topfill;
822 else
823#endif
824 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
825#ifdef FEAT_FOLDING
826 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
827#endif
828 curwin->w_valid |= VALID_CHEIGHT;
829 }
830}
831
832/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000833 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 */
835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100836validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 colnr_T off;
839 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100840 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842 validate_virtcol();
843 if (!(curwin->w_valid & VALID_WCOL))
844 {
845 col = curwin->w_virtcol;
846 off = curwin_col_off();
847 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200848 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaar85a20022019-12-21 18:25:54 +0100850 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000851 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200852 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100853 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100854 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200855 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000856 if (col > (int)curwin->w_leftcol)
857 col -= curwin->w_leftcol;
858 else
859 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000861
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100863#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100864 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867}
868
869/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200870 * Compute offset of a window, occupied by absolute or relative line number,
871 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 */
873 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100874win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
Bram Moolenaar64486672010-05-16 15:46:46 +0200876 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#ifdef FEAT_CMDWIN
878 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
879#endif
880#ifdef FEAT_FOLDING
881 + wp->w_p_fdc
882#endif
883#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200884 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#endif
886 );
887}
888
889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100890curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 return win_col_off(curwin);
893}
894
895/*
896 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200897 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
898 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 */
900 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100901win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902{
Bram Moolenaar64486672010-05-16 15:46:46 +0200903 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000904 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 return 0;
906}
907
908 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100909curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 return win_col_off2(curwin);
912}
913
914/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100915 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 * Also updates curwin->w_wrow and curwin->w_cline_row.
917 * Also updates curwin->w_leftcol.
918 */
919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100920curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100921 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100924 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 int off_left, off_right;
926 int n;
927 int p_lines;
928 int width = 0;
929 int textwidth;
930 int new_leftcol;
931 colnr_T startcol;
932 colnr_T endcol;
933 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100934 long so = get_scrolloff_value();
935 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
937 /*
938 * First make sure that w_topline is valid (after moving the cursor).
939 */
940 update_topline();
941
942 /*
943 * Next make sure that w_cline_row is valid.
944 */
945 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100946 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947
948 /*
949 * Compute the number of virtual columns.
950 */
951#ifdef FEAT_FOLDING
952 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100953 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
955 else
956#endif
957 getvvcol(curwin, &curwin->w_cursor,
958 &startcol, &(curwin->w_virtcol), &endcol);
959
Bram Moolenaar85a20022019-12-21 18:25:54 +0100960 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100962 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
964 extra = curwin_col_off();
965 curwin->w_wcol = curwin->w_virtcol + extra;
966 endcol += extra;
967
968 /*
969 * Now compute w_wrow, counting screen lines from w_cline_row.
970 */
971 curwin->w_wrow = curwin->w_cline_row;
972
Bram Moolenaar02631462017-09-22 15:20:32 +0200973 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (textwidth <= 0)
975 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100976 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +0200977 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +0200978 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +0200979 if (curwin->w_p_wrap)
980 curwin->w_wrow = curwin->w_height - 1;
981 else
982 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200984 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 width = textwidth + curwin_col_off2();
987
Bram Moolenaar85a20022019-12-21 18:25:54 +0100988 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +0200989 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100991#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +0100992 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100993#endif
Bram Moolenaaree857022019-11-09 23:26:40 +0100994
Bram Moolenaar85a20022019-12-21 18:25:54 +0100995 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +0200996 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 curwin->w_wcol -= n * width;
998 curwin->w_wrow += n;
999
1000#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001001 // When cursor wraps to first char of next line in Insert
1002 // mode, the 'showbreak' string isn't shown, backup to first
1003 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001004 sbr = get_showbreak_value(curwin);
1005 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001006 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 curwin->w_wcol = 0;
1008#endif
1009 }
1010 }
1011
Bram Moolenaar85a20022019-12-21 18:25:54 +01001012 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1013 // is not folded.
1014 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001015 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#ifdef FEAT_FOLDING
1017 && !curwin->w_cline_folded
1018#endif
1019 )
1020 {
1021 /*
1022 * If Cursor is left of the screen, scroll rightwards.
1023 * If Cursor is right of the screen, scroll leftwards
1024 * If we get closer to the edge than 'sidescrolloff', scroll a little
1025 * extra
1026 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001027 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001028 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001029 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (off_left < 0 || off_right > 0)
1031 {
1032 if (off_left < 0)
1033 diff = -off_left;
1034 else
1035 diff = off_right;
1036
Bram Moolenaar85a20022019-12-21 18:25:54 +01001037 // When far off or not enough room on either side, put cursor in
1038 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1040 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1041 else
1042 {
1043 if (diff < p_ss)
1044 diff = p_ss;
1045 if (off_left < 0)
1046 new_leftcol = curwin->w_leftcol - diff;
1047 else
1048 new_leftcol = curwin->w_leftcol + diff;
1049 }
1050 if (new_leftcol < 0)
1051 new_leftcol = 0;
1052 if (new_leftcol != (int)curwin->w_leftcol)
1053 {
1054 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001055 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 redraw_later(NOT_VALID);
1057 }
1058 }
1059 curwin->w_wcol -= curwin->w_leftcol;
1060 }
1061 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1062 curwin->w_wcol -= curwin->w_leftcol;
1063 else
1064 curwin->w_wcol = 0;
1065
1066#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001067 // Skip over filler lines. At the top use w_topfill, there
1068 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (curwin->w_cursor.lnum == curwin->w_topline)
1070 curwin->w_wrow += curwin->w_topfill;
1071 else
1072 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1073#endif
1074
1075 prev_skipcol = curwin->w_skipcol;
1076
1077 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if ((curwin->w_wrow >= curwin->w_height
1080 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001081 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 && (p_lines =
1083#ifdef FEAT_DIFF
1084 plines_win_nofill
1085#else
1086 plines_win
1087#endif
1088 (curwin, curwin->w_cursor.lnum, FALSE))
1089 - 1 >= curwin->w_height))
1090 && curwin->w_height != 0
1091 && curwin->w_cursor.lnum == curwin->w_topline
1092 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001093 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001095 // Cursor past end of screen. Happens with a single line that does
1096 // not fit on screen. Find a skipcol to show the text around the
1097 // cursor. Avoid scrolling all the time. compute value of "extra":
1098 // 1: Less than 'scrolloff' lines above
1099 // 2: Less than 'scrolloff' lines below
1100 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001102 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001104 // Compute last display line of the buffer line that we want at the
1105 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (p_lines == 0)
1107 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1108 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001109 if (p_lines > curwin->w_wrow + so)
1110 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 else
1112 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001113 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 extra += 2;
1115
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001116 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001118 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 n = curwin->w_virtcol / width;
1120 if (n > curwin->w_height / 2)
1121 n -= curwin->w_height / 2;
1122 else
1123 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (n > p_lines - curwin->w_height + 1)
1126 n = p_lines - curwin->w_height + 1;
1127 curwin->w_skipcol = n * width;
1128 }
1129 else if (extra == 1)
1130 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001131 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001132 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 + width - 1) / width;
1134 if (extra > 0)
1135 {
1136 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1137 extra = curwin->w_skipcol / width;
1138 curwin->w_skipcol -= extra * width;
1139 }
1140 }
1141 else if (extra == 2)
1142 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001143 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 endcol = (n - curwin->w_height + 1) * width;
1145 while (endcol > curwin->w_virtcol)
1146 endcol -= width;
1147 if (endcol > curwin->w_skipcol)
1148 curwin->w_skipcol = endcol;
1149 }
1150
1151 curwin->w_wrow -= curwin->w_skipcol / width;
1152 if (curwin->w_wrow >= curwin->w_height)
1153 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 extra = curwin->w_wrow - curwin->w_height + 1;
1156 curwin->w_skipcol += extra * width;
1157 curwin->w_wrow -= extra;
1158 }
1159
1160 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1161 if (extra > 0)
1162 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1163 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001164 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 else
1167 curwin->w_skipcol = 0;
1168 if (prev_skipcol != curwin->w_skipcol)
1169 redraw_later(NOT_VALID);
1170
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001171#ifdef FEAT_SYN_HL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001172 // Redraw when w_virtcol changes and 'cursorcolumn' is set
Bram Moolenaarb6798752014-03-27 12:11:48 +01001173 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001174 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001175 redraw_later(SOME_VALID);
1176#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001177#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1178 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1179 {
1180 curwin->w_wrow += popup_top_extra(curwin);
1181 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001182 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001183 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001184 else
1185 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001186#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001187
Bram Moolenaar08f23632019-11-16 14:19:33 +01001188 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1189 curwin->w_valid_leftcol = curwin->w_leftcol;
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1192}
1193
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001194#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001195/*
1196 * Compute the screen position of text character at "pos" in window "wp"
1197 * The resulting values are one-based, zero when character is not visible.
1198 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001199 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001200textpos2screenpos(
1201 win_T *wp,
1202 pos_T *pos,
1203 int *rowp, // screen row
1204 int *scolp, // start screen column
1205 int *ccolp, // cursor screen column
1206 int *ecolp) // end screen column
1207{
1208 colnr_T scol = 0, ccol = 0, ecol = 0;
1209 int row = 0;
1210 int rowoff = 0;
1211 colnr_T coloff = 0;
1212
Bram Moolenaar189663b2021-07-21 18:04:56 +02001213 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001214 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001215 colnr_T off;
1216 colnr_T col;
1217 int width;
1218 linenr_T lnum = pos->lnum;
1219#ifdef FEAT_FOLDING
1220 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001221
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001222 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1223#endif
1224 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1225#ifdef FEAT_FOLDING
1226 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001227 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001228 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001229 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001230 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001231 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001232#endif
1233 {
1234 getvcol(wp, pos, &scol, &ccol, &ecol);
1235
1236 // similar to what is done in validate_cursor_col()
1237 col = scol;
1238 off = win_col_off(wp);
1239 col += off;
1240 width = wp->w_width - off + win_col_off2(wp);
1241
1242 // long line wrapping, adjust row
1243 if (wp->w_p_wrap
1244 && col >= (colnr_T)wp->w_width
1245 && width > 0)
1246 {
1247 // use same formula as what is used in curs_columns()
1248 rowoff = ((col - wp->w_width) / width + 1);
1249 col -= rowoff * width;
1250 }
1251 col -= wp->w_leftcol;
1252 if (col >= wp->w_width)
1253 col = -1;
1254 if (col >= 0 && row + rowoff <= wp->w_height)
1255 {
1256 coloff = col - scol + wp->w_wincol + 1;
1257 row += W_WINROW(wp);
1258 }
1259 else
1260 // character is left, right or below of the window
1261 row = rowoff = scol = ccol = ecol = 0;
1262 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001263 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001264 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001265 *scolp = scol + coloff;
1266 *ccolp = ccol + coloff;
1267 *ecolp = ecol + coloff;
1268}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001269#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001270
Bram Moolenaar12034e22019-08-25 22:25:02 +02001271#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001272/*
1273 * "screenpos({winid}, {lnum}, {col})" function
1274 */
1275 void
1276f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1277{
1278 dict_T *dict;
1279 win_T *wp;
1280 pos_T pos;
1281 int row = 0;
1282 int scol = 0, ccol = 0, ecol = 0;
1283
1284 if (rettv_dict_alloc(rettv) != OK)
1285 return;
1286 dict = rettv->vval.v_dict;
1287
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001288 if (in_vim9script()
1289 && (check_for_number_arg(argvars, 0) == FAIL
1290 || check_for_number_arg(argvars, 1) == FAIL
1291 || check_for_number_arg(argvars, 2) == FAIL))
1292 return;
1293
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001294 wp = find_win_by_nr_or_id(&argvars[0]);
1295 if (wp == NULL)
1296 return;
1297
1298 pos.lnum = tv_get_number(&argvars[1]);
1299 pos.col = tv_get_number(&argvars[2]) - 1;
1300 pos.coladd = 0;
1301 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1302
1303 dict_add_number(dict, "row", row);
1304 dict_add_number(dict, "col", scol);
1305 dict_add_number(dict, "curscol", ccol);
1306 dict_add_number(dict, "endcol", ecol);
1307}
1308#endif
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310/*
1311 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001314scrolldown(
1315 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001316 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001318 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 int wrow;
1320 int moved = FALSE;
1321
1322#ifdef FEAT_FOLDING
1323 linenr_T first;
1324
Bram Moolenaar85a20022019-12-21 18:25:54 +01001325 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1327#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001328 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 while (line_count-- > 0)
1330 {
1331#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001332 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1333 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
1335 ++curwin->w_topfill;
1336 ++done;
1337 }
1338 else
1339#endif
1340 {
1341 if (curwin->w_topline == 1)
1342 break;
1343 --curwin->w_topline;
1344#ifdef FEAT_DIFF
1345 curwin->w_topfill = 0;
1346#endif
1347#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001348 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (hasFolding(curwin->w_topline, &first, NULL))
1350 {
1351 ++done;
1352 if (!byfold)
1353 line_count -= curwin->w_topline - first - 1;
1354 curwin->w_botline -= curwin->w_topline - first;
1355 curwin->w_topline = first;
1356 }
1357 else
1358#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001359 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001361 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 invalidate_botline();
1363 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001364 curwin->w_wrow += done; // keep w_wrow updated
1365 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367#ifdef FEAT_DIFF
1368 if (curwin->w_cursor.lnum == curwin->w_topline)
1369 curwin->w_cline_row = 0;
1370 check_topfill(curwin, TRUE);
1371#endif
1372
1373 /*
1374 * Compute the row number of the last row of the cursor line
1375 * and move the cursor onto the displayed part of the window.
1376 */
1377 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001378 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 validate_virtcol();
1381 validate_cheight();
1382 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001383 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1386 {
1387#ifdef FEAT_FOLDING
1388 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1389 {
1390 --wrow;
1391 if (first == 1)
1392 curwin->w_cursor.lnum = 1;
1393 else
1394 curwin->w_cursor.lnum = first - 1;
1395 }
1396 else
1397#endif
1398 wrow -= plines(curwin->w_cursor.lnum--);
1399 curwin->w_valid &=
1400 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1401 moved = TRUE;
1402 }
1403 if (moved)
1404 {
1405#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001406 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 foldAdjustCursor();
1408#endif
1409 coladvance(curwin->w_curswant);
1410 }
1411}
1412
1413/*
1414 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001417scrollup(
1418 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001419 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1422 linenr_T lnum;
1423
1424 if (
1425# ifdef FEAT_FOLDING
1426 (byfold && hasAnyFolding(curwin))
1427# ifdef FEAT_DIFF
1428 ||
1429# endif
1430# endif
1431# ifdef FEAT_DIFF
1432 curwin->w_p_diff
1433# endif
1434 )
1435 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001436 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 lnum = curwin->w_topline;
1438 while (line_count--)
1439 {
1440# ifdef FEAT_DIFF
1441 if (curwin->w_topfill > 0)
1442 --curwin->w_topfill;
1443 else
1444# endif
1445 {
1446# ifdef FEAT_FOLDING
1447 if (byfold)
1448 (void)hasFolding(lnum, NULL, &lnum);
1449# endif
1450 if (lnum >= curbuf->b_ml.ml_line_count)
1451 break;
1452 ++lnum;
1453# ifdef FEAT_DIFF
1454 curwin->w_topfill = diff_check_fill(curwin, lnum);
1455# endif
1456 }
1457 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001458 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 curwin->w_botline += lnum - curwin->w_topline;
1460 curwin->w_topline = lnum;
1461 }
1462 else
1463#endif
1464 {
1465 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001466 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468
1469 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1470 curwin->w_topline = curbuf->b_ml.ml_line_count;
1471 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1472 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1473
1474#ifdef FEAT_DIFF
1475 check_topfill(curwin, FALSE);
1476#endif
1477
1478#ifdef FEAT_FOLDING
1479 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001480 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1482#endif
1483
1484 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1485 if (curwin->w_cursor.lnum < curwin->w_topline)
1486 {
1487 curwin->w_cursor.lnum = curwin->w_topline;
1488 curwin->w_valid &=
1489 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1490 coladvance(curwin->w_curswant);
1491 }
1492}
1493
1494#ifdef FEAT_DIFF
1495/*
1496 * Don't end up with too many filler lines in the window.
1497 */
1498 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001499check_topfill(
1500 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001501 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 int n;
1504
1505 if (wp->w_topfill > 0)
1506 {
1507 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1508 if (wp->w_topfill + n > wp->w_height)
1509 {
1510 if (down && wp->w_topline > 1)
1511 {
1512 --wp->w_topline;
1513 wp->w_topfill = 0;
1514 }
1515 else
1516 {
1517 wp->w_topfill = wp->w_height - n;
1518 if (wp->w_topfill < 0)
1519 wp->w_topfill = 0;
1520 }
1521 }
1522 }
1523}
1524
1525/*
1526 * Use as many filler lines as possible for w_topline. Make sure w_topline
1527 * is still visible.
1528 */
1529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001530max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 int n;
1533
1534 n = plines_nofill(curwin->w_topline);
1535 if (n >= curwin->w_height)
1536 curwin->w_topfill = 0;
1537 else
1538 {
1539 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1540 if (curwin->w_topfill + n > curwin->w_height)
1541 curwin->w_topfill = curwin->w_height - n;
1542 }
1543}
1544#endif
1545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546/*
1547 * Scroll the screen one line down, but don't do it if it would move the
1548 * cursor off the screen.
1549 */
1550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001551scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 int end_row;
1554#ifdef FEAT_DIFF
1555 int can_fill = (curwin->w_topfill
1556 < diff_check_fill(curwin, curwin->w_topline));
1557#endif
1558
1559 if (curwin->w_topline <= 1
1560#ifdef FEAT_DIFF
1561 && !can_fill
1562#endif
1563 )
1564 return;
1565
Bram Moolenaar85a20022019-12-21 18:25:54 +01001566 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 /*
1569 * Compute the row number of the last row of the cursor line
1570 * and make sure it doesn't go off the screen. Make sure the cursor
1571 * doesn't go past 'scrolloff' lines from the screen end.
1572 */
1573 end_row = curwin->w_wrow;
1574#ifdef FEAT_DIFF
1575 if (can_fill)
1576 ++end_row;
1577 else
1578 end_row += plines_nofill(curwin->w_topline - 1);
1579#else
1580 end_row += plines(curwin->w_topline - 1);
1581#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001582 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 validate_cheight();
1585 validate_virtcol();
1586 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001587 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001589 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591#ifdef FEAT_DIFF
1592 if (can_fill)
1593 {
1594 ++curwin->w_topfill;
1595 check_topfill(curwin, TRUE);
1596 }
1597 else
1598 {
1599 --curwin->w_topline;
1600 curwin->w_topfill = 0;
1601 }
1602#else
1603 --curwin->w_topline;
1604#endif
1605#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001606 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001608 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1610 }
1611}
1612
1613/*
1614 * Scroll the screen one line up, but don't do it if it would move the cursor
1615 * off the screen.
1616 */
1617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001618scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 int start_row;
1621
1622 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1623#ifdef FEAT_DIFF
1624 && curwin->w_topfill == 0
1625#endif
1626 )
1627 return;
1628
Bram Moolenaar85a20022019-12-21 18:25:54 +01001629 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
1631 /*
1632 * Compute the row number of the first row of the cursor line
1633 * and make sure it doesn't go off the screen. Make sure the cursor
1634 * doesn't go before 'scrolloff' lines from the screen start.
1635 */
1636#ifdef FEAT_DIFF
1637 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1638 - curwin->w_topfill;
1639#else
1640 start_row = curwin->w_wrow - plines(curwin->w_topline);
1641#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001642 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001645 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001647 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 {
1649#ifdef FEAT_DIFF
1650 if (curwin->w_topfill > 0)
1651 --curwin->w_topfill;
1652 else
1653#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001654 {
1655#ifdef FEAT_FOLDING
1656 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001659 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001660 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1662 }
1663}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665/*
1666 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1667 * a (wrapped) text line. Uses and sets "lp->fill".
1668 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001669 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 */
1671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001672topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674#ifdef FEAT_DIFF
1675 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1676 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001677 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 ++lp->fill;
1679 lp->height = 1;
1680 }
1681 else
1682#endif
1683 {
1684 --lp->lnum;
1685#ifdef FEAT_DIFF
1686 lp->fill = 0;
1687#endif
1688 if (lp->lnum < 1)
1689 lp->height = MAXCOL;
1690 else
1691#ifdef FEAT_FOLDING
1692 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001693 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 lp->height = 1;
1695 else
1696#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001697 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699}
1700
1701/*
1702 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1703 * a (wrapped) text line. Uses and sets "lp->fill".
1704 * Returns the height of the added line in "lp->height".
1705 * Lines below the last one are incredibly high.
1706 */
1707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001708botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710#ifdef FEAT_DIFF
1711 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1712 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001713 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 ++lp->fill;
1715 lp->height = 1;
1716 }
1717 else
1718#endif
1719 {
1720 ++lp->lnum;
1721#ifdef FEAT_DIFF
1722 lp->fill = 0;
1723#endif
1724 if (lp->lnum > curbuf->b_ml.ml_line_count)
1725 lp->height = MAXCOL;
1726 else
1727#ifdef FEAT_FOLDING
1728 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001729 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 lp->height = 1;
1731 else
1732#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001733 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
1735}
1736
1737#ifdef FEAT_DIFF
1738/*
1739 * Switch from including filler lines below lp->lnum to including filler
1740 * lines above loff.lnum + 1. This keeps pointing to the same line.
1741 * When there are no filler lines nothing changes.
1742 */
1743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001744botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 if (lp->fill > 0)
1747 {
1748 ++lp->lnum;
1749 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1750 }
1751}
1752
1753/*
1754 * Switch from including filler lines above lp->lnum to including filler
1755 * lines below loff.lnum - 1. This keeps pointing to the same line.
1756 * When there are no filler lines nothing changes.
1757 */
1758 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001759topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 if (lp->fill > 0)
1762 {
1763 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1764 --lp->lnum;
1765 }
1766}
1767#endif
1768
1769/*
1770 * Recompute topline to put the cursor at the top of the window.
1771 * Scroll at least "min_scroll" lines.
1772 * If "always" is TRUE, always set topline (for "zt").
1773 */
1774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 int scrolled = 0;
1778 int extra = 0;
1779 int used;
1780 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001781 linenr_T top; // just above displayed lines
1782 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 linenr_T old_topline = curwin->w_topline;
1784#ifdef FEAT_DIFF
1785 linenr_T old_topfill = curwin->w_topfill;
1786#endif
1787 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001788 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (mouse_dragging > 0)
1791 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793 /*
1794 * Decrease topline until:
1795 * - it has become 1
1796 * - (part of) the cursor line is moved off the screen or
1797 * - moved at least 'scrolljump' lines and
1798 * - at least 'scrolloff' lines above and below the cursor
1799 */
1800 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001801 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (curwin->w_cursor.lnum < curwin->w_topline)
1803 scrolled = used;
1804
1805#ifdef FEAT_FOLDING
1806 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1807 {
1808 --top;
1809 ++bot;
1810 }
1811 else
1812#endif
1813 {
1814 top = curwin->w_cursor.lnum - 1;
1815 bot = curwin->w_cursor.lnum + 1;
1816 }
1817 new_topline = top + 1;
1818
1819#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001820 // "used" already contains the number of filler lines above, don't add it
1821 // again.
1822 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001823 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#endif
1825
1826 /*
1827 * Check if the lines from "top" to "bot" fit in the window. If they do,
1828 * set new_topline and advance "top" and "bot" to include more lines.
1829 */
1830 while (top > 0)
1831 {
1832#ifdef FEAT_FOLDING
1833 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001834 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 i = 1;
1836 else
1837#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001838 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 used += i;
1840 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1841 {
1842#ifdef FEAT_FOLDING
1843 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001844 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 ++used;
1846 else
1847#endif
1848 used += plines(bot);
1849 }
1850 if (used > curwin->w_height)
1851 break;
1852 if (top < curwin->w_topline)
1853 scrolled += i;
1854
1855 /*
1856 * If scrolling is needed, scroll at least 'sj' lines.
1857 */
1858 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1859 && extra >= off)
1860 break;
1861
1862 extra += i;
1863 new_topline = top;
1864 --top;
1865 ++bot;
1866 }
1867
1868 /*
1869 * If we don't have enough space, put cursor in the middle.
1870 * This makes sure we get the same position when using "k" and "j"
1871 * in a small window.
1872 */
1873 if (used > curwin->w_height)
1874 scroll_cursor_halfway(FALSE);
1875 else
1876 {
1877 /*
1878 * If "always" is FALSE, only adjust topline to a lower value, higher
1879 * value may happen with wrapping lines
1880 */
1881 if (new_topline < curwin->w_topline || always)
1882 curwin->w_topline = new_topline;
1883 if (curwin->w_topline > curwin->w_cursor.lnum)
1884 curwin->w_topline = curwin->w_cursor.lnum;
1885#ifdef FEAT_DIFF
1886 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1887 if (curwin->w_topfill > 0 && extra > off)
1888 {
1889 curwin->w_topfill -= extra - off;
1890 if (curwin->w_topfill < 0)
1891 curwin->w_topfill = 0;
1892 }
1893 check_topfill(curwin, FALSE);
1894#endif
1895 if (curwin->w_topline != old_topline
1896#ifdef FEAT_DIFF
1897 || curwin->w_topfill != old_topfill
1898#endif
1899 )
1900 curwin->w_valid &=
1901 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1902 curwin->w_valid |= VALID_TOPLINE;
1903 }
1904}
1905
1906/*
1907 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1908 * screen lines for text lines.
1909 */
1910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001911set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
1913#ifdef FEAT_DIFF
1914 wp->w_filler_rows = 0;
1915#endif
1916 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001917 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 else
1919 {
1920 wp->w_empty_rows = wp->w_height - used;
1921#ifdef FEAT_DIFF
1922 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1923 {
1924 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1925 if (wp->w_empty_rows > wp->w_filler_rows)
1926 wp->w_empty_rows -= wp->w_filler_rows;
1927 else
1928 {
1929 wp->w_filler_rows = wp->w_empty_rows;
1930 wp->w_empty_rows = 0;
1931 }
1932 }
1933#endif
1934 }
1935}
1936
1937/*
1938 * Recompute topline to put the cursor at the bottom of the window.
1939 * Scroll at least "min_scroll" lines.
1940 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1941 * This is messy stuff!!!
1942 */
1943 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001944scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 int used;
1947 int scrolled = 0;
1948 int extra = 0;
1949 int i;
1950 linenr_T line_count;
1951 linenr_T old_topline = curwin->w_topline;
1952 lineoff_T loff;
1953 lineoff_T boff;
1954#ifdef FEAT_DIFF
1955 int old_topfill = curwin->w_topfill;
1956 int fill_below_window;
1957#endif
1958 linenr_T old_botline = curwin->w_botline;
1959 linenr_T old_valid = curwin->w_valid;
1960 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001961 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001962 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 cln = curwin->w_cursor.lnum;
1965 if (set_topbot)
1966 {
1967 used = 0;
1968 curwin->w_botline = cln + 1;
1969#ifdef FEAT_DIFF
1970 loff.fill = 0;
1971#endif
1972 for (curwin->w_topline = curwin->w_botline;
1973 curwin->w_topline > 1;
1974 curwin->w_topline = loff.lnum)
1975 {
1976 loff.lnum = curwin->w_topline;
1977 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001978 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 break;
1980 used += loff.height;
1981#ifdef FEAT_DIFF
1982 curwin->w_topfill = loff.fill;
1983#endif
1984 }
1985 set_empty_rows(curwin, used);
1986 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1987 if (curwin->w_topline != old_topline
1988#ifdef FEAT_DIFF
1989 || curwin->w_topfill != old_topfill
1990#endif
1991 )
1992 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1993 }
1994 else
1995 validate_botline();
1996
Bram Moolenaar85a20022019-12-21 18:25:54 +01001997 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998#ifdef FEAT_DIFF
1999 used = plines_nofill(cln);
2000#else
2001 validate_cheight();
2002 used = curwin->w_cline_height;
2003#endif
2004
Bram Moolenaar85a20022019-12-21 18:25:54 +01002005 // If the cursor is below botline, we will at least scroll by the height
2006 // of the cursor line. Correct for empty lines, which are really part of
2007 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (cln >= curwin->w_botline)
2009 {
2010 scrolled = used;
2011 if (cln == curwin->w_botline)
2012 scrolled -= curwin->w_empty_rows;
2013 }
2014
2015 /*
2016 * Stop counting lines to scroll when
2017 * - hitting start of the file
2018 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002019 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 * - lines between botline and cursor have been counted
2021 */
2022#ifdef FEAT_FOLDING
2023 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2024#endif
2025 {
2026 loff.lnum = cln;
2027 boff.lnum = cln;
2028 }
2029#ifdef FEAT_DIFF
2030 loff.fill = 0;
2031 boff.fill = 0;
2032 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2033 - curwin->w_filler_rows;
2034#endif
2035
2036 while (loff.lnum > 1)
2037 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002038 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2039 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002041 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2043 && loff.lnum <= curwin->w_botline
2044#ifdef FEAT_DIFF
2045 && (loff.lnum < curwin->w_botline
2046 || loff.fill >= fill_below_window)
2047#endif
2048 )
2049 break;
2050
Bram Moolenaar85a20022019-12-21 18:25:54 +01002051 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002053 if (loff.height == MAXCOL)
2054 used = MAXCOL;
2055 else
2056 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (used > curwin->w_height)
2058 break;
2059 if (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 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002066 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 scrolled += loff.height;
2068 if (loff.lnum == curwin->w_botline
2069#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002070 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#endif
2072 )
2073 scrolled -= curwin->w_empty_rows;
2074 }
2075
2076 if (boff.lnum < curbuf->b_ml.ml_line_count)
2077 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002078 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 botline_forw(&boff);
2080 used += boff.height;
2081 if (used > curwin->w_height)
2082 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002083 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2084 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 extra += boff.height;
2087 if (boff.lnum >= curwin->w_botline
2088#ifdef FEAT_DIFF
2089 || (boff.lnum + 1 == curwin->w_botline
2090 && boff.fill > curwin->w_filler_rows)
2091#endif
2092 )
2093 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002094 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 scrolled += boff.height;
2096 if (boff.lnum == curwin->w_botline
2097#ifdef FEAT_DIFF
2098 && boff.fill == 0
2099#endif
2100 )
2101 scrolled -= curwin->w_empty_rows;
2102 }
2103 }
2104 }
2105 }
2106
Bram Moolenaar85a20022019-12-21 18:25:54 +01002107 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (scrolled <= 0)
2109 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002110 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 else if (used > curwin->w_height)
2112 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002113 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 else
2115 {
2116 line_count = 0;
2117#ifdef FEAT_DIFF
2118 boff.fill = curwin->w_topfill;
2119#endif
2120 boff.lnum = curwin->w_topline - 1;
2121 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2122 {
2123 botline_forw(&boff);
2124 i += boff.height;
2125 ++line_count;
2126 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002127 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 line_count = 9999;
2129 }
2130
2131 /*
2132 * Scroll up if the cursor is off the bottom of the screen a bit.
2133 * Otherwise put it at 1/2 of the screen.
2134 */
2135 if (line_count >= curwin->w_height && line_count > min_scroll)
2136 scroll_cursor_halfway(FALSE);
2137 else
2138 scrollup(line_count, TRUE);
2139
2140 /*
2141 * If topline didn't change we need to restore w_botline and w_empty_rows
2142 * (we changed them).
2143 * If topline did change, update_screen() will set botline.
2144 */
2145 if (curwin->w_topline == old_topline && set_topbot)
2146 {
2147 curwin->w_botline = old_botline;
2148 curwin->w_empty_rows = old_empty_rows;
2149 curwin->w_valid = old_valid;
2150 }
2151 curwin->w_valid |= VALID_TOPLINE;
2152}
2153
2154/*
2155 * Recompute topline to put the cursor halfway the window
2156 * If "atend" is TRUE, also put it halfway at the end of the file.
2157 */
2158 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002159scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 int above = 0;
2162 linenr_T topline;
2163#ifdef FEAT_DIFF
2164 int topfill = 0;
2165#endif
2166 int below = 0;
2167 int used;
2168 lineoff_T loff;
2169 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002170#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002171 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002174#ifdef FEAT_PROP_POPUP
2175 // if the width changed this needs to be updated first
2176 may_update_popup_position();
2177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2179#ifdef FEAT_FOLDING
2180 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2181#endif
2182#ifdef FEAT_DIFF
2183 used = plines_nofill(loff.lnum);
2184 loff.fill = 0;
2185 boff.fill = 0;
2186#else
2187 used = plines(loff.lnum);
2188#endif
2189 topline = loff.lnum;
2190 while (topline > 1)
2191 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002192 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 if (boff.lnum < curbuf->b_ml.ml_line_count)
2195 {
2196 botline_forw(&boff);
2197 used += boff.height;
2198 if (used > curwin->w_height)
2199 break;
2200 below += boff.height;
2201 }
2202 else
2203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002204 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (atend)
2206 ++used;
2207 }
2208 }
2209
Bram Moolenaar85a20022019-12-21 18:25:54 +01002210 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
2212 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002213 if (loff.height == MAXCOL)
2214 used = MAXCOL;
2215 else
2216 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (used > curwin->w_height)
2218 break;
2219 above += loff.height;
2220 topline = loff.lnum;
2221#ifdef FEAT_DIFF
2222 topfill = loff.fill;
2223#endif
2224 }
2225 }
2226#ifdef FEAT_FOLDING
2227 if (!hasFolding(topline, &curwin->w_topline, NULL))
2228#endif
2229 curwin->w_topline = topline;
2230#ifdef FEAT_DIFF
2231 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002232 if (old_topline > curwin->w_topline + curwin->w_height)
2233 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 check_topfill(curwin, FALSE);
2235#endif
2236 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2237 curwin->w_valid |= VALID_TOPLINE;
2238}
2239
2240/*
2241 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002242 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 * If not possible, put it at the same position as scroll_cursor_halfway().
2244 * When called topline must be valid!
2245 */
2246 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002247cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002249 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002251 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 linenr_T botline;
2253 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002256 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
2258 /*
2259 * How many lines we would like to have above/below the cursor depends on
2260 * whether the first/last line of the file is on screen.
2261 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002262 above_wanted = so;
2263 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002264 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
2266 above_wanted = mouse_dragging - 1;
2267 below_wanted = mouse_dragging - 1;
2268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (curwin->w_topline == 1)
2270 {
2271 above_wanted = 0;
2272 max_off = curwin->w_height / 2;
2273 if (below_wanted > max_off)
2274 below_wanted = max_off;
2275 }
2276 validate_botline();
2277 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002278 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
2280 below_wanted = 0;
2281 max_off = (curwin->w_height - 1) / 2;
2282 if (above_wanted > max_off)
2283 above_wanted = max_off;
2284 }
2285
2286 /*
2287 * If there are sufficient file-lines above and below the cursor, we can
2288 * return now.
2289 */
2290 cln = curwin->w_cursor.lnum;
2291 if (cln >= curwin->w_topline + above_wanted
2292 && cln < curwin->w_botline - below_wanted
2293#ifdef FEAT_FOLDING
2294 && !hasAnyFolding(curwin)
2295#endif
2296 )
2297 return;
2298
2299 /*
2300 * Narrow down the area where the cursor can be put by taking lines from
2301 * the top and the bottom until:
2302 * - the desired context lines are found
2303 * - the lines from the top is past the lines from the bottom
2304 */
2305 topline = curwin->w_topline;
2306 botline = curwin->w_botline - 1;
2307#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002308 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 above = curwin->w_topfill;
2310 below = curwin->w_filler_rows;
2311#endif
2312 while ((above < above_wanted || below < below_wanted) && topline < botline)
2313 {
2314 if (below < below_wanted && (below <= above || above >= above_wanted))
2315 {
2316#ifdef FEAT_FOLDING
2317 if (hasFolding(botline, &botline, NULL))
2318 ++below;
2319 else
2320#endif
2321 below += plines(botline);
2322 --botline;
2323 }
2324 if (above < above_wanted && (above < below || below >= below_wanted))
2325 {
2326#ifdef FEAT_FOLDING
2327 if (hasFolding(topline, NULL, &topline))
2328 ++above;
2329 else
2330#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002331 above += PLINES_NOFILL(topline);
2332#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002333 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (topline < botline)
2335 above += diff_check_fill(curwin, topline + 1);
2336#endif
2337 ++topline;
2338 }
2339 }
2340 if (topline == botline || botline == 0)
2341 curwin->w_cursor.lnum = topline;
2342 else if (topline > botline)
2343 curwin->w_cursor.lnum = botline;
2344 else
2345 {
2346 if (cln < topline && curwin->w_topline > 1)
2347 {
2348 curwin->w_cursor.lnum = topline;
2349 curwin->w_valid &=
2350 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2351 }
2352 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2353 {
2354 curwin->w_cursor.lnum = botline;
2355 curwin->w_valid &=
2356 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2357 }
2358 }
2359 curwin->w_valid |= VALID_TOPLINE;
2360}
2361
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002362static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364/*
2365 * move screen 'count' pages up or down and update screen
2366 *
2367 * return FAIL for failure, OK otherwise
2368 */
2369 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002370onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 long n;
2373 int retval = OK;
2374 lineoff_T loff;
2375 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002376 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar85a20022019-12-21 18:25:54 +01002378 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
2380 beep_flush();
2381 return FAIL;
2382 }
2383
2384 for ( ; count > 0; --count)
2385 {
2386 validate_botline();
2387 /*
2388 * It's an error to move a page up when the first line is already on
2389 * the screen. It's an error to move a page down when the last line
2390 * is on the screen and the topline is 'scrolloff' lines from the
2391 * last line.
2392 */
2393 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002394 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2396 : (curwin->w_topline == 1
2397#ifdef FEAT_DIFF
2398 && curwin->w_topfill ==
2399 diff_check_fill(curwin, curwin->w_topline)
2400#endif
2401 ))
2402 {
2403 beep_flush();
2404 retval = FAIL;
2405 break;
2406 }
2407
2408#ifdef FEAT_DIFF
2409 loff.fill = 0;
2410#endif
2411 if (dir == FORWARD)
2412 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002413 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002415 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002416 if (p_window <= 2)
2417 ++curwin->w_topline;
2418 else
2419 curwin->w_topline += p_window - 2;
2420 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2421 curwin->w_topline = curbuf->b_ml.ml_line_count;
2422 curwin->w_cursor.lnum = curwin->w_topline;
2423 }
2424 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2425 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002426 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 curwin->w_topline = curbuf->b_ml.ml_line_count;
2428#ifdef FEAT_DIFF
2429 curwin->w_topfill = 0;
2430#endif
2431 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2432 }
2433 else
2434 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002435 // For the overlap, start with the line just below the window
2436 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 loff.lnum = curwin->w_botline;
2438#ifdef FEAT_DIFF
2439 loff.fill = diff_check_fill(curwin, loff.lnum)
2440 - curwin->w_filler_rows;
2441#endif
2442 get_scroll_overlap(&loff, -1);
2443 curwin->w_topline = loff.lnum;
2444#ifdef FEAT_DIFF
2445 curwin->w_topfill = loff.fill;
2446 check_topfill(curwin, FALSE);
2447#endif
2448 curwin->w_cursor.lnum = curwin->w_topline;
2449 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2450 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2451 }
2452 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002453 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
2455#ifdef FEAT_DIFF
2456 if (curwin->w_topline == 1)
2457 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002458 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 max_topfill();
2460 continue;
2461 }
2462#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002463 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002464 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002465 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002466 if (p_window <= 2)
2467 --curwin->w_topline;
2468 else
2469 curwin->w_topline -= p_window - 2;
2470 if (curwin->w_topline < 1)
2471 curwin->w_topline = 1;
2472 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2473 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2474 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2475 continue;
2476 }
2477
Bram Moolenaar85a20022019-12-21 18:25:54 +01002478 // Find the line at the top of the window that is going to be the
2479 // line at the bottom of the window. Make sure this results in
2480 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 loff.lnum = curwin->w_topline - 1;
2482#ifdef FEAT_DIFF
2483 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2484 - curwin->w_topfill;
2485#endif
2486 get_scroll_overlap(&loff, 1);
2487
2488 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2489 {
2490 loff.lnum = curbuf->b_ml.ml_line_count;
2491#ifdef FEAT_DIFF
2492 loff.fill = 0;
2493 }
2494 else
2495 {
2496 botline_topline(&loff);
2497#endif
2498 }
2499 curwin->w_cursor.lnum = loff.lnum;
2500
Bram Moolenaar85a20022019-12-21 18:25:54 +01002501 // Find the line just above the new topline to get the right line
2502 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 n = 0;
2504 while (n <= curwin->w_height && loff.lnum >= 1)
2505 {
2506 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002507 if (loff.height == MAXCOL)
2508 n = MAXCOL;
2509 else
2510 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002512 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 curwin->w_topline = 1;
2515#ifdef FEAT_DIFF
2516 max_topfill();
2517#endif
2518 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2519 }
2520 else
2521 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002522 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523#ifdef FEAT_DIFF
2524 topline_botline(&loff);
2525#endif
2526 botline_forw(&loff);
2527 botline_forw(&loff);
2528#ifdef FEAT_DIFF
2529 botline_topline(&loff);
2530#endif
2531#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002532 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2534#endif
2535
Bram Moolenaar85a20022019-12-21 18:25:54 +01002536 // Always scroll at least one line. Avoid getting stuck on
2537 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (loff.lnum >= curwin->w_topline
2539#ifdef FEAT_DIFF
2540 && (loff.lnum > curwin->w_topline
2541 || loff.fill >= curwin->w_topfill)
2542#endif
2543 )
2544 {
2545#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002546 // First try using the maximum number of filler lines. If
2547 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 loff.fill = curwin->w_topfill;
2549 if (curwin->w_topfill < diff_check_fill(curwin,
2550 curwin->w_topline))
2551 max_topfill();
2552 if (curwin->w_topfill == loff.fill)
2553#endif
2554 {
2555 --curwin->w_topline;
2556#ifdef FEAT_DIFF
2557 curwin->w_topfill = 0;
2558#endif
2559 }
2560 comp_botline(curwin);
2561 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002562 curwin->w_valid &=
2563 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
2565 else
2566 {
2567 curwin->w_topline = loff.lnum;
2568#ifdef FEAT_DIFF
2569 curwin->w_topfill = loff.fill;
2570 check_topfill(curwin, FALSE);
2571#endif
2572 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2573 }
2574 }
2575 }
2576 }
2577#ifdef FEAT_FOLDING
2578 foldAdjustCursor();
2579#endif
2580 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002581 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002582 if (retval == OK)
2583 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2585
Bram Moolenaar907dad72018-07-10 15:07:15 +02002586 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002588 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2589 // But make sure we scroll at least one line (happens with mix of long
2590 // wrapping lines and non-wrapping line).
2591 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002593 scroll_cursor_top(1, FALSE);
2594 if (curwin->w_topline <= old_topline
2595 && old_topline < curbuf->b_ml.ml_line_count)
2596 {
2597 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002599 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2600#endif
2601 }
2602 }
2603#ifdef FEAT_FOLDING
2604 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608
2609 redraw_later(VALID);
2610 return retval;
2611}
2612
2613/*
2614 * Decide how much overlap to use for page-up or page-down scrolling.
2615 * This is symmetric, so that doing both keeps the same lines displayed.
2616 * Three lines are examined:
2617 *
2618 * before CTRL-F after CTRL-F / before CTRL-B
2619 * etc. l1
2620 * l1 last but one line ------------
2621 * l2 last text line l2 top text line
2622 * ------------- l3 second text line
2623 * l3 etc.
2624 */
2625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002626get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 int h1, h2, h3, h4;
2629 int min_height = curwin->w_height - 2;
2630 lineoff_T loff0, loff1, loff2;
2631
2632#ifdef FEAT_DIFF
2633 if (lp->fill > 0)
2634 lp->height = 1;
2635 else
2636 lp->height = plines_nofill(lp->lnum);
2637#else
2638 lp->height = plines(lp->lnum);
2639#endif
2640 h1 = lp->height;
2641 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002642 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
2644 loff0 = *lp;
2645 if (dir > 0)
2646 botline_forw(lp);
2647 else
2648 topline_back(lp);
2649 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002650 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002652 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 return;
2654 }
2655
2656 loff1 = *lp;
2657 if (dir > 0)
2658 botline_forw(lp);
2659 else
2660 topline_back(lp);
2661 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002662 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002664 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 return;
2666 }
2667
2668 loff2 = *lp;
2669 if (dir > 0)
2670 botline_forw(lp);
2671 else
2672 topline_back(lp);
2673 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002674 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002675 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002677 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678}
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680/*
2681 * Scroll 'scroll' lines up or down.
2682 */
2683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002684halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
2686 long scrolled = 0;
2687 int i;
2688 int n;
2689 int room;
2690
2691 if (Prenum)
2692 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2693 curwin->w_height : Prenum;
2694 n = (curwin->w_p_scr <= curwin->w_height) ?
2695 curwin->w_p_scr : curwin->w_height;
2696
Bram Moolenaard5d37532017-03-27 23:02:07 +02002697 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 validate_botline();
2699 room = curwin->w_empty_rows;
2700#ifdef FEAT_DIFF
2701 room += curwin->w_filler_rows;
2702#endif
2703 if (flag)
2704 {
2705 /*
2706 * scroll the text up
2707 */
2708 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2709 {
2710#ifdef FEAT_DIFF
2711 if (curwin->w_topfill > 0)
2712 {
2713 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002714 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 --curwin->w_topfill;
2716 }
2717 else
2718#endif
2719 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002720 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 n -= i;
2722 if (n < 0 && scrolled > 0)
2723 break;
2724#ifdef FEAT_FOLDING
2725 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2726#endif
2727 ++curwin->w_topline;
2728#ifdef FEAT_DIFF
2729 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2730#endif
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2733 {
2734 ++curwin->w_cursor.lnum;
2735 curwin->w_valid &=
2736 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
2739 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2740 scrolled += i;
2741
2742 /*
2743 * Correct w_botline for changed w_topline.
2744 * Won't work when there are filler lines.
2745 */
2746#ifdef FEAT_DIFF
2747 if (curwin->w_p_diff)
2748 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2749 else
2750#endif
2751 {
2752 room += i;
2753 do
2754 {
2755 i = plines(curwin->w_botline);
2756 if (i > room)
2757 break;
2758#ifdef FEAT_FOLDING
2759 (void)hasFolding(curwin->w_botline, NULL,
2760 &curwin->w_botline);
2761#endif
2762 ++curwin->w_botline;
2763 room -= i;
2764 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2765 }
2766 }
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 /*
2769 * When hit bottom of the file: move cursor down.
2770 */
2771 if (n > 0)
2772 {
2773# ifdef FEAT_FOLDING
2774 if (hasAnyFolding(curwin))
2775 {
2776 while (--n >= 0
2777 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2778 {
2779 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2780 &curwin->w_cursor.lnum);
2781 ++curwin->w_cursor.lnum;
2782 }
2783 }
2784 else
2785# endif
2786 curwin->w_cursor.lnum += n;
2787 check_cursor_lnum();
2788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 else
2791 {
2792 /*
2793 * scroll the text down
2794 */
2795 while (n > 0 && curwin->w_topline > 1)
2796 {
2797#ifdef FEAT_DIFF
2798 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2799 {
2800 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002801 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 ++curwin->w_topfill;
2803 }
2804 else
2805#endif
2806 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002807 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 n -= i;
2809 if (n < 0 && scrolled > 0)
2810 break;
2811 --curwin->w_topline;
2812#ifdef FEAT_FOLDING
2813 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2814#endif
2815#ifdef FEAT_DIFF
2816 curwin->w_topfill = 0;
2817#endif
2818 }
2819 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2820 VALID_BOTLINE|VALID_BOTLINE_AP);
2821 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (curwin->w_cursor.lnum > 1)
2823 {
2824 --curwin->w_cursor.lnum;
2825 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002828
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 /*
2830 * When hit top of the file: move cursor up.
2831 */
2832 if (n > 0)
2833 {
2834 if (curwin->w_cursor.lnum <= (linenr_T)n)
2835 curwin->w_cursor.lnum = 1;
2836 else
2837# ifdef FEAT_FOLDING
2838 if (hasAnyFolding(curwin))
2839 {
2840 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2841 {
2842 --curwin->w_cursor.lnum;
2843 (void)hasFolding(curwin->w_cursor.lnum,
2844 &curwin->w_cursor.lnum, NULL);
2845 }
2846 }
2847 else
2848# endif
2849 curwin->w_cursor.lnum -= n;
2850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002853 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 foldAdjustCursor();
2855# endif
2856#ifdef FEAT_DIFF
2857 check_topfill(curwin, !flag);
2858#endif
2859 cursor_correct();
2860 beginline(BL_SOL | BL_FIX);
2861 redraw_later(VALID);
2862}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002863
Bram Moolenaar860cae12010-06-05 23:22:07 +02002864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002865do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002866{
2867 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002868 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002869 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002870 colnr_T curswant = curwin->w_curswant;
2871 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002872 win_T *old_curwin = curwin;
2873 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002874 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002875 int old_VIsual_select = VIsual_select;
2876 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002877
2878 /*
2879 * loop through the cursorbound windows
2880 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002881 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002882 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002883 {
2884 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002885 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002886 if (curwin != old_curwin && curwin->w_p_crb)
2887 {
2888# ifdef FEAT_DIFF
2889 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002890 curwin->w_cursor.lnum =
2891 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002892 else
2893# endif
2894 curwin->w_cursor.lnum = line;
2895 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002896 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002897 curwin->w_curswant = curswant;
2898 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002899
Bram Moolenaar85a20022019-12-21 18:25:54 +01002900 // Make sure the cursor is in a valid position. Temporarily set
2901 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002902 restart_edit_save = restart_edit;
2903 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002904 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002905# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002906 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002907 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002908# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002909 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002910 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002911 if (has_mbyte)
2912 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002913 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002914
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002916 if (!curwin->w_p_scb)
2917 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002918 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002919 }
2920 }
2921
2922 /*
2923 * reset current-window
2924 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002925 VIsual_select = old_VIsual_select;
2926 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002927 curwin = old_curwin;
2928 curbuf = old_curbuf;
2929}