blob: 0c2f23a7d6b6388f13b84f7598c9beadd7befdb4 [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/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
41 static int
42adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
53 width -= win_col_off2(wp);
54 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 * Compute wp->w_botline for the current wp->w_topline. Can be called after
66 * wp->w_topline changed.
67 */
68 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010069comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070{
71 int n;
72 linenr_T lnum;
73 int done;
74#ifdef FEAT_FOLDING
75 linenr_T last;
76 int folded;
77#endif
78
79 /*
80 * If w_cline_row is valid, start there.
81 * Otherwise have to start at w_topline.
82 */
83 check_cursor_moved(wp);
84 if (wp->w_valid & VALID_CROW)
85 {
86 lnum = wp->w_cursor.lnum;
87 done = wp->w_cline_row;
88 }
89 else
90 {
91 lnum = wp->w_topline;
92 done = 0;
93 }
94
95 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
96 {
97#ifdef FEAT_FOLDING
98 last = lnum;
99 folded = FALSE;
100 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
101 {
102 n = 1;
103 folded = TRUE;
104 }
105 else
106#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#ifdef FEAT_DIFF
109 if (lnum == wp->w_topline)
110 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
111 else
112#endif
113 n = plines_win(wp, lnum, TRUE);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100114 if (lnum == wp->w_topline)
115 n = adjust_plines_for_skipcol(wp, n);
116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 if (
118#ifdef FEAT_FOLDING
119 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
120#else
121 lnum == wp->w_cursor.lnum
122#endif
123 )
124 {
125 wp->w_cline_row = done;
126 wp->w_cline_height = n;
127#ifdef FEAT_FOLDING
128 wp->w_cline_folded = folded;
129#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100130 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
132 }
133 if (done + n > wp->w_height)
134 break;
135 done += n;
136#ifdef FEAT_FOLDING
137 lnum = last;
138#endif
139 }
140
Bram Moolenaar85a20022019-12-21 18:25:54 +0100141 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 wp->w_botline = lnum;
143 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
144
145 set_empty_rows(wp, done);
146}
147
148/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100149 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
150 * set.
151 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100152 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100153redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100154{
155 if ((wp->w_p_rnu
156#ifdef FEAT_SYN_HL
157 || wp->w_p_cul
158#endif
159 )
160 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200161 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200162 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000163 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100164 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200165 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100166}
167
zeertzjq3e559cd2022-03-27 19:26:55 +0100168#ifdef FEAT_SYN_HL
169/*
170 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
171 * contains "screenline".
172 */
173 static void
174redraw_for_cursorcolumn(win_T *wp)
175{
176 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
177 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100178 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100179 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100180 redraw_win_later(wp, UPD_SOME_VALID);
181 // When 'cursorlineopt' contains "screenline" need to redraw with
182 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100183 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100184 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100185 }
186}
187#endif
188
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 * Update curwin->w_topline and redraw if necessary.
191 * Used to update the screen before printing a message.
192 */
193 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100194update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 update_topline();
197 if (must_redraw)
198 update_screen(0);
199}
200
201/*
202 * Update curwin->w_topline to move the cursor onto the screen.
203 */
204 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100205update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206{
207 long line_count;
208 int halfheight;
209 int n;
210 linenr_T old_topline;
211#ifdef FEAT_DIFF
212 int old_topfill;
213#endif
214#ifdef FEAT_FOLDING
215 linenr_T lnum;
216#endif
217 int check_topline = FALSE;
218 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100219 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100220 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // If there is no valid screen and when the window height is zero just use
223 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200224 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200225 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100226 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200227 curwin->w_topline = curwin->w_cursor.lnum;
228 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200229 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200230 return;
231 }
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 check_cursor_moved(curwin);
234 if (curwin->w_valid & VALID_TOPLINE)
235 return;
236
Bram Moolenaar85a20022019-12-21 18:25:54 +0100237 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000238 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100239 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
241 old_topline = curwin->w_topline;
242#ifdef FEAT_DIFF
243 old_topfill = curwin->w_topfill;
244#endif
245
246 /*
247 * If the buffer is empty, always set topline to 1.
248 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100249 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 {
251 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100252 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 curwin->w_botline = 2;
255 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 }
258
259 /*
260 * If the cursor is above or near the top of the window, scroll the window
261 * to show the line the cursor is in, with 'scrolloff' context.
262 */
263 else
264 {
265 if (curwin->w_topline > 1)
266 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100267 // If the cursor is above topline, scrolling is always needed.
268 // If the cursor is far below topline and there is no folding,
269 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 if (curwin->w_cursor.lnum < curwin->w_topline)
271 check_topline = TRUE;
272 else if (check_top_offset())
273 check_topline = TRUE;
274 }
275#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100276 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
278 curwin->w_topline))
279 check_topline = TRUE;
280#endif
281
282 if (check_topline)
283 {
284 halfheight = curwin->w_height / 2 - 1;
285 if (halfheight < 2)
286 halfheight = 2;
287
288#ifdef FEAT_FOLDING
289 if (hasAnyFolding(curwin))
290 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100291 // Count the number of logical lines between the cursor and
292 // topline + scrolloff (approximation of how much will be
293 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 n = 0;
295 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100296 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 {
298 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100299 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
301 break;
302 (void)hasFolding(lnum, NULL, &lnum);
303 }
304 }
305 else
306#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100307 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308
Bram Moolenaar85a20022019-12-21 18:25:54 +0100309 // If we weren't very close to begin with, we scroll to put the
310 // cursor in the middle of the window. Otherwise put the cursor
311 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 if (n >= halfheight)
313 scroll_cursor_halfway(FALSE);
314 else
315 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000316 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 check_botline = TRUE;
318 }
319 }
320
321 else
322 {
323#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100324 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
326#endif
327 check_botline = TRUE;
328 }
329 }
330
331 /*
332 * If the cursor is below the bottom of the window, scroll the window
333 * to put the cursor on the window.
334 * When w_botline is invalid, recompute it first, to avoid a redraw later.
335 * If w_botline was approximated, we might need a redraw later in a few
336 * cases, but we don't want to spend (a lot of) time recomputing w_botline
337 * for every small change.
338 */
339 if (check_botline)
340 {
341 if (!(curwin->w_valid & VALID_BOTLINE_AP))
342 validate_botline();
343
344 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
345 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000346 if (curwin->w_cursor.lnum < curwin->w_botline)
347 {
348 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100349 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#ifdef FEAT_FOLDING
351 || hasAnyFolding(curwin)
352#endif
353 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 lineoff_T loff;
356
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 // Cursor is (a few lines) above botline, check if there are
358 // 'scrolloff' window lines below the cursor. If not, need to
359 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 n = curwin->w_empty_rows;
361 loff.lnum = curwin->w_cursor.lnum;
362#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100363 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
365#endif
366#ifdef FEAT_DIFF
367 loff.fill = 0;
368 n += curwin->w_filler_rows;
369#endif
370 loff.height = 0;
371 while (loff.lnum < curwin->w_botline
372#ifdef FEAT_DIFF
373 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
374#endif
375 )
376 {
377 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100378 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 break;
380 botline_forw(&loff);
381 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100382 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100383 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000385 }
386 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100387 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000388 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 }
390 if (check_botline)
391 {
392#ifdef FEAT_FOLDING
393 if (hasAnyFolding(curwin))
394 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100395 // Count the number of logical lines between the cursor and
396 // botline - scrolloff (approximation of how much will be
397 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 line_count = 0;
399 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100400 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {
402 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100403 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 if (lnum <= 0 || line_count > curwin->w_height + 1)
405 break;
406 (void)hasFolding(lnum, &lnum, NULL);
407 }
408 }
409 else
410#endif
411 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100412 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000414 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 else
416 scroll_cursor_halfway(FALSE);
417 }
418 }
419 }
420 curwin->w_valid |= VALID_TOPLINE;
421
422 /*
423 * Need to redraw when topline changed.
424 */
425 if (curwin->w_topline != old_topline
426#ifdef FEAT_DIFF
427 || curwin->w_topfill != old_topfill
428#endif
429 )
430 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100431 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000432 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {
434 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100435 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 }
437 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100438 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100439 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 if (curwin->w_cursor.lnum == curwin->w_topline)
441 validate_cursor();
442 }
443
Bram Moolenaar375e3392019-01-31 18:26:10 +0100444 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445}
446
447/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000448 * Return the scrolljump value to use for the current window.
449 * When 'scrolljump' is positive use it as-is.
450 * When 'scrolljump' is negative use it as a percentage of the window height.
451 */
452 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100453scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000454{
455 if (p_sj >= 0)
456 return (int)p_sj;
457 return (curwin->w_height * -p_sj) / 100;
458}
459
460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
462 * current window.
463 */
464 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100465check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 lineoff_T loff;
468 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100469 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar375e3392019-01-31 18:26:10 +0100471 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472#ifdef FEAT_FOLDING
473 || hasAnyFolding(curwin)
474#endif
475 )
476 {
477 loff.lnum = curwin->w_cursor.lnum;
478#ifdef FEAT_DIFF
479 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481#else
482 n = 0;
483#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100484 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100485 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {
487 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100488 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (loff.lnum < curwin->w_topline
490#ifdef FEAT_DIFF
491 || (loff.lnum == curwin->w_topline && loff.fill > 0)
492#endif
493 )
494 break;
495 n += loff.height;
496 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100497 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 return TRUE;
499 }
500 return FALSE;
501}
502
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100503/*
504 * Update w_curswant.
505 */
506 void
507update_curswant_force(void)
508{
509 validate_virtcol();
510 curwin->w_curswant = curwin->w_virtcol
511#ifdef FEAT_PROP_POPUP
512 - curwin->w_virtcol_first_char
513#endif
514 ;
515 curwin->w_set_curswant = FALSE;
516}
517
518/*
519 * Update w_curswant if w_set_curswant is set.
520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100522update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100525 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526}
527
528/*
529 * Check if the cursor has moved. Set the w_valid flag accordingly.
530 */
531 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100532check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
535 {
536 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100537 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
538 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 wp->w_valid_cursor = wp->w_cursor;
540 wp->w_valid_leftcol = wp->w_leftcol;
541 }
542 else if (wp->w_cursor.col != wp->w_valid_cursor.col
543 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100544 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
546 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
547 wp->w_valid_cursor.col = wp->w_cursor.col;
548 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551}
552
553/*
554 * Call this function when some window settings have changed, which require
555 * the cursor position, botline and topline to be recomputed and the window to
556 * be redrawn. E.g, when changing the 'wrap' option or folding.
557 */
558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100559changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 changed_window_setting_win(curwin);
562}
563
564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100565changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 wp->w_lines_valid = 0;
568 changed_line_abv_curs_win(wp);
569 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100570 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571}
572
573/*
574 * Set wp->w_topline to a certain number.
575 */
576 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100577set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200579#ifdef FEAT_DIFF
580 linenr_T prev_topline = wp->w_topline;
581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100584 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
586#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100587 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100589 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
590 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000592 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200594 if (lnum != prev_topline)
595 // Keep the filler lines when the topline didn't change.
596 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597#endif
598 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100599 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100600 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601}
602
603/*
604 * Call this function when the length of the cursor line (in screen
605 * characters) has changed, and the change is before the cursor.
606 * Need to take care of w_botline separately!
607 */
608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100609changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
612 |VALID_CHEIGHT|VALID_TOPLINE);
613}
614
615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100616changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
619 |VALID_CHEIGHT|VALID_TOPLINE);
620}
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622/*
623 * Call this function when the length of a line (in screen characters) above
624 * the cursor have changed.
625 * Need to take care of w_botline separately!
626 */
627 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100628changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
631 |VALID_CHEIGHT|VALID_TOPLINE);
632}
633
634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100635changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
638 |VALID_CHEIGHT|VALID_TOPLINE);
639}
640
641/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100642 * Display of line has changed for "buf", invalidate cursor position and
643 * w_botline.
644 */
645 void
646changed_line_display_buf(buf_T *buf)
647{
648 win_T *wp;
649
650 FOR_ALL_WINDOWS(wp)
651 if (wp->w_buffer == buf)
652 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
653 |VALID_CROW|VALID_CHEIGHT
654 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
655}
656
657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 * Make sure the value of curwin->w_botline is valid.
659 */
660 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100661validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100663 validate_botline_win(curwin);
664}
665
666/*
667 * Make sure the value of wp->w_botline is valid.
668 */
669 void
670validate_botline_win(win_T *wp)
671{
672 if (!(wp->w_valid & VALID_BOTLINE))
673 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674}
675
676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * Mark curwin->w_botline as invalid (because of some change in the buffer).
678 */
679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
683}
684
685 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100686invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
689}
690
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100692approximate_botline_win(
693 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
695 wp->w_valid &= ~VALID_BOTLINE;
696}
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698/*
699 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
700 */
701 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100702cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703{
704 check_cursor_moved(curwin);
705 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
706 (VALID_WROW|VALID_WCOL));
707}
708
709/*
710 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
711 * w_topline must be valid, you may need to call update_topline() first!
712 */
713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100714validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100716 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 check_cursor_moved(curwin);
718 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
719 curs_columns(TRUE);
720}
721
722#if defined(FEAT_GUI) || defined(PROTO)
723/*
724 * validate w_cline_row.
725 */
726 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100727validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
729 /*
730 * First make sure that w_topline is valid (after moving the cursor).
731 */
732 update_topline();
733 check_cursor_moved(curwin);
734 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100735 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736}
737#endif
738
739/*
740 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200741 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100744curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 linenr_T lnum;
747 int i;
748 int all_invalid;
749 int valid;
750#ifdef FEAT_FOLDING
751 long fold_count;
752#endif
753
Bram Moolenaar85a20022019-12-21 18:25:54 +0100754 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 all_invalid = (!redrawing()
756 || wp->w_lines_valid == 0
757 || wp->w_lines[0].wl_lnum > wp->w_topline);
758 i = 0;
759 wp->w_cline_row = 0;
760 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
761 {
762 valid = FALSE;
763 if (!all_invalid && i < wp->w_lines_valid)
764 {
765 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100766 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (wp->w_lines[i].wl_lnum == lnum)
768 {
769#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100770 // Check for newly inserted lines below this row, in which
771 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (!wp->w_buffer->b_mod_set
773 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
774 || wp->w_buffer->b_mod_top
775 > wp->w_lines[i].wl_lastlnum + 1)
776#endif
777 valid = TRUE;
778 }
779 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100780 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 if (valid
783#ifdef FEAT_DIFF
784 && (lnum != wp->w_topline || !wp->w_p_diff)
785#endif
786 )
787 {
788#ifdef FEAT_FOLDING
789 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100790 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (lnum > wp->w_cursor.lnum)
792 break;
793#else
794 ++lnum;
795#endif
796 wp->w_cline_row += wp->w_lines[i].wl_size;
797 }
798 else
799 {
800#ifdef FEAT_FOLDING
801 fold_count = foldedCount(wp, lnum, NULL);
802 if (fold_count)
803 {
804 lnum += fold_count;
805 if (lnum > wp->w_cursor.lnum)
806 break;
807 ++wp->w_cline_row;
808 }
809 else
810#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100811 {
812 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_DIFF
814 if (lnum == wp->w_topline)
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100815 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 else
817#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100818 n = plines_win(wp, lnum, TRUE);
819 if (lnum++ == wp->w_topline)
820 n = adjust_plines_for_skipcol(wp, n);
821 wp->w_cline_row += n;
822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
824 }
825
826 check_cursor_moved(wp);
827 if (!(wp->w_valid & VALID_CHEIGHT))
828 {
829 if (all_invalid
830 || i == wp->w_lines_valid
831 || (i < wp->w_lines_valid
832 && (!wp->w_lines[i].wl_valid
833 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
834 {
835#ifdef FEAT_DIFF
836 if (wp->w_cursor.lnum == wp->w_topline)
837 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
838 TRUE) + wp->w_topfill;
839 else
840#endif
841 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
842#ifdef FEAT_FOLDING
843 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
844 NULL, NULL, TRUE, NULL);
845#endif
846 }
847 else if (i > wp->w_lines_valid)
848 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100849 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 wp->w_cline_height = 0;
851#ifdef FEAT_FOLDING
852 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
853 NULL, NULL, TRUE, NULL);
854#endif
855 }
856 else
857 {
858 wp->w_cline_height = wp->w_lines[i].wl_size;
859#ifdef FEAT_FOLDING
860 wp->w_cline_folded = wp->w_lines[i].wl_folded;
861#endif
862 }
863 }
864
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100865 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
867
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868}
869
870/*
871 * Validate curwin->w_virtcol only.
872 */
873 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100874validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
876 validate_virtcol_win(curwin);
877}
878
879/*
880 * Validate wp->w_virtcol only.
881 */
882 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100883validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 check_cursor_moved(wp);
886 if (!(wp->w_valid & VALID_VIRTCOL))
887 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100888#ifdef FEAT_PROP_POPUP
889 wp->w_virtcol_first_char = 0;
890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000892#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100893 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000894#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100895 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897}
898
899/*
900 * Validate curwin->w_cline_height only.
901 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100902 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100903validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 check_cursor_moved(curwin);
906 if (!(curwin->w_valid & VALID_CHEIGHT))
907 {
908#ifdef FEAT_DIFF
909 if (curwin->w_cursor.lnum == curwin->w_topline)
910 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
911 + curwin->w_topfill;
912 else
913#endif
914 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
915#ifdef FEAT_FOLDING
916 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
917#endif
918 curwin->w_valid |= VALID_CHEIGHT;
919 }
920}
921
922/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000923 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 */
925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 colnr_T off;
929 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100930 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 validate_virtcol();
933 if (!(curwin->w_valid & VALID_WCOL))
934 {
935 col = curwin->w_virtcol;
936 off = curwin_col_off();
937 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200938 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
Bram Moolenaar85a20022019-12-21 18:25:54 +0100940 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000941 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200942 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100943 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100944 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200945 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000946 if (col > (int)curwin->w_leftcol)
947 col -= curwin->w_leftcol;
948 else
949 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100953#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100954 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957}
958
959/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200960 * Compute offset of a window, occupied by absolute or relative line number,
961 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 */
963 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100964win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
Bram Moolenaar64486672010-05-16 15:46:46 +0200966 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef FEAT_CMDWIN
968 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
969#endif
970#ifdef FEAT_FOLDING
971 + wp->w_p_fdc
972#endif
973#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200974 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975#endif
976 );
977}
978
979 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100980curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
982 return win_col_off(curwin);
983}
984
985/*
986 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +0100987 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
988 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 */
990 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100991win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaar64486672010-05-16 15:46:46 +0200993 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000994 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 return 0;
996}
997
998 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100999curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
1001 return win_col_off2(curwin);
1002}
1003
1004/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001005 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Also updates curwin->w_wrow and curwin->w_cline_row.
1007 * Also updates curwin->w_leftcol.
1008 */
1009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001010curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001011 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012{
1013 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001014 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 int off_left, off_right;
1016 int n;
1017 int p_lines;
1018 int width = 0;
1019 int textwidth;
1020 int new_leftcol;
1021 colnr_T startcol;
1022 colnr_T endcol;
1023 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001024 long so = get_scrolloff_value();
1025 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 /*
1028 * First make sure that w_topline is valid (after moving the cursor).
1029 */
Luuk van Baalfaf1d412022-09-19 16:45:29 +01001030 if (!skip_update_topline)
Luuk van Baal29ab5242022-09-11 16:59:53 +01001031 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 /*
1034 * Next make sure that w_cline_row is valid.
1035 */
1036 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001037 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001039#ifdef FEAT_PROP_POPUP
1040 // will be set by getvvcol() but not reset
1041 curwin->w_virtcol_first_char = 0;
1042#endif
1043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 /*
1045 * Compute the number of virtual columns.
1046 */
1047#ifdef FEAT_FOLDING
1048 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001049 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1051 else
1052#endif
1053 getvvcol(curwin, &curwin->w_cursor,
1054 &startcol, &(curwin->w_virtcol), &endcol);
1055
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001058 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 extra = curwin_col_off();
1061 curwin->w_wcol = curwin->w_virtcol + extra;
1062 endcol += extra;
1063
1064 /*
1065 * Now compute w_wrow, counting screen lines from w_cline_row.
1066 */
1067 curwin->w_wrow = curwin->w_cline_row;
1068
Bram Moolenaar02631462017-09-22 15:20:32 +02001069 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (textwidth <= 0)
1071 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001072 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001073 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001074 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001075 if (curwin->w_p_wrap)
1076 curwin->w_wrow = curwin->w_height - 1;
1077 else
1078 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001080 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 width = textwidth + curwin_col_off2();
1083
Bram Moolenaar85a20022019-12-21 18:25:54 +01001084 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001085 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001087#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001088 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001089#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001090
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001092 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 curwin->w_wcol -= n * width;
1094 curwin->w_wrow += n;
1095
1096#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001097 // When cursor wraps to first char of next line in Insert
1098 // mode, the 'showbreak' string isn't shown, backup to first
1099 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001100 sbr = get_showbreak_value(curwin);
1101 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001102 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 curwin->w_wcol = 0;
1104#endif
1105 }
1106 }
1107
Bram Moolenaar85a20022019-12-21 18:25:54 +01001108 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1109 // is not folded.
1110 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001111 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#ifdef FEAT_FOLDING
1113 && !curwin->w_cline_folded
1114#endif
1115 )
1116 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001117#ifdef FEAT_PROP_POPUP
1118 if (curwin->w_virtcol_first_char > 0)
1119 {
1120 int cols = (curwin->w_width - extra);
1121 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1122
1123 // each "above" text prop shifts the text one row down
1124 curwin->w_wrow += rows;
1125 curwin->w_wcol -= rows * cols;
1126 endcol -= rows * cols;
1127 curwin->w_cline_height = rows + 1;
1128 }
1129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 /*
1131 * If Cursor is left of the screen, scroll rightwards.
1132 * If Cursor is right of the screen, scroll leftwards
1133 * If we get closer to the edge than 'sidescrolloff', scroll a little
1134 * extra
1135 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001136 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001137 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001138 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 if (off_left < 0 || off_right > 0)
1140 {
1141 if (off_left < 0)
1142 diff = -off_left;
1143 else
1144 diff = off_right;
1145
Bram Moolenaar85a20022019-12-21 18:25:54 +01001146 // When far off or not enough room on either side, put cursor in
1147 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1149 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1150 else
1151 {
1152 if (diff < p_ss)
1153 diff = p_ss;
1154 if (off_left < 0)
1155 new_leftcol = curwin->w_leftcol - diff;
1156 else
1157 new_leftcol = curwin->w_leftcol + diff;
1158 }
1159 if (new_leftcol < 0)
1160 new_leftcol = 0;
1161 if (new_leftcol != (int)curwin->w_leftcol)
1162 {
1163 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001164 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001165 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 }
1168 curwin->w_wcol -= curwin->w_leftcol;
1169 }
1170 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1171 curwin->w_wcol -= curwin->w_leftcol;
1172 else
1173 curwin->w_wcol = 0;
1174
1175#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001176 // Skip over filler lines. At the top use w_topfill, there
1177 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 if (curwin->w_cursor.lnum == curwin->w_topline)
1179 curwin->w_wrow += curwin->w_topfill;
1180 else
1181 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1182#endif
1183
1184 prev_skipcol = curwin->w_skipcol;
1185
1186 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if ((curwin->w_wrow >= curwin->w_height
1189 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001190 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 && (p_lines =
1192#ifdef FEAT_DIFF
1193 plines_win_nofill
1194#else
1195 plines_win
1196#endif
1197 (curwin, curwin->w_cursor.lnum, FALSE))
1198 - 1 >= curwin->w_height))
1199 && curwin->w_height != 0
1200 && curwin->w_cursor.lnum == curwin->w_topline
1201 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001202 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001204 // Cursor past end of screen. Happens with a single line that does
1205 // not fit on screen. Find a skipcol to show the text around the
1206 // cursor. Avoid scrolling all the time. compute value of "extra":
1207 // 1: Less than 'scrolloff' lines above
1208 // 2: Less than 'scrolloff' lines below
1209 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001211 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001213 // Compute last display line of the buffer line that we want at the
1214 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (p_lines == 0)
1216 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1217 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001218 if (p_lines > curwin->w_wrow + so)
1219 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else
1221 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001222 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 extra += 2;
1224
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001225 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001227 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 n = curwin->w_virtcol / width;
1229 if (n > curwin->w_height / 2)
1230 n -= curwin->w_height / 2;
1231 else
1232 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001233 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (n > p_lines - curwin->w_height + 1)
1235 n = p_lines - curwin->w_height + 1;
1236 curwin->w_skipcol = n * width;
1237 }
1238 else if (extra == 1)
1239 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001240 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001241 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 + width - 1) / width;
1243 if (extra > 0)
1244 {
1245 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1246 extra = curwin->w_skipcol / width;
1247 curwin->w_skipcol -= extra * width;
1248 }
1249 }
1250 else if (extra == 2)
1251 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001252 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 endcol = (n - curwin->w_height + 1) * width;
1254 while (endcol > curwin->w_virtcol)
1255 endcol -= width;
1256 if (endcol > curwin->w_skipcol)
1257 curwin->w_skipcol = endcol;
1258 }
1259
1260 curwin->w_wrow -= curwin->w_skipcol / width;
1261 if (curwin->w_wrow >= curwin->w_height)
1262 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001263 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 extra = curwin->w_wrow - curwin->w_height + 1;
1265 curwin->w_skipcol += extra * width;
1266 curwin->w_wrow -= extra;
1267 }
1268
1269 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1270 if (extra > 0)
1271 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1272 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001273 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001275 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 curwin->w_skipcol = 0;
1277 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001278 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001280#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001281 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001282#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001283#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1284 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1285 {
1286 curwin->w_wrow += popup_top_extra(curwin);
1287 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001288 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001289 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001290 else
1291 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001292#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001293
Bram Moolenaar08f23632019-11-16 14:19:33 +01001294 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1295 curwin->w_valid_leftcol = curwin->w_leftcol;
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1298}
1299
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001300#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001301/*
1302 * Compute the screen position of text character at "pos" in window "wp"
1303 * The resulting values are one-based, zero when character is not visible.
1304 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001305 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001306textpos2screenpos(
1307 win_T *wp,
1308 pos_T *pos,
1309 int *rowp, // screen row
1310 int *scolp, // start screen column
1311 int *ccolp, // cursor screen column
1312 int *ecolp) // end screen column
1313{
1314 colnr_T scol = 0, ccol = 0, ecol = 0;
1315 int row = 0;
1316 int rowoff = 0;
1317 colnr_T coloff = 0;
1318
Bram Moolenaar189663b2021-07-21 18:04:56 +02001319 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001320 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001321 colnr_T off;
1322 colnr_T col;
1323 int width;
1324 linenr_T lnum = pos->lnum;
1325#ifdef FEAT_FOLDING
1326 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001327
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001328 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1329#endif
1330 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1331#ifdef FEAT_FOLDING
1332 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001333 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001334 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001335 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001336 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001337 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001338#endif
1339 {
1340 getvcol(wp, pos, &scol, &ccol, &ecol);
1341
1342 // similar to what is done in validate_cursor_col()
1343 col = scol;
1344 off = win_col_off(wp);
1345 col += off;
1346 width = wp->w_width - off + win_col_off2(wp);
1347
1348 // long line wrapping, adjust row
1349 if (wp->w_p_wrap
1350 && col >= (colnr_T)wp->w_width
1351 && width > 0)
1352 {
1353 // use same formula as what is used in curs_columns()
1354 rowoff = ((col - wp->w_width) / width + 1);
1355 col -= rowoff * width;
1356 }
1357 col -= wp->w_leftcol;
1358 if (col >= wp->w_width)
1359 col = -1;
1360 if (col >= 0 && row + rowoff <= wp->w_height)
1361 {
1362 coloff = col - scol + wp->w_wincol + 1;
1363 row += W_WINROW(wp);
1364 }
1365 else
1366 // character is left, right or below of the window
1367 row = rowoff = scol = ccol = ecol = 0;
1368 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001369 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001370 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001371 *scolp = scol + coloff;
1372 *ccolp = ccol + coloff;
1373 *ecolp = ecol + coloff;
1374}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001375#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001376
Bram Moolenaar12034e22019-08-25 22:25:02 +02001377#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001378/*
1379 * "screenpos({winid}, {lnum}, {col})" function
1380 */
1381 void
1382f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1383{
1384 dict_T *dict;
1385 win_T *wp;
1386 pos_T pos;
1387 int row = 0;
1388 int scol = 0, ccol = 0, ecol = 0;
1389
Bram Moolenaar93a10962022-06-16 11:42:09 +01001390 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001391 return;
1392 dict = rettv->vval.v_dict;
1393
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001394 if (in_vim9script()
1395 && (check_for_number_arg(argvars, 0) == FAIL
1396 || check_for_number_arg(argvars, 1) == FAIL
1397 || check_for_number_arg(argvars, 2) == FAIL))
1398 return;
1399
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001400 wp = find_win_by_nr_or_id(&argvars[0]);
1401 if (wp == NULL)
1402 return;
1403
1404 pos.lnum = tv_get_number(&argvars[1]);
1405 pos.col = tv_get_number(&argvars[2]) - 1;
1406 pos.coladd = 0;
1407 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1408
1409 dict_add_number(dict, "row", row);
1410 dict_add_number(dict, "col", scol);
1411 dict_add_number(dict, "curscol", ccol);
1412 dict_add_number(dict, "endcol", ecol);
1413}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001414
1415/*
1416 * "virtcol2col({winid}, {lnum}, {col})" function
1417 */
1418 void
1419f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1420{
1421 win_T *wp;
1422 linenr_T lnum;
1423 int screencol;
1424 int error = FALSE;
1425
1426 rettv->vval.v_number = -1;
1427
1428 if (check_for_number_arg(argvars, 0) == FAIL
1429 || check_for_number_arg(argvars, 1) == FAIL
1430 || check_for_number_arg(argvars, 2) == FAIL)
1431 return;
1432
1433 wp = find_win_by_nr_or_id(&argvars[0]);
1434 if (wp == NULL)
1435 return;
1436
1437 lnum = tv_get_number_chk(&argvars[1], &error);
1438 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1439 return;
1440
1441 screencol = tv_get_number_chk(&argvars[2], &error);
1442 if (error || screencol < 0)
1443 return;
1444
1445 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1446}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001447#endif
1448
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449/*
1450 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001453scrolldown(
1454 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001455 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001457 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 int wrow;
1459 int moved = FALSE;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001460 int width1 = 0;
1461 int width2 = 0;
1462
1463 if (curwin->w_p_wrap && curwin->w_p_sms)
1464 {
1465 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001466 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
1469#ifdef FEAT_FOLDING
1470 linenr_T first;
1471
Bram Moolenaar85a20022019-12-21 18:25:54 +01001472 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1474#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001475 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 while (line_count-- > 0)
1477 {
1478#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001479 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1480 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
1482 ++curwin->w_topfill;
1483 ++done;
1484 }
1485 else
1486#endif
1487 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001488 // break when at the very top
1489 if (curwin->w_topline == 1
1490 && (!curwin->w_p_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 break;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001492 if (curwin->w_p_wrap && curwin->w_p_sms
Bram Moolenaar8df97482022-10-03 12:11:13 +01001493 && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001495 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001496 if (curwin->w_skipcol >= width1 + width2)
1497 curwin->w_skipcol -= width2;
1498 else
1499 curwin->w_skipcol -= width1;
1500 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
1503 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001504 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001505 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001506 --curwin->w_topline;
1507 curwin->w_skipcol = 0;
1508#ifdef FEAT_DIFF
1509 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001511#ifdef FEAT_FOLDING
1512 // A sequence of folded lines only counts for one logical line
1513 if (hasFolding(curwin->w_topline, &first, NULL))
1514 {
1515 ++done;
1516 if (!byfold)
1517 line_count -= curwin->w_topline - first - 1;
1518 curwin->w_botline -= curwin->w_topline - first;
1519 curwin->w_topline = first;
1520 }
1521 else
1522#endif
1523 if (curwin->w_p_wrap && curwin->w_p_sms)
1524 {
1525 int size = win_linetabsize(curwin, curwin->w_topline,
1526 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1527 if (size > width1)
1528 {
1529 curwin->w_skipcol = width1;
1530 size -= width1;
1531 redraw_later(UPD_NOT_VALID);
1532 }
1533 while (size > width2)
1534 {
1535 curwin->w_skipcol += width2;
1536 size -= width2;
1537 }
1538 ++done;
1539 }
1540 else
1541 done += PLINES_NOFILL(curwin->w_topline);
1542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001544 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 invalidate_botline();
1546 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001547 curwin->w_wrow += done; // keep w_wrow updated
1548 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
1550#ifdef FEAT_DIFF
1551 if (curwin->w_cursor.lnum == curwin->w_topline)
1552 curwin->w_cline_row = 0;
1553 check_topfill(curwin, TRUE);
1554#endif
1555
1556 /*
1557 * Compute the row number of the last row of the cursor line
1558 * and move the cursor onto the displayed part of the window.
1559 */
1560 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001561 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563 validate_virtcol();
1564 validate_cheight();
1565 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001566 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1569 {
1570#ifdef FEAT_FOLDING
1571 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1572 {
1573 --wrow;
1574 if (first == 1)
1575 curwin->w_cursor.lnum = 1;
1576 else
1577 curwin->w_cursor.lnum = first - 1;
1578 }
1579 else
1580#endif
1581 wrow -= plines(curwin->w_cursor.lnum--);
1582 curwin->w_valid &=
1583 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1584 moved = TRUE;
1585 }
1586 if (moved)
1587 {
1588#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001589 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 foldAdjustCursor();
1591#endif
1592 coladvance(curwin->w_curswant);
1593 }
1594}
1595
1596/*
1597 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001600scrollup(
1601 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001602 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001604 int do_smoothscroll = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001606 if (do_smoothscroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607# ifdef FEAT_FOLDING
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001608 || (byfold && hasAnyFolding(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609# endif
1610# ifdef FEAT_DIFF
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001611 || curwin->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612# endif
1613 )
1614 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001615 int width1 = curwin->w_width - curwin_col_off();
1616 int width2 = width1 + curwin_col_off2();
1617 int size = 0;
1618 linenr_T prev_topline = curwin->w_topline;
1619
1620 if (do_smoothscroll)
1621 size = win_linetabsize(curwin, curwin->w_topline,
1622 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1623
1624 // diff mode: first consume "topfill"
1625 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1626 // the line, then advance to the next line.
1627 // folding: count each sequence of folded lines as one logical line.
1628 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630# ifdef FEAT_DIFF
1631 if (curwin->w_topfill > 0)
1632 --curwin->w_topfill;
1633 else
1634# endif
1635 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001636 linenr_T lnum = curwin->w_topline;
1637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638# ifdef FEAT_FOLDING
1639 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001640 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 (void)hasFolding(lnum, NULL, &lnum);
1642# endif
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001643 if (lnum == curwin->w_topline
1644 && curwin->w_p_wrap && curwin->w_p_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001645 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001646 // 'smoothscroll': increase "w_skipcol" until it goes over
1647 // the end of the line, then advance to the next line.
1648 int add = curwin->w_skipcol > 0 ? width2 : width1;
1649 curwin->w_skipcol += add;
1650 if (curwin->w_skipcol >= size)
1651 {
1652 if (lnum == curbuf->b_ml.ml_line_count)
1653 {
1654 // at the last screen line, can't scroll further
1655 curwin->w_skipcol -= add;
1656 break;
1657 }
1658 ++lnum;
1659 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001660 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001661 else
1662 {
1663 if (lnum >= curbuf->b_ml.ml_line_count)
1664 break;
1665 ++lnum;
1666 }
1667
1668 if (lnum > curwin->w_topline)
1669 {
1670 // approximate w_botline
1671 curwin->w_botline += lnum - curwin->w_topline;
1672 curwin->w_topline = lnum;
1673# ifdef FEAT_DIFF
1674 curwin->w_topfill = diff_check_fill(curwin, lnum);
1675# endif
1676 curwin->w_skipcol = 0;
1677 if (todo > 1 && do_smoothscroll)
1678 size = win_linetabsize(curwin, curwin->w_topline,
1679 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1680 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001681 }
1682 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001683
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001684 if (curwin->w_topline == prev_topline)
1685 // need to redraw even though w_topline didn't change
1686 redraw_later(UPD_NOT_VALID);
1687 }
1688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001691 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
1693
1694 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1695 curwin->w_topline = curbuf->b_ml.ml_line_count;
1696 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1697 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1698
1699#ifdef FEAT_DIFF
1700 check_topfill(curwin, FALSE);
1701#endif
1702
1703#ifdef FEAT_FOLDING
1704 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001705 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1707#endif
1708
1709 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1710 if (curwin->w_cursor.lnum < curwin->w_topline)
1711 {
1712 curwin->w_cursor.lnum = curwin->w_topline;
1713 curwin->w_valid &=
1714 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1715 coladvance(curwin->w_curswant);
1716 }
1717}
1718
1719#ifdef FEAT_DIFF
1720/*
1721 * Don't end up with too many filler lines in the window.
1722 */
1723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001724check_topfill(
1725 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001726 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 int n;
1729
1730 if (wp->w_topfill > 0)
1731 {
1732 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1733 if (wp->w_topfill + n > wp->w_height)
1734 {
1735 if (down && wp->w_topline > 1)
1736 {
1737 --wp->w_topline;
1738 wp->w_topfill = 0;
1739 }
1740 else
1741 {
1742 wp->w_topfill = wp->w_height - n;
1743 if (wp->w_topfill < 0)
1744 wp->w_topfill = 0;
1745 }
1746 }
1747 }
1748}
1749
1750/*
1751 * Use as many filler lines as possible for w_topline. Make sure w_topline
1752 * is still visible.
1753 */
1754 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001755max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 int n;
1758
1759 n = plines_nofill(curwin->w_topline);
1760 if (n >= curwin->w_height)
1761 curwin->w_topfill = 0;
1762 else
1763 {
1764 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1765 if (curwin->w_topfill + n > curwin->w_height)
1766 curwin->w_topfill = curwin->w_height - n;
1767 }
1768}
1769#endif
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771/*
1772 * Scroll the screen one line down, but don't do it if it would move the
1773 * cursor off the screen.
1774 */
1775 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001776scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 int end_row;
1779#ifdef FEAT_DIFF
1780 int can_fill = (curwin->w_topfill
1781 < diff_check_fill(curwin, curwin->w_topline));
1782#endif
1783
1784 if (curwin->w_topline <= 1
1785#ifdef FEAT_DIFF
1786 && !can_fill
1787#endif
1788 )
1789 return;
1790
Bram Moolenaar85a20022019-12-21 18:25:54 +01001791 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793 /*
1794 * Compute the row number of the last row of the cursor line
1795 * and make sure it doesn't go off the screen. Make sure the cursor
1796 * doesn't go past 'scrolloff' lines from the screen end.
1797 */
1798 end_row = curwin->w_wrow;
1799#ifdef FEAT_DIFF
1800 if (can_fill)
1801 ++end_row;
1802 else
1803 end_row += plines_nofill(curwin->w_topline - 1);
1804#else
1805 end_row += plines(curwin->w_topline - 1);
1806#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001807 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
1809 validate_cheight();
1810 validate_virtcol();
1811 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001812 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001814 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
1816#ifdef FEAT_DIFF
1817 if (can_fill)
1818 {
1819 ++curwin->w_topfill;
1820 check_topfill(curwin, TRUE);
1821 }
1822 else
1823 {
1824 --curwin->w_topline;
1825 curwin->w_topfill = 0;
1826 }
1827#else
1828 --curwin->w_topline;
1829#endif
1830#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001831 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001833 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1835 }
1836}
1837
1838/*
1839 * Scroll the screen one line up, but don't do it if it would move the cursor
1840 * off the screen.
1841 */
1842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001843scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
1845 int start_row;
1846
1847 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1848#ifdef FEAT_DIFF
1849 && curwin->w_topfill == 0
1850#endif
1851 )
1852 return;
1853
Bram Moolenaar85a20022019-12-21 18:25:54 +01001854 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856 /*
1857 * Compute the row number of the first row of the cursor line
1858 * and make sure it doesn't go off the screen. Make sure the cursor
1859 * doesn't go before 'scrolloff' lines from the screen start.
1860 */
1861#ifdef FEAT_DIFF
1862 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1863 - curwin->w_topfill;
1864#else
1865 start_row = curwin->w_wrow - plines(curwin->w_topline);
1866#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001867 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
1869 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001870 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001872 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
1874#ifdef FEAT_DIFF
1875 if (curwin->w_topfill > 0)
1876 --curwin->w_topfill;
1877 else
1878#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001879 {
1880#ifdef FEAT_FOLDING
1881 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001884 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001885 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1887 }
1888}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890/*
1891 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1892 * a (wrapped) text line. Uses and sets "lp->fill".
1893 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001894 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 */
1896 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001897topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899#ifdef FEAT_DIFF
1900 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1901 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001902 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 ++lp->fill;
1904 lp->height = 1;
1905 }
1906 else
1907#endif
1908 {
1909 --lp->lnum;
1910#ifdef FEAT_DIFF
1911 lp->fill = 0;
1912#endif
1913 if (lp->lnum < 1)
1914 lp->height = MAXCOL;
1915 else
1916#ifdef FEAT_FOLDING
1917 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001918 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 lp->height = 1;
1920 else
1921#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001922 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924}
1925
1926/*
1927 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1928 * a (wrapped) text line. Uses and sets "lp->fill".
1929 * Returns the height of the added line in "lp->height".
1930 * Lines below the last one are incredibly high.
1931 */
1932 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001933botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
1935#ifdef FEAT_DIFF
1936 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1937 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001938 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 ++lp->fill;
1940 lp->height = 1;
1941 }
1942 else
1943#endif
1944 {
1945 ++lp->lnum;
1946#ifdef FEAT_DIFF
1947 lp->fill = 0;
1948#endif
1949 if (lp->lnum > curbuf->b_ml.ml_line_count)
1950 lp->height = MAXCOL;
1951 else
1952#ifdef FEAT_FOLDING
1953 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001954 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 lp->height = 1;
1956 else
1957#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001958 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960}
1961
1962#ifdef FEAT_DIFF
1963/*
1964 * Switch from including filler lines below lp->lnum to including filler
1965 * lines above loff.lnum + 1. This keeps pointing to the same line.
1966 * When there are no filler lines nothing changes.
1967 */
1968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001969botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
1971 if (lp->fill > 0)
1972 {
1973 ++lp->lnum;
1974 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1975 }
1976}
1977
1978/*
1979 * Switch from including filler lines above lp->lnum to including filler
1980 * lines below loff.lnum - 1. This keeps pointing to the same line.
1981 * When there are no filler lines nothing changes.
1982 */
1983 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001984topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
1986 if (lp->fill > 0)
1987 {
1988 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1989 --lp->lnum;
1990 }
1991}
1992#endif
1993
1994/*
1995 * Recompute topline to put the cursor at the top of the window.
1996 * Scroll at least "min_scroll" lines.
1997 * If "always" is TRUE, always set topline (for "zt").
1998 */
1999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002000scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002 int scrolled = 0;
2003 int extra = 0;
2004 int used;
2005 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002006 linenr_T top; // just above displayed lines
2007 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 linenr_T old_topline = curwin->w_topline;
2009#ifdef FEAT_DIFF
2010 linenr_T old_topfill = curwin->w_topfill;
2011#endif
2012 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002013 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (mouse_dragging > 0)
2016 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 /*
2019 * Decrease topline until:
2020 * - it has become 1
2021 * - (part of) the cursor line is moved off the screen or
2022 * - moved at least 'scrolljump' lines and
2023 * - at least 'scrolloff' lines above and below the cursor
2024 */
2025 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002026 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (curwin->w_cursor.lnum < curwin->w_topline)
2028 scrolled = used;
2029
2030#ifdef FEAT_FOLDING
2031 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2032 {
2033 --top;
2034 ++bot;
2035 }
2036 else
2037#endif
2038 {
2039 top = curwin->w_cursor.lnum - 1;
2040 bot = curwin->w_cursor.lnum + 1;
2041 }
2042 new_topline = top + 1;
2043
2044#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002045 // "used" already contains the number of filler lines above, don't add it
2046 // again.
2047 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002048 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049#endif
2050
2051 /*
2052 * Check if the lines from "top" to "bot" fit in the window. If they do,
2053 * set new_topline and advance "top" and "bot" to include more lines.
2054 */
2055 while (top > 0)
2056 {
2057#ifdef FEAT_FOLDING
2058 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002059 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 i = 1;
2061 else
2062#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002063 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 used += i;
2065 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2066 {
2067#ifdef FEAT_FOLDING
2068 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002069 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 ++used;
2071 else
2072#endif
2073 used += plines(bot);
2074 }
2075 if (used > curwin->w_height)
2076 break;
2077 if (top < curwin->w_topline)
2078 scrolled += i;
2079
2080 /*
2081 * If scrolling is needed, scroll at least 'sj' lines.
2082 */
2083 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2084 && extra >= off)
2085 break;
2086
2087 extra += i;
2088 new_topline = top;
2089 --top;
2090 ++bot;
2091 }
2092
2093 /*
2094 * If we don't have enough space, put cursor in the middle.
2095 * This makes sure we get the same position when using "k" and "j"
2096 * in a small window.
2097 */
2098 if (used > curwin->w_height)
2099 scroll_cursor_halfway(FALSE);
2100 else
2101 {
2102 /*
2103 * If "always" is FALSE, only adjust topline to a lower value, higher
2104 * value may happen with wrapping lines
2105 */
2106 if (new_topline < curwin->w_topline || always)
2107 curwin->w_topline = new_topline;
2108 if (curwin->w_topline > curwin->w_cursor.lnum)
2109 curwin->w_topline = curwin->w_cursor.lnum;
2110#ifdef FEAT_DIFF
2111 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2112 if (curwin->w_topfill > 0 && extra > off)
2113 {
2114 curwin->w_topfill -= extra - off;
2115 if (curwin->w_topfill < 0)
2116 curwin->w_topfill = 0;
2117 }
2118 check_topfill(curwin, FALSE);
2119#endif
2120 if (curwin->w_topline != old_topline
2121#ifdef FEAT_DIFF
2122 || curwin->w_topfill != old_topfill
2123#endif
2124 )
2125 curwin->w_valid &=
2126 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2127 curwin->w_valid |= VALID_TOPLINE;
2128 }
2129}
2130
2131/*
2132 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2133 * screen lines for text lines.
2134 */
2135 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002136set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138#ifdef FEAT_DIFF
2139 wp->w_filler_rows = 0;
2140#endif
2141 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002142 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 else
2144 {
2145 wp->w_empty_rows = wp->w_height - used;
2146#ifdef FEAT_DIFF
2147 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2148 {
2149 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2150 if (wp->w_empty_rows > wp->w_filler_rows)
2151 wp->w_empty_rows -= wp->w_filler_rows;
2152 else
2153 {
2154 wp->w_filler_rows = wp->w_empty_rows;
2155 wp->w_empty_rows = 0;
2156 }
2157 }
2158#endif
2159 }
2160}
2161
2162/*
2163 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002164 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2166 * This is messy stuff!!!
2167 */
2168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002169scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170{
2171 int used;
2172 int scrolled = 0;
2173 int extra = 0;
2174 int i;
2175 linenr_T line_count;
2176 linenr_T old_topline = curwin->w_topline;
2177 lineoff_T loff;
2178 lineoff_T boff;
2179#ifdef FEAT_DIFF
2180 int old_topfill = curwin->w_topfill;
2181 int fill_below_window;
2182#endif
2183 linenr_T old_botline = curwin->w_botline;
2184 linenr_T old_valid = curwin->w_valid;
2185 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002186 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002187 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 cln = curwin->w_cursor.lnum;
2190 if (set_topbot)
2191 {
2192 used = 0;
2193 curwin->w_botline = cln + 1;
2194#ifdef FEAT_DIFF
2195 loff.fill = 0;
2196#endif
2197 for (curwin->w_topline = curwin->w_botline;
2198 curwin->w_topline > 1;
2199 curwin->w_topline = loff.lnum)
2200 {
2201 loff.lnum = curwin->w_topline;
2202 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002203 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 break;
2205 used += loff.height;
2206#ifdef FEAT_DIFF
2207 curwin->w_topfill = loff.fill;
2208#endif
2209 }
2210 set_empty_rows(curwin, used);
2211 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2212 if (curwin->w_topline != old_topline
2213#ifdef FEAT_DIFF
2214 || curwin->w_topfill != old_topfill
2215#endif
2216 )
2217 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2218 }
2219 else
2220 validate_botline();
2221
Bram Moolenaar85a20022019-12-21 18:25:54 +01002222 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223#ifdef FEAT_DIFF
2224 used = plines_nofill(cln);
2225#else
2226 validate_cheight();
2227 used = curwin->w_cline_height;
2228#endif
2229
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 // If the cursor is below botline, we will at least scroll by the height
2231 // of the cursor line. Correct for empty lines, which are really part of
2232 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (cln >= curwin->w_botline)
2234 {
2235 scrolled = used;
2236 if (cln == curwin->w_botline)
2237 scrolled -= curwin->w_empty_rows;
2238 }
2239
2240 /*
2241 * Stop counting lines to scroll when
2242 * - hitting start of the file
2243 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002244 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 * - lines between botline and cursor have been counted
2246 */
2247#ifdef FEAT_FOLDING
2248 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2249#endif
2250 {
2251 loff.lnum = cln;
2252 boff.lnum = cln;
2253 }
2254#ifdef FEAT_DIFF
2255 loff.fill = 0;
2256 boff.fill = 0;
2257 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2258 - curwin->w_filler_rows;
2259#endif
2260
2261 while (loff.lnum > 1)
2262 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2264 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002266 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2268 && loff.lnum <= curwin->w_botline
2269#ifdef FEAT_DIFF
2270 && (loff.lnum < curwin->w_botline
2271 || loff.fill >= fill_below_window)
2272#endif
2273 )
2274 break;
2275
Bram Moolenaar85a20022019-12-21 18:25:54 +01002276 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002278 if (loff.height == MAXCOL)
2279 used = MAXCOL;
2280 else
2281 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (used > curwin->w_height)
2283 break;
2284 if (loff.lnum >= curwin->w_botline
2285#ifdef FEAT_DIFF
2286 && (loff.lnum > curwin->w_botline
2287 || loff.fill <= fill_below_window)
2288#endif
2289 )
2290 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002291 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 scrolled += loff.height;
2293 if (loff.lnum == curwin->w_botline
2294#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002295 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296#endif
2297 )
2298 scrolled -= curwin->w_empty_rows;
2299 }
2300
2301 if (boff.lnum < curbuf->b_ml.ml_line_count)
2302 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002303 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 botline_forw(&boff);
2305 used += boff.height;
2306 if (used > curwin->w_height)
2307 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002308 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2309 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 extra += boff.height;
2312 if (boff.lnum >= curwin->w_botline
2313#ifdef FEAT_DIFF
2314 || (boff.lnum + 1 == curwin->w_botline
2315 && boff.fill > curwin->w_filler_rows)
2316#endif
2317 )
2318 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002319 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 scrolled += boff.height;
2321 if (boff.lnum == curwin->w_botline
2322#ifdef FEAT_DIFF
2323 && boff.fill == 0
2324#endif
2325 )
2326 scrolled -= curwin->w_empty_rows;
2327 }
2328 }
2329 }
2330 }
2331
Bram Moolenaar85a20022019-12-21 18:25:54 +01002332 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (scrolled <= 0)
2334 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002335 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 else if (used > curwin->w_height)
2337 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002338 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
2340 {
2341 line_count = 0;
2342#ifdef FEAT_DIFF
2343 boff.fill = curwin->w_topfill;
2344#endif
2345 boff.lnum = curwin->w_topline - 1;
2346 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2347 {
2348 botline_forw(&boff);
2349 i += boff.height;
2350 ++line_count;
2351 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002352 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 line_count = 9999;
2354 }
2355
2356 /*
2357 * Scroll up if the cursor is off the bottom of the screen a bit.
2358 * Otherwise put it at 1/2 of the screen.
2359 */
2360 if (line_count >= curwin->w_height && line_count > min_scroll)
2361 scroll_cursor_halfway(FALSE);
2362 else
2363 scrollup(line_count, TRUE);
2364
2365 /*
2366 * If topline didn't change we need to restore w_botline and w_empty_rows
2367 * (we changed them).
2368 * If topline did change, update_screen() will set botline.
2369 */
2370 if (curwin->w_topline == old_topline && set_topbot)
2371 {
2372 curwin->w_botline = old_botline;
2373 curwin->w_empty_rows = old_empty_rows;
2374 curwin->w_valid = old_valid;
2375 }
2376 curwin->w_valid |= VALID_TOPLINE;
2377}
2378
2379/*
2380 * Recompute topline to put the cursor halfway the window
2381 * If "atend" is TRUE, also put it halfway at the end of the file.
2382 */
2383 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002384scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 int above = 0;
2387 linenr_T topline;
2388#ifdef FEAT_DIFF
2389 int topfill = 0;
2390#endif
2391 int below = 0;
2392 int used;
2393 lineoff_T loff;
2394 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002395#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002396 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002399#ifdef FEAT_PROP_POPUP
2400 // if the width changed this needs to be updated first
2401 may_update_popup_position();
2402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2404#ifdef FEAT_FOLDING
2405 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2406#endif
2407#ifdef FEAT_DIFF
2408 used = plines_nofill(loff.lnum);
2409 loff.fill = 0;
2410 boff.fill = 0;
2411#else
2412 used = plines(loff.lnum);
2413#endif
2414 topline = loff.lnum;
2415 while (topline > 1)
2416 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002417 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419 if (boff.lnum < curbuf->b_ml.ml_line_count)
2420 {
2421 botline_forw(&boff);
2422 used += boff.height;
2423 if (used > curwin->w_height)
2424 break;
2425 below += boff.height;
2426 }
2427 else
2428 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002429 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (atend)
2431 ++used;
2432 }
2433 }
2434
Bram Moolenaar85a20022019-12-21 18:25:54 +01002435 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002438 if (loff.height == MAXCOL)
2439 used = MAXCOL;
2440 else
2441 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 if (used > curwin->w_height)
2443 break;
2444 above += loff.height;
2445 topline = loff.lnum;
2446#ifdef FEAT_DIFF
2447 topfill = loff.fill;
2448#endif
2449 }
2450 }
2451#ifdef FEAT_FOLDING
2452 if (!hasFolding(topline, &curwin->w_topline, NULL))
2453#endif
2454 curwin->w_topline = topline;
2455#ifdef FEAT_DIFF
2456 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002457 if (old_topline > curwin->w_topline + curwin->w_height)
2458 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 check_topfill(curwin, FALSE);
2460#endif
2461 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2462 curwin->w_valid |= VALID_TOPLINE;
2463}
2464
2465/*
2466 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002467 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 * If not possible, put it at the same position as scroll_cursor_halfway().
2469 * When called topline must be valid!
2470 */
2471 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002472cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002474 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002476 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 linenr_T botline;
2478 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002479 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002481 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 /*
2484 * How many lines we would like to have above/below the cursor depends on
2485 * whether the first/last line of the file is on screen.
2486 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002487 above_wanted = so;
2488 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002489 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 above_wanted = mouse_dragging - 1;
2492 below_wanted = mouse_dragging - 1;
2493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (curwin->w_topline == 1)
2495 {
2496 above_wanted = 0;
2497 max_off = curwin->w_height / 2;
2498 if (below_wanted > max_off)
2499 below_wanted = max_off;
2500 }
2501 validate_botline();
2502 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002503 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 below_wanted = 0;
2506 max_off = (curwin->w_height - 1) / 2;
2507 if (above_wanted > max_off)
2508 above_wanted = max_off;
2509 }
2510
2511 /*
2512 * If there are sufficient file-lines above and below the cursor, we can
2513 * return now.
2514 */
2515 cln = curwin->w_cursor.lnum;
2516 if (cln >= curwin->w_topline + above_wanted
2517 && cln < curwin->w_botline - below_wanted
2518#ifdef FEAT_FOLDING
2519 && !hasAnyFolding(curwin)
2520#endif
2521 )
2522 return;
2523
2524 /*
2525 * Narrow down the area where the cursor can be put by taking lines from
2526 * the top and the bottom until:
2527 * - the desired context lines are found
2528 * - the lines from the top is past the lines from the bottom
2529 */
2530 topline = curwin->w_topline;
2531 botline = curwin->w_botline - 1;
2532#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002533 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 above = curwin->w_topfill;
2535 below = curwin->w_filler_rows;
2536#endif
2537 while ((above < above_wanted || below < below_wanted) && topline < botline)
2538 {
2539 if (below < below_wanted && (below <= above || above >= above_wanted))
2540 {
2541#ifdef FEAT_FOLDING
2542 if (hasFolding(botline, &botline, NULL))
2543 ++below;
2544 else
2545#endif
2546 below += plines(botline);
2547 --botline;
2548 }
2549 if (above < above_wanted && (above < below || below >= below_wanted))
2550 {
2551#ifdef FEAT_FOLDING
2552 if (hasFolding(topline, NULL, &topline))
2553 ++above;
2554 else
2555#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002556 above += PLINES_NOFILL(topline);
2557#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002558 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (topline < botline)
2560 above += diff_check_fill(curwin, topline + 1);
2561#endif
2562 ++topline;
2563 }
2564 }
2565 if (topline == botline || botline == 0)
2566 curwin->w_cursor.lnum = topline;
2567 else if (topline > botline)
2568 curwin->w_cursor.lnum = botline;
2569 else
2570 {
2571 if (cln < topline && curwin->w_topline > 1)
2572 {
2573 curwin->w_cursor.lnum = topline;
2574 curwin->w_valid &=
2575 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2576 }
2577 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2578 {
2579 curwin->w_cursor.lnum = botline;
2580 curwin->w_valid &=
2581 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2582 }
2583 }
2584 curwin->w_valid |= VALID_TOPLINE;
2585}
2586
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002587static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589/*
2590 * move screen 'count' pages up or down and update screen
2591 *
2592 * return FAIL for failure, OK otherwise
2593 */
2594 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002595onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
2597 long n;
2598 int retval = OK;
2599 lineoff_T loff;
2600 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002601 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
Bram Moolenaar85a20022019-12-21 18:25:54 +01002603 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
2605 beep_flush();
2606 return FAIL;
2607 }
2608
2609 for ( ; count > 0; --count)
2610 {
2611 validate_botline();
2612 /*
2613 * It's an error to move a page up when the first line is already on
2614 * the screen. It's an error to move a page down when the last line
2615 * is on the screen and the topline is 'scrolloff' lines from the
2616 * last line.
2617 */
2618 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002619 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2621 : (curwin->w_topline == 1
2622#ifdef FEAT_DIFF
2623 && curwin->w_topfill ==
2624 diff_check_fill(curwin, curwin->w_topline)
2625#endif
2626 ))
2627 {
2628 beep_flush();
2629 retval = FAIL;
2630 break;
2631 }
2632
2633#ifdef FEAT_DIFF
2634 loff.fill = 0;
2635#endif
2636 if (dir == FORWARD)
2637 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002638 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002640 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002641 if (p_window <= 2)
2642 ++curwin->w_topline;
2643 else
2644 curwin->w_topline += p_window - 2;
2645 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2646 curwin->w_topline = curbuf->b_ml.ml_line_count;
2647 curwin->w_cursor.lnum = curwin->w_topline;
2648 }
2649 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2650 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 curwin->w_topline = curbuf->b_ml.ml_line_count;
2653#ifdef FEAT_DIFF
2654 curwin->w_topfill = 0;
2655#endif
2656 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2657 }
2658 else
2659 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002660 // For the overlap, start with the line just below the window
2661 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 loff.lnum = curwin->w_botline;
2663#ifdef FEAT_DIFF
2664 loff.fill = diff_check_fill(curwin, loff.lnum)
2665 - curwin->w_filler_rows;
2666#endif
2667 get_scroll_overlap(&loff, -1);
2668 curwin->w_topline = loff.lnum;
2669#ifdef FEAT_DIFF
2670 curwin->w_topfill = loff.fill;
2671 check_topfill(curwin, FALSE);
2672#endif
2673 curwin->w_cursor.lnum = curwin->w_topline;
2674 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2675 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2676 }
2677 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002678 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
2680#ifdef FEAT_DIFF
2681 if (curwin->w_topline == 1)
2682 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002683 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 max_topfill();
2685 continue;
2686 }
2687#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002688 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002689 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002690 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002691 if (p_window <= 2)
2692 --curwin->w_topline;
2693 else
2694 curwin->w_topline -= p_window - 2;
2695 if (curwin->w_topline < 1)
2696 curwin->w_topline = 1;
2697 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2698 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2699 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2700 continue;
2701 }
2702
Bram Moolenaar85a20022019-12-21 18:25:54 +01002703 // Find the line at the top of the window that is going to be the
2704 // line at the bottom of the window. Make sure this results in
2705 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 loff.lnum = curwin->w_topline - 1;
2707#ifdef FEAT_DIFF
2708 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2709 - curwin->w_topfill;
2710#endif
2711 get_scroll_overlap(&loff, 1);
2712
2713 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2714 {
2715 loff.lnum = curbuf->b_ml.ml_line_count;
2716#ifdef FEAT_DIFF
2717 loff.fill = 0;
2718 }
2719 else
2720 {
2721 botline_topline(&loff);
2722#endif
2723 }
2724 curwin->w_cursor.lnum = loff.lnum;
2725
Bram Moolenaar85a20022019-12-21 18:25:54 +01002726 // Find the line just above the new topline to get the right line
2727 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 n = 0;
2729 while (n <= curwin->w_height && loff.lnum >= 1)
2730 {
2731 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002732 if (loff.height == MAXCOL)
2733 n = MAXCOL;
2734 else
2735 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002737 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
2739 curwin->w_topline = 1;
2740#ifdef FEAT_DIFF
2741 max_topfill();
2742#endif
2743 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2744 }
2745 else
2746 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002747 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748#ifdef FEAT_DIFF
2749 topline_botline(&loff);
2750#endif
2751 botline_forw(&loff);
2752 botline_forw(&loff);
2753#ifdef FEAT_DIFF
2754 botline_topline(&loff);
2755#endif
2756#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002757 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2759#endif
2760
Bram Moolenaar85a20022019-12-21 18:25:54 +01002761 // Always scroll at least one line. Avoid getting stuck on
2762 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (loff.lnum >= curwin->w_topline
2764#ifdef FEAT_DIFF
2765 && (loff.lnum > curwin->w_topline
2766 || loff.fill >= curwin->w_topfill)
2767#endif
2768 )
2769 {
2770#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002771 // First try using the maximum number of filler lines. If
2772 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 loff.fill = curwin->w_topfill;
2774 if (curwin->w_topfill < diff_check_fill(curwin,
2775 curwin->w_topline))
2776 max_topfill();
2777 if (curwin->w_topfill == loff.fill)
2778#endif
2779 {
2780 --curwin->w_topline;
2781#ifdef FEAT_DIFF
2782 curwin->w_topfill = 0;
2783#endif
2784 }
2785 comp_botline(curwin);
2786 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002787 curwin->w_valid &=
2788 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 else
2791 {
2792 curwin->w_topline = loff.lnum;
2793#ifdef FEAT_DIFF
2794 curwin->w_topfill = loff.fill;
2795 check_topfill(curwin, FALSE);
2796#endif
2797 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2798 }
2799 }
2800 }
2801 }
2802#ifdef FEAT_FOLDING
2803 foldAdjustCursor();
2804#endif
2805 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002806 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002807 if (retval == OK)
2808 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2810
Bram Moolenaar907dad72018-07-10 15:07:15 +02002811 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002813 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2814 // But make sure we scroll at least one line (happens with mix of long
2815 // wrapping lines and non-wrapping line).
2816 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002818 scroll_cursor_top(1, FALSE);
2819 if (curwin->w_topline <= old_topline
2820 && old_topline < curbuf->b_ml.ml_line_count)
2821 {
2822 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002824 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2825#endif
2826 }
2827 }
2828#ifdef FEAT_FOLDING
2829 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002834 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 return retval;
2836}
2837
2838/*
2839 * Decide how much overlap to use for page-up or page-down scrolling.
2840 * This is symmetric, so that doing both keeps the same lines displayed.
2841 * Three lines are examined:
2842 *
2843 * before CTRL-F after CTRL-F / before CTRL-B
2844 * etc. l1
2845 * l1 last but one line ------------
2846 * l2 last text line l2 top text line
2847 * ------------- l3 second text line
2848 * l3 etc.
2849 */
2850 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002851get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 int h1, h2, h3, h4;
2854 int min_height = curwin->w_height - 2;
2855 lineoff_T loff0, loff1, loff2;
2856
2857#ifdef FEAT_DIFF
2858 if (lp->fill > 0)
2859 lp->height = 1;
2860 else
2861 lp->height = plines_nofill(lp->lnum);
2862#else
2863 lp->height = plines(lp->lnum);
2864#endif
2865 h1 = lp->height;
2866 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002867 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 loff0 = *lp;
2870 if (dir > 0)
2871 botline_forw(lp);
2872 else
2873 topline_back(lp);
2874 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002875 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002877 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 return;
2879 }
2880
2881 loff1 = *lp;
2882 if (dir > 0)
2883 botline_forw(lp);
2884 else
2885 topline_back(lp);
2886 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002887 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002889 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 return;
2891 }
2892
2893 loff2 = *lp;
2894 if (dir > 0)
2895 botline_forw(lp);
2896 else
2897 topline_back(lp);
2898 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002899 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002900 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002902 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903}
2904
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905/*
2906 * Scroll 'scroll' lines up or down.
2907 */
2908 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002909halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 long scrolled = 0;
2912 int i;
2913 int n;
2914 int room;
2915
2916 if (Prenum)
2917 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2918 curwin->w_height : Prenum;
2919 n = (curwin->w_p_scr <= curwin->w_height) ?
2920 curwin->w_p_scr : curwin->w_height;
2921
Bram Moolenaard5d37532017-03-27 23:02:07 +02002922 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 validate_botline();
2924 room = curwin->w_empty_rows;
2925#ifdef FEAT_DIFF
2926 room += curwin->w_filler_rows;
2927#endif
2928 if (flag)
2929 {
2930 /*
2931 * scroll the text up
2932 */
2933 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2934 {
2935#ifdef FEAT_DIFF
2936 if (curwin->w_topfill > 0)
2937 {
2938 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002939 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 --curwin->w_topfill;
2941 }
2942 else
2943#endif
2944 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002945 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 n -= i;
2947 if (n < 0 && scrolled > 0)
2948 break;
2949#ifdef FEAT_FOLDING
2950 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2951#endif
2952 ++curwin->w_topline;
2953#ifdef FEAT_DIFF
2954 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2955#endif
2956
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2958 {
2959 ++curwin->w_cursor.lnum;
2960 curwin->w_valid &=
2961 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2965 scrolled += i;
2966
2967 /*
2968 * Correct w_botline for changed w_topline.
2969 * Won't work when there are filler lines.
2970 */
2971#ifdef FEAT_DIFF
2972 if (curwin->w_p_diff)
2973 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2974 else
2975#endif
2976 {
2977 room += i;
2978 do
2979 {
2980 i = plines(curwin->w_botline);
2981 if (i > room)
2982 break;
2983#ifdef FEAT_FOLDING
2984 (void)hasFolding(curwin->w_botline, NULL,
2985 &curwin->w_botline);
2986#endif
2987 ++curwin->w_botline;
2988 room -= i;
2989 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
2990 }
2991 }
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 /*
2994 * When hit bottom of the file: move cursor down.
2995 */
2996 if (n > 0)
2997 {
2998# ifdef FEAT_FOLDING
2999 if (hasAnyFolding(curwin))
3000 {
3001 while (--n >= 0
3002 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3003 {
3004 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3005 &curwin->w_cursor.lnum);
3006 ++curwin->w_cursor.lnum;
3007 }
3008 }
3009 else
3010# endif
3011 curwin->w_cursor.lnum += n;
3012 check_cursor_lnum();
3013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015 else
3016 {
3017 /*
3018 * scroll the text down
3019 */
3020 while (n > 0 && curwin->w_topline > 1)
3021 {
3022#ifdef FEAT_DIFF
3023 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3024 {
3025 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003026 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 ++curwin->w_topfill;
3028 }
3029 else
3030#endif
3031 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003032 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 n -= i;
3034 if (n < 0 && scrolled > 0)
3035 break;
3036 --curwin->w_topline;
3037#ifdef FEAT_FOLDING
3038 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3039#endif
3040#ifdef FEAT_DIFF
3041 curwin->w_topfill = 0;
3042#endif
3043 }
3044 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3045 VALID_BOTLINE|VALID_BOTLINE_AP);
3046 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 if (curwin->w_cursor.lnum > 1)
3048 {
3049 --curwin->w_cursor.lnum;
3050 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 /*
3055 * When hit top of the file: move cursor up.
3056 */
3057 if (n > 0)
3058 {
3059 if (curwin->w_cursor.lnum <= (linenr_T)n)
3060 curwin->w_cursor.lnum = 1;
3061 else
3062# ifdef FEAT_FOLDING
3063 if (hasAnyFolding(curwin))
3064 {
3065 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3066 {
3067 --curwin->w_cursor.lnum;
3068 (void)hasFolding(curwin->w_cursor.lnum,
3069 &curwin->w_cursor.lnum, NULL);
3070 }
3071 }
3072 else
3073# endif
3074 curwin->w_cursor.lnum -= n;
3075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003078 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 foldAdjustCursor();
3080# endif
3081#ifdef FEAT_DIFF
3082 check_topfill(curwin, !flag);
3083#endif
3084 cursor_correct();
3085 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003086 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003088
Bram Moolenaar860cae12010-06-05 23:22:07 +02003089 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003090do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003091{
3092 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003093 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003094 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003095 colnr_T curswant = curwin->w_curswant;
3096 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003097 win_T *old_curwin = curwin;
3098 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003099 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003100 int old_VIsual_select = VIsual_select;
3101 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003102
3103 /*
3104 * loop through the cursorbound windows
3105 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003106 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003107 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003108 {
3109 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003110 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003111 if (curwin != old_curwin && curwin->w_p_crb)
3112 {
3113# ifdef FEAT_DIFF
3114 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003115 curwin->w_cursor.lnum =
3116 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003117 else
3118# endif
3119 curwin->w_cursor.lnum = line;
3120 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003121 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003122 curwin->w_curswant = curswant;
3123 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003124
Bram Moolenaar85a20022019-12-21 18:25:54 +01003125 // Make sure the cursor is in a valid position. Temporarily set
3126 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003127 restart_edit_save = restart_edit;
3128 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003129 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003130
3131 // Avoid a scroll here for the cursor position, 'scrollbind' is
3132 // more important.
3133 if (!curwin->w_p_scb)
3134 validate_cursor();
3135
Bram Moolenaar61452852011-02-01 18:01:11 +01003136 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003137 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003138 if (has_mbyte)
3139 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003140 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003141
Bram Moolenaar85a20022019-12-21 18:25:54 +01003142 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003143 if (!curwin->w_p_scb)
3144 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003145 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003146 }
3147 }
3148
3149 /*
3150 * reset current-window
3151 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003152 VIsual_select = old_VIsual_select;
3153 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003154 curwin = old_curwin;
3155 curbuf = old_curbuf;
3156}