blob: b8e8780aa54afeb066f1e0d1332e3dc006676394 [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;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200202 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200203 return;
204 }
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 check_cursor_moved(curwin);
207 if (curwin->w_valid & VALID_TOPLINE)
208 return;
209
Bram Moolenaar85a20022019-12-21 18:25:54 +0100210 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000211 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100212 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214 old_topline = curwin->w_topline;
215#ifdef FEAT_DIFF
216 old_topfill = curwin->w_topfill;
217#endif
218
219 /*
220 * If the buffer is empty, always set topline to 1.
221 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 {
224 if (curwin->w_topline != 1)
225 redraw_later(NOT_VALID);
226 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 curwin->w_botline = 2;
228 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
231
232 /*
233 * If the cursor is above or near the top of the window, scroll the window
234 * to show the line the cursor is in, with 'scrolloff' context.
235 */
236 else
237 {
238 if (curwin->w_topline > 1)
239 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100240 // If the cursor is above topline, scrolling is always needed.
241 // If the cursor is far below topline and there is no folding,
242 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 if (curwin->w_cursor.lnum < curwin->w_topline)
244 check_topline = TRUE;
245 else if (check_top_offset())
246 check_topline = TRUE;
247 }
248#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100249 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
251 curwin->w_topline))
252 check_topline = TRUE;
253#endif
254
255 if (check_topline)
256 {
257 halfheight = curwin->w_height / 2 - 1;
258 if (halfheight < 2)
259 halfheight = 2;
260
261#ifdef FEAT_FOLDING
262 if (hasAnyFolding(curwin))
263 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100264 // Count the number of logical lines between the cursor and
265 // topline + scrolloff (approximation of how much will be
266 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 n = 0;
268 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100269 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 {
271 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100272 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
274 break;
275 (void)hasFolding(lnum, NULL, &lnum);
276 }
277 }
278 else
279#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100280 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281
Bram Moolenaar85a20022019-12-21 18:25:54 +0100282 // If we weren't very close to begin with, we scroll to put the
283 // cursor in the middle of the window. Otherwise put the cursor
284 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 if (n >= halfheight)
286 scroll_cursor_halfway(FALSE);
287 else
288 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000289 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 check_botline = TRUE;
291 }
292 }
293
294 else
295 {
296#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100297 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
299#endif
300 check_botline = TRUE;
301 }
302 }
303
304 /*
305 * If the cursor is below the bottom of the window, scroll the window
306 * to put the cursor on the window.
307 * When w_botline is invalid, recompute it first, to avoid a redraw later.
308 * If w_botline was approximated, we might need a redraw later in a few
309 * cases, but we don't want to spend (a lot of) time recomputing w_botline
310 * for every small change.
311 */
312 if (check_botline)
313 {
314 if (!(curwin->w_valid & VALID_BOTLINE_AP))
315 validate_botline();
316
317 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
318 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000319 if (curwin->w_cursor.lnum < curwin->w_botline)
320 {
321 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100322 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_FOLDING
324 || hasAnyFolding(curwin)
325#endif
326 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 lineoff_T loff;
329
Bram Moolenaar85a20022019-12-21 18:25:54 +0100330 // Cursor is (a few lines) above botline, check if there are
331 // 'scrolloff' window lines below the cursor. If not, need to
332 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 n = curwin->w_empty_rows;
334 loff.lnum = curwin->w_cursor.lnum;
335#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100336 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
338#endif
339#ifdef FEAT_DIFF
340 loff.fill = 0;
341 n += curwin->w_filler_rows;
342#endif
343 loff.height = 0;
344 while (loff.lnum < curwin->w_botline
345#ifdef FEAT_DIFF
346 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
347#endif
348 )
349 {
350 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100351 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 break;
353 botline_forw(&loff);
354 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100355 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000358 }
359 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100360 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000361 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 }
363 if (check_botline)
364 {
365#ifdef FEAT_FOLDING
366 if (hasAnyFolding(curwin))
367 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100368 // Count the number of logical lines between the cursor and
369 // botline - scrolloff (approximation of how much will be
370 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 line_count = 0;
372 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
375 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100376 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 if (lnum <= 0 || line_count > curwin->w_height + 1)
378 break;
379 (void)hasFolding(lnum, &lnum, NULL);
380 }
381 }
382 else
383#endif
384 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100385 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000387 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 else
389 scroll_cursor_halfway(FALSE);
390 }
391 }
392 }
393 curwin->w_valid |= VALID_TOPLINE;
394
395 /*
396 * Need to redraw when topline changed.
397 */
398 if (curwin->w_topline != old_topline
399#ifdef FEAT_DIFF
400 || curwin->w_topfill != old_topfill
401#endif
402 )
403 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100404 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000405 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {
407 curwin->w_skipcol = 0;
408 redraw_later(NOT_VALID);
409 }
410 else
411 redraw_later(VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100412 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (curwin->w_cursor.lnum == curwin->w_topline)
414 validate_cursor();
415 }
416
Bram Moolenaar375e3392019-01-31 18:26:10 +0100417 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418}
419
420/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000421 * Return the scrolljump value to use for the current window.
422 * When 'scrolljump' is positive use it as-is.
423 * When 'scrolljump' is negative use it as a percentage of the window height.
424 */
425 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100426scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000427{
428 if (p_sj >= 0)
429 return (int)p_sj;
430 return (curwin->w_height * -p_sj) / 100;
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
435 * current window.
436 */
437 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100438check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 lineoff_T loff;
441 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100442 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
Bram Moolenaar375e3392019-01-31 18:26:10 +0100444 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#ifdef FEAT_FOLDING
446 || hasAnyFolding(curwin)
447#endif
448 )
449 {
450 loff.lnum = curwin->w_cursor.lnum;
451#ifdef FEAT_DIFF
452 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100453 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454#else
455 n = 0;
456#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100457 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
460 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100461 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 if (loff.lnum < curwin->w_topline
463#ifdef FEAT_DIFF
464 || (loff.lnum == curwin->w_topline && loff.fill > 0)
465#endif
466 )
467 break;
468 n += loff.height;
469 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100470 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 return TRUE;
472 }
473 return FALSE;
474}
475
476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100477update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 if (curwin->w_set_curswant)
480 {
481 validate_virtcol();
482 curwin->w_curswant = curwin->w_virtcol;
483 curwin->w_set_curswant = FALSE;
484 }
485}
486
487/*
488 * Check if the cursor has moved. Set the w_valid flag accordingly.
489 */
490 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100491check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
494 {
495 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
496 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
497 wp->w_valid_cursor = wp->w_cursor;
498 wp->w_valid_leftcol = wp->w_leftcol;
499 }
500 else if (wp->w_cursor.col != wp->w_valid_cursor.col
501 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100502 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 {
504 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
505 wp->w_valid_cursor.col = wp->w_cursor.col;
506 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 }
509}
510
511/*
512 * Call this function when some window settings have changed, which require
513 * the cursor position, botline and topline to be recomputed and the window to
514 * be redrawn. E.g, when changing the 'wrap' option or folding.
515 */
516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100517changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 changed_window_setting_win(curwin);
520}
521
522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 wp->w_lines_valid = 0;
526 changed_line_abv_curs_win(wp);
527 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
528 redraw_win_later(wp, NOT_VALID);
529}
530
531/*
532 * Set wp->w_topline to a certain number.
533 */
534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200537#ifdef FEAT_DIFF
538 linenr_T prev_topline = wp->w_topline;
539#endif
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100542 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
544#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100545 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100547 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
548 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000550 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200552 if (lnum != prev_topline)
553 // Keep the filler lines when the topline didn't change.
554 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#endif
556 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100557 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 redraw_later(VALID);
559}
560
561/*
562 * Call this function when the length of the cursor line (in screen
563 * characters) has changed, and the change is before the cursor.
564 * Need to take care of w_botline separately!
565 */
566 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100567changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
570 |VALID_CHEIGHT|VALID_TOPLINE);
571}
572
573 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100574changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
576 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
577 |VALID_CHEIGHT|VALID_TOPLINE);
578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580/*
581 * Call this function when the length of a line (in screen characters) above
582 * the cursor have changed.
583 * Need to take care of w_botline separately!
584 */
585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100586changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
589 |VALID_CHEIGHT|VALID_TOPLINE);
590}
591
592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100593changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
596 |VALID_CHEIGHT|VALID_TOPLINE);
597}
598
599/*
600 * Make sure the value of curwin->w_botline is valid.
601 */
602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100603validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100605 validate_botline_win(curwin);
606}
607
608/*
609 * Make sure the value of wp->w_botline is valid.
610 */
611 void
612validate_botline_win(win_T *wp)
613{
614 if (!(wp->w_valid & VALID_BOTLINE))
615 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616}
617
618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 * Mark curwin->w_botline as invalid (because of some change in the buffer).
620 */
621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100622invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
625}
626
627 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100628invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
631}
632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100634approximate_botline_win(
635 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 wp->w_valid &= ~VALID_BOTLINE;
638}
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640/*
641 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
642 */
643 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 check_cursor_moved(curwin);
647 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
648 (VALID_WROW|VALID_WCOL));
649}
650
651/*
652 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
653 * w_topline must be valid, you may need to call update_topline() first!
654 */
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 check_cursor_moved(curwin);
659 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
660 curs_columns(TRUE);
661}
662
663#if defined(FEAT_GUI) || defined(PROTO)
664/*
665 * validate w_cline_row.
666 */
667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100668validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 /*
671 * First make sure that w_topline is valid (after moving the cursor).
672 */
673 update_topline();
674 check_cursor_moved(curwin);
675 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100676 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677}
678#endif
679
680/*
681 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200682 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 */
684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100685curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
687 linenr_T lnum;
688 int i;
689 int all_invalid;
690 int valid;
691#ifdef FEAT_FOLDING
692 long fold_count;
693#endif
694
Bram Moolenaar85a20022019-12-21 18:25:54 +0100695 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 all_invalid = (!redrawing()
697 || wp->w_lines_valid == 0
698 || wp->w_lines[0].wl_lnum > wp->w_topline);
699 i = 0;
700 wp->w_cline_row = 0;
701 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
702 {
703 valid = FALSE;
704 if (!all_invalid && i < wp->w_lines_valid)
705 {
706 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100707 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (wp->w_lines[i].wl_lnum == lnum)
709 {
710#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100711 // Check for newly inserted lines below this row, in which
712 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (!wp->w_buffer->b_mod_set
714 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
715 || wp->w_buffer->b_mod_top
716 > wp->w_lines[i].wl_lastlnum + 1)
717#endif
718 valid = TRUE;
719 }
720 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100721 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 }
723 if (valid
724#ifdef FEAT_DIFF
725 && (lnum != wp->w_topline || !wp->w_p_diff)
726#endif
727 )
728 {
729#ifdef FEAT_FOLDING
730 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100731 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (lnum > wp->w_cursor.lnum)
733 break;
734#else
735 ++lnum;
736#endif
737 wp->w_cline_row += wp->w_lines[i].wl_size;
738 }
739 else
740 {
741#ifdef FEAT_FOLDING
742 fold_count = foldedCount(wp, lnum, NULL);
743 if (fold_count)
744 {
745 lnum += fold_count;
746 if (lnum > wp->w_cursor.lnum)
747 break;
748 ++wp->w_cline_row;
749 }
750 else
751#endif
752#ifdef FEAT_DIFF
753 if (lnum == wp->w_topline)
754 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
755 + wp->w_topfill;
756 else
757#endif
758 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
759 }
760 }
761
762 check_cursor_moved(wp);
763 if (!(wp->w_valid & VALID_CHEIGHT))
764 {
765 if (all_invalid
766 || i == wp->w_lines_valid
767 || (i < wp->w_lines_valid
768 && (!wp->w_lines[i].wl_valid
769 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
770 {
771#ifdef FEAT_DIFF
772 if (wp->w_cursor.lnum == wp->w_topline)
773 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
774 TRUE) + wp->w_topfill;
775 else
776#endif
777 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
778#ifdef FEAT_FOLDING
779 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
780 NULL, NULL, TRUE, NULL);
781#endif
782 }
783 else if (i > wp->w_lines_valid)
784 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100785 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 wp->w_cline_height = 0;
787#ifdef FEAT_FOLDING
788 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
789 NULL, NULL, TRUE, NULL);
790#endif
791 }
792 else
793 {
794 wp->w_cline_height = wp->w_lines[i].wl_size;
795#ifdef FEAT_FOLDING
796 wp->w_cline_folded = wp->w_lines[i].wl_folded;
797#endif
798 }
799 }
800
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100801 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804}
805
806/*
807 * Validate curwin->w_virtcol only.
808 */
809 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100810validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 validate_virtcol_win(curwin);
813}
814
815/*
816 * Validate wp->w_virtcol only.
817 */
818 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 check_cursor_moved(wp);
822 if (!(wp->w_valid & VALID_VIRTCOL))
823 {
824 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
825 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000826#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200827 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000828 redraw_win_later(wp, SOME_VALID);
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
831}
832
833/*
834 * Validate curwin->w_cline_height only.
835 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 check_cursor_moved(curwin);
840 if (!(curwin->w_valid & VALID_CHEIGHT))
841 {
842#ifdef FEAT_DIFF
843 if (curwin->w_cursor.lnum == curwin->w_topline)
844 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
845 + curwin->w_topfill;
846 else
847#endif
848 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
849#ifdef FEAT_FOLDING
850 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
851#endif
852 curwin->w_valid |= VALID_CHEIGHT;
853 }
854}
855
856/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000857 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 */
859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100860validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
862 colnr_T off;
863 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100864 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 validate_virtcol();
867 if (!(curwin->w_valid & VALID_WCOL))
868 {
869 col = curwin->w_virtcol;
870 off = curwin_col_off();
871 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200872 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
Bram Moolenaar85a20022019-12-21 18:25:54 +0100874 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000875 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200876 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100877 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100878 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200879 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000880 if (col > (int)curwin->w_leftcol)
881 col -= curwin->w_leftcol;
882 else
883 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100887#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100888 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 }
891}
892
893/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200894 * Compute offset of a window, occupied by absolute or relative line number,
895 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 */
897 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100898win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
Bram Moolenaar64486672010-05-16 15:46:46 +0200900 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#ifdef FEAT_CMDWIN
902 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
903#endif
904#ifdef FEAT_FOLDING
905 + wp->w_p_fdc
906#endif
907#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200908 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#endif
910 );
911}
912
913 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100914curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 return win_col_off(curwin);
917}
918
919/*
920 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200921 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
922 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100925win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
Bram Moolenaar64486672010-05-16 15:46:46 +0200927 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000928 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 return 0;
930}
931
932 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100933curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934{
935 return win_col_off2(curwin);
936}
937
938/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100939 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Also updates curwin->w_wrow and curwin->w_cline_row.
941 * Also updates curwin->w_leftcol.
942 */
943 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100944curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100945 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100948 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 int off_left, off_right;
950 int n;
951 int p_lines;
952 int width = 0;
953 int textwidth;
954 int new_leftcol;
955 colnr_T startcol;
956 colnr_T endcol;
957 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100958 long so = get_scrolloff_value();
959 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 /*
962 * First make sure that w_topline is valid (after moving the cursor).
963 */
964 update_topline();
965
966 /*
967 * Next make sure that w_cline_row is valid.
968 */
969 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100970 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 /*
973 * Compute the number of virtual columns.
974 */
975#ifdef FEAT_FOLDING
976 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100977 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
979 else
980#endif
981 getvvcol(curwin, &curwin->w_cursor,
982 &startcol, &(curwin->w_virtcol), &endcol);
983
Bram Moolenaar85a20022019-12-21 18:25:54 +0100984 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100986 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 extra = curwin_col_off();
989 curwin->w_wcol = curwin->w_virtcol + extra;
990 endcol += extra;
991
992 /*
993 * Now compute w_wrow, counting screen lines from w_cline_row.
994 */
995 curwin->w_wrow = curwin->w_cline_row;
996
Bram Moolenaar02631462017-09-22 15:20:32 +0200997 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (textwidth <= 0)
999 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001000 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001001 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001002 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001003 if (curwin->w_p_wrap)
1004 curwin->w_wrow = curwin->w_height - 1;
1005 else
1006 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001008 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
1010 width = textwidth + curwin_col_off2();
1011
Bram Moolenaar85a20022019-12-21 18:25:54 +01001012 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001013 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001015#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001016 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001017#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001018
Bram Moolenaar85a20022019-12-21 18:25:54 +01001019 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001020 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 curwin->w_wcol -= n * width;
1022 curwin->w_wrow += n;
1023
1024#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001025 // When cursor wraps to first char of next line in Insert
1026 // mode, the 'showbreak' string isn't shown, backup to first
1027 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001028 sbr = get_showbreak_value(curwin);
1029 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001030 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 curwin->w_wcol = 0;
1032#endif
1033 }
1034 }
1035
Bram Moolenaar85a20022019-12-21 18:25:54 +01001036 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1037 // is not folded.
1038 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001039 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#ifdef FEAT_FOLDING
1041 && !curwin->w_cline_folded
1042#endif
1043 )
1044 {
1045 /*
1046 * If Cursor is left of the screen, scroll rightwards.
1047 * If Cursor is right of the screen, scroll leftwards
1048 * If we get closer to the edge than 'sidescrolloff', scroll a little
1049 * extra
1050 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001051 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001052 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001053 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (off_left < 0 || off_right > 0)
1055 {
1056 if (off_left < 0)
1057 diff = -off_left;
1058 else
1059 diff = off_right;
1060
Bram Moolenaar85a20022019-12-21 18:25:54 +01001061 // When far off or not enough room on either side, put cursor in
1062 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1064 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1065 else
1066 {
1067 if (diff < p_ss)
1068 diff = p_ss;
1069 if (off_left < 0)
1070 new_leftcol = curwin->w_leftcol - diff;
1071 else
1072 new_leftcol = curwin->w_leftcol + diff;
1073 }
1074 if (new_leftcol < 0)
1075 new_leftcol = 0;
1076 if (new_leftcol != (int)curwin->w_leftcol)
1077 {
1078 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001079 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 redraw_later(NOT_VALID);
1081 }
1082 }
1083 curwin->w_wcol -= curwin->w_leftcol;
1084 }
1085 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1086 curwin->w_wcol -= curwin->w_leftcol;
1087 else
1088 curwin->w_wcol = 0;
1089
1090#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // Skip over filler lines. At the top use w_topfill, there
1092 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (curwin->w_cursor.lnum == curwin->w_topline)
1094 curwin->w_wrow += curwin->w_topfill;
1095 else
1096 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1097#endif
1098
1099 prev_skipcol = curwin->w_skipcol;
1100
1101 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if ((curwin->w_wrow >= curwin->w_height
1104 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001105 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 && (p_lines =
1107#ifdef FEAT_DIFF
1108 plines_win_nofill
1109#else
1110 plines_win
1111#endif
1112 (curwin, curwin->w_cursor.lnum, FALSE))
1113 - 1 >= curwin->w_height))
1114 && curwin->w_height != 0
1115 && curwin->w_cursor.lnum == curwin->w_topline
1116 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001117 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001119 // Cursor past end of screen. Happens with a single line that does
1120 // not fit on screen. Find a skipcol to show the text around the
1121 // cursor. Avoid scrolling all the time. compute value of "extra":
1122 // 1: Less than 'scrolloff' lines above
1123 // 2: Less than 'scrolloff' lines below
1124 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001126 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 // Compute last display line of the buffer line that we want at the
1129 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (p_lines == 0)
1131 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1132 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001133 if (p_lines > curwin->w_wrow + so)
1134 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 else
1136 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001137 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 extra += 2;
1139
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001140 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001142 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 n = curwin->w_virtcol / width;
1144 if (n > curwin->w_height / 2)
1145 n -= curwin->w_height / 2;
1146 else
1147 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001148 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (n > p_lines - curwin->w_height + 1)
1150 n = p_lines - curwin->w_height + 1;
1151 curwin->w_skipcol = n * width;
1152 }
1153 else if (extra == 1)
1154 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001155 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001156 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 + width - 1) / width;
1158 if (extra > 0)
1159 {
1160 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1161 extra = curwin->w_skipcol / width;
1162 curwin->w_skipcol -= extra * width;
1163 }
1164 }
1165 else if (extra == 2)
1166 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001167 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 endcol = (n - curwin->w_height + 1) * width;
1169 while (endcol > curwin->w_virtcol)
1170 endcol -= width;
1171 if (endcol > curwin->w_skipcol)
1172 curwin->w_skipcol = endcol;
1173 }
1174
1175 curwin->w_wrow -= curwin->w_skipcol / width;
1176 if (curwin->w_wrow >= curwin->w_height)
1177 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001178 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 extra = curwin->w_wrow - curwin->w_height + 1;
1180 curwin->w_skipcol += extra * width;
1181 curwin->w_wrow -= extra;
1182 }
1183
1184 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1185 if (extra > 0)
1186 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1187 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001188 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else
1191 curwin->w_skipcol = 0;
1192 if (prev_skipcol != curwin->w_skipcol)
1193 redraw_later(NOT_VALID);
1194
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001195#ifdef FEAT_SYN_HL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001196 // Redraw when w_virtcol changes and 'cursorcolumn' is set
Bram Moolenaarb6798752014-03-27 12:11:48 +01001197 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001198 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001199 redraw_later(SOME_VALID);
1200#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001201#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1202 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1203 {
1204 curwin->w_wrow += popup_top_extra(curwin);
1205 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001206 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001207 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001208 else
1209 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001210#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001211
Bram Moolenaar08f23632019-11-16 14:19:33 +01001212 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1213 curwin->w_valid_leftcol = curwin->w_leftcol;
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1216}
1217
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001218#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001219/*
1220 * Compute the screen position of text character at "pos" in window "wp"
1221 * The resulting values are one-based, zero when character is not visible.
1222 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001223 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001224textpos2screenpos(
1225 win_T *wp,
1226 pos_T *pos,
1227 int *rowp, // screen row
1228 int *scolp, // start screen column
1229 int *ccolp, // cursor screen column
1230 int *ecolp) // end screen column
1231{
1232 colnr_T scol = 0, ccol = 0, ecol = 0;
1233 int row = 0;
1234 int rowoff = 0;
1235 colnr_T coloff = 0;
1236
Bram Moolenaar189663b2021-07-21 18:04:56 +02001237 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001238 {
1239 colnr_T off;
1240 colnr_T col;
1241 int width;
1242
1243 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1244 getvcol(wp, pos, &scol, &ccol, &ecol);
1245
1246 // similar to what is done in validate_cursor_col()
1247 col = scol;
1248 off = win_col_off(wp);
1249 col += off;
1250 width = wp->w_width - off + win_col_off2(wp);
1251
Bram Moolenaar12034e22019-08-25 22:25:02 +02001252 // long line wrapping, adjust row
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001253 if (wp->w_p_wrap
1254 && col >= (colnr_T)wp->w_width
1255 && width > 0)
1256 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001257 // use same formula as what is used in curs_columns()
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001258 rowoff = ((col - wp->w_width) / width + 1);
1259 col -= rowoff * width;
1260 }
1261 col -= wp->w_leftcol;
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +01001262 if (col >= wp->w_width)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001263 col = -1;
Bram Moolenaar189663b2021-07-21 18:04:56 +02001264 if (col >= 0 && row + rowoff <= wp->w_height)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001265 coloff = col - scol + wp->w_wincol + 1;
1266 else
Bram Moolenaar189663b2021-07-21 18:04:56 +02001267 // character is left, right or below of the window
1268 row = rowoff = scol = ccol = ecol = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001269 }
Bram Moolenaar8dd46e72020-12-17 21:35:29 +01001270 *rowp = W_WINROW(wp) + row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001271 *scolp = scol + coloff;
1272 *ccolp = ccol + coloff;
1273 *ecolp = ecol + coloff;
1274}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001275#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001276
Bram Moolenaar12034e22019-08-25 22:25:02 +02001277#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001278/*
1279 * "screenpos({winid}, {lnum}, {col})" function
1280 */
1281 void
1282f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1283{
1284 dict_T *dict;
1285 win_T *wp;
1286 pos_T pos;
1287 int row = 0;
1288 int scol = 0, ccol = 0, ecol = 0;
1289
1290 if (rettv_dict_alloc(rettv) != OK)
1291 return;
1292 dict = rettv->vval.v_dict;
1293
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001294 if (in_vim9script()
1295 && (check_for_number_arg(argvars, 0) == FAIL
1296 || check_for_number_arg(argvars, 1) == FAIL
1297 || check_for_number_arg(argvars, 2) == FAIL))
1298 return;
1299
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001300 wp = find_win_by_nr_or_id(&argvars[0]);
1301 if (wp == NULL)
1302 return;
1303
1304 pos.lnum = tv_get_number(&argvars[1]);
1305 pos.col = tv_get_number(&argvars[2]) - 1;
1306 pos.coladd = 0;
1307 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1308
1309 dict_add_number(dict, "row", row);
1310 dict_add_number(dict, "col", scol);
1311 dict_add_number(dict, "curscol", ccol);
1312 dict_add_number(dict, "endcol", ecol);
1313}
1314#endif
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316/*
1317 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001320scrolldown(
1321 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001322 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001324 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 int wrow;
1326 int moved = FALSE;
1327
1328#ifdef FEAT_FOLDING
1329 linenr_T first;
1330
Bram Moolenaar85a20022019-12-21 18:25:54 +01001331 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1333#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001334 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 while (line_count-- > 0)
1336 {
1337#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001338 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1339 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 {
1341 ++curwin->w_topfill;
1342 ++done;
1343 }
1344 else
1345#endif
1346 {
1347 if (curwin->w_topline == 1)
1348 break;
1349 --curwin->w_topline;
1350#ifdef FEAT_DIFF
1351 curwin->w_topfill = 0;
1352#endif
1353#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001354 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (hasFolding(curwin->w_topline, &first, NULL))
1356 {
1357 ++done;
1358 if (!byfold)
1359 line_count -= curwin->w_topline - first - 1;
1360 curwin->w_botline -= curwin->w_topline - first;
1361 curwin->w_topline = first;
1362 }
1363 else
1364#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001365 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001367 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 invalidate_botline();
1369 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001370 curwin->w_wrow += done; // keep w_wrow updated
1371 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373#ifdef FEAT_DIFF
1374 if (curwin->w_cursor.lnum == curwin->w_topline)
1375 curwin->w_cline_row = 0;
1376 check_topfill(curwin, TRUE);
1377#endif
1378
1379 /*
1380 * Compute the row number of the last row of the cursor line
1381 * and move the cursor onto the displayed part of the window.
1382 */
1383 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001384 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
1386 validate_virtcol();
1387 validate_cheight();
1388 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001389 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1392 {
1393#ifdef FEAT_FOLDING
1394 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1395 {
1396 --wrow;
1397 if (first == 1)
1398 curwin->w_cursor.lnum = 1;
1399 else
1400 curwin->w_cursor.lnum = first - 1;
1401 }
1402 else
1403#endif
1404 wrow -= plines(curwin->w_cursor.lnum--);
1405 curwin->w_valid &=
1406 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1407 moved = TRUE;
1408 }
1409 if (moved)
1410 {
1411#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001412 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 foldAdjustCursor();
1414#endif
1415 coladvance(curwin->w_curswant);
1416 }
1417}
1418
1419/*
1420 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001423scrollup(
1424 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001425 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
1427#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1428 linenr_T lnum;
1429
1430 if (
1431# ifdef FEAT_FOLDING
1432 (byfold && hasAnyFolding(curwin))
1433# ifdef FEAT_DIFF
1434 ||
1435# endif
1436# endif
1437# ifdef FEAT_DIFF
1438 curwin->w_p_diff
1439# endif
1440 )
1441 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001442 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 lnum = curwin->w_topline;
1444 while (line_count--)
1445 {
1446# ifdef FEAT_DIFF
1447 if (curwin->w_topfill > 0)
1448 --curwin->w_topfill;
1449 else
1450# endif
1451 {
1452# ifdef FEAT_FOLDING
1453 if (byfold)
1454 (void)hasFolding(lnum, NULL, &lnum);
1455# endif
1456 if (lnum >= curbuf->b_ml.ml_line_count)
1457 break;
1458 ++lnum;
1459# ifdef FEAT_DIFF
1460 curwin->w_topfill = diff_check_fill(curwin, lnum);
1461# endif
1462 }
1463 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001464 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 curwin->w_botline += lnum - curwin->w_topline;
1466 curwin->w_topline = lnum;
1467 }
1468 else
1469#endif
1470 {
1471 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001472 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474
1475 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1476 curwin->w_topline = curbuf->b_ml.ml_line_count;
1477 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1478 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1479
1480#ifdef FEAT_DIFF
1481 check_topfill(curwin, FALSE);
1482#endif
1483
1484#ifdef FEAT_FOLDING
1485 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001486 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1488#endif
1489
1490 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1491 if (curwin->w_cursor.lnum < curwin->w_topline)
1492 {
1493 curwin->w_cursor.lnum = curwin->w_topline;
1494 curwin->w_valid &=
1495 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1496 coladvance(curwin->w_curswant);
1497 }
1498}
1499
1500#ifdef FEAT_DIFF
1501/*
1502 * Don't end up with too many filler lines in the window.
1503 */
1504 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001505check_topfill(
1506 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001507 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 int n;
1510
1511 if (wp->w_topfill > 0)
1512 {
1513 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1514 if (wp->w_topfill + n > wp->w_height)
1515 {
1516 if (down && wp->w_topline > 1)
1517 {
1518 --wp->w_topline;
1519 wp->w_topfill = 0;
1520 }
1521 else
1522 {
1523 wp->w_topfill = wp->w_height - n;
1524 if (wp->w_topfill < 0)
1525 wp->w_topfill = 0;
1526 }
1527 }
1528 }
1529}
1530
1531/*
1532 * Use as many filler lines as possible for w_topline. Make sure w_topline
1533 * is still visible.
1534 */
1535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001536max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537{
1538 int n;
1539
1540 n = plines_nofill(curwin->w_topline);
1541 if (n >= curwin->w_height)
1542 curwin->w_topfill = 0;
1543 else
1544 {
1545 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1546 if (curwin->w_topfill + n > curwin->w_height)
1547 curwin->w_topfill = curwin->w_height - n;
1548 }
1549}
1550#endif
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
1553 * Scroll the screen one line down, but don't do it if it would move the
1554 * cursor off the screen.
1555 */
1556 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001557scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 int end_row;
1560#ifdef FEAT_DIFF
1561 int can_fill = (curwin->w_topfill
1562 < diff_check_fill(curwin, curwin->w_topline));
1563#endif
1564
1565 if (curwin->w_topline <= 1
1566#ifdef FEAT_DIFF
1567 && !can_fill
1568#endif
1569 )
1570 return;
1571
Bram Moolenaar85a20022019-12-21 18:25:54 +01001572 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 /*
1575 * Compute the row number of the last row of the cursor line
1576 * and make sure it doesn't go off the screen. Make sure the cursor
1577 * doesn't go past 'scrolloff' lines from the screen end.
1578 */
1579 end_row = curwin->w_wrow;
1580#ifdef FEAT_DIFF
1581 if (can_fill)
1582 ++end_row;
1583 else
1584 end_row += plines_nofill(curwin->w_topline - 1);
1585#else
1586 end_row += plines(curwin->w_topline - 1);
1587#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001588 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 validate_cheight();
1591 validate_virtcol();
1592 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001593 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001595 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597#ifdef FEAT_DIFF
1598 if (can_fill)
1599 {
1600 ++curwin->w_topfill;
1601 check_topfill(curwin, TRUE);
1602 }
1603 else
1604 {
1605 --curwin->w_topline;
1606 curwin->w_topfill = 0;
1607 }
1608#else
1609 --curwin->w_topline;
1610#endif
1611#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001612 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001614 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1616 }
1617}
1618
1619/*
1620 * Scroll the screen one line up, but don't do it if it would move the cursor
1621 * off the screen.
1622 */
1623 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001624scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 int start_row;
1627
1628 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1629#ifdef FEAT_DIFF
1630 && curwin->w_topfill == 0
1631#endif
1632 )
1633 return;
1634
Bram Moolenaar85a20022019-12-21 18:25:54 +01001635 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 /*
1638 * Compute the row number of the first row of the cursor line
1639 * and make sure it doesn't go off the screen. Make sure the cursor
1640 * doesn't go before 'scrolloff' lines from the screen start.
1641 */
1642#ifdef FEAT_DIFF
1643 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1644 - curwin->w_topfill;
1645#else
1646 start_row = curwin->w_wrow - plines(curwin->w_topline);
1647#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001648 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001651 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001653 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 {
1655#ifdef FEAT_DIFF
1656 if (curwin->w_topfill > 0)
1657 --curwin->w_topfill;
1658 else
1659#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001660 {
1661#ifdef FEAT_FOLDING
1662 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001665 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001666 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1668 }
1669}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671/*
1672 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1673 * a (wrapped) text line. Uses and sets "lp->fill".
1674 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001675 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 */
1677 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001678topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680#ifdef FEAT_DIFF
1681 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1682 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001683 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 ++lp->fill;
1685 lp->height = 1;
1686 }
1687 else
1688#endif
1689 {
1690 --lp->lnum;
1691#ifdef FEAT_DIFF
1692 lp->fill = 0;
1693#endif
1694 if (lp->lnum < 1)
1695 lp->height = MAXCOL;
1696 else
1697#ifdef FEAT_FOLDING
1698 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001699 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 lp->height = 1;
1701 else
1702#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001703 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 }
1705}
1706
1707/*
1708 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1709 * a (wrapped) text line. Uses and sets "lp->fill".
1710 * Returns the height of the added line in "lp->height".
1711 * Lines below the last one are incredibly high.
1712 */
1713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001714botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716#ifdef FEAT_DIFF
1717 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1718 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001719 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 ++lp->fill;
1721 lp->height = 1;
1722 }
1723 else
1724#endif
1725 {
1726 ++lp->lnum;
1727#ifdef FEAT_DIFF
1728 lp->fill = 0;
1729#endif
1730 if (lp->lnum > curbuf->b_ml.ml_line_count)
1731 lp->height = MAXCOL;
1732 else
1733#ifdef FEAT_FOLDING
1734 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001735 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 lp->height = 1;
1737 else
1738#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001739 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
1741}
1742
1743#ifdef FEAT_DIFF
1744/*
1745 * Switch from including filler lines below lp->lnum to including filler
1746 * lines above loff.lnum + 1. This keeps pointing to the same line.
1747 * When there are no filler lines nothing changes.
1748 */
1749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001750botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 if (lp->fill > 0)
1753 {
1754 ++lp->lnum;
1755 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1756 }
1757}
1758
1759/*
1760 * Switch from including filler lines above lp->lnum to including filler
1761 * lines below loff.lnum - 1. This keeps pointing to the same line.
1762 * When there are no filler lines nothing changes.
1763 */
1764 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001765topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 if (lp->fill > 0)
1768 {
1769 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1770 --lp->lnum;
1771 }
1772}
1773#endif
1774
1775/*
1776 * Recompute topline to put the cursor at the top of the window.
1777 * Scroll at least "min_scroll" lines.
1778 * If "always" is TRUE, always set topline (for "zt").
1779 */
1780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001781scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 int scrolled = 0;
1784 int extra = 0;
1785 int used;
1786 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001787 linenr_T top; // just above displayed lines
1788 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 linenr_T old_topline = curwin->w_topline;
1790#ifdef FEAT_DIFF
1791 linenr_T old_topfill = curwin->w_topfill;
1792#endif
1793 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001794 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (mouse_dragging > 0)
1797 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799 /*
1800 * Decrease topline until:
1801 * - it has become 1
1802 * - (part of) the cursor line is moved off the screen or
1803 * - moved at least 'scrolljump' lines and
1804 * - at least 'scrolloff' lines above and below the cursor
1805 */
1806 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001807 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (curwin->w_cursor.lnum < curwin->w_topline)
1809 scrolled = used;
1810
1811#ifdef FEAT_FOLDING
1812 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1813 {
1814 --top;
1815 ++bot;
1816 }
1817 else
1818#endif
1819 {
1820 top = curwin->w_cursor.lnum - 1;
1821 bot = curwin->w_cursor.lnum + 1;
1822 }
1823 new_topline = top + 1;
1824
1825#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001826 // "used" already contains the number of filler lines above, don't add it
1827 // again.
1828 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001829 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830#endif
1831
1832 /*
1833 * Check if the lines from "top" to "bot" fit in the window. If they do,
1834 * set new_topline and advance "top" and "bot" to include more lines.
1835 */
1836 while (top > 0)
1837 {
1838#ifdef FEAT_FOLDING
1839 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001840 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 i = 1;
1842 else
1843#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001844 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 used += i;
1846 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1847 {
1848#ifdef FEAT_FOLDING
1849 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001850 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 ++used;
1852 else
1853#endif
1854 used += plines(bot);
1855 }
1856 if (used > curwin->w_height)
1857 break;
1858 if (top < curwin->w_topline)
1859 scrolled += i;
1860
1861 /*
1862 * If scrolling is needed, scroll at least 'sj' lines.
1863 */
1864 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1865 && extra >= off)
1866 break;
1867
1868 extra += i;
1869 new_topline = top;
1870 --top;
1871 ++bot;
1872 }
1873
1874 /*
1875 * If we don't have enough space, put cursor in the middle.
1876 * This makes sure we get the same position when using "k" and "j"
1877 * in a small window.
1878 */
1879 if (used > curwin->w_height)
1880 scroll_cursor_halfway(FALSE);
1881 else
1882 {
1883 /*
1884 * If "always" is FALSE, only adjust topline to a lower value, higher
1885 * value may happen with wrapping lines
1886 */
1887 if (new_topline < curwin->w_topline || always)
1888 curwin->w_topline = new_topline;
1889 if (curwin->w_topline > curwin->w_cursor.lnum)
1890 curwin->w_topline = curwin->w_cursor.lnum;
1891#ifdef FEAT_DIFF
1892 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1893 if (curwin->w_topfill > 0 && extra > off)
1894 {
1895 curwin->w_topfill -= extra - off;
1896 if (curwin->w_topfill < 0)
1897 curwin->w_topfill = 0;
1898 }
1899 check_topfill(curwin, FALSE);
1900#endif
1901 if (curwin->w_topline != old_topline
1902#ifdef FEAT_DIFF
1903 || curwin->w_topfill != old_topfill
1904#endif
1905 )
1906 curwin->w_valid &=
1907 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1908 curwin->w_valid |= VALID_TOPLINE;
1909 }
1910}
1911
1912/*
1913 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1914 * screen lines for text lines.
1915 */
1916 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001917set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918{
1919#ifdef FEAT_DIFF
1920 wp->w_filler_rows = 0;
1921#endif
1922 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001923 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 else
1925 {
1926 wp->w_empty_rows = wp->w_height - used;
1927#ifdef FEAT_DIFF
1928 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1929 {
1930 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1931 if (wp->w_empty_rows > wp->w_filler_rows)
1932 wp->w_empty_rows -= wp->w_filler_rows;
1933 else
1934 {
1935 wp->w_filler_rows = wp->w_empty_rows;
1936 wp->w_empty_rows = 0;
1937 }
1938 }
1939#endif
1940 }
1941}
1942
1943/*
1944 * Recompute topline to put the cursor at the bottom of the window.
1945 * Scroll at least "min_scroll" lines.
1946 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1947 * This is messy stuff!!!
1948 */
1949 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001950scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
1952 int used;
1953 int scrolled = 0;
1954 int extra = 0;
1955 int i;
1956 linenr_T line_count;
1957 linenr_T old_topline = curwin->w_topline;
1958 lineoff_T loff;
1959 lineoff_T boff;
1960#ifdef FEAT_DIFF
1961 int old_topfill = curwin->w_topfill;
1962 int fill_below_window;
1963#endif
1964 linenr_T old_botline = curwin->w_botline;
1965 linenr_T old_valid = curwin->w_valid;
1966 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001967 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001968 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
1970 cln = curwin->w_cursor.lnum;
1971 if (set_topbot)
1972 {
1973 used = 0;
1974 curwin->w_botline = cln + 1;
1975#ifdef FEAT_DIFF
1976 loff.fill = 0;
1977#endif
1978 for (curwin->w_topline = curwin->w_botline;
1979 curwin->w_topline > 1;
1980 curwin->w_topline = loff.lnum)
1981 {
1982 loff.lnum = curwin->w_topline;
1983 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001984 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 break;
1986 used += loff.height;
1987#ifdef FEAT_DIFF
1988 curwin->w_topfill = loff.fill;
1989#endif
1990 }
1991 set_empty_rows(curwin, used);
1992 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1993 if (curwin->w_topline != old_topline
1994#ifdef FEAT_DIFF
1995 || curwin->w_topfill != old_topfill
1996#endif
1997 )
1998 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1999 }
2000 else
2001 validate_botline();
2002
Bram Moolenaar85a20022019-12-21 18:25:54 +01002003 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#ifdef FEAT_DIFF
2005 used = plines_nofill(cln);
2006#else
2007 validate_cheight();
2008 used = curwin->w_cline_height;
2009#endif
2010
Bram Moolenaar85a20022019-12-21 18:25:54 +01002011 // If the cursor is below botline, we will at least scroll by the height
2012 // of the cursor line. Correct for empty lines, which are really part of
2013 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (cln >= curwin->w_botline)
2015 {
2016 scrolled = used;
2017 if (cln == curwin->w_botline)
2018 scrolled -= curwin->w_empty_rows;
2019 }
2020
2021 /*
2022 * Stop counting lines to scroll when
2023 * - hitting start of the file
2024 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002025 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 * - lines between botline and cursor have been counted
2027 */
2028#ifdef FEAT_FOLDING
2029 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2030#endif
2031 {
2032 loff.lnum = cln;
2033 boff.lnum = cln;
2034 }
2035#ifdef FEAT_DIFF
2036 loff.fill = 0;
2037 boff.fill = 0;
2038 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2039 - curwin->w_filler_rows;
2040#endif
2041
2042 while (loff.lnum > 1)
2043 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002044 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2045 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002047 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2049 && loff.lnum <= curwin->w_botline
2050#ifdef FEAT_DIFF
2051 && (loff.lnum < curwin->w_botline
2052 || loff.fill >= fill_below_window)
2053#endif
2054 )
2055 break;
2056
Bram Moolenaar85a20022019-12-21 18:25:54 +01002057 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002059 if (loff.height == MAXCOL)
2060 used = MAXCOL;
2061 else
2062 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (used > curwin->w_height)
2064 break;
2065 if (loff.lnum >= curwin->w_botline
2066#ifdef FEAT_DIFF
2067 && (loff.lnum > curwin->w_botline
2068 || loff.fill <= fill_below_window)
2069#endif
2070 )
2071 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002072 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 scrolled += loff.height;
2074 if (loff.lnum == curwin->w_botline
2075#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002076 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#endif
2078 )
2079 scrolled -= curwin->w_empty_rows;
2080 }
2081
2082 if (boff.lnum < curbuf->b_ml.ml_line_count)
2083 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002084 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 botline_forw(&boff);
2086 used += boff.height;
2087 if (used > curwin->w_height)
2088 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002089 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2090 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
2092 extra += boff.height;
2093 if (boff.lnum >= curwin->w_botline
2094#ifdef FEAT_DIFF
2095 || (boff.lnum + 1 == curwin->w_botline
2096 && boff.fill > curwin->w_filler_rows)
2097#endif
2098 )
2099 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002100 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 scrolled += boff.height;
2102 if (boff.lnum == curwin->w_botline
2103#ifdef FEAT_DIFF
2104 && boff.fill == 0
2105#endif
2106 )
2107 scrolled -= curwin->w_empty_rows;
2108 }
2109 }
2110 }
2111 }
2112
Bram Moolenaar85a20022019-12-21 18:25:54 +01002113 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (scrolled <= 0)
2115 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002116 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 else if (used > curwin->w_height)
2118 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002119 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 else
2121 {
2122 line_count = 0;
2123#ifdef FEAT_DIFF
2124 boff.fill = curwin->w_topfill;
2125#endif
2126 boff.lnum = curwin->w_topline - 1;
2127 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2128 {
2129 botline_forw(&boff);
2130 i += boff.height;
2131 ++line_count;
2132 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002133 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 line_count = 9999;
2135 }
2136
2137 /*
2138 * Scroll up if the cursor is off the bottom of the screen a bit.
2139 * Otherwise put it at 1/2 of the screen.
2140 */
2141 if (line_count >= curwin->w_height && line_count > min_scroll)
2142 scroll_cursor_halfway(FALSE);
2143 else
2144 scrollup(line_count, TRUE);
2145
2146 /*
2147 * If topline didn't change we need to restore w_botline and w_empty_rows
2148 * (we changed them).
2149 * If topline did change, update_screen() will set botline.
2150 */
2151 if (curwin->w_topline == old_topline && set_topbot)
2152 {
2153 curwin->w_botline = old_botline;
2154 curwin->w_empty_rows = old_empty_rows;
2155 curwin->w_valid = old_valid;
2156 }
2157 curwin->w_valid |= VALID_TOPLINE;
2158}
2159
2160/*
2161 * Recompute topline to put the cursor halfway the window
2162 * If "atend" is TRUE, also put it halfway at the end of the file.
2163 */
2164 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002165scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 int above = 0;
2168 linenr_T topline;
2169#ifdef FEAT_DIFF
2170 int topfill = 0;
2171#endif
2172 int below = 0;
2173 int used;
2174 lineoff_T loff;
2175 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002176#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002177 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002180#ifdef FEAT_PROP_POPUP
2181 // if the width changed this needs to be updated first
2182 may_update_popup_position();
2183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2185#ifdef FEAT_FOLDING
2186 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2187#endif
2188#ifdef FEAT_DIFF
2189 used = plines_nofill(loff.lnum);
2190 loff.fill = 0;
2191 boff.fill = 0;
2192#else
2193 used = plines(loff.lnum);
2194#endif
2195 topline = loff.lnum;
2196 while (topline > 1)
2197 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002198 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
2200 if (boff.lnum < curbuf->b_ml.ml_line_count)
2201 {
2202 botline_forw(&boff);
2203 used += boff.height;
2204 if (used > curwin->w_height)
2205 break;
2206 below += boff.height;
2207 }
2208 else
2209 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002210 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (atend)
2212 ++used;
2213 }
2214 }
2215
Bram Moolenaar85a20022019-12-21 18:25:54 +01002216 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002219 if (loff.height == MAXCOL)
2220 used = MAXCOL;
2221 else
2222 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (used > curwin->w_height)
2224 break;
2225 above += loff.height;
2226 topline = loff.lnum;
2227#ifdef FEAT_DIFF
2228 topfill = loff.fill;
2229#endif
2230 }
2231 }
2232#ifdef FEAT_FOLDING
2233 if (!hasFolding(topline, &curwin->w_topline, NULL))
2234#endif
2235 curwin->w_topline = topline;
2236#ifdef FEAT_DIFF
2237 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002238 if (old_topline > curwin->w_topline + curwin->w_height)
2239 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 check_topfill(curwin, FALSE);
2241#endif
2242 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2243 curwin->w_valid |= VALID_TOPLINE;
2244}
2245
2246/*
2247 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002248 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 * If not possible, put it at the same position as scroll_cursor_halfway().
2250 * When called topline must be valid!
2251 */
2252 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002253cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002255 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002257 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 linenr_T botline;
2259 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002260 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002262 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264 /*
2265 * How many lines we would like to have above/below the cursor depends on
2266 * whether the first/last line of the file is on screen.
2267 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002268 above_wanted = so;
2269 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002270 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 above_wanted = mouse_dragging - 1;
2273 below_wanted = mouse_dragging - 1;
2274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (curwin->w_topline == 1)
2276 {
2277 above_wanted = 0;
2278 max_off = curwin->w_height / 2;
2279 if (below_wanted > max_off)
2280 below_wanted = max_off;
2281 }
2282 validate_botline();
2283 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002284 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 below_wanted = 0;
2287 max_off = (curwin->w_height - 1) / 2;
2288 if (above_wanted > max_off)
2289 above_wanted = max_off;
2290 }
2291
2292 /*
2293 * If there are sufficient file-lines above and below the cursor, we can
2294 * return now.
2295 */
2296 cln = curwin->w_cursor.lnum;
2297 if (cln >= curwin->w_topline + above_wanted
2298 && cln < curwin->w_botline - below_wanted
2299#ifdef FEAT_FOLDING
2300 && !hasAnyFolding(curwin)
2301#endif
2302 )
2303 return;
2304
2305 /*
2306 * Narrow down the area where the cursor can be put by taking lines from
2307 * the top and the bottom until:
2308 * - the desired context lines are found
2309 * - the lines from the top is past the lines from the bottom
2310 */
2311 topline = curwin->w_topline;
2312 botline = curwin->w_botline - 1;
2313#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002314 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 above = curwin->w_topfill;
2316 below = curwin->w_filler_rows;
2317#endif
2318 while ((above < above_wanted || below < below_wanted) && topline < botline)
2319 {
2320 if (below < below_wanted && (below <= above || above >= above_wanted))
2321 {
2322#ifdef FEAT_FOLDING
2323 if (hasFolding(botline, &botline, NULL))
2324 ++below;
2325 else
2326#endif
2327 below += plines(botline);
2328 --botline;
2329 }
2330 if (above < above_wanted && (above < below || below >= below_wanted))
2331 {
2332#ifdef FEAT_FOLDING
2333 if (hasFolding(topline, NULL, &topline))
2334 ++above;
2335 else
2336#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002337 above += PLINES_NOFILL(topline);
2338#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002339 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (topline < botline)
2341 above += diff_check_fill(curwin, topline + 1);
2342#endif
2343 ++topline;
2344 }
2345 }
2346 if (topline == botline || botline == 0)
2347 curwin->w_cursor.lnum = topline;
2348 else if (topline > botline)
2349 curwin->w_cursor.lnum = botline;
2350 else
2351 {
2352 if (cln < topline && curwin->w_topline > 1)
2353 {
2354 curwin->w_cursor.lnum = topline;
2355 curwin->w_valid &=
2356 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2357 }
2358 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2359 {
2360 curwin->w_cursor.lnum = botline;
2361 curwin->w_valid &=
2362 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2363 }
2364 }
2365 curwin->w_valid |= VALID_TOPLINE;
2366}
2367
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002368static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370/*
2371 * move screen 'count' pages up or down and update screen
2372 *
2373 * return FAIL for failure, OK otherwise
2374 */
2375 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002376onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
2378 long n;
2379 int retval = OK;
2380 lineoff_T loff;
2381 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002382 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
Bram Moolenaar85a20022019-12-21 18:25:54 +01002384 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
2386 beep_flush();
2387 return FAIL;
2388 }
2389
2390 for ( ; count > 0; --count)
2391 {
2392 validate_botline();
2393 /*
2394 * It's an error to move a page up when the first line is already on
2395 * the screen. It's an error to move a page down when the last line
2396 * is on the screen and the topline is 'scrolloff' lines from the
2397 * last line.
2398 */
2399 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002400 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2402 : (curwin->w_topline == 1
2403#ifdef FEAT_DIFF
2404 && curwin->w_topfill ==
2405 diff_check_fill(curwin, curwin->w_topline)
2406#endif
2407 ))
2408 {
2409 beep_flush();
2410 retval = FAIL;
2411 break;
2412 }
2413
2414#ifdef FEAT_DIFF
2415 loff.fill = 0;
2416#endif
2417 if (dir == FORWARD)
2418 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002419 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002421 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002422 if (p_window <= 2)
2423 ++curwin->w_topline;
2424 else
2425 curwin->w_topline += p_window - 2;
2426 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2427 curwin->w_topline = curbuf->b_ml.ml_line_count;
2428 curwin->w_cursor.lnum = curwin->w_topline;
2429 }
2430 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2431 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002432 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 curwin->w_topline = curbuf->b_ml.ml_line_count;
2434#ifdef FEAT_DIFF
2435 curwin->w_topfill = 0;
2436#endif
2437 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2438 }
2439 else
2440 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002441 // For the overlap, start with the line just below the window
2442 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 loff.lnum = curwin->w_botline;
2444#ifdef FEAT_DIFF
2445 loff.fill = diff_check_fill(curwin, loff.lnum)
2446 - curwin->w_filler_rows;
2447#endif
2448 get_scroll_overlap(&loff, -1);
2449 curwin->w_topline = loff.lnum;
2450#ifdef FEAT_DIFF
2451 curwin->w_topfill = loff.fill;
2452 check_topfill(curwin, FALSE);
2453#endif
2454 curwin->w_cursor.lnum = curwin->w_topline;
2455 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2456 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2457 }
2458 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002459 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
2461#ifdef FEAT_DIFF
2462 if (curwin->w_topline == 1)
2463 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002464 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 max_topfill();
2466 continue;
2467 }
2468#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002469 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002470 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002471 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002472 if (p_window <= 2)
2473 --curwin->w_topline;
2474 else
2475 curwin->w_topline -= p_window - 2;
2476 if (curwin->w_topline < 1)
2477 curwin->w_topline = 1;
2478 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2479 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2480 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2481 continue;
2482 }
2483
Bram Moolenaar85a20022019-12-21 18:25:54 +01002484 // Find the line at the top of the window that is going to be the
2485 // line at the bottom of the window. Make sure this results in
2486 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 loff.lnum = curwin->w_topline - 1;
2488#ifdef FEAT_DIFF
2489 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2490 - curwin->w_topfill;
2491#endif
2492 get_scroll_overlap(&loff, 1);
2493
2494 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2495 {
2496 loff.lnum = curbuf->b_ml.ml_line_count;
2497#ifdef FEAT_DIFF
2498 loff.fill = 0;
2499 }
2500 else
2501 {
2502 botline_topline(&loff);
2503#endif
2504 }
2505 curwin->w_cursor.lnum = loff.lnum;
2506
Bram Moolenaar85a20022019-12-21 18:25:54 +01002507 // Find the line just above the new topline to get the right line
2508 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 n = 0;
2510 while (n <= curwin->w_height && loff.lnum >= 1)
2511 {
2512 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002513 if (loff.height == MAXCOL)
2514 n = MAXCOL;
2515 else
2516 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002518 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
2520 curwin->w_topline = 1;
2521#ifdef FEAT_DIFF
2522 max_topfill();
2523#endif
2524 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2525 }
2526 else
2527 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002528 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529#ifdef FEAT_DIFF
2530 topline_botline(&loff);
2531#endif
2532 botline_forw(&loff);
2533 botline_forw(&loff);
2534#ifdef FEAT_DIFF
2535 botline_topline(&loff);
2536#endif
2537#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002538 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2540#endif
2541
Bram Moolenaar85a20022019-12-21 18:25:54 +01002542 // Always scroll at least one line. Avoid getting stuck on
2543 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (loff.lnum >= curwin->w_topline
2545#ifdef FEAT_DIFF
2546 && (loff.lnum > curwin->w_topline
2547 || loff.fill >= curwin->w_topfill)
2548#endif
2549 )
2550 {
2551#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002552 // First try using the maximum number of filler lines. If
2553 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 loff.fill = curwin->w_topfill;
2555 if (curwin->w_topfill < diff_check_fill(curwin,
2556 curwin->w_topline))
2557 max_topfill();
2558 if (curwin->w_topfill == loff.fill)
2559#endif
2560 {
2561 --curwin->w_topline;
2562#ifdef FEAT_DIFF
2563 curwin->w_topfill = 0;
2564#endif
2565 }
2566 comp_botline(curwin);
2567 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002568 curwin->w_valid &=
2569 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571 else
2572 {
2573 curwin->w_topline = loff.lnum;
2574#ifdef FEAT_DIFF
2575 curwin->w_topfill = loff.fill;
2576 check_topfill(curwin, FALSE);
2577#endif
2578 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2579 }
2580 }
2581 }
2582 }
2583#ifdef FEAT_FOLDING
2584 foldAdjustCursor();
2585#endif
2586 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002587 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002588 if (retval == OK)
2589 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2591
Bram Moolenaar907dad72018-07-10 15:07:15 +02002592 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002594 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2595 // But make sure we scroll at least one line (happens with mix of long
2596 // wrapping lines and non-wrapping line).
2597 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002599 scroll_cursor_top(1, FALSE);
2600 if (curwin->w_topline <= old_topline
2601 && old_topline < curbuf->b_ml.ml_line_count)
2602 {
2603 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002605 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2606#endif
2607 }
2608 }
2609#ifdef FEAT_FOLDING
2610 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614
2615 redraw_later(VALID);
2616 return retval;
2617}
2618
2619/*
2620 * Decide how much overlap to use for page-up or page-down scrolling.
2621 * This is symmetric, so that doing both keeps the same lines displayed.
2622 * Three lines are examined:
2623 *
2624 * before CTRL-F after CTRL-F / before CTRL-B
2625 * etc. l1
2626 * l1 last but one line ------------
2627 * l2 last text line l2 top text line
2628 * ------------- l3 second text line
2629 * l3 etc.
2630 */
2631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002632get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 int h1, h2, h3, h4;
2635 int min_height = curwin->w_height - 2;
2636 lineoff_T loff0, loff1, loff2;
2637
2638#ifdef FEAT_DIFF
2639 if (lp->fill > 0)
2640 lp->height = 1;
2641 else
2642 lp->height = plines_nofill(lp->lnum);
2643#else
2644 lp->height = plines(lp->lnum);
2645#endif
2646 h1 = lp->height;
2647 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002648 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 loff0 = *lp;
2651 if (dir > 0)
2652 botline_forw(lp);
2653 else
2654 topline_back(lp);
2655 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002656 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002658 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 return;
2660 }
2661
2662 loff1 = *lp;
2663 if (dir > 0)
2664 botline_forw(lp);
2665 else
2666 topline_back(lp);
2667 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002668 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002670 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 return;
2672 }
2673
2674 loff2 = *lp;
2675 if (dir > 0)
2676 botline_forw(lp);
2677 else
2678 topline_back(lp);
2679 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002680 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002681 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002683 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684}
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686/*
2687 * Scroll 'scroll' lines up or down.
2688 */
2689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 long scrolled = 0;
2693 int i;
2694 int n;
2695 int room;
2696
2697 if (Prenum)
2698 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2699 curwin->w_height : Prenum;
2700 n = (curwin->w_p_scr <= curwin->w_height) ?
2701 curwin->w_p_scr : curwin->w_height;
2702
Bram Moolenaard5d37532017-03-27 23:02:07 +02002703 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 validate_botline();
2705 room = curwin->w_empty_rows;
2706#ifdef FEAT_DIFF
2707 room += curwin->w_filler_rows;
2708#endif
2709 if (flag)
2710 {
2711 /*
2712 * scroll the text up
2713 */
2714 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2715 {
2716#ifdef FEAT_DIFF
2717 if (curwin->w_topfill > 0)
2718 {
2719 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002720 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 --curwin->w_topfill;
2722 }
2723 else
2724#endif
2725 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002726 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 n -= i;
2728 if (n < 0 && scrolled > 0)
2729 break;
2730#ifdef FEAT_FOLDING
2731 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2732#endif
2733 ++curwin->w_topline;
2734#ifdef FEAT_DIFF
2735 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2736#endif
2737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2739 {
2740 ++curwin->w_cursor.lnum;
2741 curwin->w_valid &=
2742 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 }
2745 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2746 scrolled += i;
2747
2748 /*
2749 * Correct w_botline for changed w_topline.
2750 * Won't work when there are filler lines.
2751 */
2752#ifdef FEAT_DIFF
2753 if (curwin->w_p_diff)
2754 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2755 else
2756#endif
2757 {
2758 room += i;
2759 do
2760 {
2761 i = plines(curwin->w_botline);
2762 if (i > room)
2763 break;
2764#ifdef FEAT_FOLDING
2765 (void)hasFolding(curwin->w_botline, NULL,
2766 &curwin->w_botline);
2767#endif
2768 ++curwin->w_botline;
2769 room -= i;
2770 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2771 }
2772 }
2773
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 /*
2775 * When hit bottom of the file: move cursor down.
2776 */
2777 if (n > 0)
2778 {
2779# ifdef FEAT_FOLDING
2780 if (hasAnyFolding(curwin))
2781 {
2782 while (--n >= 0
2783 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2784 {
2785 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2786 &curwin->w_cursor.lnum);
2787 ++curwin->w_cursor.lnum;
2788 }
2789 }
2790 else
2791# endif
2792 curwin->w_cursor.lnum += n;
2793 check_cursor_lnum();
2794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796 else
2797 {
2798 /*
2799 * scroll the text down
2800 */
2801 while (n > 0 && curwin->w_topline > 1)
2802 {
2803#ifdef FEAT_DIFF
2804 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2805 {
2806 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002807 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 ++curwin->w_topfill;
2809 }
2810 else
2811#endif
2812 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002813 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 n -= i;
2815 if (n < 0 && scrolled > 0)
2816 break;
2817 --curwin->w_topline;
2818#ifdef FEAT_FOLDING
2819 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2820#endif
2821#ifdef FEAT_DIFF
2822 curwin->w_topfill = 0;
2823#endif
2824 }
2825 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2826 VALID_BOTLINE|VALID_BOTLINE_AP);
2827 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (curwin->w_cursor.lnum > 1)
2829 {
2830 --curwin->w_cursor.lnum;
2831 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 /*
2836 * When hit top of the file: move cursor up.
2837 */
2838 if (n > 0)
2839 {
2840 if (curwin->w_cursor.lnum <= (linenr_T)n)
2841 curwin->w_cursor.lnum = 1;
2842 else
2843# ifdef FEAT_FOLDING
2844 if (hasAnyFolding(curwin))
2845 {
2846 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2847 {
2848 --curwin->w_cursor.lnum;
2849 (void)hasFolding(curwin->w_cursor.lnum,
2850 &curwin->w_cursor.lnum, NULL);
2851 }
2852 }
2853 else
2854# endif
2855 curwin->w_cursor.lnum -= n;
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002859 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 foldAdjustCursor();
2861# endif
2862#ifdef FEAT_DIFF
2863 check_topfill(curwin, !flag);
2864#endif
2865 cursor_correct();
2866 beginline(BL_SOL | BL_FIX);
2867 redraw_later(VALID);
2868}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002869
Bram Moolenaar860cae12010-06-05 23:22:07 +02002870 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002871do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002872{
2873 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002874 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002875 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002876 colnr_T curswant = curwin->w_curswant;
2877 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002878 win_T *old_curwin = curwin;
2879 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002880 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002881 int old_VIsual_select = VIsual_select;
2882 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002883
2884 /*
2885 * loop through the cursorbound windows
2886 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002887 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002888 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002889 {
2890 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002891 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002892 if (curwin != old_curwin && curwin->w_p_crb)
2893 {
2894# ifdef FEAT_DIFF
2895 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002896 curwin->w_cursor.lnum =
2897 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002898 else
2899# endif
2900 curwin->w_cursor.lnum = line;
2901 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002902 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002903 curwin->w_curswant = curswant;
2904 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002905
Bram Moolenaar85a20022019-12-21 18:25:54 +01002906 // Make sure the cursor is in a valid position. Temporarily set
2907 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002908 restart_edit_save = restart_edit;
2909 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002910 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002911# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002912 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002913 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002914# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002915 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002916 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002917 if (has_mbyte)
2918 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002919 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002920
Bram Moolenaar85a20022019-12-21 18:25:54 +01002921 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002922 if (!curwin->w_p_scb)
2923 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002924 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002925 }
2926 }
2927
2928 /*
2929 * reset current-window
2930 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002931 VIsual_select = old_VIsual_select;
2932 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002933 curwin = old_curwin;
2934 curbuf = old_curbuf;
2935}