blob: 20a34e8e56aa5e156f5d2c75cc56fd294fc61894 [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 {
199 curwin->w_topline = curwin->w_cursor.lnum;
200 curwin->w_botline = curwin->w_topline;
201 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
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{
537#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100538 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
540#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100541 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 wp->w_botline += lnum - wp->w_topline;
543 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000544 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545#ifdef FEAT_DIFF
546 wp->w_topfill = 0;
547#endif
548 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100549 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 redraw_later(VALID);
551}
552
553/*
554 * Call this function when the length of the cursor line (in screen
555 * characters) has changed, and the change is before the cursor.
556 * Need to take care of w_botline separately!
557 */
558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100559changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
562 |VALID_CHEIGHT|VALID_TOPLINE);
563}
564
565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100566changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
569 |VALID_CHEIGHT|VALID_TOPLINE);
570}
571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572/*
573 * Call this function when the length of a line (in screen characters) above
574 * the cursor have changed.
575 * Need to take care of w_botline separately!
576 */
577 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100578changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
581 |VALID_CHEIGHT|VALID_TOPLINE);
582}
583
584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100585changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
588 |VALID_CHEIGHT|VALID_TOPLINE);
589}
590
591/*
592 * Make sure the value of curwin->w_botline is valid.
593 */
594 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100595validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596{
597 if (!(curwin->w_valid & VALID_BOTLINE))
598 comp_botline(curwin);
599}
600
601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 * Mark curwin->w_botline as invalid (because of some change in the buffer).
603 */
604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100605invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606{
607 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
608}
609
610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100611invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
614}
615
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100617approximate_botline_win(
618 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
620 wp->w_valid &= ~VALID_BOTLINE;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
624 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
625 */
626 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100627cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 check_cursor_moved(curwin);
630 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
631 (VALID_WROW|VALID_WCOL));
632}
633
634/*
635 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
636 * w_topline must be valid, you may need to call update_topline() first!
637 */
638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100639validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 check_cursor_moved(curwin);
642 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
643 curs_columns(TRUE);
644}
645
646#if defined(FEAT_GUI) || defined(PROTO)
647/*
648 * validate w_cline_row.
649 */
650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100651validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 /*
654 * First make sure that w_topline is valid (after moving the cursor).
655 */
656 update_topline();
657 check_cursor_moved(curwin);
658 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100659 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660}
661#endif
662
663/*
664 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200665 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 */
667 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100668curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 linenr_T lnum;
671 int i;
672 int all_invalid;
673 int valid;
674#ifdef FEAT_FOLDING
675 long fold_count;
676#endif
677
Bram Moolenaar85a20022019-12-21 18:25:54 +0100678 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 all_invalid = (!redrawing()
680 || wp->w_lines_valid == 0
681 || wp->w_lines[0].wl_lnum > wp->w_topline);
682 i = 0;
683 wp->w_cline_row = 0;
684 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
685 {
686 valid = FALSE;
687 if (!all_invalid && i < wp->w_lines_valid)
688 {
689 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100690 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 if (wp->w_lines[i].wl_lnum == lnum)
692 {
693#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100694 // Check for newly inserted lines below this row, in which
695 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (!wp->w_buffer->b_mod_set
697 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
698 || wp->w_buffer->b_mod_top
699 > wp->w_lines[i].wl_lastlnum + 1)
700#endif
701 valid = TRUE;
702 }
703 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100704 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706 if (valid
707#ifdef FEAT_DIFF
708 && (lnum != wp->w_topline || !wp->w_p_diff)
709#endif
710 )
711 {
712#ifdef FEAT_FOLDING
713 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100714 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (lnum > wp->w_cursor.lnum)
716 break;
717#else
718 ++lnum;
719#endif
720 wp->w_cline_row += wp->w_lines[i].wl_size;
721 }
722 else
723 {
724#ifdef FEAT_FOLDING
725 fold_count = foldedCount(wp, lnum, NULL);
726 if (fold_count)
727 {
728 lnum += fold_count;
729 if (lnum > wp->w_cursor.lnum)
730 break;
731 ++wp->w_cline_row;
732 }
733 else
734#endif
735#ifdef FEAT_DIFF
736 if (lnum == wp->w_topline)
737 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
738 + wp->w_topfill;
739 else
740#endif
741 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
742 }
743 }
744
745 check_cursor_moved(wp);
746 if (!(wp->w_valid & VALID_CHEIGHT))
747 {
748 if (all_invalid
749 || i == wp->w_lines_valid
750 || (i < wp->w_lines_valid
751 && (!wp->w_lines[i].wl_valid
752 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
753 {
754#ifdef FEAT_DIFF
755 if (wp->w_cursor.lnum == wp->w_topline)
756 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
757 TRUE) + wp->w_topfill;
758 else
759#endif
760 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
761#ifdef FEAT_FOLDING
762 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
763 NULL, NULL, TRUE, NULL);
764#endif
765 }
766 else if (i > wp->w_lines_valid)
767 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100768 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 wp->w_cline_height = 0;
770#ifdef FEAT_FOLDING
771 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
772 NULL, NULL, TRUE, NULL);
773#endif
774 }
775 else
776 {
777 wp->w_cline_height = wp->w_lines[i].wl_size;
778#ifdef FEAT_FOLDING
779 wp->w_cline_folded = wp->w_lines[i].wl_folded;
780#endif
781 }
782 }
783
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100784 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
786
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788
789/*
790 * Validate curwin->w_virtcol only.
791 */
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 validate_virtcol_win(curwin);
796}
797
798/*
799 * Validate wp->w_virtcol only.
800 */
801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 check_cursor_moved(wp);
805 if (!(wp->w_valid & VALID_VIRTCOL))
806 {
807 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
808 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000809#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200810 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000811 redraw_win_later(wp, SOME_VALID);
812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 }
814}
815
816/*
817 * Validate curwin->w_cline_height only.
818 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100820validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
822 check_cursor_moved(curwin);
823 if (!(curwin->w_valid & VALID_CHEIGHT))
824 {
825#ifdef FEAT_DIFF
826 if (curwin->w_cursor.lnum == curwin->w_topline)
827 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
828 + curwin->w_topfill;
829 else
830#endif
831 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
832#ifdef FEAT_FOLDING
833 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
834#endif
835 curwin->w_valid |= VALID_CHEIGHT;
836 }
837}
838
839/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000840 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 */
842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100843validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
845 colnr_T off;
846 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100847 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
849 validate_virtcol();
850 if (!(curwin->w_valid & VALID_WCOL))
851 {
852 col = curwin->w_virtcol;
853 off = curwin_col_off();
854 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200855 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaar85a20022019-12-21 18:25:54 +0100857 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000858 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200859 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100860 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100861 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200862 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000863 if (col > (int)curwin->w_leftcol)
864 col -= curwin->w_leftcol;
865 else
866 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 curwin->w_valid |= VALID_WCOL;
870 }
871}
872
873/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200874 * Compute offset of a window, occupied by absolute or relative line number,
875 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 */
877 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100878win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
Bram Moolenaar64486672010-05-16 15:46:46 +0200880 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#ifdef FEAT_CMDWIN
882 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
883#endif
884#ifdef FEAT_FOLDING
885 + wp->w_p_fdc
886#endif
887#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200888 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889#endif
890 );
891}
892
893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100894curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 return win_col_off(curwin);
897}
898
899/*
900 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200901 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
902 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 */
904 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100905win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
Bram Moolenaar64486672010-05-16 15:46:46 +0200907 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000908 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 return 0;
910}
911
912 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100913curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
915 return win_col_off2(curwin);
916}
917
918/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100919 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 * Also updates curwin->w_wrow and curwin->w_cline_row.
921 * Also updates curwin->w_leftcol.
922 */
923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100924curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100925 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
927 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100928 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 int off_left, off_right;
930 int n;
931 int p_lines;
932 int width = 0;
933 int textwidth;
934 int new_leftcol;
935 colnr_T startcol;
936 colnr_T endcol;
937 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100938 long so = get_scrolloff_value();
939 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 /*
942 * First make sure that w_topline is valid (after moving the cursor).
943 */
944 update_topline();
945
946 /*
947 * Next make sure that w_cline_row is valid.
948 */
949 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100950 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952 /*
953 * Compute the number of virtual columns.
954 */
955#ifdef FEAT_FOLDING
956 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100957 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
959 else
960#endif
961 getvvcol(curwin, &curwin->w_cursor,
962 &startcol, &(curwin->w_virtcol), &endcol);
963
Bram Moolenaar85a20022019-12-21 18:25:54 +0100964 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100966 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968 extra = curwin_col_off();
969 curwin->w_wcol = curwin->w_virtcol + extra;
970 endcol += extra;
971
972 /*
973 * Now compute w_wrow, counting screen lines from w_cline_row.
974 */
975 curwin->w_wrow = curwin->w_cline_row;
976
Bram Moolenaar02631462017-09-22 15:20:32 +0200977 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (textwidth <= 0)
979 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100980 // No room for text, put cursor in last char of window.
Bram Moolenaar02631462017-09-22 15:20:32 +0200981 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 curwin->w_wrow = curwin->w_height - 1;
983 }
Bram Moolenaar4033c552017-09-16 20:54:51 +0200984 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 width = textwidth + curwin_col_off2();
987
Bram Moolenaar85a20022019-12-21 18:25:54 +0100988 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +0200989 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100991#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +0100992 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +0100993#endif
Bram Moolenaaree857022019-11-09 23:26:40 +0100994
Bram Moolenaar85a20022019-12-21 18:25:54 +0100995 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +0200996 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 curwin->w_wcol -= n * width;
998 curwin->w_wrow += n;
999
1000#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001001 // When cursor wraps to first char of next line in Insert
1002 // mode, the 'showbreak' string isn't shown, backup to first
1003 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001004 sbr = get_showbreak_value(curwin);
1005 if (*sbr && *ml_get_cursor() == NUL
1006 && curwin->w_wcol == (int)vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 curwin->w_wcol = 0;
1008#endif
1009 }
1010 }
1011
Bram Moolenaar85a20022019-12-21 18:25:54 +01001012 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1013 // is not folded.
1014 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001015 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#ifdef FEAT_FOLDING
1017 && !curwin->w_cline_folded
1018#endif
1019 )
1020 {
1021 /*
1022 * If Cursor is left of the screen, scroll rightwards.
1023 * If Cursor is right of the screen, scroll leftwards
1024 * If we get closer to the edge than 'sidescrolloff', scroll a little
1025 * extra
1026 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001027 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001028 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001029 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (off_left < 0 || off_right > 0)
1031 {
1032 if (off_left < 0)
1033 diff = -off_left;
1034 else
1035 diff = off_right;
1036
Bram Moolenaar85a20022019-12-21 18:25:54 +01001037 // When far off or not enough room on either side, put cursor in
1038 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1040 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1041 else
1042 {
1043 if (diff < p_ss)
1044 diff = p_ss;
1045 if (off_left < 0)
1046 new_leftcol = curwin->w_leftcol - diff;
1047 else
1048 new_leftcol = curwin->w_leftcol + diff;
1049 }
1050 if (new_leftcol < 0)
1051 new_leftcol = 0;
1052 if (new_leftcol != (int)curwin->w_leftcol)
1053 {
1054 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001055 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 redraw_later(NOT_VALID);
1057 }
1058 }
1059 curwin->w_wcol -= curwin->w_leftcol;
1060 }
1061 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1062 curwin->w_wcol -= curwin->w_leftcol;
1063 else
1064 curwin->w_wcol = 0;
1065
1066#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001067 // Skip over filler lines. At the top use w_topfill, there
1068 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (curwin->w_cursor.lnum == curwin->w_topline)
1070 curwin->w_wrow += curwin->w_topfill;
1071 else
1072 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1073#endif
1074
1075 prev_skipcol = curwin->w_skipcol;
1076
1077 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if ((curwin->w_wrow >= curwin->w_height
1080 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001081 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 && (p_lines =
1083#ifdef FEAT_DIFF
1084 plines_win_nofill
1085#else
1086 plines_win
1087#endif
1088 (curwin, curwin->w_cursor.lnum, FALSE))
1089 - 1 >= curwin->w_height))
1090 && curwin->w_height != 0
1091 && curwin->w_cursor.lnum == curwin->w_topline
1092 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001093 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001095 // Cursor past end of screen. Happens with a single line that does
1096 // not fit on screen. Find a skipcol to show the text around the
1097 // cursor. Avoid scrolling all the time. compute value of "extra":
1098 // 1: Less than 'scrolloff' lines above
1099 // 2: Less than 'scrolloff' lines below
1100 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001102 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001104 // Compute last display line of the buffer line that we want at the
1105 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (p_lines == 0)
1107 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1108 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001109 if (p_lines > curwin->w_wrow + so)
1110 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 else
1112 n = p_lines;
1113 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width)
1114 extra += 2;
1115
Bram Moolenaar375e3392019-01-31 18:26:10 +01001116 if (extra == 3 || p_lines < so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001118 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 n = curwin->w_virtcol / width;
1120 if (n > curwin->w_height / 2)
1121 n -= curwin->w_height / 2;
1122 else
1123 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (n > p_lines - curwin->w_height + 1)
1126 n = p_lines - curwin->w_height + 1;
1127 curwin->w_skipcol = n * width;
1128 }
1129 else if (extra == 1)
1130 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001131 // less then 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001132 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 + width - 1) / width;
1134 if (extra > 0)
1135 {
1136 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1137 extra = curwin->w_skipcol / width;
1138 curwin->w_skipcol -= extra * width;
1139 }
1140 }
1141 else if (extra == 2)
1142 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001143 // less then 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 endcol = (n - curwin->w_height + 1) * width;
1145 while (endcol > curwin->w_virtcol)
1146 endcol -= width;
1147 if (endcol > curwin->w_skipcol)
1148 curwin->w_skipcol = endcol;
1149 }
1150
1151 curwin->w_wrow -= curwin->w_skipcol / width;
1152 if (curwin->w_wrow >= curwin->w_height)
1153 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001154 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 extra = curwin->w_wrow - curwin->w_height + 1;
1156 curwin->w_skipcol += extra * width;
1157 curwin->w_wrow -= extra;
1158 }
1159
1160 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1161 if (extra > 0)
1162 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1163 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001164 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 else
1167 curwin->w_skipcol = 0;
1168 if (prev_skipcol != curwin->w_skipcol)
1169 redraw_later(NOT_VALID);
1170
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001171#ifdef FEAT_SYN_HL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001172 // Redraw when w_virtcol changes and 'cursorcolumn' is set
Bram Moolenaarb6798752014-03-27 12:11:48 +01001173 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001174 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001175 redraw_later(SOME_VALID);
1176#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001177#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1178 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1179 {
1180 curwin->w_wrow += popup_top_extra(curwin);
1181 curwin->w_wcol += popup_left_extra(curwin);
1182 }
1183#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001184
Bram Moolenaar08f23632019-11-16 14:19:33 +01001185 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1186 curwin->w_valid_leftcol = curwin->w_leftcol;
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1189}
1190
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001191#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001192/*
1193 * Compute the screen position of text character at "pos" in window "wp"
1194 * The resulting values are one-based, zero when character is not visible.
1195 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001196 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001197textpos2screenpos(
1198 win_T *wp,
1199 pos_T *pos,
1200 int *rowp, // screen row
1201 int *scolp, // start screen column
1202 int *ccolp, // cursor screen column
1203 int *ecolp) // end screen column
1204{
1205 colnr_T scol = 0, ccol = 0, ecol = 0;
1206 int row = 0;
1207 int rowoff = 0;
1208 colnr_T coloff = 0;
1209
1210 if (pos->lnum >= wp->w_topline && pos->lnum < wp->w_botline)
1211 {
1212 colnr_T off;
1213 colnr_T col;
1214 int width;
1215
1216 row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1;
1217 getvcol(wp, pos, &scol, &ccol, &ecol);
1218
1219 // similar to what is done in validate_cursor_col()
1220 col = scol;
1221 off = win_col_off(wp);
1222 col += off;
1223 width = wp->w_width - off + win_col_off2(wp);
1224
Bram Moolenaar12034e22019-08-25 22:25:02 +02001225 // long line wrapping, adjust row
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001226 if (wp->w_p_wrap
1227 && col >= (colnr_T)wp->w_width
1228 && width > 0)
1229 {
Bram Moolenaar12034e22019-08-25 22:25:02 +02001230 // use same formula as what is used in curs_columns()
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001231 rowoff = ((col - wp->w_width) / width + 1);
1232 col -= rowoff * width;
1233 }
1234 col -= wp->w_leftcol;
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +01001235 if (col >= wp->w_width)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001236 col = -1;
1237 if (col >= 0)
1238 coloff = col - scol + wp->w_wincol + 1;
1239 else
1240 // character is left or right of the window
1241 row = scol = ccol = ecol = 0;
1242 }
1243 *rowp = wp->w_winrow + row + rowoff;
1244 *scolp = scol + coloff;
1245 *ccolp = ccol + coloff;
1246 *ecolp = ecol + coloff;
1247}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001248#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001249
Bram Moolenaar12034e22019-08-25 22:25:02 +02001250#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001251/*
1252 * "screenpos({winid}, {lnum}, {col})" function
1253 */
1254 void
1255f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1256{
1257 dict_T *dict;
1258 win_T *wp;
1259 pos_T pos;
1260 int row = 0;
1261 int scol = 0, ccol = 0, ecol = 0;
1262
1263 if (rettv_dict_alloc(rettv) != OK)
1264 return;
1265 dict = rettv->vval.v_dict;
1266
1267 wp = find_win_by_nr_or_id(&argvars[0]);
1268 if (wp == NULL)
1269 return;
1270
1271 pos.lnum = tv_get_number(&argvars[1]);
1272 pos.col = tv_get_number(&argvars[2]) - 1;
1273 pos.coladd = 0;
1274 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1275
1276 dict_add_number(dict, "row", row);
1277 dict_add_number(dict, "col", scol);
1278 dict_add_number(dict, "curscol", ccol);
1279 dict_add_number(dict, "endcol", ecol);
1280}
1281#endif
1282
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283/*
1284 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001287scrolldown(
1288 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001289 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001291 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 int wrow;
1293 int moved = FALSE;
1294
1295#ifdef FEAT_FOLDING
1296 linenr_T first;
1297
Bram Moolenaar85a20022019-12-21 18:25:54 +01001298 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1300#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001301 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 while (line_count-- > 0)
1303 {
1304#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001305 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1306 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
1308 ++curwin->w_topfill;
1309 ++done;
1310 }
1311 else
1312#endif
1313 {
1314 if (curwin->w_topline == 1)
1315 break;
1316 --curwin->w_topline;
1317#ifdef FEAT_DIFF
1318 curwin->w_topfill = 0;
1319#endif
1320#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001321 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (hasFolding(curwin->w_topline, &first, NULL))
1323 {
1324 ++done;
1325 if (!byfold)
1326 line_count -= curwin->w_topline - first - 1;
1327 curwin->w_botline -= curwin->w_topline - first;
1328 curwin->w_topline = first;
1329 }
1330 else
1331#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001332 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001334 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 invalidate_botline();
1336 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001337 curwin->w_wrow += done; // keep w_wrow updated
1338 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340#ifdef FEAT_DIFF
1341 if (curwin->w_cursor.lnum == curwin->w_topline)
1342 curwin->w_cline_row = 0;
1343 check_topfill(curwin, TRUE);
1344#endif
1345
1346 /*
1347 * Compute the row number of the last row of the cursor line
1348 * and move the cursor onto the displayed part of the window.
1349 */
1350 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001351 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 {
1353 validate_virtcol();
1354 validate_cheight();
1355 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001356 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1359 {
1360#ifdef FEAT_FOLDING
1361 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1362 {
1363 --wrow;
1364 if (first == 1)
1365 curwin->w_cursor.lnum = 1;
1366 else
1367 curwin->w_cursor.lnum = first - 1;
1368 }
1369 else
1370#endif
1371 wrow -= plines(curwin->w_cursor.lnum--);
1372 curwin->w_valid &=
1373 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1374 moved = TRUE;
1375 }
1376 if (moved)
1377 {
1378#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001379 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 foldAdjustCursor();
1381#endif
1382 coladvance(curwin->w_curswant);
1383 }
1384}
1385
1386/*
1387 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001390scrollup(
1391 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001392 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1395 linenr_T lnum;
1396
1397 if (
1398# ifdef FEAT_FOLDING
1399 (byfold && hasAnyFolding(curwin))
1400# ifdef FEAT_DIFF
1401 ||
1402# endif
1403# endif
1404# ifdef FEAT_DIFF
1405 curwin->w_p_diff
1406# endif
1407 )
1408 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001409 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 lnum = curwin->w_topline;
1411 while (line_count--)
1412 {
1413# ifdef FEAT_DIFF
1414 if (curwin->w_topfill > 0)
1415 --curwin->w_topfill;
1416 else
1417# endif
1418 {
1419# ifdef FEAT_FOLDING
1420 if (byfold)
1421 (void)hasFolding(lnum, NULL, &lnum);
1422# endif
1423 if (lnum >= curbuf->b_ml.ml_line_count)
1424 break;
1425 ++lnum;
1426# ifdef FEAT_DIFF
1427 curwin->w_topfill = diff_check_fill(curwin, lnum);
1428# endif
1429 }
1430 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001431 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 curwin->w_botline += lnum - curwin->w_topline;
1433 curwin->w_topline = lnum;
1434 }
1435 else
1436#endif
1437 {
1438 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001439 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
1441
1442 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1443 curwin->w_topline = curbuf->b_ml.ml_line_count;
1444 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1445 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1446
1447#ifdef FEAT_DIFF
1448 check_topfill(curwin, FALSE);
1449#endif
1450
1451#ifdef FEAT_FOLDING
1452 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001453 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1455#endif
1456
1457 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1458 if (curwin->w_cursor.lnum < curwin->w_topline)
1459 {
1460 curwin->w_cursor.lnum = curwin->w_topline;
1461 curwin->w_valid &=
1462 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1463 coladvance(curwin->w_curswant);
1464 }
1465}
1466
1467#ifdef FEAT_DIFF
1468/*
1469 * Don't end up with too many filler lines in the window.
1470 */
1471 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001472check_topfill(
1473 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001474 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 int n;
1477
1478 if (wp->w_topfill > 0)
1479 {
1480 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1481 if (wp->w_topfill + n > wp->w_height)
1482 {
1483 if (down && wp->w_topline > 1)
1484 {
1485 --wp->w_topline;
1486 wp->w_topfill = 0;
1487 }
1488 else
1489 {
1490 wp->w_topfill = wp->w_height - n;
1491 if (wp->w_topfill < 0)
1492 wp->w_topfill = 0;
1493 }
1494 }
1495 }
1496}
1497
1498/*
1499 * Use as many filler lines as possible for w_topline. Make sure w_topline
1500 * is still visible.
1501 */
1502 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001503max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 int n;
1506
1507 n = plines_nofill(curwin->w_topline);
1508 if (n >= curwin->w_height)
1509 curwin->w_topfill = 0;
1510 else
1511 {
1512 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1513 if (curwin->w_topfill + n > curwin->w_height)
1514 curwin->w_topfill = curwin->w_height - n;
1515 }
1516}
1517#endif
1518
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519/*
1520 * Scroll the screen one line down, but don't do it if it would move the
1521 * cursor off the screen.
1522 */
1523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001524scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 int end_row;
1527#ifdef FEAT_DIFF
1528 int can_fill = (curwin->w_topfill
1529 < diff_check_fill(curwin, curwin->w_topline));
1530#endif
1531
1532 if (curwin->w_topline <= 1
1533#ifdef FEAT_DIFF
1534 && !can_fill
1535#endif
1536 )
1537 return;
1538
Bram Moolenaar85a20022019-12-21 18:25:54 +01001539 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 /*
1542 * Compute the row number of the last row of the cursor line
1543 * and make sure it doesn't go off the screen. Make sure the cursor
1544 * doesn't go past 'scrolloff' lines from the screen end.
1545 */
1546 end_row = curwin->w_wrow;
1547#ifdef FEAT_DIFF
1548 if (can_fill)
1549 ++end_row;
1550 else
1551 end_row += plines_nofill(curwin->w_topline - 1);
1552#else
1553 end_row += plines(curwin->w_topline - 1);
1554#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001555 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557 validate_cheight();
1558 validate_virtcol();
1559 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001560 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001562 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
1564#ifdef FEAT_DIFF
1565 if (can_fill)
1566 {
1567 ++curwin->w_topfill;
1568 check_topfill(curwin, TRUE);
1569 }
1570 else
1571 {
1572 --curwin->w_topline;
1573 curwin->w_topfill = 0;
1574 }
1575#else
1576 --curwin->w_topline;
1577#endif
1578#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001579 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001581 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1583 }
1584}
1585
1586/*
1587 * Scroll the screen one line up, but don't do it if it would move the cursor
1588 * off the screen.
1589 */
1590 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001591scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
1593 int start_row;
1594
1595 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1596#ifdef FEAT_DIFF
1597 && curwin->w_topfill == 0
1598#endif
1599 )
1600 return;
1601
Bram Moolenaar85a20022019-12-21 18:25:54 +01001602 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604 /*
1605 * Compute the row number of the first row of the cursor line
1606 * and make sure it doesn't go off the screen. Make sure the cursor
1607 * doesn't go before 'scrolloff' lines from the screen start.
1608 */
1609#ifdef FEAT_DIFF
1610 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1611 - curwin->w_topfill;
1612#else
1613 start_row = curwin->w_wrow - plines(curwin->w_topline);
1614#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001615 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001618 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001620 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622#ifdef FEAT_DIFF
1623 if (curwin->w_topfill > 0)
1624 --curwin->w_topfill;
1625 else
1626#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001627 {
1628#ifdef FEAT_FOLDING
1629 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001632 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001633 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1635 }
1636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638/*
1639 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1640 * a (wrapped) text line. Uses and sets "lp->fill".
1641 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001642 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 */
1644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001645topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647#ifdef FEAT_DIFF
1648 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1649 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001650 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 ++lp->fill;
1652 lp->height = 1;
1653 }
1654 else
1655#endif
1656 {
1657 --lp->lnum;
1658#ifdef FEAT_DIFF
1659 lp->fill = 0;
1660#endif
1661 if (lp->lnum < 1)
1662 lp->height = MAXCOL;
1663 else
1664#ifdef FEAT_FOLDING
1665 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001666 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 lp->height = 1;
1668 else
1669#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001670 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672}
1673
1674/*
1675 * Add one line below "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".
1678 * Lines below the last one are incredibly high.
1679 */
1680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681botline_forw(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 + 1))
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 > curbuf->b_ml.ml_line_count)
1698 lp->height = MAXCOL;
1699 else
1700#ifdef FEAT_FOLDING
1701 if (hasFolding(lp->lnum, NULL, &lp->lnum))
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#ifdef FEAT_DIFF
1711/*
1712 * Switch from including filler lines below lp->lnum to including filler
1713 * lines above loff.lnum + 1. This keeps pointing to the same line.
1714 * When there are no filler lines nothing changes.
1715 */
1716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001717botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 if (lp->fill > 0)
1720 {
1721 ++lp->lnum;
1722 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1723 }
1724}
1725
1726/*
1727 * Switch from including filler lines above lp->lnum to including filler
1728 * lines below loff.lnum - 1. This keeps pointing to the same line.
1729 * When there are no filler lines nothing changes.
1730 */
1731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001732topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 if (lp->fill > 0)
1735 {
1736 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1737 --lp->lnum;
1738 }
1739}
1740#endif
1741
1742/*
1743 * Recompute topline to put the cursor at the top of the window.
1744 * Scroll at least "min_scroll" lines.
1745 * If "always" is TRUE, always set topline (for "zt").
1746 */
1747 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001748scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 int scrolled = 0;
1751 int extra = 0;
1752 int used;
1753 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001754 linenr_T top; // just above displayed lines
1755 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 linenr_T old_topline = curwin->w_topline;
1757#ifdef FEAT_DIFF
1758 linenr_T old_topfill = curwin->w_topfill;
1759#endif
1760 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001761 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (mouse_dragging > 0)
1764 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 /*
1767 * Decrease topline until:
1768 * - it has become 1
1769 * - (part of) the cursor line is moved off the screen or
1770 * - moved at least 'scrolljump' lines and
1771 * - at least 'scrolloff' lines above and below the cursor
1772 */
1773 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001774 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (curwin->w_cursor.lnum < curwin->w_topline)
1776 scrolled = used;
1777
1778#ifdef FEAT_FOLDING
1779 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1780 {
1781 --top;
1782 ++bot;
1783 }
1784 else
1785#endif
1786 {
1787 top = curwin->w_cursor.lnum - 1;
1788 bot = curwin->w_cursor.lnum + 1;
1789 }
1790 new_topline = top + 1;
1791
1792#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001793 // "used" already contains the number of filler lines above, don't add it
1794 // again.
1795 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001796 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797#endif
1798
1799 /*
1800 * Check if the lines from "top" to "bot" fit in the window. If they do,
1801 * set new_topline and advance "top" and "bot" to include more lines.
1802 */
1803 while (top > 0)
1804 {
1805#ifdef FEAT_FOLDING
1806 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001807 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 i = 1;
1809 else
1810#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001811 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 used += i;
1813 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1814 {
1815#ifdef FEAT_FOLDING
1816 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001817 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 ++used;
1819 else
1820#endif
1821 used += plines(bot);
1822 }
1823 if (used > curwin->w_height)
1824 break;
1825 if (top < curwin->w_topline)
1826 scrolled += i;
1827
1828 /*
1829 * If scrolling is needed, scroll at least 'sj' lines.
1830 */
1831 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1832 && extra >= off)
1833 break;
1834
1835 extra += i;
1836 new_topline = top;
1837 --top;
1838 ++bot;
1839 }
1840
1841 /*
1842 * If we don't have enough space, put cursor in the middle.
1843 * This makes sure we get the same position when using "k" and "j"
1844 * in a small window.
1845 */
1846 if (used > curwin->w_height)
1847 scroll_cursor_halfway(FALSE);
1848 else
1849 {
1850 /*
1851 * If "always" is FALSE, only adjust topline to a lower value, higher
1852 * value may happen with wrapping lines
1853 */
1854 if (new_topline < curwin->w_topline || always)
1855 curwin->w_topline = new_topline;
1856 if (curwin->w_topline > curwin->w_cursor.lnum)
1857 curwin->w_topline = curwin->w_cursor.lnum;
1858#ifdef FEAT_DIFF
1859 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1860 if (curwin->w_topfill > 0 && extra > off)
1861 {
1862 curwin->w_topfill -= extra - off;
1863 if (curwin->w_topfill < 0)
1864 curwin->w_topfill = 0;
1865 }
1866 check_topfill(curwin, FALSE);
1867#endif
1868 if (curwin->w_topline != old_topline
1869#ifdef FEAT_DIFF
1870 || curwin->w_topfill != old_topfill
1871#endif
1872 )
1873 curwin->w_valid &=
1874 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1875 curwin->w_valid |= VALID_TOPLINE;
1876 }
1877}
1878
1879/*
1880 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1881 * screen lines for text lines.
1882 */
1883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001884set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885{
1886#ifdef FEAT_DIFF
1887 wp->w_filler_rows = 0;
1888#endif
1889 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001890 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 else
1892 {
1893 wp->w_empty_rows = wp->w_height - used;
1894#ifdef FEAT_DIFF
1895 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1896 {
1897 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1898 if (wp->w_empty_rows > wp->w_filler_rows)
1899 wp->w_empty_rows -= wp->w_filler_rows;
1900 else
1901 {
1902 wp->w_filler_rows = wp->w_empty_rows;
1903 wp->w_empty_rows = 0;
1904 }
1905 }
1906#endif
1907 }
1908}
1909
1910/*
1911 * Recompute topline to put the cursor at the bottom of the window.
1912 * Scroll at least "min_scroll" lines.
1913 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1914 * This is messy stuff!!!
1915 */
1916 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001917scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918{
1919 int used;
1920 int scrolled = 0;
1921 int extra = 0;
1922 int i;
1923 linenr_T line_count;
1924 linenr_T old_topline = curwin->w_topline;
1925 lineoff_T loff;
1926 lineoff_T boff;
1927#ifdef FEAT_DIFF
1928 int old_topfill = curwin->w_topfill;
1929 int fill_below_window;
1930#endif
1931 linenr_T old_botline = curwin->w_botline;
1932 linenr_T old_valid = curwin->w_valid;
1933 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001934 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001935 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
1937 cln = curwin->w_cursor.lnum;
1938 if (set_topbot)
1939 {
1940 used = 0;
1941 curwin->w_botline = cln + 1;
1942#ifdef FEAT_DIFF
1943 loff.fill = 0;
1944#endif
1945 for (curwin->w_topline = curwin->w_botline;
1946 curwin->w_topline > 1;
1947 curwin->w_topline = loff.lnum)
1948 {
1949 loff.lnum = curwin->w_topline;
1950 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001951 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 break;
1953 used += loff.height;
1954#ifdef FEAT_DIFF
1955 curwin->w_topfill = loff.fill;
1956#endif
1957 }
1958 set_empty_rows(curwin, used);
1959 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
1960 if (curwin->w_topline != old_topline
1961#ifdef FEAT_DIFF
1962 || curwin->w_topfill != old_topfill
1963#endif
1964 )
1965 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
1966 }
1967 else
1968 validate_botline();
1969
Bram Moolenaar85a20022019-12-21 18:25:54 +01001970 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971#ifdef FEAT_DIFF
1972 used = plines_nofill(cln);
1973#else
1974 validate_cheight();
1975 used = curwin->w_cline_height;
1976#endif
1977
Bram Moolenaar85a20022019-12-21 18:25:54 +01001978 // If the cursor is below botline, we will at least scroll by the height
1979 // of the cursor line. Correct for empty lines, which are really part of
1980 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (cln >= curwin->w_botline)
1982 {
1983 scrolled = used;
1984 if (cln == curwin->w_botline)
1985 scrolled -= curwin->w_empty_rows;
1986 }
1987
1988 /*
1989 * Stop counting lines to scroll when
1990 * - hitting start of the file
1991 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01001992 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 * - lines between botline and cursor have been counted
1994 */
1995#ifdef FEAT_FOLDING
1996 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
1997#endif
1998 {
1999 loff.lnum = cln;
2000 boff.lnum = cln;
2001 }
2002#ifdef FEAT_DIFF
2003 loff.fill = 0;
2004 boff.fill = 0;
2005 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2006 - curwin->w_filler_rows;
2007#endif
2008
2009 while (loff.lnum > 1)
2010 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002011 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2012 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002014 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2016 && loff.lnum <= curwin->w_botline
2017#ifdef FEAT_DIFF
2018 && (loff.lnum < curwin->w_botline
2019 || loff.fill >= fill_below_window)
2020#endif
2021 )
2022 break;
2023
Bram Moolenaar85a20022019-12-21 18:25:54 +01002024 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002026 if (loff.height == MAXCOL)
2027 used = MAXCOL;
2028 else
2029 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (used > curwin->w_height)
2031 break;
2032 if (loff.lnum >= curwin->w_botline
2033#ifdef FEAT_DIFF
2034 && (loff.lnum > curwin->w_botline
2035 || loff.fill <= fill_below_window)
2036#endif
2037 )
2038 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002039 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 scrolled += loff.height;
2041 if (loff.lnum == curwin->w_botline
2042#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002043 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044#endif
2045 )
2046 scrolled -= curwin->w_empty_rows;
2047 }
2048
2049 if (boff.lnum < curbuf->b_ml.ml_line_count)
2050 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002051 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 botline_forw(&boff);
2053 used += boff.height;
2054 if (used > curwin->w_height)
2055 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002056 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2057 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
2059 extra += boff.height;
2060 if (boff.lnum >= curwin->w_botline
2061#ifdef FEAT_DIFF
2062 || (boff.lnum + 1 == curwin->w_botline
2063 && boff.fill > curwin->w_filler_rows)
2064#endif
2065 )
2066 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002067 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 scrolled += boff.height;
2069 if (boff.lnum == curwin->w_botline
2070#ifdef FEAT_DIFF
2071 && boff.fill == 0
2072#endif
2073 )
2074 scrolled -= curwin->w_empty_rows;
2075 }
2076 }
2077 }
2078 }
2079
Bram Moolenaar85a20022019-12-21 18:25:54 +01002080 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (scrolled <= 0)
2082 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002083 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 else if (used > curwin->w_height)
2085 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002086 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 else
2088 {
2089 line_count = 0;
2090#ifdef FEAT_DIFF
2091 boff.fill = curwin->w_topfill;
2092#endif
2093 boff.lnum = curwin->w_topline - 1;
2094 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2095 {
2096 botline_forw(&boff);
2097 i += boff.height;
2098 ++line_count;
2099 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002100 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 line_count = 9999;
2102 }
2103
2104 /*
2105 * Scroll up if the cursor is off the bottom of the screen a bit.
2106 * Otherwise put it at 1/2 of the screen.
2107 */
2108 if (line_count >= curwin->w_height && line_count > min_scroll)
2109 scroll_cursor_halfway(FALSE);
2110 else
2111 scrollup(line_count, TRUE);
2112
2113 /*
2114 * If topline didn't change we need to restore w_botline and w_empty_rows
2115 * (we changed them).
2116 * If topline did change, update_screen() will set botline.
2117 */
2118 if (curwin->w_topline == old_topline && set_topbot)
2119 {
2120 curwin->w_botline = old_botline;
2121 curwin->w_empty_rows = old_empty_rows;
2122 curwin->w_valid = old_valid;
2123 }
2124 curwin->w_valid |= VALID_TOPLINE;
2125}
2126
2127/*
2128 * Recompute topline to put the cursor halfway the window
2129 * If "atend" is TRUE, also put it halfway at the end of the file.
2130 */
2131 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002132scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
2134 int above = 0;
2135 linenr_T topline;
2136#ifdef FEAT_DIFF
2137 int topfill = 0;
2138#endif
2139 int below = 0;
2140 int used;
2141 lineoff_T loff;
2142 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002143#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002144 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002147#ifdef FEAT_PROP_POPUP
2148 // if the width changed this needs to be updated first
2149 may_update_popup_position();
2150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2152#ifdef FEAT_FOLDING
2153 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2154#endif
2155#ifdef FEAT_DIFF
2156 used = plines_nofill(loff.lnum);
2157 loff.fill = 0;
2158 boff.fill = 0;
2159#else
2160 used = plines(loff.lnum);
2161#endif
2162 topline = loff.lnum;
2163 while (topline > 1)
2164 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002165 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 if (boff.lnum < curbuf->b_ml.ml_line_count)
2168 {
2169 botline_forw(&boff);
2170 used += boff.height;
2171 if (used > curwin->w_height)
2172 break;
2173 below += boff.height;
2174 }
2175 else
2176 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002177 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (atend)
2179 ++used;
2180 }
2181 }
2182
Bram Moolenaar85a20022019-12-21 18:25:54 +01002183 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
2185 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002186 if (loff.height == MAXCOL)
2187 used = MAXCOL;
2188 else
2189 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (used > curwin->w_height)
2191 break;
2192 above += loff.height;
2193 topline = loff.lnum;
2194#ifdef FEAT_DIFF
2195 topfill = loff.fill;
2196#endif
2197 }
2198 }
2199#ifdef FEAT_FOLDING
2200 if (!hasFolding(topline, &curwin->w_topline, NULL))
2201#endif
2202 curwin->w_topline = topline;
2203#ifdef FEAT_DIFF
2204 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002205 if (old_topline > curwin->w_topline + curwin->w_height)
2206 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 check_topfill(curwin, FALSE);
2208#endif
2209 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2210 curwin->w_valid |= VALID_TOPLINE;
2211}
2212
2213/*
2214 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002215 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 * If not possible, put it at the same position as scroll_cursor_halfway().
2217 * When called topline must be valid!
2218 */
2219 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002220cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002222 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 linenr_T botline;
2226 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002229 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
2231 /*
2232 * How many lines we would like to have above/below the cursor depends on
2233 * whether the first/last line of the file is on screen.
2234 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002235 above_wanted = so;
2236 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002237 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 above_wanted = mouse_dragging - 1;
2240 below_wanted = mouse_dragging - 1;
2241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (curwin->w_topline == 1)
2243 {
2244 above_wanted = 0;
2245 max_off = curwin->w_height / 2;
2246 if (below_wanted > max_off)
2247 below_wanted = max_off;
2248 }
2249 validate_botline();
2250 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002251 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
2253 below_wanted = 0;
2254 max_off = (curwin->w_height - 1) / 2;
2255 if (above_wanted > max_off)
2256 above_wanted = max_off;
2257 }
2258
2259 /*
2260 * If there are sufficient file-lines above and below the cursor, we can
2261 * return now.
2262 */
2263 cln = curwin->w_cursor.lnum;
2264 if (cln >= curwin->w_topline + above_wanted
2265 && cln < curwin->w_botline - below_wanted
2266#ifdef FEAT_FOLDING
2267 && !hasAnyFolding(curwin)
2268#endif
2269 )
2270 return;
2271
2272 /*
2273 * Narrow down the area where the cursor can be put by taking lines from
2274 * the top and the bottom until:
2275 * - the desired context lines are found
2276 * - the lines from the top is past the lines from the bottom
2277 */
2278 topline = curwin->w_topline;
2279 botline = curwin->w_botline - 1;
2280#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002281 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 above = curwin->w_topfill;
2283 below = curwin->w_filler_rows;
2284#endif
2285 while ((above < above_wanted || below < below_wanted) && topline < botline)
2286 {
2287 if (below < below_wanted && (below <= above || above >= above_wanted))
2288 {
2289#ifdef FEAT_FOLDING
2290 if (hasFolding(botline, &botline, NULL))
2291 ++below;
2292 else
2293#endif
2294 below += plines(botline);
2295 --botline;
2296 }
2297 if (above < above_wanted && (above < below || below >= below_wanted))
2298 {
2299#ifdef FEAT_FOLDING
2300 if (hasFolding(topline, NULL, &topline))
2301 ++above;
2302 else
2303#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002304 above += PLINES_NOFILL(topline);
2305#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002306 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (topline < botline)
2308 above += diff_check_fill(curwin, topline + 1);
2309#endif
2310 ++topline;
2311 }
2312 }
2313 if (topline == botline || botline == 0)
2314 curwin->w_cursor.lnum = topline;
2315 else if (topline > botline)
2316 curwin->w_cursor.lnum = botline;
2317 else
2318 {
2319 if (cln < topline && curwin->w_topline > 1)
2320 {
2321 curwin->w_cursor.lnum = topline;
2322 curwin->w_valid &=
2323 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2324 }
2325 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2326 {
2327 curwin->w_cursor.lnum = botline;
2328 curwin->w_valid &=
2329 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2330 }
2331 }
2332 curwin->w_valid |= VALID_TOPLINE;
2333}
2334
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002335static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337/*
2338 * move screen 'count' pages up or down and update screen
2339 *
2340 * return FAIL for failure, OK otherwise
2341 */
2342 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002343onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344{
2345 long n;
2346 int retval = OK;
2347 lineoff_T loff;
2348 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002349 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
Bram Moolenaar85a20022019-12-21 18:25:54 +01002351 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 beep_flush();
2354 return FAIL;
2355 }
2356
2357 for ( ; count > 0; --count)
2358 {
2359 validate_botline();
2360 /*
2361 * It's an error to move a page up when the first line is already on
2362 * the screen. It's an error to move a page down when the last line
2363 * is on the screen and the topline is 'scrolloff' lines from the
2364 * last line.
2365 */
2366 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002367 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2369 : (curwin->w_topline == 1
2370#ifdef FEAT_DIFF
2371 && curwin->w_topfill ==
2372 diff_check_fill(curwin, curwin->w_topline)
2373#endif
2374 ))
2375 {
2376 beep_flush();
2377 retval = FAIL;
2378 break;
2379 }
2380
2381#ifdef FEAT_DIFF
2382 loff.fill = 0;
2383#endif
2384 if (dir == FORWARD)
2385 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002386 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002388 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002389 if (p_window <= 2)
2390 ++curwin->w_topline;
2391 else
2392 curwin->w_topline += p_window - 2;
2393 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2394 curwin->w_topline = curbuf->b_ml.ml_line_count;
2395 curwin->w_cursor.lnum = curwin->w_topline;
2396 }
2397 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2398 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002399 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 curwin->w_topline = curbuf->b_ml.ml_line_count;
2401#ifdef FEAT_DIFF
2402 curwin->w_topfill = 0;
2403#endif
2404 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2405 }
2406 else
2407 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002408 // For the overlap, start with the line just below the window
2409 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 loff.lnum = curwin->w_botline;
2411#ifdef FEAT_DIFF
2412 loff.fill = diff_check_fill(curwin, loff.lnum)
2413 - curwin->w_filler_rows;
2414#endif
2415 get_scroll_overlap(&loff, -1);
2416 curwin->w_topline = loff.lnum;
2417#ifdef FEAT_DIFF
2418 curwin->w_topfill = loff.fill;
2419 check_topfill(curwin, FALSE);
2420#endif
2421 curwin->w_cursor.lnum = curwin->w_topline;
2422 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2423 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2424 }
2425 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002426 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428#ifdef FEAT_DIFF
2429 if (curwin->w_topline == 1)
2430 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 max_topfill();
2433 continue;
2434 }
2435#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002436 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002437 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002438 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002439 if (p_window <= 2)
2440 --curwin->w_topline;
2441 else
2442 curwin->w_topline -= p_window - 2;
2443 if (curwin->w_topline < 1)
2444 curwin->w_topline = 1;
2445 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2446 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2447 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2448 continue;
2449 }
2450
Bram Moolenaar85a20022019-12-21 18:25:54 +01002451 // Find the line at the top of the window that is going to be the
2452 // line at the bottom of the window. Make sure this results in
2453 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 loff.lnum = curwin->w_topline - 1;
2455#ifdef FEAT_DIFF
2456 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2457 - curwin->w_topfill;
2458#endif
2459 get_scroll_overlap(&loff, 1);
2460
2461 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2462 {
2463 loff.lnum = curbuf->b_ml.ml_line_count;
2464#ifdef FEAT_DIFF
2465 loff.fill = 0;
2466 }
2467 else
2468 {
2469 botline_topline(&loff);
2470#endif
2471 }
2472 curwin->w_cursor.lnum = loff.lnum;
2473
Bram Moolenaar85a20022019-12-21 18:25:54 +01002474 // Find the line just above the new topline to get the right line
2475 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 n = 0;
2477 while (n <= curwin->w_height && loff.lnum >= 1)
2478 {
2479 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002480 if (loff.height == MAXCOL)
2481 n = MAXCOL;
2482 else
2483 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002485 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 curwin->w_topline = 1;
2488#ifdef FEAT_DIFF
2489 max_topfill();
2490#endif
2491 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2492 }
2493 else
2494 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002495 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496#ifdef FEAT_DIFF
2497 topline_botline(&loff);
2498#endif
2499 botline_forw(&loff);
2500 botline_forw(&loff);
2501#ifdef FEAT_DIFF
2502 botline_topline(&loff);
2503#endif
2504#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002505 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2507#endif
2508
Bram Moolenaar85a20022019-12-21 18:25:54 +01002509 // Always scroll at least one line. Avoid getting stuck on
2510 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 if (loff.lnum >= curwin->w_topline
2512#ifdef FEAT_DIFF
2513 && (loff.lnum > curwin->w_topline
2514 || loff.fill >= curwin->w_topfill)
2515#endif
2516 )
2517 {
2518#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002519 // First try using the maximum number of filler lines. If
2520 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 loff.fill = curwin->w_topfill;
2522 if (curwin->w_topfill < diff_check_fill(curwin,
2523 curwin->w_topline))
2524 max_topfill();
2525 if (curwin->w_topfill == loff.fill)
2526#endif
2527 {
2528 --curwin->w_topline;
2529#ifdef FEAT_DIFF
2530 curwin->w_topfill = 0;
2531#endif
2532 }
2533 comp_botline(curwin);
2534 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002535 curwin->w_valid &=
2536 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
2538 else
2539 {
2540 curwin->w_topline = loff.lnum;
2541#ifdef FEAT_DIFF
2542 curwin->w_topfill = loff.fill;
2543 check_topfill(curwin, FALSE);
2544#endif
2545 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2546 }
2547 }
2548 }
2549 }
2550#ifdef FEAT_FOLDING
2551 foldAdjustCursor();
2552#endif
2553 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002554 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002555 if (retval == OK)
2556 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2558
Bram Moolenaar907dad72018-07-10 15:07:15 +02002559 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002561 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2562 // But make sure we scroll at least one line (happens with mix of long
2563 // wrapping lines and non-wrapping line).
2564 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002566 scroll_cursor_top(1, FALSE);
2567 if (curwin->w_topline <= old_topline
2568 && old_topline < curbuf->b_ml.ml_line_count)
2569 {
2570 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002572 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2573#endif
2574 }
2575 }
2576#ifdef FEAT_FOLDING
2577 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581
2582 redraw_later(VALID);
2583 return retval;
2584}
2585
2586/*
2587 * Decide how much overlap to use for page-up or page-down scrolling.
2588 * This is symmetric, so that doing both keeps the same lines displayed.
2589 * Three lines are examined:
2590 *
2591 * before CTRL-F after CTRL-F / before CTRL-B
2592 * etc. l1
2593 * l1 last but one line ------------
2594 * l2 last text line l2 top text line
2595 * ------------- l3 second text line
2596 * l3 etc.
2597 */
2598 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002599get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600{
2601 int h1, h2, h3, h4;
2602 int min_height = curwin->w_height - 2;
2603 lineoff_T loff0, loff1, loff2;
2604
2605#ifdef FEAT_DIFF
2606 if (lp->fill > 0)
2607 lp->height = 1;
2608 else
2609 lp->height = plines_nofill(lp->lnum);
2610#else
2611 lp->height = plines(lp->lnum);
2612#endif
2613 h1 = lp->height;
2614 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002615 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 loff0 = *lp;
2618 if (dir > 0)
2619 botline_forw(lp);
2620 else
2621 topline_back(lp);
2622 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002623 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002625 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 return;
2627 }
2628
2629 loff1 = *lp;
2630 if (dir > 0)
2631 botline_forw(lp);
2632 else
2633 topline_back(lp);
2634 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002635 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002637 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 return;
2639 }
2640
2641 loff2 = *lp;
2642 if (dir > 0)
2643 botline_forw(lp);
2644 else
2645 topline_back(lp);
2646 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002647 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002648 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002650 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 return;
2652}
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654/*
2655 * Scroll 'scroll' lines up or down.
2656 */
2657 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002658halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
2660 long scrolled = 0;
2661 int i;
2662 int n;
2663 int room;
2664
2665 if (Prenum)
2666 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2667 curwin->w_height : Prenum;
2668 n = (curwin->w_p_scr <= curwin->w_height) ?
2669 curwin->w_p_scr : curwin->w_height;
2670
Bram Moolenaard5d37532017-03-27 23:02:07 +02002671 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 validate_botline();
2673 room = curwin->w_empty_rows;
2674#ifdef FEAT_DIFF
2675 room += curwin->w_filler_rows;
2676#endif
2677 if (flag)
2678 {
2679 /*
2680 * scroll the text up
2681 */
2682 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2683 {
2684#ifdef FEAT_DIFF
2685 if (curwin->w_topfill > 0)
2686 {
2687 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002688 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 --curwin->w_topfill;
2690 }
2691 else
2692#endif
2693 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002694 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 n -= i;
2696 if (n < 0 && scrolled > 0)
2697 break;
2698#ifdef FEAT_FOLDING
2699 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2700#endif
2701 ++curwin->w_topline;
2702#ifdef FEAT_DIFF
2703 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2704#endif
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2707 {
2708 ++curwin->w_cursor.lnum;
2709 curwin->w_valid &=
2710 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2714 scrolled += i;
2715
2716 /*
2717 * Correct w_botline for changed w_topline.
2718 * Won't work when there are filler lines.
2719 */
2720#ifdef FEAT_DIFF
2721 if (curwin->w_p_diff)
2722 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2723 else
2724#endif
2725 {
2726 room += i;
2727 do
2728 {
2729 i = plines(curwin->w_botline);
2730 if (i > room)
2731 break;
2732#ifdef FEAT_FOLDING
2733 (void)hasFolding(curwin->w_botline, NULL,
2734 &curwin->w_botline);
2735#endif
2736 ++curwin->w_botline;
2737 room -= i;
2738 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2739 }
2740 }
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /*
2743 * When hit bottom of the file: move cursor down.
2744 */
2745 if (n > 0)
2746 {
2747# ifdef FEAT_FOLDING
2748 if (hasAnyFolding(curwin))
2749 {
2750 while (--n >= 0
2751 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2752 {
2753 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2754 &curwin->w_cursor.lnum);
2755 ++curwin->w_cursor.lnum;
2756 }
2757 }
2758 else
2759# endif
2760 curwin->w_cursor.lnum += n;
2761 check_cursor_lnum();
2762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 else
2765 {
2766 /*
2767 * scroll the text down
2768 */
2769 while (n > 0 && curwin->w_topline > 1)
2770 {
2771#ifdef FEAT_DIFF
2772 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2773 {
2774 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002775 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 ++curwin->w_topfill;
2777 }
2778 else
2779#endif
2780 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002781 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 n -= i;
2783 if (n < 0 && scrolled > 0)
2784 break;
2785 --curwin->w_topline;
2786#ifdef FEAT_FOLDING
2787 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2788#endif
2789#ifdef FEAT_DIFF
2790 curwin->w_topfill = 0;
2791#endif
2792 }
2793 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2794 VALID_BOTLINE|VALID_BOTLINE_AP);
2795 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (curwin->w_cursor.lnum > 1)
2797 {
2798 --curwin->w_cursor.lnum;
2799 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 /*
2804 * When hit top of the file: move cursor up.
2805 */
2806 if (n > 0)
2807 {
2808 if (curwin->w_cursor.lnum <= (linenr_T)n)
2809 curwin->w_cursor.lnum = 1;
2810 else
2811# ifdef FEAT_FOLDING
2812 if (hasAnyFolding(curwin))
2813 {
2814 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2815 {
2816 --curwin->w_cursor.lnum;
2817 (void)hasFolding(curwin->w_cursor.lnum,
2818 &curwin->w_cursor.lnum, NULL);
2819 }
2820 }
2821 else
2822# endif
2823 curwin->w_cursor.lnum -= n;
2824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002827 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 foldAdjustCursor();
2829# endif
2830#ifdef FEAT_DIFF
2831 check_topfill(curwin, !flag);
2832#endif
2833 cursor_correct();
2834 beginline(BL_SOL | BL_FIX);
2835 redraw_later(VALID);
2836}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002837
Bram Moolenaar860cae12010-06-05 23:22:07 +02002838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002839do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002840{
2841 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002842 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002843 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002844 colnr_T curswant = curwin->w_curswant;
2845 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002846 win_T *old_curwin = curwin;
2847 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002848 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002849 int old_VIsual_select = VIsual_select;
2850 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002851
2852 /*
2853 * loop through the cursorbound windows
2854 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002855 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002856 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002857 {
2858 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002859 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002860 if (curwin != old_curwin && curwin->w_p_crb)
2861 {
2862# ifdef FEAT_DIFF
2863 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002864 curwin->w_cursor.lnum =
2865 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002866 else
2867# endif
2868 curwin->w_cursor.lnum = line;
2869 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002870 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002871 curwin->w_curswant = curswant;
2872 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002873
Bram Moolenaar85a20022019-12-21 18:25:54 +01002874 // Make sure the cursor is in a valid position. Temporarily set
2875 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002876 restart_edit_save = restart_edit;
2877 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002878 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002879# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002880 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002881 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002882# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002883 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002884 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002885 if (has_mbyte)
2886 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002887 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002888
Bram Moolenaar85a20022019-12-21 18:25:54 +01002889 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002890 if (!curwin->w_p_scb)
2891 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002892 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002893 }
2894 }
2895
2896 /*
2897 * reset current-window
2898 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002899 VIsual_select = old_VIsual_select;
2900 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002901 curwin = old_curwin;
2902 curbuf = old_curbuf;
2903}