blob: ea86b88ea0b92f28a1a9f96ae0621c0eb1b939b5 [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{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200538#ifdef FEAT_DIFF
539 linenr_T prev_topline = wp->w_topline;
540#endif
541
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100543 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
545#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100546 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100548 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
549 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000551 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200553 if (lnum != prev_topline)
554 // Keep the filler lines when the topline didn't change.
555 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#endif
557 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100558 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 redraw_later(VALID);
560}
561
562/*
563 * Call this function when the length of the cursor line (in screen
564 * characters) has changed, and the change is before the cursor.
565 * Need to take care of w_botline separately!
566 */
567 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100568changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
570 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
571 |VALID_CHEIGHT|VALID_TOPLINE);
572}
573
574 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100575changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
577 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
578 |VALID_CHEIGHT|VALID_TOPLINE);
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * Call this function when the length of a line (in screen characters) above
583 * the cursor have changed.
584 * Need to take care of w_botline separately!
585 */
586 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100587changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
590 |VALID_CHEIGHT|VALID_TOPLINE);
591}
592
593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595{
596 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
597 |VALID_CHEIGHT|VALID_TOPLINE);
598}
599
600/*
601 * Make sure the value of curwin->w_botline is valid.
602 */
603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100606 validate_botline_win(curwin);
607}
608
609/*
610 * Make sure the value of wp->w_botline is valid.
611 */
612 void
613validate_botline_win(win_T *wp)
614{
615 if (!(wp->w_valid & VALID_BOTLINE))
616 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617}
618
619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 * Mark curwin->w_botline as invalid (because of some change in the buffer).
621 */
622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100623invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
626}
627
628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100629invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100635approximate_botline_win(
636 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637{
638 wp->w_valid &= ~VALID_BOTLINE;
639}
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641/*
642 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
643 */
644 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100645cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
647 check_cursor_moved(curwin);
648 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
649 (VALID_WROW|VALID_WCOL));
650}
651
652/*
653 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
654 * w_topline must be valid, you may need to call update_topline() first!
655 */
656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100657validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 check_cursor_moved(curwin);
660 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
661 curs_columns(TRUE);
662}
663
664#if defined(FEAT_GUI) || defined(PROTO)
665/*
666 * validate w_cline_row.
667 */
668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100669validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 /*
672 * First make sure that w_topline is valid (after moving the cursor).
673 */
674 update_topline();
675 check_cursor_moved(curwin);
676 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100677 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678}
679#endif
680
681/*
682 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200683 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 */
685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100686curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 linenr_T lnum;
689 int i;
690 int all_invalid;
691 int valid;
692#ifdef FEAT_FOLDING
693 long fold_count;
694#endif
695
Bram Moolenaar85a20022019-12-21 18:25:54 +0100696 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 all_invalid = (!redrawing()
698 || wp->w_lines_valid == 0
699 || wp->w_lines[0].wl_lnum > wp->w_topline);
700 i = 0;
701 wp->w_cline_row = 0;
702 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
703 {
704 valid = FALSE;
705 if (!all_invalid && i < wp->w_lines_valid)
706 {
707 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100708 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (wp->w_lines[i].wl_lnum == lnum)
710 {
711#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100712 // Check for newly inserted lines below this row, in which
713 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (!wp->w_buffer->b_mod_set
715 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
716 || wp->w_buffer->b_mod_top
717 > wp->w_lines[i].wl_lastlnum + 1)
718#endif
719 valid = TRUE;
720 }
721 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100722 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 }
724 if (valid
725#ifdef FEAT_DIFF
726 && (lnum != wp->w_topline || !wp->w_p_diff)
727#endif
728 )
729 {
730#ifdef FEAT_FOLDING
731 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100732 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 if (lnum > wp->w_cursor.lnum)
734 break;
735#else
736 ++lnum;
737#endif
738 wp->w_cline_row += wp->w_lines[i].wl_size;
739 }
740 else
741 {
742#ifdef FEAT_FOLDING
743 fold_count = foldedCount(wp, lnum, NULL);
744 if (fold_count)
745 {
746 lnum += fold_count;
747 if (lnum > wp->w_cursor.lnum)
748 break;
749 ++wp->w_cline_row;
750 }
751 else
752#endif
753#ifdef FEAT_DIFF
754 if (lnum == wp->w_topline)
755 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
756 + wp->w_topfill;
757 else
758#endif
759 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
760 }
761 }
762
763 check_cursor_moved(wp);
764 if (!(wp->w_valid & VALID_CHEIGHT))
765 {
766 if (all_invalid
767 || i == wp->w_lines_valid
768 || (i < wp->w_lines_valid
769 && (!wp->w_lines[i].wl_valid
770 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
771 {
772#ifdef FEAT_DIFF
773 if (wp->w_cursor.lnum == wp->w_topline)
774 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
775 TRUE) + wp->w_topfill;
776 else
777#endif
778 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
779#ifdef FEAT_FOLDING
780 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
781 NULL, NULL, TRUE, NULL);
782#endif
783 }
784 else if (i > wp->w_lines_valid)
785 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100786 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 wp->w_cline_height = 0;
788#ifdef FEAT_FOLDING
789 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
790 NULL, NULL, TRUE, NULL);
791#endif
792 }
793 else
794 {
795 wp->w_cline_height = wp->w_lines[i].wl_size;
796#ifdef FEAT_FOLDING
797 wp->w_cline_folded = wp->w_lines[i].wl_folded;
798#endif
799 }
800 }
801
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100802 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805}
806
807/*
808 * Validate curwin->w_virtcol only.
809 */
810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100811validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 validate_virtcol_win(curwin);
814}
815
816/*
817 * Validate wp->w_virtcol only.
818 */
819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100820validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
822 check_cursor_moved(wp);
823 if (!(wp->w_valid & VALID_VIRTCOL))
824 {
825 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
826 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000827#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200828 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000829 redraw_win_later(wp, SOME_VALID);
830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832}
833
834/*
835 * Validate curwin->w_cline_height only.
836 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100838validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 check_cursor_moved(curwin);
841 if (!(curwin->w_valid & VALID_CHEIGHT))
842 {
843#ifdef FEAT_DIFF
844 if (curwin->w_cursor.lnum == curwin->w_topline)
845 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
846 + curwin->w_topfill;
847 else
848#endif
849 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
850#ifdef FEAT_FOLDING
851 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
852#endif
853 curwin->w_valid |= VALID_CHEIGHT;
854 }
855}
856
857/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000858 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100861validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 colnr_T off;
864 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100865 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 validate_virtcol();
868 if (!(curwin->w_valid & VALID_WCOL))
869 {
870 col = curwin->w_virtcol;
871 off = curwin_col_off();
872 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200873 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaar85a20022019-12-21 18:25:54 +0100875 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000876 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200877 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100878 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100879 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200880 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000881 if (col > (int)curwin->w_leftcol)
882 col -= curwin->w_leftcol;
883 else
884 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000886
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100888#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100889 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 }
892}
893
894/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200895 * Compute offset of a window, occupied by absolute or relative line number,
896 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 */
898 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100899win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900{
Bram Moolenaar64486672010-05-16 15:46:46 +0200901 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#ifdef FEAT_CMDWIN
903 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
904#endif
905#ifdef FEAT_FOLDING
906 + wp->w_p_fdc
907#endif
908#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200909 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
911 );
912}
913
914 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100915curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
917 return win_col_off(curwin);
918}
919
920/*
921 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200922 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
923 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 */
925 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
Bram Moolenaar64486672010-05-16 15:46:46 +0200928 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000929 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 return 0;
931}
932
933 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100934curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 return win_col_off2(curwin);
937}
938
939/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100940 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * Also updates curwin->w_wrow and curwin->w_cline_row.
942 * Also updates curwin->w_leftcol.
943 */
944 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100945curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100946 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100949 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 int off_left, off_right;
951 int n;
952 int p_lines;
953 int width = 0;
954 int textwidth;
955 int new_leftcol;
956 colnr_T startcol;
957 colnr_T endcol;
958 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100959 long so = get_scrolloff_value();
960 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961
962 /*
963 * First make sure that w_topline is valid (after moving the cursor).
964 */
965 update_topline();
966
967 /*
968 * Next make sure that w_cline_row is valid.
969 */
970 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100971 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973 /*
974 * Compute the number of virtual columns.
975 */
976#ifdef FEAT_FOLDING
977 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100978 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
980 else
981#endif
982 getvvcol(curwin, &curwin->w_cursor,
983 &startcol, &(curwin->w_virtcol), &endcol);
984
Bram Moolenaar85a20022019-12-21 18:25:54 +0100985 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100987 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 extra = curwin_col_off();
990 curwin->w_wcol = curwin->w_virtcol + extra;
991 endcol += extra;
992
993 /*
994 * Now compute w_wrow, counting screen lines from w_cline_row.
995 */
996 curwin->w_wrow = curwin->w_cline_row;
997
Bram Moolenaar02631462017-09-22 15:20:32 +0200998 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (textwidth <= 0)
1000 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001001 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001002 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001003 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001004 if (curwin->w_p_wrap)
1005 curwin->w_wrow = curwin->w_height - 1;
1006 else
1007 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001009 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
1011 width = textwidth + curwin_col_off2();
1012
Bram Moolenaar85a20022019-12-21 18:25:54 +01001013 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001014 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001016#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001017 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001018#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001019
Bram Moolenaar85a20022019-12-21 18:25:54 +01001020 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001021 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 curwin->w_wcol -= n * width;
1023 curwin->w_wrow += n;
1024
1025#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001026 // When cursor wraps to first char of next line in Insert
1027 // mode, the 'showbreak' string isn't shown, backup to first
1028 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001029 sbr = get_showbreak_value(curwin);
1030 if (*sbr && *ml_get_cursor() == NUL
1031 && curwin->w_wcol == (int)vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 curwin->w_wcol = 0;
1033#endif
1034 }
1035 }
1036
Bram Moolenaar85a20022019-12-21 18:25:54 +01001037 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1038 // is not folded.
1039 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001040 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041#ifdef FEAT_FOLDING
1042 && !curwin->w_cline_folded
1043#endif
1044 )
1045 {
1046 /*
1047 * If Cursor is left of the screen, scroll rightwards.
1048 * If Cursor is right of the screen, scroll leftwards
1049 * If we get closer to the edge than 'sidescrolloff', scroll a little
1050 * extra
1051 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001052 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001053 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001054 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (off_left < 0 || off_right > 0)
1056 {
1057 if (off_left < 0)
1058 diff = -off_left;
1059 else
1060 diff = off_right;
1061
Bram Moolenaar85a20022019-12-21 18:25:54 +01001062 // When far off or not enough room on either side, put cursor in
1063 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1065 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1066 else
1067 {
1068 if (diff < p_ss)
1069 diff = p_ss;
1070 if (off_left < 0)
1071 new_leftcol = curwin->w_leftcol - diff;
1072 else
1073 new_leftcol = curwin->w_leftcol + diff;
1074 }
1075 if (new_leftcol < 0)
1076 new_leftcol = 0;
1077 if (new_leftcol != (int)curwin->w_leftcol)
1078 {
1079 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001080 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 redraw_later(NOT_VALID);
1082 }
1083 }
1084 curwin->w_wcol -= curwin->w_leftcol;
1085 }
1086 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1087 curwin->w_wcol -= curwin->w_leftcol;
1088 else
1089 curwin->w_wcol = 0;
1090
1091#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001092 // Skip over filler lines. At the top use w_topfill, there
1093 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (curwin->w_cursor.lnum == curwin->w_topline)
1095 curwin->w_wrow += curwin->w_topfill;
1096 else
1097 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1098#endif
1099
1100 prev_skipcol = curwin->w_skipcol;
1101
1102 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if ((curwin->w_wrow >= curwin->w_height
1105 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001106 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 && (p_lines =
1108#ifdef FEAT_DIFF
1109 plines_win_nofill
1110#else
1111 plines_win
1112#endif
1113 (curwin, curwin->w_cursor.lnum, FALSE))
1114 - 1 >= curwin->w_height))
1115 && curwin->w_height != 0
1116 && curwin->w_cursor.lnum == curwin->w_topline
1117 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001118 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001120 // Cursor past end of screen. Happens with a single line that does
1121 // not fit on screen. Find a skipcol to show the text around the
1122 // cursor. Avoid scrolling all the time. compute value of "extra":
1123 // 1: Less than 'scrolloff' lines above
1124 // 2: Less than 'scrolloff' lines below
1125 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001127 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001129 // Compute last display line of the buffer line that we want at the
1130 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (p_lines == 0)
1132 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1133 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001134 if (p_lines > curwin->w_wrow + so)
1135 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 else
1137 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001138 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 extra += 2;
1140
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001141 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001143 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 n = curwin->w_virtcol / width;
1145 if (n > curwin->w_height / 2)
1146 n -= curwin->w_height / 2;
1147 else
1148 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001149 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (n > p_lines - curwin->w_height + 1)
1151 n = p_lines - curwin->w_height + 1;
1152 curwin->w_skipcol = n * width;
1153 }
1154 else if (extra == 1)
1155 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001156 // less then 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001157 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 + width - 1) / width;
1159 if (extra > 0)
1160 {
1161 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1162 extra = curwin->w_skipcol / width;
1163 curwin->w_skipcol -= extra * width;
1164 }
1165 }
1166 else if (extra == 2)
1167 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001168 // less then 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 endcol = (n - curwin->w_height + 1) * width;
1170 while (endcol > curwin->w_virtcol)
1171 endcol -= width;
1172 if (endcol > curwin->w_skipcol)
1173 curwin->w_skipcol = endcol;
1174 }
1175
1176 curwin->w_wrow -= curwin->w_skipcol / width;
1177 if (curwin->w_wrow >= curwin->w_height)
1178 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 extra = curwin->w_wrow - curwin->w_height + 1;
1181 curwin->w_skipcol += extra * width;
1182 curwin->w_wrow -= extra;
1183 }
1184
1185 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1186 if (extra > 0)
1187 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1188 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001189 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 else
1192 curwin->w_skipcol = 0;
1193 if (prev_skipcol != curwin->w_skipcol)
1194 redraw_later(NOT_VALID);
1195
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001196#ifdef FEAT_SYN_HL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001197 // Redraw when w_virtcol changes and 'cursorcolumn' is set
Bram Moolenaarb6798752014-03-27 12:11:48 +01001198 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001199 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001200 redraw_later(SOME_VALID);
1201#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001202#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1203 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1204 {
1205 curwin->w_wrow += popup_top_extra(curwin);
1206 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001207 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001208 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001209 else
1210 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001211#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001212
Bram Moolenaar08f23632019-11-16 14:19:33 +01001213 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1214 curwin->w_valid_leftcol = curwin->w_leftcol;
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1217}
1218
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001219#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001220/*
1221 * Compute the screen position of text character at "pos" in window "wp"
1222 * The resulting values are one-based, zero when character is not visible.
1223 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001224 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001225textpos2screenpos(
1226 win_T *wp,
1227 pos_T *pos,
1228 int *rowp, // screen row
1229 int *scolp, // start screen column
1230 int *ccolp, // cursor screen column
1231 int *ecolp) // end screen column
1232{
1233 colnr_T scol = 0, ccol = 0, ecol = 0;
1234 int row = 0;
1235 int rowoff = 0;
1236 colnr_T coloff = 0;
1237
Bram Moolenaar189663b2021-07-21 18:04:56 +02001238 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001239 {
1240 colnr_T off;
1241 colnr_T col;
1242 int width;
1243
1244 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1245 getvcol(wp, pos, &scol, &ccol, &ecol);
1246
1247 // similar to what is done in validate_cursor_col()
1248 col = scol;
1249 off = win_col_off(wp);
1250 col += off;
1251 width = wp->w_width - off + win_col_off2(wp);
1252
Bram Moolenaar12034e22019-08-25 22:25:02 +02001253 // long line wrapping, adjust row
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001254 if (wp->w_p_wrap
1255 && col >= (colnr_T)wp->w_width
1256 && width > 0)
1257 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001258 // use same formula as what is used in curs_columns()
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001259 rowoff = ((col - wp->w_width) / width + 1);
1260 col -= rowoff * width;
1261 }
1262 col -= wp->w_leftcol;
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +01001263 if (col >= wp->w_width)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001264 col = -1;
Bram Moolenaar189663b2021-07-21 18:04:56 +02001265 if (col >= 0 && row + rowoff <= wp->w_height)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001266 coloff = col - scol + wp->w_wincol + 1;
1267 else
Bram Moolenaar189663b2021-07-21 18:04:56 +02001268 // character is left, right or below of the window
1269 row = rowoff = scol = ccol = ecol = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001270 }
Bram Moolenaar8dd46e72020-12-17 21:35:29 +01001271 *rowp = W_WINROW(wp) + row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001272 *scolp = scol + coloff;
1273 *ccolp = ccol + coloff;
1274 *ecolp = ecol + coloff;
1275}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001276#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001277
Bram Moolenaar12034e22019-08-25 22:25:02 +02001278#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001279/*
1280 * "screenpos({winid}, {lnum}, {col})" function
1281 */
1282 void
1283f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1284{
1285 dict_T *dict;
1286 win_T *wp;
1287 pos_T pos;
1288 int row = 0;
1289 int scol = 0, ccol = 0, ecol = 0;
1290
1291 if (rettv_dict_alloc(rettv) != OK)
1292 return;
1293 dict = rettv->vval.v_dict;
1294
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001295 if (in_vim9script()
1296 && (check_for_number_arg(argvars, 0) == FAIL
1297 || check_for_number_arg(argvars, 1) == FAIL
1298 || check_for_number_arg(argvars, 2) == FAIL))
1299 return;
1300
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001301 wp = find_win_by_nr_or_id(&argvars[0]);
1302 if (wp == NULL)
1303 return;
1304
1305 pos.lnum = tv_get_number(&argvars[1]);
1306 pos.col = tv_get_number(&argvars[2]) - 1;
1307 pos.coladd = 0;
1308 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1309
1310 dict_add_number(dict, "row", row);
1311 dict_add_number(dict, "col", scol);
1312 dict_add_number(dict, "curscol", ccol);
1313 dict_add_number(dict, "endcol", ecol);
1314}
1315#endif
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317/*
1318 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001321scrolldown(
1322 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001323 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001325 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 int wrow;
1327 int moved = FALSE;
1328
1329#ifdef FEAT_FOLDING
1330 linenr_T first;
1331
Bram Moolenaar85a20022019-12-21 18:25:54 +01001332 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1334#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001335 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 while (line_count-- > 0)
1337 {
1338#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001339 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1340 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 ++curwin->w_topfill;
1343 ++done;
1344 }
1345 else
1346#endif
1347 {
1348 if (curwin->w_topline == 1)
1349 break;
1350 --curwin->w_topline;
1351#ifdef FEAT_DIFF
1352 curwin->w_topfill = 0;
1353#endif
1354#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001355 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (hasFolding(curwin->w_topline, &first, NULL))
1357 {
1358 ++done;
1359 if (!byfold)
1360 line_count -= curwin->w_topline - first - 1;
1361 curwin->w_botline -= curwin->w_topline - first;
1362 curwin->w_topline = first;
1363 }
1364 else
1365#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001366 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001368 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 invalidate_botline();
1370 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001371 curwin->w_wrow += done; // keep w_wrow updated
1372 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
1374#ifdef FEAT_DIFF
1375 if (curwin->w_cursor.lnum == curwin->w_topline)
1376 curwin->w_cline_row = 0;
1377 check_topfill(curwin, TRUE);
1378#endif
1379
1380 /*
1381 * Compute the row number of the last row of the cursor line
1382 * and move the cursor onto the displayed part of the window.
1383 */
1384 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001385 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 {
1387 validate_virtcol();
1388 validate_cheight();
1389 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001390 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1393 {
1394#ifdef FEAT_FOLDING
1395 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1396 {
1397 --wrow;
1398 if (first == 1)
1399 curwin->w_cursor.lnum = 1;
1400 else
1401 curwin->w_cursor.lnum = first - 1;
1402 }
1403 else
1404#endif
1405 wrow -= plines(curwin->w_cursor.lnum--);
1406 curwin->w_valid &=
1407 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1408 moved = TRUE;
1409 }
1410 if (moved)
1411 {
1412#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001413 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 foldAdjustCursor();
1415#endif
1416 coladvance(curwin->w_curswant);
1417 }
1418}
1419
1420/*
1421 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001424scrollup(
1425 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001426 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1429 linenr_T lnum;
1430
1431 if (
1432# ifdef FEAT_FOLDING
1433 (byfold && hasAnyFolding(curwin))
1434# ifdef FEAT_DIFF
1435 ||
1436# endif
1437# endif
1438# ifdef FEAT_DIFF
1439 curwin->w_p_diff
1440# endif
1441 )
1442 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001443 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 lnum = curwin->w_topline;
1445 while (line_count--)
1446 {
1447# ifdef FEAT_DIFF
1448 if (curwin->w_topfill > 0)
1449 --curwin->w_topfill;
1450 else
1451# endif
1452 {
1453# ifdef FEAT_FOLDING
1454 if (byfold)
1455 (void)hasFolding(lnum, NULL, &lnum);
1456# endif
1457 if (lnum >= curbuf->b_ml.ml_line_count)
1458 break;
1459 ++lnum;
1460# ifdef FEAT_DIFF
1461 curwin->w_topfill = diff_check_fill(curwin, lnum);
1462# endif
1463 }
1464 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001465 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 curwin->w_botline += lnum - curwin->w_topline;
1467 curwin->w_topline = lnum;
1468 }
1469 else
1470#endif
1471 {
1472 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001473 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 }
1475
1476 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1477 curwin->w_topline = curbuf->b_ml.ml_line_count;
1478 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1479 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1480
1481#ifdef FEAT_DIFF
1482 check_topfill(curwin, FALSE);
1483#endif
1484
1485#ifdef FEAT_FOLDING
1486 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001487 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1489#endif
1490
1491 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1492 if (curwin->w_cursor.lnum < curwin->w_topline)
1493 {
1494 curwin->w_cursor.lnum = curwin->w_topline;
1495 curwin->w_valid &=
1496 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1497 coladvance(curwin->w_curswant);
1498 }
1499}
1500
1501#ifdef FEAT_DIFF
1502/*
1503 * Don't end up with too many filler lines in the window.
1504 */
1505 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001506check_topfill(
1507 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001508 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 int n;
1511
1512 if (wp->w_topfill > 0)
1513 {
1514 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1515 if (wp->w_topfill + n > wp->w_height)
1516 {
1517 if (down && wp->w_topline > 1)
1518 {
1519 --wp->w_topline;
1520 wp->w_topfill = 0;
1521 }
1522 else
1523 {
1524 wp->w_topfill = wp->w_height - n;
1525 if (wp->w_topfill < 0)
1526 wp->w_topfill = 0;
1527 }
1528 }
1529 }
1530}
1531
1532/*
1533 * Use as many filler lines as possible for w_topline. Make sure w_topline
1534 * is still visible.
1535 */
1536 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001537max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 int n;
1540
1541 n = plines_nofill(curwin->w_topline);
1542 if (n >= curwin->w_height)
1543 curwin->w_topfill = 0;
1544 else
1545 {
1546 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1547 if (curwin->w_topfill + n > curwin->w_height)
1548 curwin->w_topfill = curwin->w_height - n;
1549 }
1550}
1551#endif
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553/*
1554 * Scroll the screen one line down, but don't do it if it would move the
1555 * cursor off the screen.
1556 */
1557 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001558scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560 int end_row;
1561#ifdef FEAT_DIFF
1562 int can_fill = (curwin->w_topfill
1563 < diff_check_fill(curwin, curwin->w_topline));
1564#endif
1565
1566 if (curwin->w_topline <= 1
1567#ifdef FEAT_DIFF
1568 && !can_fill
1569#endif
1570 )
1571 return;
1572
Bram Moolenaar85a20022019-12-21 18:25:54 +01001573 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 /*
1576 * Compute the row number of the last row of the cursor line
1577 * and make sure it doesn't go off the screen. Make sure the cursor
1578 * doesn't go past 'scrolloff' lines from the screen end.
1579 */
1580 end_row = curwin->w_wrow;
1581#ifdef FEAT_DIFF
1582 if (can_fill)
1583 ++end_row;
1584 else
1585 end_row += plines_nofill(curwin->w_topline - 1);
1586#else
1587 end_row += plines(curwin->w_topline - 1);
1588#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001589 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 validate_cheight();
1592 validate_virtcol();
1593 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001594 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001596 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598#ifdef FEAT_DIFF
1599 if (can_fill)
1600 {
1601 ++curwin->w_topfill;
1602 check_topfill(curwin, TRUE);
1603 }
1604 else
1605 {
1606 --curwin->w_topline;
1607 curwin->w_topfill = 0;
1608 }
1609#else
1610 --curwin->w_topline;
1611#endif
1612#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001613 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001615 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1617 }
1618}
1619
1620/*
1621 * Scroll the screen one line up, but don't do it if it would move the cursor
1622 * off the screen.
1623 */
1624 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001625scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
1627 int start_row;
1628
1629 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1630#ifdef FEAT_DIFF
1631 && curwin->w_topfill == 0
1632#endif
1633 )
1634 return;
1635
Bram Moolenaar85a20022019-12-21 18:25:54 +01001636 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 /*
1639 * Compute the row number of the first row of the cursor line
1640 * and make sure it doesn't go off the screen. Make sure the cursor
1641 * doesn't go before 'scrolloff' lines from the screen start.
1642 */
1643#ifdef FEAT_DIFF
1644 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1645 - curwin->w_topfill;
1646#else
1647 start_row = curwin->w_wrow - plines(curwin->w_topline);
1648#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001649 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001652 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001654 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 {
1656#ifdef FEAT_DIFF
1657 if (curwin->w_topfill > 0)
1658 --curwin->w_topfill;
1659 else
1660#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001661 {
1662#ifdef FEAT_FOLDING
1663 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001666 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001667 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1669 }
1670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672/*
1673 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1674 * a (wrapped) text line. Uses and sets "lp->fill".
1675 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001676 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 */
1678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001679topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680{
1681#ifdef FEAT_DIFF
1682 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1683 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001684 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 ++lp->fill;
1686 lp->height = 1;
1687 }
1688 else
1689#endif
1690 {
1691 --lp->lnum;
1692#ifdef FEAT_DIFF
1693 lp->fill = 0;
1694#endif
1695 if (lp->lnum < 1)
1696 lp->height = MAXCOL;
1697 else
1698#ifdef FEAT_FOLDING
1699 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001700 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 lp->height = 1;
1702 else
1703#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001704 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
1706}
1707
1708/*
1709 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1710 * a (wrapped) text line. Uses and sets "lp->fill".
1711 * Returns the height of the added line in "lp->height".
1712 * Lines below the last one are incredibly high.
1713 */
1714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001715botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717#ifdef FEAT_DIFF
1718 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1719 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001720 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 ++lp->fill;
1722 lp->height = 1;
1723 }
1724 else
1725#endif
1726 {
1727 ++lp->lnum;
1728#ifdef FEAT_DIFF
1729 lp->fill = 0;
1730#endif
1731 if (lp->lnum > curbuf->b_ml.ml_line_count)
1732 lp->height = MAXCOL;
1733 else
1734#ifdef FEAT_FOLDING
1735 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001736 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 lp->height = 1;
1738 else
1739#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001740 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
1742}
1743
1744#ifdef FEAT_DIFF
1745/*
1746 * Switch from including filler lines below lp->lnum to including filler
1747 * lines above loff.lnum + 1. This keeps pointing to the same line.
1748 * When there are no filler lines nothing changes.
1749 */
1750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001751botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
1753 if (lp->fill > 0)
1754 {
1755 ++lp->lnum;
1756 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1757 }
1758}
1759
1760/*
1761 * Switch from including filler lines above lp->lnum to including filler
1762 * lines below loff.lnum - 1. This keeps pointing to the same line.
1763 * When there are no filler lines nothing changes.
1764 */
1765 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001766topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 if (lp->fill > 0)
1769 {
1770 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1771 --lp->lnum;
1772 }
1773}
1774#endif
1775
1776/*
1777 * Recompute topline to put the cursor at the top of the window.
1778 * Scroll at least "min_scroll" lines.
1779 * If "always" is TRUE, always set topline (for "zt").
1780 */
1781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001782scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
1784 int scrolled = 0;
1785 int extra = 0;
1786 int used;
1787 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001788 linenr_T top; // just above displayed lines
1789 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 linenr_T old_topline = curwin->w_topline;
1791#ifdef FEAT_DIFF
1792 linenr_T old_topfill = curwin->w_topfill;
1793#endif
1794 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001795 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (mouse_dragging > 0)
1798 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 /*
1801 * Decrease topline until:
1802 * - it has become 1
1803 * - (part of) the cursor line is moved off the screen or
1804 * - moved at least 'scrolljump' lines and
1805 * - at least 'scrolloff' lines above and below the cursor
1806 */
1807 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001808 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (curwin->w_cursor.lnum < curwin->w_topline)
1810 scrolled = used;
1811
1812#ifdef FEAT_FOLDING
1813 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1814 {
1815 --top;
1816 ++bot;
1817 }
1818 else
1819#endif
1820 {
1821 top = curwin->w_cursor.lnum - 1;
1822 bot = curwin->w_cursor.lnum + 1;
1823 }
1824 new_topline = top + 1;
1825
1826#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001827 // "used" already contains the number of filler lines above, don't add it
1828 // again.
1829 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001830 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#endif
1832
1833 /*
1834 * Check if the lines from "top" to "bot" fit in the window. If they do,
1835 * set new_topline and advance "top" and "bot" to include more lines.
1836 */
1837 while (top > 0)
1838 {
1839#ifdef FEAT_FOLDING
1840 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001841 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 i = 1;
1843 else
1844#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001845 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 used += i;
1847 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1848 {
1849#ifdef FEAT_FOLDING
1850 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001851 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 ++used;
1853 else
1854#endif
1855 used += plines(bot);
1856 }
1857 if (used > curwin->w_height)
1858 break;
1859 if (top < curwin->w_topline)
1860 scrolled += i;
1861
1862 /*
1863 * If scrolling is needed, scroll at least 'sj' lines.
1864 */
1865 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1866 && extra >= off)
1867 break;
1868
1869 extra += i;
1870 new_topline = top;
1871 --top;
1872 ++bot;
1873 }
1874
1875 /*
1876 * If we don't have enough space, put cursor in the middle.
1877 * This makes sure we get the same position when using "k" and "j"
1878 * in a small window.
1879 */
1880 if (used > curwin->w_height)
1881 scroll_cursor_halfway(FALSE);
1882 else
1883 {
1884 /*
1885 * If "always" is FALSE, only adjust topline to a lower value, higher
1886 * value may happen with wrapping lines
1887 */
1888 if (new_topline < curwin->w_topline || always)
1889 curwin->w_topline = new_topline;
1890 if (curwin->w_topline > curwin->w_cursor.lnum)
1891 curwin->w_topline = curwin->w_cursor.lnum;
1892#ifdef FEAT_DIFF
1893 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1894 if (curwin->w_topfill > 0 && extra > off)
1895 {
1896 curwin->w_topfill -= extra - off;
1897 if (curwin->w_topfill < 0)
1898 curwin->w_topfill = 0;
1899 }
1900 check_topfill(curwin, FALSE);
1901#endif
1902 if (curwin->w_topline != old_topline
1903#ifdef FEAT_DIFF
1904 || curwin->w_topfill != old_topfill
1905#endif
1906 )
1907 curwin->w_valid &=
1908 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1909 curwin->w_valid |= VALID_TOPLINE;
1910 }
1911}
1912
1913/*
1914 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1915 * screen lines for text lines.
1916 */
1917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001918set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920#ifdef FEAT_DIFF
1921 wp->w_filler_rows = 0;
1922#endif
1923 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001924 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 else
1926 {
1927 wp->w_empty_rows = wp->w_height - used;
1928#ifdef FEAT_DIFF
1929 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1930 {
1931 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1932 if (wp->w_empty_rows > wp->w_filler_rows)
1933 wp->w_empty_rows -= wp->w_filler_rows;
1934 else
1935 {
1936 wp->w_filler_rows = wp->w_empty_rows;
1937 wp->w_empty_rows = 0;
1938 }
1939 }
1940#endif
1941 }
1942}
1943
1944/*
1945 * Recompute topline to put the cursor at the bottom of the window.
1946 * Scroll at least "min_scroll" lines.
1947 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1948 * This is messy stuff!!!
1949 */
1950 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001951scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
1953 int used;
1954 int scrolled = 0;
1955 int extra = 0;
1956 int i;
1957 linenr_T line_count;
1958 linenr_T old_topline = curwin->w_topline;
1959 lineoff_T loff;
1960 lineoff_T boff;
1961#ifdef FEAT_DIFF
1962 int old_topfill = curwin->w_topfill;
1963 int fill_below_window;
1964#endif
1965 linenr_T old_botline = curwin->w_botline;
1966 linenr_T old_valid = curwin->w_valid;
1967 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001968 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001969 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
1971 cln = curwin->w_cursor.lnum;
1972 if (set_topbot)
1973 {
1974 used = 0;
1975 curwin->w_botline = cln + 1;
1976#ifdef FEAT_DIFF
1977 loff.fill = 0;
1978#endif
1979 for (curwin->w_topline = curwin->w_botline;
1980 curwin->w_topline > 1;
1981 curwin->w_topline = loff.lnum)
1982 {
1983 loff.lnum = curwin->w_topline;
1984 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001985 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 break;
1987 used += loff.height;
1988#ifdef FEAT_DIFF
1989 curwin->w_topfill = loff.fill;
1990#endif
1991 }
1992 set_empty_rows(curwin, used);
1993 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1994 if (curwin->w_topline != old_topline
1995#ifdef FEAT_DIFF
1996 || curwin->w_topfill != old_topfill
1997#endif
1998 )
1999 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2000 }
2001 else
2002 validate_botline();
2003
Bram Moolenaar85a20022019-12-21 18:25:54 +01002004 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005#ifdef FEAT_DIFF
2006 used = plines_nofill(cln);
2007#else
2008 validate_cheight();
2009 used = curwin->w_cline_height;
2010#endif
2011
Bram Moolenaar85a20022019-12-21 18:25:54 +01002012 // If the cursor is below botline, we will at least scroll by the height
2013 // of the cursor line. Correct for empty lines, which are really part of
2014 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (cln >= curwin->w_botline)
2016 {
2017 scrolled = used;
2018 if (cln == curwin->w_botline)
2019 scrolled -= curwin->w_empty_rows;
2020 }
2021
2022 /*
2023 * Stop counting lines to scroll when
2024 * - hitting start of the file
2025 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002026 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 * - lines between botline and cursor have been counted
2028 */
2029#ifdef FEAT_FOLDING
2030 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2031#endif
2032 {
2033 loff.lnum = cln;
2034 boff.lnum = cln;
2035 }
2036#ifdef FEAT_DIFF
2037 loff.fill = 0;
2038 boff.fill = 0;
2039 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2040 - curwin->w_filler_rows;
2041#endif
2042
2043 while (loff.lnum > 1)
2044 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002045 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2046 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002048 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2050 && loff.lnum <= curwin->w_botline
2051#ifdef FEAT_DIFF
2052 && (loff.lnum < curwin->w_botline
2053 || loff.fill >= fill_below_window)
2054#endif
2055 )
2056 break;
2057
Bram Moolenaar85a20022019-12-21 18:25:54 +01002058 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002060 if (loff.height == MAXCOL)
2061 used = MAXCOL;
2062 else
2063 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (used > curwin->w_height)
2065 break;
2066 if (loff.lnum >= curwin->w_botline
2067#ifdef FEAT_DIFF
2068 && (loff.lnum > curwin->w_botline
2069 || loff.fill <= fill_below_window)
2070#endif
2071 )
2072 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002073 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 scrolled += loff.height;
2075 if (loff.lnum == curwin->w_botline
2076#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002077 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#endif
2079 )
2080 scrolled -= curwin->w_empty_rows;
2081 }
2082
2083 if (boff.lnum < curbuf->b_ml.ml_line_count)
2084 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002085 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 botline_forw(&boff);
2087 used += boff.height;
2088 if (used > curwin->w_height)
2089 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002090 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2091 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 extra += boff.height;
2094 if (boff.lnum >= curwin->w_botline
2095#ifdef FEAT_DIFF
2096 || (boff.lnum + 1 == curwin->w_botline
2097 && boff.fill > curwin->w_filler_rows)
2098#endif
2099 )
2100 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002101 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 scrolled += boff.height;
2103 if (boff.lnum == curwin->w_botline
2104#ifdef FEAT_DIFF
2105 && boff.fill == 0
2106#endif
2107 )
2108 scrolled -= curwin->w_empty_rows;
2109 }
2110 }
2111 }
2112 }
2113
Bram Moolenaar85a20022019-12-21 18:25:54 +01002114 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 if (scrolled <= 0)
2116 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002117 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 else if (used > curwin->w_height)
2119 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002120 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 else
2122 {
2123 line_count = 0;
2124#ifdef FEAT_DIFF
2125 boff.fill = curwin->w_topfill;
2126#endif
2127 boff.lnum = curwin->w_topline - 1;
2128 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2129 {
2130 botline_forw(&boff);
2131 i += boff.height;
2132 ++line_count;
2133 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002134 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 line_count = 9999;
2136 }
2137
2138 /*
2139 * Scroll up if the cursor is off the bottom of the screen a bit.
2140 * Otherwise put it at 1/2 of the screen.
2141 */
2142 if (line_count >= curwin->w_height && line_count > min_scroll)
2143 scroll_cursor_halfway(FALSE);
2144 else
2145 scrollup(line_count, TRUE);
2146
2147 /*
2148 * If topline didn't change we need to restore w_botline and w_empty_rows
2149 * (we changed them).
2150 * If topline did change, update_screen() will set botline.
2151 */
2152 if (curwin->w_topline == old_topline && set_topbot)
2153 {
2154 curwin->w_botline = old_botline;
2155 curwin->w_empty_rows = old_empty_rows;
2156 curwin->w_valid = old_valid;
2157 }
2158 curwin->w_valid |= VALID_TOPLINE;
2159}
2160
2161/*
2162 * Recompute topline to put the cursor halfway the window
2163 * If "atend" is TRUE, also put it halfway at the end of the file.
2164 */
2165 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002166scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 int above = 0;
2169 linenr_T topline;
2170#ifdef FEAT_DIFF
2171 int topfill = 0;
2172#endif
2173 int below = 0;
2174 int used;
2175 lineoff_T loff;
2176 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002177#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002178 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002181#ifdef FEAT_PROP_POPUP
2182 // if the width changed this needs to be updated first
2183 may_update_popup_position();
2184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2186#ifdef FEAT_FOLDING
2187 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2188#endif
2189#ifdef FEAT_DIFF
2190 used = plines_nofill(loff.lnum);
2191 loff.fill = 0;
2192 boff.fill = 0;
2193#else
2194 used = plines(loff.lnum);
2195#endif
2196 topline = loff.lnum;
2197 while (topline > 1)
2198 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002199 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
2201 if (boff.lnum < curbuf->b_ml.ml_line_count)
2202 {
2203 botline_forw(&boff);
2204 used += boff.height;
2205 if (used > curwin->w_height)
2206 break;
2207 below += boff.height;
2208 }
2209 else
2210 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (atend)
2213 ++used;
2214 }
2215 }
2216
Bram Moolenaar85a20022019-12-21 18:25:54 +01002217 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002220 if (loff.height == MAXCOL)
2221 used = MAXCOL;
2222 else
2223 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (used > curwin->w_height)
2225 break;
2226 above += loff.height;
2227 topline = loff.lnum;
2228#ifdef FEAT_DIFF
2229 topfill = loff.fill;
2230#endif
2231 }
2232 }
2233#ifdef FEAT_FOLDING
2234 if (!hasFolding(topline, &curwin->w_topline, NULL))
2235#endif
2236 curwin->w_topline = topline;
2237#ifdef FEAT_DIFF
2238 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002239 if (old_topline > curwin->w_topline + curwin->w_height)
2240 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 check_topfill(curwin, FALSE);
2242#endif
2243 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2244 curwin->w_valid |= VALID_TOPLINE;
2245}
2246
2247/*
2248 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002249 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 * If not possible, put it at the same position as scroll_cursor_halfway().
2251 * When called topline must be valid!
2252 */
2253 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002254cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002256 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002258 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 linenr_T botline;
2260 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002261 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002263 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
2265 /*
2266 * How many lines we would like to have above/below the cursor depends on
2267 * whether the first/last line of the file is on screen.
2268 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002269 above_wanted = so;
2270 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002271 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 above_wanted = mouse_dragging - 1;
2274 below_wanted = mouse_dragging - 1;
2275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (curwin->w_topline == 1)
2277 {
2278 above_wanted = 0;
2279 max_off = curwin->w_height / 2;
2280 if (below_wanted > max_off)
2281 below_wanted = max_off;
2282 }
2283 validate_botline();
2284 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002285 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 below_wanted = 0;
2288 max_off = (curwin->w_height - 1) / 2;
2289 if (above_wanted > max_off)
2290 above_wanted = max_off;
2291 }
2292
2293 /*
2294 * If there are sufficient file-lines above and below the cursor, we can
2295 * return now.
2296 */
2297 cln = curwin->w_cursor.lnum;
2298 if (cln >= curwin->w_topline + above_wanted
2299 && cln < curwin->w_botline - below_wanted
2300#ifdef FEAT_FOLDING
2301 && !hasAnyFolding(curwin)
2302#endif
2303 )
2304 return;
2305
2306 /*
2307 * Narrow down the area where the cursor can be put by taking lines from
2308 * the top and the bottom until:
2309 * - the desired context lines are found
2310 * - the lines from the top is past the lines from the bottom
2311 */
2312 topline = curwin->w_topline;
2313 botline = curwin->w_botline - 1;
2314#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002315 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 above = curwin->w_topfill;
2317 below = curwin->w_filler_rows;
2318#endif
2319 while ((above < above_wanted || below < below_wanted) && topline < botline)
2320 {
2321 if (below < below_wanted && (below <= above || above >= above_wanted))
2322 {
2323#ifdef FEAT_FOLDING
2324 if (hasFolding(botline, &botline, NULL))
2325 ++below;
2326 else
2327#endif
2328 below += plines(botline);
2329 --botline;
2330 }
2331 if (above < above_wanted && (above < below || below >= below_wanted))
2332 {
2333#ifdef FEAT_FOLDING
2334 if (hasFolding(topline, NULL, &topline))
2335 ++above;
2336 else
2337#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002338 above += PLINES_NOFILL(topline);
2339#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002340 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (topline < botline)
2342 above += diff_check_fill(curwin, topline + 1);
2343#endif
2344 ++topline;
2345 }
2346 }
2347 if (topline == botline || botline == 0)
2348 curwin->w_cursor.lnum = topline;
2349 else if (topline > botline)
2350 curwin->w_cursor.lnum = botline;
2351 else
2352 {
2353 if (cln < topline && curwin->w_topline > 1)
2354 {
2355 curwin->w_cursor.lnum = topline;
2356 curwin->w_valid &=
2357 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2358 }
2359 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2360 {
2361 curwin->w_cursor.lnum = botline;
2362 curwin->w_valid &=
2363 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2364 }
2365 }
2366 curwin->w_valid |= VALID_TOPLINE;
2367}
2368
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002369static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371/*
2372 * move screen 'count' pages up or down and update screen
2373 *
2374 * return FAIL for failure, OK otherwise
2375 */
2376 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002377onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 long n;
2380 int retval = OK;
2381 lineoff_T loff;
2382 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002383 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaar85a20022019-12-21 18:25:54 +01002385 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
2387 beep_flush();
2388 return FAIL;
2389 }
2390
2391 for ( ; count > 0; --count)
2392 {
2393 validate_botline();
2394 /*
2395 * It's an error to move a page up when the first line is already on
2396 * the screen. It's an error to move a page down when the last line
2397 * is on the screen and the topline is 'scrolloff' lines from the
2398 * last line.
2399 */
2400 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002401 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2403 : (curwin->w_topline == 1
2404#ifdef FEAT_DIFF
2405 && curwin->w_topfill ==
2406 diff_check_fill(curwin, curwin->w_topline)
2407#endif
2408 ))
2409 {
2410 beep_flush();
2411 retval = FAIL;
2412 break;
2413 }
2414
2415#ifdef FEAT_DIFF
2416 loff.fill = 0;
2417#endif
2418 if (dir == FORWARD)
2419 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002420 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002422 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002423 if (p_window <= 2)
2424 ++curwin->w_topline;
2425 else
2426 curwin->w_topline += p_window - 2;
2427 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2428 curwin->w_topline = curbuf->b_ml.ml_line_count;
2429 curwin->w_cursor.lnum = curwin->w_topline;
2430 }
2431 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2432 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002433 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 curwin->w_topline = curbuf->b_ml.ml_line_count;
2435#ifdef FEAT_DIFF
2436 curwin->w_topfill = 0;
2437#endif
2438 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2439 }
2440 else
2441 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002442 // For the overlap, start with the line just below the window
2443 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 loff.lnum = curwin->w_botline;
2445#ifdef FEAT_DIFF
2446 loff.fill = diff_check_fill(curwin, loff.lnum)
2447 - curwin->w_filler_rows;
2448#endif
2449 get_scroll_overlap(&loff, -1);
2450 curwin->w_topline = loff.lnum;
2451#ifdef FEAT_DIFF
2452 curwin->w_topfill = loff.fill;
2453 check_topfill(curwin, FALSE);
2454#endif
2455 curwin->w_cursor.lnum = curwin->w_topline;
2456 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2457 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2458 }
2459 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002460 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462#ifdef FEAT_DIFF
2463 if (curwin->w_topline == 1)
2464 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002465 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 max_topfill();
2467 continue;
2468 }
2469#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002470 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002471 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002472 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002473 if (p_window <= 2)
2474 --curwin->w_topline;
2475 else
2476 curwin->w_topline -= p_window - 2;
2477 if (curwin->w_topline < 1)
2478 curwin->w_topline = 1;
2479 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2480 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2481 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2482 continue;
2483 }
2484
Bram Moolenaar85a20022019-12-21 18:25:54 +01002485 // Find the line at the top of the window that is going to be the
2486 // line at the bottom of the window. Make sure this results in
2487 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 loff.lnum = curwin->w_topline - 1;
2489#ifdef FEAT_DIFF
2490 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2491 - curwin->w_topfill;
2492#endif
2493 get_scroll_overlap(&loff, 1);
2494
2495 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2496 {
2497 loff.lnum = curbuf->b_ml.ml_line_count;
2498#ifdef FEAT_DIFF
2499 loff.fill = 0;
2500 }
2501 else
2502 {
2503 botline_topline(&loff);
2504#endif
2505 }
2506 curwin->w_cursor.lnum = loff.lnum;
2507
Bram Moolenaar85a20022019-12-21 18:25:54 +01002508 // Find the line just above the new topline to get the right line
2509 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 n = 0;
2511 while (n <= curwin->w_height && loff.lnum >= 1)
2512 {
2513 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002514 if (loff.height == MAXCOL)
2515 n = MAXCOL;
2516 else
2517 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002519 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 curwin->w_topline = 1;
2522#ifdef FEAT_DIFF
2523 max_topfill();
2524#endif
2525 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2526 }
2527 else
2528 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002529 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530#ifdef FEAT_DIFF
2531 topline_botline(&loff);
2532#endif
2533 botline_forw(&loff);
2534 botline_forw(&loff);
2535#ifdef FEAT_DIFF
2536 botline_topline(&loff);
2537#endif
2538#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002539 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2541#endif
2542
Bram Moolenaar85a20022019-12-21 18:25:54 +01002543 // Always scroll at least one line. Avoid getting stuck on
2544 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if (loff.lnum >= curwin->w_topline
2546#ifdef FEAT_DIFF
2547 && (loff.lnum > curwin->w_topline
2548 || loff.fill >= curwin->w_topfill)
2549#endif
2550 )
2551 {
2552#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002553 // First try using the maximum number of filler lines. If
2554 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 loff.fill = curwin->w_topfill;
2556 if (curwin->w_topfill < diff_check_fill(curwin,
2557 curwin->w_topline))
2558 max_topfill();
2559 if (curwin->w_topfill == loff.fill)
2560#endif
2561 {
2562 --curwin->w_topline;
2563#ifdef FEAT_DIFF
2564 curwin->w_topfill = 0;
2565#endif
2566 }
2567 comp_botline(curwin);
2568 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002569 curwin->w_valid &=
2570 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572 else
2573 {
2574 curwin->w_topline = loff.lnum;
2575#ifdef FEAT_DIFF
2576 curwin->w_topfill = loff.fill;
2577 check_topfill(curwin, FALSE);
2578#endif
2579 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2580 }
2581 }
2582 }
2583 }
2584#ifdef FEAT_FOLDING
2585 foldAdjustCursor();
2586#endif
2587 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002588 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002589 if (retval == OK)
2590 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2592
Bram Moolenaar907dad72018-07-10 15:07:15 +02002593 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002595 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2596 // But make sure we scroll at least one line (happens with mix of long
2597 // wrapping lines and non-wrapping line).
2598 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002600 scroll_cursor_top(1, FALSE);
2601 if (curwin->w_topline <= old_topline
2602 && old_topline < curbuf->b_ml.ml_line_count)
2603 {
2604 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002606 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2607#endif
2608 }
2609 }
2610#ifdef FEAT_FOLDING
2611 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615
2616 redraw_later(VALID);
2617 return retval;
2618}
2619
2620/*
2621 * Decide how much overlap to use for page-up or page-down scrolling.
2622 * This is symmetric, so that doing both keeps the same lines displayed.
2623 * Three lines are examined:
2624 *
2625 * before CTRL-F after CTRL-F / before CTRL-B
2626 * etc. l1
2627 * l1 last but one line ------------
2628 * l2 last text line l2 top text line
2629 * ------------- l3 second text line
2630 * l3 etc.
2631 */
2632 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002633get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
2635 int h1, h2, h3, h4;
2636 int min_height = curwin->w_height - 2;
2637 lineoff_T loff0, loff1, loff2;
2638
2639#ifdef FEAT_DIFF
2640 if (lp->fill > 0)
2641 lp->height = 1;
2642 else
2643 lp->height = plines_nofill(lp->lnum);
2644#else
2645 lp->height = plines(lp->lnum);
2646#endif
2647 h1 = lp->height;
2648 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002649 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 loff0 = *lp;
2652 if (dir > 0)
2653 botline_forw(lp);
2654 else
2655 topline_back(lp);
2656 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002657 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002659 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 return;
2661 }
2662
2663 loff1 = *lp;
2664 if (dir > 0)
2665 botline_forw(lp);
2666 else
2667 topline_back(lp);
2668 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002669 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002671 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 return;
2673 }
2674
2675 loff2 = *lp;
2676 if (dir > 0)
2677 botline_forw(lp);
2678 else
2679 topline_back(lp);
2680 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002681 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002682 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002684 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 return;
2686}
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688/*
2689 * Scroll 'scroll' lines up or down.
2690 */
2691 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002692halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 long scrolled = 0;
2695 int i;
2696 int n;
2697 int room;
2698
2699 if (Prenum)
2700 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2701 curwin->w_height : Prenum;
2702 n = (curwin->w_p_scr <= curwin->w_height) ?
2703 curwin->w_p_scr : curwin->w_height;
2704
Bram Moolenaard5d37532017-03-27 23:02:07 +02002705 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 validate_botline();
2707 room = curwin->w_empty_rows;
2708#ifdef FEAT_DIFF
2709 room += curwin->w_filler_rows;
2710#endif
2711 if (flag)
2712 {
2713 /*
2714 * scroll the text up
2715 */
2716 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2717 {
2718#ifdef FEAT_DIFF
2719 if (curwin->w_topfill > 0)
2720 {
2721 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002722 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 --curwin->w_topfill;
2724 }
2725 else
2726#endif
2727 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002728 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 n -= i;
2730 if (n < 0 && scrolled > 0)
2731 break;
2732#ifdef FEAT_FOLDING
2733 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2734#endif
2735 ++curwin->w_topline;
2736#ifdef FEAT_DIFF
2737 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2738#endif
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2741 {
2742 ++curwin->w_cursor.lnum;
2743 curwin->w_valid &=
2744 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
2747 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2748 scrolled += i;
2749
2750 /*
2751 * Correct w_botline for changed w_topline.
2752 * Won't work when there are filler lines.
2753 */
2754#ifdef FEAT_DIFF
2755 if (curwin->w_p_diff)
2756 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2757 else
2758#endif
2759 {
2760 room += i;
2761 do
2762 {
2763 i = plines(curwin->w_botline);
2764 if (i > room)
2765 break;
2766#ifdef FEAT_FOLDING
2767 (void)hasFolding(curwin->w_botline, NULL,
2768 &curwin->w_botline);
2769#endif
2770 ++curwin->w_botline;
2771 room -= i;
2772 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2773 }
2774 }
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 /*
2777 * When hit bottom of the file: move cursor down.
2778 */
2779 if (n > 0)
2780 {
2781# ifdef FEAT_FOLDING
2782 if (hasAnyFolding(curwin))
2783 {
2784 while (--n >= 0
2785 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2786 {
2787 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2788 &curwin->w_cursor.lnum);
2789 ++curwin->w_cursor.lnum;
2790 }
2791 }
2792 else
2793# endif
2794 curwin->w_cursor.lnum += n;
2795 check_cursor_lnum();
2796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798 else
2799 {
2800 /*
2801 * scroll the text down
2802 */
2803 while (n > 0 && curwin->w_topline > 1)
2804 {
2805#ifdef FEAT_DIFF
2806 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2807 {
2808 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002809 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 ++curwin->w_topfill;
2811 }
2812 else
2813#endif
2814 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002815 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 n -= i;
2817 if (n < 0 && scrolled > 0)
2818 break;
2819 --curwin->w_topline;
2820#ifdef FEAT_FOLDING
2821 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2822#endif
2823#ifdef FEAT_DIFF
2824 curwin->w_topfill = 0;
2825#endif
2826 }
2827 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2828 VALID_BOTLINE|VALID_BOTLINE_AP);
2829 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (curwin->w_cursor.lnum > 1)
2831 {
2832 --curwin->w_cursor.lnum;
2833 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002836
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 /*
2838 * When hit top of the file: move cursor up.
2839 */
2840 if (n > 0)
2841 {
2842 if (curwin->w_cursor.lnum <= (linenr_T)n)
2843 curwin->w_cursor.lnum = 1;
2844 else
2845# ifdef FEAT_FOLDING
2846 if (hasAnyFolding(curwin))
2847 {
2848 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2849 {
2850 --curwin->w_cursor.lnum;
2851 (void)hasFolding(curwin->w_cursor.lnum,
2852 &curwin->w_cursor.lnum, NULL);
2853 }
2854 }
2855 else
2856# endif
2857 curwin->w_cursor.lnum -= n;
2858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002861 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 foldAdjustCursor();
2863# endif
2864#ifdef FEAT_DIFF
2865 check_topfill(curwin, !flag);
2866#endif
2867 cursor_correct();
2868 beginline(BL_SOL | BL_FIX);
2869 redraw_later(VALID);
2870}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002871
Bram Moolenaar860cae12010-06-05 23:22:07 +02002872 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002873do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002874{
2875 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002876 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002877 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002878 colnr_T curswant = curwin->w_curswant;
2879 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002880 win_T *old_curwin = curwin;
2881 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002882 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002883 int old_VIsual_select = VIsual_select;
2884 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002885
2886 /*
2887 * loop through the cursorbound windows
2888 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002889 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002890 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002891 {
2892 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002893 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002894 if (curwin != old_curwin && curwin->w_p_crb)
2895 {
2896# ifdef FEAT_DIFF
2897 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002898 curwin->w_cursor.lnum =
2899 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002900 else
2901# endif
2902 curwin->w_cursor.lnum = line;
2903 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002904 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002905 curwin->w_curswant = curswant;
2906 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002907
Bram Moolenaar85a20022019-12-21 18:25:54 +01002908 // Make sure the cursor is in a valid position. Temporarily set
2909 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002910 restart_edit_save = restart_edit;
2911 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002912 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002913# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002914 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002915 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002916# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002917 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002918 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002919 if (has_mbyte)
2920 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002921 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002922
Bram Moolenaar85a20022019-12-21 18:25:54 +01002923 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002924 if (!curwin->w_p_scb)
2925 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002926 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002927 }
2928 }
2929
2930 /*
2931 * reset current-window
2932 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002933 VIsual_select = old_VIsual_select;
2934 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002935 curwin = old_curwin;
2936 curbuf = old_curbuf;
2937}