blob: 3a2eb5df89b43c7014a4d6198c2a65e145ff6157 [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;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001039 int width1; // text width for first screen line
1040 int width2 = 0; // text width for second and later screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 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 Moolenaar856c5d22022-10-13 21:54:28 +01001090 width1 = curwin->w_width - extra;
1091 if (width1 <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
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 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001103 width2 = width1 + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001105 // skip columns that are not visible
1106 if (curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001107 && curwin->w_skipcol > 0
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001108 && curwin->w_wcol >= curwin->w_skipcol)
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001109 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001110 // w_skipcol excludes win_col_off(). Include it here, since w_wcol
1111 // counts actual screen columns.
1112 if (curwin->w_skipcol <= width1)
1113 curwin->w_wcol -= curwin->w_width;
1114 else
1115 curwin->w_wcol -= curwin->w_width
1116 * (((curwin->w_skipcol - width1) / width2) + 1);
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001117 did_sub_skipcol = TRUE;
1118 }
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001119
Bram Moolenaar85a20022019-12-21 18:25:54 +01001120 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001121 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001123 // this same formula is used in validate_cursor_col()
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001124 n = (curwin->w_wcol - curwin->w_width) / width2 + 1;
1125 curwin->w_wcol -= n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 curwin->w_wrow += n;
1127
1128#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001129 // When cursor wraps to first char of next line in Insert
1130 // mode, the 'showbreak' string isn't shown, backup to first
1131 // column
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001132 char_u *sbr = get_showbreak_value(curwin);
Bram Moolenaaree857022019-11-09 23:26:40 +01001133 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001134 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 curwin->w_wcol = 0;
1136#endif
1137 }
1138 }
1139
Bram Moolenaar85a20022019-12-21 18:25:54 +01001140 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1141 // is not folded.
1142 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001143 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#ifdef FEAT_FOLDING
1145 && !curwin->w_cline_folded
1146#endif
1147 )
1148 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001149#ifdef FEAT_PROP_POPUP
1150 if (curwin->w_virtcol_first_char > 0)
1151 {
1152 int cols = (curwin->w_width - extra);
1153 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1154
1155 // each "above" text prop shifts the text one row down
1156 curwin->w_wrow += rows;
1157 curwin->w_wcol -= rows * cols;
1158 endcol -= rows * cols;
1159 curwin->w_cline_height = rows + 1;
1160 }
1161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 /*
1163 * If Cursor is left of the screen, scroll rightwards.
1164 * If Cursor is right of the screen, scroll leftwards
1165 * If we get closer to the edge than 'sidescrolloff', scroll a little
1166 * extra
1167 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001168 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001169 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001170 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (off_left < 0 || off_right > 0)
1172 {
1173 if (off_left < 0)
1174 diff = -off_left;
1175 else
1176 diff = off_right;
1177
Bram Moolenaar85a20022019-12-21 18:25:54 +01001178 // When far off or not enough room on either side, put cursor in
1179 // middle of window.
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001180 if (p_ss == 0 || diff >= width1 / 2 || off_right >= off_left)
1181 new_leftcol = curwin->w_wcol - extra - width1 / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 else
1183 {
1184 if (diff < p_ss)
1185 diff = p_ss;
1186 if (off_left < 0)
1187 new_leftcol = curwin->w_leftcol - diff;
1188 else
1189 new_leftcol = curwin->w_leftcol + diff;
1190 }
1191 if (new_leftcol < 0)
1192 new_leftcol = 0;
1193 if (new_leftcol != (int)curwin->w_leftcol)
1194 {
1195 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001196 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001197 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199 }
1200 curwin->w_wcol -= curwin->w_leftcol;
1201 }
1202 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1203 curwin->w_wcol -= curwin->w_leftcol;
1204 else
1205 curwin->w_wcol = 0;
1206
1207#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // Skip over filler lines. At the top use w_topfill, there
1209 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (curwin->w_cursor.lnum == curwin->w_topline)
1211 curwin->w_wrow += curwin->w_topfill;
1212 else
1213 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1214#endif
1215
1216 prev_skipcol = curwin->w_skipcol;
1217
1218 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if ((curwin->w_wrow >= curwin->w_height
1221 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001222 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && (p_lines =
1224#ifdef FEAT_DIFF
1225 plines_win_nofill
1226#else
1227 plines_win
1228#endif
1229 (curwin, curwin->w_cursor.lnum, FALSE))
1230 - 1 >= curwin->w_height))
1231 && curwin->w_height != 0
1232 && curwin->w_cursor.lnum == curwin->w_topline
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001233 && width2 > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001234 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001236 // Cursor past end of screen. Happens with a single line that does
1237 // not fit on screen. Find a skipcol to show the text around the
1238 // cursor. Avoid scrolling all the time. compute value of "extra":
1239 // 1: Less than 'scrolloff' lines above
1240 // 2: Less than 'scrolloff' lines below
1241 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 extra = 0;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001243 if (curwin->w_skipcol + so * width2 > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001245 // Compute last display line of the buffer line that we want at the
1246 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (p_lines == 0)
1248 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1249 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001250 if (p_lines > curwin->w_wrow + so)
1251 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 else
1253 n = p_lines;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001254 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width2 - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 extra += 2;
1256
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001257 if (extra == 3 || curwin->w_height <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001259 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001260 n = curwin->w_virtcol / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (n > curwin->w_height / 2)
1262 n -= curwin->w_height / 2;
1263 else
1264 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001265 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 if (n > p_lines - curwin->w_height + 1)
1267 n = p_lines - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001268 curwin->w_skipcol = n * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 else if (extra == 1)
1271 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001272 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001273 extra = (curwin->w_skipcol + so * width2 - curwin->w_virtcol
1274 + width2 - 1) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (extra > 0)
1276 {
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001277 if ((colnr_T)(extra * width2) > curwin->w_skipcol)
1278 extra = curwin->w_skipcol / width2;
1279 curwin->w_skipcol -= extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 }
1282 else if (extra == 2)
1283 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001284 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001285 endcol = (n - curwin->w_height + 1) * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 while (endcol > curwin->w_virtcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001287 endcol -= width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (endcol > curwin->w_skipcol)
1289 curwin->w_skipcol = endcol;
1290 }
1291
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001292 // adjust w_wrow for the changed w_skipcol
1293 if (did_sub_skipcol)
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001294 curwin->w_wrow -= (curwin->w_skipcol - prev_skipcol) / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001295 else
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001296 curwin->w_wrow -= curwin->w_skipcol / width2;
Bram Moolenaar4b6172e2022-10-13 20:23:28 +01001297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (curwin->w_wrow >= curwin->w_height)
1299 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001300 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 extra = curwin->w_wrow - curwin->w_height + 1;
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001302 curwin->w_skipcol += extra * width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 curwin->w_wrow -= extra;
1304 }
1305
Bram Moolenaar856c5d22022-10-13 21:54:28 +01001306 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 if (extra > 0)
1308 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1309 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001310 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001312 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 curwin->w_skipcol = 0;
1314 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001315 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001317#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001318 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001319#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001320#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1321 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1322 {
1323 curwin->w_wrow += popup_top_extra(curwin);
1324 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001325 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001326 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001327 else
1328 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001329#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001330
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001331 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1332 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001333 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001334 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1337}
1338
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001339#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001340/*
1341 * Compute the screen position of text character at "pos" in window "wp"
1342 * The resulting values are one-based, zero when character is not visible.
1343 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001344 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001345textpos2screenpos(
1346 win_T *wp,
1347 pos_T *pos,
1348 int *rowp, // screen row
1349 int *scolp, // start screen column
1350 int *ccolp, // cursor screen column
1351 int *ecolp) // end screen column
1352{
1353 colnr_T scol = 0, ccol = 0, ecol = 0;
1354 int row = 0;
1355 int rowoff = 0;
1356 colnr_T coloff = 0;
1357
Bram Moolenaar189663b2021-07-21 18:04:56 +02001358 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001359 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001360 colnr_T off;
1361 colnr_T col;
1362 int width;
1363 linenr_T lnum = pos->lnum;
1364#ifdef FEAT_FOLDING
1365 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001366
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001367 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1368#endif
1369 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1370#ifdef FEAT_FOLDING
1371 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001372 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001373 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001374 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001375 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001376 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001377#endif
1378 {
1379 getvcol(wp, pos, &scol, &ccol, &ecol);
1380
1381 // similar to what is done in validate_cursor_col()
1382 col = scol;
1383 off = win_col_off(wp);
1384 col += off;
1385 width = wp->w_width - off + win_col_off2(wp);
1386
1387 // long line wrapping, adjust row
1388 if (wp->w_p_wrap
1389 && col >= (colnr_T)wp->w_width
1390 && width > 0)
1391 {
1392 // use same formula as what is used in curs_columns()
1393 rowoff = ((col - wp->w_width) / width + 1);
1394 col -= rowoff * width;
1395 }
1396 col -= wp->w_leftcol;
1397 if (col >= wp->w_width)
1398 col = -1;
1399 if (col >= 0 && row + rowoff <= wp->w_height)
1400 {
1401 coloff = col - scol + wp->w_wincol + 1;
1402 row += W_WINROW(wp);
1403 }
1404 else
1405 // character is left, right or below of the window
1406 row = rowoff = scol = ccol = ecol = 0;
1407 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001408 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001409 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001410 *scolp = scol + coloff;
1411 *ccolp = ccol + coloff;
1412 *ecolp = ecol + coloff;
1413}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001414#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001415
Bram Moolenaar12034e22019-08-25 22:25:02 +02001416#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001417/*
1418 * "screenpos({winid}, {lnum}, {col})" function
1419 */
1420 void
1421f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1422{
1423 dict_T *dict;
1424 win_T *wp;
1425 pos_T pos;
1426 int row = 0;
1427 int scol = 0, ccol = 0, ecol = 0;
1428
Bram Moolenaar93a10962022-06-16 11:42:09 +01001429 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001430 return;
1431 dict = rettv->vval.v_dict;
1432
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001433 if (in_vim9script()
1434 && (check_for_number_arg(argvars, 0) == FAIL
1435 || check_for_number_arg(argvars, 1) == FAIL
1436 || check_for_number_arg(argvars, 2) == FAIL))
1437 return;
1438
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001439 wp = find_win_by_nr_or_id(&argvars[0]);
1440 if (wp == NULL)
1441 return;
1442
1443 pos.lnum = tv_get_number(&argvars[1]);
1444 pos.col = tv_get_number(&argvars[2]) - 1;
1445 pos.coladd = 0;
1446 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1447
1448 dict_add_number(dict, "row", row);
1449 dict_add_number(dict, "col", scol);
1450 dict_add_number(dict, "curscol", ccol);
1451 dict_add_number(dict, "endcol", ecol);
1452}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001453
1454/*
1455 * "virtcol2col({winid}, {lnum}, {col})" function
1456 */
1457 void
1458f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1459{
1460 win_T *wp;
1461 linenr_T lnum;
1462 int screencol;
1463 int error = FALSE;
1464
1465 rettv->vval.v_number = -1;
1466
1467 if (check_for_number_arg(argvars, 0) == FAIL
1468 || check_for_number_arg(argvars, 1) == FAIL
1469 || check_for_number_arg(argvars, 2) == FAIL)
1470 return;
1471
1472 wp = find_win_by_nr_or_id(&argvars[0]);
1473 if (wp == NULL)
1474 return;
1475
1476 lnum = tv_get_number_chk(&argvars[1], &error);
1477 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1478 return;
1479
1480 screencol = tv_get_number_chk(&argvars[2], &error);
1481 if (error || screencol < 0)
1482 return;
1483
1484 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1485}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488/*
1489 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001492scrolldown(
1493 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001494 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001496 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 int wrow;
1498 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001499 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001500 int width1 = 0;
1501 int width2 = 0;
1502
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001503 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001504 {
1505 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001506 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
1509#ifdef FEAT_FOLDING
1510 linenr_T first;
1511
Bram Moolenaar85a20022019-12-21 18:25:54 +01001512 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1514#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001515 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001516 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001519 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1520 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
1522 ++curwin->w_topfill;
1523 ++done;
1524 }
1525 else
1526#endif
1527 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001528 // break when at the very top
1529 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001530 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001532 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001534 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001535 if (curwin->w_skipcol >= width1 + width2)
1536 curwin->w_skipcol -= width2;
1537 else
1538 curwin->w_skipcol -= width1;
1539 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 }
1542 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001543 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001544 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001545 --curwin->w_topline;
1546 curwin->w_skipcol = 0;
1547#ifdef FEAT_DIFF
1548 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001550#ifdef FEAT_FOLDING
1551 // A sequence of folded lines only counts for one logical line
1552 if (hasFolding(curwin->w_topline, &first, NULL))
1553 {
1554 ++done;
1555 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001556 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001557 curwin->w_botline -= curwin->w_topline - first;
1558 curwin->w_topline = first;
1559 }
1560 else
1561#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001562 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001563 {
1564 int size = win_linetabsize(curwin, curwin->w_topline,
1565 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1566 if (size > width1)
1567 {
1568 curwin->w_skipcol = width1;
1569 size -= width1;
1570 redraw_later(UPD_NOT_VALID);
1571 }
1572 while (size > width2)
1573 {
1574 curwin->w_skipcol += width2;
1575 size -= width2;
1576 }
1577 ++done;
1578 }
1579 else
1580 done += PLINES_NOFILL(curwin->w_topline);
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001583 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 invalidate_botline();
1585 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001586 curwin->w_wrow += done; // keep w_wrow updated
1587 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589#ifdef FEAT_DIFF
1590 if (curwin->w_cursor.lnum == curwin->w_topline)
1591 curwin->w_cline_row = 0;
1592 check_topfill(curwin, TRUE);
1593#endif
1594
1595 /*
1596 * Compute the row number of the last row of the cursor line
1597 * and move the cursor onto the displayed part of the window.
1598 */
1599 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001600 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 validate_virtcol();
1603 validate_cheight();
1604 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001605 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1608 {
1609#ifdef FEAT_FOLDING
1610 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1611 {
1612 --wrow;
1613 if (first == 1)
1614 curwin->w_cursor.lnum = 1;
1615 else
1616 curwin->w_cursor.lnum = first - 1;
1617 }
1618 else
1619#endif
1620 wrow -= plines(curwin->w_cursor.lnum--);
1621 curwin->w_valid &=
1622 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1623 moved = TRUE;
1624 }
1625 if (moved)
1626 {
1627#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001628 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 foldAdjustCursor();
1630#endif
1631 coladvance(curwin->w_curswant);
1632 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001633
1634 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1635 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001636 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1637 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1638
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001639 // make sure the cursor is in the visible text
1640 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001641 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001642 int row = 0;
1643 if (col >= width1)
1644 {
1645 col -= width1;
1646 ++row;
1647 }
1648 if (col > width2)
1649 {
1650 row += col / width2;
1651 col = col % width2;
1652 }
1653 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001654 {
1655 curwin->w_curswant = curwin->w_virtcol
1656 - (row - curwin->w_height + 1) * width2;
1657 coladvance(curwin->w_curswant);
1658 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660}
1661
1662/*
1663 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001666scrollup(
1667 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001668 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001670 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001672 if (do_sms
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673# ifdef FEAT_FOLDING
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001674 || (byfold && hasAnyFolding(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675# endif
1676# ifdef FEAT_DIFF
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001677 || curwin->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678# endif
1679 )
1680 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001681 int width1 = curwin->w_width - curwin_col_off();
1682 int width2 = width1 + curwin_col_off2();
1683 int size = 0;
1684 linenr_T prev_topline = curwin->w_topline;
1685
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001686 if (do_sms)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001687 size = win_linetabsize(curwin, curwin->w_topline,
1688 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1689
1690 // diff mode: first consume "topfill"
1691 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1692 // the line, then advance to the next line.
1693 // folding: count each sequence of folded lines as one logical line.
1694 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
1696# ifdef FEAT_DIFF
1697 if (curwin->w_topfill > 0)
1698 --curwin->w_topfill;
1699 else
1700# endif
1701 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001702 linenr_T lnum = curwin->w_topline;
1703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704# ifdef FEAT_FOLDING
1705 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001706 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 (void)hasFolding(lnum, NULL, &lnum);
1708# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001709 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001710 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001711 // 'smoothscroll': increase "w_skipcol" until it goes over
1712 // the end of the line, then advance to the next line.
1713 int add = curwin->w_skipcol > 0 ? width2 : width1;
1714 curwin->w_skipcol += add;
1715 if (curwin->w_skipcol >= size)
1716 {
1717 if (lnum == curbuf->b_ml.ml_line_count)
1718 {
1719 // at the last screen line, can't scroll further
1720 curwin->w_skipcol -= add;
1721 break;
1722 }
1723 ++lnum;
1724 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001725 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001726 else
1727 {
1728 if (lnum >= curbuf->b_ml.ml_line_count)
1729 break;
1730 ++lnum;
1731 }
1732
1733 if (lnum > curwin->w_topline)
1734 {
1735 // approximate w_botline
1736 curwin->w_botline += lnum - curwin->w_topline;
1737 curwin->w_topline = lnum;
1738# ifdef FEAT_DIFF
1739 curwin->w_topfill = diff_check_fill(curwin, lnum);
1740# endif
1741 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001742 if (todo > 1 && do_sms)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001743 size = win_linetabsize(curwin, curwin->w_topline,
1744 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1745 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001746 }
1747 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001748
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001749 if (curwin->w_topline == prev_topline)
1750 // need to redraw even though w_topline didn't change
1751 redraw_later(UPD_NOT_VALID);
1752 }
1753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
1755 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001756 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758
1759 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1760 curwin->w_topline = curbuf->b_ml.ml_line_count;
1761 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1762 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1763
1764#ifdef FEAT_DIFF
1765 check_topfill(curwin, FALSE);
1766#endif
1767
1768#ifdef FEAT_FOLDING
1769 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001770 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1772#endif
1773
1774 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1775 if (curwin->w_cursor.lnum < curwin->w_topline)
1776 {
1777 curwin->w_cursor.lnum = curwin->w_topline;
1778 curwin->w_valid &=
1779 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1780 coladvance(curwin->w_curswant);
1781 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001782 if (curwin->w_cursor.lnum == curwin->w_topline
1783 && do_sms && curwin->w_skipcol > 0)
1784 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001785 int width1 = curwin->w_width - curwin_col_off();
1786 int width2 = width1 + curwin_col_off2();
1787 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1788 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1789
1790 // Make sure the cursor is in a visible part of the line, taking
1791 // 'scrolloff' into account, but using screen lines.
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001792 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001793 if (curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001794 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001795 colnr_T col = curwin->w_virtcol;
1796
1797 if (col < width1)
1798 col += width1;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001799 while (col < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001800 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001801 curwin->w_curswant = col;
1802 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001803
1804 // validate_virtcol() marked various things as valid, but after
1805 // moving the cursor they need to be recomputed
1806 curwin->w_valid &=
1807 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001808 }
1809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810}
1811
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001812/*
1813 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1814 * valid for 'smoothscroll'.
1815 */
1816 void
1817adjust_skipcol(void)
1818{
1819 if (!curwin->w_p_wrap
1820 || !curwin->w_p_sms
1821 || curwin->w_cursor.lnum != curwin->w_topline)
1822 return;
1823
1824 int width1 = curwin->w_width - curwin_col_off();
1825 int width2 = width1 + curwin_col_off2();
1826 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1827 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1828 int scrolled = FALSE;
1829
1830 validate_virtcol();
1831 while (curwin->w_skipcol > 0
1832 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1833 {
1834 // scroll a screen line down
1835 if (curwin->w_skipcol >= width1 + width2)
1836 curwin->w_skipcol -= width2;
1837 else
1838 curwin->w_skipcol -= width1;
1839 redraw_later(UPD_NOT_VALID);
1840 scrolled = TRUE;
1841 validate_virtcol();
1842 }
1843 if (scrolled)
1844 return; // don't scroll in the other direction now
1845
1846 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1847 int row = 0;
1848 if (col >= width1)
1849 {
1850 col -= width1;
1851 ++row;
1852 }
1853 if (col > width2)
1854 {
1855 row += col / width2;
1856 col = col % width2;
1857 }
1858 if (row >= curwin->w_height)
1859 {
1860 if (curwin->w_skipcol == 0)
1861 {
1862 curwin->w_skipcol += width1;
1863 --row;
1864 }
1865 if (row >= curwin->w_height)
1866 curwin->w_skipcol += (row - curwin->w_height) * width2;
1867 redraw_later(UPD_NOT_VALID);
1868 }
1869}
1870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#ifdef FEAT_DIFF
1872/*
1873 * Don't end up with too many filler lines in the window.
1874 */
1875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001876check_topfill(
1877 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001878 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
1880 int n;
1881
1882 if (wp->w_topfill > 0)
1883 {
1884 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1885 if (wp->w_topfill + n > wp->w_height)
1886 {
1887 if (down && wp->w_topline > 1)
1888 {
1889 --wp->w_topline;
1890 wp->w_topfill = 0;
1891 }
1892 else
1893 {
1894 wp->w_topfill = wp->w_height - n;
1895 if (wp->w_topfill < 0)
1896 wp->w_topfill = 0;
1897 }
1898 }
1899 }
1900}
1901
1902/*
1903 * Use as many filler lines as possible for w_topline. Make sure w_topline
1904 * is still visible.
1905 */
1906 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001907max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908{
1909 int n;
1910
1911 n = plines_nofill(curwin->w_topline);
1912 if (n >= curwin->w_height)
1913 curwin->w_topfill = 0;
1914 else
1915 {
1916 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1917 if (curwin->w_topfill + n > curwin->w_height)
1918 curwin->w_topfill = curwin->w_height - n;
1919 }
1920}
1921#endif
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923/*
1924 * Scroll the screen one line down, but don't do it if it would move the
1925 * cursor off the screen.
1926 */
1927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001928scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930 int end_row;
1931#ifdef FEAT_DIFF
1932 int can_fill = (curwin->w_topfill
1933 < diff_check_fill(curwin, curwin->w_topline));
1934#endif
1935
1936 if (curwin->w_topline <= 1
1937#ifdef FEAT_DIFF
1938 && !can_fill
1939#endif
1940 )
1941 return;
1942
Bram Moolenaar85a20022019-12-21 18:25:54 +01001943 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 /*
1946 * Compute the row number of the last row of the cursor line
1947 * and make sure it doesn't go off the screen. Make sure the cursor
1948 * doesn't go past 'scrolloff' lines from the screen end.
1949 */
1950 end_row = curwin->w_wrow;
1951#ifdef FEAT_DIFF
1952 if (can_fill)
1953 ++end_row;
1954 else
1955 end_row += plines_nofill(curwin->w_topline - 1);
1956#else
1957 end_row += plines(curwin->w_topline - 1);
1958#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001959 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961 validate_cheight();
1962 validate_virtcol();
1963 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001964 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001966 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968#ifdef FEAT_DIFF
1969 if (can_fill)
1970 {
1971 ++curwin->w_topfill;
1972 check_topfill(curwin, TRUE);
1973 }
1974 else
1975 {
1976 --curwin->w_topline;
1977 curwin->w_topfill = 0;
1978 }
1979#else
1980 --curwin->w_topline;
1981#endif
1982#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001983 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001985 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1987 }
1988}
1989
1990/*
1991 * Scroll the screen one line up, but don't do it if it would move the cursor
1992 * off the screen.
1993 */
1994 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001995scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 int start_row;
1998
1999 if (curwin->w_topline == curbuf->b_ml.ml_line_count
2000#ifdef FEAT_DIFF
2001 && curwin->w_topfill == 0
2002#endif
2003 )
2004 return;
2005
Bram Moolenaar85a20022019-12-21 18:25:54 +01002006 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
2008 /*
2009 * Compute the row number of the first row of the cursor line
2010 * and make sure it doesn't go off the screen. Make sure the cursor
2011 * doesn't go before 'scrolloff' lines from the screen start.
2012 */
2013#ifdef FEAT_DIFF
2014 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2015 - curwin->w_topfill;
2016#else
2017 start_row = curwin->w_wrow - plines(curwin->w_topline);
2018#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002019 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
2021 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002022 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002024 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026#ifdef FEAT_DIFF
2027 if (curwin->w_topfill > 0)
2028 --curwin->w_topfill;
2029 else
2030#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002031 {
2032#ifdef FEAT_FOLDING
2033 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002036 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002037 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2039 }
2040}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042/*
2043 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2044 * a (wrapped) text line. Uses and sets "lp->fill".
2045 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002046 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 */
2048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002049topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051#ifdef FEAT_DIFF
2052 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2053 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002054 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 ++lp->fill;
2056 lp->height = 1;
2057 }
2058 else
2059#endif
2060 {
2061 --lp->lnum;
2062#ifdef FEAT_DIFF
2063 lp->fill = 0;
2064#endif
2065 if (lp->lnum < 1)
2066 lp->height = MAXCOL;
2067 else
2068#ifdef FEAT_FOLDING
2069 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002070 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 lp->height = 1;
2072 else
2073#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002074 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076}
2077
2078/*
2079 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2080 * a (wrapped) text line. Uses and sets "lp->fill".
2081 * Returns the height of the added line in "lp->height".
2082 * Lines below the last one are incredibly high.
2083 */
2084 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002085botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
2087#ifdef FEAT_DIFF
2088 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2089 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002090 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 ++lp->fill;
2092 lp->height = 1;
2093 }
2094 else
2095#endif
2096 {
2097 ++lp->lnum;
2098#ifdef FEAT_DIFF
2099 lp->fill = 0;
2100#endif
2101 if (lp->lnum > curbuf->b_ml.ml_line_count)
2102 lp->height = MAXCOL;
2103 else
2104#ifdef FEAT_FOLDING
2105 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002106 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 lp->height = 1;
2108 else
2109#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002110 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 }
2112}
2113
2114#ifdef FEAT_DIFF
2115/*
2116 * Switch from including filler lines below lp->lnum to including filler
2117 * lines above loff.lnum + 1. This keeps pointing to the same line.
2118 * When there are no filler lines nothing changes.
2119 */
2120 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002121botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123 if (lp->fill > 0)
2124 {
2125 ++lp->lnum;
2126 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2127 }
2128}
2129
2130/*
2131 * Switch from including filler lines above lp->lnum to including filler
2132 * lines below loff.lnum - 1. This keeps pointing to the same line.
2133 * When there are no filler lines nothing changes.
2134 */
2135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002136topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 if (lp->fill > 0)
2139 {
2140 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2141 --lp->lnum;
2142 }
2143}
2144#endif
2145
2146/*
2147 * Recompute topline to put the cursor at the top of the window.
2148 * Scroll at least "min_scroll" lines.
2149 * If "always" is TRUE, always set topline (for "zt").
2150 */
2151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 int scrolled = 0;
2155 int extra = 0;
2156 int used;
2157 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002158 linenr_T top; // just above displayed lines
2159 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002161 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#ifdef FEAT_DIFF
2163 linenr_T old_topfill = curwin->w_topfill;
2164#endif
2165 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002166 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (mouse_dragging > 0)
2169 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171 /*
2172 * Decrease topline until:
2173 * - it has become 1
2174 * - (part of) the cursor line is moved off the screen or
2175 * - moved at least 'scrolljump' lines and
2176 * - at least 'scrolloff' lines above and below the cursor
2177 */
2178 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002179 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (curwin->w_cursor.lnum < curwin->w_topline)
2181 scrolled = used;
2182
2183#ifdef FEAT_FOLDING
2184 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2185 {
2186 --top;
2187 ++bot;
2188 }
2189 else
2190#endif
2191 {
2192 top = curwin->w_cursor.lnum - 1;
2193 bot = curwin->w_cursor.lnum + 1;
2194 }
2195 new_topline = top + 1;
2196
2197#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002198 // "used" already contains the number of filler lines above, don't add it
2199 // again.
2200 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002201 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#endif
2203
2204 /*
2205 * Check if the lines from "top" to "bot" fit in the window. If they do,
2206 * set new_topline and advance "top" and "bot" to include more lines.
2207 */
2208 while (top > 0)
2209 {
2210#ifdef FEAT_FOLDING
2211 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002212 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 i = 1;
2214 else
2215#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002216 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 used += i;
2218 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2219 {
2220#ifdef FEAT_FOLDING
2221 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002222 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 ++used;
2224 else
2225#endif
2226 used += plines(bot);
2227 }
2228 if (used > curwin->w_height)
2229 break;
2230 if (top < curwin->w_topline)
2231 scrolled += i;
2232
2233 /*
2234 * If scrolling is needed, scroll at least 'sj' lines.
2235 */
2236 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2237 && extra >= off)
2238 break;
2239
2240 extra += i;
2241 new_topline = top;
2242 --top;
2243 ++bot;
2244 }
2245
2246 /*
2247 * If we don't have enough space, put cursor in the middle.
2248 * This makes sure we get the same position when using "k" and "j"
2249 * in a small window.
2250 */
2251 if (used > curwin->w_height)
2252 scroll_cursor_halfway(FALSE);
2253 else
2254 {
2255 /*
2256 * If "always" is FALSE, only adjust topline to a lower value, higher
2257 * value may happen with wrapping lines
2258 */
2259 if (new_topline < curwin->w_topline || always)
2260 curwin->w_topline = new_topline;
2261 if (curwin->w_topline > curwin->w_cursor.lnum)
2262 curwin->w_topline = curwin->w_cursor.lnum;
2263#ifdef FEAT_DIFF
2264 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2265 if (curwin->w_topfill > 0 && extra > off)
2266 {
2267 curwin->w_topfill -= extra - off;
2268 if (curwin->w_topfill < 0)
2269 curwin->w_topfill = 0;
2270 }
2271 check_topfill(curwin, FALSE);
2272#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002273 // TODO: if the line doesn't fit may optimize w_skipcol
2274 if (curwin->w_topline == curwin->w_cursor.lnum)
2275 {
2276 curwin->w_skipcol = 0;
2277 redraw_later(UPD_NOT_VALID);
2278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002280 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#ifdef FEAT_DIFF
2282 || curwin->w_topfill != old_topfill
2283#endif
2284 )
2285 curwin->w_valid &=
2286 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2287 curwin->w_valid |= VALID_TOPLINE;
2288 }
2289}
2290
2291/*
2292 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2293 * screen lines for text lines.
2294 */
2295 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002296set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298#ifdef FEAT_DIFF
2299 wp->w_filler_rows = 0;
2300#endif
2301 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002302 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 else
2304 {
2305 wp->w_empty_rows = wp->w_height - used;
2306#ifdef FEAT_DIFF
2307 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2308 {
2309 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2310 if (wp->w_empty_rows > wp->w_filler_rows)
2311 wp->w_empty_rows -= wp->w_filler_rows;
2312 else
2313 {
2314 wp->w_filler_rows = wp->w_empty_rows;
2315 wp->w_empty_rows = 0;
2316 }
2317 }
2318#endif
2319 }
2320}
2321
2322/*
2323 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002324 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2326 * This is messy stuff!!!
2327 */
2328 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002329scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 int used;
2332 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002333 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 int extra = 0;
2335 int i;
2336 linenr_T line_count;
2337 linenr_T old_topline = curwin->w_topline;
2338 lineoff_T loff;
2339 lineoff_T boff;
2340#ifdef FEAT_DIFF
2341 int old_topfill = curwin->w_topfill;
2342 int fill_below_window;
2343#endif
2344 linenr_T old_botline = curwin->w_botline;
2345 linenr_T old_valid = curwin->w_valid;
2346 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002347 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002348 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 cln = curwin->w_cursor.lnum;
2351 if (set_topbot)
2352 {
2353 used = 0;
2354 curwin->w_botline = cln + 1;
2355#ifdef FEAT_DIFF
2356 loff.fill = 0;
2357#endif
2358 for (curwin->w_topline = curwin->w_botline;
2359 curwin->w_topline > 1;
2360 curwin->w_topline = loff.lnum)
2361 {
2362 loff.lnum = curwin->w_topline;
2363 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002364 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 break;
2366 used += loff.height;
2367#ifdef FEAT_DIFF
2368 curwin->w_topfill = loff.fill;
2369#endif
2370 }
2371 set_empty_rows(curwin, used);
2372 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2373 if (curwin->w_topline != old_topline
2374#ifdef FEAT_DIFF
2375 || curwin->w_topfill != old_topfill
2376#endif
2377 )
2378 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2379 }
2380 else
2381 validate_botline();
2382
Bram Moolenaar85a20022019-12-21 18:25:54 +01002383 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#ifdef FEAT_DIFF
2385 used = plines_nofill(cln);
2386#else
2387 validate_cheight();
2388 used = curwin->w_cline_height;
2389#endif
2390
Bram Moolenaar85a20022019-12-21 18:25:54 +01002391 // If the cursor is below botline, we will at least scroll by the height
2392 // of the cursor line. Correct for empty lines, which are really part of
2393 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (cln >= curwin->w_botline)
2395 {
2396 scrolled = used;
2397 if (cln == curwin->w_botline)
2398 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002399 min_scrolled = scrolled;
2400 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2401 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
Bram Moolenaar4d31b482022-10-06 16:03:09 +01002402 min_scrolled += PLINES_NOFILL(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404
2405 /*
2406 * Stop counting lines to scroll when
2407 * - hitting start of the file
2408 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002409 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 * - lines between botline and cursor have been counted
2411 */
2412#ifdef FEAT_FOLDING
2413 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2414#endif
2415 {
2416 loff.lnum = cln;
2417 boff.lnum = cln;
2418 }
2419#ifdef FEAT_DIFF
2420 loff.fill = 0;
2421 boff.fill = 0;
2422 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2423 - curwin->w_filler_rows;
2424#endif
2425
2426 while (loff.lnum > 1)
2427 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002428 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2429 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002431 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2433 && loff.lnum <= curwin->w_botline
2434#ifdef FEAT_DIFF
2435 && (loff.lnum < curwin->w_botline
2436 || loff.fill >= fill_below_window)
2437#endif
2438 )
2439 break;
2440
Bram Moolenaar85a20022019-12-21 18:25:54 +01002441 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002443 if (loff.height == MAXCOL)
2444 used = MAXCOL;
2445 else
2446 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 if (used > curwin->w_height)
2448 break;
2449 if (loff.lnum >= curwin->w_botline
2450#ifdef FEAT_DIFF
2451 && (loff.lnum > curwin->w_botline
2452 || loff.fill <= fill_below_window)
2453#endif
2454 )
2455 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002456 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 scrolled += loff.height;
2458 if (loff.lnum == curwin->w_botline
2459#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002460 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461#endif
2462 )
2463 scrolled -= curwin->w_empty_rows;
2464 }
2465
2466 if (boff.lnum < curbuf->b_ml.ml_line_count)
2467 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002468 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 botline_forw(&boff);
2470 used += boff.height;
2471 if (used > curwin->w_height)
2472 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002473 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2474 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
2476 extra += boff.height;
2477 if (boff.lnum >= curwin->w_botline
2478#ifdef FEAT_DIFF
2479 || (boff.lnum + 1 == curwin->w_botline
2480 && boff.fill > curwin->w_filler_rows)
2481#endif
2482 )
2483 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002484 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 scrolled += boff.height;
2486 if (boff.lnum == curwin->w_botline
2487#ifdef FEAT_DIFF
2488 && boff.fill == 0
2489#endif
2490 )
2491 scrolled -= curwin->w_empty_rows;
2492 }
2493 }
2494 }
2495 }
2496
Bram Moolenaar85a20022019-12-21 18:25:54 +01002497 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (scrolled <= 0)
2499 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002500 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 else if (used > curwin->w_height)
2502 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002503 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 else
2505 {
2506 line_count = 0;
2507#ifdef FEAT_DIFF
2508 boff.fill = curwin->w_topfill;
2509#endif
2510 boff.lnum = curwin->w_topline - 1;
2511 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2512 {
2513 botline_forw(&boff);
2514 i += boff.height;
2515 ++line_count;
2516 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002517 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 line_count = 9999;
2519 }
2520
2521 /*
2522 * Scroll up if the cursor is off the bottom of the screen a bit.
2523 * Otherwise put it at 1/2 of the screen.
2524 */
2525 if (line_count >= curwin->w_height && line_count > min_scroll)
2526 scroll_cursor_halfway(FALSE);
2527 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002528 {
2529 // With 'smoothscroll' scroll at least the height of the cursor line.
2530 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled)
2531 line_count = min_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 /*
2536 * If topline didn't change we need to restore w_botline and w_empty_rows
2537 * (we changed them).
2538 * If topline did change, update_screen() will set botline.
2539 */
2540 if (curwin->w_topline == old_topline && set_topbot)
2541 {
2542 curwin->w_botline = old_botline;
2543 curwin->w_empty_rows = old_empty_rows;
2544 curwin->w_valid = old_valid;
2545 }
2546 curwin->w_valid |= VALID_TOPLINE;
2547}
2548
2549/*
2550 * Recompute topline to put the cursor halfway the window
2551 * If "atend" is TRUE, also put it halfway at the end of the file.
2552 */
2553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002554scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 int above = 0;
2557 linenr_T topline;
2558#ifdef FEAT_DIFF
2559 int topfill = 0;
2560#endif
2561 int below = 0;
2562 int used;
2563 lineoff_T loff;
2564 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002565#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002566 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002569#ifdef FEAT_PROP_POPUP
2570 // if the width changed this needs to be updated first
2571 may_update_popup_position();
2572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2574#ifdef FEAT_FOLDING
2575 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2576#endif
2577#ifdef FEAT_DIFF
2578 used = plines_nofill(loff.lnum);
2579 loff.fill = 0;
2580 boff.fill = 0;
2581#else
2582 used = plines(loff.lnum);
2583#endif
2584 topline = loff.lnum;
2585 while (topline > 1)
2586 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002587 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
2589 if (boff.lnum < curbuf->b_ml.ml_line_count)
2590 {
2591 botline_forw(&boff);
2592 used += boff.height;
2593 if (used > curwin->w_height)
2594 break;
2595 below += boff.height;
2596 }
2597 else
2598 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002599 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (atend)
2601 ++used;
2602 }
2603 }
2604
Bram Moolenaar85a20022019-12-21 18:25:54 +01002605 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
2607 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002608 if (loff.height == MAXCOL)
2609 used = MAXCOL;
2610 else
2611 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 if (used > curwin->w_height)
2613 break;
2614 above += loff.height;
2615 topline = loff.lnum;
2616#ifdef FEAT_DIFF
2617 topfill = loff.fill;
2618#endif
2619 }
2620 }
2621#ifdef FEAT_FOLDING
2622 if (!hasFolding(topline, &curwin->w_topline, NULL))
2623#endif
2624 curwin->w_topline = topline;
2625#ifdef FEAT_DIFF
2626 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002627 if (old_topline > curwin->w_topline + curwin->w_height)
2628 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 check_topfill(curwin, FALSE);
2630#endif
2631 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2632 curwin->w_valid |= VALID_TOPLINE;
2633}
2634
2635/*
2636 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002637 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 * If not possible, put it at the same position as scroll_cursor_halfway().
2639 * When called topline must be valid!
2640 */
2641 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002642cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002644 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002646 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 linenr_T botline;
2648 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002649 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002651 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 /*
2654 * How many lines we would like to have above/below the cursor depends on
2655 * whether the first/last line of the file is on screen.
2656 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002657 above_wanted = so;
2658 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002659 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 above_wanted = mouse_dragging - 1;
2662 below_wanted = mouse_dragging - 1;
2663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (curwin->w_topline == 1)
2665 {
2666 above_wanted = 0;
2667 max_off = curwin->w_height / 2;
2668 if (below_wanted > max_off)
2669 below_wanted = max_off;
2670 }
2671 validate_botline();
2672 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002673 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
2675 below_wanted = 0;
2676 max_off = (curwin->w_height - 1) / 2;
2677 if (above_wanted > max_off)
2678 above_wanted = max_off;
2679 }
2680
2681 /*
2682 * If there are sufficient file-lines above and below the cursor, we can
2683 * return now.
2684 */
2685 cln = curwin->w_cursor.lnum;
2686 if (cln >= curwin->w_topline + above_wanted
2687 && cln < curwin->w_botline - below_wanted
2688#ifdef FEAT_FOLDING
2689 && !hasAnyFolding(curwin)
2690#endif
2691 )
2692 return;
2693
2694 /*
2695 * Narrow down the area where the cursor can be put by taking lines from
2696 * the top and the bottom until:
2697 * - the desired context lines are found
2698 * - the lines from the top is past the lines from the bottom
2699 */
2700 topline = curwin->w_topline;
2701 botline = curwin->w_botline - 1;
2702#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002703 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 above = curwin->w_topfill;
2705 below = curwin->w_filler_rows;
2706#endif
2707 while ((above < above_wanted || below < below_wanted) && topline < botline)
2708 {
2709 if (below < below_wanted && (below <= above || above >= above_wanted))
2710 {
2711#ifdef FEAT_FOLDING
2712 if (hasFolding(botline, &botline, NULL))
2713 ++below;
2714 else
2715#endif
2716 below += plines(botline);
2717 --botline;
2718 }
2719 if (above < above_wanted && (above < below || below >= below_wanted))
2720 {
2721#ifdef FEAT_FOLDING
2722 if (hasFolding(topline, NULL, &topline))
2723 ++above;
2724 else
2725#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002726 above += PLINES_NOFILL(topline);
2727#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002728 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (topline < botline)
2730 above += diff_check_fill(curwin, topline + 1);
2731#endif
2732 ++topline;
2733 }
2734 }
2735 if (topline == botline || botline == 0)
2736 curwin->w_cursor.lnum = topline;
2737 else if (topline > botline)
2738 curwin->w_cursor.lnum = botline;
2739 else
2740 {
2741 if (cln < topline && curwin->w_topline > 1)
2742 {
2743 curwin->w_cursor.lnum = topline;
2744 curwin->w_valid &=
2745 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2746 }
2747 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2748 {
2749 curwin->w_cursor.lnum = botline;
2750 curwin->w_valid &=
2751 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2752 }
2753 }
2754 curwin->w_valid |= VALID_TOPLINE;
2755}
2756
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002757static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759/*
2760 * move screen 'count' pages up or down and update screen
2761 *
2762 * return FAIL for failure, OK otherwise
2763 */
2764 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002765onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 long n;
2768 int retval = OK;
2769 lineoff_T loff;
2770 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002771 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
Bram Moolenaar85a20022019-12-21 18:25:54 +01002773 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
2775 beep_flush();
2776 return FAIL;
2777 }
2778
2779 for ( ; count > 0; --count)
2780 {
2781 validate_botline();
2782 /*
2783 * It's an error to move a page up when the first line is already on
2784 * the screen. It's an error to move a page down when the last line
2785 * is on the screen and the topline is 'scrolloff' lines from the
2786 * last line.
2787 */
2788 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002789 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2791 : (curwin->w_topline == 1
2792#ifdef FEAT_DIFF
2793 && curwin->w_topfill ==
2794 diff_check_fill(curwin, curwin->w_topline)
2795#endif
2796 ))
2797 {
2798 beep_flush();
2799 retval = FAIL;
2800 break;
2801 }
2802
2803#ifdef FEAT_DIFF
2804 loff.fill = 0;
2805#endif
2806 if (dir == FORWARD)
2807 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002808 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002810 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002811 if (p_window <= 2)
2812 ++curwin->w_topline;
2813 else
2814 curwin->w_topline += p_window - 2;
2815 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2816 curwin->w_topline = curbuf->b_ml.ml_line_count;
2817 curwin->w_cursor.lnum = curwin->w_topline;
2818 }
2819 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2820 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002821 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 curwin->w_topline = curbuf->b_ml.ml_line_count;
2823#ifdef FEAT_DIFF
2824 curwin->w_topfill = 0;
2825#endif
2826 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2827 }
2828 else
2829 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002830 // For the overlap, start with the line just below the window
2831 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 loff.lnum = curwin->w_botline;
2833#ifdef FEAT_DIFF
2834 loff.fill = diff_check_fill(curwin, loff.lnum)
2835 - curwin->w_filler_rows;
2836#endif
2837 get_scroll_overlap(&loff, -1);
2838 curwin->w_topline = loff.lnum;
2839#ifdef FEAT_DIFF
2840 curwin->w_topfill = loff.fill;
2841 check_topfill(curwin, FALSE);
2842#endif
2843 curwin->w_cursor.lnum = curwin->w_topline;
2844 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2845 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2846 }
2847 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002848 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
2850#ifdef FEAT_DIFF
2851 if (curwin->w_topline == 1)
2852 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002853 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 max_topfill();
2855 continue;
2856 }
2857#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002858 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002859 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002860 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002861 if (p_window <= 2)
2862 --curwin->w_topline;
2863 else
2864 curwin->w_topline -= p_window - 2;
2865 if (curwin->w_topline < 1)
2866 curwin->w_topline = 1;
2867 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2868 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2869 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2870 continue;
2871 }
2872
Bram Moolenaar85a20022019-12-21 18:25:54 +01002873 // Find the line at the top of the window that is going to be the
2874 // line at the bottom of the window. Make sure this results in
2875 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 loff.lnum = curwin->w_topline - 1;
2877#ifdef FEAT_DIFF
2878 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2879 - curwin->w_topfill;
2880#endif
2881 get_scroll_overlap(&loff, 1);
2882
2883 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2884 {
2885 loff.lnum = curbuf->b_ml.ml_line_count;
2886#ifdef FEAT_DIFF
2887 loff.fill = 0;
2888 }
2889 else
2890 {
2891 botline_topline(&loff);
2892#endif
2893 }
2894 curwin->w_cursor.lnum = loff.lnum;
2895
Bram Moolenaar85a20022019-12-21 18:25:54 +01002896 // Find the line just above the new topline to get the right line
2897 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 n = 0;
2899 while (n <= curwin->w_height && loff.lnum >= 1)
2900 {
2901 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002902 if (loff.height == MAXCOL)
2903 n = MAXCOL;
2904 else
2905 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002907 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
2909 curwin->w_topline = 1;
2910#ifdef FEAT_DIFF
2911 max_topfill();
2912#endif
2913 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2914 }
2915 else
2916 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002917 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#ifdef FEAT_DIFF
2919 topline_botline(&loff);
2920#endif
2921 botline_forw(&loff);
2922 botline_forw(&loff);
2923#ifdef FEAT_DIFF
2924 botline_topline(&loff);
2925#endif
2926#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002927 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2929#endif
2930
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 // Always scroll at least one line. Avoid getting stuck on
2932 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 if (loff.lnum >= curwin->w_topline
2934#ifdef FEAT_DIFF
2935 && (loff.lnum > curwin->w_topline
2936 || loff.fill >= curwin->w_topfill)
2937#endif
2938 )
2939 {
2940#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002941 // First try using the maximum number of filler lines. If
2942 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 loff.fill = curwin->w_topfill;
2944 if (curwin->w_topfill < diff_check_fill(curwin,
2945 curwin->w_topline))
2946 max_topfill();
2947 if (curwin->w_topfill == loff.fill)
2948#endif
2949 {
2950 --curwin->w_topline;
2951#ifdef FEAT_DIFF
2952 curwin->w_topfill = 0;
2953#endif
2954 }
2955 comp_botline(curwin);
2956 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002957 curwin->w_valid &=
2958 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 }
2960 else
2961 {
2962 curwin->w_topline = loff.lnum;
2963#ifdef FEAT_DIFF
2964 curwin->w_topfill = loff.fill;
2965 check_topfill(curwin, FALSE);
2966#endif
2967 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2968 }
2969 }
2970 }
2971 }
2972#ifdef FEAT_FOLDING
2973 foldAdjustCursor();
2974#endif
2975 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002976 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002977 if (retval == OK)
2978 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2980
Bram Moolenaar907dad72018-07-10 15:07:15 +02002981 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002983 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2984 // But make sure we scroll at least one line (happens with mix of long
2985 // wrapping lines and non-wrapping line).
2986 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002988 scroll_cursor_top(1, FALSE);
2989 if (curwin->w_topline <= old_topline
2990 && old_topline < curbuf->b_ml.ml_line_count)
2991 {
2992 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002994 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2995#endif
2996 }
2997 }
2998#ifdef FEAT_FOLDING
2999 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003004 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 return retval;
3006}
3007
3008/*
3009 * Decide how much overlap to use for page-up or page-down scrolling.
3010 * This is symmetric, so that doing both keeps the same lines displayed.
3011 * Three lines are examined:
3012 *
3013 * before CTRL-F after CTRL-F / before CTRL-B
3014 * etc. l1
3015 * l1 last but one line ------------
3016 * l2 last text line l2 top text line
3017 * ------------- l3 second text line
3018 * l3 etc.
3019 */
3020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003021get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 int h1, h2, h3, h4;
3024 int min_height = curwin->w_height - 2;
3025 lineoff_T loff0, loff1, loff2;
3026
3027#ifdef FEAT_DIFF
3028 if (lp->fill > 0)
3029 lp->height = 1;
3030 else
3031 lp->height = plines_nofill(lp->lnum);
3032#else
3033 lp->height = plines(lp->lnum);
3034#endif
3035 h1 = lp->height;
3036 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003037 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039 loff0 = *lp;
3040 if (dir > 0)
3041 botline_forw(lp);
3042 else
3043 topline_back(lp);
3044 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003045 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003047 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 return;
3049 }
3050
3051 loff1 = *lp;
3052 if (dir > 0)
3053 botline_forw(lp);
3054 else
3055 topline_back(lp);
3056 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003057 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003059 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 return;
3061 }
3062
3063 loff2 = *lp;
3064 if (dir > 0)
3065 botline_forw(lp);
3066 else
3067 topline_back(lp);
3068 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003069 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003070 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003072 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073}
3074
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075/*
3076 * Scroll 'scroll' lines up or down.
3077 */
3078 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003079halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 long scrolled = 0;
3082 int i;
3083 int n;
3084 int room;
3085
3086 if (Prenum)
3087 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3088 curwin->w_height : Prenum;
3089 n = (curwin->w_p_scr <= curwin->w_height) ?
3090 curwin->w_p_scr : curwin->w_height;
3091
Bram Moolenaard5d37532017-03-27 23:02:07 +02003092 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 validate_botline();
3094 room = curwin->w_empty_rows;
3095#ifdef FEAT_DIFF
3096 room += curwin->w_filler_rows;
3097#endif
3098 if (flag)
3099 {
3100 /*
3101 * scroll the text up
3102 */
3103 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3104 {
3105#ifdef FEAT_DIFF
3106 if (curwin->w_topfill > 0)
3107 {
3108 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003109 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 --curwin->w_topfill;
3111 }
3112 else
3113#endif
3114 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003115 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 n -= i;
3117 if (n < 0 && scrolled > 0)
3118 break;
3119#ifdef FEAT_FOLDING
3120 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3121#endif
3122 ++curwin->w_topline;
3123#ifdef FEAT_DIFF
3124 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3125#endif
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3128 {
3129 ++curwin->w_cursor.lnum;
3130 curwin->w_valid &=
3131 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 }
3134 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3135 scrolled += i;
3136
3137 /*
3138 * Correct w_botline for changed w_topline.
3139 * Won't work when there are filler lines.
3140 */
3141#ifdef FEAT_DIFF
3142 if (curwin->w_p_diff)
3143 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3144 else
3145#endif
3146 {
3147 room += i;
3148 do
3149 {
3150 i = plines(curwin->w_botline);
3151 if (i > room)
3152 break;
3153#ifdef FEAT_FOLDING
3154 (void)hasFolding(curwin->w_botline, NULL,
3155 &curwin->w_botline);
3156#endif
3157 ++curwin->w_botline;
3158 room -= i;
3159 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3160 }
3161 }
3162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 /*
3164 * When hit bottom of the file: move cursor down.
3165 */
3166 if (n > 0)
3167 {
3168# ifdef FEAT_FOLDING
3169 if (hasAnyFolding(curwin))
3170 {
3171 while (--n >= 0
3172 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3173 {
3174 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3175 &curwin->w_cursor.lnum);
3176 ++curwin->w_cursor.lnum;
3177 }
3178 }
3179 else
3180# endif
3181 curwin->w_cursor.lnum += n;
3182 check_cursor_lnum();
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
3185 else
3186 {
3187 /*
3188 * scroll the text down
3189 */
3190 while (n > 0 && curwin->w_topline > 1)
3191 {
3192#ifdef FEAT_DIFF
3193 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3194 {
3195 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003196 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 ++curwin->w_topfill;
3198 }
3199 else
3200#endif
3201 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003202 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 n -= i;
3204 if (n < 0 && scrolled > 0)
3205 break;
3206 --curwin->w_topline;
3207#ifdef FEAT_FOLDING
3208 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3209#endif
3210#ifdef FEAT_DIFF
3211 curwin->w_topfill = 0;
3212#endif
3213 }
3214 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3215 VALID_BOTLINE|VALID_BOTLINE_AP);
3216 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (curwin->w_cursor.lnum > 1)
3218 {
3219 --curwin->w_cursor.lnum;
3220 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003223
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 /*
3225 * When hit top of the file: move cursor up.
3226 */
3227 if (n > 0)
3228 {
3229 if (curwin->w_cursor.lnum <= (linenr_T)n)
3230 curwin->w_cursor.lnum = 1;
3231 else
3232# ifdef FEAT_FOLDING
3233 if (hasAnyFolding(curwin))
3234 {
3235 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3236 {
3237 --curwin->w_cursor.lnum;
3238 (void)hasFolding(curwin->w_cursor.lnum,
3239 &curwin->w_cursor.lnum, NULL);
3240 }
3241 }
3242 else
3243# endif
3244 curwin->w_cursor.lnum -= n;
3245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003248 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 foldAdjustCursor();
3250# endif
3251#ifdef FEAT_DIFF
3252 check_topfill(curwin, !flag);
3253#endif
3254 cursor_correct();
3255 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003256 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003258
Bram Moolenaar860cae12010-06-05 23:22:07 +02003259 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003260do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003261{
3262 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003263 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003264 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003265 colnr_T curswant = curwin->w_curswant;
3266 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003267 win_T *old_curwin = curwin;
3268 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003269 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003270 int old_VIsual_select = VIsual_select;
3271 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003272
3273 /*
3274 * loop through the cursorbound windows
3275 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003276 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003277 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003278 {
3279 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003280 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003281 if (curwin != old_curwin && curwin->w_p_crb)
3282 {
3283# ifdef FEAT_DIFF
3284 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003285 curwin->w_cursor.lnum =
3286 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003287 else
3288# endif
3289 curwin->w_cursor.lnum = line;
3290 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003291 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003292 curwin->w_curswant = curswant;
3293 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003294
Bram Moolenaar85a20022019-12-21 18:25:54 +01003295 // Make sure the cursor is in a valid position. Temporarily set
3296 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003297 restart_edit_save = restart_edit;
3298 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003299 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003300
3301 // Avoid a scroll here for the cursor position, 'scrollbind' is
3302 // more important.
3303 if (!curwin->w_p_scb)
3304 validate_cursor();
3305
Bram Moolenaar61452852011-02-01 18:01:11 +01003306 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003307 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003308 if (has_mbyte)
3309 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003310 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003311
Bram Moolenaar85a20022019-12-21 18:25:54 +01003312 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003313 if (!curwin->w_p_scb)
3314 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003315 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003316 }
3317 }
3318
3319 /*
3320 * reset current-window
3321 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003322 VIsual_select = old_VIsual_select;
3323 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003324 curwin = old_curwin;
3325 curbuf = old_curbuf;
3326}