blob: f52e4c071e48549fc3592aa5ed416c4b28d9de45 [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 Moolenaar7924a172022-01-24 16:15:15 +00001265 {
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001266 coloff = col - scol + wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001267 row += W_WINROW(wp);
1268 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001269 else
Bram Moolenaar189663b2021-07-21 18:04:56 +02001270 // character is left, right or below of the window
1271 row = rowoff = scol = ccol = ecol = 0;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001272 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001273 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001274 *scolp = scol + coloff;
1275 *ccolp = ccol + coloff;
1276 *ecolp = ecol + coloff;
1277}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001278#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001279
Bram Moolenaar12034e22019-08-25 22:25:02 +02001280#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001281/*
1282 * "screenpos({winid}, {lnum}, {col})" function
1283 */
1284 void
1285f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1286{
1287 dict_T *dict;
1288 win_T *wp;
1289 pos_T pos;
1290 int row = 0;
1291 int scol = 0, ccol = 0, ecol = 0;
1292
1293 if (rettv_dict_alloc(rettv) != OK)
1294 return;
1295 dict = rettv->vval.v_dict;
1296
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001297 if (in_vim9script()
1298 && (check_for_number_arg(argvars, 0) == FAIL
1299 || check_for_number_arg(argvars, 1) == FAIL
1300 || check_for_number_arg(argvars, 2) == FAIL))
1301 return;
1302
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001303 wp = find_win_by_nr_or_id(&argvars[0]);
1304 if (wp == NULL)
1305 return;
1306
1307 pos.lnum = tv_get_number(&argvars[1]);
1308 pos.col = tv_get_number(&argvars[2]) - 1;
1309 pos.coladd = 0;
1310 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1311
1312 dict_add_number(dict, "row", row);
1313 dict_add_number(dict, "col", scol);
1314 dict_add_number(dict, "curscol", ccol);
1315 dict_add_number(dict, "endcol", ecol);
1316}
1317#endif
1318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319/*
1320 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001323scrolldown(
1324 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001325 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001327 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 int wrow;
1329 int moved = FALSE;
1330
1331#ifdef FEAT_FOLDING
1332 linenr_T first;
1333
Bram Moolenaar85a20022019-12-21 18:25:54 +01001334 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1336#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001337 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 while (line_count-- > 0)
1339 {
1340#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001341 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1342 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
1344 ++curwin->w_topfill;
1345 ++done;
1346 }
1347 else
1348#endif
1349 {
1350 if (curwin->w_topline == 1)
1351 break;
1352 --curwin->w_topline;
1353#ifdef FEAT_DIFF
1354 curwin->w_topfill = 0;
1355#endif
1356#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001357 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (hasFolding(curwin->w_topline, &first, NULL))
1359 {
1360 ++done;
1361 if (!byfold)
1362 line_count -= curwin->w_topline - first - 1;
1363 curwin->w_botline -= curwin->w_topline - first;
1364 curwin->w_topline = first;
1365 }
1366 else
1367#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001368 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001370 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 invalidate_botline();
1372 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001373 curwin->w_wrow += done; // keep w_wrow updated
1374 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376#ifdef FEAT_DIFF
1377 if (curwin->w_cursor.lnum == curwin->w_topline)
1378 curwin->w_cline_row = 0;
1379 check_topfill(curwin, TRUE);
1380#endif
1381
1382 /*
1383 * Compute the row number of the last row of the cursor line
1384 * and move the cursor onto the displayed part of the window.
1385 */
1386 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001387 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
1389 validate_virtcol();
1390 validate_cheight();
1391 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001392 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1395 {
1396#ifdef FEAT_FOLDING
1397 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1398 {
1399 --wrow;
1400 if (first == 1)
1401 curwin->w_cursor.lnum = 1;
1402 else
1403 curwin->w_cursor.lnum = first - 1;
1404 }
1405 else
1406#endif
1407 wrow -= plines(curwin->w_cursor.lnum--);
1408 curwin->w_valid &=
1409 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1410 moved = TRUE;
1411 }
1412 if (moved)
1413 {
1414#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001415 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 foldAdjustCursor();
1417#endif
1418 coladvance(curwin->w_curswant);
1419 }
1420}
1421
1422/*
1423 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001426scrollup(
1427 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001428 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
1430#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1431 linenr_T lnum;
1432
1433 if (
1434# ifdef FEAT_FOLDING
1435 (byfold && hasAnyFolding(curwin))
1436# ifdef FEAT_DIFF
1437 ||
1438# endif
1439# endif
1440# ifdef FEAT_DIFF
1441 curwin->w_p_diff
1442# endif
1443 )
1444 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001445 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 lnum = curwin->w_topline;
1447 while (line_count--)
1448 {
1449# ifdef FEAT_DIFF
1450 if (curwin->w_topfill > 0)
1451 --curwin->w_topfill;
1452 else
1453# endif
1454 {
1455# ifdef FEAT_FOLDING
1456 if (byfold)
1457 (void)hasFolding(lnum, NULL, &lnum);
1458# endif
1459 if (lnum >= curbuf->b_ml.ml_line_count)
1460 break;
1461 ++lnum;
1462# ifdef FEAT_DIFF
1463 curwin->w_topfill = diff_check_fill(curwin, lnum);
1464# endif
1465 }
1466 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001467 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 curwin->w_botline += lnum - curwin->w_topline;
1469 curwin->w_topline = lnum;
1470 }
1471 else
1472#endif
1473 {
1474 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001475 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477
1478 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1479 curwin->w_topline = curbuf->b_ml.ml_line_count;
1480 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1481 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1482
1483#ifdef FEAT_DIFF
1484 check_topfill(curwin, FALSE);
1485#endif
1486
1487#ifdef FEAT_FOLDING
1488 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001489 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1491#endif
1492
1493 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1494 if (curwin->w_cursor.lnum < curwin->w_topline)
1495 {
1496 curwin->w_cursor.lnum = curwin->w_topline;
1497 curwin->w_valid &=
1498 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1499 coladvance(curwin->w_curswant);
1500 }
1501}
1502
1503#ifdef FEAT_DIFF
1504/*
1505 * Don't end up with too many filler lines in the window.
1506 */
1507 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001508check_topfill(
1509 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001510 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 int n;
1513
1514 if (wp->w_topfill > 0)
1515 {
1516 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1517 if (wp->w_topfill + n > wp->w_height)
1518 {
1519 if (down && wp->w_topline > 1)
1520 {
1521 --wp->w_topline;
1522 wp->w_topfill = 0;
1523 }
1524 else
1525 {
1526 wp->w_topfill = wp->w_height - n;
1527 if (wp->w_topfill < 0)
1528 wp->w_topfill = 0;
1529 }
1530 }
1531 }
1532}
1533
1534/*
1535 * Use as many filler lines as possible for w_topline. Make sure w_topline
1536 * is still visible.
1537 */
1538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001539max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 int n;
1542
1543 n = plines_nofill(curwin->w_topline);
1544 if (n >= curwin->w_height)
1545 curwin->w_topfill = 0;
1546 else
1547 {
1548 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1549 if (curwin->w_topfill + n > curwin->w_height)
1550 curwin->w_topfill = curwin->w_height - n;
1551 }
1552}
1553#endif
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
1556 * Scroll the screen one line down, but don't do it if it would move the
1557 * cursor off the screen.
1558 */
1559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001560scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 int end_row;
1563#ifdef FEAT_DIFF
1564 int can_fill = (curwin->w_topfill
1565 < diff_check_fill(curwin, curwin->w_topline));
1566#endif
1567
1568 if (curwin->w_topline <= 1
1569#ifdef FEAT_DIFF
1570 && !can_fill
1571#endif
1572 )
1573 return;
1574
Bram Moolenaar85a20022019-12-21 18:25:54 +01001575 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 /*
1578 * Compute the row number of the last row of the cursor line
1579 * and make sure it doesn't go off the screen. Make sure the cursor
1580 * doesn't go past 'scrolloff' lines from the screen end.
1581 */
1582 end_row = curwin->w_wrow;
1583#ifdef FEAT_DIFF
1584 if (can_fill)
1585 ++end_row;
1586 else
1587 end_row += plines_nofill(curwin->w_topline - 1);
1588#else
1589 end_row += plines(curwin->w_topline - 1);
1590#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001591 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 validate_cheight();
1594 validate_virtcol();
1595 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001596 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001598 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600#ifdef FEAT_DIFF
1601 if (can_fill)
1602 {
1603 ++curwin->w_topfill;
1604 check_topfill(curwin, TRUE);
1605 }
1606 else
1607 {
1608 --curwin->w_topline;
1609 curwin->w_topfill = 0;
1610 }
1611#else
1612 --curwin->w_topline;
1613#endif
1614#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001615 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001617 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1619 }
1620}
1621
1622/*
1623 * Scroll the screen one line up, but don't do it if it would move the cursor
1624 * off the screen.
1625 */
1626 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001627scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 int start_row;
1630
1631 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1632#ifdef FEAT_DIFF
1633 && curwin->w_topfill == 0
1634#endif
1635 )
1636 return;
1637
Bram Moolenaar85a20022019-12-21 18:25:54 +01001638 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 /*
1641 * Compute the row number of the first row of the cursor line
1642 * and make sure it doesn't go off the screen. Make sure the cursor
1643 * doesn't go before 'scrolloff' lines from the screen start.
1644 */
1645#ifdef FEAT_DIFF
1646 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1647 - curwin->w_topfill;
1648#else
1649 start_row = curwin->w_wrow - plines(curwin->w_topline);
1650#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001651 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001654 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001656 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658#ifdef FEAT_DIFF
1659 if (curwin->w_topfill > 0)
1660 --curwin->w_topfill;
1661 else
1662#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001663 {
1664#ifdef FEAT_FOLDING
1665 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001668 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001669 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1671 }
1672}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
1674/*
1675 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1676 * a (wrapped) text line. Uses and sets "lp->fill".
1677 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001678 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 */
1680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683#ifdef FEAT_DIFF
1684 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1685 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001686 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 ++lp->fill;
1688 lp->height = 1;
1689 }
1690 else
1691#endif
1692 {
1693 --lp->lnum;
1694#ifdef FEAT_DIFF
1695 lp->fill = 0;
1696#endif
1697 if (lp->lnum < 1)
1698 lp->height = MAXCOL;
1699 else
1700#ifdef FEAT_FOLDING
1701 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001702 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 lp->height = 1;
1704 else
1705#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001706 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708}
1709
1710/*
1711 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1712 * a (wrapped) text line. Uses and sets "lp->fill".
1713 * Returns the height of the added line in "lp->height".
1714 * Lines below the last one are incredibly high.
1715 */
1716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001717botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719#ifdef FEAT_DIFF
1720 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1721 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001722 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 ++lp->fill;
1724 lp->height = 1;
1725 }
1726 else
1727#endif
1728 {
1729 ++lp->lnum;
1730#ifdef FEAT_DIFF
1731 lp->fill = 0;
1732#endif
1733 if (lp->lnum > curbuf->b_ml.ml_line_count)
1734 lp->height = MAXCOL;
1735 else
1736#ifdef FEAT_FOLDING
1737 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001738 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 lp->height = 1;
1740 else
1741#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001742 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744}
1745
1746#ifdef FEAT_DIFF
1747/*
1748 * Switch from including filler lines below lp->lnum to including filler
1749 * lines above loff.lnum + 1. This keeps pointing to the same line.
1750 * When there are no filler lines nothing changes.
1751 */
1752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001753botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
1755 if (lp->fill > 0)
1756 {
1757 ++lp->lnum;
1758 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1759 }
1760}
1761
1762/*
1763 * Switch from including filler lines above lp->lnum to including filler
1764 * lines below loff.lnum - 1. This keeps pointing to the same line.
1765 * When there are no filler lines nothing changes.
1766 */
1767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001768topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 if (lp->fill > 0)
1771 {
1772 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1773 --lp->lnum;
1774 }
1775}
1776#endif
1777
1778/*
1779 * Recompute topline to put the cursor at the top of the window.
1780 * Scroll at least "min_scroll" lines.
1781 * If "always" is TRUE, always set topline (for "zt").
1782 */
1783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001784scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 int scrolled = 0;
1787 int extra = 0;
1788 int used;
1789 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001790 linenr_T top; // just above displayed lines
1791 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 linenr_T old_topline = curwin->w_topline;
1793#ifdef FEAT_DIFF
1794 linenr_T old_topfill = curwin->w_topfill;
1795#endif
1796 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001797 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (mouse_dragging > 0)
1800 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 /*
1803 * Decrease topline until:
1804 * - it has become 1
1805 * - (part of) the cursor line is moved off the screen or
1806 * - moved at least 'scrolljump' lines and
1807 * - at least 'scrolloff' lines above and below the cursor
1808 */
1809 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001810 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (curwin->w_cursor.lnum < curwin->w_topline)
1812 scrolled = used;
1813
1814#ifdef FEAT_FOLDING
1815 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1816 {
1817 --top;
1818 ++bot;
1819 }
1820 else
1821#endif
1822 {
1823 top = curwin->w_cursor.lnum - 1;
1824 bot = curwin->w_cursor.lnum + 1;
1825 }
1826 new_topline = top + 1;
1827
1828#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001829 // "used" already contains the number of filler lines above, don't add it
1830 // again.
1831 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001832 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#endif
1834
1835 /*
1836 * Check if the lines from "top" to "bot" fit in the window. If they do,
1837 * set new_topline and advance "top" and "bot" to include more lines.
1838 */
1839 while (top > 0)
1840 {
1841#ifdef FEAT_FOLDING
1842 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001843 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 i = 1;
1845 else
1846#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001847 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 used += i;
1849 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1850 {
1851#ifdef FEAT_FOLDING
1852 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001853 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 ++used;
1855 else
1856#endif
1857 used += plines(bot);
1858 }
1859 if (used > curwin->w_height)
1860 break;
1861 if (top < curwin->w_topline)
1862 scrolled += i;
1863
1864 /*
1865 * If scrolling is needed, scroll at least 'sj' lines.
1866 */
1867 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1868 && extra >= off)
1869 break;
1870
1871 extra += i;
1872 new_topline = top;
1873 --top;
1874 ++bot;
1875 }
1876
1877 /*
1878 * If we don't have enough space, put cursor in the middle.
1879 * This makes sure we get the same position when using "k" and "j"
1880 * in a small window.
1881 */
1882 if (used > curwin->w_height)
1883 scroll_cursor_halfway(FALSE);
1884 else
1885 {
1886 /*
1887 * If "always" is FALSE, only adjust topline to a lower value, higher
1888 * value may happen with wrapping lines
1889 */
1890 if (new_topline < curwin->w_topline || always)
1891 curwin->w_topline = new_topline;
1892 if (curwin->w_topline > curwin->w_cursor.lnum)
1893 curwin->w_topline = curwin->w_cursor.lnum;
1894#ifdef FEAT_DIFF
1895 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1896 if (curwin->w_topfill > 0 && extra > off)
1897 {
1898 curwin->w_topfill -= extra - off;
1899 if (curwin->w_topfill < 0)
1900 curwin->w_topfill = 0;
1901 }
1902 check_topfill(curwin, FALSE);
1903#endif
1904 if (curwin->w_topline != old_topline
1905#ifdef FEAT_DIFF
1906 || curwin->w_topfill != old_topfill
1907#endif
1908 )
1909 curwin->w_valid &=
1910 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1911 curwin->w_valid |= VALID_TOPLINE;
1912 }
1913}
1914
1915/*
1916 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1917 * screen lines for text lines.
1918 */
1919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001920set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921{
1922#ifdef FEAT_DIFF
1923 wp->w_filler_rows = 0;
1924#endif
1925 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001926 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 else
1928 {
1929 wp->w_empty_rows = wp->w_height - used;
1930#ifdef FEAT_DIFF
1931 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1932 {
1933 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1934 if (wp->w_empty_rows > wp->w_filler_rows)
1935 wp->w_empty_rows -= wp->w_filler_rows;
1936 else
1937 {
1938 wp->w_filler_rows = wp->w_empty_rows;
1939 wp->w_empty_rows = 0;
1940 }
1941 }
1942#endif
1943 }
1944}
1945
1946/*
1947 * Recompute topline to put the cursor at the bottom of the window.
1948 * Scroll at least "min_scroll" lines.
1949 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1950 * This is messy stuff!!!
1951 */
1952 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001953scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
1955 int used;
1956 int scrolled = 0;
1957 int extra = 0;
1958 int i;
1959 linenr_T line_count;
1960 linenr_T old_topline = curwin->w_topline;
1961 lineoff_T loff;
1962 lineoff_T boff;
1963#ifdef FEAT_DIFF
1964 int old_topfill = curwin->w_topfill;
1965 int fill_below_window;
1966#endif
1967 linenr_T old_botline = curwin->w_botline;
1968 linenr_T old_valid = curwin->w_valid;
1969 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001970 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001971 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 cln = curwin->w_cursor.lnum;
1974 if (set_topbot)
1975 {
1976 used = 0;
1977 curwin->w_botline = cln + 1;
1978#ifdef FEAT_DIFF
1979 loff.fill = 0;
1980#endif
1981 for (curwin->w_topline = curwin->w_botline;
1982 curwin->w_topline > 1;
1983 curwin->w_topline = loff.lnum)
1984 {
1985 loff.lnum = curwin->w_topline;
1986 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001987 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 break;
1989 used += loff.height;
1990#ifdef FEAT_DIFF
1991 curwin->w_topfill = loff.fill;
1992#endif
1993 }
1994 set_empty_rows(curwin, used);
1995 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1996 if (curwin->w_topline != old_topline
1997#ifdef FEAT_DIFF
1998 || curwin->w_topfill != old_topfill
1999#endif
2000 )
2001 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2002 }
2003 else
2004 validate_botline();
2005
Bram Moolenaar85a20022019-12-21 18:25:54 +01002006 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007#ifdef FEAT_DIFF
2008 used = plines_nofill(cln);
2009#else
2010 validate_cheight();
2011 used = curwin->w_cline_height;
2012#endif
2013
Bram Moolenaar85a20022019-12-21 18:25:54 +01002014 // If the cursor is below botline, we will at least scroll by the height
2015 // of the cursor line. Correct for empty lines, which are really part of
2016 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (cln >= curwin->w_botline)
2018 {
2019 scrolled = used;
2020 if (cln == curwin->w_botline)
2021 scrolled -= curwin->w_empty_rows;
2022 }
2023
2024 /*
2025 * Stop counting lines to scroll when
2026 * - hitting start of the file
2027 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002028 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 * - lines between botline and cursor have been counted
2030 */
2031#ifdef FEAT_FOLDING
2032 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2033#endif
2034 {
2035 loff.lnum = cln;
2036 boff.lnum = cln;
2037 }
2038#ifdef FEAT_DIFF
2039 loff.fill = 0;
2040 boff.fill = 0;
2041 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2042 - curwin->w_filler_rows;
2043#endif
2044
2045 while (loff.lnum > 1)
2046 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002047 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2048 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002050 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2052 && loff.lnum <= curwin->w_botline
2053#ifdef FEAT_DIFF
2054 && (loff.lnum < curwin->w_botline
2055 || loff.fill >= fill_below_window)
2056#endif
2057 )
2058 break;
2059
Bram Moolenaar85a20022019-12-21 18:25:54 +01002060 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002062 if (loff.height == MAXCOL)
2063 used = MAXCOL;
2064 else
2065 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (used > curwin->w_height)
2067 break;
2068 if (loff.lnum >= curwin->w_botline
2069#ifdef FEAT_DIFF
2070 && (loff.lnum > curwin->w_botline
2071 || loff.fill <= fill_below_window)
2072#endif
2073 )
2074 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002075 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 scrolled += loff.height;
2077 if (loff.lnum == curwin->w_botline
2078#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002079 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#endif
2081 )
2082 scrolled -= curwin->w_empty_rows;
2083 }
2084
2085 if (boff.lnum < curbuf->b_ml.ml_line_count)
2086 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002087 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 botline_forw(&boff);
2089 used += boff.height;
2090 if (used > curwin->w_height)
2091 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002092 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2093 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
2095 extra += boff.height;
2096 if (boff.lnum >= curwin->w_botline
2097#ifdef FEAT_DIFF
2098 || (boff.lnum + 1 == curwin->w_botline
2099 && boff.fill > curwin->w_filler_rows)
2100#endif
2101 )
2102 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002103 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 scrolled += boff.height;
2105 if (boff.lnum == curwin->w_botline
2106#ifdef FEAT_DIFF
2107 && boff.fill == 0
2108#endif
2109 )
2110 scrolled -= curwin->w_empty_rows;
2111 }
2112 }
2113 }
2114 }
2115
Bram Moolenaar85a20022019-12-21 18:25:54 +01002116 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (scrolled <= 0)
2118 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002119 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 else if (used > curwin->w_height)
2121 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002122 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 else
2124 {
2125 line_count = 0;
2126#ifdef FEAT_DIFF
2127 boff.fill = curwin->w_topfill;
2128#endif
2129 boff.lnum = curwin->w_topline - 1;
2130 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2131 {
2132 botline_forw(&boff);
2133 i += boff.height;
2134 ++line_count;
2135 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 line_count = 9999;
2138 }
2139
2140 /*
2141 * Scroll up if the cursor is off the bottom of the screen a bit.
2142 * Otherwise put it at 1/2 of the screen.
2143 */
2144 if (line_count >= curwin->w_height && line_count > min_scroll)
2145 scroll_cursor_halfway(FALSE);
2146 else
2147 scrollup(line_count, TRUE);
2148
2149 /*
2150 * If topline didn't change we need to restore w_botline and w_empty_rows
2151 * (we changed them).
2152 * If topline did change, update_screen() will set botline.
2153 */
2154 if (curwin->w_topline == old_topline && set_topbot)
2155 {
2156 curwin->w_botline = old_botline;
2157 curwin->w_empty_rows = old_empty_rows;
2158 curwin->w_valid = old_valid;
2159 }
2160 curwin->w_valid |= VALID_TOPLINE;
2161}
2162
2163/*
2164 * Recompute topline to put the cursor halfway the window
2165 * If "atend" is TRUE, also put it halfway at the end of the file.
2166 */
2167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002168scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170 int above = 0;
2171 linenr_T topline;
2172#ifdef FEAT_DIFF
2173 int topfill = 0;
2174#endif
2175 int below = 0;
2176 int used;
2177 lineoff_T loff;
2178 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002179#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002180 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002183#ifdef FEAT_PROP_POPUP
2184 // if the width changed this needs to be updated first
2185 may_update_popup_position();
2186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2188#ifdef FEAT_FOLDING
2189 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2190#endif
2191#ifdef FEAT_DIFF
2192 used = plines_nofill(loff.lnum);
2193 loff.fill = 0;
2194 boff.fill = 0;
2195#else
2196 used = plines(loff.lnum);
2197#endif
2198 topline = loff.lnum;
2199 while (topline > 1)
2200 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002201 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
2203 if (boff.lnum < curbuf->b_ml.ml_line_count)
2204 {
2205 botline_forw(&boff);
2206 used += boff.height;
2207 if (used > curwin->w_height)
2208 break;
2209 below += boff.height;
2210 }
2211 else
2212 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002213 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (atend)
2215 ++used;
2216 }
2217 }
2218
Bram Moolenaar85a20022019-12-21 18:25:54 +01002219 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
2221 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002222 if (loff.height == MAXCOL)
2223 used = MAXCOL;
2224 else
2225 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 if (used > curwin->w_height)
2227 break;
2228 above += loff.height;
2229 topline = loff.lnum;
2230#ifdef FEAT_DIFF
2231 topfill = loff.fill;
2232#endif
2233 }
2234 }
2235#ifdef FEAT_FOLDING
2236 if (!hasFolding(topline, &curwin->w_topline, NULL))
2237#endif
2238 curwin->w_topline = topline;
2239#ifdef FEAT_DIFF
2240 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002241 if (old_topline > curwin->w_topline + curwin->w_height)
2242 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 check_topfill(curwin, FALSE);
2244#endif
2245 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2246 curwin->w_valid |= VALID_TOPLINE;
2247}
2248
2249/*
2250 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002251 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 * If not possible, put it at the same position as scroll_cursor_halfway().
2253 * When called topline must be valid!
2254 */
2255 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002256cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002258 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002260 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 linenr_T botline;
2262 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002265 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
2267 /*
2268 * How many lines we would like to have above/below the cursor depends on
2269 * whether the first/last line of the file is on screen.
2270 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002271 above_wanted = so;
2272 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002273 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 above_wanted = mouse_dragging - 1;
2276 below_wanted = mouse_dragging - 1;
2277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (curwin->w_topline == 1)
2279 {
2280 above_wanted = 0;
2281 max_off = curwin->w_height / 2;
2282 if (below_wanted > max_off)
2283 below_wanted = max_off;
2284 }
2285 validate_botline();
2286 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002287 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 below_wanted = 0;
2290 max_off = (curwin->w_height - 1) / 2;
2291 if (above_wanted > max_off)
2292 above_wanted = max_off;
2293 }
2294
2295 /*
2296 * If there are sufficient file-lines above and below the cursor, we can
2297 * return now.
2298 */
2299 cln = curwin->w_cursor.lnum;
2300 if (cln >= curwin->w_topline + above_wanted
2301 && cln < curwin->w_botline - below_wanted
2302#ifdef FEAT_FOLDING
2303 && !hasAnyFolding(curwin)
2304#endif
2305 )
2306 return;
2307
2308 /*
2309 * Narrow down the area where the cursor can be put by taking lines from
2310 * the top and the bottom until:
2311 * - the desired context lines are found
2312 * - the lines from the top is past the lines from the bottom
2313 */
2314 topline = curwin->w_topline;
2315 botline = curwin->w_botline - 1;
2316#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002317 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 above = curwin->w_topfill;
2319 below = curwin->w_filler_rows;
2320#endif
2321 while ((above < above_wanted || below < below_wanted) && topline < botline)
2322 {
2323 if (below < below_wanted && (below <= above || above >= above_wanted))
2324 {
2325#ifdef FEAT_FOLDING
2326 if (hasFolding(botline, &botline, NULL))
2327 ++below;
2328 else
2329#endif
2330 below += plines(botline);
2331 --botline;
2332 }
2333 if (above < above_wanted && (above < below || below >= below_wanted))
2334 {
2335#ifdef FEAT_FOLDING
2336 if (hasFolding(topline, NULL, &topline))
2337 ++above;
2338 else
2339#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002340 above += PLINES_NOFILL(topline);
2341#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002342 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (topline < botline)
2344 above += diff_check_fill(curwin, topline + 1);
2345#endif
2346 ++topline;
2347 }
2348 }
2349 if (topline == botline || botline == 0)
2350 curwin->w_cursor.lnum = topline;
2351 else if (topline > botline)
2352 curwin->w_cursor.lnum = botline;
2353 else
2354 {
2355 if (cln < topline && curwin->w_topline > 1)
2356 {
2357 curwin->w_cursor.lnum = topline;
2358 curwin->w_valid &=
2359 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2360 }
2361 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2362 {
2363 curwin->w_cursor.lnum = botline;
2364 curwin->w_valid &=
2365 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2366 }
2367 }
2368 curwin->w_valid |= VALID_TOPLINE;
2369}
2370
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002371static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373/*
2374 * move screen 'count' pages up or down and update screen
2375 *
2376 * return FAIL for failure, OK otherwise
2377 */
2378 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002379onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 long n;
2382 int retval = OK;
2383 lineoff_T loff;
2384 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002385 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 beep_flush();
2390 return FAIL;
2391 }
2392
2393 for ( ; count > 0; --count)
2394 {
2395 validate_botline();
2396 /*
2397 * It's an error to move a page up when the first line is already on
2398 * the screen. It's an error to move a page down when the last line
2399 * is on the screen and the topline is 'scrolloff' lines from the
2400 * last line.
2401 */
2402 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002403 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2405 : (curwin->w_topline == 1
2406#ifdef FEAT_DIFF
2407 && curwin->w_topfill ==
2408 diff_check_fill(curwin, curwin->w_topline)
2409#endif
2410 ))
2411 {
2412 beep_flush();
2413 retval = FAIL;
2414 break;
2415 }
2416
2417#ifdef FEAT_DIFF
2418 loff.fill = 0;
2419#endif
2420 if (dir == FORWARD)
2421 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002422 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002424 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002425 if (p_window <= 2)
2426 ++curwin->w_topline;
2427 else
2428 curwin->w_topline += p_window - 2;
2429 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2430 curwin->w_topline = curbuf->b_ml.ml_line_count;
2431 curwin->w_cursor.lnum = curwin->w_topline;
2432 }
2433 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2434 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002435 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 curwin->w_topline = curbuf->b_ml.ml_line_count;
2437#ifdef FEAT_DIFF
2438 curwin->w_topfill = 0;
2439#endif
2440 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2441 }
2442 else
2443 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002444 // For the overlap, start with the line just below the window
2445 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 loff.lnum = curwin->w_botline;
2447#ifdef FEAT_DIFF
2448 loff.fill = diff_check_fill(curwin, loff.lnum)
2449 - curwin->w_filler_rows;
2450#endif
2451 get_scroll_overlap(&loff, -1);
2452 curwin->w_topline = loff.lnum;
2453#ifdef FEAT_DIFF
2454 curwin->w_topfill = loff.fill;
2455 check_topfill(curwin, FALSE);
2456#endif
2457 curwin->w_cursor.lnum = curwin->w_topline;
2458 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2459 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2460 }
2461 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002462 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
2464#ifdef FEAT_DIFF
2465 if (curwin->w_topline == 1)
2466 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002467 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 max_topfill();
2469 continue;
2470 }
2471#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002472 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002473 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002474 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002475 if (p_window <= 2)
2476 --curwin->w_topline;
2477 else
2478 curwin->w_topline -= p_window - 2;
2479 if (curwin->w_topline < 1)
2480 curwin->w_topline = 1;
2481 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2482 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2483 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2484 continue;
2485 }
2486
Bram Moolenaar85a20022019-12-21 18:25:54 +01002487 // Find the line at the top of the window that is going to be the
2488 // line at the bottom of the window. Make sure this results in
2489 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 loff.lnum = curwin->w_topline - 1;
2491#ifdef FEAT_DIFF
2492 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2493 - curwin->w_topfill;
2494#endif
2495 get_scroll_overlap(&loff, 1);
2496
2497 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2498 {
2499 loff.lnum = curbuf->b_ml.ml_line_count;
2500#ifdef FEAT_DIFF
2501 loff.fill = 0;
2502 }
2503 else
2504 {
2505 botline_topline(&loff);
2506#endif
2507 }
2508 curwin->w_cursor.lnum = loff.lnum;
2509
Bram Moolenaar85a20022019-12-21 18:25:54 +01002510 // Find the line just above the new topline to get the right line
2511 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 n = 0;
2513 while (n <= curwin->w_height && loff.lnum >= 1)
2514 {
2515 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002516 if (loff.height == MAXCOL)
2517 n = MAXCOL;
2518 else
2519 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002521 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
2523 curwin->w_topline = 1;
2524#ifdef FEAT_DIFF
2525 max_topfill();
2526#endif
2527 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2528 }
2529 else
2530 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002531 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#ifdef FEAT_DIFF
2533 topline_botline(&loff);
2534#endif
2535 botline_forw(&loff);
2536 botline_forw(&loff);
2537#ifdef FEAT_DIFF
2538 botline_topline(&loff);
2539#endif
2540#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002541 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2543#endif
2544
Bram Moolenaar85a20022019-12-21 18:25:54 +01002545 // Always scroll at least one line. Avoid getting stuck on
2546 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (loff.lnum >= curwin->w_topline
2548#ifdef FEAT_DIFF
2549 && (loff.lnum > curwin->w_topline
2550 || loff.fill >= curwin->w_topfill)
2551#endif
2552 )
2553 {
2554#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002555 // First try using the maximum number of filler lines. If
2556 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 loff.fill = curwin->w_topfill;
2558 if (curwin->w_topfill < diff_check_fill(curwin,
2559 curwin->w_topline))
2560 max_topfill();
2561 if (curwin->w_topfill == loff.fill)
2562#endif
2563 {
2564 --curwin->w_topline;
2565#ifdef FEAT_DIFF
2566 curwin->w_topfill = 0;
2567#endif
2568 }
2569 comp_botline(curwin);
2570 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002571 curwin->w_valid &=
2572 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 else
2575 {
2576 curwin->w_topline = loff.lnum;
2577#ifdef FEAT_DIFF
2578 curwin->w_topfill = loff.fill;
2579 check_topfill(curwin, FALSE);
2580#endif
2581 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2582 }
2583 }
2584 }
2585 }
2586#ifdef FEAT_FOLDING
2587 foldAdjustCursor();
2588#endif
2589 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002590 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002591 if (retval == OK)
2592 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2594
Bram Moolenaar907dad72018-07-10 15:07:15 +02002595 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002597 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2598 // But make sure we scroll at least one line (happens with mix of long
2599 // wrapping lines and non-wrapping line).
2600 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002602 scroll_cursor_top(1, FALSE);
2603 if (curwin->w_topline <= old_topline
2604 && old_topline < curbuf->b_ml.ml_line_count)
2605 {
2606 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002608 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2609#endif
2610 }
2611 }
2612#ifdef FEAT_FOLDING
2613 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
2617
2618 redraw_later(VALID);
2619 return retval;
2620}
2621
2622/*
2623 * Decide how much overlap to use for page-up or page-down scrolling.
2624 * This is symmetric, so that doing both keeps the same lines displayed.
2625 * Three lines are examined:
2626 *
2627 * before CTRL-F after CTRL-F / before CTRL-B
2628 * etc. l1
2629 * l1 last but one line ------------
2630 * l2 last text line l2 top text line
2631 * ------------- l3 second text line
2632 * l3 etc.
2633 */
2634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002635get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 int h1, h2, h3, h4;
2638 int min_height = curwin->w_height - 2;
2639 lineoff_T loff0, loff1, loff2;
2640
2641#ifdef FEAT_DIFF
2642 if (lp->fill > 0)
2643 lp->height = 1;
2644 else
2645 lp->height = plines_nofill(lp->lnum);
2646#else
2647 lp->height = plines(lp->lnum);
2648#endif
2649 h1 = lp->height;
2650 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 loff0 = *lp;
2654 if (dir > 0)
2655 botline_forw(lp);
2656 else
2657 topline_back(lp);
2658 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002659 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002661 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 return;
2663 }
2664
2665 loff1 = *lp;
2666 if (dir > 0)
2667 botline_forw(lp);
2668 else
2669 topline_back(lp);
2670 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002671 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002673 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 return;
2675 }
2676
2677 loff2 = *lp;
2678 if (dir > 0)
2679 botline_forw(lp);
2680 else
2681 topline_back(lp);
2682 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002683 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002684 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002686 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687}
2688
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689/*
2690 * Scroll 'scroll' lines up or down.
2691 */
2692 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002693halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 long scrolled = 0;
2696 int i;
2697 int n;
2698 int room;
2699
2700 if (Prenum)
2701 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2702 curwin->w_height : Prenum;
2703 n = (curwin->w_p_scr <= curwin->w_height) ?
2704 curwin->w_p_scr : curwin->w_height;
2705
Bram Moolenaard5d37532017-03-27 23:02:07 +02002706 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 validate_botline();
2708 room = curwin->w_empty_rows;
2709#ifdef FEAT_DIFF
2710 room += curwin->w_filler_rows;
2711#endif
2712 if (flag)
2713 {
2714 /*
2715 * scroll the text up
2716 */
2717 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2718 {
2719#ifdef FEAT_DIFF
2720 if (curwin->w_topfill > 0)
2721 {
2722 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002723 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 --curwin->w_topfill;
2725 }
2726 else
2727#endif
2728 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002729 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 n -= i;
2731 if (n < 0 && scrolled > 0)
2732 break;
2733#ifdef FEAT_FOLDING
2734 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2735#endif
2736 ++curwin->w_topline;
2737#ifdef FEAT_DIFF
2738 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2739#endif
2740
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2742 {
2743 ++curwin->w_cursor.lnum;
2744 curwin->w_valid &=
2745 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
2748 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2749 scrolled += i;
2750
2751 /*
2752 * Correct w_botline for changed w_topline.
2753 * Won't work when there are filler lines.
2754 */
2755#ifdef FEAT_DIFF
2756 if (curwin->w_p_diff)
2757 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2758 else
2759#endif
2760 {
2761 room += i;
2762 do
2763 {
2764 i = plines(curwin->w_botline);
2765 if (i > room)
2766 break;
2767#ifdef FEAT_FOLDING
2768 (void)hasFolding(curwin->w_botline, NULL,
2769 &curwin->w_botline);
2770#endif
2771 ++curwin->w_botline;
2772 room -= i;
2773 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2774 }
2775 }
2776
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 /*
2778 * When hit bottom of the file: move cursor down.
2779 */
2780 if (n > 0)
2781 {
2782# ifdef FEAT_FOLDING
2783 if (hasAnyFolding(curwin))
2784 {
2785 while (--n >= 0
2786 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2787 {
2788 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2789 &curwin->w_cursor.lnum);
2790 ++curwin->w_cursor.lnum;
2791 }
2792 }
2793 else
2794# endif
2795 curwin->w_cursor.lnum += n;
2796 check_cursor_lnum();
2797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799 else
2800 {
2801 /*
2802 * scroll the text down
2803 */
2804 while (n > 0 && curwin->w_topline > 1)
2805 {
2806#ifdef FEAT_DIFF
2807 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2808 {
2809 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002810 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 ++curwin->w_topfill;
2812 }
2813 else
2814#endif
2815 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002816 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 n -= i;
2818 if (n < 0 && scrolled > 0)
2819 break;
2820 --curwin->w_topline;
2821#ifdef FEAT_FOLDING
2822 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2823#endif
2824#ifdef FEAT_DIFF
2825 curwin->w_topfill = 0;
2826#endif
2827 }
2828 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2829 VALID_BOTLINE|VALID_BOTLINE_AP);
2830 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 if (curwin->w_cursor.lnum > 1)
2832 {
2833 --curwin->w_cursor.lnum;
2834 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 /*
2839 * When hit top of the file: move cursor up.
2840 */
2841 if (n > 0)
2842 {
2843 if (curwin->w_cursor.lnum <= (linenr_T)n)
2844 curwin->w_cursor.lnum = 1;
2845 else
2846# ifdef FEAT_FOLDING
2847 if (hasAnyFolding(curwin))
2848 {
2849 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2850 {
2851 --curwin->w_cursor.lnum;
2852 (void)hasFolding(curwin->w_cursor.lnum,
2853 &curwin->w_cursor.lnum, NULL);
2854 }
2855 }
2856 else
2857# endif
2858 curwin->w_cursor.lnum -= n;
2859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002862 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 foldAdjustCursor();
2864# endif
2865#ifdef FEAT_DIFF
2866 check_topfill(curwin, !flag);
2867#endif
2868 cursor_correct();
2869 beginline(BL_SOL | BL_FIX);
2870 redraw_later(VALID);
2871}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002872
Bram Moolenaar860cae12010-06-05 23:22:07 +02002873 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002874do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002875{
2876 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002877 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002878 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002879 colnr_T curswant = curwin->w_curswant;
2880 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002881 win_T *old_curwin = curwin;
2882 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002883 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002884 int old_VIsual_select = VIsual_select;
2885 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002886
2887 /*
2888 * loop through the cursorbound windows
2889 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002890 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002891 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002892 {
2893 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002894 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002895 if (curwin != old_curwin && curwin->w_p_crb)
2896 {
2897# ifdef FEAT_DIFF
2898 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002899 curwin->w_cursor.lnum =
2900 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002901 else
2902# endif
2903 curwin->w_cursor.lnum = line;
2904 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002905 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002906 curwin->w_curswant = curswant;
2907 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002908
Bram Moolenaar85a20022019-12-21 18:25:54 +01002909 // Make sure the cursor is in a valid position. Temporarily set
2910 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002911 restart_edit_save = restart_edit;
2912 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002913 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002914# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002915 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002916 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002917# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002918 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002920 if (has_mbyte)
2921 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002922 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002923
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002925 if (!curwin->w_p_scb)
2926 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002927 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002928 }
2929 }
2930
2931 /*
2932 * reset current-window
2933 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002934 VIsual_select = old_VIsual_select;
2935 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002936 curwin = old_curwin;
2937 curbuf = old_curbuf;
2938}