blob: 0a98f0347a2e7e9f9a3bcbdf6bcf95d90156ec0f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
39 * Compute wp->w_botline for the current wp->w_topline. Can be called after
40 * wp->w_topline changed.
41 */
42 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010043comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int n;
46 linenr_T lnum;
47 int done;
48#ifdef FEAT_FOLDING
49 linenr_T last;
50 int folded;
51#endif
52
53 /*
54 * If w_cline_row is valid, start there.
55 * Otherwise have to start at w_topline.
56 */
57 check_cursor_moved(wp);
58 if (wp->w_valid & VALID_CROW)
59 {
60 lnum = wp->w_cursor.lnum;
61 done = wp->w_cline_row;
62 }
63 else
64 {
65 lnum = wp->w_topline;
66 done = 0;
67 }
68
69 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
70 {
71#ifdef FEAT_FOLDING
72 last = lnum;
73 folded = FALSE;
74 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
75 {
76 n = 1;
77 folded = TRUE;
78 }
79 else
80#endif
81#ifdef FEAT_DIFF
82 if (lnum == wp->w_topline)
83 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
84 else
85#endif
86 n = plines_win(wp, lnum, TRUE);
87 if (
88#ifdef FEAT_FOLDING
89 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
90#else
91 lnum == wp->w_cursor.lnum
92#endif
93 )
94 {
95 wp->w_cline_row = done;
96 wp->w_cline_height = n;
97#ifdef FEAT_FOLDING
98 wp->w_cline_folded = folded;
99#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100100 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
102 }
103 if (done + n > wp->w_height)
104 break;
105 done += n;
106#ifdef FEAT_FOLDING
107 lnum = last;
108#endif
109 }
110
Bram Moolenaar85a20022019-12-21 18:25:54 +0100111 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
Bram Moolenaar90a99792018-09-12 21:52:18 +0200118#ifdef FEAT_SYN_HL
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200119 void
120reset_cursorline(void)
121{
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200122 curwin->w_last_cursorline = 0;
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200123}
Bram Moolenaar90a99792018-09-12 21:52:18 +0200124#endif
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100127 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
128 * set.
129 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100131redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100132{
133 if ((wp->w_p_rnu
134#ifdef FEAT_SYN_HL
135 || wp->w_p_cul
136#endif
137 )
138 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200139 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200140 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200141 if (wp->w_p_rnu)
142 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200143 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200144#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200145 if (wp->w_p_cul)
146 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200147 if (wp->w_redr_type <= VALID && wp->w_last_cursorline != 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200148 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200149 // "w_last_cursorline" may be outdated, worst case we redraw
150 // too much. This is optimized for moving the cursor around in
151 // the current window.
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100152 redrawWinline(wp, wp->w_last_cursorline);
153 redrawWinline(wp, wp->w_cursor.lnum);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200154 }
155 else
156 redraw_win_later(wp, SOME_VALID);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200157 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200158#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200159 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100160}
161
162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 * Update curwin->w_topline and redraw if necessary.
164 * Used to update the screen before printing a message.
165 */
166 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100167update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 update_topline();
170 if (must_redraw)
171 update_screen(0);
172}
173
174/*
175 * Update curwin->w_topline to move the cursor onto the screen.
176 */
177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100178update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
180 long line_count;
181 int halfheight;
182 int n;
183 linenr_T old_topline;
184#ifdef FEAT_DIFF
185 int old_topfill;
186#endif
187#ifdef FEAT_FOLDING
188 linenr_T lnum;
189#endif
190 int check_topline = FALSE;
191 int check_botline = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100192 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100193 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaar85a20022019-12-21 18:25:54 +0100195 // If there is no valid screen and when the window height is zero just use
196 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200197 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200198 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100199 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200200 curwin->w_topline = curwin->w_cursor.lnum;
201 curwin->w_botline = curwin->w_topline;
202 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200203 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200204 return;
205 }
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 check_cursor_moved(curwin);
208 if (curwin->w_valid & VALID_TOPLINE)
209 return;
210
Bram Moolenaar85a20022019-12-21 18:25:54 +0100211 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000212 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100213 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215 old_topline = curwin->w_topline;
216#ifdef FEAT_DIFF
217 old_topfill = curwin->w_topfill;
218#endif
219
220 /*
221 * If the buffer is empty, always set topline to 1.
222 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100223 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 {
225 if (curwin->w_topline != 1)
226 redraw_later(NOT_VALID);
227 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 curwin->w_botline = 2;
229 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 }
232
233 /*
234 * If the cursor is above or near the top of the window, scroll the window
235 * to show the line the cursor is in, with 'scrolloff' context.
236 */
237 else
238 {
239 if (curwin->w_topline > 1)
240 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100241 // If the cursor is above topline, scrolling is always needed.
242 // If the cursor is far below topline and there is no folding,
243 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 if (curwin->w_cursor.lnum < curwin->w_topline)
245 check_topline = TRUE;
246 else if (check_top_offset())
247 check_topline = TRUE;
248 }
249#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100250 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
252 curwin->w_topline))
253 check_topline = TRUE;
254#endif
255
256 if (check_topline)
257 {
258 halfheight = curwin->w_height / 2 - 1;
259 if (halfheight < 2)
260 halfheight = 2;
261
262#ifdef FEAT_FOLDING
263 if (hasAnyFolding(curwin))
264 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100265 // Count the number of logical lines between the cursor and
266 // topline + scrolloff (approximation of how much will be
267 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 n = 0;
269 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100270 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 {
272 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100273 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
275 break;
276 (void)hasFolding(lnum, NULL, &lnum);
277 }
278 }
279 else
280#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100281 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
Bram Moolenaar85a20022019-12-21 18:25:54 +0100283 // If we weren't very close to begin with, we scroll to put the
284 // cursor in the middle of the window. Otherwise put the cursor
285 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (n >= halfheight)
287 scroll_cursor_halfway(FALSE);
288 else
289 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000290 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 check_botline = TRUE;
292 }
293 }
294
295 else
296 {
297#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100298 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
300#endif
301 check_botline = TRUE;
302 }
303 }
304
305 /*
306 * If the cursor is below the bottom of the window, scroll the window
307 * to put the cursor on the window.
308 * When w_botline is invalid, recompute it first, to avoid a redraw later.
309 * If w_botline was approximated, we might need a redraw later in a few
310 * cases, but we don't want to spend (a lot of) time recomputing w_botline
311 * for every small change.
312 */
313 if (check_botline)
314 {
315 if (!(curwin->w_valid & VALID_BOTLINE_AP))
316 validate_botline();
317
318 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
319 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000320 if (curwin->w_cursor.lnum < curwin->w_botline)
321 {
322 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100323 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifdef FEAT_FOLDING
325 || hasAnyFolding(curwin)
326#endif
327 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 lineoff_T loff;
330
Bram Moolenaar85a20022019-12-21 18:25:54 +0100331 // Cursor is (a few lines) above botline, check if there are
332 // 'scrolloff' window lines below the cursor. If not, need to
333 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 n = curwin->w_empty_rows;
335 loff.lnum = curwin->w_cursor.lnum;
336#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100337 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
339#endif
340#ifdef FEAT_DIFF
341 loff.fill = 0;
342 n += curwin->w_filler_rows;
343#endif
344 loff.height = 0;
345 while (loff.lnum < curwin->w_botline
346#ifdef FEAT_DIFF
347 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
348#endif
349 )
350 {
351 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100352 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 break;
354 botline_forw(&loff);
355 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100356 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000359 }
360 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100361 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000362 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 }
364 if (check_botline)
365 {
366#ifdef FEAT_FOLDING
367 if (hasAnyFolding(curwin))
368 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100369 // Count the number of logical lines between the cursor and
370 // botline - scrolloff (approximation of how much will be
371 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 line_count = 0;
373 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100374 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {
376 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 if (lnum <= 0 || line_count > curwin->w_height + 1)
379 break;
380 (void)hasFolding(lnum, &lnum, NULL);
381 }
382 }
383 else
384#endif
385 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100386 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000388 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 else
390 scroll_cursor_halfway(FALSE);
391 }
392 }
393 }
394 curwin->w_valid |= VALID_TOPLINE;
395
396 /*
397 * Need to redraw when topline changed.
398 */
399 if (curwin->w_topline != old_topline
400#ifdef FEAT_DIFF
401 || curwin->w_topfill != old_topfill
402#endif
403 )
404 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100405 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000406 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {
408 curwin->w_skipcol = 0;
409 redraw_later(NOT_VALID);
410 }
411 else
412 redraw_later(VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100413 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (curwin->w_cursor.lnum == curwin->w_topline)
415 validate_cursor();
416 }
417
Bram Moolenaar375e3392019-01-31 18:26:10 +0100418 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419}
420
421/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000422 * Return the scrolljump value to use for the current window.
423 * When 'scrolljump' is positive use it as-is.
424 * When 'scrolljump' is negative use it as a percentage of the window height.
425 */
426 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100427scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000428{
429 if (p_sj >= 0)
430 return (int)p_sj;
431 return (curwin->w_height * -p_sj) / 100;
432}
433
434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
436 * current window.
437 */
438 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100439check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
441 lineoff_T loff;
442 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100443 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
Bram Moolenaar375e3392019-01-31 18:26:10 +0100445 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446#ifdef FEAT_FOLDING
447 || hasAnyFolding(curwin)
448#endif
449 )
450 {
451 loff.lnum = curwin->w_cursor.lnum;
452#ifdef FEAT_DIFF
453 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100454 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455#else
456 n = 0;
457#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100458 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100459 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 {
461 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100462 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 if (loff.lnum < curwin->w_topline
464#ifdef FEAT_DIFF
465 || (loff.lnum == curwin->w_topline && loff.fill > 0)
466#endif
467 )
468 break;
469 n += loff.height;
470 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100471 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 return TRUE;
473 }
474 return FALSE;
475}
476
477 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100478update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 if (curwin->w_set_curswant)
481 {
482 validate_virtcol();
483 curwin->w_curswant = curwin->w_virtcol;
484 curwin->w_set_curswant = FALSE;
485 }
486}
487
488/*
489 * Check if the cursor has moved. Set the w_valid flag accordingly.
490 */
491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
495 {
496 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
497 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
498 wp->w_valid_cursor = wp->w_cursor;
499 wp->w_valid_leftcol = wp->w_leftcol;
500 }
501 else if (wp->w_cursor.col != wp->w_valid_cursor.col
502 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100503 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 {
505 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
506 wp->w_valid_cursor.col = wp->w_cursor.col;
507 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 }
510}
511
512/*
513 * Call this function when some window settings have changed, which require
514 * the cursor position, botline and topline to be recomputed and the window to
515 * be redrawn. E.g, when changing the 'wrap' option or folding.
516 */
517 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100518changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 changed_window_setting_win(curwin);
521}
522
523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100524changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 wp->w_lines_valid = 0;
527 changed_line_abv_curs_win(wp);
528 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
529 redraw_win_later(wp, NOT_VALID);
530}
531
532/*
533 * Set wp->w_topline to a certain number.
534 */
535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100539 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
541#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100542 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 wp->w_botline += lnum - wp->w_topline;
544 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000545 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546#ifdef FEAT_DIFF
547 wp->w_topfill = 0;
548#endif
549 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100550 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 redraw_later(VALID);
552}
553
554/*
555 * Call this function when the length of the cursor line (in screen
556 * characters) has changed, and the change is before the cursor.
557 * Need to take care of w_botline separately!
558 */
559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100560changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
562 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
563 |VALID_CHEIGHT|VALID_TOPLINE);
564}
565
566 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100567changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
570 |VALID_CHEIGHT|VALID_TOPLINE);
571}
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573/*
574 * Call this function when the length of a line (in screen characters) above
575 * the cursor have changed.
576 * Need to take care of w_botline separately!
577 */
578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100579changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
581 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
582 |VALID_CHEIGHT|VALID_TOPLINE);
583}
584
585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100586changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
589 |VALID_CHEIGHT|VALID_TOPLINE);
590}
591
592/*
593 * Make sure the value of curwin->w_botline is valid.
594 */
595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100596validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 if (!(curwin->w_valid & VALID_BOTLINE))
599 comp_botline(curwin);
600}
601
602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * Mark curwin->w_botline as invalid (because of some change in the buffer).
604 */
605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100606invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
609}
610
611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100612invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
614 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
615}
616
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100618approximate_botline_win(
619 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 wp->w_valid &= ~VALID_BOTLINE;
622}
623
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624/*
625 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
626 */
627 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100628cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 check_cursor_moved(curwin);
631 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
632 (VALID_WROW|VALID_WCOL));
633}
634
635/*
636 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
637 * w_topline must be valid, you may need to call update_topline() first!
638 */
639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 check_cursor_moved(curwin);
643 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
644 curs_columns(TRUE);
645}
646
647#if defined(FEAT_GUI) || defined(PROTO)
648/*
649 * validate w_cline_row.
650 */
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 /*
655 * First make sure that w_topline is valid (after moving the cursor).
656 */
657 update_topline();
658 check_cursor_moved(curwin);
659 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100660 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661}
662#endif
663
664/*
665 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200666 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 */
668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100669curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 linenr_T lnum;
672 int i;
673 int all_invalid;
674 int valid;
675#ifdef FEAT_FOLDING
676 long fold_count;
677#endif
678
Bram Moolenaar85a20022019-12-21 18:25:54 +0100679 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 all_invalid = (!redrawing()
681 || wp->w_lines_valid == 0
682 || wp->w_lines[0].wl_lnum > wp->w_topline);
683 i = 0;
684 wp->w_cline_row = 0;
685 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
686 {
687 valid = FALSE;
688 if (!all_invalid && i < wp->w_lines_valid)
689 {
690 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100691 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 if (wp->w_lines[i].wl_lnum == lnum)
693 {
694#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100695 // Check for newly inserted lines below this row, in which
696 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 if (!wp->w_buffer->b_mod_set
698 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
699 || wp->w_buffer->b_mod_top
700 > wp->w_lines[i].wl_lastlnum + 1)
701#endif
702 valid = TRUE;
703 }
704 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100705 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707 if (valid
708#ifdef FEAT_DIFF
709 && (lnum != wp->w_topline || !wp->w_p_diff)
710#endif
711 )
712 {
713#ifdef FEAT_FOLDING
714 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100715 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (lnum > wp->w_cursor.lnum)
717 break;
718#else
719 ++lnum;
720#endif
721 wp->w_cline_row += wp->w_lines[i].wl_size;
722 }
723 else
724 {
725#ifdef FEAT_FOLDING
726 fold_count = foldedCount(wp, lnum, NULL);
727 if (fold_count)
728 {
729 lnum += fold_count;
730 if (lnum > wp->w_cursor.lnum)
731 break;
732 ++wp->w_cline_row;
733 }
734 else
735#endif
736#ifdef FEAT_DIFF
737 if (lnum == wp->w_topline)
738 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
739 + wp->w_topfill;
740 else
741#endif
742 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
743 }
744 }
745
746 check_cursor_moved(wp);
747 if (!(wp->w_valid & VALID_CHEIGHT))
748 {
749 if (all_invalid
750 || i == wp->w_lines_valid
751 || (i < wp->w_lines_valid
752 && (!wp->w_lines[i].wl_valid
753 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
754 {
755#ifdef FEAT_DIFF
756 if (wp->w_cursor.lnum == wp->w_topline)
757 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
758 TRUE) + wp->w_topfill;
759 else
760#endif
761 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
762#ifdef FEAT_FOLDING
763 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
764 NULL, NULL, TRUE, NULL);
765#endif
766 }
767 else if (i > wp->w_lines_valid)
768 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100769 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 wp->w_cline_height = 0;
771#ifdef FEAT_FOLDING
772 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
773 NULL, NULL, TRUE, NULL);
774#endif
775 }
776 else
777 {
778 wp->w_cline_height = wp->w_lines[i].wl_size;
779#ifdef FEAT_FOLDING
780 wp->w_cline_folded = wp->w_lines[i].wl_folded;
781#endif
782 }
783 }
784
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100785 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
790/*
791 * Validate curwin->w_virtcol only.
792 */
793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100794validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 validate_virtcol_win(curwin);
797}
798
799/*
800 * Validate wp->w_virtcol only.
801 */
802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 check_cursor_moved(wp);
806 if (!(wp->w_valid & VALID_VIRTCOL))
807 {
808 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
809 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000810#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200811 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000812 redraw_win_later(wp, SOME_VALID);
813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 }
815}
816
817/*
818 * Validate curwin->w_cline_height only.
819 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 check_cursor_moved(curwin);
824 if (!(curwin->w_valid & VALID_CHEIGHT))
825 {
826#ifdef FEAT_DIFF
827 if (curwin->w_cursor.lnum == curwin->w_topline)
828 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
829 + curwin->w_topfill;
830 else
831#endif
832 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
833#ifdef FEAT_FOLDING
834 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
835#endif
836 curwin->w_valid |= VALID_CHEIGHT;
837 }
838}
839
840/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000841 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 */
843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100844validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 colnr_T off;
847 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100848 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850 validate_virtcol();
851 if (!(curwin->w_valid & VALID_WCOL))
852 {
853 col = curwin->w_virtcol;
854 off = curwin_col_off();
855 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200856 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
Bram Moolenaar85a20022019-12-21 18:25:54 +0100858 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000859 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200860 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100861 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100862 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200863 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000864 if (col > (int)curwin->w_leftcol)
865 col -= curwin->w_leftcol;
866 else
867 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000869
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar6a076442020-11-15 20:32:58 +0100871 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873}
874
875/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200876 * Compute offset of a window, occupied by absolute or relative line number,
877 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 */
879 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100880win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
Bram Moolenaar64486672010-05-16 15:46:46 +0200882 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#ifdef FEAT_CMDWIN
884 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
885#endif
886#ifdef FEAT_FOLDING
887 + wp->w_p_fdc
888#endif
889#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200890 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#endif
892 );
893}
894
895 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100896curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
898 return win_col_off(curwin);
899}
900
901/*
902 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200903 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
904 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 */
906 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100907win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
Bram Moolenaar64486672010-05-16 15:46:46 +0200909 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000910 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 return 0;
912}
913
914 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100915curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
917 return win_col_off2(curwin);
918}
919
920/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100921 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 * Also updates curwin->w_wrow and curwin->w_cline_row.
923 * Also updates curwin->w_leftcol.
924 */
925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100927 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100930 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 int off_left, off_right;
932 int n;
933 int p_lines;
934 int width = 0;
935 int textwidth;
936 int new_leftcol;
937 colnr_T startcol;
938 colnr_T endcol;
939 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100940 long so = get_scrolloff_value();
941 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943 /*
944 * First make sure that w_topline is valid (after moving the cursor).
945 */
946 update_topline();
947
948 /*
949 * Next make sure that w_cline_row is valid.
950 */
951 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100952 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
954 /*
955 * Compute the number of virtual columns.
956 */
957#ifdef FEAT_FOLDING
958 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100959 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
961 else
962#endif
963 getvvcol(curwin, &curwin->w_cursor,
964 &startcol, &(curwin->w_virtcol), &endcol);
965
Bram Moolenaar85a20022019-12-21 18:25:54 +0100966 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100968 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970 extra = curwin_col_off();
971 curwin->w_wcol = curwin->w_virtcol + extra;
972 endcol += extra;
973
974 /*
975 * Now compute w_wrow, counting screen lines from w_cline_row.
976 */
977 curwin->w_wrow = curwin->w_cline_row;
978
Bram Moolenaar02631462017-09-22 15:20:32 +0200979 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (textwidth <= 0)
981 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100982 // No room for text, put cursor in last char of window.
Bram Moolenaar02631462017-09-22 15:20:32 +0200983 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 curwin->w_wrow = curwin->w_height - 1;
985 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200986 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
988 width = textwidth + curwin_col_off2();
989
Bram Moolenaar85a20022019-12-21 18:25:54 +0100990 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +0200991 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100993#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +0100994 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100995#endif
Bram Moolenaaree857022019-11-09 23:26:40 +0100996
Bram Moolenaar85a20022019-12-21 18:25:54 +0100997 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +0200998 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 curwin->w_wcol -= n * width;
1000 curwin->w_wrow += n;
1001
1002#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001003 // When cursor wraps to first char of next line in Insert
1004 // mode, the 'showbreak' string isn't shown, backup to first
1005 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001006 sbr = get_showbreak_value(curwin);
1007 if (*sbr && *ml_get_cursor() == NUL
1008 && curwin->w_wcol == (int)vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 curwin->w_wcol = 0;
1010#endif
1011 }
1012 }
1013
Bram Moolenaar85a20022019-12-21 18:25:54 +01001014 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1015 // is not folded.
1016 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001017 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#ifdef FEAT_FOLDING
1019 && !curwin->w_cline_folded
1020#endif
1021 )
1022 {
1023 /*
1024 * If Cursor is left of the screen, scroll rightwards.
1025 * If Cursor is right of the screen, scroll leftwards
1026 * If we get closer to the edge than 'sidescrolloff', scroll a little
1027 * extra
1028 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001029 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001030 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001031 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (off_left < 0 || off_right > 0)
1033 {
1034 if (off_left < 0)
1035 diff = -off_left;
1036 else
1037 diff = off_right;
1038
Bram Moolenaar85a20022019-12-21 18:25:54 +01001039 // When far off or not enough room on either side, put cursor in
1040 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1042 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1043 else
1044 {
1045 if (diff < p_ss)
1046 diff = p_ss;
1047 if (off_left < 0)
1048 new_leftcol = curwin->w_leftcol - diff;
1049 else
1050 new_leftcol = curwin->w_leftcol + diff;
1051 }
1052 if (new_leftcol < 0)
1053 new_leftcol = 0;
1054 if (new_leftcol != (int)curwin->w_leftcol)
1055 {
1056 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001057 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 redraw_later(NOT_VALID);
1059 }
1060 }
1061 curwin->w_wcol -= curwin->w_leftcol;
1062 }
1063 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1064 curwin->w_wcol -= curwin->w_leftcol;
1065 else
1066 curwin->w_wcol = 0;
1067
1068#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001069 // Skip over filler lines. At the top use w_topfill, there
1070 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (curwin->w_cursor.lnum == curwin->w_topline)
1072 curwin->w_wrow += curwin->w_topfill;
1073 else
1074 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1075#endif
1076
1077 prev_skipcol = curwin->w_skipcol;
1078
1079 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if ((curwin->w_wrow >= curwin->w_height
1082 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001083 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 && (p_lines =
1085#ifdef FEAT_DIFF
1086 plines_win_nofill
1087#else
1088 plines_win
1089#endif
1090 (curwin, curwin->w_cursor.lnum, FALSE))
1091 - 1 >= curwin->w_height))
1092 && curwin->w_height != 0
1093 && curwin->w_cursor.lnum == curwin->w_topline
1094 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001095 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001097 // Cursor past end of screen. Happens with a single line that does
1098 // not fit on screen. Find a skipcol to show the text around the
1099 // cursor. Avoid scrolling all the time. compute value of "extra":
1100 // 1: Less than 'scrolloff' lines above
1101 // 2: Less than 'scrolloff' lines below
1102 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001104 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001106 // Compute last display line of the buffer line that we want at the
1107 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (p_lines == 0)
1109 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1110 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001111 if (p_lines > curwin->w_wrow + so)
1112 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 else
1114 n = p_lines;
1115 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1116 extra += 2;
1117
Bram Moolenaar375e3392019-01-31 18:26:10 +01001118 if (extra == 3 || p_lines < so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001120 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 n = curwin->w_virtcol / width;
1122 if (n > curwin->w_height / 2)
1123 n -= curwin->w_height / 2;
1124 else
1125 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001126 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (n > p_lines - curwin->w_height + 1)
1128 n = p_lines - curwin->w_height + 1;
1129 curwin->w_skipcol = n * width;
1130 }
1131 else if (extra == 1)
1132 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001133 // less then 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001134 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 + width - 1) / width;
1136 if (extra > 0)
1137 {
1138 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1139 extra = curwin->w_skipcol / width;
1140 curwin->w_skipcol -= extra * width;
1141 }
1142 }
1143 else if (extra == 2)
1144 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001145 // less then 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 endcol = (n - curwin->w_height + 1) * width;
1147 while (endcol > curwin->w_virtcol)
1148 endcol -= width;
1149 if (endcol > curwin->w_skipcol)
1150 curwin->w_skipcol = endcol;
1151 }
1152
1153 curwin->w_wrow -= curwin->w_skipcol / width;
1154 if (curwin->w_wrow >= curwin->w_height)
1155 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001156 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 extra = curwin->w_wrow - curwin->w_height + 1;
1158 curwin->w_skipcol += extra * width;
1159 curwin->w_wrow -= extra;
1160 }
1161
1162 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1163 if (extra > 0)
1164 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1165 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001166 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 }
1168 else
1169 curwin->w_skipcol = 0;
1170 if (prev_skipcol != curwin->w_skipcol)
1171 redraw_later(NOT_VALID);
1172
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001173#ifdef FEAT_SYN_HL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 // Redraw when w_virtcol changes and 'cursorcolumn' is set
Bram Moolenaarb6798752014-03-27 12:11:48 +01001175 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001176 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001177 redraw_later(SOME_VALID);
1178#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001179#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1180 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1181 {
1182 curwin->w_wrow += popup_top_extra(curwin);
1183 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001184 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001185 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001186 else
1187 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001188#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001189
Bram Moolenaar08f23632019-11-16 14:19:33 +01001190 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1191 curwin->w_valid_leftcol = curwin->w_leftcol;
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1194}
1195
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001196#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001197/*
1198 * Compute the screen position of text character at "pos" in window "wp"
1199 * The resulting values are one-based, zero when character is not visible.
1200 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001201 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001202textpos2screenpos(
1203 win_T *wp,
1204 pos_T *pos,
1205 int *rowp, // screen row
1206 int *scolp, // start screen column
1207 int *ccolp, // cursor screen column
1208 int *ecolp) // end screen column
1209{
1210 colnr_T scol = 0, ccol = 0, ecol = 0;
1211 int row = 0;
1212 int rowoff = 0;
1213 colnr_T coloff = 0;
1214
1215 if (pos->lnum >= wp->w_topline && pos->lnum < wp->w_botline)
1216 {
1217 colnr_T off;
1218 colnr_T col;
1219 int width;
1220
1221 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1222 getvcol(wp, pos, &scol, &ccol, &ecol);
1223
1224 // similar to what is done in validate_cursor_col()
1225 col = scol;
1226 off = win_col_off(wp);
1227 col += off;
1228 width = wp->w_width - off + win_col_off2(wp);
1229
Bram Moolenaar12034e22019-08-25 22:25:02 +02001230 // long line wrapping, adjust row
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001231 if (wp->w_p_wrap
1232 && col >= (colnr_T)wp->w_width
1233 && width > 0)
1234 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001235 // use same formula as what is used in curs_columns()
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001236 rowoff = ((col - wp->w_width) / width + 1);
1237 col -= rowoff * width;
1238 }
1239 col -= wp->w_leftcol;
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +01001240 if (col >= wp->w_width)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001241 col = -1;
1242 if (col >= 0)
1243 coloff = col - scol + wp->w_wincol + 1;
1244 else
1245 // character is left or right of the window
1246 row = scol = ccol = ecol = 0;
1247 }
1248 *rowp = wp->w_winrow + row + rowoff;
1249 *scolp = scol + coloff;
1250 *ccolp = ccol + coloff;
1251 *ecolp = ecol + coloff;
1252}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001253#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001254
Bram Moolenaar12034e22019-08-25 22:25:02 +02001255#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001256/*
1257 * "screenpos({winid}, {lnum}, {col})" function
1258 */
1259 void
1260f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1261{
1262 dict_T *dict;
1263 win_T *wp;
1264 pos_T pos;
1265 int row = 0;
1266 int scol = 0, ccol = 0, ecol = 0;
1267
1268 if (rettv_dict_alloc(rettv) != OK)
1269 return;
1270 dict = rettv->vval.v_dict;
1271
1272 wp = find_win_by_nr_or_id(&argvars[0]);
1273 if (wp == NULL)
1274 return;
1275
1276 pos.lnum = tv_get_number(&argvars[1]);
1277 pos.col = tv_get_number(&argvars[2]) - 1;
1278 pos.coladd = 0;
1279 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1280
1281 dict_add_number(dict, "row", row);
1282 dict_add_number(dict, "col", scol);
1283 dict_add_number(dict, "curscol", ccol);
1284 dict_add_number(dict, "endcol", ecol);
1285}
1286#endif
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288/*
1289 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001292scrolldown(
1293 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001294 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001296 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 int wrow;
1298 int moved = FALSE;
1299
1300#ifdef FEAT_FOLDING
1301 linenr_T first;
1302
Bram Moolenaar85a20022019-12-21 18:25:54 +01001303 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1305#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001306 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 while (line_count-- > 0)
1308 {
1309#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001310 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1311 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 ++curwin->w_topfill;
1314 ++done;
1315 }
1316 else
1317#endif
1318 {
1319 if (curwin->w_topline == 1)
1320 break;
1321 --curwin->w_topline;
1322#ifdef FEAT_DIFF
1323 curwin->w_topfill = 0;
1324#endif
1325#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001326 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (hasFolding(curwin->w_topline, &first, NULL))
1328 {
1329 ++done;
1330 if (!byfold)
1331 line_count -= curwin->w_topline - first - 1;
1332 curwin->w_botline -= curwin->w_topline - first;
1333 curwin->w_topline = first;
1334 }
1335 else
1336#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001337 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001339 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 invalidate_botline();
1341 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001342 curwin->w_wrow += done; // keep w_wrow updated
1343 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345#ifdef FEAT_DIFF
1346 if (curwin->w_cursor.lnum == curwin->w_topline)
1347 curwin->w_cline_row = 0;
1348 check_topfill(curwin, TRUE);
1349#endif
1350
1351 /*
1352 * Compute the row number of the last row of the cursor line
1353 * and move the cursor onto the displayed part of the window.
1354 */
1355 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001356 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 validate_virtcol();
1359 validate_cheight();
1360 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001361 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1364 {
1365#ifdef FEAT_FOLDING
1366 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1367 {
1368 --wrow;
1369 if (first == 1)
1370 curwin->w_cursor.lnum = 1;
1371 else
1372 curwin->w_cursor.lnum = first - 1;
1373 }
1374 else
1375#endif
1376 wrow -= plines(curwin->w_cursor.lnum--);
1377 curwin->w_valid &=
1378 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1379 moved = TRUE;
1380 }
1381 if (moved)
1382 {
1383#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001384 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 foldAdjustCursor();
1386#endif
1387 coladvance(curwin->w_curswant);
1388 }
1389}
1390
1391/*
1392 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001395scrollup(
1396 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001397 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1400 linenr_T lnum;
1401
1402 if (
1403# ifdef FEAT_FOLDING
1404 (byfold && hasAnyFolding(curwin))
1405# ifdef FEAT_DIFF
1406 ||
1407# endif
1408# endif
1409# ifdef FEAT_DIFF
1410 curwin->w_p_diff
1411# endif
1412 )
1413 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001414 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 lnum = curwin->w_topline;
1416 while (line_count--)
1417 {
1418# ifdef FEAT_DIFF
1419 if (curwin->w_topfill > 0)
1420 --curwin->w_topfill;
1421 else
1422# endif
1423 {
1424# ifdef FEAT_FOLDING
1425 if (byfold)
1426 (void)hasFolding(lnum, NULL, &lnum);
1427# endif
1428 if (lnum >= curbuf->b_ml.ml_line_count)
1429 break;
1430 ++lnum;
1431# ifdef FEAT_DIFF
1432 curwin->w_topfill = diff_check_fill(curwin, lnum);
1433# endif
1434 }
1435 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001436 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 curwin->w_botline += lnum - curwin->w_topline;
1438 curwin->w_topline = lnum;
1439 }
1440 else
1441#endif
1442 {
1443 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001444 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446
1447 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1448 curwin->w_topline = curbuf->b_ml.ml_line_count;
1449 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1450 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1451
1452#ifdef FEAT_DIFF
1453 check_topfill(curwin, FALSE);
1454#endif
1455
1456#ifdef FEAT_FOLDING
1457 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001458 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1460#endif
1461
1462 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1463 if (curwin->w_cursor.lnum < curwin->w_topline)
1464 {
1465 curwin->w_cursor.lnum = curwin->w_topline;
1466 curwin->w_valid &=
1467 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1468 coladvance(curwin->w_curswant);
1469 }
1470}
1471
1472#ifdef FEAT_DIFF
1473/*
1474 * Don't end up with too many filler lines in the window.
1475 */
1476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001477check_topfill(
1478 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001479 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 int n;
1482
1483 if (wp->w_topfill > 0)
1484 {
1485 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1486 if (wp->w_topfill + n > wp->w_height)
1487 {
1488 if (down && wp->w_topline > 1)
1489 {
1490 --wp->w_topline;
1491 wp->w_topfill = 0;
1492 }
1493 else
1494 {
1495 wp->w_topfill = wp->w_height - n;
1496 if (wp->w_topfill < 0)
1497 wp->w_topfill = 0;
1498 }
1499 }
1500 }
1501}
1502
1503/*
1504 * Use as many filler lines as possible for w_topline. Make sure w_topline
1505 * is still visible.
1506 */
1507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001508max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 int n;
1511
1512 n = plines_nofill(curwin->w_topline);
1513 if (n >= curwin->w_height)
1514 curwin->w_topfill = 0;
1515 else
1516 {
1517 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1518 if (curwin->w_topfill + n > curwin->w_height)
1519 curwin->w_topfill = curwin->w_height - n;
1520 }
1521}
1522#endif
1523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524/*
1525 * Scroll the screen one line down, but don't do it if it would move the
1526 * cursor off the screen.
1527 */
1528 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001529scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 int end_row;
1532#ifdef FEAT_DIFF
1533 int can_fill = (curwin->w_topfill
1534 < diff_check_fill(curwin, curwin->w_topline));
1535#endif
1536
1537 if (curwin->w_topline <= 1
1538#ifdef FEAT_DIFF
1539 && !can_fill
1540#endif
1541 )
1542 return;
1543
Bram Moolenaar85a20022019-12-21 18:25:54 +01001544 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546 /*
1547 * Compute the row number of the last row of the cursor line
1548 * and make sure it doesn't go off the screen. Make sure the cursor
1549 * doesn't go past 'scrolloff' lines from the screen end.
1550 */
1551 end_row = curwin->w_wrow;
1552#ifdef FEAT_DIFF
1553 if (can_fill)
1554 ++end_row;
1555 else
1556 end_row += plines_nofill(curwin->w_topline - 1);
1557#else
1558 end_row += plines(curwin->w_topline - 1);
1559#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001560 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 validate_cheight();
1563 validate_virtcol();
1564 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001565 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001567 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
1569#ifdef FEAT_DIFF
1570 if (can_fill)
1571 {
1572 ++curwin->w_topfill;
1573 check_topfill(curwin, TRUE);
1574 }
1575 else
1576 {
1577 --curwin->w_topline;
1578 curwin->w_topfill = 0;
1579 }
1580#else
1581 --curwin->w_topline;
1582#endif
1583#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001584 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001586 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1588 }
1589}
1590
1591/*
1592 * Scroll the screen one line up, but don't do it if it would move the cursor
1593 * off the screen.
1594 */
1595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001596scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 int start_row;
1599
1600 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1601#ifdef FEAT_DIFF
1602 && curwin->w_topfill == 0
1603#endif
1604 )
1605 return;
1606
Bram Moolenaar85a20022019-12-21 18:25:54 +01001607 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 /*
1610 * Compute the row number of the first row of the cursor line
1611 * and make sure it doesn't go off the screen. Make sure the cursor
1612 * doesn't go before 'scrolloff' lines from the screen start.
1613 */
1614#ifdef FEAT_DIFF
1615 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1616 - curwin->w_topfill;
1617#else
1618 start_row = curwin->w_wrow - plines(curwin->w_topline);
1619#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001620 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001623 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001625 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627#ifdef FEAT_DIFF
1628 if (curwin->w_topfill > 0)
1629 --curwin->w_topfill;
1630 else
1631#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001632 {
1633#ifdef FEAT_FOLDING
1634 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001637 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001638 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1640 }
1641}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643/*
1644 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1645 * a (wrapped) text line. Uses and sets "lp->fill".
1646 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001647 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 */
1649 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001650topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
1652#ifdef FEAT_DIFF
1653 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1654 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001655 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 ++lp->fill;
1657 lp->height = 1;
1658 }
1659 else
1660#endif
1661 {
1662 --lp->lnum;
1663#ifdef FEAT_DIFF
1664 lp->fill = 0;
1665#endif
1666 if (lp->lnum < 1)
1667 lp->height = MAXCOL;
1668 else
1669#ifdef FEAT_FOLDING
1670 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001671 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 lp->height = 1;
1673 else
1674#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001675 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677}
1678
1679/*
1680 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1681 * a (wrapped) text line. Uses and sets "lp->fill".
1682 * Returns the height of the added line in "lp->height".
1683 * Lines below the last one are incredibly high.
1684 */
1685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001686botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
1688#ifdef FEAT_DIFF
1689 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1690 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001691 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 ++lp->fill;
1693 lp->height = 1;
1694 }
1695 else
1696#endif
1697 {
1698 ++lp->lnum;
1699#ifdef FEAT_DIFF
1700 lp->fill = 0;
1701#endif
1702 if (lp->lnum > curbuf->b_ml.ml_line_count)
1703 lp->height = MAXCOL;
1704 else
1705#ifdef FEAT_FOLDING
1706 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001707 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 lp->height = 1;
1709 else
1710#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001711 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713}
1714
1715#ifdef FEAT_DIFF
1716/*
1717 * Switch from including filler lines below lp->lnum to including filler
1718 * lines above loff.lnum + 1. This keeps pointing to the same line.
1719 * When there are no filler lines nothing changes.
1720 */
1721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001722botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 if (lp->fill > 0)
1725 {
1726 ++lp->lnum;
1727 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1728 }
1729}
1730
1731/*
1732 * Switch from including filler lines above lp->lnum to including filler
1733 * lines below loff.lnum - 1. This keeps pointing to the same line.
1734 * When there are no filler lines nothing changes.
1735 */
1736 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001737topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 if (lp->fill > 0)
1740 {
1741 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1742 --lp->lnum;
1743 }
1744}
1745#endif
1746
1747/*
1748 * Recompute topline to put the cursor at the top of the window.
1749 * Scroll at least "min_scroll" lines.
1750 * If "always" is TRUE, always set topline (for "zt").
1751 */
1752 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001753scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 int scrolled = 0;
1756 int extra = 0;
1757 int used;
1758 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001759 linenr_T top; // just above displayed lines
1760 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 linenr_T old_topline = curwin->w_topline;
1762#ifdef FEAT_DIFF
1763 linenr_T old_topfill = curwin->w_topfill;
1764#endif
1765 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001766 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (mouse_dragging > 0)
1769 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
1771 /*
1772 * Decrease topline until:
1773 * - it has become 1
1774 * - (part of) the cursor line is moved off the screen or
1775 * - moved at least 'scrolljump' lines and
1776 * - at least 'scrolloff' lines above and below the cursor
1777 */
1778 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (curwin->w_cursor.lnum < curwin->w_topline)
1781 scrolled = used;
1782
1783#ifdef FEAT_FOLDING
1784 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1785 {
1786 --top;
1787 ++bot;
1788 }
1789 else
1790#endif
1791 {
1792 top = curwin->w_cursor.lnum - 1;
1793 bot = curwin->w_cursor.lnum + 1;
1794 }
1795 new_topline = top + 1;
1796
1797#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001798 // "used" already contains the number of filler lines above, don't add it
1799 // again.
1800 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001801 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802#endif
1803
1804 /*
1805 * Check if the lines from "top" to "bot" fit in the window. If they do,
1806 * set new_topline and advance "top" and "bot" to include more lines.
1807 */
1808 while (top > 0)
1809 {
1810#ifdef FEAT_FOLDING
1811 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001812 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 i = 1;
1814 else
1815#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001816 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 used += i;
1818 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1819 {
1820#ifdef FEAT_FOLDING
1821 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001822 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 ++used;
1824 else
1825#endif
1826 used += plines(bot);
1827 }
1828 if (used > curwin->w_height)
1829 break;
1830 if (top < curwin->w_topline)
1831 scrolled += i;
1832
1833 /*
1834 * If scrolling is needed, scroll at least 'sj' lines.
1835 */
1836 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1837 && extra >= off)
1838 break;
1839
1840 extra += i;
1841 new_topline = top;
1842 --top;
1843 ++bot;
1844 }
1845
1846 /*
1847 * If we don't have enough space, put cursor in the middle.
1848 * This makes sure we get the same position when using "k" and "j"
1849 * in a small window.
1850 */
1851 if (used > curwin->w_height)
1852 scroll_cursor_halfway(FALSE);
1853 else
1854 {
1855 /*
1856 * If "always" is FALSE, only adjust topline to a lower value, higher
1857 * value may happen with wrapping lines
1858 */
1859 if (new_topline < curwin->w_topline || always)
1860 curwin->w_topline = new_topline;
1861 if (curwin->w_topline > curwin->w_cursor.lnum)
1862 curwin->w_topline = curwin->w_cursor.lnum;
1863#ifdef FEAT_DIFF
1864 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1865 if (curwin->w_topfill > 0 && extra > off)
1866 {
1867 curwin->w_topfill -= extra - off;
1868 if (curwin->w_topfill < 0)
1869 curwin->w_topfill = 0;
1870 }
1871 check_topfill(curwin, FALSE);
1872#endif
1873 if (curwin->w_topline != old_topline
1874#ifdef FEAT_DIFF
1875 || curwin->w_topfill != old_topfill
1876#endif
1877 )
1878 curwin->w_valid &=
1879 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1880 curwin->w_valid |= VALID_TOPLINE;
1881 }
1882}
1883
1884/*
1885 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1886 * screen lines for text lines.
1887 */
1888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001889set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891#ifdef FEAT_DIFF
1892 wp->w_filler_rows = 0;
1893#endif
1894 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001895 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 else
1897 {
1898 wp->w_empty_rows = wp->w_height - used;
1899#ifdef FEAT_DIFF
1900 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1901 {
1902 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1903 if (wp->w_empty_rows > wp->w_filler_rows)
1904 wp->w_empty_rows -= wp->w_filler_rows;
1905 else
1906 {
1907 wp->w_filler_rows = wp->w_empty_rows;
1908 wp->w_empty_rows = 0;
1909 }
1910 }
1911#endif
1912 }
1913}
1914
1915/*
1916 * Recompute topline to put the cursor at the bottom of the window.
1917 * Scroll at least "min_scroll" lines.
1918 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1919 * This is messy stuff!!!
1920 */
1921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001922scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923{
1924 int used;
1925 int scrolled = 0;
1926 int extra = 0;
1927 int i;
1928 linenr_T line_count;
1929 linenr_T old_topline = curwin->w_topline;
1930 lineoff_T loff;
1931 lineoff_T boff;
1932#ifdef FEAT_DIFF
1933 int old_topfill = curwin->w_topfill;
1934 int fill_below_window;
1935#endif
1936 linenr_T old_botline = curwin->w_botline;
1937 linenr_T old_valid = curwin->w_valid;
1938 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001939 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001940 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
1942 cln = curwin->w_cursor.lnum;
1943 if (set_topbot)
1944 {
1945 used = 0;
1946 curwin->w_botline = cln + 1;
1947#ifdef FEAT_DIFF
1948 loff.fill = 0;
1949#endif
1950 for (curwin->w_topline = curwin->w_botline;
1951 curwin->w_topline > 1;
1952 curwin->w_topline = loff.lnum)
1953 {
1954 loff.lnum = curwin->w_topline;
1955 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001956 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 break;
1958 used += loff.height;
1959#ifdef FEAT_DIFF
1960 curwin->w_topfill = loff.fill;
1961#endif
1962 }
1963 set_empty_rows(curwin, used);
1964 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1965 if (curwin->w_topline != old_topline
1966#ifdef FEAT_DIFF
1967 || curwin->w_topfill != old_topfill
1968#endif
1969 )
1970 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1971 }
1972 else
1973 validate_botline();
1974
Bram Moolenaar85a20022019-12-21 18:25:54 +01001975 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976#ifdef FEAT_DIFF
1977 used = plines_nofill(cln);
1978#else
1979 validate_cheight();
1980 used = curwin->w_cline_height;
1981#endif
1982
Bram Moolenaar85a20022019-12-21 18:25:54 +01001983 // If the cursor is below botline, we will at least scroll by the height
1984 // of the cursor line. Correct for empty lines, which are really part of
1985 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (cln >= curwin->w_botline)
1987 {
1988 scrolled = used;
1989 if (cln == curwin->w_botline)
1990 scrolled -= curwin->w_empty_rows;
1991 }
1992
1993 /*
1994 * Stop counting lines to scroll when
1995 * - hitting start of the file
1996 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01001997 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 * - lines between botline and cursor have been counted
1999 */
2000#ifdef FEAT_FOLDING
2001 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2002#endif
2003 {
2004 loff.lnum = cln;
2005 boff.lnum = cln;
2006 }
2007#ifdef FEAT_DIFF
2008 loff.fill = 0;
2009 boff.fill = 0;
2010 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2011 - curwin->w_filler_rows;
2012#endif
2013
2014 while (loff.lnum > 1)
2015 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002016 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2017 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002019 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2021 && loff.lnum <= curwin->w_botline
2022#ifdef FEAT_DIFF
2023 && (loff.lnum < curwin->w_botline
2024 || loff.fill >= fill_below_window)
2025#endif
2026 )
2027 break;
2028
Bram Moolenaar85a20022019-12-21 18:25:54 +01002029 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002031 if (loff.height == MAXCOL)
2032 used = MAXCOL;
2033 else
2034 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (used > curwin->w_height)
2036 break;
2037 if (loff.lnum >= curwin->w_botline
2038#ifdef FEAT_DIFF
2039 && (loff.lnum > curwin->w_botline
2040 || loff.fill <= fill_below_window)
2041#endif
2042 )
2043 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002044 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 scrolled += loff.height;
2046 if (loff.lnum == curwin->w_botline
2047#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002048 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049#endif
2050 )
2051 scrolled -= curwin->w_empty_rows;
2052 }
2053
2054 if (boff.lnum < curbuf->b_ml.ml_line_count)
2055 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002056 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 botline_forw(&boff);
2058 used += boff.height;
2059 if (used > curwin->w_height)
2060 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002061 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2062 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
2064 extra += boff.height;
2065 if (boff.lnum >= curwin->w_botline
2066#ifdef FEAT_DIFF
2067 || (boff.lnum + 1 == curwin->w_botline
2068 && boff.fill > curwin->w_filler_rows)
2069#endif
2070 )
2071 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002072 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 scrolled += boff.height;
2074 if (boff.lnum == curwin->w_botline
2075#ifdef FEAT_DIFF
2076 && boff.fill == 0
2077#endif
2078 )
2079 scrolled -= curwin->w_empty_rows;
2080 }
2081 }
2082 }
2083 }
2084
Bram Moolenaar85a20022019-12-21 18:25:54 +01002085 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (scrolled <= 0)
2087 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002088 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 else if (used > curwin->w_height)
2090 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002091 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 else
2093 {
2094 line_count = 0;
2095#ifdef FEAT_DIFF
2096 boff.fill = curwin->w_topfill;
2097#endif
2098 boff.lnum = curwin->w_topline - 1;
2099 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2100 {
2101 botline_forw(&boff);
2102 i += boff.height;
2103 ++line_count;
2104 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002105 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 line_count = 9999;
2107 }
2108
2109 /*
2110 * Scroll up if the cursor is off the bottom of the screen a bit.
2111 * Otherwise put it at 1/2 of the screen.
2112 */
2113 if (line_count >= curwin->w_height && line_count > min_scroll)
2114 scroll_cursor_halfway(FALSE);
2115 else
2116 scrollup(line_count, TRUE);
2117
2118 /*
2119 * If topline didn't change we need to restore w_botline and w_empty_rows
2120 * (we changed them).
2121 * If topline did change, update_screen() will set botline.
2122 */
2123 if (curwin->w_topline == old_topline && set_topbot)
2124 {
2125 curwin->w_botline = old_botline;
2126 curwin->w_empty_rows = old_empty_rows;
2127 curwin->w_valid = old_valid;
2128 }
2129 curwin->w_valid |= VALID_TOPLINE;
2130}
2131
2132/*
2133 * Recompute topline to put the cursor halfway the window
2134 * If "atend" is TRUE, also put it halfway at the end of the file.
2135 */
2136 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002137scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139 int above = 0;
2140 linenr_T topline;
2141#ifdef FEAT_DIFF
2142 int topfill = 0;
2143#endif
2144 int below = 0;
2145 int used;
2146 lineoff_T loff;
2147 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002148#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002149 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002152#ifdef FEAT_PROP_POPUP
2153 // if the width changed this needs to be updated first
2154 may_update_popup_position();
2155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2157#ifdef FEAT_FOLDING
2158 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2159#endif
2160#ifdef FEAT_DIFF
2161 used = plines_nofill(loff.lnum);
2162 loff.fill = 0;
2163 boff.fill = 0;
2164#else
2165 used = plines(loff.lnum);
2166#endif
2167 topline = loff.lnum;
2168 while (topline > 1)
2169 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002170 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
2172 if (boff.lnum < curbuf->b_ml.ml_line_count)
2173 {
2174 botline_forw(&boff);
2175 used += boff.height;
2176 if (used > curwin->w_height)
2177 break;
2178 below += boff.height;
2179 }
2180 else
2181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002182 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (atend)
2184 ++used;
2185 }
2186 }
2187
Bram Moolenaar85a20022019-12-21 18:25:54 +01002188 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
2190 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002191 if (loff.height == MAXCOL)
2192 used = MAXCOL;
2193 else
2194 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (used > curwin->w_height)
2196 break;
2197 above += loff.height;
2198 topline = loff.lnum;
2199#ifdef FEAT_DIFF
2200 topfill = loff.fill;
2201#endif
2202 }
2203 }
2204#ifdef FEAT_FOLDING
2205 if (!hasFolding(topline, &curwin->w_topline, NULL))
2206#endif
2207 curwin->w_topline = topline;
2208#ifdef FEAT_DIFF
2209 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002210 if (old_topline > curwin->w_topline + curwin->w_height)
2211 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 check_topfill(curwin, FALSE);
2213#endif
2214 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2215 curwin->w_valid |= VALID_TOPLINE;
2216}
2217
2218/*
2219 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002220 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * If not possible, put it at the same position as scroll_cursor_halfway().
2222 * When called topline must be valid!
2223 */
2224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002225cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 linenr_T botline;
2231 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002232 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002234 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 /*
2237 * How many lines we would like to have above/below the cursor depends on
2238 * whether the first/last line of the file is on screen.
2239 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002240 above_wanted = so;
2241 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002242 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 above_wanted = mouse_dragging - 1;
2245 below_wanted = mouse_dragging - 1;
2246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (curwin->w_topline == 1)
2248 {
2249 above_wanted = 0;
2250 max_off = curwin->w_height / 2;
2251 if (below_wanted > max_off)
2252 below_wanted = max_off;
2253 }
2254 validate_botline();
2255 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002256 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
2258 below_wanted = 0;
2259 max_off = (curwin->w_height - 1) / 2;
2260 if (above_wanted > max_off)
2261 above_wanted = max_off;
2262 }
2263
2264 /*
2265 * If there are sufficient file-lines above and below the cursor, we can
2266 * return now.
2267 */
2268 cln = curwin->w_cursor.lnum;
2269 if (cln >= curwin->w_topline + above_wanted
2270 && cln < curwin->w_botline - below_wanted
2271#ifdef FEAT_FOLDING
2272 && !hasAnyFolding(curwin)
2273#endif
2274 )
2275 return;
2276
2277 /*
2278 * Narrow down the area where the cursor can be put by taking lines from
2279 * the top and the bottom until:
2280 * - the desired context lines are found
2281 * - the lines from the top is past the lines from the bottom
2282 */
2283 topline = curwin->w_topline;
2284 botline = curwin->w_botline - 1;
2285#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002286 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 above = curwin->w_topfill;
2288 below = curwin->w_filler_rows;
2289#endif
2290 while ((above < above_wanted || below < below_wanted) && topline < botline)
2291 {
2292 if (below < below_wanted && (below <= above || above >= above_wanted))
2293 {
2294#ifdef FEAT_FOLDING
2295 if (hasFolding(botline, &botline, NULL))
2296 ++below;
2297 else
2298#endif
2299 below += plines(botline);
2300 --botline;
2301 }
2302 if (above < above_wanted && (above < below || below >= below_wanted))
2303 {
2304#ifdef FEAT_FOLDING
2305 if (hasFolding(topline, NULL, &topline))
2306 ++above;
2307 else
2308#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002309 above += PLINES_NOFILL(topline);
2310#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002311 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (topline < botline)
2313 above += diff_check_fill(curwin, topline + 1);
2314#endif
2315 ++topline;
2316 }
2317 }
2318 if (topline == botline || botline == 0)
2319 curwin->w_cursor.lnum = topline;
2320 else if (topline > botline)
2321 curwin->w_cursor.lnum = botline;
2322 else
2323 {
2324 if (cln < topline && curwin->w_topline > 1)
2325 {
2326 curwin->w_cursor.lnum = topline;
2327 curwin->w_valid &=
2328 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2329 }
2330 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2331 {
2332 curwin->w_cursor.lnum = botline;
2333 curwin->w_valid &=
2334 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2335 }
2336 }
2337 curwin->w_valid |= VALID_TOPLINE;
2338}
2339
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002340static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342/*
2343 * move screen 'count' pages up or down and update screen
2344 *
2345 * return FAIL for failure, OK otherwise
2346 */
2347 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002348onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349{
2350 long n;
2351 int retval = OK;
2352 lineoff_T loff;
2353 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002354 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Bram Moolenaar85a20022019-12-21 18:25:54 +01002356 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 beep_flush();
2359 return FAIL;
2360 }
2361
2362 for ( ; count > 0; --count)
2363 {
2364 validate_botline();
2365 /*
2366 * It's an error to move a page up when the first line is already on
2367 * the screen. It's an error to move a page down when the last line
2368 * is on the screen and the topline is 'scrolloff' lines from the
2369 * last line.
2370 */
2371 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002372 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2374 : (curwin->w_topline == 1
2375#ifdef FEAT_DIFF
2376 && curwin->w_topfill ==
2377 diff_check_fill(curwin, curwin->w_topline)
2378#endif
2379 ))
2380 {
2381 beep_flush();
2382 retval = FAIL;
2383 break;
2384 }
2385
2386#ifdef FEAT_DIFF
2387 loff.fill = 0;
2388#endif
2389 if (dir == FORWARD)
2390 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002391 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002393 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002394 if (p_window <= 2)
2395 ++curwin->w_topline;
2396 else
2397 curwin->w_topline += p_window - 2;
2398 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2399 curwin->w_topline = curbuf->b_ml.ml_line_count;
2400 curwin->w_cursor.lnum = curwin->w_topline;
2401 }
2402 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2403 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002404 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 curwin->w_topline = curbuf->b_ml.ml_line_count;
2406#ifdef FEAT_DIFF
2407 curwin->w_topfill = 0;
2408#endif
2409 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2410 }
2411 else
2412 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002413 // For the overlap, start with the line just below the window
2414 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 loff.lnum = curwin->w_botline;
2416#ifdef FEAT_DIFF
2417 loff.fill = diff_check_fill(curwin, loff.lnum)
2418 - curwin->w_filler_rows;
2419#endif
2420 get_scroll_overlap(&loff, -1);
2421 curwin->w_topline = loff.lnum;
2422#ifdef FEAT_DIFF
2423 curwin->w_topfill = loff.fill;
2424 check_topfill(curwin, FALSE);
2425#endif
2426 curwin->w_cursor.lnum = curwin->w_topline;
2427 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2428 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2429 }
2430 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
2433#ifdef FEAT_DIFF
2434 if (curwin->w_topline == 1)
2435 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002436 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 max_topfill();
2438 continue;
2439 }
2440#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002441 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002442 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002443 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002444 if (p_window <= 2)
2445 --curwin->w_topline;
2446 else
2447 curwin->w_topline -= p_window - 2;
2448 if (curwin->w_topline < 1)
2449 curwin->w_topline = 1;
2450 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2451 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2452 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2453 continue;
2454 }
2455
Bram Moolenaar85a20022019-12-21 18:25:54 +01002456 // Find the line at the top of the window that is going to be the
2457 // line at the bottom of the window. Make sure this results in
2458 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 loff.lnum = curwin->w_topline - 1;
2460#ifdef FEAT_DIFF
2461 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2462 - curwin->w_topfill;
2463#endif
2464 get_scroll_overlap(&loff, 1);
2465
2466 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2467 {
2468 loff.lnum = curbuf->b_ml.ml_line_count;
2469#ifdef FEAT_DIFF
2470 loff.fill = 0;
2471 }
2472 else
2473 {
2474 botline_topline(&loff);
2475#endif
2476 }
2477 curwin->w_cursor.lnum = loff.lnum;
2478
Bram Moolenaar85a20022019-12-21 18:25:54 +01002479 // Find the line just above the new topline to get the right line
2480 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 n = 0;
2482 while (n <= curwin->w_height && loff.lnum >= 1)
2483 {
2484 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002485 if (loff.height == MAXCOL)
2486 n = MAXCOL;
2487 else
2488 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002490 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
2492 curwin->w_topline = 1;
2493#ifdef FEAT_DIFF
2494 max_topfill();
2495#endif
2496 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2497 }
2498 else
2499 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002500 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501#ifdef FEAT_DIFF
2502 topline_botline(&loff);
2503#endif
2504 botline_forw(&loff);
2505 botline_forw(&loff);
2506#ifdef FEAT_DIFF
2507 botline_topline(&loff);
2508#endif
2509#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002510 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2512#endif
2513
Bram Moolenaar85a20022019-12-21 18:25:54 +01002514 // Always scroll at least one line. Avoid getting stuck on
2515 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 if (loff.lnum >= curwin->w_topline
2517#ifdef FEAT_DIFF
2518 && (loff.lnum > curwin->w_topline
2519 || loff.fill >= curwin->w_topfill)
2520#endif
2521 )
2522 {
2523#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002524 // First try using the maximum number of filler lines. If
2525 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 loff.fill = curwin->w_topfill;
2527 if (curwin->w_topfill < diff_check_fill(curwin,
2528 curwin->w_topline))
2529 max_topfill();
2530 if (curwin->w_topfill == loff.fill)
2531#endif
2532 {
2533 --curwin->w_topline;
2534#ifdef FEAT_DIFF
2535 curwin->w_topfill = 0;
2536#endif
2537 }
2538 comp_botline(curwin);
2539 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002540 curwin->w_valid &=
2541 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 else
2544 {
2545 curwin->w_topline = loff.lnum;
2546#ifdef FEAT_DIFF
2547 curwin->w_topfill = loff.fill;
2548 check_topfill(curwin, FALSE);
2549#endif
2550 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2551 }
2552 }
2553 }
2554 }
2555#ifdef FEAT_FOLDING
2556 foldAdjustCursor();
2557#endif
2558 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002559 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002560 if (retval == OK)
2561 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2563
Bram Moolenaar907dad72018-07-10 15:07:15 +02002564 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002566 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2567 // But make sure we scroll at least one line (happens with mix of long
2568 // wrapping lines and non-wrapping line).
2569 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002571 scroll_cursor_top(1, FALSE);
2572 if (curwin->w_topline <= old_topline
2573 && old_topline < curbuf->b_ml.ml_line_count)
2574 {
2575 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002577 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2578#endif
2579 }
2580 }
2581#ifdef FEAT_FOLDING
2582 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586
2587 redraw_later(VALID);
2588 return retval;
2589}
2590
2591/*
2592 * Decide how much overlap to use for page-up or page-down scrolling.
2593 * This is symmetric, so that doing both keeps the same lines displayed.
2594 * Three lines are examined:
2595 *
2596 * before CTRL-F after CTRL-F / before CTRL-B
2597 * etc. l1
2598 * l1 last but one line ------------
2599 * l2 last text line l2 top text line
2600 * ------------- l3 second text line
2601 * l3 etc.
2602 */
2603 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002604get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606 int h1, h2, h3, h4;
2607 int min_height = curwin->w_height - 2;
2608 lineoff_T loff0, loff1, loff2;
2609
2610#ifdef FEAT_DIFF
2611 if (lp->fill > 0)
2612 lp->height = 1;
2613 else
2614 lp->height = plines_nofill(lp->lnum);
2615#else
2616 lp->height = plines(lp->lnum);
2617#endif
2618 h1 = lp->height;
2619 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002620 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622 loff0 = *lp;
2623 if (dir > 0)
2624 botline_forw(lp);
2625 else
2626 topline_back(lp);
2627 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002628 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002630 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 return;
2632 }
2633
2634 loff1 = *lp;
2635 if (dir > 0)
2636 botline_forw(lp);
2637 else
2638 topline_back(lp);
2639 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002640 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002642 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 return;
2644 }
2645
2646 loff2 = *lp;
2647 if (dir > 0)
2648 botline_forw(lp);
2649 else
2650 topline_back(lp);
2651 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002652 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002653 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002655 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 return;
2657}
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659/*
2660 * Scroll 'scroll' lines up or down.
2661 */
2662 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002663halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665 long scrolled = 0;
2666 int i;
2667 int n;
2668 int room;
2669
2670 if (Prenum)
2671 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2672 curwin->w_height : Prenum;
2673 n = (curwin->w_p_scr <= curwin->w_height) ?
2674 curwin->w_p_scr : curwin->w_height;
2675
Bram Moolenaard5d37532017-03-27 23:02:07 +02002676 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 validate_botline();
2678 room = curwin->w_empty_rows;
2679#ifdef FEAT_DIFF
2680 room += curwin->w_filler_rows;
2681#endif
2682 if (flag)
2683 {
2684 /*
2685 * scroll the text up
2686 */
2687 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2688 {
2689#ifdef FEAT_DIFF
2690 if (curwin->w_topfill > 0)
2691 {
2692 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002693 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 --curwin->w_topfill;
2695 }
2696 else
2697#endif
2698 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002699 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 n -= i;
2701 if (n < 0 && scrolled > 0)
2702 break;
2703#ifdef FEAT_FOLDING
2704 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2705#endif
2706 ++curwin->w_topline;
2707#ifdef FEAT_DIFF
2708 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2709#endif
2710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2712 {
2713 ++curwin->w_cursor.lnum;
2714 curwin->w_valid &=
2715 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2719 scrolled += i;
2720
2721 /*
2722 * Correct w_botline for changed w_topline.
2723 * Won't work when there are filler lines.
2724 */
2725#ifdef FEAT_DIFF
2726 if (curwin->w_p_diff)
2727 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2728 else
2729#endif
2730 {
2731 room += i;
2732 do
2733 {
2734 i = plines(curwin->w_botline);
2735 if (i > room)
2736 break;
2737#ifdef FEAT_FOLDING
2738 (void)hasFolding(curwin->w_botline, NULL,
2739 &curwin->w_botline);
2740#endif
2741 ++curwin->w_botline;
2742 room -= i;
2743 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2744 }
2745 }
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 /*
2748 * When hit bottom of the file: move cursor down.
2749 */
2750 if (n > 0)
2751 {
2752# ifdef FEAT_FOLDING
2753 if (hasAnyFolding(curwin))
2754 {
2755 while (--n >= 0
2756 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2757 {
2758 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2759 &curwin->w_cursor.lnum);
2760 ++curwin->w_cursor.lnum;
2761 }
2762 }
2763 else
2764# endif
2765 curwin->w_cursor.lnum += n;
2766 check_cursor_lnum();
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else
2770 {
2771 /*
2772 * scroll the text down
2773 */
2774 while (n > 0 && curwin->w_topline > 1)
2775 {
2776#ifdef FEAT_DIFF
2777 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2778 {
2779 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002780 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 ++curwin->w_topfill;
2782 }
2783 else
2784#endif
2785 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002786 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 n -= i;
2788 if (n < 0 && scrolled > 0)
2789 break;
2790 --curwin->w_topline;
2791#ifdef FEAT_FOLDING
2792 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2793#endif
2794#ifdef FEAT_DIFF
2795 curwin->w_topfill = 0;
2796#endif
2797 }
2798 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2799 VALID_BOTLINE|VALID_BOTLINE_AP);
2800 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (curwin->w_cursor.lnum > 1)
2802 {
2803 --curwin->w_cursor.lnum;
2804 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002807
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 /*
2809 * When hit top of the file: move cursor up.
2810 */
2811 if (n > 0)
2812 {
2813 if (curwin->w_cursor.lnum <= (linenr_T)n)
2814 curwin->w_cursor.lnum = 1;
2815 else
2816# ifdef FEAT_FOLDING
2817 if (hasAnyFolding(curwin))
2818 {
2819 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2820 {
2821 --curwin->w_cursor.lnum;
2822 (void)hasFolding(curwin->w_cursor.lnum,
2823 &curwin->w_cursor.lnum, NULL);
2824 }
2825 }
2826 else
2827# endif
2828 curwin->w_cursor.lnum -= n;
2829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
2831# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002832 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 foldAdjustCursor();
2834# endif
2835#ifdef FEAT_DIFF
2836 check_topfill(curwin, !flag);
2837#endif
2838 cursor_correct();
2839 beginline(BL_SOL | BL_FIX);
2840 redraw_later(VALID);
2841}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002842
Bram Moolenaar860cae12010-06-05 23:22:07 +02002843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002844do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002845{
2846 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002847 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002848 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002849 colnr_T curswant = curwin->w_curswant;
2850 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002851 win_T *old_curwin = curwin;
2852 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002853 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002854 int old_VIsual_select = VIsual_select;
2855 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002856
2857 /*
2858 * loop through the cursorbound windows
2859 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002860 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002861 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002862 {
2863 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002864 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002865 if (curwin != old_curwin && curwin->w_p_crb)
2866 {
2867# ifdef FEAT_DIFF
2868 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002869 curwin->w_cursor.lnum =
2870 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002871 else
2872# endif
2873 curwin->w_cursor.lnum = line;
2874 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002875 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002876 curwin->w_curswant = curswant;
2877 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002878
Bram Moolenaar85a20022019-12-21 18:25:54 +01002879 // Make sure the cursor is in a valid position. Temporarily set
2880 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002881 restart_edit_save = restart_edit;
2882 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002883 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002884# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002885 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002886 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002887# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002888 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002889 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002890 if (has_mbyte)
2891 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002892 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002893
Bram Moolenaar85a20022019-12-21 18:25:54 +01002894 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002895 if (!curwin->w_p_scb)
2896 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002897 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002898 }
2899 }
2900
2901 /*
2902 * reset current-window
2903 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002904 VIsual_select = old_VIsual_select;
2905 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002906 curwin = old_curwin;
2907 curbuf = old_curbuf;
2908}