blob: 1c88e6ebcc5959a55707b44ad27fd34e147b1758 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
39 * Compute wp->w_botline for the current wp->w_topline. Can be called after
40 * wp->w_topline changed.
41 */
42 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010043comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044{
45 int n;
46 linenr_T lnum;
47 int done;
48#ifdef FEAT_FOLDING
49 linenr_T last;
50 int folded;
51#endif
52
53 /*
54 * If w_cline_row is valid, start there.
55 * Otherwise have to start at w_topline.
56 */
57 check_cursor_moved(wp);
58 if (wp->w_valid & VALID_CROW)
59 {
60 lnum = wp->w_cursor.lnum;
61 done = wp->w_cline_row;
62 }
63 else
64 {
65 lnum = wp->w_topline;
66 done = 0;
67 }
68
69 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
70 {
71#ifdef FEAT_FOLDING
72 last = lnum;
73 folded = FALSE;
74 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
75 {
76 n = 1;
77 folded = TRUE;
78 }
79 else
80#endif
81#ifdef FEAT_DIFF
82 if (lnum == wp->w_topline)
83 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
84 else
85#endif
86 n = plines_win(wp, lnum, TRUE);
87 if (
88#ifdef FEAT_FOLDING
89 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
90#else
91 lnum == wp->w_cursor.lnum
92#endif
93 )
94 {
95 wp->w_cline_row = done;
96 wp->w_cline_height = n;
97#ifdef FEAT_FOLDING
98 wp->w_cline_folded = folded;
99#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100100 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
102 }
103 if (done + n > wp->w_height)
104 break;
105 done += n;
106#ifdef FEAT_FOLDING
107 lnum = last;
108#endif
109 }
110
Bram Moolenaar85a20022019-12-21 18:25:54 +0100111 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 wp->w_botline = lnum;
113 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
114
115 set_empty_rows(wp, done);
116}
117
Bram Moolenaar90a99792018-09-12 21:52:18 +0200118#ifdef FEAT_SYN_HL
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200119 void
120reset_cursorline(void)
121{
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200122 curwin->w_last_cursorline = 0;
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +0200123}
Bram Moolenaar90a99792018-09-12 21:52:18 +0200124#endif
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100127 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
128 * set.
129 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100131redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100132{
133 if ((wp->w_p_rnu
134#ifdef FEAT_SYN_HL
135 || wp->w_p_cul
136#endif
137 )
138 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200139 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200140 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200141 if (wp->w_p_rnu)
142 // win_line() will redraw the number column only.
Bram Moolenaar90a99792018-09-12 21:52:18 +0200143 redraw_win_later(wp, VALID);
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200144#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200145 if (wp->w_p_cul)
146 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200147 if (wp->w_redr_type <= VALID && wp->w_last_cursorline != 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200148 {
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200149 // "w_last_cursorline" may be outdated, worst case we redraw
150 // too much. This is optimized for moving the cursor around in
151 // the current window.
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100152 redrawWinline(wp, wp->w_last_cursorline);
153 redrawWinline(wp, wp->w_cursor.lnum);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200154 }
155 else
156 redraw_win_later(wp, SOME_VALID);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200157 }
Bram Moolenaar1b7fefc2018-09-12 22:27:15 +0200158#endif
Bram Moolenaar90a99792018-09-12 21:52:18 +0200159 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100160}
161
162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 * Update curwin->w_topline and redraw if necessary.
164 * Used to update the screen before printing a message.
165 */
166 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100167update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 update_topline();
170 if (must_redraw)
171 update_screen(0);
172}
173
174/*
175 * Update curwin->w_topline to move the cursor onto the screen.
176 */
177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100178update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
180 long line_count;
181 int halfheight;
182 int n;
183 linenr_T old_topline;
184#ifdef FEAT_DIFF
185 int old_topfill;
186#endif
187#ifdef FEAT_FOLDING
188 linenr_T lnum;
189#endif
190 int check_topline = FALSE;
191 int check_botline = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100192 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100193 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaar85a20022019-12-21 18:25:54 +0100195 // If there is no valid screen and when the window height is zero just use
196 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200197 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200198 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100199 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200200 curwin->w_topline = curwin->w_cursor.lnum;
201 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200202 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200203 return;
204 }
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 check_cursor_moved(curwin);
207 if (curwin->w_valid & VALID_TOPLINE)
208 return;
209
Bram Moolenaar85a20022019-12-21 18:25:54 +0100210 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000211 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100212 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214 old_topline = curwin->w_topline;
215#ifdef FEAT_DIFF
216 old_topfill = curwin->w_topfill;
217#endif
218
219 /*
220 * If the buffer is empty, always set topline to 1.
221 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 {
224 if (curwin->w_topline != 1)
225 redraw_later(NOT_VALID);
226 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 curwin->w_botline = 2;
228 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
231
232 /*
233 * If the cursor is above or near the top of the window, scroll the window
234 * to show the line the cursor is in, with 'scrolloff' context.
235 */
236 else
237 {
238 if (curwin->w_topline > 1)
239 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100240 // If the cursor is above topline, scrolling is always needed.
241 // If the cursor is far below topline and there is no folding,
242 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 if (curwin->w_cursor.lnum < curwin->w_topline)
244 check_topline = TRUE;
245 else if (check_top_offset())
246 check_topline = TRUE;
247 }
248#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100249 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
251 curwin->w_topline))
252 check_topline = TRUE;
253#endif
254
255 if (check_topline)
256 {
257 halfheight = curwin->w_height / 2 - 1;
258 if (halfheight < 2)
259 halfheight = 2;
260
261#ifdef FEAT_FOLDING
262 if (hasAnyFolding(curwin))
263 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100264 // Count the number of logical lines between the cursor and
265 // topline + scrolloff (approximation of how much will be
266 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 n = 0;
268 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100269 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 {
271 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100272 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
274 break;
275 (void)hasFolding(lnum, NULL, &lnum);
276 }
277 }
278 else
279#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100280 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281
Bram Moolenaar85a20022019-12-21 18:25:54 +0100282 // If we weren't very close to begin with, we scroll to put the
283 // cursor in the middle of the window. Otherwise put the cursor
284 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 if (n >= halfheight)
286 scroll_cursor_halfway(FALSE);
287 else
288 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000289 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 check_botline = TRUE;
291 }
292 }
293
294 else
295 {
296#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100297 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
299#endif
300 check_botline = TRUE;
301 }
302 }
303
304 /*
305 * If the cursor is below the bottom of the window, scroll the window
306 * to put the cursor on the window.
307 * When w_botline is invalid, recompute it first, to avoid a redraw later.
308 * If w_botline was approximated, we might need a redraw later in a few
309 * cases, but we don't want to spend (a lot of) time recomputing w_botline
310 * for every small change.
311 */
312 if (check_botline)
313 {
314 if (!(curwin->w_valid & VALID_BOTLINE_AP))
315 validate_botline();
316
317 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
318 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000319 if (curwin->w_cursor.lnum < curwin->w_botline)
320 {
321 if (((long)curwin->w_cursor.lnum
Bram Moolenaar375e3392019-01-31 18:26:10 +0100322 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_FOLDING
324 || hasAnyFolding(curwin)
325#endif
326 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 lineoff_T loff;
329
Bram Moolenaar85a20022019-12-21 18:25:54 +0100330 // Cursor is (a few lines) above botline, check if there are
331 // 'scrolloff' window lines below the cursor. If not, need to
332 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 n = curwin->w_empty_rows;
334 loff.lnum = curwin->w_cursor.lnum;
335#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100336 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
338#endif
339#ifdef FEAT_DIFF
340 loff.fill = 0;
341 n += curwin->w_filler_rows;
342#endif
343 loff.height = 0;
344 while (loff.lnum < curwin->w_botline
345#ifdef FEAT_DIFF
346 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
347#endif
348 )
349 {
350 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100351 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 break;
353 botline_forw(&loff);
354 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100355 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000358 }
359 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100360 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000361 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 }
363 if (check_botline)
364 {
365#ifdef FEAT_FOLDING
366 if (hasAnyFolding(curwin))
367 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100368 // Count the number of logical lines between the cursor and
369 // botline - scrolloff (approximation of how much will be
370 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 line_count = 0;
372 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
375 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100376 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 if (lnum <= 0 || line_count > curwin->w_height + 1)
378 break;
379 (void)hasFolding(lnum, &lnum, NULL);
380 }
381 }
382 else
383#endif
384 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar375e3392019-01-31 18:26:10 +0100385 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000387 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 else
389 scroll_cursor_halfway(FALSE);
390 }
391 }
392 }
393 curwin->w_valid |= VALID_TOPLINE;
394
395 /*
396 * Need to redraw when topline changed.
397 */
398 if (curwin->w_topline != old_topline
399#ifdef FEAT_DIFF
400 || curwin->w_topfill != old_topfill
401#endif
402 )
403 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100404 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000405 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {
407 curwin->w_skipcol = 0;
408 redraw_later(NOT_VALID);
409 }
410 else
411 redraw_later(VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100412 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (curwin->w_cursor.lnum == curwin->w_topline)
414 validate_cursor();
415 }
416
Bram Moolenaar375e3392019-01-31 18:26:10 +0100417 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418}
419
420/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000421 * Return the scrolljump value to use for the current window.
422 * When 'scrolljump' is positive use it as-is.
423 * When 'scrolljump' is negative use it as a percentage of the window height.
424 */
425 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100426scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000427{
428 if (p_sj >= 0)
429 return (int)p_sj;
430 return (curwin->w_height * -p_sj) / 100;
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
435 * current window.
436 */
437 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100438check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 lineoff_T loff;
441 int n;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100442 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
Bram Moolenaar375e3392019-01-31 18:26:10 +0100444 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445#ifdef FEAT_FOLDING
446 || hasAnyFolding(curwin)
447#endif
448 )
449 {
450 loff.lnum = curwin->w_cursor.lnum;
451#ifdef FEAT_DIFF
452 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100453 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454#else
455 n = 0;
456#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100457 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
460 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100461 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 if (loff.lnum < curwin->w_topline
463#ifdef FEAT_DIFF
464 || (loff.lnum == curwin->w_topline && loff.fill > 0)
465#endif
466 )
467 break;
468 n += loff.height;
469 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100470 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 return TRUE;
472 }
473 return FALSE;
474}
475
476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100477update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 if (curwin->w_set_curswant)
480 {
481 validate_virtcol();
482 curwin->w_curswant = curwin->w_virtcol;
483 curwin->w_set_curswant = FALSE;
484 }
485}
486
487/*
488 * Check if the cursor has moved. Set the w_valid flag accordingly.
489 */
490 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100491check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
494 {
495 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
496 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE);
497 wp->w_valid_cursor = wp->w_cursor;
498 wp->w_valid_leftcol = wp->w_leftcol;
499 }
500 else if (wp->w_cursor.col != wp->w_valid_cursor.col
501 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100502 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 {
504 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
505 wp->w_valid_cursor.col = wp->w_cursor.col;
506 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 }
509}
510
511/*
512 * Call this function when some window settings have changed, which require
513 * the cursor position, botline and topline to be recomputed and the window to
514 * be redrawn. E.g, when changing the 'wrap' option or folding.
515 */
516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100517changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 changed_window_setting_win(curwin);
520}
521
522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 wp->w_lines_valid = 0;
526 changed_line_abv_curs_win(wp);
527 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
528 redraw_win_later(wp, NOT_VALID);
529}
530
531/*
532 * Set wp->w_topline to a certain number.
533 */
534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100535set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200537#ifdef FEAT_DIFF
538 linenr_T prev_topline = wp->w_topline;
539#endif
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100542 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
544#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100545 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100547 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
548 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000550 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200552 if (lnum != prev_topline)
553 // Keep the filler lines when the topline didn't change.
554 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#endif
556 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100557 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 redraw_later(VALID);
559}
560
561/*
562 * Call this function when the length of the cursor line (in screen
563 * characters) has changed, and the change is before the cursor.
564 * Need to take care of w_botline separately!
565 */
566 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100567changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
570 |VALID_CHEIGHT|VALID_TOPLINE);
571}
572
573 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100574changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
576 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
577 |VALID_CHEIGHT|VALID_TOPLINE);
578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580/*
581 * Call this function when the length of a line (in screen characters) above
582 * the cursor have changed.
583 * Need to take care of w_botline separately!
584 */
585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100586changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
589 |VALID_CHEIGHT|VALID_TOPLINE);
590}
591
592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100593changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
596 |VALID_CHEIGHT|VALID_TOPLINE);
597}
598
599/*
600 * Make sure the value of curwin->w_botline is valid.
601 */
602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100603validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100605 validate_botline_win(curwin);
606}
607
608/*
609 * Make sure the value of wp->w_botline is valid.
610 */
611 void
612validate_botline_win(win_T *wp)
613{
614 if (!(wp->w_valid & VALID_BOTLINE))
615 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616}
617
618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 * Mark curwin->w_botline as invalid (because of some change in the buffer).
620 */
621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100622invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
625}
626
627 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100628invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
631}
632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100634approximate_botline_win(
635 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 wp->w_valid &= ~VALID_BOTLINE;
638}
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640/*
641 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
642 */
643 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 check_cursor_moved(curwin);
647 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
648 (VALID_WROW|VALID_WCOL));
649}
650
651/*
652 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
653 * w_topline must be valid, you may need to call update_topline() first!
654 */
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 check_cursor_moved(curwin);
659 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
660 curs_columns(TRUE);
661}
662
663#if defined(FEAT_GUI) || defined(PROTO)
664/*
665 * validate w_cline_row.
666 */
667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100668validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
670 /*
671 * First make sure that w_topline is valid (after moving the cursor).
672 */
673 update_topline();
674 check_cursor_moved(curwin);
675 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100676 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677}
678#endif
679
680/*
681 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200682 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 */
684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100685curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
687 linenr_T lnum;
688 int i;
689 int all_invalid;
690 int valid;
691#ifdef FEAT_FOLDING
692 long fold_count;
693#endif
694
Bram Moolenaar85a20022019-12-21 18:25:54 +0100695 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 all_invalid = (!redrawing()
697 || wp->w_lines_valid == 0
698 || wp->w_lines[0].wl_lnum > wp->w_topline);
699 i = 0;
700 wp->w_cline_row = 0;
701 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
702 {
703 valid = FALSE;
704 if (!all_invalid && i < wp->w_lines_valid)
705 {
706 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100707 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (wp->w_lines[i].wl_lnum == lnum)
709 {
710#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100711 // Check for newly inserted lines below this row, in which
712 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (!wp->w_buffer->b_mod_set
714 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
715 || wp->w_buffer->b_mod_top
716 > wp->w_lines[i].wl_lastlnum + 1)
717#endif
718 valid = TRUE;
719 }
720 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100721 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 }
723 if (valid
724#ifdef FEAT_DIFF
725 && (lnum != wp->w_topline || !wp->w_p_diff)
726#endif
727 )
728 {
729#ifdef FEAT_FOLDING
730 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100731 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (lnum > wp->w_cursor.lnum)
733 break;
734#else
735 ++lnum;
736#endif
737 wp->w_cline_row += wp->w_lines[i].wl_size;
738 }
739 else
740 {
741#ifdef FEAT_FOLDING
742 fold_count = foldedCount(wp, lnum, NULL);
743 if (fold_count)
744 {
745 lnum += fold_count;
746 if (lnum > wp->w_cursor.lnum)
747 break;
748 ++wp->w_cline_row;
749 }
750 else
751#endif
752#ifdef FEAT_DIFF
753 if (lnum == wp->w_topline)
754 wp->w_cline_row += plines_win_nofill(wp, lnum++, TRUE)
755 + wp->w_topfill;
756 else
757#endif
758 wp->w_cline_row += plines_win(wp, lnum++, TRUE);
759 }
760 }
761
762 check_cursor_moved(wp);
763 if (!(wp->w_valid & VALID_CHEIGHT))
764 {
765 if (all_invalid
766 || i == wp->w_lines_valid
767 || (i < wp->w_lines_valid
768 && (!wp->w_lines[i].wl_valid
769 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
770 {
771#ifdef FEAT_DIFF
772 if (wp->w_cursor.lnum == wp->w_topline)
773 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
774 TRUE) + wp->w_topfill;
775 else
776#endif
777 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
778#ifdef FEAT_FOLDING
779 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
780 NULL, NULL, TRUE, NULL);
781#endif
782 }
783 else if (i > wp->w_lines_valid)
784 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100785 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 wp->w_cline_height = 0;
787#ifdef FEAT_FOLDING
788 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
789 NULL, NULL, TRUE, NULL);
790#endif
791 }
792 else
793 {
794 wp->w_cline_height = wp->w_lines[i].wl_size;
795#ifdef FEAT_FOLDING
796 wp->w_cline_folded = wp->w_lines[i].wl_folded;
797#endif
798 }
799 }
800
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100801 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804}
805
806/*
807 * Validate curwin->w_virtcol only.
808 */
809 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100810validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 validate_virtcol_win(curwin);
813}
814
815/*
816 * Validate wp->w_virtcol only.
817 */
818 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 check_cursor_moved(wp);
822 if (!(wp->w_valid & VALID_VIRTCOL))
823 {
824 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
825 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000826#ifdef FEAT_SYN_HL
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200827 if (wp->w_p_cuc && !pum_visible())
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000828 redraw_win_later(wp, SOME_VALID);
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
831}
832
833/*
834 * Validate curwin->w_cline_height only.
835 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 check_cursor_moved(curwin);
840 if (!(curwin->w_valid & VALID_CHEIGHT))
841 {
842#ifdef FEAT_DIFF
843 if (curwin->w_cursor.lnum == curwin->w_topline)
844 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
845 + curwin->w_topfill;
846 else
847#endif
848 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
849#ifdef FEAT_FOLDING
850 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
851#endif
852 curwin->w_valid |= VALID_CHEIGHT;
853 }
854}
855
856/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000857 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 */
859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100860validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
862 colnr_T off;
863 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100864 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 validate_virtcol();
867 if (!(curwin->w_valid & VALID_WCOL))
868 {
869 col = curwin->w_virtcol;
870 off = curwin_col_off();
871 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200872 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
Bram Moolenaar85a20022019-12-21 18:25:54 +0100874 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000875 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200876 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100877 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100878 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200879 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000880 if (col > (int)curwin->w_leftcol)
881 col -= curwin->w_leftcol;
882 else
883 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100887#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100888 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 }
891}
892
893/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200894 * Compute offset of a window, occupied by absolute or relative line number,
895 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 */
897 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100898win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
Bram Moolenaar64486672010-05-16 15:46:46 +0200900 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#ifdef FEAT_CMDWIN
902 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
903#endif
904#ifdef FEAT_FOLDING
905 + wp->w_p_fdc
906#endif
907#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200908 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#endif
910 );
911}
912
913 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100914curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
916 return win_col_off(curwin);
917}
918
919/*
920 * Return the difference in column offset for the second screen line of a
Bram Moolenaar64486672010-05-16 15:46:46 +0200921 * wrapped line. It's 8 if 'number' or 'relativenumber' is on and 'n' is in
922 * 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 */
924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100925win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
Bram Moolenaar64486672010-05-16 15:46:46 +0200927 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000928 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 return 0;
930}
931
932 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100933curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934{
935 return win_col_off2(curwin);
936}
937
938/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100939 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Also updates curwin->w_wrow and curwin->w_cline_row.
941 * Also updates curwin->w_leftcol.
942 */
943 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100944curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +0100945 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100948 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 int off_left, off_right;
950 int n;
951 int p_lines;
952 int width = 0;
953 int textwidth;
954 int new_leftcol;
955 colnr_T startcol;
956 colnr_T endcol;
957 colnr_T prev_skipcol;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100958 long so = get_scrolloff_value();
959 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 /*
962 * First make sure that w_topline is valid (after moving the cursor).
963 */
964 update_topline();
965
966 /*
967 * Next make sure that w_cline_row is valid.
968 */
969 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100970 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 /*
973 * Compute the number of virtual columns.
974 */
975#ifdef FEAT_FOLDING
976 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100977 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
979 else
980#endif
981 getvvcol(curwin, &curwin->w_cursor,
982 &startcol, &(curwin->w_virtcol), &endcol);
983
Bram Moolenaar85a20022019-12-21 18:25:54 +0100984 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100986 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 extra = curwin_col_off();
989 curwin->w_wcol = curwin->w_virtcol + extra;
990 endcol += extra;
991
992 /*
993 * Now compute w_wrow, counting screen lines from w_cline_row.
994 */
995 curwin->w_wrow = curwin->w_cline_row;
996
Bram Moolenaar02631462017-09-22 15:20:32 +0200997 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (textwidth <= 0)
999 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001000 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001001 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001002 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001003 if (curwin->w_p_wrap)
1004 curwin->w_wrow = curwin->w_height - 1;
1005 else
1006 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001008 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
1010 width = textwidth + curwin_col_off2();
1011
Bram Moolenaar85a20022019-12-21 18:25:54 +01001012 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001013 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001015#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001016 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001017#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001018
Bram Moolenaar85a20022019-12-21 18:25:54 +01001019 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001020 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 curwin->w_wcol -= n * width;
1022 curwin->w_wrow += n;
1023
1024#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001025 // When cursor wraps to first char of next line in Insert
1026 // mode, the 'showbreak' string isn't shown, backup to first
1027 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001028 sbr = get_showbreak_value(curwin);
1029 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001030 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 curwin->w_wcol = 0;
1032#endif
1033 }
1034 }
1035
Bram Moolenaar85a20022019-12-21 18:25:54 +01001036 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1037 // is not folded.
1038 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001039 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#ifdef FEAT_FOLDING
1041 && !curwin->w_cline_folded
1042#endif
1043 )
1044 {
1045 /*
1046 * If Cursor is left of the screen, scroll rightwards.
1047 * If Cursor is right of the screen, scroll leftwards
1048 * If we get closer to the edge than 'sidescrolloff', scroll a little
1049 * extra
1050 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001051 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001052 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001053 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (off_left < 0 || off_right > 0)
1055 {
1056 if (off_left < 0)
1057 diff = -off_left;
1058 else
1059 diff = off_right;
1060
Bram Moolenaar85a20022019-12-21 18:25:54 +01001061 // When far off or not enough room on either side, put cursor in
1062 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1064 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1065 else
1066 {
1067 if (diff < p_ss)
1068 diff = p_ss;
1069 if (off_left < 0)
1070 new_leftcol = curwin->w_leftcol - diff;
1071 else
1072 new_leftcol = curwin->w_leftcol + diff;
1073 }
1074 if (new_leftcol < 0)
1075 new_leftcol = 0;
1076 if (new_leftcol != (int)curwin->w_leftcol)
1077 {
1078 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001079 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 redraw_later(NOT_VALID);
1081 }
1082 }
1083 curwin->w_wcol -= curwin->w_leftcol;
1084 }
1085 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1086 curwin->w_wcol -= curwin->w_leftcol;
1087 else
1088 curwin->w_wcol = 0;
1089
1090#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // Skip over filler lines. At the top use w_topfill, there
1092 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (curwin->w_cursor.lnum == curwin->w_topline)
1094 curwin->w_wrow += curwin->w_topfill;
1095 else
1096 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1097#endif
1098
1099 prev_skipcol = curwin->w_skipcol;
1100
1101 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if ((curwin->w_wrow >= curwin->w_height
1104 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001105 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 && (p_lines =
1107#ifdef FEAT_DIFF
1108 plines_win_nofill
1109#else
1110 plines_win
1111#endif
1112 (curwin, curwin->w_cursor.lnum, FALSE))
1113 - 1 >= curwin->w_height))
1114 && curwin->w_height != 0
1115 && curwin->w_cursor.lnum == curwin->w_topline
1116 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001117 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001119 // Cursor past end of screen. Happens with a single line that does
1120 // not fit on screen. Find a skipcol to show the text around the
1121 // cursor. Avoid scrolling all the time. compute value of "extra":
1122 // 1: Less than 'scrolloff' lines above
1123 // 2: Less than 'scrolloff' lines below
1124 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001126 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001128 // Compute last display line of the buffer line that we want at the
1129 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (p_lines == 0)
1131 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1132 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001133 if (p_lines > curwin->w_wrow + so)
1134 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 else
1136 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001137 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 extra += 2;
1139
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001140 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001142 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 n = curwin->w_virtcol / width;
1144 if (n > curwin->w_height / 2)
1145 n -= curwin->w_height / 2;
1146 else
1147 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001148 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (n > p_lines - curwin->w_height + 1)
1150 n = p_lines - curwin->w_height + 1;
1151 curwin->w_skipcol = n * width;
1152 }
1153 else if (extra == 1)
1154 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001155 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001156 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 + width - 1) / width;
1158 if (extra > 0)
1159 {
1160 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1161 extra = curwin->w_skipcol / width;
1162 curwin->w_skipcol -= extra * width;
1163 }
1164 }
1165 else if (extra == 2)
1166 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001167 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 endcol = (n - curwin->w_height + 1) * width;
1169 while (endcol > curwin->w_virtcol)
1170 endcol -= width;
1171 if (endcol > curwin->w_skipcol)
1172 curwin->w_skipcol = endcol;
1173 }
1174
1175 curwin->w_wrow -= curwin->w_skipcol / width;
1176 if (curwin->w_wrow >= curwin->w_height)
1177 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001178 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 extra = curwin->w_wrow - curwin->w_height + 1;
1180 curwin->w_skipcol += extra * width;
1181 curwin->w_wrow -= extra;
1182 }
1183
1184 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1185 if (extra > 0)
1186 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1187 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001188 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else
1191 curwin->w_skipcol = 0;
1192 if (prev_skipcol != curwin->w_skipcol)
1193 redraw_later(NOT_VALID);
1194
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001195#ifdef FEAT_SYN_HL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001196 // Redraw when w_virtcol changes and 'cursorcolumn' is set
Bram Moolenaarb6798752014-03-27 12:11:48 +01001197 if (curwin->w_p_cuc && (curwin->w_valid & VALID_VIRTCOL) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001198 && !pum_visible())
Bram Moolenaarb6798752014-03-27 12:11:48 +01001199 redraw_later(SOME_VALID);
1200#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001201#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1202 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1203 {
1204 curwin->w_wrow += popup_top_extra(curwin);
1205 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001206 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001207 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001208 else
1209 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001210#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001211
Bram Moolenaar08f23632019-11-16 14:19:33 +01001212 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1213 curwin->w_valid_leftcol = curwin->w_leftcol;
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1216}
1217
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001218#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001219/*
1220 * Compute the screen position of text character at "pos" in window "wp"
1221 * The resulting values are one-based, zero when character is not visible.
1222 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001223 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001224textpos2screenpos(
1225 win_T *wp,
1226 pos_T *pos,
1227 int *rowp, // screen row
1228 int *scolp, // start screen column
1229 int *ccolp, // cursor screen column
1230 int *ecolp) // end screen column
1231{
1232 colnr_T scol = 0, ccol = 0, ecol = 0;
1233 int row = 0;
1234 int rowoff = 0;
1235 colnr_T coloff = 0;
1236
Bram Moolenaar189663b2021-07-21 18:04:56 +02001237 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001238 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001239 colnr_T off;
1240 colnr_T col;
1241 int width;
1242 linenr_T lnum = pos->lnum;
1243#ifdef FEAT_FOLDING
1244 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001245
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001246 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1247#endif
1248 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1249#ifdef FEAT_FOLDING
1250 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001251 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001252 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001253 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001254 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001255 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001256#endif
1257 {
1258 getvcol(wp, pos, &scol, &ccol, &ecol);
1259
1260 // similar to what is done in validate_cursor_col()
1261 col = scol;
1262 off = win_col_off(wp);
1263 col += off;
1264 width = wp->w_width - off + win_col_off2(wp);
1265
1266 // long line wrapping, adjust row
1267 if (wp->w_p_wrap
1268 && col >= (colnr_T)wp->w_width
1269 && width > 0)
1270 {
1271 // use same formula as what is used in curs_columns()
1272 rowoff = ((col - wp->w_width) / width + 1);
1273 col -= rowoff * width;
1274 }
1275 col -= wp->w_leftcol;
1276 if (col >= wp->w_width)
1277 col = -1;
1278 if (col >= 0 && row + rowoff <= wp->w_height)
1279 {
1280 coloff = col - scol + wp->w_wincol + 1;
1281 row += W_WINROW(wp);
1282 }
1283 else
1284 // character is left, right or below of the window
1285 row = rowoff = scol = ccol = ecol = 0;
1286 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001287 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001288 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001289 *scolp = scol + coloff;
1290 *ccolp = ccol + coloff;
1291 *ecolp = ecol + coloff;
1292}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001293#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001294
Bram Moolenaar12034e22019-08-25 22:25:02 +02001295#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001296/*
1297 * "screenpos({winid}, {lnum}, {col})" function
1298 */
1299 void
1300f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1301{
1302 dict_T *dict;
1303 win_T *wp;
1304 pos_T pos;
1305 int row = 0;
1306 int scol = 0, ccol = 0, ecol = 0;
1307
1308 if (rettv_dict_alloc(rettv) != OK)
1309 return;
1310 dict = rettv->vval.v_dict;
1311
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001312 if (in_vim9script()
1313 && (check_for_number_arg(argvars, 0) == FAIL
1314 || check_for_number_arg(argvars, 1) == FAIL
1315 || check_for_number_arg(argvars, 2) == FAIL))
1316 return;
1317
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001318 wp = find_win_by_nr_or_id(&argvars[0]);
1319 if (wp == NULL)
1320 return;
1321
1322 pos.lnum = tv_get_number(&argvars[1]);
1323 pos.col = tv_get_number(&argvars[2]) - 1;
1324 pos.coladd = 0;
1325 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1326
1327 dict_add_number(dict, "row", row);
1328 dict_add_number(dict, "col", scol);
1329 dict_add_number(dict, "curscol", ccol);
1330 dict_add_number(dict, "endcol", ecol);
1331}
1332#endif
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334/*
1335 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001338scrolldown(
1339 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001340 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001342 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int wrow;
1344 int moved = FALSE;
1345
1346#ifdef FEAT_FOLDING
1347 linenr_T first;
1348
Bram Moolenaar85a20022019-12-21 18:25:54 +01001349 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1351#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001352 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 while (line_count-- > 0)
1354 {
1355#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001356 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1357 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 ++curwin->w_topfill;
1360 ++done;
1361 }
1362 else
1363#endif
1364 {
1365 if (curwin->w_topline == 1)
1366 break;
1367 --curwin->w_topline;
1368#ifdef FEAT_DIFF
1369 curwin->w_topfill = 0;
1370#endif
1371#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001372 // A sequence of folded lines only counts for one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (hasFolding(curwin->w_topline, &first, NULL))
1374 {
1375 ++done;
1376 if (!byfold)
1377 line_count -= curwin->w_topline - first - 1;
1378 curwin->w_botline -= curwin->w_topline - first;
1379 curwin->w_topline = first;
1380 }
1381 else
1382#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001383 done += PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001385 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 invalidate_botline();
1387 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001388 curwin->w_wrow += done; // keep w_wrow updated
1389 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390
1391#ifdef FEAT_DIFF
1392 if (curwin->w_cursor.lnum == curwin->w_topline)
1393 curwin->w_cline_row = 0;
1394 check_topfill(curwin, TRUE);
1395#endif
1396
1397 /*
1398 * Compute the row number of the last row of the cursor line
1399 * and move the cursor onto the displayed part of the window.
1400 */
1401 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001402 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 {
1404 validate_virtcol();
1405 validate_cheight();
1406 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001407 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1410 {
1411#ifdef FEAT_FOLDING
1412 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1413 {
1414 --wrow;
1415 if (first == 1)
1416 curwin->w_cursor.lnum = 1;
1417 else
1418 curwin->w_cursor.lnum = first - 1;
1419 }
1420 else
1421#endif
1422 wrow -= plines(curwin->w_cursor.lnum--);
1423 curwin->w_valid &=
1424 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1425 moved = TRUE;
1426 }
1427 if (moved)
1428 {
1429#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001430 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 foldAdjustCursor();
1432#endif
1433 coladvance(curwin->w_curswant);
1434 }
1435}
1436
1437/*
1438 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001441scrollup(
1442 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001443 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444{
1445#if defined(FEAT_FOLDING) || defined(FEAT_DIFF)
1446 linenr_T lnum;
1447
1448 if (
1449# ifdef FEAT_FOLDING
1450 (byfold && hasAnyFolding(curwin))
1451# ifdef FEAT_DIFF
1452 ||
1453# endif
1454# endif
1455# ifdef FEAT_DIFF
1456 curwin->w_p_diff
1457# endif
1458 )
1459 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001460 // count each sequence of folded lines as one logical line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 lnum = curwin->w_topline;
1462 while (line_count--)
1463 {
1464# ifdef FEAT_DIFF
1465 if (curwin->w_topfill > 0)
1466 --curwin->w_topfill;
1467 else
1468# endif
1469 {
1470# ifdef FEAT_FOLDING
1471 if (byfold)
1472 (void)hasFolding(lnum, NULL, &lnum);
1473# endif
1474 if (lnum >= curbuf->b_ml.ml_line_count)
1475 break;
1476 ++lnum;
1477# ifdef FEAT_DIFF
1478 curwin->w_topfill = diff_check_fill(curwin, lnum);
1479# endif
1480 }
1481 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001482 // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 curwin->w_botline += lnum - curwin->w_topline;
1484 curwin->w_topline = lnum;
1485 }
1486 else
1487#endif
1488 {
1489 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001490 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492
1493 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1494 curwin->w_topline = curbuf->b_ml.ml_line_count;
1495 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1496 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1497
1498#ifdef FEAT_DIFF
1499 check_topfill(curwin, FALSE);
1500#endif
1501
1502#ifdef FEAT_FOLDING
1503 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001504 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1506#endif
1507
1508 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1509 if (curwin->w_cursor.lnum < curwin->w_topline)
1510 {
1511 curwin->w_cursor.lnum = curwin->w_topline;
1512 curwin->w_valid &=
1513 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1514 coladvance(curwin->w_curswant);
1515 }
1516}
1517
1518#ifdef FEAT_DIFF
1519/*
1520 * Don't end up with too many filler lines in the window.
1521 */
1522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001523check_topfill(
1524 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001525 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 int n;
1528
1529 if (wp->w_topfill > 0)
1530 {
1531 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1532 if (wp->w_topfill + n > wp->w_height)
1533 {
1534 if (down && wp->w_topline > 1)
1535 {
1536 --wp->w_topline;
1537 wp->w_topfill = 0;
1538 }
1539 else
1540 {
1541 wp->w_topfill = wp->w_height - n;
1542 if (wp->w_topfill < 0)
1543 wp->w_topfill = 0;
1544 }
1545 }
1546 }
1547}
1548
1549/*
1550 * Use as many filler lines as possible for w_topline. Make sure w_topline
1551 * is still visible.
1552 */
1553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001554max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 int n;
1557
1558 n = plines_nofill(curwin->w_topline);
1559 if (n >= curwin->w_height)
1560 curwin->w_topfill = 0;
1561 else
1562 {
1563 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1564 if (curwin->w_topfill + n > curwin->w_height)
1565 curwin->w_topfill = curwin->w_height - n;
1566 }
1567}
1568#endif
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570/*
1571 * Scroll the screen one line down, but don't do it if it would move the
1572 * cursor off the screen.
1573 */
1574 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001575scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 int end_row;
1578#ifdef FEAT_DIFF
1579 int can_fill = (curwin->w_topfill
1580 < diff_check_fill(curwin, curwin->w_topline));
1581#endif
1582
1583 if (curwin->w_topline <= 1
1584#ifdef FEAT_DIFF
1585 && !can_fill
1586#endif
1587 )
1588 return;
1589
Bram Moolenaar85a20022019-12-21 18:25:54 +01001590 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
1592 /*
1593 * Compute the row number of the last row of the cursor line
1594 * and make sure it doesn't go off the screen. Make sure the cursor
1595 * doesn't go past 'scrolloff' lines from the screen end.
1596 */
1597 end_row = curwin->w_wrow;
1598#ifdef FEAT_DIFF
1599 if (can_fill)
1600 ++end_row;
1601 else
1602 end_row += plines_nofill(curwin->w_topline - 1);
1603#else
1604 end_row += plines(curwin->w_topline - 1);
1605#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001606 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 validate_cheight();
1609 validate_virtcol();
1610 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001611 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001613 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615#ifdef FEAT_DIFF
1616 if (can_fill)
1617 {
1618 ++curwin->w_topfill;
1619 check_topfill(curwin, TRUE);
1620 }
1621 else
1622 {
1623 --curwin->w_topline;
1624 curwin->w_topfill = 0;
1625 }
1626#else
1627 --curwin->w_topline;
1628#endif
1629#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001630 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001632 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1634 }
1635}
1636
1637/*
1638 * Scroll the screen one line up, but don't do it if it would move the cursor
1639 * off the screen.
1640 */
1641 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001642scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 int start_row;
1645
1646 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1647#ifdef FEAT_DIFF
1648 && curwin->w_topfill == 0
1649#endif
1650 )
1651 return;
1652
Bram Moolenaar85a20022019-12-21 18:25:54 +01001653 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
1655 /*
1656 * Compute the row number of the first row of the cursor line
1657 * and make sure it doesn't go off the screen. Make sure the cursor
1658 * doesn't go before 'scrolloff' lines from the screen start.
1659 */
1660#ifdef FEAT_DIFF
1661 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1662 - curwin->w_topfill;
1663#else
1664 start_row = curwin->w_wrow - plines(curwin->w_topline);
1665#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001666 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001669 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001671 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 {
1673#ifdef FEAT_DIFF
1674 if (curwin->w_topfill > 0)
1675 --curwin->w_topfill;
1676 else
1677#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001678 {
1679#ifdef FEAT_FOLDING
1680 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001683 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001684 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1686 }
1687}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688
1689/*
1690 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1691 * a (wrapped) text line. Uses and sets "lp->fill".
1692 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001693 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 */
1695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001696topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698#ifdef FEAT_DIFF
1699 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1700 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001701 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 ++lp->fill;
1703 lp->height = 1;
1704 }
1705 else
1706#endif
1707 {
1708 --lp->lnum;
1709#ifdef FEAT_DIFF
1710 lp->fill = 0;
1711#endif
1712 if (lp->lnum < 1)
1713 lp->height = MAXCOL;
1714 else
1715#ifdef FEAT_FOLDING
1716 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001717 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 lp->height = 1;
1719 else
1720#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001721 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
1723}
1724
1725/*
1726 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1727 * a (wrapped) text line. Uses and sets "lp->fill".
1728 * Returns the height of the added line in "lp->height".
1729 * Lines below the last one are incredibly high.
1730 */
1731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001732botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734#ifdef FEAT_DIFF
1735 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1736 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001737 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ++lp->fill;
1739 lp->height = 1;
1740 }
1741 else
1742#endif
1743 {
1744 ++lp->lnum;
1745#ifdef FEAT_DIFF
1746 lp->fill = 0;
1747#endif
1748 if (lp->lnum > curbuf->b_ml.ml_line_count)
1749 lp->height = MAXCOL;
1750 else
1751#ifdef FEAT_FOLDING
1752 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001753 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 lp->height = 1;
1755 else
1756#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001757 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759}
1760
1761#ifdef FEAT_DIFF
1762/*
1763 * Switch from including filler lines below lp->lnum to including filler
1764 * lines above loff.lnum + 1. This keeps pointing to the same line.
1765 * When there are no filler lines nothing changes.
1766 */
1767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001768botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 if (lp->fill > 0)
1771 {
1772 ++lp->lnum;
1773 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1774 }
1775}
1776
1777/*
1778 * Switch from including filler lines above lp->lnum to including filler
1779 * lines below loff.lnum - 1. This keeps pointing to the same line.
1780 * When there are no filler lines nothing changes.
1781 */
1782 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001783topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 if (lp->fill > 0)
1786 {
1787 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1788 --lp->lnum;
1789 }
1790}
1791#endif
1792
1793/*
1794 * Recompute topline to put the cursor at the top of the window.
1795 * Scroll at least "min_scroll" lines.
1796 * If "always" is TRUE, always set topline (for "zt").
1797 */
1798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 int scrolled = 0;
1802 int extra = 0;
1803 int used;
1804 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001805 linenr_T top; // just above displayed lines
1806 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 linenr_T old_topline = curwin->w_topline;
1808#ifdef FEAT_DIFF
1809 linenr_T old_topfill = curwin->w_topfill;
1810#endif
1811 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001812 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (mouse_dragging > 0)
1815 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 /*
1818 * Decrease topline until:
1819 * - it has become 1
1820 * - (part of) the cursor line is moved off the screen or
1821 * - moved at least 'scrolljump' lines and
1822 * - at least 'scrolloff' lines above and below the cursor
1823 */
1824 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01001825 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (curwin->w_cursor.lnum < curwin->w_topline)
1827 scrolled = used;
1828
1829#ifdef FEAT_FOLDING
1830 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
1831 {
1832 --top;
1833 ++bot;
1834 }
1835 else
1836#endif
1837 {
1838 top = curwin->w_cursor.lnum - 1;
1839 bot = curwin->w_cursor.lnum + 1;
1840 }
1841 new_topline = top + 1;
1842
1843#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001844 // "used" already contains the number of filler lines above, don't add it
1845 // again.
1846 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02001847 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848#endif
1849
1850 /*
1851 * Check if the lines from "top" to "bot" fit in the window. If they do,
1852 * set new_topline and advance "top" and "bot" to include more lines.
1853 */
1854 while (top > 0)
1855 {
1856#ifdef FEAT_FOLDING
1857 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001858 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 i = 1;
1860 else
1861#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001862 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 used += i;
1864 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
1865 {
1866#ifdef FEAT_FOLDING
1867 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001868 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 ++used;
1870 else
1871#endif
1872 used += plines(bot);
1873 }
1874 if (used > curwin->w_height)
1875 break;
1876 if (top < curwin->w_topline)
1877 scrolled += i;
1878
1879 /*
1880 * If scrolling is needed, scroll at least 'sj' lines.
1881 */
1882 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
1883 && extra >= off)
1884 break;
1885
1886 extra += i;
1887 new_topline = top;
1888 --top;
1889 ++bot;
1890 }
1891
1892 /*
1893 * If we don't have enough space, put cursor in the middle.
1894 * This makes sure we get the same position when using "k" and "j"
1895 * in a small window.
1896 */
1897 if (used > curwin->w_height)
1898 scroll_cursor_halfway(FALSE);
1899 else
1900 {
1901 /*
1902 * If "always" is FALSE, only adjust topline to a lower value, higher
1903 * value may happen with wrapping lines
1904 */
1905 if (new_topline < curwin->w_topline || always)
1906 curwin->w_topline = new_topline;
1907 if (curwin->w_topline > curwin->w_cursor.lnum)
1908 curwin->w_topline = curwin->w_cursor.lnum;
1909#ifdef FEAT_DIFF
1910 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1911 if (curwin->w_topfill > 0 && extra > off)
1912 {
1913 curwin->w_topfill -= extra - off;
1914 if (curwin->w_topfill < 0)
1915 curwin->w_topfill = 0;
1916 }
1917 check_topfill(curwin, FALSE);
1918#endif
1919 if (curwin->w_topline != old_topline
1920#ifdef FEAT_DIFF
1921 || curwin->w_topfill != old_topfill
1922#endif
1923 )
1924 curwin->w_valid &=
1925 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
1926 curwin->w_valid |= VALID_TOPLINE;
1927 }
1928}
1929
1930/*
1931 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
1932 * screen lines for text lines.
1933 */
1934 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001935set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936{
1937#ifdef FEAT_DIFF
1938 wp->w_filler_rows = 0;
1939#endif
1940 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001941 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 else
1943 {
1944 wp->w_empty_rows = wp->w_height - used;
1945#ifdef FEAT_DIFF
1946 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
1947 {
1948 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
1949 if (wp->w_empty_rows > wp->w_filler_rows)
1950 wp->w_empty_rows -= wp->w_filler_rows;
1951 else
1952 {
1953 wp->w_filler_rows = wp->w_empty_rows;
1954 wp->w_empty_rows = 0;
1955 }
1956 }
1957#endif
1958 }
1959}
1960
1961/*
1962 * Recompute topline to put the cursor at the bottom of the window.
1963 * Scroll at least "min_scroll" lines.
1964 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
1965 * This is messy stuff!!!
1966 */
1967 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001968scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 int used;
1971 int scrolled = 0;
1972 int extra = 0;
1973 int i;
1974 linenr_T line_count;
1975 linenr_T old_topline = curwin->w_topline;
1976 lineoff_T loff;
1977 lineoff_T boff;
1978#ifdef FEAT_DIFF
1979 int old_topfill = curwin->w_topfill;
1980 int fill_below_window;
1981#endif
1982 linenr_T old_botline = curwin->w_botline;
1983 linenr_T old_valid = curwin->w_valid;
1984 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001985 linenr_T cln; // Cursor Line Number
Bram Moolenaar375e3392019-01-31 18:26:10 +01001986 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
1988 cln = curwin->w_cursor.lnum;
1989 if (set_topbot)
1990 {
1991 used = 0;
1992 curwin->w_botline = cln + 1;
1993#ifdef FEAT_DIFF
1994 loff.fill = 0;
1995#endif
1996 for (curwin->w_topline = curwin->w_botline;
1997 curwin->w_topline > 1;
1998 curwin->w_topline = loff.lnum)
1999 {
2000 loff.lnum = curwin->w_topline;
2001 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002002 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 break;
2004 used += loff.height;
2005#ifdef FEAT_DIFF
2006 curwin->w_topfill = loff.fill;
2007#endif
2008 }
2009 set_empty_rows(curwin, used);
2010 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2011 if (curwin->w_topline != old_topline
2012#ifdef FEAT_DIFF
2013 || curwin->w_topfill != old_topfill
2014#endif
2015 )
2016 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2017 }
2018 else
2019 validate_botline();
2020
Bram Moolenaar85a20022019-12-21 18:25:54 +01002021 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#ifdef FEAT_DIFF
2023 used = plines_nofill(cln);
2024#else
2025 validate_cheight();
2026 used = curwin->w_cline_height;
2027#endif
2028
Bram Moolenaar85a20022019-12-21 18:25:54 +01002029 // If the cursor is below botline, we will at least scroll by the height
2030 // of the cursor line. Correct for empty lines, which are really part of
2031 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (cln >= curwin->w_botline)
2033 {
2034 scrolled = used;
2035 if (cln == curwin->w_botline)
2036 scrolled -= curwin->w_empty_rows;
2037 }
2038
2039 /*
2040 * Stop counting lines to scroll when
2041 * - hitting start of the file
2042 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002043 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * - lines between botline and cursor have been counted
2045 */
2046#ifdef FEAT_FOLDING
2047 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2048#endif
2049 {
2050 loff.lnum = cln;
2051 boff.lnum = cln;
2052 }
2053#ifdef FEAT_DIFF
2054 loff.fill = 0;
2055 boff.fill = 0;
2056 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2057 - curwin->w_filler_rows;
2058#endif
2059
2060 while (loff.lnum > 1)
2061 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002062 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2063 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002065 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2067 && loff.lnum <= curwin->w_botline
2068#ifdef FEAT_DIFF
2069 && (loff.lnum < curwin->w_botline
2070 || loff.fill >= fill_below_window)
2071#endif
2072 )
2073 break;
2074
Bram Moolenaar85a20022019-12-21 18:25:54 +01002075 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002077 if (loff.height == MAXCOL)
2078 used = MAXCOL;
2079 else
2080 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (used > curwin->w_height)
2082 break;
2083 if (loff.lnum >= curwin->w_botline
2084#ifdef FEAT_DIFF
2085 && (loff.lnum > curwin->w_botline
2086 || loff.fill <= fill_below_window)
2087#endif
2088 )
2089 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002090 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 scrolled += loff.height;
2092 if (loff.lnum == curwin->w_botline
2093#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002094 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#endif
2096 )
2097 scrolled -= curwin->w_empty_rows;
2098 }
2099
2100 if (boff.lnum < curbuf->b_ml.ml_line_count)
2101 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002102 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 botline_forw(&boff);
2104 used += boff.height;
2105 if (used > curwin->w_height)
2106 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002107 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2108 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 extra += boff.height;
2111 if (boff.lnum >= curwin->w_botline
2112#ifdef FEAT_DIFF
2113 || (boff.lnum + 1 == curwin->w_botline
2114 && boff.fill > curwin->w_filler_rows)
2115#endif
2116 )
2117 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002118 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 scrolled += boff.height;
2120 if (boff.lnum == curwin->w_botline
2121#ifdef FEAT_DIFF
2122 && boff.fill == 0
2123#endif
2124 )
2125 scrolled -= curwin->w_empty_rows;
2126 }
2127 }
2128 }
2129 }
2130
Bram Moolenaar85a20022019-12-21 18:25:54 +01002131 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (scrolled <= 0)
2133 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002134 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 else if (used > curwin->w_height)
2136 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002137 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 else
2139 {
2140 line_count = 0;
2141#ifdef FEAT_DIFF
2142 boff.fill = curwin->w_topfill;
2143#endif
2144 boff.lnum = curwin->w_topline - 1;
2145 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2146 {
2147 botline_forw(&boff);
2148 i += boff.height;
2149 ++line_count;
2150 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 line_count = 9999;
2153 }
2154
2155 /*
2156 * Scroll up if the cursor is off the bottom of the screen a bit.
2157 * Otherwise put it at 1/2 of the screen.
2158 */
2159 if (line_count >= curwin->w_height && line_count > min_scroll)
2160 scroll_cursor_halfway(FALSE);
2161 else
2162 scrollup(line_count, TRUE);
2163
2164 /*
2165 * If topline didn't change we need to restore w_botline and w_empty_rows
2166 * (we changed them).
2167 * If topline did change, update_screen() will set botline.
2168 */
2169 if (curwin->w_topline == old_topline && set_topbot)
2170 {
2171 curwin->w_botline = old_botline;
2172 curwin->w_empty_rows = old_empty_rows;
2173 curwin->w_valid = old_valid;
2174 }
2175 curwin->w_valid |= VALID_TOPLINE;
2176}
2177
2178/*
2179 * Recompute topline to put the cursor halfway the window
2180 * If "atend" is TRUE, also put it halfway at the end of the file.
2181 */
2182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002183scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
2185 int above = 0;
2186 linenr_T topline;
2187#ifdef FEAT_DIFF
2188 int topfill = 0;
2189#endif
2190 int below = 0;
2191 int used;
2192 lineoff_T loff;
2193 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002194#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002195 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002198#ifdef FEAT_PROP_POPUP
2199 // if the width changed this needs to be updated first
2200 may_update_popup_position();
2201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2203#ifdef FEAT_FOLDING
2204 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2205#endif
2206#ifdef FEAT_DIFF
2207 used = plines_nofill(loff.lnum);
2208 loff.fill = 0;
2209 boff.fill = 0;
2210#else
2211 used = plines(loff.lnum);
2212#endif
2213 topline = loff.lnum;
2214 while (topline > 1)
2215 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002216 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 if (boff.lnum < curbuf->b_ml.ml_line_count)
2219 {
2220 botline_forw(&boff);
2221 used += boff.height;
2222 if (used > curwin->w_height)
2223 break;
2224 below += boff.height;
2225 }
2226 else
2227 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 if (atend)
2230 ++used;
2231 }
2232 }
2233
Bram Moolenaar85a20022019-12-21 18:25:54 +01002234 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002237 if (loff.height == MAXCOL)
2238 used = MAXCOL;
2239 else
2240 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (used > curwin->w_height)
2242 break;
2243 above += loff.height;
2244 topline = loff.lnum;
2245#ifdef FEAT_DIFF
2246 topfill = loff.fill;
2247#endif
2248 }
2249 }
2250#ifdef FEAT_FOLDING
2251 if (!hasFolding(topline, &curwin->w_topline, NULL))
2252#endif
2253 curwin->w_topline = topline;
2254#ifdef FEAT_DIFF
2255 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002256 if (old_topline > curwin->w_topline + curwin->w_height)
2257 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 check_topfill(curwin, FALSE);
2259#endif
2260 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2261 curwin->w_valid |= VALID_TOPLINE;
2262}
2263
2264/*
2265 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002266 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 * If not possible, put it at the same position as scroll_cursor_halfway().
2268 * When called topline must be valid!
2269 */
2270 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002271cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002273 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002275 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 linenr_T botline;
2277 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002278 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 int max_off;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002280 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282 /*
2283 * How many lines we would like to have above/below the cursor depends on
2284 * whether the first/last line of the file is on screen.
2285 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002286 above_wanted = so;
2287 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002288 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 above_wanted = mouse_dragging - 1;
2291 below_wanted = mouse_dragging - 1;
2292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (curwin->w_topline == 1)
2294 {
2295 above_wanted = 0;
2296 max_off = curwin->w_height / 2;
2297 if (below_wanted > max_off)
2298 below_wanted = max_off;
2299 }
2300 validate_botline();
2301 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002302 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 below_wanted = 0;
2305 max_off = (curwin->w_height - 1) / 2;
2306 if (above_wanted > max_off)
2307 above_wanted = max_off;
2308 }
2309
2310 /*
2311 * If there are sufficient file-lines above and below the cursor, we can
2312 * return now.
2313 */
2314 cln = curwin->w_cursor.lnum;
2315 if (cln >= curwin->w_topline + above_wanted
2316 && cln < curwin->w_botline - below_wanted
2317#ifdef FEAT_FOLDING
2318 && !hasAnyFolding(curwin)
2319#endif
2320 )
2321 return;
2322
2323 /*
2324 * Narrow down the area where the cursor can be put by taking lines from
2325 * the top and the bottom until:
2326 * - the desired context lines are found
2327 * - the lines from the top is past the lines from the bottom
2328 */
2329 topline = curwin->w_topline;
2330 botline = curwin->w_botline - 1;
2331#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002332 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 above = curwin->w_topfill;
2334 below = curwin->w_filler_rows;
2335#endif
2336 while ((above < above_wanted || below < below_wanted) && topline < botline)
2337 {
2338 if (below < below_wanted && (below <= above || above >= above_wanted))
2339 {
2340#ifdef FEAT_FOLDING
2341 if (hasFolding(botline, &botline, NULL))
2342 ++below;
2343 else
2344#endif
2345 below += plines(botline);
2346 --botline;
2347 }
2348 if (above < above_wanted && (above < below || below >= below_wanted))
2349 {
2350#ifdef FEAT_FOLDING
2351 if (hasFolding(topline, NULL, &topline))
2352 ++above;
2353 else
2354#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002355 above += PLINES_NOFILL(topline);
2356#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002357 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (topline < botline)
2359 above += diff_check_fill(curwin, topline + 1);
2360#endif
2361 ++topline;
2362 }
2363 }
2364 if (topline == botline || botline == 0)
2365 curwin->w_cursor.lnum = topline;
2366 else if (topline > botline)
2367 curwin->w_cursor.lnum = botline;
2368 else
2369 {
2370 if (cln < topline && curwin->w_topline > 1)
2371 {
2372 curwin->w_cursor.lnum = topline;
2373 curwin->w_valid &=
2374 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2375 }
2376 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2377 {
2378 curwin->w_cursor.lnum = botline;
2379 curwin->w_valid &=
2380 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2381 }
2382 }
2383 curwin->w_valid |= VALID_TOPLINE;
2384}
2385
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002386static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388/*
2389 * move screen 'count' pages up or down and update screen
2390 *
2391 * return FAIL for failure, OK otherwise
2392 */
2393 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002394onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 long n;
2397 int retval = OK;
2398 lineoff_T loff;
2399 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002400 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
Bram Moolenaar85a20022019-12-21 18:25:54 +01002402 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
2404 beep_flush();
2405 return FAIL;
2406 }
2407
2408 for ( ; count > 0; --count)
2409 {
2410 validate_botline();
2411 /*
2412 * It's an error to move a page up when the first line is already on
2413 * the screen. It's an error to move a page down when the last line
2414 * is on the screen and the topline is 'scrolloff' lines from the
2415 * last line.
2416 */
2417 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002418 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2420 : (curwin->w_topline == 1
2421#ifdef FEAT_DIFF
2422 && curwin->w_topfill ==
2423 diff_check_fill(curwin, curwin->w_topline)
2424#endif
2425 ))
2426 {
2427 beep_flush();
2428 retval = FAIL;
2429 break;
2430 }
2431
2432#ifdef FEAT_DIFF
2433 loff.fill = 0;
2434#endif
2435 if (dir == FORWARD)
2436 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002437 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002440 if (p_window <= 2)
2441 ++curwin->w_topline;
2442 else
2443 curwin->w_topline += p_window - 2;
2444 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2445 curwin->w_topline = curbuf->b_ml.ml_line_count;
2446 curwin->w_cursor.lnum = curwin->w_topline;
2447 }
2448 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2449 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002450 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 curwin->w_topline = curbuf->b_ml.ml_line_count;
2452#ifdef FEAT_DIFF
2453 curwin->w_topfill = 0;
2454#endif
2455 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2456 }
2457 else
2458 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002459 // For the overlap, start with the line just below the window
2460 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 loff.lnum = curwin->w_botline;
2462#ifdef FEAT_DIFF
2463 loff.fill = diff_check_fill(curwin, loff.lnum)
2464 - curwin->w_filler_rows;
2465#endif
2466 get_scroll_overlap(&loff, -1);
2467 curwin->w_topline = loff.lnum;
2468#ifdef FEAT_DIFF
2469 curwin->w_topfill = loff.fill;
2470 check_topfill(curwin, FALSE);
2471#endif
2472 curwin->w_cursor.lnum = curwin->w_topline;
2473 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2474 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2475 }
2476 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002477 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479#ifdef FEAT_DIFF
2480 if (curwin->w_topline == 1)
2481 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002482 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 max_topfill();
2484 continue;
2485 }
2486#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002487 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002488 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002489 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002490 if (p_window <= 2)
2491 --curwin->w_topline;
2492 else
2493 curwin->w_topline -= p_window - 2;
2494 if (curwin->w_topline < 1)
2495 curwin->w_topline = 1;
2496 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2497 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2498 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2499 continue;
2500 }
2501
Bram Moolenaar85a20022019-12-21 18:25:54 +01002502 // Find the line at the top of the window that is going to be the
2503 // line at the bottom of the window. Make sure this results in
2504 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 loff.lnum = curwin->w_topline - 1;
2506#ifdef FEAT_DIFF
2507 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2508 - curwin->w_topfill;
2509#endif
2510 get_scroll_overlap(&loff, 1);
2511
2512 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2513 {
2514 loff.lnum = curbuf->b_ml.ml_line_count;
2515#ifdef FEAT_DIFF
2516 loff.fill = 0;
2517 }
2518 else
2519 {
2520 botline_topline(&loff);
2521#endif
2522 }
2523 curwin->w_cursor.lnum = loff.lnum;
2524
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525 // Find the line just above the new topline to get the right line
2526 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 n = 0;
2528 while (n <= curwin->w_height && loff.lnum >= 1)
2529 {
2530 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002531 if (loff.height == MAXCOL)
2532 n = MAXCOL;
2533 else
2534 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002536 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 curwin->w_topline = 1;
2539#ifdef FEAT_DIFF
2540 max_topfill();
2541#endif
2542 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2543 }
2544 else
2545 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002546 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547#ifdef FEAT_DIFF
2548 topline_botline(&loff);
2549#endif
2550 botline_forw(&loff);
2551 botline_forw(&loff);
2552#ifdef FEAT_DIFF
2553 botline_topline(&loff);
2554#endif
2555#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002556 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2558#endif
2559
Bram Moolenaar85a20022019-12-21 18:25:54 +01002560 // Always scroll at least one line. Avoid getting stuck on
2561 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (loff.lnum >= curwin->w_topline
2563#ifdef FEAT_DIFF
2564 && (loff.lnum > curwin->w_topline
2565 || loff.fill >= curwin->w_topfill)
2566#endif
2567 )
2568 {
2569#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002570 // First try using the maximum number of filler lines. If
2571 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 loff.fill = curwin->w_topfill;
2573 if (curwin->w_topfill < diff_check_fill(curwin,
2574 curwin->w_topline))
2575 max_topfill();
2576 if (curwin->w_topfill == loff.fill)
2577#endif
2578 {
2579 --curwin->w_topline;
2580#ifdef FEAT_DIFF
2581 curwin->w_topfill = 0;
2582#endif
2583 }
2584 comp_botline(curwin);
2585 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002586 curwin->w_valid &=
2587 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589 else
2590 {
2591 curwin->w_topline = loff.lnum;
2592#ifdef FEAT_DIFF
2593 curwin->w_topfill = loff.fill;
2594 check_topfill(curwin, FALSE);
2595#endif
2596 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2597 }
2598 }
2599 }
2600 }
2601#ifdef FEAT_FOLDING
2602 foldAdjustCursor();
2603#endif
2604 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002605 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002606 if (retval == OK)
2607 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2609
Bram Moolenaar907dad72018-07-10 15:07:15 +02002610 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002612 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2613 // But make sure we scroll at least one line (happens with mix of long
2614 // wrapping lines and non-wrapping line).
2615 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002617 scroll_cursor_top(1, FALSE);
2618 if (curwin->w_topline <= old_topline
2619 && old_topline < curbuf->b_ml.ml_line_count)
2620 {
2621 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002623 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2624#endif
2625 }
2626 }
2627#ifdef FEAT_FOLDING
2628 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632
2633 redraw_later(VALID);
2634 return retval;
2635}
2636
2637/*
2638 * Decide how much overlap to use for page-up or page-down scrolling.
2639 * This is symmetric, so that doing both keeps the same lines displayed.
2640 * Three lines are examined:
2641 *
2642 * before CTRL-F after CTRL-F / before CTRL-B
2643 * etc. l1
2644 * l1 last but one line ------------
2645 * l2 last text line l2 top text line
2646 * ------------- l3 second text line
2647 * l3 etc.
2648 */
2649 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002650get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 int h1, h2, h3, h4;
2653 int min_height = curwin->w_height - 2;
2654 lineoff_T loff0, loff1, loff2;
2655
2656#ifdef FEAT_DIFF
2657 if (lp->fill > 0)
2658 lp->height = 1;
2659 else
2660 lp->height = plines_nofill(lp->lnum);
2661#else
2662 lp->height = plines(lp->lnum);
2663#endif
2664 h1 = lp->height;
2665 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002666 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
2668 loff0 = *lp;
2669 if (dir > 0)
2670 botline_forw(lp);
2671 else
2672 topline_back(lp);
2673 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002674 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002676 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 return;
2678 }
2679
2680 loff1 = *lp;
2681 if (dir > 0)
2682 botline_forw(lp);
2683 else
2684 topline_back(lp);
2685 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002686 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002688 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 return;
2690 }
2691
2692 loff2 = *lp;
2693 if (dir > 0)
2694 botline_forw(lp);
2695 else
2696 topline_back(lp);
2697 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002698 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002699 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002701 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702}
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704/*
2705 * Scroll 'scroll' lines up or down.
2706 */
2707 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002708halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 long scrolled = 0;
2711 int i;
2712 int n;
2713 int room;
2714
2715 if (Prenum)
2716 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2717 curwin->w_height : Prenum;
2718 n = (curwin->w_p_scr <= curwin->w_height) ?
2719 curwin->w_p_scr : curwin->w_height;
2720
Bram Moolenaard5d37532017-03-27 23:02:07 +02002721 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 validate_botline();
2723 room = curwin->w_empty_rows;
2724#ifdef FEAT_DIFF
2725 room += curwin->w_filler_rows;
2726#endif
2727 if (flag)
2728 {
2729 /*
2730 * scroll the text up
2731 */
2732 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2733 {
2734#ifdef FEAT_DIFF
2735 if (curwin->w_topfill > 0)
2736 {
2737 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002738 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 --curwin->w_topfill;
2740 }
2741 else
2742#endif
2743 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002744 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 n -= i;
2746 if (n < 0 && scrolled > 0)
2747 break;
2748#ifdef FEAT_FOLDING
2749 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2750#endif
2751 ++curwin->w_topline;
2752#ifdef FEAT_DIFF
2753 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2754#endif
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2757 {
2758 ++curwin->w_cursor.lnum;
2759 curwin->w_valid &=
2760 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2764 scrolled += i;
2765
2766 /*
2767 * Correct w_botline for changed w_topline.
2768 * Won't work when there are filler lines.
2769 */
2770#ifdef FEAT_DIFF
2771 if (curwin->w_p_diff)
2772 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2773 else
2774#endif
2775 {
2776 room += i;
2777 do
2778 {
2779 i = plines(curwin->w_botline);
2780 if (i > room)
2781 break;
2782#ifdef FEAT_FOLDING
2783 (void)hasFolding(curwin->w_botline, NULL,
2784 &curwin->w_botline);
2785#endif
2786 ++curwin->w_botline;
2787 room -= i;
2788 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2789 }
2790 }
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 /*
2793 * When hit bottom of the file: move cursor down.
2794 */
2795 if (n > 0)
2796 {
2797# ifdef FEAT_FOLDING
2798 if (hasAnyFolding(curwin))
2799 {
2800 while (--n >= 0
2801 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2802 {
2803 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2804 &curwin->w_cursor.lnum);
2805 ++curwin->w_cursor.lnum;
2806 }
2807 }
2808 else
2809# endif
2810 curwin->w_cursor.lnum += n;
2811 check_cursor_lnum();
2812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
2814 else
2815 {
2816 /*
2817 * scroll the text down
2818 */
2819 while (n > 0 && curwin->w_topline > 1)
2820 {
2821#ifdef FEAT_DIFF
2822 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
2823 {
2824 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01002825 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 ++curwin->w_topfill;
2827 }
2828 else
2829#endif
2830 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002831 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 n -= i;
2833 if (n < 0 && scrolled > 0)
2834 break;
2835 --curwin->w_topline;
2836#ifdef FEAT_FOLDING
2837 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2838#endif
2839#ifdef FEAT_DIFF
2840 curwin->w_topfill = 0;
2841#endif
2842 }
2843 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
2844 VALID_BOTLINE|VALID_BOTLINE_AP);
2845 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 if (curwin->w_cursor.lnum > 1)
2847 {
2848 --curwin->w_cursor.lnum;
2849 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01002852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 /*
2854 * When hit top of the file: move cursor up.
2855 */
2856 if (n > 0)
2857 {
2858 if (curwin->w_cursor.lnum <= (linenr_T)n)
2859 curwin->w_cursor.lnum = 1;
2860 else
2861# ifdef FEAT_FOLDING
2862 if (hasAnyFolding(curwin))
2863 {
2864 while (--n >= 0 && curwin->w_cursor.lnum > 1)
2865 {
2866 --curwin->w_cursor.lnum;
2867 (void)hasFolding(curwin->w_cursor.lnum,
2868 &curwin->w_cursor.lnum, NULL);
2869 }
2870 }
2871 else
2872# endif
2873 curwin->w_cursor.lnum -= n;
2874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002877 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 foldAdjustCursor();
2879# endif
2880#ifdef FEAT_DIFF
2881 check_topfill(curwin, !flag);
2882#endif
2883 cursor_correct();
2884 beginline(BL_SOL | BL_FIX);
2885 redraw_later(VALID);
2886}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002887
Bram Moolenaar860cae12010-06-05 23:22:07 +02002888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002889do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002890{
2891 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002892 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002893 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002894 colnr_T curswant = curwin->w_curswant;
2895 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002896 win_T *old_curwin = curwin;
2897 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01002898 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002899 int old_VIsual_select = VIsual_select;
2900 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002901
2902 /*
2903 * loop through the cursorbound windows
2904 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002905 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002906 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002907 {
2908 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002909 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02002910 if (curwin != old_curwin && curwin->w_p_crb)
2911 {
2912# ifdef FEAT_DIFF
2913 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002914 curwin->w_cursor.lnum =
2915 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002916 else
2917# endif
2918 curwin->w_cursor.lnum = line;
2919 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01002920 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02002921 curwin->w_curswant = curswant;
2922 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002923
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 // Make sure the cursor is in a valid position. Temporarily set
2925 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01002926 restart_edit_save = restart_edit;
2927 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002928 check_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002929# ifdef FEAT_SYN_HL
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002930 if (curwin->w_p_cul || curwin->w_p_cuc)
Bram Moolenaar519d7782017-01-14 14:54:33 +01002931 validate_cursor();
Bram Moolenaar1b9750d2017-01-15 20:51:37 +01002932# endif
Bram Moolenaar61452852011-02-01 18:01:11 +01002933 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002935 if (has_mbyte)
2936 mb_adjust_cursor();
Bram Moolenaar9506cad2017-01-15 13:53:49 +01002937 redraw_later(VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002938
Bram Moolenaar85a20022019-12-21 18:25:54 +01002939 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01002940 if (!curwin->w_p_scb)
2941 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002942 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002943 }
2944 }
2945
2946 /*
2947 * reset current-window
2948 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002949 VIsual_select = old_VIsual_select;
2950 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002951 curwin = old_curwin;
2952 curbuf = old_curbuf;
2953}