blob: cd0af3e64f477b17ee5abc6a4a4e6211d7f110fa [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
Bram Moolenaar90a99792018-09-12 21:52:18 +0200118#ifdef FEAT_SYN_HL
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200119 void
120reset_cursorline(void)
121{
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200122 curwin->w_last_cursorline = 0;
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200123}
Bram Moolenaar90a99792018-09-12 21:52:18 +0200124#endif
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100127 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
128 * set.
129 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100131redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100132{
133 if ((wp->w_p_rnu
134#ifdef FEAT_SYN_HL
135 || wp->w_p_cul
136#endif
137 )
138 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200139 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200140 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200141 if (wp->w_p_rnu)
142 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200143 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200144#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200145 if (wp->w_p_cul)
146 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200147 if (wp->w_redr_type <= VALID && wp->w_last_cursorline != 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200148 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200149 // "w_last_cursorline" may be outdated, worst case we redraw
150 // too much. This is optimized for moving the cursor around in
151 // the current window.
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100152 redrawWinline(wp, wp->w_last_cursorline);
153 redrawWinline(wp, wp->w_cursor.lnum);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200154 }
155 else
156 redraw_win_later(wp, SOME_VALID);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200157 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200158#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200159 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100160}
161
162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 * Update curwin->w_topline and redraw if necessary.
164 * Used to update the screen before printing a message.
165 */
166 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100167update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 update_topline();
170 if (must_redraw)
171 update_screen(0);
172}
173
174/*
175 * Update curwin->w_topline to move the cursor onto the screen.
176 */
177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100178update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
180 long line_count;
181 int halfheight;
182 int n;
183 linenr_T old_topline;
184#ifdef FEAT_DIFF
185 int old_topfill;
186#endif
187#ifdef FEAT_FOLDING
188 linenr_T lnum;
189#endif
190 int check_topline = FALSE;
191 int check_botline = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100192 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100193 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaar85a20022019-12-21 18:25:54 +0100195 // If there is no valid screen and when the window height is zero just use
196 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200197 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200198 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100199 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200200 curwin->w_topline = curwin->w_cursor.lnum;
201 curwin->w_botline = curwin->w_topline;
202 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200203 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200204 return;
205 }
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 check_cursor_moved(curwin);
208 if (curwin->w_valid & VALID_TOPLINE)
209 return;
210
Bram Moolenaar85a20022019-12-21 18:25:54 +0100211 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000212 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100213 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215 old_topline = curwin->w_topline;
216#ifdef FEAT_DIFF
217 old_topfill = curwin->w_topfill;
218#endif
219
220 /*
221 * If the buffer is empty, always set topline to 1.
222 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100223 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 {
225 if (curwin->w_topline != 1)
226 redraw_later(NOT_VALID);
227 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 curwin->w_botline = 2;
229 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
232
233 /*
234 * If the cursor is above or near the top of the window, scroll the window
235 * to show the line the cursor is in, with 'scrolloff' context.
236 */
237 else
238 {
239 if (curwin->w_topline > 1)
240 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100241 // If the cursor is above topline, scrolling is always needed.
242 // If the cursor is far below topline and there is no folding,
243 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 if (curwin->w_cursor.lnum < curwin->w_topline)
245 check_topline = TRUE;
246 else if (check_top_offset())
247 check_topline = TRUE;
248 }
249#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100250 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
252 curwin->w_topline))
253 check_topline = TRUE;
254#endif
255
256 if (check_topline)
257 {
258 halfheight = curwin->w_height / 2 - 1;
259 if (halfheight < 2)
260 halfheight = 2;
261
262#ifdef FEAT_FOLDING
263 if (hasAnyFolding(curwin))
264 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100265 // Count the number of logical lines between the cursor and
266 // topline + scrolloff (approximation of how much will be
267 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 n = 0;
269 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100270 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
272 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100273 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
275 break;
276 (void)hasFolding(lnum, NULL, &lnum);
277 }
278 }
279 else
280#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100281 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
Bram Moolenaar85a20022019-12-21 18:25:54 +0100283 // If we weren't very close to begin with, we scroll to put the
284 // cursor in the middle of the window. Otherwise put the cursor
285 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (n >= halfheight)
287 scroll_cursor_halfway(FALSE);
288 else
289 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000290 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 check_botline = TRUE;
292 }
293 }
294
295 else
296 {
297#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100298 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
300#endif
301 check_botline = TRUE;
302 }
303 }
304
305 /*
306 * If the cursor is below the bottom of the window, scroll the window
307 * to put the cursor on the window.
308 * When w_botline is invalid, recompute it first, to avoid a redraw later.
309 * If w_botline was approximated, we might need a redraw later in a few
310 * cases, but we don't want to spend (a lot of) time recomputing w_botline
311 * for every small change.
312 */
313 if (check_botline)
314 {
315 if (!(curwin->w_valid & VALID_BOTLINE_AP))
316 validate_botline();
317
318 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
319 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000320 if (curwin->w_cursor.lnum < curwin->w_botline)
321 {
322 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100323 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifdef FEAT_FOLDING
325 || hasAnyFolding(curwin)
326#endif
327 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 lineoff_T loff;
330
Bram Moolenaar85a20022019-12-21 18:25:54 +0100331 // Cursor is (a few lines) above botline, check if there are
332 // 'scrolloff' window lines below the cursor. If not, need to
333 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 n = curwin->w_empty_rows;
335 loff.lnum = curwin->w_cursor.lnum;
336#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100337 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
339#endif
340#ifdef FEAT_DIFF
341 loff.fill = 0;
342 n += curwin->w_filler_rows;
343#endif
344 loff.height = 0;
345 while (loff.lnum < curwin->w_botline
346#ifdef FEAT_DIFF
347 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
348#endif
349 )
350 {
351 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100352 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 break;
354 botline_forw(&loff);
355 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100356 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000359 }
360 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100361 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000362 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 }
364 if (check_botline)
365 {
366#ifdef FEAT_FOLDING
367 if (hasAnyFolding(curwin))
368 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100369 // Count the number of logical lines between the cursor and
370 // botline - scrolloff (approximation of how much will be
371 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 line_count = 0;
373 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100374 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {
376 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 if (lnum <= 0 || line_count > curwin->w_height + 1)
379 break;
380 (void)hasFolding(lnum, &lnum, NULL);
381 }
382 }
383 else
384#endif
385 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100386 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000388 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 else
390 scroll_cursor_halfway(FALSE);
391 }
392 }
393 }
394 curwin->w_valid |= VALID_TOPLINE;
395
396 /*
397 * Need to redraw when topline changed.
398 */
399 if (curwin->w_topline != old_topline
400#ifdef FEAT_DIFF
401 || curwin->w_topfill != old_topfill
402#endif
403 )
404 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100405 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000406 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {
408 curwin->w_skipcol = 0;
409 redraw_later(NOT_VALID);
410 }
411 else
412 redraw_later(VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100413 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (curwin->w_cursor.lnum == curwin->w_topline)
415 validate_cursor();
416 }
417
Bram Moolenaar375e3392019-01-31 18:26:10 +0100418 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419}
420
421/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000422 * Return the scrolljump value to use for the current window.
423 * When 'scrolljump' is positive use it as-is.
424 * When 'scrolljump' is negative use it as a percentage of the window height.
425 */
426 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100427scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000428{
429 if (p_sj >= 0)
430 return (int)p_sj;
431 return (curwin->w_height * -p_sj) / 100;
432}
433
434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
436 * current window.
437 */
438 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100439check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
441 lineoff_T loff;
442 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100443 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
Bram Moolenaar375e3392019-01-31 18:26:10 +0100445 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446#ifdef FEAT_FOLDING
447 || hasAnyFolding(curwin)
448#endif
449 )
450 {
451 loff.lnum = curwin->w_cursor.lnum;
452#ifdef FEAT_DIFF
453 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100454 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455#else
456 n = 0;
457#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100458 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100459 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 {
461 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100462 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 if (loff.lnum < curwin->w_topline
464#ifdef FEAT_DIFF
465 || (loff.lnum == curwin->w_topline && loff.fill > 0)
466#endif
467 )
468 break;
469 n += loff.height;
470 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100471 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 return TRUE;
473 }
474 return FALSE;
475}
476
477 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100478update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 if (curwin->w_set_curswant)
481 {
482 validate_virtcol();
483 curwin->w_curswant = curwin->w_virtcol;
484 curwin->w_set_curswant = FALSE;
485 }
486}
487
488/*
489 * Check if the cursor has moved. Set the w_valid flag accordingly.
490 */
491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
495 {
496 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
497 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
498 wp->w_valid_cursor = wp->w_cursor;
499 wp->w_valid_leftcol = wp->w_leftcol;
500 }
501 else if (wp->w_cursor.col != wp->w_valid_cursor.col
502 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100503 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 {
505 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
506 wp->w_valid_cursor.col = wp->w_cursor.col;
507 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 }
510}
511
512/*
513 * Call this function when some window settings have changed, which require
514 * the cursor position, botline and topline to be recomputed and the window to
515 * be redrawn. E.g, when changing the 'wrap' option or folding.
516 */
517 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100518changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 changed_window_setting_win(curwin);
521}
522
523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100524changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 wp->w_lines_valid = 0;
527 changed_line_abv_curs_win(wp);
528 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
529 redraw_win_later(wp, NOT_VALID);
530}
531
532/*
533 * Set wp->w_topline to a certain number.
534 */
535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100539 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
541#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100542 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 wp->w_botline += lnum - wp->w_topline;
544 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000545 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546#ifdef FEAT_DIFF
547 wp->w_topfill = 0;
548#endif
549 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100550 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 redraw_later(VALID);
552}
553
554/*
555 * Call this function when the length of the cursor line (in screen
556 * characters) has changed, and the change is before the cursor.
557 * Need to take care of w_botline separately!
558 */
559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100560changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
562 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
563 |VALID_CHEIGHT|VALID_TOPLINE);
564}
565
566 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100567changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
570 |VALID_CHEIGHT|VALID_TOPLINE);
571}
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573/*
574 * Call this function when the length of a line (in screen characters) above
575 * the cursor have changed.
576 * Need to take care of w_botline separately!
577 */
578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100579changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
581 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
582 |VALID_CHEIGHT|VALID_TOPLINE);
583}
584
585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100586changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
589 |VALID_CHEIGHT|VALID_TOPLINE);
590}
591
592/*
593 * Make sure the value of curwin->w_botline is valid.
594 */
595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100596validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 if (!(curwin->w_valid & VALID_BOTLINE))
599 comp_botline(curwin);
600}
601
602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * Mark curwin->w_botline as invalid (because of some change in the buffer).
604 */
605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100606invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
609}
610
611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100612invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
614 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
615}
616
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100618approximate_botline_win(
619 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 wp->w_valid &= ~VALID_BOTLINE;
622}
623
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624/*
625 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
626 */
627 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100628cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 check_cursor_moved(curwin);
631 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
632 (VALID_WROW|VALID_WCOL));
633}
634
635/*
636 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
637 * w_topline must be valid, you may need to call update_topline() first!
638 */
639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 check_cursor_moved(curwin);
643 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
644 curs_columns(TRUE);
645}
646
647#if defined(FEAT_GUI) || defined(PROTO)
648/*
649 * validate w_cline_row.
650 */
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 /*
655 * First make sure that w_topline is valid (after moving the cursor).
656 */
657 update_topline();
658 check_cursor_moved(curwin);
659 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100660 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661}
662#endif
663
664/*
665 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200666 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 */
668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100669curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 linenr_T lnum;
672 int i;
673 int all_invalid;
674 int valid;
675#ifdef FEAT_FOLDING
676 long fold_count;
677#endif
678
Bram Moolenaar85a20022019-12-21 18:25:54 +0100679 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 all_invalid = (!redrawing()
681 || wp->w_lines_valid == 0
682 || wp->w_lines[0].wl_lnum > wp->w_topline);
683 i = 0;
684 wp->w_cline_row = 0;
685 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
686 {
687 valid = FALSE;
688 if (!all_invalid && i < wp->w_lines_valid)
689 {
690 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100691 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 if (wp->w_lines[i].wl_lnum == lnum)
693 {
694#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100695 // Check for newly inserted lines below this row, in which
696 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 if (!wp->w_buffer->b_mod_set
698 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
699 || wp->w_buffer->b_mod_top
700 > wp->w_lines[i].wl_lastlnum + 1)
701#endif
702 valid = TRUE;
703 }
704 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100705 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 if (valid
708#ifdef FEAT_DIFF
709 && (lnum != wp->w_topline || !wp->w_p_diff)
710#endif
711 )
712 {
713#ifdef FEAT_FOLDING
714 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100715 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (lnum > wp->w_cursor.lnum)
717 break;
718#else
719 ++lnum;
720#endif
721 wp->w_cline_row += wp->w_lines[i].wl_size;
722 }
723 else
724 {
725#ifdef FEAT_FOLDING
726 fold_count = foldedCount(wp, lnum, NULL);
727 if (fold_count)
728 {
729 lnum += fold_count;
730 if (lnum > wp->w_cursor.lnum)
731 break;
732 ++wp->w_cline_row;
733 }
734 else
735#endif
736#ifdef FEAT_DIFF
737 if (lnum == wp->w_topline)
738 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
739 + wp->w_topfill;
740 else
741#endif
742 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
743 }
744 }
745
746 check_cursor_moved(wp);
747 if (!(wp->w_valid & VALID_CHEIGHT))
748 {
749 if (all_invalid
750 || i == wp->w_lines_valid
751 || (i < wp->w_lines_valid
752 && (!wp->w_lines[i].wl_valid
753 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
754 {
755#ifdef FEAT_DIFF
756 if (wp->w_cursor.lnum == wp->w_topline)
757 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
758 TRUE) + wp->w_topfill;
759 else
760#endif
761 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
762#ifdef FEAT_FOLDING
763 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
764 NULL, NULL, TRUE, NULL);
765#endif
766 }
767 else if (i > wp->w_lines_valid)
768 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100769 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 wp->w_cline_height = 0;
771#ifdef FEAT_FOLDING
772 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
773 NULL, NULL, TRUE, NULL);
774#endif
775 }
776 else
777 {
778 wp->w_cline_height = wp->w_lines[i].wl_size;
779#ifdef FEAT_FOLDING
780 wp->w_cline_folded = wp->w_lines[i].wl_folded;
781#endif
782 }
783 }
784
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100785 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
790/*
791 * Validate curwin->w_virtcol only.
792 */
793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100794validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 validate_virtcol_win(curwin);
797}
798
799/*
800 * Validate wp->w_virtcol only.
801 */
802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 check_cursor_moved(wp);
806 if (!(wp->w_valid & VALID_VIRTCOL))
807 {
808 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
809 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000810#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200811 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000812 redraw_win_later(wp, SOME_VALID);
813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
815}
816
817/*
818 * Validate curwin->w_cline_height only.
819 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 check_cursor_moved(curwin);
824 if (!(curwin->w_valid & VALID_CHEIGHT))
825 {
826#ifdef FEAT_DIFF
827 if (curwin->w_cursor.lnum == curwin->w_topline)
828 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
829 + curwin->w_topfill;
830 else
831#endif
832 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
833#ifdef FEAT_FOLDING
834 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
835#endif
836 curwin->w_valid |= VALID_CHEIGHT;
837 }
838}
839
840/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000841 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 */
843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100844validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 colnr_T off;
847 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100848 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850 validate_virtcol();
851 if (!(curwin->w_valid & VALID_WCOL))
852 {
853 col = curwin->w_virtcol;
854 off = curwin_col_off();
855 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200856 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
Bram Moolenaar85a20022019-12-21 18:25:54 +0100858 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000859 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200860 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100861 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100862 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200863 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000864 if (col > (int)curwin->w_leftcol)
865 col -= curwin->w_leftcol;
866 else
867 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000869
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 curwin->w_valid |= VALID_WCOL;
871 }
872}
873
874/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200875 * Compute offset of a window, occupied by absolute or relative line number,
876 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 */
878 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100879win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
Bram Moolenaar64486672010-05-16 15:46:46 +0200881 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#ifdef FEAT_CMDWIN
883 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
884#endif
885#ifdef FEAT_FOLDING
886 + wp->w_p_fdc
887#endif
888#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200889 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890#endif
891 );
892}
893
894 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100895curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897 return win_col_off(curwin);
898}
899
900/*
901 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200902 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
903 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 */
905 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100906win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
Bram Moolenaar64486672010-05-16 15:46:46 +0200908 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000909 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 return 0;
911}
912
913 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100914curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 return win_col_off2(curwin);
917}
918
919/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100920 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 * Also updates curwin->w_wrow and curwin->w_cline_row.
922 * Also updates curwin->w_leftcol.
923 */
924 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100925curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100926 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100929 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 int off_left, off_right;
931 int n;
932 int p_lines;
933 int width = 0;
934 int textwidth;
935 int new_leftcol;
936 colnr_T startcol;
937 colnr_T endcol;
938 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100939 long so = get_scrolloff_value();
940 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942 /*
943 * First make sure that w_topline is valid (after moving the cursor).
944 */
945 update_topline();
946
947 /*
948 * Next make sure that w_cline_row is valid.
949 */
950 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100951 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
953 /*
954 * Compute the number of virtual columns.
955 */
956#ifdef FEAT_FOLDING
957 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100958 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
960 else
961#endif
962 getvvcol(curwin, &curwin->w_cursor,
963 &startcol, &(curwin->w_virtcol), &endcol);
964
Bram Moolenaar85a20022019-12-21 18:25:54 +0100965 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100967 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 extra = curwin_col_off();
970 curwin->w_wcol = curwin->w_virtcol + extra;
971 endcol += extra;
972
973 /*
974 * Now compute w_wrow, counting screen lines from w_cline_row.
975 */
976 curwin->w_wrow = curwin->w_cline_row;
977
Bram Moolenaar02631462017-09-22 15:20:32 +0200978 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 if (textwidth <= 0)
980 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100981 // No room for text, put cursor in last char of window.
Bram Moolenaar02631462017-09-22 15:20:32 +0200982 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 curwin->w_wrow = curwin->w_height - 1;
984 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200985 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
987 width = textwidth + curwin_col_off2();
988
Bram Moolenaar85a20022019-12-21 18:25:54 +0100989 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +0200990 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100992#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +0100993 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100994#endif
Bram Moolenaaree857022019-11-09 23:26:40 +0100995
Bram Moolenaar85a20022019-12-21 18:25:54 +0100996 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +0200997 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 curwin->w_wcol -= n * width;
999 curwin->w_wrow += n;
1000
1001#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001002 // When cursor wraps to first char of next line in Insert
1003 // mode, the 'showbreak' string isn't shown, backup to first
1004 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001005 sbr = get_showbreak_value(curwin);
1006 if (*sbr && *ml_get_cursor() == NUL
1007 && curwin->w_wcol == (int)vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 curwin->w_wcol = 0;
1009#endif
1010 }
1011 }
1012
Bram Moolenaar85a20022019-12-21 18:25:54 +01001013 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1014 // is not folded.
1015 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001016 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#ifdef FEAT_FOLDING
1018 && !curwin->w_cline_folded
1019#endif
1020 )
1021 {
1022 /*
1023 * If Cursor is left of the screen, scroll rightwards.
1024 * If Cursor is right of the screen, scroll leftwards
1025 * If we get closer to the edge than 'sidescrolloff', scroll a little
1026 * extra
1027 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001028 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001029 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001030 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (off_left < 0 || off_right > 0)
1032 {
1033 if (off_left < 0)
1034 diff = -off_left;
1035 else
1036 diff = off_right;
1037
Bram Moolenaar85a20022019-12-21 18:25:54 +01001038 // When far off or not enough room on either side, put cursor in
1039 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1041 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1042 else
1043 {
1044 if (diff < p_ss)
1045 diff = p_ss;
1046 if (off_left < 0)
1047 new_leftcol = curwin->w_leftcol - diff;
1048 else
1049 new_leftcol = curwin->w_leftcol + diff;
1050 }
1051 if (new_leftcol < 0)
1052 new_leftcol = 0;
1053 if (new_leftcol != (int)curwin->w_leftcol)
1054 {
1055 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 redraw_later(NOT_VALID);
1058 }
1059 }
1060 curwin->w_wcol -= curwin->w_leftcol;
1061 }
1062 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1063 curwin->w_wcol -= curwin->w_leftcol;
1064 else
1065 curwin->w_wcol = 0;
1066
1067#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001068 // Skip over filler lines. At the top use w_topfill, there
1069 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (curwin->w_cursor.lnum == curwin->w_topline)
1071 curwin->w_wrow += curwin->w_topfill;
1072 else
1073 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1074#endif
1075
1076 prev_skipcol = curwin->w_skipcol;
1077
1078 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if ((curwin->w_wrow >= curwin->w_height
1081 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001082 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 && (p_lines =
1084#ifdef FEAT_DIFF
1085 plines_win_nofill
1086#else
1087 plines_win
1088#endif
1089 (curwin, curwin->w_cursor.lnum, FALSE))
1090 - 1 >= curwin->w_height))
1091 && curwin->w_height != 0
1092 && curwin->w_cursor.lnum == curwin->w_topline
1093 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001094 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001096 // Cursor past end of screen. Happens with a single line that does
1097 // not fit on screen. Find a skipcol to show the text around the
1098 // cursor. Avoid scrolling all the time. compute value of "extra":
1099 // 1: Less than 'scrolloff' lines above
1100 // 2: Less than 'scrolloff' lines below
1101 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001103 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001105 // Compute last display line of the buffer line that we want at the
1106 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (p_lines == 0)
1108 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1109 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001110 if (p_lines > curwin->w_wrow + so)
1111 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 else
1113 n = p_lines;
1114 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1115 extra += 2;
1116
Bram Moolenaar375e3392019-01-31 18:26:10 +01001117 if (extra == 3 || p_lines < so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001119 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 n = curwin->w_virtcol / width;
1121 if (n > curwin->w_height / 2)
1122 n -= curwin->w_height / 2;
1123 else
1124 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001125 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (n > p_lines - curwin->w_height + 1)
1127 n = p_lines - curwin->w_height + 1;
1128 curwin->w_skipcol = n * width;
1129 }
1130 else if (extra == 1)
1131 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001132 // less then 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001133 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 + width - 1) / width;
1135 if (extra > 0)
1136 {
1137 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1138 extra = curwin->w_skipcol / width;
1139 curwin->w_skipcol -= extra * width;
1140 }
1141 }
1142 else if (extra == 2)
1143 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001144 // less then 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 endcol = (n - curwin->w_height + 1) * width;
1146 while (endcol > curwin->w_virtcol)
1147 endcol -= width;
1148 if (endcol > curwin->w_skipcol)
1149 curwin->w_skipcol = endcol;
1150 }
1151
1152 curwin->w_wrow -= curwin->w_skipcol / width;
1153 if (curwin->w_wrow >= curwin->w_height)
1154 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001155 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 extra = curwin->w_wrow - curwin->w_height + 1;
1157 curwin->w_skipcol += extra * width;
1158 curwin->w_wrow -= extra;
1159 }
1160
1161 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1162 if (extra > 0)
1163 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1164 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001165 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 else
1168 curwin->w_skipcol = 0;
1169 if (prev_skipcol != curwin->w_skipcol)
1170 redraw_later(NOT_VALID);
1171
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001172#ifdef FEAT_SYN_HL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 // Redraw when w_virtcol changes and 'cursorcolumn' is set
Bram Moolenaarb6798752014-03-27 12:11:48 +01001174 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001175 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001176 redraw_later(SOME_VALID);
1177#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001178#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1179 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1180 {
1181 curwin->w_wrow += popup_top_extra(curwin);
1182 curwin->w_wcol += popup_left_extra(curwin);
1183 }
1184#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001185
Bram Moolenaar08f23632019-11-16 14:19:33 +01001186 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1187 curwin->w_valid_leftcol = curwin->w_leftcol;
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1190}
1191
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001192#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001193/*
1194 * Compute the screen position of text character at "pos" in window "wp"
1195 * The resulting values are one-based, zero when character is not visible.
1196 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001197 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001198textpos2screenpos(
1199 win_T *wp,
1200 pos_T *pos,
1201 int *rowp, // screen row
1202 int *scolp, // start screen column
1203 int *ccolp, // cursor screen column
1204 int *ecolp) // end screen column
1205{
1206 colnr_T scol = 0, ccol = 0, ecol = 0;
1207 int row = 0;
1208 int rowoff = 0;
1209 colnr_T coloff = 0;
1210
1211 if (pos->lnum >= wp->w_topline && pos->lnum < wp->w_botline)
1212 {
1213 colnr_T off;
1214 colnr_T col;
1215 int width;
1216
1217 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1218 getvcol(wp, pos, &scol, &ccol, &ecol);
1219
1220 // similar to what is done in validate_cursor_col()
1221 col = scol;
1222 off = win_col_off(wp);
1223 col += off;
1224 width = wp->w_width - off + win_col_off2(wp);
1225
Bram Moolenaar12034e22019-08-25 22:25:02 +02001226 // long line wrapping, adjust row
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001227 if (wp->w_p_wrap
1228 && col >= (colnr_T)wp->w_width
1229 && width > 0)
1230 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001231 // use same formula as what is used in curs_columns()
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001232 rowoff = ((col - wp->w_width) / width + 1);
1233 col -= rowoff * width;
1234 }
1235 col -= wp->w_leftcol;
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +01001236 if (col >= wp->w_width)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001237 col = -1;
1238 if (col >= 0)
1239 coloff = col - scol + wp->w_wincol + 1;
1240 else
1241 // character is left or right of the window
1242 row = scol = ccol = ecol = 0;
1243 }
1244 *rowp = wp->w_winrow + row + rowoff;
1245 *scolp = scol + coloff;
1246 *ccolp = ccol + coloff;
1247 *ecolp = ecol + coloff;
1248}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001249#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001250
Bram Moolenaar12034e22019-08-25 22:25:02 +02001251#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001252/*
1253 * "screenpos({winid}, {lnum}, {col})" function
1254 */
1255 void
1256f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1257{
1258 dict_T *dict;
1259 win_T *wp;
1260 pos_T pos;
1261 int row = 0;
1262 int scol = 0, ccol = 0, ecol = 0;
1263
1264 if (rettv_dict_alloc(rettv) != OK)
1265 return;
1266 dict = rettv->vval.v_dict;
1267
1268 wp = find_win_by_nr_or_id(&argvars[0]);
1269 if (wp == NULL)
1270 return;
1271
1272 pos.lnum = tv_get_number(&argvars[1]);
1273 pos.col = tv_get_number(&argvars[2]) - 1;
1274 pos.coladd = 0;
1275 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1276
1277 dict_add_number(dict, "row", row);
1278 dict_add_number(dict, "col", scol);
1279 dict_add_number(dict, "curscol", ccol);
1280 dict_add_number(dict, "endcol", ecol);
1281}
1282#endif
1283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284/*
1285 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001288scrolldown(
1289 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001290 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001292 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 int wrow;
1294 int moved = FALSE;
1295
1296#ifdef FEAT_FOLDING
1297 linenr_T first;
1298
Bram Moolenaar85a20022019-12-21 18:25:54 +01001299 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1301#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001302 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 while (line_count-- > 0)
1304 {
1305#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001306 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1307 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
1309 ++curwin->w_topfill;
1310 ++done;
1311 }
1312 else
1313#endif
1314 {
1315 if (curwin->w_topline == 1)
1316 break;
1317 --curwin->w_topline;
1318#ifdef FEAT_DIFF
1319 curwin->w_topfill = 0;
1320#endif
1321#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001322 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (hasFolding(curwin->w_topline, &first, NULL))
1324 {
1325 ++done;
1326 if (!byfold)
1327 line_count -= curwin->w_topline - first - 1;
1328 curwin->w_botline -= curwin->w_topline - first;
1329 curwin->w_topline = first;
1330 }
1331 else
1332#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001333 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001335 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 invalidate_botline();
1337 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 curwin->w_wrow += done; // keep w_wrow updated
1339 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341#ifdef FEAT_DIFF
1342 if (curwin->w_cursor.lnum == curwin->w_topline)
1343 curwin->w_cline_row = 0;
1344 check_topfill(curwin, TRUE);
1345#endif
1346
1347 /*
1348 * Compute the row number of the last row of the cursor line
1349 * and move the cursor onto the displayed part of the window.
1350 */
1351 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001352 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 {
1354 validate_virtcol();
1355 validate_cheight();
1356 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001357 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1360 {
1361#ifdef FEAT_FOLDING
1362 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1363 {
1364 --wrow;
1365 if (first == 1)
1366 curwin->w_cursor.lnum = 1;
1367 else
1368 curwin->w_cursor.lnum = first - 1;
1369 }
1370 else
1371#endif
1372 wrow -= plines(curwin->w_cursor.lnum--);
1373 curwin->w_valid &=
1374 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1375 moved = TRUE;
1376 }
1377 if (moved)
1378 {
1379#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001380 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 foldAdjustCursor();
1382#endif
1383 coladvance(curwin->w_curswant);
1384 }
1385}
1386
1387/*
1388 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001391scrollup(
1392 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001393 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1396 linenr_T lnum;
1397
1398 if (
1399# ifdef FEAT_FOLDING
1400 (byfold && hasAnyFolding(curwin))
1401# ifdef FEAT_DIFF
1402 ||
1403# endif
1404# endif
1405# ifdef FEAT_DIFF
1406 curwin->w_p_diff
1407# endif
1408 )
1409 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001410 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 lnum = curwin->w_topline;
1412 while (line_count--)
1413 {
1414# ifdef FEAT_DIFF
1415 if (curwin->w_topfill > 0)
1416 --curwin->w_topfill;
1417 else
1418# endif
1419 {
1420# ifdef FEAT_FOLDING
1421 if (byfold)
1422 (void)hasFolding(lnum, NULL, &lnum);
1423# endif
1424 if (lnum >= curbuf->b_ml.ml_line_count)
1425 break;
1426 ++lnum;
1427# ifdef FEAT_DIFF
1428 curwin->w_topfill = diff_check_fill(curwin, lnum);
1429# endif
1430 }
1431 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001432 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 curwin->w_botline += lnum - curwin->w_topline;
1434 curwin->w_topline = lnum;
1435 }
1436 else
1437#endif
1438 {
1439 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001440 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442
1443 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1444 curwin->w_topline = curbuf->b_ml.ml_line_count;
1445 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1446 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1447
1448#ifdef FEAT_DIFF
1449 check_topfill(curwin, FALSE);
1450#endif
1451
1452#ifdef FEAT_FOLDING
1453 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001454 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1456#endif
1457
1458 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1459 if (curwin->w_cursor.lnum < curwin->w_topline)
1460 {
1461 curwin->w_cursor.lnum = curwin->w_topline;
1462 curwin->w_valid &=
1463 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1464 coladvance(curwin->w_curswant);
1465 }
1466}
1467
1468#ifdef FEAT_DIFF
1469/*
1470 * Don't end up with too many filler lines in the window.
1471 */
1472 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001473check_topfill(
1474 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001475 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 int n;
1478
1479 if (wp->w_topfill > 0)
1480 {
1481 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1482 if (wp->w_topfill + n > wp->w_height)
1483 {
1484 if (down && wp->w_topline > 1)
1485 {
1486 --wp->w_topline;
1487 wp->w_topfill = 0;
1488 }
1489 else
1490 {
1491 wp->w_topfill = wp->w_height - n;
1492 if (wp->w_topfill < 0)
1493 wp->w_topfill = 0;
1494 }
1495 }
1496 }
1497}
1498
1499/*
1500 * Use as many filler lines as possible for w_topline. Make sure w_topline
1501 * is still visible.
1502 */
1503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001504max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 int n;
1507
1508 n = plines_nofill(curwin->w_topline);
1509 if (n >= curwin->w_height)
1510 curwin->w_topfill = 0;
1511 else
1512 {
1513 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1514 if (curwin->w_topfill + n > curwin->w_height)
1515 curwin->w_topfill = curwin->w_height - n;
1516 }
1517}
1518#endif
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520/*
1521 * Scroll the screen one line down, but don't do it if it would move the
1522 * cursor off the screen.
1523 */
1524 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001525scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 int end_row;
1528#ifdef FEAT_DIFF
1529 int can_fill = (curwin->w_topfill
1530 < diff_check_fill(curwin, curwin->w_topline));
1531#endif
1532
1533 if (curwin->w_topline <= 1
1534#ifdef FEAT_DIFF
1535 && !can_fill
1536#endif
1537 )
1538 return;
1539
Bram Moolenaar85a20022019-12-21 18:25:54 +01001540 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
1542 /*
1543 * Compute the row number of the last row of the cursor line
1544 * and make sure it doesn't go off the screen. Make sure the cursor
1545 * doesn't go past 'scrolloff' lines from the screen end.
1546 */
1547 end_row = curwin->w_wrow;
1548#ifdef FEAT_DIFF
1549 if (can_fill)
1550 ++end_row;
1551 else
1552 end_row += plines_nofill(curwin->w_topline - 1);
1553#else
1554 end_row += plines(curwin->w_topline - 1);
1555#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001556 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 {
1558 validate_cheight();
1559 validate_virtcol();
1560 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001561 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001563 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
1565#ifdef FEAT_DIFF
1566 if (can_fill)
1567 {
1568 ++curwin->w_topfill;
1569 check_topfill(curwin, TRUE);
1570 }
1571 else
1572 {
1573 --curwin->w_topline;
1574 curwin->w_topfill = 0;
1575 }
1576#else
1577 --curwin->w_topline;
1578#endif
1579#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001580 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001582 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1584 }
1585}
1586
1587/*
1588 * Scroll the screen one line up, but don't do it if it would move the cursor
1589 * off the screen.
1590 */
1591 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001592scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593{
1594 int start_row;
1595
1596 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1597#ifdef FEAT_DIFF
1598 && curwin->w_topfill == 0
1599#endif
1600 )
1601 return;
1602
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
1605 /*
1606 * Compute the row number of the first row of the cursor line
1607 * and make sure it doesn't go off the screen. Make sure the cursor
1608 * doesn't go before 'scrolloff' lines from the screen start.
1609 */
1610#ifdef FEAT_DIFF
1611 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1612 - curwin->w_topfill;
1613#else
1614 start_row = curwin->w_wrow - plines(curwin->w_topline);
1615#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001616 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {
1618 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001619 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001621 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623#ifdef FEAT_DIFF
1624 if (curwin->w_topfill > 0)
1625 --curwin->w_topfill;
1626 else
1627#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001628 {
1629#ifdef FEAT_FOLDING
1630 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001633 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001634 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1636 }
1637}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639/*
1640 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1641 * a (wrapped) text line. Uses and sets "lp->fill".
1642 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001643 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 */
1645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001646topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648#ifdef FEAT_DIFF
1649 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1650 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001651 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 ++lp->fill;
1653 lp->height = 1;
1654 }
1655 else
1656#endif
1657 {
1658 --lp->lnum;
1659#ifdef FEAT_DIFF
1660 lp->fill = 0;
1661#endif
1662 if (lp->lnum < 1)
1663 lp->height = MAXCOL;
1664 else
1665#ifdef FEAT_FOLDING
1666 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001667 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 lp->height = 1;
1669 else
1670#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001671 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673}
1674
1675/*
1676 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1677 * a (wrapped) text line. Uses and sets "lp->fill".
1678 * Returns the height of the added line in "lp->height".
1679 * Lines below the last one are incredibly high.
1680 */
1681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001682botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684#ifdef FEAT_DIFF
1685 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1686 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001687 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 ++lp->fill;
1689 lp->height = 1;
1690 }
1691 else
1692#endif
1693 {
1694 ++lp->lnum;
1695#ifdef FEAT_DIFF
1696 lp->fill = 0;
1697#endif
1698 if (lp->lnum > curbuf->b_ml.ml_line_count)
1699 lp->height = MAXCOL;
1700 else
1701#ifdef FEAT_FOLDING
1702 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001703 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 lp->height = 1;
1705 else
1706#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001707 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
1709}
1710
1711#ifdef FEAT_DIFF
1712/*
1713 * Switch from including filler lines below lp->lnum to including filler
1714 * lines above loff.lnum + 1. This keeps pointing to the same line.
1715 * When there are no filler lines nothing changes.
1716 */
1717 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001718botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 if (lp->fill > 0)
1721 {
1722 ++lp->lnum;
1723 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1724 }
1725}
1726
1727/*
1728 * Switch from including filler lines above lp->lnum to including filler
1729 * lines below loff.lnum - 1. This keeps pointing to the same line.
1730 * When there are no filler lines nothing changes.
1731 */
1732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001733topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
1735 if (lp->fill > 0)
1736 {
1737 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1738 --lp->lnum;
1739 }
1740}
1741#endif
1742
1743/*
1744 * Recompute topline to put the cursor at the top of the window.
1745 * Scroll at least "min_scroll" lines.
1746 * If "always" is TRUE, always set topline (for "zt").
1747 */
1748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001749scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 int scrolled = 0;
1752 int extra = 0;
1753 int used;
1754 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001755 linenr_T top; // just above displayed lines
1756 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 linenr_T old_topline = curwin->w_topline;
1758#ifdef FEAT_DIFF
1759 linenr_T old_topfill = curwin->w_topfill;
1760#endif
1761 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001762 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (mouse_dragging > 0)
1765 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
1767 /*
1768 * Decrease topline until:
1769 * - it has become 1
1770 * - (part of) the cursor line is moved off the screen or
1771 * - moved at least 'scrolljump' lines and
1772 * - at least 'scrolloff' lines above and below the cursor
1773 */
1774 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001775 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (curwin->w_cursor.lnum < curwin->w_topline)
1777 scrolled = used;
1778
1779#ifdef FEAT_FOLDING
1780 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1781 {
1782 --top;
1783 ++bot;
1784 }
1785 else
1786#endif
1787 {
1788 top = curwin->w_cursor.lnum - 1;
1789 bot = curwin->w_cursor.lnum + 1;
1790 }
1791 new_topline = top + 1;
1792
1793#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001794 // "used" already contains the number of filler lines above, don't add it
1795 // again.
1796 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001797 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
1799
1800 /*
1801 * Check if the lines from "top" to "bot" fit in the window. If they do,
1802 * set new_topline and advance "top" and "bot" to include more lines.
1803 */
1804 while (top > 0)
1805 {
1806#ifdef FEAT_FOLDING
1807 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001808 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 i = 1;
1810 else
1811#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001812 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 used += i;
1814 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1815 {
1816#ifdef FEAT_FOLDING
1817 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001818 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 ++used;
1820 else
1821#endif
1822 used += plines(bot);
1823 }
1824 if (used > curwin->w_height)
1825 break;
1826 if (top < curwin->w_topline)
1827 scrolled += i;
1828
1829 /*
1830 * If scrolling is needed, scroll at least 'sj' lines.
1831 */
1832 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1833 && extra >= off)
1834 break;
1835
1836 extra += i;
1837 new_topline = top;
1838 --top;
1839 ++bot;
1840 }
1841
1842 /*
1843 * If we don't have enough space, put cursor in the middle.
1844 * This makes sure we get the same position when using "k" and "j"
1845 * in a small window.
1846 */
1847 if (used > curwin->w_height)
1848 scroll_cursor_halfway(FALSE);
1849 else
1850 {
1851 /*
1852 * If "always" is FALSE, only adjust topline to a lower value, higher
1853 * value may happen with wrapping lines
1854 */
1855 if (new_topline < curwin->w_topline || always)
1856 curwin->w_topline = new_topline;
1857 if (curwin->w_topline > curwin->w_cursor.lnum)
1858 curwin->w_topline = curwin->w_cursor.lnum;
1859#ifdef FEAT_DIFF
1860 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1861 if (curwin->w_topfill > 0 && extra > off)
1862 {
1863 curwin->w_topfill -= extra - off;
1864 if (curwin->w_topfill < 0)
1865 curwin->w_topfill = 0;
1866 }
1867 check_topfill(curwin, FALSE);
1868#endif
1869 if (curwin->w_topline != old_topline
1870#ifdef FEAT_DIFF
1871 || curwin->w_topfill != old_topfill
1872#endif
1873 )
1874 curwin->w_valid &=
1875 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1876 curwin->w_valid |= VALID_TOPLINE;
1877 }
1878}
1879
1880/*
1881 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1882 * screen lines for text lines.
1883 */
1884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001885set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886{
1887#ifdef FEAT_DIFF
1888 wp->w_filler_rows = 0;
1889#endif
1890 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001891 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 else
1893 {
1894 wp->w_empty_rows = wp->w_height - used;
1895#ifdef FEAT_DIFF
1896 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1897 {
1898 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1899 if (wp->w_empty_rows > wp->w_filler_rows)
1900 wp->w_empty_rows -= wp->w_filler_rows;
1901 else
1902 {
1903 wp->w_filler_rows = wp->w_empty_rows;
1904 wp->w_empty_rows = 0;
1905 }
1906 }
1907#endif
1908 }
1909}
1910
1911/*
1912 * Recompute topline to put the cursor at the bottom of the window.
1913 * Scroll at least "min_scroll" lines.
1914 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1915 * This is messy stuff!!!
1916 */
1917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001918scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 int used;
1921 int scrolled = 0;
1922 int extra = 0;
1923 int i;
1924 linenr_T line_count;
1925 linenr_T old_topline = curwin->w_topline;
1926 lineoff_T loff;
1927 lineoff_T boff;
1928#ifdef FEAT_DIFF
1929 int old_topfill = curwin->w_topfill;
1930 int fill_below_window;
1931#endif
1932 linenr_T old_botline = curwin->w_botline;
1933 linenr_T old_valid = curwin->w_valid;
1934 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001935 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001936 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 cln = curwin->w_cursor.lnum;
1939 if (set_topbot)
1940 {
1941 used = 0;
1942 curwin->w_botline = cln + 1;
1943#ifdef FEAT_DIFF
1944 loff.fill = 0;
1945#endif
1946 for (curwin->w_topline = curwin->w_botline;
1947 curwin->w_topline > 1;
1948 curwin->w_topline = loff.lnum)
1949 {
1950 loff.lnum = curwin->w_topline;
1951 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001952 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 break;
1954 used += loff.height;
1955#ifdef FEAT_DIFF
1956 curwin->w_topfill = loff.fill;
1957#endif
1958 }
1959 set_empty_rows(curwin, used);
1960 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1961 if (curwin->w_topline != old_topline
1962#ifdef FEAT_DIFF
1963 || curwin->w_topfill != old_topfill
1964#endif
1965 )
1966 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1967 }
1968 else
1969 validate_botline();
1970
Bram Moolenaar85a20022019-12-21 18:25:54 +01001971 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972#ifdef FEAT_DIFF
1973 used = plines_nofill(cln);
1974#else
1975 validate_cheight();
1976 used = curwin->w_cline_height;
1977#endif
1978
Bram Moolenaar85a20022019-12-21 18:25:54 +01001979 // If the cursor is below botline, we will at least scroll by the height
1980 // of the cursor line. Correct for empty lines, which are really part of
1981 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (cln >= curwin->w_botline)
1983 {
1984 scrolled = used;
1985 if (cln == curwin->w_botline)
1986 scrolled -= curwin->w_empty_rows;
1987 }
1988
1989 /*
1990 * Stop counting lines to scroll when
1991 * - hitting start of the file
1992 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01001993 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 * - lines between botline and cursor have been counted
1995 */
1996#ifdef FEAT_FOLDING
1997 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1998#endif
1999 {
2000 loff.lnum = cln;
2001 boff.lnum = cln;
2002 }
2003#ifdef FEAT_DIFF
2004 loff.fill = 0;
2005 boff.fill = 0;
2006 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2007 - curwin->w_filler_rows;
2008#endif
2009
2010 while (loff.lnum > 1)
2011 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002012 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2013 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002015 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2017 && loff.lnum <= curwin->w_botline
2018#ifdef FEAT_DIFF
2019 && (loff.lnum < curwin->w_botline
2020 || loff.fill >= fill_below_window)
2021#endif
2022 )
2023 break;
2024
Bram Moolenaar85a20022019-12-21 18:25:54 +01002025 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002027 if (loff.height == MAXCOL)
2028 used = MAXCOL;
2029 else
2030 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 if (used > curwin->w_height)
2032 break;
2033 if (loff.lnum >= curwin->w_botline
2034#ifdef FEAT_DIFF
2035 && (loff.lnum > curwin->w_botline
2036 || loff.fill <= fill_below_window)
2037#endif
2038 )
2039 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002040 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 scrolled += loff.height;
2042 if (loff.lnum == curwin->w_botline
2043#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002044 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#endif
2046 )
2047 scrolled -= curwin->w_empty_rows;
2048 }
2049
2050 if (boff.lnum < curbuf->b_ml.ml_line_count)
2051 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002052 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 botline_forw(&boff);
2054 used += boff.height;
2055 if (used > curwin->w_height)
2056 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002057 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2058 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
2060 extra += boff.height;
2061 if (boff.lnum >= curwin->w_botline
2062#ifdef FEAT_DIFF
2063 || (boff.lnum + 1 == curwin->w_botline
2064 && boff.fill > curwin->w_filler_rows)
2065#endif
2066 )
2067 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002068 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 scrolled += boff.height;
2070 if (boff.lnum == curwin->w_botline
2071#ifdef FEAT_DIFF
2072 && boff.fill == 0
2073#endif
2074 )
2075 scrolled -= curwin->w_empty_rows;
2076 }
2077 }
2078 }
2079 }
2080
Bram Moolenaar85a20022019-12-21 18:25:54 +01002081 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (scrolled <= 0)
2083 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002084 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 else if (used > curwin->w_height)
2086 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002087 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 else
2089 {
2090 line_count = 0;
2091#ifdef FEAT_DIFF
2092 boff.fill = curwin->w_topfill;
2093#endif
2094 boff.lnum = curwin->w_topline - 1;
2095 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2096 {
2097 botline_forw(&boff);
2098 i += boff.height;
2099 ++line_count;
2100 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002101 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 line_count = 9999;
2103 }
2104
2105 /*
2106 * Scroll up if the cursor is off the bottom of the screen a bit.
2107 * Otherwise put it at 1/2 of the screen.
2108 */
2109 if (line_count >= curwin->w_height && line_count > min_scroll)
2110 scroll_cursor_halfway(FALSE);
2111 else
2112 scrollup(line_count, TRUE);
2113
2114 /*
2115 * If topline didn't change we need to restore w_botline and w_empty_rows
2116 * (we changed them).
2117 * If topline did change, update_screen() will set botline.
2118 */
2119 if (curwin->w_topline == old_topline && set_topbot)
2120 {
2121 curwin->w_botline = old_botline;
2122 curwin->w_empty_rows = old_empty_rows;
2123 curwin->w_valid = old_valid;
2124 }
2125 curwin->w_valid |= VALID_TOPLINE;
2126}
2127
2128/*
2129 * Recompute topline to put the cursor halfway the window
2130 * If "atend" is TRUE, also put it halfway at the end of the file.
2131 */
2132 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002133scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135 int above = 0;
2136 linenr_T topline;
2137#ifdef FEAT_DIFF
2138 int topfill = 0;
2139#endif
2140 int below = 0;
2141 int used;
2142 lineoff_T loff;
2143 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002144#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002145 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002148#ifdef FEAT_PROP_POPUP
2149 // if the width changed this needs to be updated first
2150 may_update_popup_position();
2151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2153#ifdef FEAT_FOLDING
2154 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2155#endif
2156#ifdef FEAT_DIFF
2157 used = plines_nofill(loff.lnum);
2158 loff.fill = 0;
2159 boff.fill = 0;
2160#else
2161 used = plines(loff.lnum);
2162#endif
2163 topline = loff.lnum;
2164 while (topline > 1)
2165 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002166 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
2168 if (boff.lnum < curbuf->b_ml.ml_line_count)
2169 {
2170 botline_forw(&boff);
2171 used += boff.height;
2172 if (used > curwin->w_height)
2173 break;
2174 below += boff.height;
2175 }
2176 else
2177 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002178 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (atend)
2180 ++used;
2181 }
2182 }
2183
Bram Moolenaar85a20022019-12-21 18:25:54 +01002184 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
2186 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002187 if (loff.height == MAXCOL)
2188 used = MAXCOL;
2189 else
2190 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (used > curwin->w_height)
2192 break;
2193 above += loff.height;
2194 topline = loff.lnum;
2195#ifdef FEAT_DIFF
2196 topfill = loff.fill;
2197#endif
2198 }
2199 }
2200#ifdef FEAT_FOLDING
2201 if (!hasFolding(topline, &curwin->w_topline, NULL))
2202#endif
2203 curwin->w_topline = topline;
2204#ifdef FEAT_DIFF
2205 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002206 if (old_topline > curwin->w_topline + curwin->w_height)
2207 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 check_topfill(curwin, FALSE);
2209#endif
2210 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2211 curwin->w_valid |= VALID_TOPLINE;
2212}
2213
2214/*
2215 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002216 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 * If not possible, put it at the same position as scroll_cursor_halfway().
2218 * When called topline must be valid!
2219 */
2220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002221cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002223 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002225 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 linenr_T botline;
2227 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002230 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 /*
2233 * How many lines we would like to have above/below the cursor depends on
2234 * whether the first/last line of the file is on screen.
2235 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002236 above_wanted = so;
2237 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002238 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
2240 above_wanted = mouse_dragging - 1;
2241 below_wanted = mouse_dragging - 1;
2242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (curwin->w_topline == 1)
2244 {
2245 above_wanted = 0;
2246 max_off = curwin->w_height / 2;
2247 if (below_wanted > max_off)
2248 below_wanted = max_off;
2249 }
2250 validate_botline();
2251 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002252 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 below_wanted = 0;
2255 max_off = (curwin->w_height - 1) / 2;
2256 if (above_wanted > max_off)
2257 above_wanted = max_off;
2258 }
2259
2260 /*
2261 * If there are sufficient file-lines above and below the cursor, we can
2262 * return now.
2263 */
2264 cln = curwin->w_cursor.lnum;
2265 if (cln >= curwin->w_topline + above_wanted
2266 && cln < curwin->w_botline - below_wanted
2267#ifdef FEAT_FOLDING
2268 && !hasAnyFolding(curwin)
2269#endif
2270 )
2271 return;
2272
2273 /*
2274 * Narrow down the area where the cursor can be put by taking lines from
2275 * the top and the bottom until:
2276 * - the desired context lines are found
2277 * - the lines from the top is past the lines from the bottom
2278 */
2279 topline = curwin->w_topline;
2280 botline = curwin->w_botline - 1;
2281#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002282 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 above = curwin->w_topfill;
2284 below = curwin->w_filler_rows;
2285#endif
2286 while ((above < above_wanted || below < below_wanted) && topline < botline)
2287 {
2288 if (below < below_wanted && (below <= above || above >= above_wanted))
2289 {
2290#ifdef FEAT_FOLDING
2291 if (hasFolding(botline, &botline, NULL))
2292 ++below;
2293 else
2294#endif
2295 below += plines(botline);
2296 --botline;
2297 }
2298 if (above < above_wanted && (above < below || below >= below_wanted))
2299 {
2300#ifdef FEAT_FOLDING
2301 if (hasFolding(topline, NULL, &topline))
2302 ++above;
2303 else
2304#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002305 above += PLINES_NOFILL(topline);
2306#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002307 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (topline < botline)
2309 above += diff_check_fill(curwin, topline + 1);
2310#endif
2311 ++topline;
2312 }
2313 }
2314 if (topline == botline || botline == 0)
2315 curwin->w_cursor.lnum = topline;
2316 else if (topline > botline)
2317 curwin->w_cursor.lnum = botline;
2318 else
2319 {
2320 if (cln < topline && curwin->w_topline > 1)
2321 {
2322 curwin->w_cursor.lnum = topline;
2323 curwin->w_valid &=
2324 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2325 }
2326 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2327 {
2328 curwin->w_cursor.lnum = botline;
2329 curwin->w_valid &=
2330 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2331 }
2332 }
2333 curwin->w_valid |= VALID_TOPLINE;
2334}
2335
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002336static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338/*
2339 * move screen 'count' pages up or down and update screen
2340 *
2341 * return FAIL for failure, OK otherwise
2342 */
2343 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002344onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 long n;
2347 int retval = OK;
2348 lineoff_T loff;
2349 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002350 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
Bram Moolenaar85a20022019-12-21 18:25:54 +01002352 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 beep_flush();
2355 return FAIL;
2356 }
2357
2358 for ( ; count > 0; --count)
2359 {
2360 validate_botline();
2361 /*
2362 * It's an error to move a page up when the first line is already on
2363 * the screen. It's an error to move a page down when the last line
2364 * is on the screen and the topline is 'scrolloff' lines from the
2365 * last line.
2366 */
2367 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002368 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2370 : (curwin->w_topline == 1
2371#ifdef FEAT_DIFF
2372 && curwin->w_topfill ==
2373 diff_check_fill(curwin, curwin->w_topline)
2374#endif
2375 ))
2376 {
2377 beep_flush();
2378 retval = FAIL;
2379 break;
2380 }
2381
2382#ifdef FEAT_DIFF
2383 loff.fill = 0;
2384#endif
2385 if (dir == FORWARD)
2386 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002387 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002389 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002390 if (p_window <= 2)
2391 ++curwin->w_topline;
2392 else
2393 curwin->w_topline += p_window - 2;
2394 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2395 curwin->w_topline = curbuf->b_ml.ml_line_count;
2396 curwin->w_cursor.lnum = curwin->w_topline;
2397 }
2398 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2399 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002400 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 curwin->w_topline = curbuf->b_ml.ml_line_count;
2402#ifdef FEAT_DIFF
2403 curwin->w_topfill = 0;
2404#endif
2405 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2406 }
2407 else
2408 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002409 // For the overlap, start with the line just below the window
2410 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 loff.lnum = curwin->w_botline;
2412#ifdef FEAT_DIFF
2413 loff.fill = diff_check_fill(curwin, loff.lnum)
2414 - curwin->w_filler_rows;
2415#endif
2416 get_scroll_overlap(&loff, -1);
2417 curwin->w_topline = loff.lnum;
2418#ifdef FEAT_DIFF
2419 curwin->w_topfill = loff.fill;
2420 check_topfill(curwin, FALSE);
2421#endif
2422 curwin->w_cursor.lnum = curwin->w_topline;
2423 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2424 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2425 }
2426 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002427 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
2429#ifdef FEAT_DIFF
2430 if (curwin->w_topline == 1)
2431 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002432 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 max_topfill();
2434 continue;
2435 }
2436#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002437 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002438 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002440 if (p_window <= 2)
2441 --curwin->w_topline;
2442 else
2443 curwin->w_topline -= p_window - 2;
2444 if (curwin->w_topline < 1)
2445 curwin->w_topline = 1;
2446 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2447 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2448 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2449 continue;
2450 }
2451
Bram Moolenaar85a20022019-12-21 18:25:54 +01002452 // Find the line at the top of the window that is going to be the
2453 // line at the bottom of the window. Make sure this results in
2454 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 loff.lnum = curwin->w_topline - 1;
2456#ifdef FEAT_DIFF
2457 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2458 - curwin->w_topfill;
2459#endif
2460 get_scroll_overlap(&loff, 1);
2461
2462 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2463 {
2464 loff.lnum = curbuf->b_ml.ml_line_count;
2465#ifdef FEAT_DIFF
2466 loff.fill = 0;
2467 }
2468 else
2469 {
2470 botline_topline(&loff);
2471#endif
2472 }
2473 curwin->w_cursor.lnum = loff.lnum;
2474
Bram Moolenaar85a20022019-12-21 18:25:54 +01002475 // Find the line just above the new topline to get the right line
2476 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 n = 0;
2478 while (n <= curwin->w_height && loff.lnum >= 1)
2479 {
2480 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002481 if (loff.height == MAXCOL)
2482 n = MAXCOL;
2483 else
2484 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002486 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 curwin->w_topline = 1;
2489#ifdef FEAT_DIFF
2490 max_topfill();
2491#endif
2492 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2493 }
2494 else
2495 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002496 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497#ifdef FEAT_DIFF
2498 topline_botline(&loff);
2499#endif
2500 botline_forw(&loff);
2501 botline_forw(&loff);
2502#ifdef FEAT_DIFF
2503 botline_topline(&loff);
2504#endif
2505#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002506 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2508#endif
2509
Bram Moolenaar85a20022019-12-21 18:25:54 +01002510 // Always scroll at least one line. Avoid getting stuck on
2511 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (loff.lnum >= curwin->w_topline
2513#ifdef FEAT_DIFF
2514 && (loff.lnum > curwin->w_topline
2515 || loff.fill >= curwin->w_topfill)
2516#endif
2517 )
2518 {
2519#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002520 // First try using the maximum number of filler lines. If
2521 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 loff.fill = curwin->w_topfill;
2523 if (curwin->w_topfill < diff_check_fill(curwin,
2524 curwin->w_topline))
2525 max_topfill();
2526 if (curwin->w_topfill == loff.fill)
2527#endif
2528 {
2529 --curwin->w_topline;
2530#ifdef FEAT_DIFF
2531 curwin->w_topfill = 0;
2532#endif
2533 }
2534 comp_botline(curwin);
2535 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002536 curwin->w_valid &=
2537 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539 else
2540 {
2541 curwin->w_topline = loff.lnum;
2542#ifdef FEAT_DIFF
2543 curwin->w_topfill = loff.fill;
2544 check_topfill(curwin, FALSE);
2545#endif
2546 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2547 }
2548 }
2549 }
2550 }
2551#ifdef FEAT_FOLDING
2552 foldAdjustCursor();
2553#endif
2554 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002555 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002556 if (retval == OK)
2557 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2559
Bram Moolenaar907dad72018-07-10 15:07:15 +02002560 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002562 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2563 // But make sure we scroll at least one line (happens with mix of long
2564 // wrapping lines and non-wrapping line).
2565 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002567 scroll_cursor_top(1, FALSE);
2568 if (curwin->w_topline <= old_topline
2569 && old_topline < curbuf->b_ml.ml_line_count)
2570 {
2571 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002573 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2574#endif
2575 }
2576 }
2577#ifdef FEAT_FOLDING
2578 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582
2583 redraw_later(VALID);
2584 return retval;
2585}
2586
2587/*
2588 * Decide how much overlap to use for page-up or page-down scrolling.
2589 * This is symmetric, so that doing both keeps the same lines displayed.
2590 * Three lines are examined:
2591 *
2592 * before CTRL-F after CTRL-F / before CTRL-B
2593 * etc. l1
2594 * l1 last but one line ------------
2595 * l2 last text line l2 top text line
2596 * ------------- l3 second text line
2597 * l3 etc.
2598 */
2599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002600get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
2602 int h1, h2, h3, h4;
2603 int min_height = curwin->w_height - 2;
2604 lineoff_T loff0, loff1, loff2;
2605
2606#ifdef FEAT_DIFF
2607 if (lp->fill > 0)
2608 lp->height = 1;
2609 else
2610 lp->height = plines_nofill(lp->lnum);
2611#else
2612 lp->height = plines(lp->lnum);
2613#endif
2614 h1 = lp->height;
2615 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002616 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
2618 loff0 = *lp;
2619 if (dir > 0)
2620 botline_forw(lp);
2621 else
2622 topline_back(lp);
2623 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002624 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002626 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 return;
2628 }
2629
2630 loff1 = *lp;
2631 if (dir > 0)
2632 botline_forw(lp);
2633 else
2634 topline_back(lp);
2635 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002636 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002638 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return;
2640 }
2641
2642 loff2 = *lp;
2643 if (dir > 0)
2644 botline_forw(lp);
2645 else
2646 topline_back(lp);
2647 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002648 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002649 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 return;
2653}
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655/*
2656 * Scroll 'scroll' lines up or down.
2657 */
2658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002659halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 long scrolled = 0;
2662 int i;
2663 int n;
2664 int room;
2665
2666 if (Prenum)
2667 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2668 curwin->w_height : Prenum;
2669 n = (curwin->w_p_scr <= curwin->w_height) ?
2670 curwin->w_p_scr : curwin->w_height;
2671
Bram Moolenaard5d37532017-03-27 23:02:07 +02002672 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 validate_botline();
2674 room = curwin->w_empty_rows;
2675#ifdef FEAT_DIFF
2676 room += curwin->w_filler_rows;
2677#endif
2678 if (flag)
2679 {
2680 /*
2681 * scroll the text up
2682 */
2683 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2684 {
2685#ifdef FEAT_DIFF
2686 if (curwin->w_topfill > 0)
2687 {
2688 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002689 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 --curwin->w_topfill;
2691 }
2692 else
2693#endif
2694 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002695 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 n -= i;
2697 if (n < 0 && scrolled > 0)
2698 break;
2699#ifdef FEAT_FOLDING
2700 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2701#endif
2702 ++curwin->w_topline;
2703#ifdef FEAT_DIFF
2704 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2705#endif
2706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2708 {
2709 ++curwin->w_cursor.lnum;
2710 curwin->w_valid &=
2711 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2715 scrolled += i;
2716
2717 /*
2718 * Correct w_botline for changed w_topline.
2719 * Won't work when there are filler lines.
2720 */
2721#ifdef FEAT_DIFF
2722 if (curwin->w_p_diff)
2723 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2724 else
2725#endif
2726 {
2727 room += i;
2728 do
2729 {
2730 i = plines(curwin->w_botline);
2731 if (i > room)
2732 break;
2733#ifdef FEAT_FOLDING
2734 (void)hasFolding(curwin->w_botline, NULL,
2735 &curwin->w_botline);
2736#endif
2737 ++curwin->w_botline;
2738 room -= i;
2739 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2740 }
2741 }
2742
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 /*
2744 * When hit bottom of the file: move cursor down.
2745 */
2746 if (n > 0)
2747 {
2748# ifdef FEAT_FOLDING
2749 if (hasAnyFolding(curwin))
2750 {
2751 while (--n >= 0
2752 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2753 {
2754 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2755 &curwin->w_cursor.lnum);
2756 ++curwin->w_cursor.lnum;
2757 }
2758 }
2759 else
2760# endif
2761 curwin->w_cursor.lnum += n;
2762 check_cursor_lnum();
2763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
2765 else
2766 {
2767 /*
2768 * scroll the text down
2769 */
2770 while (n > 0 && curwin->w_topline > 1)
2771 {
2772#ifdef FEAT_DIFF
2773 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2774 {
2775 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002776 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 ++curwin->w_topfill;
2778 }
2779 else
2780#endif
2781 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002782 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 n -= i;
2784 if (n < 0 && scrolled > 0)
2785 break;
2786 --curwin->w_topline;
2787#ifdef FEAT_FOLDING
2788 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2789#endif
2790#ifdef FEAT_DIFF
2791 curwin->w_topfill = 0;
2792#endif
2793 }
2794 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2795 VALID_BOTLINE|VALID_BOTLINE_AP);
2796 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (curwin->w_cursor.lnum > 1)
2798 {
2799 --curwin->w_cursor.lnum;
2800 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002803
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 /*
2805 * When hit top of the file: move cursor up.
2806 */
2807 if (n > 0)
2808 {
2809 if (curwin->w_cursor.lnum <= (linenr_T)n)
2810 curwin->w_cursor.lnum = 1;
2811 else
2812# ifdef FEAT_FOLDING
2813 if (hasAnyFolding(curwin))
2814 {
2815 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2816 {
2817 --curwin->w_cursor.lnum;
2818 (void)hasFolding(curwin->w_cursor.lnum,
2819 &curwin->w_cursor.lnum, NULL);
2820 }
2821 }
2822 else
2823# endif
2824 curwin->w_cursor.lnum -= n;
2825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002828 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 foldAdjustCursor();
2830# endif
2831#ifdef FEAT_DIFF
2832 check_topfill(curwin, !flag);
2833#endif
2834 cursor_correct();
2835 beginline(BL_SOL | BL_FIX);
2836 redraw_later(VALID);
2837}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002838
Bram Moolenaar860cae12010-06-05 23:22:07 +02002839 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002840do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002841{
2842 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002843 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002844 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002845 colnr_T curswant = curwin->w_curswant;
2846 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002847 win_T *old_curwin = curwin;
2848 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002849 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002850 int old_VIsual_select = VIsual_select;
2851 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002852
2853 /*
2854 * loop through the cursorbound windows
2855 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002856 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002857 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002858 {
2859 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002860 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002861 if (curwin != old_curwin && curwin->w_p_crb)
2862 {
2863# ifdef FEAT_DIFF
2864 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002865 curwin->w_cursor.lnum =
2866 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002867 else
2868# endif
2869 curwin->w_cursor.lnum = line;
2870 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002871 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002872 curwin->w_curswant = curswant;
2873 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002874
Bram Moolenaar85a20022019-12-21 18:25:54 +01002875 // Make sure the cursor is in a valid position. Temporarily set
2876 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002877 restart_edit_save = restart_edit;
2878 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002879 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002880# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002881 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002882 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002883# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002884 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002885 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002886 if (has_mbyte)
2887 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002888 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002889
Bram Moolenaar85a20022019-12-21 18:25:54 +01002890 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002891 if (!curwin->w_p_scb)
2892 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002893 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002894 }
2895 }
2896
2897 /*
2898 * reset current-window
2899 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002900 VIsual_select = old_VIsual_select;
2901 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002902 curwin = old_curwin;
2903 curbuf = old_curbuf;
2904}