blob: 995301e8d2e01f350017b06b5e36c1135ef870a3 [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;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 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
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100222 // Cursor is updated instead when this is TRUE for 'splitkeep'.
223 if (skip_update_topline)
224 return;
225
Bram Moolenaar85a20022019-12-21 18:25:54 +0100226 // If there is no valid screen and when the window height is zero just use
227 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200228 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200229 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100230 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200231 curwin->w_topline = curwin->w_cursor.lnum;
232 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200233 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200234 return;
235 }
236
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 check_cursor_moved(curwin);
238 if (curwin->w_valid & VALID_TOPLINE)
239 return;
240
Bram Moolenaar85a20022019-12-21 18:25:54 +0100241 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000242 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100243 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245 old_topline = curwin->w_topline;
246#ifdef FEAT_DIFF
247 old_topfill = curwin->w_topfill;
248#endif
249
250 /*
251 * If the buffer is empty, always set topline to 1.
252 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100253 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {
255 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100256 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 curwin->w_botline = 2;
259 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
262
263 /*
264 * If the cursor is above or near the top of the window, scroll the window
265 * to show the line the cursor is in, with 'scrolloff' context.
266 */
267 else
268 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100269 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100271 // If the cursor is above topline, scrolling is always needed.
272 // If the cursor is far below topline and there is no folding,
273 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 if (curwin->w_cursor.lnum < curwin->w_topline)
275 check_topline = TRUE;
276 else if (check_top_offset())
277 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100278 else if (curwin->w_cursor.lnum == curwin->w_topline)
279 {
280 colnr_T vcol;
281
282 // check the cursor position is visible. Add 3 for the ">>>"
283 // displayed in the top-left.
284 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
285 if (curwin->w_skipcol + 3 >= vcol)
286 check_topline = TRUE;
287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
289#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
292 curwin->w_topline))
293 check_topline = TRUE;
294#endif
295
296 if (check_topline)
297 {
298 halfheight = curwin->w_height / 2 - 1;
299 if (halfheight < 2)
300 halfheight = 2;
301
302#ifdef FEAT_FOLDING
303 if (hasAnyFolding(curwin))
304 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100305 // Count the number of logical lines between the cursor and
306 // topline + scrolloff (approximation of how much will be
307 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 n = 0;
309 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 {
312 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100313 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
315 break;
316 (void)hasFolding(lnum, NULL, &lnum);
317 }
318 }
319 else
320#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100321 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaar85a20022019-12-21 18:25:54 +0100323 // If we weren't very close to begin with, we scroll to put the
324 // cursor in the middle of the window. Otherwise put the cursor
325 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 if (n >= halfheight)
327 scroll_cursor_halfway(FALSE);
328 else
329 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000330 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 check_botline = TRUE;
332 }
333 }
334
335 else
336 {
337#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100338 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
340#endif
341 check_botline = TRUE;
342 }
343 }
344
345 /*
346 * If the cursor is below the bottom of the window, scroll the window
347 * to put the cursor on the window.
348 * When w_botline is invalid, recompute it first, to avoid a redraw later.
349 * If w_botline was approximated, we might need a redraw later in a few
350 * cases, but we don't want to spend (a lot of) time recomputing w_botline
351 * for every small change.
352 */
353 if (check_botline)
354 {
355 if (!(curwin->w_valid & VALID_BOTLINE_AP))
356 validate_botline();
357
358 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
359 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000360 if (curwin->w_cursor.lnum < curwin->w_botline)
361 {
362 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100363 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364#ifdef FEAT_FOLDING
365 || hasAnyFolding(curwin)
366#endif
367 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 lineoff_T loff;
370
Bram Moolenaar85a20022019-12-21 18:25:54 +0100371 // Cursor is (a few lines) above botline, check if there are
372 // 'scrolloff' window lines below the cursor. If not, need to
373 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 n = curwin->w_empty_rows;
375 loff.lnum = curwin->w_cursor.lnum;
376#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
379#endif
380#ifdef FEAT_DIFF
381 loff.fill = 0;
382 n += curwin->w_filler_rows;
383#endif
384 loff.height = 0;
385 while (loff.lnum < curwin->w_botline
386#ifdef FEAT_DIFF
387 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
388#endif
389 )
390 {
391 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100392 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 break;
394 botline_forw(&loff);
395 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100396 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100397 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000399 }
400 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100401 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000402 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 }
404 if (check_botline)
405 {
406#ifdef FEAT_FOLDING
407 if (hasAnyFolding(curwin))
408 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // Count the number of logical lines between the cursor and
410 // botline - scrolloff (approximation of how much will be
411 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 line_count = 0;
413 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100414 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 {
416 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100417 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 if (lnum <= 0 || line_count > curwin->w_height + 1)
419 break;
420 (void)hasFolding(lnum, &lnum, NULL);
421 }
422 }
423 else
424#endif
425 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100426 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000428 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 else
430 scroll_cursor_halfway(FALSE);
431 }
432 }
433 }
434 curwin->w_valid |= VALID_TOPLINE;
435
436 /*
437 * Need to redraw when topline changed.
438 */
439 if (curwin->w_topline != old_topline
440#ifdef FEAT_DIFF
441 || curwin->w_topfill != old_topfill
442#endif
443 )
444 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100445 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000446 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {
448 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100449 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 }
451 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100452 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100453 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (curwin->w_cursor.lnum == curwin->w_topline)
455 validate_cursor();
456 }
457
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459}
460
461/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000462 * Return the scrolljump value to use for the current window.
463 * When 'scrolljump' is positive use it as-is.
464 * When 'scrolljump' is negative use it as a percentage of the window height.
465 */
466 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100467scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000468{
469 if (p_sj >= 0)
470 return (int)p_sj;
471 return (curwin->w_height * -p_sj) / 100;
472}
473
474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
476 * current window.
477 */
478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100479check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 lineoff_T loff;
482 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100483 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
Bram Moolenaar375e3392019-01-31 18:26:10 +0100485 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486#ifdef FEAT_FOLDING
487 || hasAnyFolding(curwin)
488#endif
489 )
490 {
491 loff.lnum = curwin->w_cursor.lnum;
492#ifdef FEAT_DIFF
493 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100494 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#else
496 n = 0;
497#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100498 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100499 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100502 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 if (loff.lnum < curwin->w_topline
504#ifdef FEAT_DIFF
505 || (loff.lnum == curwin->w_topline && loff.fill > 0)
506#endif
507 )
508 break;
509 n += loff.height;
510 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100511 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 return TRUE;
513 }
514 return FALSE;
515}
516
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100517/*
518 * Update w_curswant.
519 */
520 void
521update_curswant_force(void)
522{
523 validate_virtcol();
524 curwin->w_curswant = curwin->w_virtcol
525#ifdef FEAT_PROP_POPUP
526 - curwin->w_virtcol_first_char
527#endif
528 ;
529 curwin->w_set_curswant = FALSE;
530}
531
532/*
533 * Update w_curswant if w_set_curswant is set.
534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100539 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
541
542/*
543 * Check if the cursor has moved. Set the w_valid flag accordingly.
544 */
545 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100546check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
549 {
550 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100551 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
552 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 wp->w_valid_cursor = wp->w_cursor;
554 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100555 wp->w_valid_skipcol = wp->w_skipcol;
556 }
557 else if (wp->w_skipcol != wp->w_valid_skipcol)
558 {
559 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
560 |VALID_CHEIGHT|VALID_CROW
561 |VALID_BOTLINE|VALID_BOTLINE_AP);
562 wp->w_valid_cursor = wp->w_cursor;
563 wp->w_valid_leftcol = wp->w_leftcol;
564 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566 else if (wp->w_cursor.col != wp->w_valid_cursor.col
567 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100568 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
570 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
571 wp->w_valid_cursor.col = wp->w_cursor.col;
572 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575}
576
577/*
578 * Call this function when some window settings have changed, which require
579 * the cursor position, botline and topline to be recomputed and the window to
580 * be redrawn. E.g, when changing the 'wrap' option or folding.
581 */
582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100583changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 changed_window_setting_win(curwin);
586}
587
588 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100589changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 wp->w_lines_valid = 0;
592 changed_line_abv_curs_win(wp);
593 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100594 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595}
596
597/*
598 * Set wp->w_topline to a certain number.
599 */
600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100601set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200603#ifdef FEAT_DIFF
604 linenr_T prev_topline = wp->w_topline;
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100608 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
610#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100611 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100613 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
614 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000616 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200618 if (lnum != prev_topline)
619 // Keep the filler lines when the topline didn't change.
620 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#endif
622 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100623 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100624 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625}
626
627/*
628 * Call this function when the length of the cursor line (in screen
629 * characters) has changed, and the change is before the cursor.
630 * Need to take care of w_botline separately!
631 */
632 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100633changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
636 |VALID_CHEIGHT|VALID_TOPLINE);
637}
638
639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
643 |VALID_CHEIGHT|VALID_TOPLINE);
644}
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646/*
647 * Call this function when the length of a line (in screen characters) above
648 * the cursor have changed.
649 * Need to take care of w_botline separately!
650 */
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
655 |VALID_CHEIGHT|VALID_TOPLINE);
656}
657
658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100659changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
662 |VALID_CHEIGHT|VALID_TOPLINE);
663}
664
665/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100666 * Display of line has changed for "buf", invalidate cursor position and
667 * w_botline.
668 */
669 void
670changed_line_display_buf(buf_T *buf)
671{
672 win_T *wp;
673
674 FOR_ALL_WINDOWS(wp)
675 if (wp->w_buffer == buf)
676 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
677 |VALID_CROW|VALID_CHEIGHT
678 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
679}
680
681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 * Make sure the value of curwin->w_botline is valid.
683 */
684 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100685validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100687 validate_botline_win(curwin);
688}
689
690/*
691 * Make sure the value of wp->w_botline is valid.
692 */
693 void
694validate_botline_win(win_T *wp)
695{
696 if (!(wp->w_valid & VALID_BOTLINE))
697 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698}
699
700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 * Mark curwin->w_botline as invalid (because of some change in the buffer).
702 */
703 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100704invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
707}
708
709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100710invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
713}
714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100716approximate_botline_win(
717 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 wp->w_valid &= ~VALID_BOTLINE;
720}
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722/*
723 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
724 */
725 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100726cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 check_cursor_moved(curwin);
729 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
730 (VALID_WROW|VALID_WCOL));
731}
732
733/*
734 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
735 * w_topline must be valid, you may need to call update_topline() first!
736 */
737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100738validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100740 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 check_cursor_moved(curwin);
742 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
743 curs_columns(TRUE);
744}
745
746#if defined(FEAT_GUI) || defined(PROTO)
747/*
748 * validate w_cline_row.
749 */
750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 /*
754 * First make sure that w_topline is valid (after moving the cursor).
755 */
756 update_topline();
757 check_cursor_moved(curwin);
758 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100759 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760}
761#endif
762
763/*
764 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200765 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100768curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
770 linenr_T lnum;
771 int i;
772 int all_invalid;
773 int valid;
774#ifdef FEAT_FOLDING
775 long fold_count;
776#endif
777
Bram Moolenaar85a20022019-12-21 18:25:54 +0100778 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 all_invalid = (!redrawing()
780 || wp->w_lines_valid == 0
781 || wp->w_lines[0].wl_lnum > wp->w_topline);
782 i = 0;
783 wp->w_cline_row = 0;
784 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
785 {
786 valid = FALSE;
787 if (!all_invalid && i < wp->w_lines_valid)
788 {
789 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100790 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (wp->w_lines[i].wl_lnum == lnum)
792 {
793#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100794 // Check for newly inserted lines below this row, in which
795 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (!wp->w_buffer->b_mod_set
797 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
798 || wp->w_buffer->b_mod_top
799 > wp->w_lines[i].wl_lastlnum + 1)
800#endif
801 valid = TRUE;
802 }
803 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100804 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 if (valid
807#ifdef FEAT_DIFF
808 && (lnum != wp->w_topline || !wp->w_p_diff)
809#endif
810 )
811 {
812#ifdef FEAT_FOLDING
813 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100814 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (lnum > wp->w_cursor.lnum)
816 break;
817#else
818 ++lnum;
819#endif
820 wp->w_cline_row += wp->w_lines[i].wl_size;
821 }
822 else
823 {
824#ifdef FEAT_FOLDING
825 fold_count = foldedCount(wp, lnum, NULL);
826 if (fold_count)
827 {
828 lnum += fold_count;
829 if (lnum > wp->w_cursor.lnum)
830 break;
831 ++wp->w_cline_row;
832 }
833 else
834#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100835 {
836 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#ifdef FEAT_DIFF
838 if (lnum == wp->w_topline)
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100839 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 else
841#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100842 n = plines_win(wp, lnum, TRUE);
843 if (lnum++ == wp->w_topline)
844 n = adjust_plines_for_skipcol(wp, n);
845 wp->w_cline_row += n;
846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 }
849
850 check_cursor_moved(wp);
851 if (!(wp->w_valid & VALID_CHEIGHT))
852 {
853 if (all_invalid
854 || i == wp->w_lines_valid
855 || (i < wp->w_lines_valid
856 && (!wp->w_lines[i].wl_valid
857 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
858 {
859#ifdef FEAT_DIFF
860 if (wp->w_cursor.lnum == wp->w_topline)
861 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
862 TRUE) + wp->w_topfill;
863 else
864#endif
865 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
866#ifdef FEAT_FOLDING
867 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
868 NULL, NULL, TRUE, NULL);
869#endif
870 }
871 else if (i > wp->w_lines_valid)
872 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100873 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 wp->w_cline_height = 0;
875#ifdef FEAT_FOLDING
876 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
877 NULL, NULL, TRUE, NULL);
878#endif
879 }
880 else
881 {
882 wp->w_cline_height = wp->w_lines[i].wl_size;
883#ifdef FEAT_FOLDING
884 wp->w_cline_folded = wp->w_lines[i].wl_folded;
885#endif
886 }
887 }
888
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100889 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891}
892
893/*
894 * Validate curwin->w_virtcol only.
895 */
896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100897validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899 validate_virtcol_win(curwin);
900}
901
902/*
903 * Validate wp->w_virtcol only.
904 */
905 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100906validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
908 check_cursor_moved(wp);
909 if (!(wp->w_valid & VALID_VIRTCOL))
910 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100911#ifdef FEAT_PROP_POPUP
912 wp->w_virtcol_first_char = 0;
913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000915#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100916 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000917#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100918 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920}
921
922/*
923 * Validate curwin->w_cline_height only.
924 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 check_cursor_moved(curwin);
929 if (!(curwin->w_valid & VALID_CHEIGHT))
930 {
931#ifdef FEAT_DIFF
932 if (curwin->w_cursor.lnum == curwin->w_topline)
933 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
934 + curwin->w_topfill;
935 else
936#endif
937 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
938#ifdef FEAT_FOLDING
939 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
940#endif
941 curwin->w_valid |= VALID_CHEIGHT;
942 }
943}
944
945/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000946 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 */
948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100949validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 colnr_T off;
952 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100953 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 validate_virtcol();
956 if (!(curwin->w_valid & VALID_WCOL))
957 {
958 col = curwin->w_virtcol;
959 off = curwin_col_off();
960 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200961 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaar85a20022019-12-21 18:25:54 +0100963 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000964 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200965 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100966 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200968 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000969 if (col > (int)curwin->w_leftcol)
970 col -= curwin->w_leftcol;
971 else
972 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100976#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100977 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980}
981
982/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200983 * Compute offset of a window, occupied by absolute or relative line number,
984 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 */
986 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100987win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
Bram Moolenaar64486672010-05-16 15:46:46 +0200989 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_FOLDING
992 + wp->w_p_fdc
993#endif
994#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200995 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#endif
997 );
998}
999
1000 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001001curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 return win_col_off(curwin);
1004}
1005
1006/*
1007 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001008 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1009 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
1011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001012win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
Bram Moolenaar64486672010-05-16 15:46:46 +02001014 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001015 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 return 0;
1017}
1018
1019 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001020curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022 return win_col_off2(curwin);
1023}
1024
1025/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001026 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Also updates curwin->w_wrow and curwin->w_cline_row.
1028 * Also updates curwin->w_leftcol.
1029 */
1030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001031curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001032 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001035 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int off_left, off_right;
1037 int n;
1038 int p_lines;
1039 int width = 0;
1040 int textwidth;
1041 int new_leftcol;
1042 colnr_T startcol;
1043 colnr_T endcol;
1044 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001045 long so = get_scrolloff_value();
1046 long siso = get_sidescrolloff_value();
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001047 int did_sub_skipcol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 /*
1050 * First make sure that w_topline is valid (after moving the cursor).
1051 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001052 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
1054 /*
1055 * Next make sure that w_cline_row is valid.
1056 */
1057 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001058 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001060#ifdef FEAT_PROP_POPUP
1061 // will be set by getvvcol() but not reset
1062 curwin->w_virtcol_first_char = 0;
1063#endif
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 /*
1066 * Compute the number of virtual columns.
1067 */
1068#ifdef FEAT_FOLDING
1069 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001070 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1072 else
1073#endif
1074 getvvcol(curwin, &curwin->w_cursor,
1075 &startcol, &(curwin->w_virtcol), &endcol);
1076
Bram Moolenaar85a20022019-12-21 18:25:54 +01001077 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001079 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
1081 extra = curwin_col_off();
1082 curwin->w_wcol = curwin->w_virtcol + extra;
1083 endcol += extra;
1084
1085 /*
1086 * Now compute w_wrow, counting screen lines from w_cline_row.
1087 */
1088 curwin->w_wrow = curwin->w_cline_row;
1089
Bram Moolenaar02631462017-09-22 15:20:32 +02001090 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (textwidth <= 0)
1092 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001093 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001094 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001095 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001096 if (curwin->w_p_wrap)
1097 curwin->w_wrow = curwin->w_height - 1;
1098 else
1099 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001101 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
1103 width = textwidth + curwin_col_off2();
1104
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001105 // skip columns that are not visible
1106 if (curwin->w_cursor.lnum == curwin->w_topline
1107 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001108 {
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001109 curwin->w_wcol -= curwin->w_skipcol;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001110 did_sub_skipcol = TRUE;
1111 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001112
Bram Moolenaar85a20022019-12-21 18:25:54 +01001113 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001114 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001116 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001117 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 curwin->w_wcol -= n * width;
1119 curwin->w_wrow += n;
1120
1121#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001122 // When cursor wraps to first char of next line in Insert
1123 // mode, the 'showbreak' string isn't shown, backup to first
1124 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001125 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001126 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001127 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 curwin->w_wcol = 0;
1129#endif
1130 }
1131 }
1132
Bram Moolenaar85a20022019-12-21 18:25:54 +01001133 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1134 // is not folded.
1135 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001136 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#ifdef FEAT_FOLDING
1138 && !curwin->w_cline_folded
1139#endif
1140 )
1141 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001142#ifdef FEAT_PROP_POPUP
1143 if (curwin->w_virtcol_first_char > 0)
1144 {
1145 int cols = (curwin->w_width - extra);
1146 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1147
1148 // each "above" text prop shifts the text one row down
1149 curwin->w_wrow += rows;
1150 curwin->w_wcol -= rows * cols;
1151 endcol -= rows * cols;
1152 curwin->w_cline_height = rows + 1;
1153 }
1154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 /*
1156 * If Cursor is left of the screen, scroll rightwards.
1157 * If Cursor is right of the screen, scroll leftwards
1158 * If we get closer to the edge than 'sidescrolloff', scroll a little
1159 * extra
1160 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001161 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001162 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001163 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (off_left < 0 || off_right > 0)
1165 {
1166 if (off_left < 0)
1167 diff = -off_left;
1168 else
1169 diff = off_right;
1170
Bram Moolenaar85a20022019-12-21 18:25:54 +01001171 // When far off or not enough room on either side, put cursor in
1172 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1174 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1175 else
1176 {
1177 if (diff < p_ss)
1178 diff = p_ss;
1179 if (off_left < 0)
1180 new_leftcol = curwin->w_leftcol - diff;
1181 else
1182 new_leftcol = curwin->w_leftcol + diff;
1183 }
1184 if (new_leftcol < 0)
1185 new_leftcol = 0;
1186 if (new_leftcol != (int)curwin->w_leftcol)
1187 {
1188 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001190 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 }
1193 curwin->w_wcol -= curwin->w_leftcol;
1194 }
1195 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1196 curwin->w_wcol -= curwin->w_leftcol;
1197 else
1198 curwin->w_wcol = 0;
1199
1200#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 // Skip over filler lines. At the top use w_topfill, there
1202 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (curwin->w_cursor.lnum == curwin->w_topline)
1204 curwin->w_wrow += curwin->w_topfill;
1205 else
1206 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1207#endif
1208
1209 prev_skipcol = curwin->w_skipcol;
1210
1211 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if ((curwin->w_wrow >= curwin->w_height
1214 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001215 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 && (p_lines =
1217#ifdef FEAT_DIFF
1218 plines_win_nofill
1219#else
1220 plines_win
1221#endif
1222 (curwin, curwin->w_cursor.lnum, FALSE))
1223 - 1 >= curwin->w_height))
1224 && curwin->w_height != 0
1225 && curwin->w_cursor.lnum == curwin->w_topline
1226 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001227 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001229 // Cursor past end of screen. Happens with a single line that does
1230 // not fit on screen. Find a skipcol to show the text around the
1231 // cursor. Avoid scrolling all the time. compute value of "extra":
1232 // 1: Less than 'scrolloff' lines above
1233 // 2: Less than 'scrolloff' lines below
1234 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001236 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001238 // Compute last display line of the buffer line that we want at the
1239 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (p_lines == 0)
1241 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1242 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001243 if (p_lines > curwin->w_wrow + so)
1244 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 else
1246 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001247 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 extra += 2;
1249
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001250 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001252 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 n = curwin->w_virtcol / width;
1254 if (n > curwin->w_height / 2)
1255 n -= curwin->w_height / 2;
1256 else
1257 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001258 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (n > p_lines - curwin->w_height + 1)
1260 n = p_lines - curwin->w_height + 1;
1261 curwin->w_skipcol = n * width;
1262 }
1263 else if (extra == 1)
1264 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001265 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001266 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 + width - 1) / width;
1268 if (extra > 0)
1269 {
1270 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1271 extra = curwin->w_skipcol / width;
1272 curwin->w_skipcol -= extra * width;
1273 }
1274 }
1275 else if (extra == 2)
1276 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001277 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 endcol = (n - curwin->w_height + 1) * width;
1279 while (endcol > curwin->w_virtcol)
1280 endcol -= width;
1281 if (endcol > curwin->w_skipcol)
1282 curwin->w_skipcol = endcol;
1283 }
1284
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001285 // adjust w_wrow for the changed w_skipcol
1286 if (did_sub_skipcol)
1287 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width;
1288 else
1289 curwin->w_wrow -= curwin->w_skipcol / width;
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (curwin->w_wrow >= curwin->w_height)
1292 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001293 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 extra = curwin->w_wrow - curwin->w_height + 1;
1295 curwin->w_skipcol += extra * width;
1296 curwin->w_wrow -= extra;
1297 }
1298
1299 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1300 if (extra > 0)
1301 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1302 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001303 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001305 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 curwin->w_skipcol = 0;
1307 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001308 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001310#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001311 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001312#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001313#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1314 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1315 {
1316 curwin->w_wrow += popup_top_extra(curwin);
1317 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001318 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001319 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001320 else
1321 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001322#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001323
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001324 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1325 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001326 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001327 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001328
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1330}
1331
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001332#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001333/*
1334 * Compute the screen position of text character at "pos" in window "wp"
1335 * The resulting values are one-based, zero when character is not visible.
1336 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001337 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001338textpos2screenpos(
1339 win_T *wp,
1340 pos_T *pos,
1341 int *rowp, // screen row
1342 int *scolp, // start screen column
1343 int *ccolp, // cursor screen column
1344 int *ecolp) // end screen column
1345{
1346 colnr_T scol = 0, ccol = 0, ecol = 0;
1347 int row = 0;
1348 int rowoff = 0;
1349 colnr_T coloff = 0;
1350
Bram Moolenaar189663b2021-07-21 18:04:56 +02001351 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001352 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001353 colnr_T off;
1354 colnr_T col;
1355 int width;
1356 linenr_T lnum = pos->lnum;
1357#ifdef FEAT_FOLDING
1358 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001359
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001360 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1361#endif
1362 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1363#ifdef FEAT_FOLDING
1364 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001365 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001366 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001367 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001368 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001369 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001370#endif
1371 {
1372 getvcol(wp, pos, &scol, &ccol, &ecol);
1373
1374 // similar to what is done in validate_cursor_col()
1375 col = scol;
1376 off = win_col_off(wp);
1377 col += off;
1378 width = wp->w_width - off + win_col_off2(wp);
1379
1380 // long line wrapping, adjust row
1381 if (wp->w_p_wrap
1382 && col >= (colnr_T)wp->w_width
1383 && width > 0)
1384 {
1385 // use same formula as what is used in curs_columns()
1386 rowoff = ((col - wp->w_width) / width + 1);
1387 col -= rowoff * width;
1388 }
1389 col -= wp->w_leftcol;
1390 if (col >= wp->w_width)
1391 col = -1;
1392 if (col >= 0 && row + rowoff <= wp->w_height)
1393 {
1394 coloff = col - scol + wp->w_wincol + 1;
1395 row += W_WINROW(wp);
1396 }
1397 else
1398 // character is left, right or below of the window
1399 row = rowoff = scol = ccol = ecol = 0;
1400 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001401 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001402 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001403 *scolp = scol + coloff;
1404 *ccolp = ccol + coloff;
1405 *ecolp = ecol + coloff;
1406}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001407#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001408
Bram Moolenaar12034e22019-08-25 22:25:02 +02001409#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001410/*
1411 * "screenpos({winid}, {lnum}, {col})" function
1412 */
1413 void
1414f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1415{
1416 dict_T *dict;
1417 win_T *wp;
1418 pos_T pos;
1419 int row = 0;
1420 int scol = 0, ccol = 0, ecol = 0;
1421
Bram Moolenaar93a10962022-06-16 11:42:09 +01001422 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001423 return;
1424 dict = rettv->vval.v_dict;
1425
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001426 if (in_vim9script()
1427 && (check_for_number_arg(argvars, 0) == FAIL
1428 || check_for_number_arg(argvars, 1) == FAIL
1429 || check_for_number_arg(argvars, 2) == FAIL))
1430 return;
1431
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001432 wp = find_win_by_nr_or_id(&argvars[0]);
1433 if (wp == NULL)
1434 return;
1435
1436 pos.lnum = tv_get_number(&argvars[1]);
1437 pos.col = tv_get_number(&argvars[2]) - 1;
1438 pos.coladd = 0;
1439 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1440
1441 dict_add_number(dict, "row", row);
1442 dict_add_number(dict, "col", scol);
1443 dict_add_number(dict, "curscol", ccol);
1444 dict_add_number(dict, "endcol", ecol);
1445}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001446
1447/*
1448 * "virtcol2col({winid}, {lnum}, {col})" function
1449 */
1450 void
1451f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1452{
1453 win_T *wp;
1454 linenr_T lnum;
1455 int screencol;
1456 int error = FALSE;
1457
1458 rettv->vval.v_number = -1;
1459
1460 if (check_for_number_arg(argvars, 0) == FAIL
1461 || check_for_number_arg(argvars, 1) == FAIL
1462 || check_for_number_arg(argvars, 2) == FAIL)
1463 return;
1464
1465 wp = find_win_by_nr_or_id(&argvars[0]);
1466 if (wp == NULL)
1467 return;
1468
1469 lnum = tv_get_number_chk(&argvars[1], &error);
1470 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1471 return;
1472
1473 screencol = tv_get_number_chk(&argvars[2], &error);
1474 if (error || screencol < 0)
1475 return;
1476
1477 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1478}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001479#endif
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481/*
1482 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001485scrolldown(
1486 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001487 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001489 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 int wrow;
1491 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001492 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001493 int width1 = 0;
1494 int width2 = 0;
1495
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001496 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001497 {
1498 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001499 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
1502#ifdef FEAT_FOLDING
1503 linenr_T first;
1504
Bram Moolenaar85a20022019-12-21 18:25:54 +01001505 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1507#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001508 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001509 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 {
1511#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001512 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1513 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 {
1515 ++curwin->w_topfill;
1516 ++done;
1517 }
1518 else
1519#endif
1520 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001521 // break when at the very top
1522 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001523 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001525 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001527 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001528 if (curwin->w_skipcol >= width1 + width2)
1529 curwin->w_skipcol -= width2;
1530 else
1531 curwin->w_skipcol -= width1;
1532 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001536 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001537 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001538 --curwin->w_topline;
1539 curwin->w_skipcol = 0;
1540#ifdef FEAT_DIFF
1541 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001543#ifdef FEAT_FOLDING
1544 // A sequence of folded lines only counts for one logical line
1545 if (hasFolding(curwin->w_topline, &first, NULL))
1546 {
1547 ++done;
1548 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001549 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001550 curwin->w_botline -= curwin->w_topline - first;
1551 curwin->w_topline = first;
1552 }
1553 else
1554#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001555 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001556 {
1557 int size = win_linetabsize(curwin, curwin->w_topline,
1558 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1559 if (size > width1)
1560 {
1561 curwin->w_skipcol = width1;
1562 size -= width1;
1563 redraw_later(UPD_NOT_VALID);
1564 }
1565 while (size > width2)
1566 {
1567 curwin->w_skipcol += width2;
1568 size -= width2;
1569 }
1570 ++done;
1571 }
1572 else
1573 done += PLINES_NOFILL(curwin->w_topline);
1574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001576 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 invalidate_botline();
1578 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001579 curwin->w_wrow += done; // keep w_wrow updated
1580 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582#ifdef FEAT_DIFF
1583 if (curwin->w_cursor.lnum == curwin->w_topline)
1584 curwin->w_cline_row = 0;
1585 check_topfill(curwin, TRUE);
1586#endif
1587
1588 /*
1589 * Compute the row number of the last row of the cursor line
1590 * and move the cursor onto the displayed part of the window.
1591 */
1592 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001593 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 validate_virtcol();
1596 validate_cheight();
1597 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001598 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1601 {
1602#ifdef FEAT_FOLDING
1603 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1604 {
1605 --wrow;
1606 if (first == 1)
1607 curwin->w_cursor.lnum = 1;
1608 else
1609 curwin->w_cursor.lnum = first - 1;
1610 }
1611 else
1612#endif
1613 wrow -= plines(curwin->w_cursor.lnum--);
1614 curwin->w_valid &=
1615 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1616 moved = TRUE;
1617 }
1618 if (moved)
1619 {
1620#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001621 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 foldAdjustCursor();
1623#endif
1624 coladvance(curwin->w_curswant);
1625 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001626
1627 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1628 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001629 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1630 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1631
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001632 // make sure the cursor is in the visible text
1633 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001634 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001635 int row = 0;
1636 if (col >= width1)
1637 {
1638 col -= width1;
1639 ++row;
1640 }
1641 if (col > width2)
1642 {
1643 row += col / width2;
1644 col = col % width2;
1645 }
1646 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001647 {
1648 curwin->w_curswant = curwin->w_virtcol
1649 - (row - curwin->w_height + 1) * width2;
1650 coladvance(curwin->w_curswant);
1651 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653}
1654
1655/*
1656 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001659scrollup(
1660 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001661 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001663 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001665 if (do_sms
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666# ifdef FEAT_FOLDING
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001667 || (byfold && hasAnyFolding(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668# endif
1669# ifdef FEAT_DIFF
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001670 || curwin->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671# endif
1672 )
1673 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001674 int width1 = curwin->w_width - curwin_col_off();
1675 int width2 = width1 + curwin_col_off2();
1676 int size = 0;
1677 linenr_T prev_topline = curwin->w_topline;
1678
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001679 if (do_sms)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001680 size = win_linetabsize(curwin, curwin->w_topline,
1681 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1682
1683 // diff mode: first consume "topfill"
1684 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1685 // the line, then advance to the next line.
1686 // folding: count each sequence of folded lines as one logical line.
1687 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 {
1689# ifdef FEAT_DIFF
1690 if (curwin->w_topfill > 0)
1691 --curwin->w_topfill;
1692 else
1693# endif
1694 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001695 linenr_T lnum = curwin->w_topline;
1696
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697# ifdef FEAT_FOLDING
1698 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001699 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 (void)hasFolding(lnum, NULL, &lnum);
1701# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001702 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001703 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001704 // 'smoothscroll': increase "w_skipcol" until it goes over
1705 // the end of the line, then advance to the next line.
1706 int add = curwin->w_skipcol > 0 ? width2 : width1;
1707 curwin->w_skipcol += add;
1708 if (curwin->w_skipcol >= size)
1709 {
1710 if (lnum == curbuf->b_ml.ml_line_count)
1711 {
1712 // at the last screen line, can't scroll further
1713 curwin->w_skipcol -= add;
1714 break;
1715 }
1716 ++lnum;
1717 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001718 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001719 else
1720 {
1721 if (lnum >= curbuf->b_ml.ml_line_count)
1722 break;
1723 ++lnum;
1724 }
1725
1726 if (lnum > curwin->w_topline)
1727 {
1728 // approximate w_botline
1729 curwin->w_botline += lnum - curwin->w_topline;
1730 curwin->w_topline = lnum;
1731# ifdef FEAT_DIFF
1732 curwin->w_topfill = diff_check_fill(curwin, lnum);
1733# endif
1734 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001735 if (todo > 1 && do_sms)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001736 size = win_linetabsize(curwin, curwin->w_topline,
1737 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1738 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001739 }
1740 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001741
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001742 if (curwin->w_topline == prev_topline)
1743 // need to redraw even though w_topline didn't change
1744 redraw_later(UPD_NOT_VALID);
1745 }
1746 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 {
1748 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001749 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
1751
1752 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1753 curwin->w_topline = curbuf->b_ml.ml_line_count;
1754 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1755 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1756
1757#ifdef FEAT_DIFF
1758 check_topfill(curwin, FALSE);
1759#endif
1760
1761#ifdef FEAT_FOLDING
1762 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001763 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1765#endif
1766
1767 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1768 if (curwin->w_cursor.lnum < curwin->w_topline)
1769 {
1770 curwin->w_cursor.lnum = curwin->w_topline;
1771 curwin->w_valid &=
1772 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1773 coladvance(curwin->w_curswant);
1774 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001775 if (curwin->w_cursor.lnum == curwin->w_topline
1776 && do_sms && curwin->w_skipcol > 0)
1777 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001778 int width1 = curwin->w_width - curwin_col_off();
1779 int width2 = width1 + curwin_col_off2();
1780 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1781 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1782
1783 // Make sure the cursor is in a visible part of the line, taking
1784 // 'scrolloff' into account, but using screen lines.
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001785 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001786 if (curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001787 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001788 colnr_T col = curwin->w_virtcol;
1789
1790 if (col < width1)
1791 col += width1;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001792 while (col < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001793 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001794 curwin->w_curswant = col;
1795 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001796
1797 // validate_virtcol() marked various things as valid, but after
1798 // moving the cursor they need to be recomputed
1799 curwin->w_valid &=
1800 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001801 }
1802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803}
1804
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001805/*
1806 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1807 * valid for 'smoothscroll'.
1808 */
1809 void
1810adjust_skipcol(void)
1811{
1812 if (!curwin->w_p_wrap
1813 || !curwin->w_p_sms
1814 || curwin->w_cursor.lnum != curwin->w_topline)
1815 return;
1816
1817 int width1 = curwin->w_width - curwin_col_off();
1818 int width2 = width1 + curwin_col_off2();
1819 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1820 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1821 int scrolled = FALSE;
1822
1823 validate_virtcol();
1824 while (curwin->w_skipcol > 0
1825 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1826 {
1827 // scroll a screen line down
1828 if (curwin->w_skipcol >= width1 + width2)
1829 curwin->w_skipcol -= width2;
1830 else
1831 curwin->w_skipcol -= width1;
1832 redraw_later(UPD_NOT_VALID);
1833 scrolled = TRUE;
1834 validate_virtcol();
1835 }
1836 if (scrolled)
1837 return; // don't scroll in the other direction now
1838
1839 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1840 int row = 0;
1841 if (col >= width1)
1842 {
1843 col -= width1;
1844 ++row;
1845 }
1846 if (col > width2)
1847 {
1848 row += col / width2;
1849 col = col % width2;
1850 }
1851 if (row >= curwin->w_height)
1852 {
1853 if (curwin->w_skipcol == 0)
1854 {
1855 curwin->w_skipcol += width1;
1856 --row;
1857 }
1858 if (row >= curwin->w_height)
1859 curwin->w_skipcol += (row - curwin->w_height) * width2;
1860 redraw_later(UPD_NOT_VALID);
1861 }
1862}
1863
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864#ifdef FEAT_DIFF
1865/*
1866 * Don't end up with too many filler lines in the window.
1867 */
1868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001869check_topfill(
1870 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001871 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872{
1873 int n;
1874
1875 if (wp->w_topfill > 0)
1876 {
1877 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1878 if (wp->w_topfill + n > wp->w_height)
1879 {
1880 if (down && wp->w_topline > 1)
1881 {
1882 --wp->w_topline;
1883 wp->w_topfill = 0;
1884 }
1885 else
1886 {
1887 wp->w_topfill = wp->w_height - n;
1888 if (wp->w_topfill < 0)
1889 wp->w_topfill = 0;
1890 }
1891 }
1892 }
1893}
1894
1895/*
1896 * Use as many filler lines as possible for w_topline. Make sure w_topline
1897 * is still visible.
1898 */
1899 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001900max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 int n;
1903
1904 n = plines_nofill(curwin->w_topline);
1905 if (n >= curwin->w_height)
1906 curwin->w_topfill = 0;
1907 else
1908 {
1909 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1910 if (curwin->w_topfill + n > curwin->w_height)
1911 curwin->w_topfill = curwin->w_height - n;
1912 }
1913}
1914#endif
1915
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916/*
1917 * Scroll the screen one line down, but don't do it if it would move the
1918 * cursor off the screen.
1919 */
1920 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001921scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922{
1923 int end_row;
1924#ifdef FEAT_DIFF
1925 int can_fill = (curwin->w_topfill
1926 < diff_check_fill(curwin, curwin->w_topline));
1927#endif
1928
1929 if (curwin->w_topline <= 1
1930#ifdef FEAT_DIFF
1931 && !can_fill
1932#endif
1933 )
1934 return;
1935
Bram Moolenaar85a20022019-12-21 18:25:54 +01001936 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 /*
1939 * Compute the row number of the last row of the cursor line
1940 * and make sure it doesn't go off the screen. Make sure the cursor
1941 * doesn't go past 'scrolloff' lines from the screen end.
1942 */
1943 end_row = curwin->w_wrow;
1944#ifdef FEAT_DIFF
1945 if (can_fill)
1946 ++end_row;
1947 else
1948 end_row += plines_nofill(curwin->w_topline - 1);
1949#else
1950 end_row += plines(curwin->w_topline - 1);
1951#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001952 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 validate_cheight();
1955 validate_virtcol();
1956 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001957 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001959 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961#ifdef FEAT_DIFF
1962 if (can_fill)
1963 {
1964 ++curwin->w_topfill;
1965 check_topfill(curwin, TRUE);
1966 }
1967 else
1968 {
1969 --curwin->w_topline;
1970 curwin->w_topfill = 0;
1971 }
1972#else
1973 --curwin->w_topline;
1974#endif
1975#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001976 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001978 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1980 }
1981}
1982
1983/*
1984 * Scroll the screen one line up, but don't do it if it would move the cursor
1985 * off the screen.
1986 */
1987 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001988scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 int start_row;
1991
1992 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1993#ifdef FEAT_DIFF
1994 && curwin->w_topfill == 0
1995#endif
1996 )
1997 return;
1998
Bram Moolenaar85a20022019-12-21 18:25:54 +01001999 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 /*
2002 * Compute the row number of the first row of the cursor line
2003 * and make sure it doesn't go off the screen. Make sure the cursor
2004 * doesn't go before 'scrolloff' lines from the screen start.
2005 */
2006#ifdef FEAT_DIFF
2007 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2008 - curwin->w_topfill;
2009#else
2010 start_row = curwin->w_wrow - plines(curwin->w_topline);
2011#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002012 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
2014 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002015 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002017 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 {
2019#ifdef FEAT_DIFF
2020 if (curwin->w_topfill > 0)
2021 --curwin->w_topfill;
2022 else
2023#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002024 {
2025#ifdef FEAT_FOLDING
2026 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002029 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002030 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2032 }
2033}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035/*
2036 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2037 * a (wrapped) text line. Uses and sets "lp->fill".
2038 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002039 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 */
2041 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002042topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044#ifdef FEAT_DIFF
2045 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2046 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002047 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 ++lp->fill;
2049 lp->height = 1;
2050 }
2051 else
2052#endif
2053 {
2054 --lp->lnum;
2055#ifdef FEAT_DIFF
2056 lp->fill = 0;
2057#endif
2058 if (lp->lnum < 1)
2059 lp->height = MAXCOL;
2060 else
2061#ifdef FEAT_FOLDING
2062 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002063 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 lp->height = 1;
2065 else
2066#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002067 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
2069}
2070
2071/*
2072 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2073 * a (wrapped) text line. Uses and sets "lp->fill".
2074 * Returns the height of the added line in "lp->height".
2075 * Lines below the last one are incredibly high.
2076 */
2077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002078botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079{
2080#ifdef FEAT_DIFF
2081 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2082 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002083 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 ++lp->fill;
2085 lp->height = 1;
2086 }
2087 else
2088#endif
2089 {
2090 ++lp->lnum;
2091#ifdef FEAT_DIFF
2092 lp->fill = 0;
2093#endif
2094 if (lp->lnum > curbuf->b_ml.ml_line_count)
2095 lp->height = MAXCOL;
2096 else
2097#ifdef FEAT_FOLDING
2098 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002099 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 lp->height = 1;
2101 else
2102#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002103 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105}
2106
2107#ifdef FEAT_DIFF
2108/*
2109 * Switch from including filler lines below lp->lnum to including filler
2110 * lines above loff.lnum + 1. This keeps pointing to the same line.
2111 * When there are no filler lines nothing changes.
2112 */
2113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002114botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 if (lp->fill > 0)
2117 {
2118 ++lp->lnum;
2119 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2120 }
2121}
2122
2123/*
2124 * Switch from including filler lines above lp->lnum to including filler
2125 * lines below loff.lnum - 1. This keeps pointing to the same line.
2126 * When there are no filler lines nothing changes.
2127 */
2128 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002129topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
2131 if (lp->fill > 0)
2132 {
2133 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2134 --lp->lnum;
2135 }
2136}
2137#endif
2138
2139/*
2140 * Recompute topline to put the cursor at the top of the window.
2141 * Scroll at least "min_scroll" lines.
2142 * If "always" is TRUE, always set topline (for "zt").
2143 */
2144 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002145scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
2147 int scrolled = 0;
2148 int extra = 0;
2149 int used;
2150 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 linenr_T top; // just above displayed lines
2152 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002154 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#ifdef FEAT_DIFF
2156 linenr_T old_topfill = curwin->w_topfill;
2157#endif
2158 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002159 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (mouse_dragging > 0)
2162 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164 /*
2165 * Decrease topline until:
2166 * - it has become 1
2167 * - (part of) the cursor line is moved off the screen or
2168 * - moved at least 'scrolljump' lines and
2169 * - at least 'scrolloff' lines above and below the cursor
2170 */
2171 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002172 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (curwin->w_cursor.lnum < curwin->w_topline)
2174 scrolled = used;
2175
2176#ifdef FEAT_FOLDING
2177 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2178 {
2179 --top;
2180 ++bot;
2181 }
2182 else
2183#endif
2184 {
2185 top = curwin->w_cursor.lnum - 1;
2186 bot = curwin->w_cursor.lnum + 1;
2187 }
2188 new_topline = top + 1;
2189
2190#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002191 // "used" already contains the number of filler lines above, don't add it
2192 // again.
2193 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002194 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
2196
2197 /*
2198 * Check if the lines from "top" to "bot" fit in the window. If they do,
2199 * set new_topline and advance "top" and "bot" to include more lines.
2200 */
2201 while (top > 0)
2202 {
2203#ifdef FEAT_FOLDING
2204 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002205 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 i = 1;
2207 else
2208#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002209 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 used += i;
2211 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2212 {
2213#ifdef FEAT_FOLDING
2214 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002215 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 ++used;
2217 else
2218#endif
2219 used += plines(bot);
2220 }
2221 if (used > curwin->w_height)
2222 break;
2223 if (top < curwin->w_topline)
2224 scrolled += i;
2225
2226 /*
2227 * If scrolling is needed, scroll at least 'sj' lines.
2228 */
2229 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2230 && extra >= off)
2231 break;
2232
2233 extra += i;
2234 new_topline = top;
2235 --top;
2236 ++bot;
2237 }
2238
2239 /*
2240 * If we don't have enough space, put cursor in the middle.
2241 * This makes sure we get the same position when using "k" and "j"
2242 * in a small window.
2243 */
2244 if (used > curwin->w_height)
2245 scroll_cursor_halfway(FALSE);
2246 else
2247 {
2248 /*
2249 * If "always" is FALSE, only adjust topline to a lower value, higher
2250 * value may happen with wrapping lines
2251 */
2252 if (new_topline < curwin->w_topline || always)
2253 curwin->w_topline = new_topline;
2254 if (curwin->w_topline > curwin->w_cursor.lnum)
2255 curwin->w_topline = curwin->w_cursor.lnum;
2256#ifdef FEAT_DIFF
2257 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2258 if (curwin->w_topfill > 0 && extra > off)
2259 {
2260 curwin->w_topfill -= extra - off;
2261 if (curwin->w_topfill < 0)
2262 curwin->w_topfill = 0;
2263 }
2264 check_topfill(curwin, FALSE);
2265#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002266 // TODO: if the line doesn't fit may optimize w_skipcol
2267 if (curwin->w_topline == curwin->w_cursor.lnum)
2268 {
2269 curwin->w_skipcol = 0;
2270 redraw_later(UPD_NOT_VALID);
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002273 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#ifdef FEAT_DIFF
2275 || curwin->w_topfill != old_topfill
2276#endif
2277 )
2278 curwin->w_valid &=
2279 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2280 curwin->w_valid |= VALID_TOPLINE;
2281 }
2282}
2283
2284/*
2285 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2286 * screen lines for text lines.
2287 */
2288 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002289set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291#ifdef FEAT_DIFF
2292 wp->w_filler_rows = 0;
2293#endif
2294 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002295 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 else
2297 {
2298 wp->w_empty_rows = wp->w_height - used;
2299#ifdef FEAT_DIFF
2300 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2301 {
2302 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2303 if (wp->w_empty_rows > wp->w_filler_rows)
2304 wp->w_empty_rows -= wp->w_filler_rows;
2305 else
2306 {
2307 wp->w_filler_rows = wp->w_empty_rows;
2308 wp->w_empty_rows = 0;
2309 }
2310 }
2311#endif
2312 }
2313}
2314
2315/*
2316 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002317 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2319 * This is messy stuff!!!
2320 */
2321 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002322scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323{
2324 int used;
2325 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002326 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int extra = 0;
2328 int i;
2329 linenr_T line_count;
2330 linenr_T old_topline = curwin->w_topline;
2331 lineoff_T loff;
2332 lineoff_T boff;
2333#ifdef FEAT_DIFF
2334 int old_topfill = curwin->w_topfill;
2335 int fill_below_window;
2336#endif
2337 linenr_T old_botline = curwin->w_botline;
2338 linenr_T old_valid = curwin->w_valid;
2339 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002340 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002341 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343 cln = curwin->w_cursor.lnum;
2344 if (set_topbot)
2345 {
2346 used = 0;
2347 curwin->w_botline = cln + 1;
2348#ifdef FEAT_DIFF
2349 loff.fill = 0;
2350#endif
2351 for (curwin->w_topline = curwin->w_botline;
2352 curwin->w_topline > 1;
2353 curwin->w_topline = loff.lnum)
2354 {
2355 loff.lnum = curwin->w_topline;
2356 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002357 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 break;
2359 used += loff.height;
2360#ifdef FEAT_DIFF
2361 curwin->w_topfill = loff.fill;
2362#endif
2363 }
2364 set_empty_rows(curwin, used);
2365 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2366 if (curwin->w_topline != old_topline
2367#ifdef FEAT_DIFF
2368 || curwin->w_topfill != old_topfill
2369#endif
2370 )
2371 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2372 }
2373 else
2374 validate_botline();
2375
Bram Moolenaar85a20022019-12-21 18:25:54 +01002376 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377#ifdef FEAT_DIFF
2378 used = plines_nofill(cln);
2379#else
2380 validate_cheight();
2381 used = curwin->w_cline_height;
2382#endif
2383
Bram Moolenaar85a20022019-12-21 18:25:54 +01002384 // If the cursor is below botline, we will at least scroll by the height
2385 // of the cursor line. Correct for empty lines, which are really part of
2386 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (cln >= curwin->w_botline)
2388 {
2389 scrolled = used;
2390 if (cln == curwin->w_botline)
2391 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002392 min_scrolled = scrolled;
2393 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2394 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
Bram Moolenaar4d31b482022-10-06 16:03:09 +01002395 min_scrolled += PLINES_NOFILL(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397
2398 /*
2399 * Stop counting lines to scroll when
2400 * - hitting start of the file
2401 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002402 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 * - lines between botline and cursor have been counted
2404 */
2405#ifdef FEAT_FOLDING
2406 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2407#endif
2408 {
2409 loff.lnum = cln;
2410 boff.lnum = cln;
2411 }
2412#ifdef FEAT_DIFF
2413 loff.fill = 0;
2414 boff.fill = 0;
2415 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2416 - curwin->w_filler_rows;
2417#endif
2418
2419 while (loff.lnum > 1)
2420 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002421 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2422 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002424 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2426 && loff.lnum <= curwin->w_botline
2427#ifdef FEAT_DIFF
2428 && (loff.lnum < curwin->w_botline
2429 || loff.fill >= fill_below_window)
2430#endif
2431 )
2432 break;
2433
Bram Moolenaar85a20022019-12-21 18:25:54 +01002434 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002436 if (loff.height == MAXCOL)
2437 used = MAXCOL;
2438 else
2439 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (used > curwin->w_height)
2441 break;
2442 if (loff.lnum >= curwin->w_botline
2443#ifdef FEAT_DIFF
2444 && (loff.lnum > curwin->w_botline
2445 || loff.fill <= fill_below_window)
2446#endif
2447 )
2448 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002449 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 scrolled += loff.height;
2451 if (loff.lnum == curwin->w_botline
2452#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002453 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#endif
2455 )
2456 scrolled -= curwin->w_empty_rows;
2457 }
2458
2459 if (boff.lnum < curbuf->b_ml.ml_line_count)
2460 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002461 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 botline_forw(&boff);
2463 used += boff.height;
2464 if (used > curwin->w_height)
2465 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002466 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2467 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 extra += boff.height;
2470 if (boff.lnum >= curwin->w_botline
2471#ifdef FEAT_DIFF
2472 || (boff.lnum + 1 == curwin->w_botline
2473 && boff.fill > curwin->w_filler_rows)
2474#endif
2475 )
2476 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002477 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 scrolled += boff.height;
2479 if (boff.lnum == curwin->w_botline
2480#ifdef FEAT_DIFF
2481 && boff.fill == 0
2482#endif
2483 )
2484 scrolled -= curwin->w_empty_rows;
2485 }
2486 }
2487 }
2488 }
2489
Bram Moolenaar85a20022019-12-21 18:25:54 +01002490 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (scrolled <= 0)
2492 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002493 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 else if (used > curwin->w_height)
2495 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002496 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 else
2498 {
2499 line_count = 0;
2500#ifdef FEAT_DIFF
2501 boff.fill = curwin->w_topfill;
2502#endif
2503 boff.lnum = curwin->w_topline - 1;
2504 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2505 {
2506 botline_forw(&boff);
2507 i += boff.height;
2508 ++line_count;
2509 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002510 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 line_count = 9999;
2512 }
2513
2514 /*
2515 * Scroll up if the cursor is off the bottom of the screen a bit.
2516 * Otherwise put it at 1/2 of the screen.
2517 */
2518 if (line_count >= curwin->w_height && line_count > min_scroll)
2519 scroll_cursor_halfway(FALSE);
2520 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002521 {
2522 // With 'smoothscroll' scroll at least the height of the cursor line.
2523 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled)
2524 line_count = min_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528 /*
2529 * If topline didn't change we need to restore w_botline and w_empty_rows
2530 * (we changed them).
2531 * If topline did change, update_screen() will set botline.
2532 */
2533 if (curwin->w_topline == old_topline && set_topbot)
2534 {
2535 curwin->w_botline = old_botline;
2536 curwin->w_empty_rows = old_empty_rows;
2537 curwin->w_valid = old_valid;
2538 }
2539 curwin->w_valid |= VALID_TOPLINE;
2540}
2541
2542/*
2543 * Recompute topline to put the cursor halfway the window
2544 * If "atend" is TRUE, also put it halfway at the end of the file.
2545 */
2546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002547scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
2549 int above = 0;
2550 linenr_T topline;
2551#ifdef FEAT_DIFF
2552 int topfill = 0;
2553#endif
2554 int below = 0;
2555 int used;
2556 lineoff_T loff;
2557 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002558#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002559 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002562#ifdef FEAT_PROP_POPUP
2563 // if the width changed this needs to be updated first
2564 may_update_popup_position();
2565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2567#ifdef FEAT_FOLDING
2568 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2569#endif
2570#ifdef FEAT_DIFF
2571 used = plines_nofill(loff.lnum);
2572 loff.fill = 0;
2573 boff.fill = 0;
2574#else
2575 used = plines(loff.lnum);
2576#endif
2577 topline = loff.lnum;
2578 while (topline > 1)
2579 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002580 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
2582 if (boff.lnum < curbuf->b_ml.ml_line_count)
2583 {
2584 botline_forw(&boff);
2585 used += boff.height;
2586 if (used > curwin->w_height)
2587 break;
2588 below += boff.height;
2589 }
2590 else
2591 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002592 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (atend)
2594 ++used;
2595 }
2596 }
2597
Bram Moolenaar85a20022019-12-21 18:25:54 +01002598 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
2600 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002601 if (loff.height == MAXCOL)
2602 used = MAXCOL;
2603 else
2604 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (used > curwin->w_height)
2606 break;
2607 above += loff.height;
2608 topline = loff.lnum;
2609#ifdef FEAT_DIFF
2610 topfill = loff.fill;
2611#endif
2612 }
2613 }
2614#ifdef FEAT_FOLDING
2615 if (!hasFolding(topline, &curwin->w_topline, NULL))
2616#endif
2617 curwin->w_topline = topline;
2618#ifdef FEAT_DIFF
2619 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002620 if (old_topline > curwin->w_topline + curwin->w_height)
2621 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 check_topfill(curwin, FALSE);
2623#endif
2624 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2625 curwin->w_valid |= VALID_TOPLINE;
2626}
2627
2628/*
2629 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002630 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * If not possible, put it at the same position as scroll_cursor_halfway().
2632 * When called topline must be valid!
2633 */
2634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002635cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002637 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002639 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 linenr_T botline;
2641 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002642 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002644 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 /*
2647 * How many lines we would like to have above/below the cursor depends on
2648 * whether the first/last line of the file is on screen.
2649 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002650 above_wanted = so;
2651 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002652 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 above_wanted = mouse_dragging - 1;
2655 below_wanted = mouse_dragging - 1;
2656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (curwin->w_topline == 1)
2658 {
2659 above_wanted = 0;
2660 max_off = curwin->w_height / 2;
2661 if (below_wanted > max_off)
2662 below_wanted = max_off;
2663 }
2664 validate_botline();
2665 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002666 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
2668 below_wanted = 0;
2669 max_off = (curwin->w_height - 1) / 2;
2670 if (above_wanted > max_off)
2671 above_wanted = max_off;
2672 }
2673
2674 /*
2675 * If there are sufficient file-lines above and below the cursor, we can
2676 * return now.
2677 */
2678 cln = curwin->w_cursor.lnum;
2679 if (cln >= curwin->w_topline + above_wanted
2680 && cln < curwin->w_botline - below_wanted
2681#ifdef FEAT_FOLDING
2682 && !hasAnyFolding(curwin)
2683#endif
2684 )
2685 return;
2686
2687 /*
2688 * Narrow down the area where the cursor can be put by taking lines from
2689 * the top and the bottom until:
2690 * - the desired context lines are found
2691 * - the lines from the top is past the lines from the bottom
2692 */
2693 topline = curwin->w_topline;
2694 botline = curwin->w_botline - 1;
2695#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002696 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 above = curwin->w_topfill;
2698 below = curwin->w_filler_rows;
2699#endif
2700 while ((above < above_wanted || below < below_wanted) && topline < botline)
2701 {
2702 if (below < below_wanted && (below <= above || above >= above_wanted))
2703 {
2704#ifdef FEAT_FOLDING
2705 if (hasFolding(botline, &botline, NULL))
2706 ++below;
2707 else
2708#endif
2709 below += plines(botline);
2710 --botline;
2711 }
2712 if (above < above_wanted && (above < below || below >= below_wanted))
2713 {
2714#ifdef FEAT_FOLDING
2715 if (hasFolding(topline, NULL, &topline))
2716 ++above;
2717 else
2718#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002719 above += PLINES_NOFILL(topline);
2720#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002721 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (topline < botline)
2723 above += diff_check_fill(curwin, topline + 1);
2724#endif
2725 ++topline;
2726 }
2727 }
2728 if (topline == botline || botline == 0)
2729 curwin->w_cursor.lnum = topline;
2730 else if (topline > botline)
2731 curwin->w_cursor.lnum = botline;
2732 else
2733 {
2734 if (cln < topline && curwin->w_topline > 1)
2735 {
2736 curwin->w_cursor.lnum = topline;
2737 curwin->w_valid &=
2738 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2739 }
2740 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2741 {
2742 curwin->w_cursor.lnum = botline;
2743 curwin->w_valid &=
2744 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2745 }
2746 }
2747 curwin->w_valid |= VALID_TOPLINE;
2748}
2749
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002750static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752/*
2753 * move screen 'count' pages up or down and update screen
2754 *
2755 * return FAIL for failure, OK otherwise
2756 */
2757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002758onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
2760 long n;
2761 int retval = OK;
2762 lineoff_T loff;
2763 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002764 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar85a20022019-12-21 18:25:54 +01002766 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
2768 beep_flush();
2769 return FAIL;
2770 }
2771
2772 for ( ; count > 0; --count)
2773 {
2774 validate_botline();
2775 /*
2776 * It's an error to move a page up when the first line is already on
2777 * the screen. It's an error to move a page down when the last line
2778 * is on the screen and the topline is 'scrolloff' lines from the
2779 * last line.
2780 */
2781 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002782 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2784 : (curwin->w_topline == 1
2785#ifdef FEAT_DIFF
2786 && curwin->w_topfill ==
2787 diff_check_fill(curwin, curwin->w_topline)
2788#endif
2789 ))
2790 {
2791 beep_flush();
2792 retval = FAIL;
2793 break;
2794 }
2795
2796#ifdef FEAT_DIFF
2797 loff.fill = 0;
2798#endif
2799 if (dir == FORWARD)
2800 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002801 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002803 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002804 if (p_window <= 2)
2805 ++curwin->w_topline;
2806 else
2807 curwin->w_topline += p_window - 2;
2808 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2809 curwin->w_topline = curbuf->b_ml.ml_line_count;
2810 curwin->w_cursor.lnum = curwin->w_topline;
2811 }
2812 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2813 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002814 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 curwin->w_topline = curbuf->b_ml.ml_line_count;
2816#ifdef FEAT_DIFF
2817 curwin->w_topfill = 0;
2818#endif
2819 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2820 }
2821 else
2822 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002823 // For the overlap, start with the line just below the window
2824 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 loff.lnum = curwin->w_botline;
2826#ifdef FEAT_DIFF
2827 loff.fill = diff_check_fill(curwin, loff.lnum)
2828 - curwin->w_filler_rows;
2829#endif
2830 get_scroll_overlap(&loff, -1);
2831 curwin->w_topline = loff.lnum;
2832#ifdef FEAT_DIFF
2833 curwin->w_topfill = loff.fill;
2834 check_topfill(curwin, FALSE);
2835#endif
2836 curwin->w_cursor.lnum = curwin->w_topline;
2837 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2838 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2839 }
2840 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002841 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
2843#ifdef FEAT_DIFF
2844 if (curwin->w_topline == 1)
2845 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002846 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 max_topfill();
2848 continue;
2849 }
2850#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002851 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002852 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002853 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002854 if (p_window <= 2)
2855 --curwin->w_topline;
2856 else
2857 curwin->w_topline -= p_window - 2;
2858 if (curwin->w_topline < 1)
2859 curwin->w_topline = 1;
2860 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2861 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2862 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2863 continue;
2864 }
2865
Bram Moolenaar85a20022019-12-21 18:25:54 +01002866 // Find the line at the top of the window that is going to be the
2867 // line at the bottom of the window. Make sure this results in
2868 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 loff.lnum = curwin->w_topline - 1;
2870#ifdef FEAT_DIFF
2871 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2872 - curwin->w_topfill;
2873#endif
2874 get_scroll_overlap(&loff, 1);
2875
2876 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2877 {
2878 loff.lnum = curbuf->b_ml.ml_line_count;
2879#ifdef FEAT_DIFF
2880 loff.fill = 0;
2881 }
2882 else
2883 {
2884 botline_topline(&loff);
2885#endif
2886 }
2887 curwin->w_cursor.lnum = loff.lnum;
2888
Bram Moolenaar85a20022019-12-21 18:25:54 +01002889 // Find the line just above the new topline to get the right line
2890 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 n = 0;
2892 while (n <= curwin->w_height && loff.lnum >= 1)
2893 {
2894 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002895 if (loff.height == MAXCOL)
2896 n = MAXCOL;
2897 else
2898 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002900 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
2902 curwin->w_topline = 1;
2903#ifdef FEAT_DIFF
2904 max_topfill();
2905#endif
2906 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2907 }
2908 else
2909 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002910 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911#ifdef FEAT_DIFF
2912 topline_botline(&loff);
2913#endif
2914 botline_forw(&loff);
2915 botline_forw(&loff);
2916#ifdef FEAT_DIFF
2917 botline_topline(&loff);
2918#endif
2919#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002920 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2922#endif
2923
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 // Always scroll at least one line. Avoid getting stuck on
2925 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (loff.lnum >= curwin->w_topline
2927#ifdef FEAT_DIFF
2928 && (loff.lnum > curwin->w_topline
2929 || loff.fill >= curwin->w_topfill)
2930#endif
2931 )
2932 {
2933#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002934 // First try using the maximum number of filler lines. If
2935 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 loff.fill = curwin->w_topfill;
2937 if (curwin->w_topfill < diff_check_fill(curwin,
2938 curwin->w_topline))
2939 max_topfill();
2940 if (curwin->w_topfill == loff.fill)
2941#endif
2942 {
2943 --curwin->w_topline;
2944#ifdef FEAT_DIFF
2945 curwin->w_topfill = 0;
2946#endif
2947 }
2948 comp_botline(curwin);
2949 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002950 curwin->w_valid &=
2951 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 else
2954 {
2955 curwin->w_topline = loff.lnum;
2956#ifdef FEAT_DIFF
2957 curwin->w_topfill = loff.fill;
2958 check_topfill(curwin, FALSE);
2959#endif
2960 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2961 }
2962 }
2963 }
2964 }
2965#ifdef FEAT_FOLDING
2966 foldAdjustCursor();
2967#endif
2968 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002969 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002970 if (retval == OK)
2971 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2973
Bram Moolenaar907dad72018-07-10 15:07:15 +02002974 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002976 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2977 // But make sure we scroll at least one line (happens with mix of long
2978 // wrapping lines and non-wrapping line).
2979 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002981 scroll_cursor_top(1, FALSE);
2982 if (curwin->w_topline <= old_topline
2983 && old_topline < curbuf->b_ml.ml_line_count)
2984 {
2985 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002987 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2988#endif
2989 }
2990 }
2991#ifdef FEAT_FOLDING
2992 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002997 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 return retval;
2999}
3000
3001/*
3002 * Decide how much overlap to use for page-up or page-down scrolling.
3003 * This is symmetric, so that doing both keeps the same lines displayed.
3004 * Three lines are examined:
3005 *
3006 * before CTRL-F after CTRL-F / before CTRL-B
3007 * etc. l1
3008 * l1 last but one line ------------
3009 * l2 last text line l2 top text line
3010 * ------------- l3 second text line
3011 * l3 etc.
3012 */
3013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003014get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 int h1, h2, h3, h4;
3017 int min_height = curwin->w_height - 2;
3018 lineoff_T loff0, loff1, loff2;
3019
3020#ifdef FEAT_DIFF
3021 if (lp->fill > 0)
3022 lp->height = 1;
3023 else
3024 lp->height = plines_nofill(lp->lnum);
3025#else
3026 lp->height = plines(lp->lnum);
3027#endif
3028 h1 = lp->height;
3029 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003030 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
3032 loff0 = *lp;
3033 if (dir > 0)
3034 botline_forw(lp);
3035 else
3036 topline_back(lp);
3037 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003038 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003040 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 return;
3042 }
3043
3044 loff1 = *lp;
3045 if (dir > 0)
3046 botline_forw(lp);
3047 else
3048 topline_back(lp);
3049 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003050 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003052 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 return;
3054 }
3055
3056 loff2 = *lp;
3057 if (dir > 0)
3058 botline_forw(lp);
3059 else
3060 topline_back(lp);
3061 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003062 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003063 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003065 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066}
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068/*
3069 * Scroll 'scroll' lines up or down.
3070 */
3071 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003072halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 long scrolled = 0;
3075 int i;
3076 int n;
3077 int room;
3078
3079 if (Prenum)
3080 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3081 curwin->w_height : Prenum;
3082 n = (curwin->w_p_scr <= curwin->w_height) ?
3083 curwin->w_p_scr : curwin->w_height;
3084
Bram Moolenaard5d37532017-03-27 23:02:07 +02003085 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 validate_botline();
3087 room = curwin->w_empty_rows;
3088#ifdef FEAT_DIFF
3089 room += curwin->w_filler_rows;
3090#endif
3091 if (flag)
3092 {
3093 /*
3094 * scroll the text up
3095 */
3096 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3097 {
3098#ifdef FEAT_DIFF
3099 if (curwin->w_topfill > 0)
3100 {
3101 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003102 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 --curwin->w_topfill;
3104 }
3105 else
3106#endif
3107 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003108 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 n -= i;
3110 if (n < 0 && scrolled > 0)
3111 break;
3112#ifdef FEAT_FOLDING
3113 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3114#endif
3115 ++curwin->w_topline;
3116#ifdef FEAT_DIFF
3117 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3118#endif
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3121 {
3122 ++curwin->w_cursor.lnum;
3123 curwin->w_valid &=
3124 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
3127 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3128 scrolled += i;
3129
3130 /*
3131 * Correct w_botline for changed w_topline.
3132 * Won't work when there are filler lines.
3133 */
3134#ifdef FEAT_DIFF
3135 if (curwin->w_p_diff)
3136 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3137 else
3138#endif
3139 {
3140 room += i;
3141 do
3142 {
3143 i = plines(curwin->w_botline);
3144 if (i > room)
3145 break;
3146#ifdef FEAT_FOLDING
3147 (void)hasFolding(curwin->w_botline, NULL,
3148 &curwin->w_botline);
3149#endif
3150 ++curwin->w_botline;
3151 room -= i;
3152 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3153 }
3154 }
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 /*
3157 * When hit bottom of the file: move cursor down.
3158 */
3159 if (n > 0)
3160 {
3161# ifdef FEAT_FOLDING
3162 if (hasAnyFolding(curwin))
3163 {
3164 while (--n >= 0
3165 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3166 {
3167 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3168 &curwin->w_cursor.lnum);
3169 ++curwin->w_cursor.lnum;
3170 }
3171 }
3172 else
3173# endif
3174 curwin->w_cursor.lnum += n;
3175 check_cursor_lnum();
3176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178 else
3179 {
3180 /*
3181 * scroll the text down
3182 */
3183 while (n > 0 && curwin->w_topline > 1)
3184 {
3185#ifdef FEAT_DIFF
3186 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3187 {
3188 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003189 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 ++curwin->w_topfill;
3191 }
3192 else
3193#endif
3194 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003195 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 n -= i;
3197 if (n < 0 && scrolled > 0)
3198 break;
3199 --curwin->w_topline;
3200#ifdef FEAT_FOLDING
3201 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3202#endif
3203#ifdef FEAT_DIFF
3204 curwin->w_topfill = 0;
3205#endif
3206 }
3207 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3208 VALID_BOTLINE|VALID_BOTLINE_AP);
3209 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (curwin->w_cursor.lnum > 1)
3211 {
3212 --curwin->w_cursor.lnum;
3213 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 /*
3218 * When hit top of the file: move cursor up.
3219 */
3220 if (n > 0)
3221 {
3222 if (curwin->w_cursor.lnum <= (linenr_T)n)
3223 curwin->w_cursor.lnum = 1;
3224 else
3225# ifdef FEAT_FOLDING
3226 if (hasAnyFolding(curwin))
3227 {
3228 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3229 {
3230 --curwin->w_cursor.lnum;
3231 (void)hasFolding(curwin->w_cursor.lnum,
3232 &curwin->w_cursor.lnum, NULL);
3233 }
3234 }
3235 else
3236# endif
3237 curwin->w_cursor.lnum -= n;
3238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003241 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 foldAdjustCursor();
3243# endif
3244#ifdef FEAT_DIFF
3245 check_topfill(curwin, !flag);
3246#endif
3247 cursor_correct();
3248 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003249 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003251
Bram Moolenaar860cae12010-06-05 23:22:07 +02003252 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003253do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003254{
3255 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003256 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003257 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003258 colnr_T curswant = curwin->w_curswant;
3259 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003260 win_T *old_curwin = curwin;
3261 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003262 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003263 int old_VIsual_select = VIsual_select;
3264 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003265
3266 /*
3267 * loop through the cursorbound windows
3268 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003269 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003270 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003271 {
3272 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003273 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003274 if (curwin != old_curwin && curwin->w_p_crb)
3275 {
3276# ifdef FEAT_DIFF
3277 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003278 curwin->w_cursor.lnum =
3279 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003280 else
3281# endif
3282 curwin->w_cursor.lnum = line;
3283 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003284 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003285 curwin->w_curswant = curswant;
3286 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003287
Bram Moolenaar85a20022019-12-21 18:25:54 +01003288 // Make sure the cursor is in a valid position. Temporarily set
3289 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003290 restart_edit_save = restart_edit;
3291 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003292 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003293
3294 // Avoid a scroll here for the cursor position, 'scrollbind' is
3295 // more important.
3296 if (!curwin->w_p_scb)
3297 validate_cursor();
3298
Bram Moolenaar61452852011-02-01 18:01:11 +01003299 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003300 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003301 if (has_mbyte)
3302 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003303 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003304
Bram Moolenaar85a20022019-12-21 18:25:54 +01003305 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003306 if (!curwin->w_p_scb)
3307 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003308 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003309 }
3310 }
3311
3312 /*
3313 * reset current-window
3314 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003315 VIsual_select = old_VIsual_select;
3316 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003317 curwin = old_curwin;
3318 curbuf = old_curbuf;
3319}