blob: 69091fd3da4f25c0a4ddd4cc903ea6bb8bacc6fc [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 Moolenaar4792a672020-11-15 21:11:18 +0100871#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100872 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875}
876
877/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200878 * Compute offset of a window, occupied by absolute or relative line number,
879 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 */
881 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100882win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
Bram Moolenaar64486672010-05-16 15:46:46 +0200884 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#ifdef FEAT_CMDWIN
886 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
887#endif
888#ifdef FEAT_FOLDING
889 + wp->w_p_fdc
890#endif
891#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200892 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
894 );
895}
896
897 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100898curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900 return win_col_off(curwin);
901}
902
903/*
904 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200905 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
906 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 */
908 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100909win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
Bram Moolenaar64486672010-05-16 15:46:46 +0200911 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000912 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 return 0;
914}
915
916 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100917curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 return win_col_off2(curwin);
920}
921
922/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100923 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 * Also updates curwin->w_wrow and curwin->w_cline_row.
925 * Also updates curwin->w_leftcol.
926 */
927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100928curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100929 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100932 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 int off_left, off_right;
934 int n;
935 int p_lines;
936 int width = 0;
937 int textwidth;
938 int new_leftcol;
939 colnr_T startcol;
940 colnr_T endcol;
941 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100942 long so = get_scrolloff_value();
943 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /*
946 * First make sure that w_topline is valid (after moving the cursor).
947 */
948 update_topline();
949
950 /*
951 * Next make sure that w_cline_row is valid.
952 */
953 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100954 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 /*
957 * Compute the number of virtual columns.
958 */
959#ifdef FEAT_FOLDING
960 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100961 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
963 else
964#endif
965 getvvcol(curwin, &curwin->w_cursor,
966 &startcol, &(curwin->w_virtcol), &endcol);
967
Bram Moolenaar85a20022019-12-21 18:25:54 +0100968 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100970 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 extra = curwin_col_off();
973 curwin->w_wcol = curwin->w_virtcol + extra;
974 endcol += extra;
975
976 /*
977 * Now compute w_wrow, counting screen lines from w_cline_row.
978 */
979 curwin->w_wrow = curwin->w_cline_row;
980
Bram Moolenaar02631462017-09-22 15:20:32 +0200981 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (textwidth <= 0)
983 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100984 // No room for text, put cursor in last char of window.
Bram Moolenaar02631462017-09-22 15:20:32 +0200985 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 curwin->w_wrow = curwin->w_height - 1;
987 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200988 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
990 width = textwidth + curwin_col_off2();
991
Bram Moolenaar85a20022019-12-21 18:25:54 +0100992 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +0200993 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100995#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +0100996 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100997#endif
Bram Moolenaaree857022019-11-09 23:26:40 +0100998
Bram Moolenaar85a20022019-12-21 18:25:54 +0100999 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001000 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 curwin->w_wcol -= n * width;
1002 curwin->w_wrow += n;
1003
1004#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001005 // When cursor wraps to first char of next line in Insert
1006 // mode, the 'showbreak' string isn't shown, backup to first
1007 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001008 sbr = get_showbreak_value(curwin);
1009 if (*sbr && *ml_get_cursor() == NUL
1010 && curwin->w_wcol == (int)vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 curwin->w_wcol = 0;
1012#endif
1013 }
1014 }
1015
Bram Moolenaar85a20022019-12-21 18:25:54 +01001016 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1017 // is not folded.
1018 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001019 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_FOLDING
1021 && !curwin->w_cline_folded
1022#endif
1023 )
1024 {
1025 /*
1026 * If Cursor is left of the screen, scroll rightwards.
1027 * If Cursor is right of the screen, scroll leftwards
1028 * If we get closer to the edge than 'sidescrolloff', scroll a little
1029 * extra
1030 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001031 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001032 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001033 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (off_left < 0 || off_right > 0)
1035 {
1036 if (off_left < 0)
1037 diff = -off_left;
1038 else
1039 diff = off_right;
1040
Bram Moolenaar85a20022019-12-21 18:25:54 +01001041 // When far off or not enough room on either side, put cursor in
1042 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1044 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1045 else
1046 {
1047 if (diff < p_ss)
1048 diff = p_ss;
1049 if (off_left < 0)
1050 new_leftcol = curwin->w_leftcol - diff;
1051 else
1052 new_leftcol = curwin->w_leftcol + diff;
1053 }
1054 if (new_leftcol < 0)
1055 new_leftcol = 0;
1056 if (new_leftcol != (int)curwin->w_leftcol)
1057 {
1058 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001059 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 redraw_later(NOT_VALID);
1061 }
1062 }
1063 curwin->w_wcol -= curwin->w_leftcol;
1064 }
1065 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1066 curwin->w_wcol -= curwin->w_leftcol;
1067 else
1068 curwin->w_wcol = 0;
1069
1070#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001071 // Skip over filler lines. At the top use w_topfill, there
1072 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (curwin->w_cursor.lnum == curwin->w_topline)
1074 curwin->w_wrow += curwin->w_topfill;
1075 else
1076 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1077#endif
1078
1079 prev_skipcol = curwin->w_skipcol;
1080
1081 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if ((curwin->w_wrow >= curwin->w_height
1084 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001085 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 && (p_lines =
1087#ifdef FEAT_DIFF
1088 plines_win_nofill
1089#else
1090 plines_win
1091#endif
1092 (curwin, curwin->w_cursor.lnum, FALSE))
1093 - 1 >= curwin->w_height))
1094 && curwin->w_height != 0
1095 && curwin->w_cursor.lnum == curwin->w_topline
1096 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001097 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001099 // Cursor past end of screen. Happens with a single line that does
1100 // not fit on screen. Find a skipcol to show the text around the
1101 // cursor. Avoid scrolling all the time. compute value of "extra":
1102 // 1: Less than 'scrolloff' lines above
1103 // 2: Less than 'scrolloff' lines below
1104 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001106 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001108 // Compute last display line of the buffer line that we want at the
1109 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (p_lines == 0)
1111 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1112 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001113 if (p_lines > curwin->w_wrow + so)
1114 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 else
1116 n = p_lines;
1117 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1118 extra += 2;
1119
Bram Moolenaar375e3392019-01-31 18:26:10 +01001120 if (extra == 3 || p_lines < so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001122 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 n = curwin->w_virtcol / width;
1124 if (n > curwin->w_height / 2)
1125 n -= curwin->w_height / 2;
1126 else
1127 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (n > p_lines - curwin->w_height + 1)
1130 n = p_lines - curwin->w_height + 1;
1131 curwin->w_skipcol = n * width;
1132 }
1133 else if (extra == 1)
1134 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001135 // less then 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001136 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 + width - 1) / width;
1138 if (extra > 0)
1139 {
1140 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1141 extra = curwin->w_skipcol / width;
1142 curwin->w_skipcol -= extra * width;
1143 }
1144 }
1145 else if (extra == 2)
1146 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001147 // less then 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 endcol = (n - curwin->w_height + 1) * width;
1149 while (endcol > curwin->w_virtcol)
1150 endcol -= width;
1151 if (endcol > curwin->w_skipcol)
1152 curwin->w_skipcol = endcol;
1153 }
1154
1155 curwin->w_wrow -= curwin->w_skipcol / width;
1156 if (curwin->w_wrow >= curwin->w_height)
1157 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001158 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 extra = curwin->w_wrow - curwin->w_height + 1;
1160 curwin->w_skipcol += extra * width;
1161 curwin->w_wrow -= extra;
1162 }
1163
1164 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1165 if (extra > 0)
1166 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1167 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001168 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
1170 else
1171 curwin->w_skipcol = 0;
1172 if (prev_skipcol != curwin->w_skipcol)
1173 redraw_later(NOT_VALID);
1174
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001175#ifdef FEAT_SYN_HL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001176 // Redraw when w_virtcol changes and 'cursorcolumn' is set
Bram Moolenaarb6798752014-03-27 12:11:48 +01001177 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001178 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001179 redraw_later(SOME_VALID);
1180#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001181#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1182 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1183 {
1184 curwin->w_wrow += popup_top_extra(curwin);
1185 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001186 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001187 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001188 else
1189 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001190#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001191
Bram Moolenaar08f23632019-11-16 14:19:33 +01001192 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1193 curwin->w_valid_leftcol = curwin->w_leftcol;
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1196}
1197
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001198#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001199/*
1200 * Compute the screen position of text character at "pos" in window "wp"
1201 * The resulting values are one-based, zero when character is not visible.
1202 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001203 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001204textpos2screenpos(
1205 win_T *wp,
1206 pos_T *pos,
1207 int *rowp, // screen row
1208 int *scolp, // start screen column
1209 int *ccolp, // cursor screen column
1210 int *ecolp) // end screen column
1211{
1212 colnr_T scol = 0, ccol = 0, ecol = 0;
1213 int row = 0;
1214 int rowoff = 0;
1215 colnr_T coloff = 0;
1216
1217 if (pos->lnum >= wp->w_topline && pos->lnum < wp->w_botline)
1218 {
1219 colnr_T off;
1220 colnr_T col;
1221 int width;
1222
1223 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1224 getvcol(wp, pos, &scol, &ccol, &ecol);
1225
1226 // similar to what is done in validate_cursor_col()
1227 col = scol;
1228 off = win_col_off(wp);
1229 col += off;
1230 width = wp->w_width - off + win_col_off2(wp);
1231
Bram Moolenaar12034e22019-08-25 22:25:02 +02001232 // long line wrapping, adjust row
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001233 if (wp->w_p_wrap
1234 && col >= (colnr_T)wp->w_width
1235 && width > 0)
1236 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001237 // use same formula as what is used in curs_columns()
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001238 rowoff = ((col - wp->w_width) / width + 1);
1239 col -= rowoff * width;
1240 }
1241 col -= wp->w_leftcol;
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +01001242 if (col >= wp->w_width)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001243 col = -1;
1244 if (col >= 0)
1245 coloff = col - scol + wp->w_wincol + 1;
1246 else
1247 // character is left or right of the window
1248 row = scol = ccol = ecol = 0;
1249 }
Bram Moolenaar8dd46e72020-12-17 21:35:29 +01001250 *rowp = W_WINROW(wp) + row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001251 *scolp = scol + coloff;
1252 *ccolp = ccol + coloff;
1253 *ecolp = ecol + coloff;
1254}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001255#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001256
Bram Moolenaar12034e22019-08-25 22:25:02 +02001257#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001258/*
1259 * "screenpos({winid}, {lnum}, {col})" function
1260 */
1261 void
1262f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1263{
1264 dict_T *dict;
1265 win_T *wp;
1266 pos_T pos;
1267 int row = 0;
1268 int scol = 0, ccol = 0, ecol = 0;
1269
1270 if (rettv_dict_alloc(rettv) != OK)
1271 return;
1272 dict = rettv->vval.v_dict;
1273
1274 wp = find_win_by_nr_or_id(&argvars[0]);
1275 if (wp == NULL)
1276 return;
1277
1278 pos.lnum = tv_get_number(&argvars[1]);
1279 pos.col = tv_get_number(&argvars[2]) - 1;
1280 pos.coladd = 0;
1281 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1282
1283 dict_add_number(dict, "row", row);
1284 dict_add_number(dict, "col", scol);
1285 dict_add_number(dict, "curscol", ccol);
1286 dict_add_number(dict, "endcol", ecol);
1287}
1288#endif
1289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290/*
1291 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001294scrolldown(
1295 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001296 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001298 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 int wrow;
1300 int moved = FALSE;
1301
1302#ifdef FEAT_FOLDING
1303 linenr_T first;
1304
Bram Moolenaar85a20022019-12-21 18:25:54 +01001305 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1307#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001308 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 while (line_count-- > 0)
1310 {
1311#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001312 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1313 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
1315 ++curwin->w_topfill;
1316 ++done;
1317 }
1318 else
1319#endif
1320 {
1321 if (curwin->w_topline == 1)
1322 break;
1323 --curwin->w_topline;
1324#ifdef FEAT_DIFF
1325 curwin->w_topfill = 0;
1326#endif
1327#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001328 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (hasFolding(curwin->w_topline, &first, NULL))
1330 {
1331 ++done;
1332 if (!byfold)
1333 line_count -= curwin->w_topline - first - 1;
1334 curwin->w_botline -= curwin->w_topline - first;
1335 curwin->w_topline = first;
1336 }
1337 else
1338#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001339 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001341 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 invalidate_botline();
1343 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001344 curwin->w_wrow += done; // keep w_wrow updated
1345 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347#ifdef FEAT_DIFF
1348 if (curwin->w_cursor.lnum == curwin->w_topline)
1349 curwin->w_cline_row = 0;
1350 check_topfill(curwin, TRUE);
1351#endif
1352
1353 /*
1354 * Compute the row number of the last row of the cursor line
1355 * and move the cursor onto the displayed part of the window.
1356 */
1357 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001358 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 {
1360 validate_virtcol();
1361 validate_cheight();
1362 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001363 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1366 {
1367#ifdef FEAT_FOLDING
1368 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1369 {
1370 --wrow;
1371 if (first == 1)
1372 curwin->w_cursor.lnum = 1;
1373 else
1374 curwin->w_cursor.lnum = first - 1;
1375 }
1376 else
1377#endif
1378 wrow -= plines(curwin->w_cursor.lnum--);
1379 curwin->w_valid &=
1380 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1381 moved = TRUE;
1382 }
1383 if (moved)
1384 {
1385#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001386 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 foldAdjustCursor();
1388#endif
1389 coladvance(curwin->w_curswant);
1390 }
1391}
1392
1393/*
1394 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001397scrollup(
1398 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001399 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1402 linenr_T lnum;
1403
1404 if (
1405# ifdef FEAT_FOLDING
1406 (byfold && hasAnyFolding(curwin))
1407# ifdef FEAT_DIFF
1408 ||
1409# endif
1410# endif
1411# ifdef FEAT_DIFF
1412 curwin->w_p_diff
1413# endif
1414 )
1415 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001416 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 lnum = curwin->w_topline;
1418 while (line_count--)
1419 {
1420# ifdef FEAT_DIFF
1421 if (curwin->w_topfill > 0)
1422 --curwin->w_topfill;
1423 else
1424# endif
1425 {
1426# ifdef FEAT_FOLDING
1427 if (byfold)
1428 (void)hasFolding(lnum, NULL, &lnum);
1429# endif
1430 if (lnum >= curbuf->b_ml.ml_line_count)
1431 break;
1432 ++lnum;
1433# ifdef FEAT_DIFF
1434 curwin->w_topfill = diff_check_fill(curwin, lnum);
1435# endif
1436 }
1437 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001438 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 curwin->w_botline += lnum - curwin->w_topline;
1440 curwin->w_topline = lnum;
1441 }
1442 else
1443#endif
1444 {
1445 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001446 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1450 curwin->w_topline = curbuf->b_ml.ml_line_count;
1451 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1452 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1453
1454#ifdef FEAT_DIFF
1455 check_topfill(curwin, FALSE);
1456#endif
1457
1458#ifdef FEAT_FOLDING
1459 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001460 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1462#endif
1463
1464 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1465 if (curwin->w_cursor.lnum < curwin->w_topline)
1466 {
1467 curwin->w_cursor.lnum = curwin->w_topline;
1468 curwin->w_valid &=
1469 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1470 coladvance(curwin->w_curswant);
1471 }
1472}
1473
1474#ifdef FEAT_DIFF
1475/*
1476 * Don't end up with too many filler lines in the window.
1477 */
1478 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001479check_topfill(
1480 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001481 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 int n;
1484
1485 if (wp->w_topfill > 0)
1486 {
1487 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1488 if (wp->w_topfill + n > wp->w_height)
1489 {
1490 if (down && wp->w_topline > 1)
1491 {
1492 --wp->w_topline;
1493 wp->w_topfill = 0;
1494 }
1495 else
1496 {
1497 wp->w_topfill = wp->w_height - n;
1498 if (wp->w_topfill < 0)
1499 wp->w_topfill = 0;
1500 }
1501 }
1502 }
1503}
1504
1505/*
1506 * Use as many filler lines as possible for w_topline. Make sure w_topline
1507 * is still visible.
1508 */
1509 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001510max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 int n;
1513
1514 n = plines_nofill(curwin->w_topline);
1515 if (n >= curwin->w_height)
1516 curwin->w_topfill = 0;
1517 else
1518 {
1519 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1520 if (curwin->w_topfill + n > curwin->w_height)
1521 curwin->w_topfill = curwin->w_height - n;
1522 }
1523}
1524#endif
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526/*
1527 * Scroll the screen one line down, but don't do it if it would move the
1528 * cursor off the screen.
1529 */
1530 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001531scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 int end_row;
1534#ifdef FEAT_DIFF
1535 int can_fill = (curwin->w_topfill
1536 < diff_check_fill(curwin, curwin->w_topline));
1537#endif
1538
1539 if (curwin->w_topline <= 1
1540#ifdef FEAT_DIFF
1541 && !can_fill
1542#endif
1543 )
1544 return;
1545
Bram Moolenaar85a20022019-12-21 18:25:54 +01001546 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548 /*
1549 * Compute the row number of the last row of the cursor line
1550 * and make sure it doesn't go off the screen. Make sure the cursor
1551 * doesn't go past 'scrolloff' lines from the screen end.
1552 */
1553 end_row = curwin->w_wrow;
1554#ifdef FEAT_DIFF
1555 if (can_fill)
1556 ++end_row;
1557 else
1558 end_row += plines_nofill(curwin->w_topline - 1);
1559#else
1560 end_row += plines(curwin->w_topline - 1);
1561#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001562 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
1564 validate_cheight();
1565 validate_virtcol();
1566 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001567 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001569 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
1571#ifdef FEAT_DIFF
1572 if (can_fill)
1573 {
1574 ++curwin->w_topfill;
1575 check_topfill(curwin, TRUE);
1576 }
1577 else
1578 {
1579 --curwin->w_topline;
1580 curwin->w_topfill = 0;
1581 }
1582#else
1583 --curwin->w_topline;
1584#endif
1585#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001586 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001588 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1590 }
1591}
1592
1593/*
1594 * Scroll the screen one line up, but don't do it if it would move the cursor
1595 * off the screen.
1596 */
1597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001598scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
1600 int start_row;
1601
1602 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1603#ifdef FEAT_DIFF
1604 && curwin->w_topfill == 0
1605#endif
1606 )
1607 return;
1608
Bram Moolenaar85a20022019-12-21 18:25:54 +01001609 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611 /*
1612 * Compute the row number of the first row of the cursor line
1613 * and make sure it doesn't go off the screen. Make sure the cursor
1614 * doesn't go before 'scrolloff' lines from the screen start.
1615 */
1616#ifdef FEAT_DIFF
1617 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1618 - curwin->w_topfill;
1619#else
1620 start_row = curwin->w_wrow - plines(curwin->w_topline);
1621#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001622 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001625 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001627 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629#ifdef FEAT_DIFF
1630 if (curwin->w_topfill > 0)
1631 --curwin->w_topfill;
1632 else
1633#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001634 {
1635#ifdef FEAT_FOLDING
1636 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001639 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001640 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1642 }
1643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645/*
1646 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1647 * a (wrapped) text line. Uses and sets "lp->fill".
1648 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001649 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 */
1651 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001652topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654#ifdef FEAT_DIFF
1655 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1656 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001657 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 ++lp->fill;
1659 lp->height = 1;
1660 }
1661 else
1662#endif
1663 {
1664 --lp->lnum;
1665#ifdef FEAT_DIFF
1666 lp->fill = 0;
1667#endif
1668 if (lp->lnum < 1)
1669 lp->height = MAXCOL;
1670 else
1671#ifdef FEAT_FOLDING
1672 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001673 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 lp->height = 1;
1675 else
1676#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001677 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679}
1680
1681/*
1682 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1683 * a (wrapped) text line. Uses and sets "lp->fill".
1684 * Returns the height of the added line in "lp->height".
1685 * Lines below the last one are incredibly high.
1686 */
1687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001688botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690#ifdef FEAT_DIFF
1691 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1692 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001693 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 ++lp->fill;
1695 lp->height = 1;
1696 }
1697 else
1698#endif
1699 {
1700 ++lp->lnum;
1701#ifdef FEAT_DIFF
1702 lp->fill = 0;
1703#endif
1704 if (lp->lnum > curbuf->b_ml.ml_line_count)
1705 lp->height = MAXCOL;
1706 else
1707#ifdef FEAT_FOLDING
1708 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001709 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 lp->height = 1;
1711 else
1712#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001713 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715}
1716
1717#ifdef FEAT_DIFF
1718/*
1719 * Switch from including filler lines below lp->lnum to including filler
1720 * lines above loff.lnum + 1. This keeps pointing to the same line.
1721 * When there are no filler lines nothing changes.
1722 */
1723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001724botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 if (lp->fill > 0)
1727 {
1728 ++lp->lnum;
1729 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1730 }
1731}
1732
1733/*
1734 * Switch from including filler lines above lp->lnum to including filler
1735 * lines below loff.lnum - 1. This keeps pointing to the same line.
1736 * When there are no filler lines nothing changes.
1737 */
1738 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001739topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
1741 if (lp->fill > 0)
1742 {
1743 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1744 --lp->lnum;
1745 }
1746}
1747#endif
1748
1749/*
1750 * Recompute topline to put the cursor at the top of the window.
1751 * Scroll at least "min_scroll" lines.
1752 * If "always" is TRUE, always set topline (for "zt").
1753 */
1754 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001755scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 int scrolled = 0;
1758 int extra = 0;
1759 int used;
1760 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001761 linenr_T top; // just above displayed lines
1762 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 linenr_T old_topline = curwin->w_topline;
1764#ifdef FEAT_DIFF
1765 linenr_T old_topfill = curwin->w_topfill;
1766#endif
1767 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001768 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (mouse_dragging > 0)
1771 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773 /*
1774 * Decrease topline until:
1775 * - it has become 1
1776 * - (part of) the cursor line is moved off the screen or
1777 * - moved at least 'scrolljump' lines and
1778 * - at least 'scrolloff' lines above and below the cursor
1779 */
1780 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001781 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (curwin->w_cursor.lnum < curwin->w_topline)
1783 scrolled = used;
1784
1785#ifdef FEAT_FOLDING
1786 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1787 {
1788 --top;
1789 ++bot;
1790 }
1791 else
1792#endif
1793 {
1794 top = curwin->w_cursor.lnum - 1;
1795 bot = curwin->w_cursor.lnum + 1;
1796 }
1797 new_topline = top + 1;
1798
1799#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001800 // "used" already contains the number of filler lines above, don't add it
1801 // again.
1802 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001803 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#endif
1805
1806 /*
1807 * Check if the lines from "top" to "bot" fit in the window. If they do,
1808 * set new_topline and advance "top" and "bot" to include more lines.
1809 */
1810 while (top > 0)
1811 {
1812#ifdef FEAT_FOLDING
1813 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001814 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 i = 1;
1816 else
1817#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001818 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 used += i;
1820 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1821 {
1822#ifdef FEAT_FOLDING
1823 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001824 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 ++used;
1826 else
1827#endif
1828 used += plines(bot);
1829 }
1830 if (used > curwin->w_height)
1831 break;
1832 if (top < curwin->w_topline)
1833 scrolled += i;
1834
1835 /*
1836 * If scrolling is needed, scroll at least 'sj' lines.
1837 */
1838 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1839 && extra >= off)
1840 break;
1841
1842 extra += i;
1843 new_topline = top;
1844 --top;
1845 ++bot;
1846 }
1847
1848 /*
1849 * If we don't have enough space, put cursor in the middle.
1850 * This makes sure we get the same position when using "k" and "j"
1851 * in a small window.
1852 */
1853 if (used > curwin->w_height)
1854 scroll_cursor_halfway(FALSE);
1855 else
1856 {
1857 /*
1858 * If "always" is FALSE, only adjust topline to a lower value, higher
1859 * value may happen with wrapping lines
1860 */
1861 if (new_topline < curwin->w_topline || always)
1862 curwin->w_topline = new_topline;
1863 if (curwin->w_topline > curwin->w_cursor.lnum)
1864 curwin->w_topline = curwin->w_cursor.lnum;
1865#ifdef FEAT_DIFF
1866 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1867 if (curwin->w_topfill > 0 && extra > off)
1868 {
1869 curwin->w_topfill -= extra - off;
1870 if (curwin->w_topfill < 0)
1871 curwin->w_topfill = 0;
1872 }
1873 check_topfill(curwin, FALSE);
1874#endif
1875 if (curwin->w_topline != old_topline
1876#ifdef FEAT_DIFF
1877 || curwin->w_topfill != old_topfill
1878#endif
1879 )
1880 curwin->w_valid &=
1881 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1882 curwin->w_valid |= VALID_TOPLINE;
1883 }
1884}
1885
1886/*
1887 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1888 * screen lines for text lines.
1889 */
1890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001891set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
1893#ifdef FEAT_DIFF
1894 wp->w_filler_rows = 0;
1895#endif
1896 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001897 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 else
1899 {
1900 wp->w_empty_rows = wp->w_height - used;
1901#ifdef FEAT_DIFF
1902 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1903 {
1904 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1905 if (wp->w_empty_rows > wp->w_filler_rows)
1906 wp->w_empty_rows -= wp->w_filler_rows;
1907 else
1908 {
1909 wp->w_filler_rows = wp->w_empty_rows;
1910 wp->w_empty_rows = 0;
1911 }
1912 }
1913#endif
1914 }
1915}
1916
1917/*
1918 * Recompute topline to put the cursor at the bottom of the window.
1919 * Scroll at least "min_scroll" lines.
1920 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1921 * This is messy stuff!!!
1922 */
1923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001924scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925{
1926 int used;
1927 int scrolled = 0;
1928 int extra = 0;
1929 int i;
1930 linenr_T line_count;
1931 linenr_T old_topline = curwin->w_topline;
1932 lineoff_T loff;
1933 lineoff_T boff;
1934#ifdef FEAT_DIFF
1935 int old_topfill = curwin->w_topfill;
1936 int fill_below_window;
1937#endif
1938 linenr_T old_botline = curwin->w_botline;
1939 linenr_T old_valid = curwin->w_valid;
1940 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001941 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001942 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
1944 cln = curwin->w_cursor.lnum;
1945 if (set_topbot)
1946 {
1947 used = 0;
1948 curwin->w_botline = cln + 1;
1949#ifdef FEAT_DIFF
1950 loff.fill = 0;
1951#endif
1952 for (curwin->w_topline = curwin->w_botline;
1953 curwin->w_topline > 1;
1954 curwin->w_topline = loff.lnum)
1955 {
1956 loff.lnum = curwin->w_topline;
1957 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001958 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 break;
1960 used += loff.height;
1961#ifdef FEAT_DIFF
1962 curwin->w_topfill = loff.fill;
1963#endif
1964 }
1965 set_empty_rows(curwin, used);
1966 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1967 if (curwin->w_topline != old_topline
1968#ifdef FEAT_DIFF
1969 || curwin->w_topfill != old_topfill
1970#endif
1971 )
1972 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1973 }
1974 else
1975 validate_botline();
1976
Bram Moolenaar85a20022019-12-21 18:25:54 +01001977 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#ifdef FEAT_DIFF
1979 used = plines_nofill(cln);
1980#else
1981 validate_cheight();
1982 used = curwin->w_cline_height;
1983#endif
1984
Bram Moolenaar85a20022019-12-21 18:25:54 +01001985 // If the cursor is below botline, we will at least scroll by the height
1986 // of the cursor line. Correct for empty lines, which are really part of
1987 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (cln >= curwin->w_botline)
1989 {
1990 scrolled = used;
1991 if (cln == curwin->w_botline)
1992 scrolled -= curwin->w_empty_rows;
1993 }
1994
1995 /*
1996 * Stop counting lines to scroll when
1997 * - hitting start of the file
1998 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01001999 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 * - lines between botline and cursor have been counted
2001 */
2002#ifdef FEAT_FOLDING
2003 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2004#endif
2005 {
2006 loff.lnum = cln;
2007 boff.lnum = cln;
2008 }
2009#ifdef FEAT_DIFF
2010 loff.fill = 0;
2011 boff.fill = 0;
2012 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2013 - curwin->w_filler_rows;
2014#endif
2015
2016 while (loff.lnum > 1)
2017 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002018 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2019 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002021 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2023 && loff.lnum <= curwin->w_botline
2024#ifdef FEAT_DIFF
2025 && (loff.lnum < curwin->w_botline
2026 || loff.fill >= fill_below_window)
2027#endif
2028 )
2029 break;
2030
Bram Moolenaar85a20022019-12-21 18:25:54 +01002031 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002033 if (loff.height == MAXCOL)
2034 used = MAXCOL;
2035 else
2036 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (used > curwin->w_height)
2038 break;
2039 if (loff.lnum >= curwin->w_botline
2040#ifdef FEAT_DIFF
2041 && (loff.lnum > curwin->w_botline
2042 || loff.fill <= fill_below_window)
2043#endif
2044 )
2045 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002046 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 scrolled += loff.height;
2048 if (loff.lnum == curwin->w_botline
2049#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002050 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#endif
2052 )
2053 scrolled -= curwin->w_empty_rows;
2054 }
2055
2056 if (boff.lnum < curbuf->b_ml.ml_line_count)
2057 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002058 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 botline_forw(&boff);
2060 used += boff.height;
2061 if (used > curwin->w_height)
2062 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002063 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2064 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
2066 extra += boff.height;
2067 if (boff.lnum >= curwin->w_botline
2068#ifdef FEAT_DIFF
2069 || (boff.lnum + 1 == curwin->w_botline
2070 && boff.fill > curwin->w_filler_rows)
2071#endif
2072 )
2073 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002074 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 scrolled += boff.height;
2076 if (boff.lnum == curwin->w_botline
2077#ifdef FEAT_DIFF
2078 && boff.fill == 0
2079#endif
2080 )
2081 scrolled -= curwin->w_empty_rows;
2082 }
2083 }
2084 }
2085 }
2086
Bram Moolenaar85a20022019-12-21 18:25:54 +01002087 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (scrolled <= 0)
2089 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002090 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 else if (used > curwin->w_height)
2092 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002093 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 else
2095 {
2096 line_count = 0;
2097#ifdef FEAT_DIFF
2098 boff.fill = curwin->w_topfill;
2099#endif
2100 boff.lnum = curwin->w_topline - 1;
2101 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2102 {
2103 botline_forw(&boff);
2104 i += boff.height;
2105 ++line_count;
2106 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002107 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 line_count = 9999;
2109 }
2110
2111 /*
2112 * Scroll up if the cursor is off the bottom of the screen a bit.
2113 * Otherwise put it at 1/2 of the screen.
2114 */
2115 if (line_count >= curwin->w_height && line_count > min_scroll)
2116 scroll_cursor_halfway(FALSE);
2117 else
2118 scrollup(line_count, TRUE);
2119
2120 /*
2121 * If topline didn't change we need to restore w_botline and w_empty_rows
2122 * (we changed them).
2123 * If topline did change, update_screen() will set botline.
2124 */
2125 if (curwin->w_topline == old_topline && set_topbot)
2126 {
2127 curwin->w_botline = old_botline;
2128 curwin->w_empty_rows = old_empty_rows;
2129 curwin->w_valid = old_valid;
2130 }
2131 curwin->w_valid |= VALID_TOPLINE;
2132}
2133
2134/*
2135 * Recompute topline to put the cursor halfway the window
2136 * If "atend" is TRUE, also put it halfway at the end of the file.
2137 */
2138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 int above = 0;
2142 linenr_T topline;
2143#ifdef FEAT_DIFF
2144 int topfill = 0;
2145#endif
2146 int below = 0;
2147 int used;
2148 lineoff_T loff;
2149 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002150#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002151 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002154#ifdef FEAT_PROP_POPUP
2155 // if the width changed this needs to be updated first
2156 may_update_popup_position();
2157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2159#ifdef FEAT_FOLDING
2160 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2161#endif
2162#ifdef FEAT_DIFF
2163 used = plines_nofill(loff.lnum);
2164 loff.fill = 0;
2165 boff.fill = 0;
2166#else
2167 used = plines(loff.lnum);
2168#endif
2169 topline = loff.lnum;
2170 while (topline > 1)
2171 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002172 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
2174 if (boff.lnum < curbuf->b_ml.ml_line_count)
2175 {
2176 botline_forw(&boff);
2177 used += boff.height;
2178 if (used > curwin->w_height)
2179 break;
2180 below += boff.height;
2181 }
2182 else
2183 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002184 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 if (atend)
2186 ++used;
2187 }
2188 }
2189
Bram Moolenaar85a20022019-12-21 18:25:54 +01002190 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
2192 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002193 if (loff.height == MAXCOL)
2194 used = MAXCOL;
2195 else
2196 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (used > curwin->w_height)
2198 break;
2199 above += loff.height;
2200 topline = loff.lnum;
2201#ifdef FEAT_DIFF
2202 topfill = loff.fill;
2203#endif
2204 }
2205 }
2206#ifdef FEAT_FOLDING
2207 if (!hasFolding(topline, &curwin->w_topline, NULL))
2208#endif
2209 curwin->w_topline = topline;
2210#ifdef FEAT_DIFF
2211 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002212 if (old_topline > curwin->w_topline + curwin->w_height)
2213 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 check_topfill(curwin, FALSE);
2215#endif
2216 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2217 curwin->w_valid |= VALID_TOPLINE;
2218}
2219
2220/*
2221 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002222 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 * If not possible, put it at the same position as scroll_cursor_halfway().
2224 * When called topline must be valid!
2225 */
2226 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002227cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002229 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002231 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 linenr_T botline;
2233 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002234 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002236 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 /*
2239 * How many lines we would like to have above/below the cursor depends on
2240 * whether the first/last line of the file is on screen.
2241 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002242 above_wanted = so;
2243 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002244 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 above_wanted = mouse_dragging - 1;
2247 below_wanted = mouse_dragging - 1;
2248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (curwin->w_topline == 1)
2250 {
2251 above_wanted = 0;
2252 max_off = curwin->w_height / 2;
2253 if (below_wanted > max_off)
2254 below_wanted = max_off;
2255 }
2256 validate_botline();
2257 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002258 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
2260 below_wanted = 0;
2261 max_off = (curwin->w_height - 1) / 2;
2262 if (above_wanted > max_off)
2263 above_wanted = max_off;
2264 }
2265
2266 /*
2267 * If there are sufficient file-lines above and below the cursor, we can
2268 * return now.
2269 */
2270 cln = curwin->w_cursor.lnum;
2271 if (cln >= curwin->w_topline + above_wanted
2272 && cln < curwin->w_botline - below_wanted
2273#ifdef FEAT_FOLDING
2274 && !hasAnyFolding(curwin)
2275#endif
2276 )
2277 return;
2278
2279 /*
2280 * Narrow down the area where the cursor can be put by taking lines from
2281 * the top and the bottom until:
2282 * - the desired context lines are found
2283 * - the lines from the top is past the lines from the bottom
2284 */
2285 topline = curwin->w_topline;
2286 botline = curwin->w_botline - 1;
2287#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002288 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 above = curwin->w_topfill;
2290 below = curwin->w_filler_rows;
2291#endif
2292 while ((above < above_wanted || below < below_wanted) && topline < botline)
2293 {
2294 if (below < below_wanted && (below <= above || above >= above_wanted))
2295 {
2296#ifdef FEAT_FOLDING
2297 if (hasFolding(botline, &botline, NULL))
2298 ++below;
2299 else
2300#endif
2301 below += plines(botline);
2302 --botline;
2303 }
2304 if (above < above_wanted && (above < below || below >= below_wanted))
2305 {
2306#ifdef FEAT_FOLDING
2307 if (hasFolding(topline, NULL, &topline))
2308 ++above;
2309 else
2310#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002311 above += PLINES_NOFILL(topline);
2312#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002313 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (topline < botline)
2315 above += diff_check_fill(curwin, topline + 1);
2316#endif
2317 ++topline;
2318 }
2319 }
2320 if (topline == botline || botline == 0)
2321 curwin->w_cursor.lnum = topline;
2322 else if (topline > botline)
2323 curwin->w_cursor.lnum = botline;
2324 else
2325 {
2326 if (cln < topline && curwin->w_topline > 1)
2327 {
2328 curwin->w_cursor.lnum = topline;
2329 curwin->w_valid &=
2330 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2331 }
2332 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2333 {
2334 curwin->w_cursor.lnum = botline;
2335 curwin->w_valid &=
2336 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2337 }
2338 }
2339 curwin->w_valid |= VALID_TOPLINE;
2340}
2341
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002342static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344/*
2345 * move screen 'count' pages up or down and update screen
2346 *
2347 * return FAIL for failure, OK otherwise
2348 */
2349 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002350onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
2352 long n;
2353 int retval = OK;
2354 lineoff_T loff;
2355 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002356 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
Bram Moolenaar85a20022019-12-21 18:25:54 +01002358 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 beep_flush();
2361 return FAIL;
2362 }
2363
2364 for ( ; count > 0; --count)
2365 {
2366 validate_botline();
2367 /*
2368 * It's an error to move a page up when the first line is already on
2369 * the screen. It's an error to move a page down when the last line
2370 * is on the screen and the topline is 'scrolloff' lines from the
2371 * last line.
2372 */
2373 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002374 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2376 : (curwin->w_topline == 1
2377#ifdef FEAT_DIFF
2378 && curwin->w_topfill ==
2379 diff_check_fill(curwin, curwin->w_topline)
2380#endif
2381 ))
2382 {
2383 beep_flush();
2384 retval = FAIL;
2385 break;
2386 }
2387
2388#ifdef FEAT_DIFF
2389 loff.fill = 0;
2390#endif
2391 if (dir == FORWARD)
2392 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002393 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002395 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002396 if (p_window <= 2)
2397 ++curwin->w_topline;
2398 else
2399 curwin->w_topline += p_window - 2;
2400 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2401 curwin->w_topline = curbuf->b_ml.ml_line_count;
2402 curwin->w_cursor.lnum = curwin->w_topline;
2403 }
2404 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2405 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002406 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 curwin->w_topline = curbuf->b_ml.ml_line_count;
2408#ifdef FEAT_DIFF
2409 curwin->w_topfill = 0;
2410#endif
2411 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2412 }
2413 else
2414 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002415 // For the overlap, start with the line just below the window
2416 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 loff.lnum = curwin->w_botline;
2418#ifdef FEAT_DIFF
2419 loff.fill = diff_check_fill(curwin, loff.lnum)
2420 - curwin->w_filler_rows;
2421#endif
2422 get_scroll_overlap(&loff, -1);
2423 curwin->w_topline = loff.lnum;
2424#ifdef FEAT_DIFF
2425 curwin->w_topfill = loff.fill;
2426 check_topfill(curwin, FALSE);
2427#endif
2428 curwin->w_cursor.lnum = curwin->w_topline;
2429 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2430 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2431 }
2432 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002433 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435#ifdef FEAT_DIFF
2436 if (curwin->w_topline == 1)
2437 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002438 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 max_topfill();
2440 continue;
2441 }
2442#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002443 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002444 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002445 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002446 if (p_window <= 2)
2447 --curwin->w_topline;
2448 else
2449 curwin->w_topline -= p_window - 2;
2450 if (curwin->w_topline < 1)
2451 curwin->w_topline = 1;
2452 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2453 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2454 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2455 continue;
2456 }
2457
Bram Moolenaar85a20022019-12-21 18:25:54 +01002458 // Find the line at the top of the window that is going to be the
2459 // line at the bottom of the window. Make sure this results in
2460 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 loff.lnum = curwin->w_topline - 1;
2462#ifdef FEAT_DIFF
2463 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2464 - curwin->w_topfill;
2465#endif
2466 get_scroll_overlap(&loff, 1);
2467
2468 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2469 {
2470 loff.lnum = curbuf->b_ml.ml_line_count;
2471#ifdef FEAT_DIFF
2472 loff.fill = 0;
2473 }
2474 else
2475 {
2476 botline_topline(&loff);
2477#endif
2478 }
2479 curwin->w_cursor.lnum = loff.lnum;
2480
Bram Moolenaar85a20022019-12-21 18:25:54 +01002481 // Find the line just above the new topline to get the right line
2482 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 n = 0;
2484 while (n <= curwin->w_height && loff.lnum >= 1)
2485 {
2486 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002487 if (loff.height == MAXCOL)
2488 n = MAXCOL;
2489 else
2490 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002492 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 curwin->w_topline = 1;
2495#ifdef FEAT_DIFF
2496 max_topfill();
2497#endif
2498 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2499 }
2500 else
2501 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002502 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503#ifdef FEAT_DIFF
2504 topline_botline(&loff);
2505#endif
2506 botline_forw(&loff);
2507 botline_forw(&loff);
2508#ifdef FEAT_DIFF
2509 botline_topline(&loff);
2510#endif
2511#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002512 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2514#endif
2515
Bram Moolenaar85a20022019-12-21 18:25:54 +01002516 // Always scroll at least one line. Avoid getting stuck on
2517 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 if (loff.lnum >= curwin->w_topline
2519#ifdef FEAT_DIFF
2520 && (loff.lnum > curwin->w_topline
2521 || loff.fill >= curwin->w_topfill)
2522#endif
2523 )
2524 {
2525#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002526 // First try using the maximum number of filler lines. If
2527 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 loff.fill = curwin->w_topfill;
2529 if (curwin->w_topfill < diff_check_fill(curwin,
2530 curwin->w_topline))
2531 max_topfill();
2532 if (curwin->w_topfill == loff.fill)
2533#endif
2534 {
2535 --curwin->w_topline;
2536#ifdef FEAT_DIFF
2537 curwin->w_topfill = 0;
2538#endif
2539 }
2540 comp_botline(curwin);
2541 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002542 curwin->w_valid &=
2543 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545 else
2546 {
2547 curwin->w_topline = loff.lnum;
2548#ifdef FEAT_DIFF
2549 curwin->w_topfill = loff.fill;
2550 check_topfill(curwin, FALSE);
2551#endif
2552 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2553 }
2554 }
2555 }
2556 }
2557#ifdef FEAT_FOLDING
2558 foldAdjustCursor();
2559#endif
2560 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002561 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002562 if (retval == OK)
2563 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2565
Bram Moolenaar907dad72018-07-10 15:07:15 +02002566 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002568 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2569 // But make sure we scroll at least one line (happens with mix of long
2570 // wrapping lines and non-wrapping line).
2571 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002573 scroll_cursor_top(1, FALSE);
2574 if (curwin->w_topline <= old_topline
2575 && old_topline < curbuf->b_ml.ml_line_count)
2576 {
2577 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002579 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2580#endif
2581 }
2582 }
2583#ifdef FEAT_FOLDING
2584 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588
2589 redraw_later(VALID);
2590 return retval;
2591}
2592
2593/*
2594 * Decide how much overlap to use for page-up or page-down scrolling.
2595 * This is symmetric, so that doing both keeps the same lines displayed.
2596 * Three lines are examined:
2597 *
2598 * before CTRL-F after CTRL-F / before CTRL-B
2599 * etc. l1
2600 * l1 last but one line ------------
2601 * l2 last text line l2 top text line
2602 * ------------- l3 second text line
2603 * l3 etc.
2604 */
2605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002606get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 int h1, h2, h3, h4;
2609 int min_height = curwin->w_height - 2;
2610 lineoff_T loff0, loff1, loff2;
2611
2612#ifdef FEAT_DIFF
2613 if (lp->fill > 0)
2614 lp->height = 1;
2615 else
2616 lp->height = plines_nofill(lp->lnum);
2617#else
2618 lp->height = plines(lp->lnum);
2619#endif
2620 h1 = lp->height;
2621 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002622 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
2624 loff0 = *lp;
2625 if (dir > 0)
2626 botline_forw(lp);
2627 else
2628 topline_back(lp);
2629 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002630 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002632 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 return;
2634 }
2635
2636 loff1 = *lp;
2637 if (dir > 0)
2638 botline_forw(lp);
2639 else
2640 topline_back(lp);
2641 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002642 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002644 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return;
2646 }
2647
2648 loff2 = *lp;
2649 if (dir > 0)
2650 botline_forw(lp);
2651 else
2652 topline_back(lp);
2653 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002654 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002655 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002657 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 return;
2659}
2660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661/*
2662 * Scroll 'scroll' lines up or down.
2663 */
2664 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002665halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 long scrolled = 0;
2668 int i;
2669 int n;
2670 int room;
2671
2672 if (Prenum)
2673 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2674 curwin->w_height : Prenum;
2675 n = (curwin->w_p_scr <= curwin->w_height) ?
2676 curwin->w_p_scr : curwin->w_height;
2677
Bram Moolenaard5d37532017-03-27 23:02:07 +02002678 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 validate_botline();
2680 room = curwin->w_empty_rows;
2681#ifdef FEAT_DIFF
2682 room += curwin->w_filler_rows;
2683#endif
2684 if (flag)
2685 {
2686 /*
2687 * scroll the text up
2688 */
2689 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2690 {
2691#ifdef FEAT_DIFF
2692 if (curwin->w_topfill > 0)
2693 {
2694 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002695 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 --curwin->w_topfill;
2697 }
2698 else
2699#endif
2700 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002701 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 n -= i;
2703 if (n < 0 && scrolled > 0)
2704 break;
2705#ifdef FEAT_FOLDING
2706 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2707#endif
2708 ++curwin->w_topline;
2709#ifdef FEAT_DIFF
2710 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2711#endif
2712
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2714 {
2715 ++curwin->w_cursor.lnum;
2716 curwin->w_valid &=
2717 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2721 scrolled += i;
2722
2723 /*
2724 * Correct w_botline for changed w_topline.
2725 * Won't work when there are filler lines.
2726 */
2727#ifdef FEAT_DIFF
2728 if (curwin->w_p_diff)
2729 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2730 else
2731#endif
2732 {
2733 room += i;
2734 do
2735 {
2736 i = plines(curwin->w_botline);
2737 if (i > room)
2738 break;
2739#ifdef FEAT_FOLDING
2740 (void)hasFolding(curwin->w_botline, NULL,
2741 &curwin->w_botline);
2742#endif
2743 ++curwin->w_botline;
2744 room -= i;
2745 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2746 }
2747 }
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 /*
2750 * When hit bottom of the file: move cursor down.
2751 */
2752 if (n > 0)
2753 {
2754# ifdef FEAT_FOLDING
2755 if (hasAnyFolding(curwin))
2756 {
2757 while (--n >= 0
2758 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2759 {
2760 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2761 &curwin->w_cursor.lnum);
2762 ++curwin->w_cursor.lnum;
2763 }
2764 }
2765 else
2766# endif
2767 curwin->w_cursor.lnum += n;
2768 check_cursor_lnum();
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 else
2772 {
2773 /*
2774 * scroll the text down
2775 */
2776 while (n > 0 && curwin->w_topline > 1)
2777 {
2778#ifdef FEAT_DIFF
2779 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2780 {
2781 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002782 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 ++curwin->w_topfill;
2784 }
2785 else
2786#endif
2787 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002788 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 n -= i;
2790 if (n < 0 && scrolled > 0)
2791 break;
2792 --curwin->w_topline;
2793#ifdef FEAT_FOLDING
2794 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2795#endif
2796#ifdef FEAT_DIFF
2797 curwin->w_topfill = 0;
2798#endif
2799 }
2800 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2801 VALID_BOTLINE|VALID_BOTLINE_AP);
2802 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (curwin->w_cursor.lnum > 1)
2804 {
2805 --curwin->w_cursor.lnum;
2806 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002809
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 /*
2811 * When hit top of the file: move cursor up.
2812 */
2813 if (n > 0)
2814 {
2815 if (curwin->w_cursor.lnum <= (linenr_T)n)
2816 curwin->w_cursor.lnum = 1;
2817 else
2818# ifdef FEAT_FOLDING
2819 if (hasAnyFolding(curwin))
2820 {
2821 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2822 {
2823 --curwin->w_cursor.lnum;
2824 (void)hasFolding(curwin->w_cursor.lnum,
2825 &curwin->w_cursor.lnum, NULL);
2826 }
2827 }
2828 else
2829# endif
2830 curwin->w_cursor.lnum -= n;
2831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002834 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 foldAdjustCursor();
2836# endif
2837#ifdef FEAT_DIFF
2838 check_topfill(curwin, !flag);
2839#endif
2840 cursor_correct();
2841 beginline(BL_SOL | BL_FIX);
2842 redraw_later(VALID);
2843}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002844
Bram Moolenaar860cae12010-06-05 23:22:07 +02002845 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002846do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002847{
2848 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002849 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002850 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002851 colnr_T curswant = curwin->w_curswant;
2852 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002853 win_T *old_curwin = curwin;
2854 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002855 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002856 int old_VIsual_select = VIsual_select;
2857 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002858
2859 /*
2860 * loop through the cursorbound windows
2861 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002862 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002863 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002864 {
2865 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002866 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002867 if (curwin != old_curwin && curwin->w_p_crb)
2868 {
2869# ifdef FEAT_DIFF
2870 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002871 curwin->w_cursor.lnum =
2872 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002873 else
2874# endif
2875 curwin->w_cursor.lnum = line;
2876 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002877 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002878 curwin->w_curswant = curswant;
2879 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002880
Bram Moolenaar85a20022019-12-21 18:25:54 +01002881 // Make sure the cursor is in a valid position. Temporarily set
2882 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002883 restart_edit_save = restart_edit;
2884 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002885 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002886# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002887 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002888 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002889# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002890 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002891 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002892 if (has_mbyte)
2893 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002894 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002895
Bram Moolenaar85a20022019-12-21 18:25:54 +01002896 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002897 if (!curwin->w_p_scb)
2898 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002899 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002900 }
2901 }
2902
2903 /*
2904 * reset current-window
2905 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002906 VIsual_select = old_VIsual_select;
2907 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002908 curwin = old_curwin;
2909 curbuf = old_curbuf;
2910}