blob: 343c6c8e02bb58cfb2b937a3f923383b51be8fa6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * move.c: Functions for moving the cursor and scrolling text.
11 *
12 * There are two ways to move the cursor:
13 * 1. Move the cursor directly, the text is scrolled to keep the cursor in the
14 * window.
15 * 2. Scroll the text, the cursor is moved into the text visible in the
16 * window.
17 * The 'scrolloff' option makes this a bit complicated.
18 */
19
20#include "vim.h"
21
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int scrolljump_value(void);
23static int check_top_offset(void);
24static void curs_rows(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26typedef struct
27{
Bram Moolenaar85a20022019-12-21 18:25:54 +010028 linenr_T lnum; // line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +010030 int fill; // filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +010032 int height; // height of added line
Bram Moolenaar071d4272004-06-13 20:20:40 +000033} lineoff_T;
34
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void topline_back(lineoff_T *lp);
36static void botline_forw(lineoff_T *lp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38/*
Bram Moolenaarf6196f42022-10-02 21:29:55 +010039 * Reduce "n" for the screen lines skipped with "wp->w_skipcol".
40 */
41 static int
42adjust_plines_for_skipcol(win_T *wp, int n)
43{
44 if (wp->w_skipcol == 0)
45 return n;
46
47 int off = 0;
48 int width = wp->w_width - win_col_off(wp);
49 if (wp->w_skipcol >= width)
50 {
51 ++off;
52 int skip = wp->w_skipcol - width;
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +010053 width += win_col_off2(wp);
Bram Moolenaarf6196f42022-10-02 21:29:55 +010054 while (skip >= width)
55 {
56 ++off;
57 skip -= width;
58 }
59 }
60 wp->w_valid &= ~VALID_WROW;
61 return n - off;
62}
63
64/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 * Compute wp->w_botline for the current wp->w_topline. Can be called after
66 * wp->w_topline changed.
67 */
68 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010069comp_botline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070{
71 int n;
72 linenr_T lnum;
73 int done;
74#ifdef FEAT_FOLDING
75 linenr_T last;
76 int folded;
77#endif
78
79 /*
80 * If w_cline_row is valid, start there.
81 * Otherwise have to start at w_topline.
82 */
83 check_cursor_moved(wp);
84 if (wp->w_valid & VALID_CROW)
85 {
86 lnum = wp->w_cursor.lnum;
87 done = wp->w_cline_row;
88 }
89 else
90 {
91 lnum = wp->w_topline;
92 done = 0;
93 }
94
95 for ( ; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
96 {
97#ifdef FEAT_FOLDING
98 last = lnum;
99 folded = FALSE;
100 if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
101 {
102 n = 1;
103 folded = TRUE;
104 }
105 else
106#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#ifdef FEAT_DIFF
109 if (lnum == wp->w_topline)
110 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
111 else
112#endif
113 n = plines_win(wp, lnum, TRUE);
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100114 if (lnum == wp->w_topline)
115 n = adjust_plines_for_skipcol(wp, n);
116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 if (
118#ifdef FEAT_FOLDING
119 lnum <= wp->w_cursor.lnum && last >= wp->w_cursor.lnum
120#else
121 lnum == wp->w_cursor.lnum
122#endif
123 )
124 {
125 wp->w_cline_row = done;
126 wp->w_cline_height = n;
127#ifdef FEAT_FOLDING
128 wp->w_cline_folded = folded;
129#endif
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100130 redraw_for_cursorline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 wp->w_valid |= (VALID_CROW|VALID_CHEIGHT);
132 }
133 if (done + n > wp->w_height)
134 break;
135 done += n;
136#ifdef FEAT_FOLDING
137 lnum = last;
138#endif
139 }
140
Bram Moolenaar85a20022019-12-21 18:25:54 +0100141 // wp->w_botline is the line that is just below the window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 wp->w_botline = lnum;
143 wp->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
144
145 set_empty_rows(wp, done);
146}
147
148/*
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100149 * Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is
150 * set.
151 */
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +0100152 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100153redraw_for_cursorline(win_T *wp)
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100154{
155 if ((wp->w_p_rnu
156#ifdef FEAT_SYN_HL
157 || wp->w_p_cul
158#endif
159 )
160 && (wp->w_valid & VALID_CROW) == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200161 && !pum_visible())
Bram Moolenaar90a99792018-09-12 21:52:18 +0200162 {
zeertzjqc20e46a2022-03-23 14:55:23 +0000163 // win_line() will redraw the number column and cursorline only.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100164 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar90a99792018-09-12 21:52:18 +0200165 }
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100166}
167
zeertzjq3e559cd2022-03-27 19:26:55 +0100168#ifdef FEAT_SYN_HL
169/*
170 * Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
171 * contains "screenline".
172 */
173 static void
174redraw_for_cursorcolumn(win_T *wp)
175{
176 if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible())
177 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100178 // When 'cursorcolumn' is set need to redraw with UPD_SOME_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100179 if (wp->w_p_cuc)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100180 redraw_win_later(wp, UPD_SOME_VALID);
181 // When 'cursorlineopt' contains "screenline" need to redraw with
182 // UPD_VALID.
zeertzjq3e559cd2022-03-27 19:26:55 +0100183 else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100184 redraw_win_later(wp, UPD_VALID);
zeertzjq3e559cd2022-03-27 19:26:55 +0100185 }
186}
187#endif
188
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 * Update curwin->w_topline and redraw if necessary.
191 * Used to update the screen before printing a message.
192 */
193 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100194update_topline_redraw(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 update_topline();
197 if (must_redraw)
198 update_screen(0);
199}
200
201/*
202 * Update curwin->w_topline to move the cursor onto the screen.
203 */
204 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100205update_topline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206{
207 long line_count;
208 int halfheight;
209 int n;
210 linenr_T old_topline;
211#ifdef FEAT_DIFF
212 int old_topfill;
213#endif
214#ifdef FEAT_FOLDING
215 linenr_T lnum;
216#endif
217 int check_topline = FALSE;
218 int check_botline = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100219 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100220 int save_so = *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
Luuk van Baal13ece2a2022-10-03 15:28:08 +0100222 // Cursor is updated instead when this is TRUE for 'splitkeep'.
223 if (skip_update_topline)
224 return;
225
Bram Moolenaar85a20022019-12-21 18:25:54 +0100226 // If there is no valid screen and when the window height is zero just use
227 // the cursor line.
Bram Moolenaard5d37532017-03-27 23:02:07 +0200228 if (!screen_valid(TRUE) || curwin->w_height == 0)
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200229 {
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100230 check_cursor_lnum();
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200231 curwin->w_topline = curwin->w_cursor.lnum;
232 curwin->w_botline = curwin->w_topline;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200233 curwin->w_scbind_pos = 1;
Bram Moolenaarcfc216e2014-09-23 18:37:56 +0200234 return;
235 }
236
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 check_cursor_moved(curwin);
238 if (curwin->w_valid & VALID_TOPLINE)
239 return;
240
Bram Moolenaar85a20022019-12-21 18:25:54 +0100241 // When dragging with the mouse, don't scroll that quickly
Bram Moolenaar9964e462007-05-05 17:54:07 +0000242 if (mouse_dragging > 0)
Bram Moolenaar375e3392019-01-31 18:26:10 +0100243 *so_ptr = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245 old_topline = curwin->w_topline;
246#ifdef FEAT_DIFF
247 old_topfill = curwin->w_topfill;
248#endif
249
250 /*
251 * If the buffer is empty, always set topline to 1.
252 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100253 if (BUFEMPTY()) // special case - file is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {
255 if (curwin->w_topline != 1)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100256 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 curwin->w_botline = 2;
259 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 curwin->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
262
263 /*
264 * If the cursor is above or near the top of the window, scroll the window
265 * to show the line the cursor is in, with 'scrolloff' context.
266 */
267 else
268 {
Bram Moolenaar46b54742022-10-06 15:46:49 +0100269 if (curwin->w_topline > 1 || curwin->w_skipcol > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100271 // If the cursor is above topline, scrolling is always needed.
272 // If the cursor is far below topline and there is no folding,
273 // scrolling down is never needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 if (curwin->w_cursor.lnum < curwin->w_topline)
275 check_topline = TRUE;
276 else if (check_top_offset())
277 check_topline = TRUE;
Bram Moolenaar46b54742022-10-06 15:46:49 +0100278 else if (curwin->w_cursor.lnum == curwin->w_topline)
279 {
280 colnr_T vcol;
281
282 // check the cursor position is visible. Add 3 for the ">>>"
283 // displayed in the top-left.
284 getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
285 if (curwin->w_skipcol + 3 >= vcol)
286 check_topline = TRUE;
287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
289#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
292 curwin->w_topline))
293 check_topline = TRUE;
294#endif
295
296 if (check_topline)
297 {
298 halfheight = curwin->w_height / 2 - 1;
299 if (halfheight < 2)
300 halfheight = 2;
301
302#ifdef FEAT_FOLDING
303 if (hasAnyFolding(curwin))
304 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100305 // Count the number of logical lines between the cursor and
306 // topline + scrolloff (approximation of how much will be
307 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 n = 0;
309 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 {
312 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100313 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
315 break;
316 (void)hasFolding(lnum, NULL, &lnum);
317 }
318 }
319 else
320#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100321 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaar85a20022019-12-21 18:25:54 +0100323 // If we weren't very close to begin with, we scroll to put the
324 // cursor in the middle of the window. Otherwise put the cursor
325 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 if (n >= halfheight)
327 scroll_cursor_halfway(FALSE);
328 else
329 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000330 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 check_botline = TRUE;
332 }
333 }
334
335 else
336 {
337#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100338 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
340#endif
341 check_botline = TRUE;
342 }
343 }
344
345 /*
346 * If the cursor is below the bottom of the window, scroll the window
347 * to put the cursor on the window.
348 * When w_botline is invalid, recompute it first, to avoid a redraw later.
349 * If w_botline was approximated, we might need a redraw later in a few
350 * cases, but we don't want to spend (a lot of) time recomputing w_botline
351 * for every small change.
352 */
353 if (check_botline)
354 {
355 if (!(curwin->w_valid & VALID_BOTLINE_AP))
356 validate_botline();
357
358 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
359 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000360 if (curwin->w_cursor.lnum < curwin->w_botline)
361 {
362 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100363 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364#ifdef FEAT_FOLDING
365 || hasAnyFolding(curwin)
366#endif
367 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 lineoff_T loff;
370
Bram Moolenaar85a20022019-12-21 18:25:54 +0100371 // Cursor is (a few lines) above botline, check if there are
372 // 'scrolloff' window lines below the cursor. If not, need to
373 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 n = curwin->w_empty_rows;
375 loff.lnum = curwin->w_cursor.lnum;
376#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100377 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
379#endif
380#ifdef FEAT_DIFF
381 loff.fill = 0;
382 n += curwin->w_filler_rows;
383#endif
384 loff.height = 0;
385 while (loff.lnum < curwin->w_botline
386#ifdef FEAT_DIFF
387 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
388#endif
389 )
390 {
391 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100392 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 break;
394 botline_forw(&loff);
395 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100396 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100397 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000399 }
400 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100401 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000402 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 }
404 if (check_botline)
405 {
406#ifdef FEAT_FOLDING
407 if (hasAnyFolding(curwin))
408 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // Count the number of logical lines between the cursor and
410 // botline - scrolloff (approximation of how much will be
411 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 line_count = 0;
413 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100414 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 {
416 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100417 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 if (lnum <= 0 || line_count > curwin->w_height + 1)
419 break;
420 (void)hasFolding(lnum, &lnum, NULL);
421 }
422 }
423 else
424#endif
425 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100426 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000428 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 else
430 scroll_cursor_halfway(FALSE);
431 }
432 }
433 }
434 curwin->w_valid |= VALID_TOPLINE;
435
436 /*
437 * Need to redraw when topline changed.
438 */
439 if (curwin->w_topline != old_topline
440#ifdef FEAT_DIFF
441 || curwin->w_topfill != old_topfill
442#endif
443 )
444 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100445 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000446 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {
448 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100449 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 }
451 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100452 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100453 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (curwin->w_cursor.lnum == curwin->w_topline)
455 validate_cursor();
456 }
457
Bram Moolenaar375e3392019-01-31 18:26:10 +0100458 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459}
460
461/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000462 * Return the scrolljump value to use for the current window.
463 * When 'scrolljump' is positive use it as-is.
464 * When 'scrolljump' is negative use it as a percentage of the window height.
465 */
466 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100467scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000468{
469 if (p_sj >= 0)
470 return (int)p_sj;
471 return (curwin->w_height * -p_sj) / 100;
472}
473
474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
476 * current window.
477 */
478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100479check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 lineoff_T loff;
482 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100483 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
Bram Moolenaar375e3392019-01-31 18:26:10 +0100485 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486#ifdef FEAT_FOLDING
487 || hasAnyFolding(curwin)
488#endif
489 )
490 {
491 loff.lnum = curwin->w_cursor.lnum;
492#ifdef FEAT_DIFF
493 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100494 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#else
496 n = 0;
497#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100498 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100499 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100502 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 if (loff.lnum < curwin->w_topline
504#ifdef FEAT_DIFF
505 || (loff.lnum == curwin->w_topline && loff.fill > 0)
506#endif
507 )
508 break;
509 n += loff.height;
510 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100511 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 return TRUE;
513 }
514 return FALSE;
515}
516
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100517/*
518 * Update w_curswant.
519 */
520 void
521update_curswant_force(void)
522{
523 validate_virtcol();
524 curwin->w_curswant = curwin->w_virtcol
525#ifdef FEAT_PROP_POPUP
526 - curwin->w_virtcol_first_char
527#endif
528 ;
529 curwin->w_set_curswant = FALSE;
530}
531
532/*
533 * Update w_curswant if w_set_curswant is set.
534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100539 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
541
542/*
543 * Check if the cursor has moved. Set the w_valid flag accordingly.
544 */
545 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100546check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
549 {
550 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100551 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
552 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 wp->w_valid_cursor = wp->w_cursor;
554 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +0100555 wp->w_valid_skipcol = wp->w_skipcol;
556 }
557 else if (wp->w_skipcol != wp->w_valid_skipcol)
558 {
559 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
560 |VALID_CHEIGHT|VALID_CROW
561 |VALID_BOTLINE|VALID_BOTLINE_AP);
562 wp->w_valid_cursor = wp->w_cursor;
563 wp->w_valid_leftcol = wp->w_leftcol;
564 wp->w_valid_skipcol = wp->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566 else if (wp->w_cursor.col != wp->w_valid_cursor.col
567 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100568 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
570 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
571 wp->w_valid_cursor.col = wp->w_cursor.col;
572 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575}
576
577/*
578 * Call this function when some window settings have changed, which require
579 * the cursor position, botline and topline to be recomputed and the window to
580 * be redrawn. E.g, when changing the 'wrap' option or folding.
581 */
582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100583changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 changed_window_setting_win(curwin);
586}
587
588 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100589changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 wp->w_lines_valid = 0;
592 changed_line_abv_curs_win(wp);
593 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100594 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595}
596
597/*
598 * Set wp->w_topline to a certain number.
599 */
600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100601set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200603#ifdef FEAT_DIFF
604 linenr_T prev_topline = wp->w_topline;
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100608 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
610#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100611 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100613 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
614 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000616 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200618 if (lnum != prev_topline)
619 // Keep the filler lines when the topline didn't change.
620 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#endif
622 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100623 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100624 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625}
626
627/*
628 * Call this function when the length of the cursor line (in screen
629 * characters) has changed, and the change is before the cursor.
630 * Need to take care of w_botline separately!
631 */
632 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100633changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
636 |VALID_CHEIGHT|VALID_TOPLINE);
637}
638
639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
643 |VALID_CHEIGHT|VALID_TOPLINE);
644}
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646/*
647 * Call this function when the length of a line (in screen characters) above
648 * the cursor have changed.
649 * Need to take care of w_botline separately!
650 */
651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100652changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
655 |VALID_CHEIGHT|VALID_TOPLINE);
656}
657
658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100659changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
662 |VALID_CHEIGHT|VALID_TOPLINE);
663}
664
665/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100666 * Display of line has changed for "buf", invalidate cursor position and
667 * w_botline.
668 */
669 void
670changed_line_display_buf(buf_T *buf)
671{
672 win_T *wp;
673
674 FOR_ALL_WINDOWS(wp)
675 if (wp->w_buffer == buf)
676 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
677 |VALID_CROW|VALID_CHEIGHT
678 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
679}
680
681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 * Make sure the value of curwin->w_botline is valid.
683 */
684 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100685validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100687 validate_botline_win(curwin);
688}
689
690/*
691 * Make sure the value of wp->w_botline is valid.
692 */
693 void
694validate_botline_win(win_T *wp)
695{
696 if (!(wp->w_valid & VALID_BOTLINE))
697 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698}
699
700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 * Mark curwin->w_botline as invalid (because of some change in the buffer).
702 */
703 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100704invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
707}
708
709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100710invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
713}
714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100716approximate_botline_win(
717 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 wp->w_valid &= ~VALID_BOTLINE;
720}
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722/*
723 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
724 */
725 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100726cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 check_cursor_moved(curwin);
729 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
730 (VALID_WROW|VALID_WCOL));
731}
732
733/*
734 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
735 * w_topline must be valid, you may need to call update_topline() first!
736 */
737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100738validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100740 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 check_cursor_moved(curwin);
742 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
743 curs_columns(TRUE);
744}
745
746#if defined(FEAT_GUI) || defined(PROTO)
747/*
748 * validate w_cline_row.
749 */
750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 /*
754 * First make sure that w_topline is valid (after moving the cursor).
755 */
756 update_topline();
757 check_cursor_moved(curwin);
758 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100759 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760}
761#endif
762
763/*
764 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200765 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 */
767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100768curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
770 linenr_T lnum;
771 int i;
772 int all_invalid;
773 int valid;
774#ifdef FEAT_FOLDING
775 long fold_count;
776#endif
777
Bram Moolenaar85a20022019-12-21 18:25:54 +0100778 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 all_invalid = (!redrawing()
780 || wp->w_lines_valid == 0
781 || wp->w_lines[0].wl_lnum > wp->w_topline);
782 i = 0;
783 wp->w_cline_row = 0;
784 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
785 {
786 valid = FALSE;
787 if (!all_invalid && i < wp->w_lines_valid)
788 {
789 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100790 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (wp->w_lines[i].wl_lnum == lnum)
792 {
793#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100794 // Check for newly inserted lines below this row, in which
795 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (!wp->w_buffer->b_mod_set
797 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
798 || wp->w_buffer->b_mod_top
799 > wp->w_lines[i].wl_lastlnum + 1)
800#endif
801 valid = TRUE;
802 }
803 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100804 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 if (valid
807#ifdef FEAT_DIFF
808 && (lnum != wp->w_topline || !wp->w_p_diff)
809#endif
810 )
811 {
812#ifdef FEAT_FOLDING
813 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100814 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (lnum > wp->w_cursor.lnum)
816 break;
817#else
818 ++lnum;
819#endif
820 wp->w_cline_row += wp->w_lines[i].wl_size;
821 }
822 else
823 {
824#ifdef FEAT_FOLDING
825 fold_count = foldedCount(wp, lnum, NULL);
826 if (fold_count)
827 {
828 lnum += fold_count;
829 if (lnum > wp->w_cursor.lnum)
830 break;
831 ++wp->w_cline_row;
832 }
833 else
834#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100835 {
836 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#ifdef FEAT_DIFF
838 if (lnum == wp->w_topline)
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100839 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 else
841#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100842 n = plines_win(wp, lnum, TRUE);
843 if (lnum++ == wp->w_topline)
844 n = adjust_plines_for_skipcol(wp, n);
845 wp->w_cline_row += n;
846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
848 }
849
850 check_cursor_moved(wp);
851 if (!(wp->w_valid & VALID_CHEIGHT))
852 {
853 if (all_invalid
854 || i == wp->w_lines_valid
855 || (i < wp->w_lines_valid
856 && (!wp->w_lines[i].wl_valid
857 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
858 {
859#ifdef FEAT_DIFF
860 if (wp->w_cursor.lnum == wp->w_topline)
861 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
862 TRUE) + wp->w_topfill;
863 else
864#endif
865 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
866#ifdef FEAT_FOLDING
867 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
868 NULL, NULL, TRUE, NULL);
869#endif
870 }
871 else if (i > wp->w_lines_valid)
872 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100873 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 wp->w_cline_height = 0;
875#ifdef FEAT_FOLDING
876 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
877 NULL, NULL, TRUE, NULL);
878#endif
879 }
880 else
881 {
882 wp->w_cline_height = wp->w_lines[i].wl_size;
883#ifdef FEAT_FOLDING
884 wp->w_cline_folded = wp->w_lines[i].wl_folded;
885#endif
886 }
887 }
888
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100889 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891}
892
893/*
894 * Validate curwin->w_virtcol only.
895 */
896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100897validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899 validate_virtcol_win(curwin);
900}
901
902/*
903 * Validate wp->w_virtcol only.
904 */
905 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100906validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
908 check_cursor_moved(wp);
909 if (!(wp->w_valid & VALID_VIRTCOL))
910 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100911#ifdef FEAT_PROP_POPUP
912 wp->w_virtcol_first_char = 0;
913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000915#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100916 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000917#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100918 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920}
921
922/*
923 * Validate curwin->w_cline_height only.
924 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100926validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 check_cursor_moved(curwin);
929 if (!(curwin->w_valid & VALID_CHEIGHT))
930 {
931#ifdef FEAT_DIFF
932 if (curwin->w_cursor.lnum == curwin->w_topline)
933 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
934 + curwin->w_topfill;
935 else
936#endif
937 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
938#ifdef FEAT_FOLDING
939 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
940#endif
941 curwin->w_valid |= VALID_CHEIGHT;
942 }
943}
944
945/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000946 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 */
948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100949validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
951 colnr_T off;
952 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100953 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 validate_virtcol();
956 if (!(curwin->w_valid & VALID_WCOL))
957 {
958 col = curwin->w_virtcol;
959 off = curwin_col_off();
960 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200961 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaar85a20022019-12-21 18:25:54 +0100963 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000964 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200965 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100966 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200968 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000969 if (col > (int)curwin->w_leftcol)
970 col -= curwin->w_leftcol;
971 else
972 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100976#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100977 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980}
981
982/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200983 * Compute offset of a window, occupied by absolute or relative line number,
984 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 */
986 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100987win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
Bram Moolenaar64486672010-05-16 15:46:46 +0200989 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_FOLDING
992 + wp->w_p_fdc
993#endif
994#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200995 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#endif
997 );
998}
999
1000 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001001curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 return win_col_off(curwin);
1004}
1005
1006/*
1007 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001008 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
1009 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
1011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001012win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
Bram Moolenaar64486672010-05-16 15:46:46 +02001014 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001015 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 return 0;
1017}
1018
1019 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001020curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022 return win_col_off2(curwin);
1023}
1024
1025/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001026 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Also updates curwin->w_wrow and curwin->w_cline_row.
1028 * Also updates curwin->w_leftcol.
1029 */
1030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001031curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001032 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001035 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int off_left, off_right;
1037 int n;
1038 int p_lines;
1039 int width = 0;
1040 int textwidth;
1041 int new_leftcol;
1042 colnr_T startcol;
1043 colnr_T endcol;
1044 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001045 long so = get_scrolloff_value();
1046 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
1048 /*
1049 * First make sure that w_topline is valid (after moving the cursor).
1050 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001051 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
1053 /*
1054 * Next make sure that w_cline_row is valid.
1055 */
1056 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001057 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001059#ifdef FEAT_PROP_POPUP
1060 // will be set by getvvcol() but not reset
1061 curwin->w_virtcol_first_char = 0;
1062#endif
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 /*
1065 * Compute the number of virtual columns.
1066 */
1067#ifdef FEAT_FOLDING
1068 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001069 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1071 else
1072#endif
1073 getvvcol(curwin, &curwin->w_cursor,
1074 &startcol, &(curwin->w_virtcol), &endcol);
1075
Bram Moolenaar85a20022019-12-21 18:25:54 +01001076 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001078 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080 extra = curwin_col_off();
1081 curwin->w_wcol = curwin->w_virtcol + extra;
1082 endcol += extra;
1083
1084 /*
1085 * Now compute w_wrow, counting screen lines from w_cline_row.
1086 */
1087 curwin->w_wrow = curwin->w_cline_row;
1088
Bram Moolenaar02631462017-09-22 15:20:32 +02001089 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (textwidth <= 0)
1091 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001092 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001093 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001094 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001095 if (curwin->w_p_wrap)
1096 curwin->w_wrow = curwin->w_height - 1;
1097 else
1098 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001100 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
1102 width = textwidth + curwin_col_off2();
1103
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001104 // skip columns that are not visible
1105 if (curwin->w_cursor.lnum == curwin->w_topline
1106 && curwin->w_wcol >= curwin->w_skipcol)
1107 curwin->w_wcol -= curwin->w_skipcol;
1108
Bram Moolenaar85a20022019-12-21 18:25:54 +01001109 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001110 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001112#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001113 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001114#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001115
Bram Moolenaar85a20022019-12-21 18:25:54 +01001116 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001117 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 curwin->w_wcol -= n * width;
1119 curwin->w_wrow += n;
1120
1121#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001122 // When cursor wraps to first char of next line in Insert
1123 // mode, the 'showbreak' string isn't shown, backup to first
1124 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001125 sbr = get_showbreak_value(curwin);
1126 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001127 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 curwin->w_wcol = 0;
1129#endif
1130 }
1131 }
1132
Bram Moolenaar85a20022019-12-21 18:25:54 +01001133 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1134 // is not folded.
1135 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001136 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#ifdef FEAT_FOLDING
1138 && !curwin->w_cline_folded
1139#endif
1140 )
1141 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001142#ifdef FEAT_PROP_POPUP
1143 if (curwin->w_virtcol_first_char > 0)
1144 {
1145 int cols = (curwin->w_width - extra);
1146 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1147
1148 // each "above" text prop shifts the text one row down
1149 curwin->w_wrow += rows;
1150 curwin->w_wcol -= rows * cols;
1151 endcol -= rows * cols;
1152 curwin->w_cline_height = rows + 1;
1153 }
1154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 /*
1156 * If Cursor is left of the screen, scroll rightwards.
1157 * If Cursor is right of the screen, scroll leftwards
1158 * If we get closer to the edge than 'sidescrolloff', scroll a little
1159 * extra
1160 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001161 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001162 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001163 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (off_left < 0 || off_right > 0)
1165 {
1166 if (off_left < 0)
1167 diff = -off_left;
1168 else
1169 diff = off_right;
1170
Bram Moolenaar85a20022019-12-21 18:25:54 +01001171 // When far off or not enough room on either side, put cursor in
1172 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1174 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1175 else
1176 {
1177 if (diff < p_ss)
1178 diff = p_ss;
1179 if (off_left < 0)
1180 new_leftcol = curwin->w_leftcol - diff;
1181 else
1182 new_leftcol = curwin->w_leftcol + diff;
1183 }
1184 if (new_leftcol < 0)
1185 new_leftcol = 0;
1186 if (new_leftcol != (int)curwin->w_leftcol)
1187 {
1188 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001190 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 }
1193 curwin->w_wcol -= curwin->w_leftcol;
1194 }
1195 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1196 curwin->w_wcol -= curwin->w_leftcol;
1197 else
1198 curwin->w_wcol = 0;
1199
1200#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 // Skip over filler lines. At the top use w_topfill, there
1202 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (curwin->w_cursor.lnum == curwin->w_topline)
1204 curwin->w_wrow += curwin->w_topfill;
1205 else
1206 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1207#endif
1208
1209 prev_skipcol = curwin->w_skipcol;
1210
1211 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if ((curwin->w_wrow >= curwin->w_height
1214 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001215 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 && (p_lines =
1217#ifdef FEAT_DIFF
1218 plines_win_nofill
1219#else
1220 plines_win
1221#endif
1222 (curwin, curwin->w_cursor.lnum, FALSE))
1223 - 1 >= curwin->w_height))
1224 && curwin->w_height != 0
1225 && curwin->w_cursor.lnum == curwin->w_topline
1226 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001227 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001229 // Cursor past end of screen. Happens with a single line that does
1230 // not fit on screen. Find a skipcol to show the text around the
1231 // cursor. Avoid scrolling all the time. compute value of "extra":
1232 // 1: Less than 'scrolloff' lines above
1233 // 2: Less than 'scrolloff' lines below
1234 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001236 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001238 // Compute last display line of the buffer line that we want at the
1239 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (p_lines == 0)
1241 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1242 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001243 if (p_lines > curwin->w_wrow + so)
1244 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 else
1246 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001247 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 extra += 2;
1249
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001250 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001252 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 n = curwin->w_virtcol / width;
1254 if (n > curwin->w_height / 2)
1255 n -= curwin->w_height / 2;
1256 else
1257 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001258 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (n > p_lines - curwin->w_height + 1)
1260 n = p_lines - curwin->w_height + 1;
1261 curwin->w_skipcol = n * width;
1262 }
1263 else if (extra == 1)
1264 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001265 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001266 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 + width - 1) / width;
1268 if (extra > 0)
1269 {
1270 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1271 extra = curwin->w_skipcol / width;
1272 curwin->w_skipcol -= extra * width;
1273 }
1274 }
1275 else if (extra == 2)
1276 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001277 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 endcol = (n - curwin->w_height + 1) * width;
1279 while (endcol > curwin->w_virtcol)
1280 endcol -= width;
1281 if (endcol > curwin->w_skipcol)
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001282 {
1283 curwin->w_wrow -= (endcol - curwin->w_skipcol) / width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 curwin->w_skipcol = endcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (curwin->w_wrow >= curwin->w_height)
1289 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001290 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 extra = curwin->w_wrow - curwin->w_height + 1;
1292 curwin->w_skipcol += extra * width;
1293 curwin->w_wrow -= extra;
1294 }
1295
1296 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1297 if (extra > 0)
1298 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1299 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001300 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001302 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 curwin->w_skipcol = 0;
1304 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001305 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001307#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001308 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001309#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001310#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1311 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1312 {
1313 curwin->w_wrow += popup_top_extra(curwin);
1314 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001315 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001316 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001317 else
1318 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001319#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001320
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001321 // now w_leftcol and w_skipcol are valid, avoid check_cursor_moved()
1322 // thinking otherwise
Bram Moolenaar08f23632019-11-16 14:19:33 +01001323 curwin->w_valid_leftcol = curwin->w_leftcol;
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001324 curwin->w_valid_skipcol = curwin->w_skipcol;
Bram Moolenaar08f23632019-11-16 14:19:33 +01001325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1327}
1328
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001329#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001330/*
1331 * Compute the screen position of text character at "pos" in window "wp"
1332 * The resulting values are one-based, zero when character is not visible.
1333 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001334 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001335textpos2screenpos(
1336 win_T *wp,
1337 pos_T *pos,
1338 int *rowp, // screen row
1339 int *scolp, // start screen column
1340 int *ccolp, // cursor screen column
1341 int *ecolp) // end screen column
1342{
1343 colnr_T scol = 0, ccol = 0, ecol = 0;
1344 int row = 0;
1345 int rowoff = 0;
1346 colnr_T coloff = 0;
1347
Bram Moolenaar189663b2021-07-21 18:04:56 +02001348 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001349 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001350 colnr_T off;
1351 colnr_T col;
1352 int width;
1353 linenr_T lnum = pos->lnum;
1354#ifdef FEAT_FOLDING
1355 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001356
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001357 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1358#endif
1359 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1360#ifdef FEAT_FOLDING
1361 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001362 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001363 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001364 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001365 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001366 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001367#endif
1368 {
1369 getvcol(wp, pos, &scol, &ccol, &ecol);
1370
1371 // similar to what is done in validate_cursor_col()
1372 col = scol;
1373 off = win_col_off(wp);
1374 col += off;
1375 width = wp->w_width - off + win_col_off2(wp);
1376
1377 // long line wrapping, adjust row
1378 if (wp->w_p_wrap
1379 && col >= (colnr_T)wp->w_width
1380 && width > 0)
1381 {
1382 // use same formula as what is used in curs_columns()
1383 rowoff = ((col - wp->w_width) / width + 1);
1384 col -= rowoff * width;
1385 }
1386 col -= wp->w_leftcol;
1387 if (col >= wp->w_width)
1388 col = -1;
1389 if (col >= 0 && row + rowoff <= wp->w_height)
1390 {
1391 coloff = col - scol + wp->w_wincol + 1;
1392 row += W_WINROW(wp);
1393 }
1394 else
1395 // character is left, right or below of the window
1396 row = rowoff = scol = ccol = ecol = 0;
1397 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001398 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001399 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001400 *scolp = scol + coloff;
1401 *ccolp = ccol + coloff;
1402 *ecolp = ecol + coloff;
1403}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001404#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001405
Bram Moolenaar12034e22019-08-25 22:25:02 +02001406#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001407/*
1408 * "screenpos({winid}, {lnum}, {col})" function
1409 */
1410 void
1411f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1412{
1413 dict_T *dict;
1414 win_T *wp;
1415 pos_T pos;
1416 int row = 0;
1417 int scol = 0, ccol = 0, ecol = 0;
1418
Bram Moolenaar93a10962022-06-16 11:42:09 +01001419 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001420 return;
1421 dict = rettv->vval.v_dict;
1422
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001423 if (in_vim9script()
1424 && (check_for_number_arg(argvars, 0) == FAIL
1425 || check_for_number_arg(argvars, 1) == FAIL
1426 || check_for_number_arg(argvars, 2) == FAIL))
1427 return;
1428
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001429 wp = find_win_by_nr_or_id(&argvars[0]);
1430 if (wp == NULL)
1431 return;
1432
1433 pos.lnum = tv_get_number(&argvars[1]);
1434 pos.col = tv_get_number(&argvars[2]) - 1;
1435 pos.coladd = 0;
1436 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1437
1438 dict_add_number(dict, "row", row);
1439 dict_add_number(dict, "col", scol);
1440 dict_add_number(dict, "curscol", ccol);
1441 dict_add_number(dict, "endcol", ecol);
1442}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001443
1444/*
1445 * "virtcol2col({winid}, {lnum}, {col})" function
1446 */
1447 void
1448f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1449{
1450 win_T *wp;
1451 linenr_T lnum;
1452 int screencol;
1453 int error = FALSE;
1454
1455 rettv->vval.v_number = -1;
1456
1457 if (check_for_number_arg(argvars, 0) == FAIL
1458 || check_for_number_arg(argvars, 1) == FAIL
1459 || check_for_number_arg(argvars, 2) == FAIL)
1460 return;
1461
1462 wp = find_win_by_nr_or_id(&argvars[0]);
1463 if (wp == NULL)
1464 return;
1465
1466 lnum = tv_get_number_chk(&argvars[1], &error);
1467 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1468 return;
1469
1470 screencol = tv_get_number_chk(&argvars[2], &error);
1471 if (error || screencol < 0)
1472 return;
1473
1474 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1475}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001476#endif
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478/*
1479 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001482scrolldown(
1483 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001484 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001486 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 int wrow;
1488 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001489 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001490 int width1 = 0;
1491 int width2 = 0;
1492
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001493 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001494 {
1495 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001496 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498
1499#ifdef FEAT_FOLDING
1500 linenr_T first;
1501
Bram Moolenaar85a20022019-12-21 18:25:54 +01001502 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1504#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001505 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001506 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001509 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1510 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
1512 ++curwin->w_topfill;
1513 ++done;
1514 }
1515 else
1516#endif
1517 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001518 // break when at the very top
1519 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001520 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001522 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001524 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001525 if (curwin->w_skipcol >= width1 + width2)
1526 curwin->w_skipcol -= width2;
1527 else
1528 curwin->w_skipcol -= width1;
1529 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001533 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001534 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001535 --curwin->w_topline;
1536 curwin->w_skipcol = 0;
1537#ifdef FEAT_DIFF
1538 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001540#ifdef FEAT_FOLDING
1541 // A sequence of folded lines only counts for one logical line
1542 if (hasFolding(curwin->w_topline, &first, NULL))
1543 {
1544 ++done;
1545 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001546 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001547 curwin->w_botline -= curwin->w_topline - first;
1548 curwin->w_topline = first;
1549 }
1550 else
1551#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001552 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001553 {
1554 int size = win_linetabsize(curwin, curwin->w_topline,
1555 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1556 if (size > width1)
1557 {
1558 curwin->w_skipcol = width1;
1559 size -= width1;
1560 redraw_later(UPD_NOT_VALID);
1561 }
1562 while (size > width2)
1563 {
1564 curwin->w_skipcol += width2;
1565 size -= width2;
1566 }
1567 ++done;
1568 }
1569 else
1570 done += PLINES_NOFILL(curwin->w_topline);
1571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001573 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 invalidate_botline();
1575 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001576 curwin->w_wrow += done; // keep w_wrow updated
1577 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579#ifdef FEAT_DIFF
1580 if (curwin->w_cursor.lnum == curwin->w_topline)
1581 curwin->w_cline_row = 0;
1582 check_topfill(curwin, TRUE);
1583#endif
1584
1585 /*
1586 * Compute the row number of the last row of the cursor line
1587 * and move the cursor onto the displayed part of the window.
1588 */
1589 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001590 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
1592 validate_virtcol();
1593 validate_cheight();
1594 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001595 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1598 {
1599#ifdef FEAT_FOLDING
1600 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1601 {
1602 --wrow;
1603 if (first == 1)
1604 curwin->w_cursor.lnum = 1;
1605 else
1606 curwin->w_cursor.lnum = first - 1;
1607 }
1608 else
1609#endif
1610 wrow -= plines(curwin->w_cursor.lnum--);
1611 curwin->w_valid &=
1612 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1613 moved = TRUE;
1614 }
1615 if (moved)
1616 {
1617#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001618 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 foldAdjustCursor();
1620#endif
1621 coladvance(curwin->w_curswant);
1622 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001623
1624 if (curwin->w_cursor.lnum == curwin->w_topline && do_sms)
1625 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001626 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1627 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1628
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001629 // make sure the cursor is in the visible text
1630 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001631 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001632 int row = 0;
1633 if (col >= width1)
1634 {
1635 col -= width1;
1636 ++row;
1637 }
1638 if (col > width2)
1639 {
1640 row += col / width2;
1641 col = col % width2;
1642 }
1643 if (row >= curwin->w_height)
Bram Moolenaar118c2352022-10-09 17:19:27 +01001644 {
1645 curwin->w_curswant = curwin->w_virtcol
1646 - (row - curwin->w_height + 1) * width2;
1647 coladvance(curwin->w_curswant);
1648 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650}
1651
1652/*
1653 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001656scrollup(
1657 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001658 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001660 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001662 if (do_sms
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663# ifdef FEAT_FOLDING
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001664 || (byfold && hasAnyFolding(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665# endif
1666# ifdef FEAT_DIFF
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001667 || curwin->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668# endif
1669 )
1670 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001671 int width1 = curwin->w_width - curwin_col_off();
1672 int width2 = width1 + curwin_col_off2();
1673 int size = 0;
1674 linenr_T prev_topline = curwin->w_topline;
1675
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001676 if (do_sms)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001677 size = win_linetabsize(curwin, curwin->w_topline,
1678 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1679
1680 // diff mode: first consume "topfill"
1681 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1682 // the line, then advance to the next line.
1683 // folding: count each sequence of folded lines as one logical line.
1684 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
1686# ifdef FEAT_DIFF
1687 if (curwin->w_topfill > 0)
1688 --curwin->w_topfill;
1689 else
1690# endif
1691 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001692 linenr_T lnum = curwin->w_topline;
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694# ifdef FEAT_FOLDING
1695 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001696 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 (void)hasFolding(lnum, NULL, &lnum);
1698# endif
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001699 if (lnum == curwin->w_topline && do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001700 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001701 // 'smoothscroll': increase "w_skipcol" until it goes over
1702 // the end of the line, then advance to the next line.
1703 int add = curwin->w_skipcol > 0 ? width2 : width1;
1704 curwin->w_skipcol += add;
1705 if (curwin->w_skipcol >= size)
1706 {
1707 if (lnum == curbuf->b_ml.ml_line_count)
1708 {
1709 // at the last screen line, can't scroll further
1710 curwin->w_skipcol -= add;
1711 break;
1712 }
1713 ++lnum;
1714 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001715 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001716 else
1717 {
1718 if (lnum >= curbuf->b_ml.ml_line_count)
1719 break;
1720 ++lnum;
1721 }
1722
1723 if (lnum > curwin->w_topline)
1724 {
1725 // approximate w_botline
1726 curwin->w_botline += lnum - curwin->w_topline;
1727 curwin->w_topline = lnum;
1728# ifdef FEAT_DIFF
1729 curwin->w_topfill = diff_check_fill(curwin, lnum);
1730# endif
1731 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001732 if (todo > 1 && do_sms)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001733 size = win_linetabsize(curwin, curwin->w_topline,
1734 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1735 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001736 }
1737 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001738
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001739 if (curwin->w_topline == prev_topline)
1740 // need to redraw even though w_topline didn't change
1741 redraw_later(UPD_NOT_VALID);
1742 }
1743 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {
1745 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001746 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748
1749 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1750 curwin->w_topline = curbuf->b_ml.ml_line_count;
1751 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1752 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1753
1754#ifdef FEAT_DIFF
1755 check_topfill(curwin, FALSE);
1756#endif
1757
1758#ifdef FEAT_FOLDING
1759 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001760 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1762#endif
1763
1764 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1765 if (curwin->w_cursor.lnum < curwin->w_topline)
1766 {
1767 curwin->w_cursor.lnum = curwin->w_topline;
1768 curwin->w_valid &=
1769 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1770 coladvance(curwin->w_curswant);
1771 }
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001772 if (curwin->w_cursor.lnum == curwin->w_topline
1773 && do_sms && curwin->w_skipcol > 0)
1774 {
Bram Moolenaar118c2352022-10-09 17:19:27 +01001775 int width1 = curwin->w_width - curwin_col_off();
1776 int width2 = width1 + curwin_col_off2();
1777 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1778 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1779
1780 // Make sure the cursor is in a visible part of the line, taking
1781 // 'scrolloff' into account, but using screen lines.
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001782 validate_virtcol();
Bram Moolenaar118c2352022-10-09 17:19:27 +01001783 if (curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001784 {
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001785 colnr_T col = curwin->w_virtcol;
1786
1787 if (col < width1)
1788 col += width1;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001789 while (col < curwin->w_skipcol + 3 + scrolloff_cols)
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001790 col += width2;
Bram Moolenaar118c2352022-10-09 17:19:27 +01001791 curwin->w_curswant = col;
1792 coladvance(curwin->w_curswant);
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001793
1794 // validate_virtcol() marked various things as valid, but after
1795 // moving the cursor they need to be recomputed
1796 curwin->w_valid &=
1797 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
Bram Moolenaar8cf34592022-10-08 21:13:40 +01001798 }
1799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800}
1801
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01001802/*
1803 * Called after changing the cursor column: make sure that curwin->w_skipcol is
1804 * valid for 'smoothscroll'.
1805 */
1806 void
1807adjust_skipcol(void)
1808{
1809 if (!curwin->w_p_wrap
1810 || !curwin->w_p_sms
1811 || curwin->w_cursor.lnum != curwin->w_topline)
1812 return;
1813
1814 int width1 = curwin->w_width - curwin_col_off();
1815 int width2 = width1 + curwin_col_off2();
1816 long so = curwin->w_p_so >= 0 ? curwin->w_p_so : p_so;
1817 int scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
1818 int scrolled = FALSE;
1819
1820 validate_virtcol();
1821 while (curwin->w_skipcol > 0
1822 && curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
1823 {
1824 // scroll a screen line down
1825 if (curwin->w_skipcol >= width1 + width2)
1826 curwin->w_skipcol -= width2;
1827 else
1828 curwin->w_skipcol -= width1;
1829 redraw_later(UPD_NOT_VALID);
1830 scrolled = TRUE;
1831 validate_virtcol();
1832 }
1833 if (scrolled)
1834 return; // don't scroll in the other direction now
1835
1836 int col = curwin->w_virtcol - curwin->w_skipcol + scrolloff_cols;
1837 int row = 0;
1838 if (col >= width1)
1839 {
1840 col -= width1;
1841 ++row;
1842 }
1843 if (col > width2)
1844 {
1845 row += col / width2;
1846 col = col % width2;
1847 }
1848 if (row >= curwin->w_height)
1849 {
1850 if (curwin->w_skipcol == 0)
1851 {
1852 curwin->w_skipcol += width1;
1853 --row;
1854 }
1855 if (row >= curwin->w_height)
1856 curwin->w_skipcol += (row - curwin->w_height) * width2;
1857 redraw_later(UPD_NOT_VALID);
1858 }
1859}
1860
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861#ifdef FEAT_DIFF
1862/*
1863 * Don't end up with too many filler lines in the window.
1864 */
1865 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001866check_topfill(
1867 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001868 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869{
1870 int n;
1871
1872 if (wp->w_topfill > 0)
1873 {
1874 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1875 if (wp->w_topfill + n > wp->w_height)
1876 {
1877 if (down && wp->w_topline > 1)
1878 {
1879 --wp->w_topline;
1880 wp->w_topfill = 0;
1881 }
1882 else
1883 {
1884 wp->w_topfill = wp->w_height - n;
1885 if (wp->w_topfill < 0)
1886 wp->w_topfill = 0;
1887 }
1888 }
1889 }
1890}
1891
1892/*
1893 * Use as many filler lines as possible for w_topline. Make sure w_topline
1894 * is still visible.
1895 */
1896 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001897max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 int n;
1900
1901 n = plines_nofill(curwin->w_topline);
1902 if (n >= curwin->w_height)
1903 curwin->w_topfill = 0;
1904 else
1905 {
1906 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1907 if (curwin->w_topfill + n > curwin->w_height)
1908 curwin->w_topfill = curwin->w_height - n;
1909 }
1910}
1911#endif
1912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913/*
1914 * Scroll the screen one line down, but don't do it if it would move the
1915 * cursor off the screen.
1916 */
1917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001918scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 int end_row;
1921#ifdef FEAT_DIFF
1922 int can_fill = (curwin->w_topfill
1923 < diff_check_fill(curwin, curwin->w_topline));
1924#endif
1925
1926 if (curwin->w_topline <= 1
1927#ifdef FEAT_DIFF
1928 && !can_fill
1929#endif
1930 )
1931 return;
1932
Bram Moolenaar85a20022019-12-21 18:25:54 +01001933 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
1935 /*
1936 * Compute the row number of the last row of the cursor line
1937 * and make sure it doesn't go off the screen. Make sure the cursor
1938 * doesn't go past 'scrolloff' lines from the screen end.
1939 */
1940 end_row = curwin->w_wrow;
1941#ifdef FEAT_DIFF
1942 if (can_fill)
1943 ++end_row;
1944 else
1945 end_row += plines_nofill(curwin->w_topline - 1);
1946#else
1947 end_row += plines(curwin->w_topline - 1);
1948#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001949 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 validate_cheight();
1952 validate_virtcol();
1953 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001954 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001956 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958#ifdef FEAT_DIFF
1959 if (can_fill)
1960 {
1961 ++curwin->w_topfill;
1962 check_topfill(curwin, TRUE);
1963 }
1964 else
1965 {
1966 --curwin->w_topline;
1967 curwin->w_topfill = 0;
1968 }
1969#else
1970 --curwin->w_topline;
1971#endif
1972#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001973 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001975 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1977 }
1978}
1979
1980/*
1981 * Scroll the screen one line up, but don't do it if it would move the cursor
1982 * off the screen.
1983 */
1984 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001985scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 int start_row;
1988
1989 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1990#ifdef FEAT_DIFF
1991 && curwin->w_topfill == 0
1992#endif
1993 )
1994 return;
1995
Bram Moolenaar85a20022019-12-21 18:25:54 +01001996 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998 /*
1999 * Compute the row number of the first row of the cursor line
2000 * and make sure it doesn't go off the screen. Make sure the cursor
2001 * doesn't go before 'scrolloff' lines from the screen start.
2002 */
2003#ifdef FEAT_DIFF
2004 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
2005 - curwin->w_topfill;
2006#else
2007 start_row = curwin->w_wrow - plines(curwin->w_topline);
2008#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002009 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02002012 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01002014 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016#ifdef FEAT_DIFF
2017 if (curwin->w_topfill > 0)
2018 --curwin->w_topfill;
2019 else
2020#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002021 {
2022#ifdef FEAT_FOLDING
2023 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002026 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002027 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2029 }
2030}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032/*
2033 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
2034 * a (wrapped) text line. Uses and sets "lp->fill".
2035 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002036 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 */
2038 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002039topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040{
2041#ifdef FEAT_DIFF
2042 if (lp->fill < diff_check_fill(curwin, lp->lnum))
2043 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002044 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 ++lp->fill;
2046 lp->height = 1;
2047 }
2048 else
2049#endif
2050 {
2051 --lp->lnum;
2052#ifdef FEAT_DIFF
2053 lp->fill = 0;
2054#endif
2055 if (lp->lnum < 1)
2056 lp->height = MAXCOL;
2057 else
2058#ifdef FEAT_FOLDING
2059 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002060 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 lp->height = 1;
2062 else
2063#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002064 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066}
2067
2068/*
2069 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
2070 * a (wrapped) text line. Uses and sets "lp->fill".
2071 * Returns the height of the added line in "lp->height".
2072 * Lines below the last one are incredibly high.
2073 */
2074 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002075botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
2077#ifdef FEAT_DIFF
2078 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
2079 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002080 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 ++lp->fill;
2082 lp->height = 1;
2083 }
2084 else
2085#endif
2086 {
2087 ++lp->lnum;
2088#ifdef FEAT_DIFF
2089 lp->fill = 0;
2090#endif
2091 if (lp->lnum > curbuf->b_ml.ml_line_count)
2092 lp->height = MAXCOL;
2093 else
2094#ifdef FEAT_FOLDING
2095 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002096 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 lp->height = 1;
2098 else
2099#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002100 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102}
2103
2104#ifdef FEAT_DIFF
2105/*
2106 * Switch from including filler lines below lp->lnum to including filler
2107 * lines above loff.lnum + 1. This keeps pointing to the same line.
2108 * When there are no filler lines nothing changes.
2109 */
2110 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002111botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 if (lp->fill > 0)
2114 {
2115 ++lp->lnum;
2116 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2117 }
2118}
2119
2120/*
2121 * Switch from including filler lines above lp->lnum to including filler
2122 * lines below loff.lnum - 1. This keeps pointing to the same line.
2123 * When there are no filler lines nothing changes.
2124 */
2125 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002126topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 if (lp->fill > 0)
2129 {
2130 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
2131 --lp->lnum;
2132 }
2133}
2134#endif
2135
2136/*
2137 * Recompute topline to put the cursor at the top of the window.
2138 * Scroll at least "min_scroll" lines.
2139 * If "always" is TRUE, always set topline (for "zt").
2140 */
2141 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002142scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 int scrolled = 0;
2145 int extra = 0;
2146 int used;
2147 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002148 linenr_T top; // just above displayed lines
2149 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar46b54742022-10-06 15:46:49 +01002151 int old_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152#ifdef FEAT_DIFF
2153 linenr_T old_topfill = curwin->w_topfill;
2154#endif
2155 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002156 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (mouse_dragging > 0)
2159 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
2161 /*
2162 * Decrease topline until:
2163 * - it has become 1
2164 * - (part of) the cursor line is moved off the screen or
2165 * - moved at least 'scrolljump' lines and
2166 * - at least 'scrolloff' lines above and below the cursor
2167 */
2168 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002169 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (curwin->w_cursor.lnum < curwin->w_topline)
2171 scrolled = used;
2172
2173#ifdef FEAT_FOLDING
2174 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2175 {
2176 --top;
2177 ++bot;
2178 }
2179 else
2180#endif
2181 {
2182 top = curwin->w_cursor.lnum - 1;
2183 bot = curwin->w_cursor.lnum + 1;
2184 }
2185 new_topline = top + 1;
2186
2187#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002188 // "used" already contains the number of filler lines above, don't add it
2189 // again.
2190 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002191 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#endif
2193
2194 /*
2195 * Check if the lines from "top" to "bot" fit in the window. If they do,
2196 * set new_topline and advance "top" and "bot" to include more lines.
2197 */
2198 while (top > 0)
2199 {
2200#ifdef FEAT_FOLDING
2201 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002202 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 i = 1;
2204 else
2205#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002206 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 used += i;
2208 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2209 {
2210#ifdef FEAT_FOLDING
2211 if (hasFolding(bot, NULL, &bot))
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 ++used;
2214 else
2215#endif
2216 used += plines(bot);
2217 }
2218 if (used > curwin->w_height)
2219 break;
2220 if (top < curwin->w_topline)
2221 scrolled += i;
2222
2223 /*
2224 * If scrolling is needed, scroll at least 'sj' lines.
2225 */
2226 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2227 && extra >= off)
2228 break;
2229
2230 extra += i;
2231 new_topline = top;
2232 --top;
2233 ++bot;
2234 }
2235
2236 /*
2237 * If we don't have enough space, put cursor in the middle.
2238 * This makes sure we get the same position when using "k" and "j"
2239 * in a small window.
2240 */
2241 if (used > curwin->w_height)
2242 scroll_cursor_halfway(FALSE);
2243 else
2244 {
2245 /*
2246 * If "always" is FALSE, only adjust topline to a lower value, higher
2247 * value may happen with wrapping lines
2248 */
2249 if (new_topline < curwin->w_topline || always)
2250 curwin->w_topline = new_topline;
2251 if (curwin->w_topline > curwin->w_cursor.lnum)
2252 curwin->w_topline = curwin->w_cursor.lnum;
2253#ifdef FEAT_DIFF
2254 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2255 if (curwin->w_topfill > 0 && extra > off)
2256 {
2257 curwin->w_topfill -= extra - off;
2258 if (curwin->w_topfill < 0)
2259 curwin->w_topfill = 0;
2260 }
2261 check_topfill(curwin, FALSE);
2262#endif
Bram Moolenaar46b54742022-10-06 15:46:49 +01002263 // TODO: if the line doesn't fit may optimize w_skipcol
2264 if (curwin->w_topline == curwin->w_cursor.lnum)
2265 {
2266 curwin->w_skipcol = 0;
2267 redraw_later(UPD_NOT_VALID);
2268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (curwin->w_topline != old_topline
Bram Moolenaar46b54742022-10-06 15:46:49 +01002270 || curwin->w_skipcol != old_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#ifdef FEAT_DIFF
2272 || curwin->w_topfill != old_topfill
2273#endif
2274 )
2275 curwin->w_valid &=
2276 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2277 curwin->w_valid |= VALID_TOPLINE;
2278 }
2279}
2280
2281/*
2282 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2283 * screen lines for text lines.
2284 */
2285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002286set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288#ifdef FEAT_DIFF
2289 wp->w_filler_rows = 0;
2290#endif
2291 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002292 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 else
2294 {
2295 wp->w_empty_rows = wp->w_height - used;
2296#ifdef FEAT_DIFF
2297 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2298 {
2299 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2300 if (wp->w_empty_rows > wp->w_filler_rows)
2301 wp->w_empty_rows -= wp->w_filler_rows;
2302 else
2303 {
2304 wp->w_filler_rows = wp->w_empty_rows;
2305 wp->w_empty_rows = 0;
2306 }
2307 }
2308#endif
2309 }
2310}
2311
2312/*
2313 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002314 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2316 * This is messy stuff!!!
2317 */
2318 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002319scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
2321 int used;
2322 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002323 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 int extra = 0;
2325 int i;
2326 linenr_T line_count;
2327 linenr_T old_topline = curwin->w_topline;
2328 lineoff_T loff;
2329 lineoff_T boff;
2330#ifdef FEAT_DIFF
2331 int old_topfill = curwin->w_topfill;
2332 int fill_below_window;
2333#endif
2334 linenr_T old_botline = curwin->w_botline;
2335 linenr_T old_valid = curwin->w_valid;
2336 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002337 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002338 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340 cln = curwin->w_cursor.lnum;
2341 if (set_topbot)
2342 {
2343 used = 0;
2344 curwin->w_botline = cln + 1;
2345#ifdef FEAT_DIFF
2346 loff.fill = 0;
2347#endif
2348 for (curwin->w_topline = curwin->w_botline;
2349 curwin->w_topline > 1;
2350 curwin->w_topline = loff.lnum)
2351 {
2352 loff.lnum = curwin->w_topline;
2353 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002354 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 break;
2356 used += loff.height;
2357#ifdef FEAT_DIFF
2358 curwin->w_topfill = loff.fill;
2359#endif
2360 }
2361 set_empty_rows(curwin, used);
2362 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2363 if (curwin->w_topline != old_topline
2364#ifdef FEAT_DIFF
2365 || curwin->w_topfill != old_topfill
2366#endif
2367 )
2368 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2369 }
2370 else
2371 validate_botline();
2372
Bram Moolenaar85a20022019-12-21 18:25:54 +01002373 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374#ifdef FEAT_DIFF
2375 used = plines_nofill(cln);
2376#else
2377 validate_cheight();
2378 used = curwin->w_cline_height;
2379#endif
2380
Bram Moolenaar85a20022019-12-21 18:25:54 +01002381 // If the cursor is below botline, we will at least scroll by the height
2382 // of the cursor line. Correct for empty lines, which are really part of
2383 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (cln >= curwin->w_botline)
2385 {
2386 scrolled = used;
2387 if (cln == curwin->w_botline)
2388 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002389 min_scrolled = scrolled;
2390 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2391 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
Bram Moolenaar4d31b482022-10-06 16:03:09 +01002392 min_scrolled += PLINES_NOFILL(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394
2395 /*
2396 * Stop counting lines to scroll when
2397 * - hitting start of the file
2398 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002399 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * - lines between botline and cursor have been counted
2401 */
2402#ifdef FEAT_FOLDING
2403 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2404#endif
2405 {
2406 loff.lnum = cln;
2407 boff.lnum = cln;
2408 }
2409#ifdef FEAT_DIFF
2410 loff.fill = 0;
2411 boff.fill = 0;
2412 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2413 - curwin->w_filler_rows;
2414#endif
2415
2416 while (loff.lnum > 1)
2417 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002418 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2419 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002421 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2423 && loff.lnum <= curwin->w_botline
2424#ifdef FEAT_DIFF
2425 && (loff.lnum < curwin->w_botline
2426 || loff.fill >= fill_below_window)
2427#endif
2428 )
2429 break;
2430
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002433 if (loff.height == MAXCOL)
2434 used = MAXCOL;
2435 else
2436 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 if (used > curwin->w_height)
2438 break;
2439 if (loff.lnum >= curwin->w_botline
2440#ifdef FEAT_DIFF
2441 && (loff.lnum > curwin->w_botline
2442 || loff.fill <= fill_below_window)
2443#endif
2444 )
2445 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002446 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 scrolled += loff.height;
2448 if (loff.lnum == curwin->w_botline
2449#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002450 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#endif
2452 )
2453 scrolled -= curwin->w_empty_rows;
2454 }
2455
2456 if (boff.lnum < curbuf->b_ml.ml_line_count)
2457 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002458 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 botline_forw(&boff);
2460 used += boff.height;
2461 if (used > curwin->w_height)
2462 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002463 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2464 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 extra += boff.height;
2467 if (boff.lnum >= curwin->w_botline
2468#ifdef FEAT_DIFF
2469 || (boff.lnum + 1 == curwin->w_botline
2470 && boff.fill > curwin->w_filler_rows)
2471#endif
2472 )
2473 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002474 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 scrolled += boff.height;
2476 if (boff.lnum == curwin->w_botline
2477#ifdef FEAT_DIFF
2478 && boff.fill == 0
2479#endif
2480 )
2481 scrolled -= curwin->w_empty_rows;
2482 }
2483 }
2484 }
2485 }
2486
Bram Moolenaar85a20022019-12-21 18:25:54 +01002487 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (scrolled <= 0)
2489 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002490 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 else if (used > curwin->w_height)
2492 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002493 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 else
2495 {
2496 line_count = 0;
2497#ifdef FEAT_DIFF
2498 boff.fill = curwin->w_topfill;
2499#endif
2500 boff.lnum = curwin->w_topline - 1;
2501 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2502 {
2503 botline_forw(&boff);
2504 i += boff.height;
2505 ++line_count;
2506 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002507 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 line_count = 9999;
2509 }
2510
2511 /*
2512 * Scroll up if the cursor is off the bottom of the screen a bit.
2513 * Otherwise put it at 1/2 of the screen.
2514 */
2515 if (line_count >= curwin->w_height && line_count > min_scroll)
2516 scroll_cursor_halfway(FALSE);
2517 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002518 {
2519 // With 'smoothscroll' scroll at least the height of the cursor line.
2520 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled)
2521 line_count = min_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 /*
2526 * If topline didn't change we need to restore w_botline and w_empty_rows
2527 * (we changed them).
2528 * If topline did change, update_screen() will set botline.
2529 */
2530 if (curwin->w_topline == old_topline && set_topbot)
2531 {
2532 curwin->w_botline = old_botline;
2533 curwin->w_empty_rows = old_empty_rows;
2534 curwin->w_valid = old_valid;
2535 }
2536 curwin->w_valid |= VALID_TOPLINE;
2537}
2538
2539/*
2540 * Recompute topline to put the cursor halfway the window
2541 * If "atend" is TRUE, also put it halfway at the end of the file.
2542 */
2543 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002544scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 int above = 0;
2547 linenr_T topline;
2548#ifdef FEAT_DIFF
2549 int topfill = 0;
2550#endif
2551 int below = 0;
2552 int used;
2553 lineoff_T loff;
2554 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002555#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002556 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002559#ifdef FEAT_PROP_POPUP
2560 // if the width changed this needs to be updated first
2561 may_update_popup_position();
2562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2564#ifdef FEAT_FOLDING
2565 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2566#endif
2567#ifdef FEAT_DIFF
2568 used = plines_nofill(loff.lnum);
2569 loff.fill = 0;
2570 boff.fill = 0;
2571#else
2572 used = plines(loff.lnum);
2573#endif
2574 topline = loff.lnum;
2575 while (topline > 1)
2576 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002577 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 if (boff.lnum < curbuf->b_ml.ml_line_count)
2580 {
2581 botline_forw(&boff);
2582 used += boff.height;
2583 if (used > curwin->w_height)
2584 break;
2585 below += boff.height;
2586 }
2587 else
2588 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002589 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (atend)
2591 ++used;
2592 }
2593 }
2594
Bram Moolenaar85a20022019-12-21 18:25:54 +01002595 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002598 if (loff.height == MAXCOL)
2599 used = MAXCOL;
2600 else
2601 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (used > curwin->w_height)
2603 break;
2604 above += loff.height;
2605 topline = loff.lnum;
2606#ifdef FEAT_DIFF
2607 topfill = loff.fill;
2608#endif
2609 }
2610 }
2611#ifdef FEAT_FOLDING
2612 if (!hasFolding(topline, &curwin->w_topline, NULL))
2613#endif
2614 curwin->w_topline = topline;
2615#ifdef FEAT_DIFF
2616 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002617 if (old_topline > curwin->w_topline + curwin->w_height)
2618 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 check_topfill(curwin, FALSE);
2620#endif
2621 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2622 curwin->w_valid |= VALID_TOPLINE;
2623}
2624
2625/*
2626 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002627 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 * If not possible, put it at the same position as scroll_cursor_halfway().
2629 * When called topline must be valid!
2630 */
2631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002632cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002634 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002636 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 linenr_T botline;
2638 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002639 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002641 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643 /*
2644 * How many lines we would like to have above/below the cursor depends on
2645 * whether the first/last line of the file is on screen.
2646 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002647 above_wanted = so;
2648 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002649 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
2651 above_wanted = mouse_dragging - 1;
2652 below_wanted = mouse_dragging - 1;
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (curwin->w_topline == 1)
2655 {
2656 above_wanted = 0;
2657 max_off = curwin->w_height / 2;
2658 if (below_wanted > max_off)
2659 below_wanted = max_off;
2660 }
2661 validate_botline();
2662 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002663 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
2665 below_wanted = 0;
2666 max_off = (curwin->w_height - 1) / 2;
2667 if (above_wanted > max_off)
2668 above_wanted = max_off;
2669 }
2670
2671 /*
2672 * If there are sufficient file-lines above and below the cursor, we can
2673 * return now.
2674 */
2675 cln = curwin->w_cursor.lnum;
2676 if (cln >= curwin->w_topline + above_wanted
2677 && cln < curwin->w_botline - below_wanted
2678#ifdef FEAT_FOLDING
2679 && !hasAnyFolding(curwin)
2680#endif
2681 )
2682 return;
2683
2684 /*
2685 * Narrow down the area where the cursor can be put by taking lines from
2686 * the top and the bottom until:
2687 * - the desired context lines are found
2688 * - the lines from the top is past the lines from the bottom
2689 */
2690 topline = curwin->w_topline;
2691 botline = curwin->w_botline - 1;
2692#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002693 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 above = curwin->w_topfill;
2695 below = curwin->w_filler_rows;
2696#endif
2697 while ((above < above_wanted || below < below_wanted) && topline < botline)
2698 {
2699 if (below < below_wanted && (below <= above || above >= above_wanted))
2700 {
2701#ifdef FEAT_FOLDING
2702 if (hasFolding(botline, &botline, NULL))
2703 ++below;
2704 else
2705#endif
2706 below += plines(botline);
2707 --botline;
2708 }
2709 if (above < above_wanted && (above < below || below >= below_wanted))
2710 {
2711#ifdef FEAT_FOLDING
2712 if (hasFolding(topline, NULL, &topline))
2713 ++above;
2714 else
2715#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002716 above += PLINES_NOFILL(topline);
2717#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002718 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (topline < botline)
2720 above += diff_check_fill(curwin, topline + 1);
2721#endif
2722 ++topline;
2723 }
2724 }
2725 if (topline == botline || botline == 0)
2726 curwin->w_cursor.lnum = topline;
2727 else if (topline > botline)
2728 curwin->w_cursor.lnum = botline;
2729 else
2730 {
2731 if (cln < topline && curwin->w_topline > 1)
2732 {
2733 curwin->w_cursor.lnum = topline;
2734 curwin->w_valid &=
2735 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2736 }
2737 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2738 {
2739 curwin->w_cursor.lnum = botline;
2740 curwin->w_valid &=
2741 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2742 }
2743 }
2744 curwin->w_valid |= VALID_TOPLINE;
2745}
2746
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002747static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749/*
2750 * move screen 'count' pages up or down and update screen
2751 *
2752 * return FAIL for failure, OK otherwise
2753 */
2754 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002755onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 long n;
2758 int retval = OK;
2759 lineoff_T loff;
2760 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002761 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
Bram Moolenaar85a20022019-12-21 18:25:54 +01002763 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
2765 beep_flush();
2766 return FAIL;
2767 }
2768
2769 for ( ; count > 0; --count)
2770 {
2771 validate_botline();
2772 /*
2773 * It's an error to move a page up when the first line is already on
2774 * the screen. It's an error to move a page down when the last line
2775 * is on the screen and the topline is 'scrolloff' lines from the
2776 * last line.
2777 */
2778 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002779 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2781 : (curwin->w_topline == 1
2782#ifdef FEAT_DIFF
2783 && curwin->w_topfill ==
2784 diff_check_fill(curwin, curwin->w_topline)
2785#endif
2786 ))
2787 {
2788 beep_flush();
2789 retval = FAIL;
2790 break;
2791 }
2792
2793#ifdef FEAT_DIFF
2794 loff.fill = 0;
2795#endif
2796 if (dir == FORWARD)
2797 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002798 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002800 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002801 if (p_window <= 2)
2802 ++curwin->w_topline;
2803 else
2804 curwin->w_topline += p_window - 2;
2805 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2806 curwin->w_topline = curbuf->b_ml.ml_line_count;
2807 curwin->w_cursor.lnum = curwin->w_topline;
2808 }
2809 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2810 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002811 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 curwin->w_topline = curbuf->b_ml.ml_line_count;
2813#ifdef FEAT_DIFF
2814 curwin->w_topfill = 0;
2815#endif
2816 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2817 }
2818 else
2819 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002820 // For the overlap, start with the line just below the window
2821 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 loff.lnum = curwin->w_botline;
2823#ifdef FEAT_DIFF
2824 loff.fill = diff_check_fill(curwin, loff.lnum)
2825 - curwin->w_filler_rows;
2826#endif
2827 get_scroll_overlap(&loff, -1);
2828 curwin->w_topline = loff.lnum;
2829#ifdef FEAT_DIFF
2830 curwin->w_topfill = loff.fill;
2831 check_topfill(curwin, FALSE);
2832#endif
2833 curwin->w_cursor.lnum = curwin->w_topline;
2834 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2835 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2836 }
2837 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002838 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
2840#ifdef FEAT_DIFF
2841 if (curwin->w_topline == 1)
2842 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002843 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 max_topfill();
2845 continue;
2846 }
2847#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002848 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002849 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002850 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002851 if (p_window <= 2)
2852 --curwin->w_topline;
2853 else
2854 curwin->w_topline -= p_window - 2;
2855 if (curwin->w_topline < 1)
2856 curwin->w_topline = 1;
2857 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2858 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2859 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2860 continue;
2861 }
2862
Bram Moolenaar85a20022019-12-21 18:25:54 +01002863 // Find the line at the top of the window that is going to be the
2864 // line at the bottom of the window. Make sure this results in
2865 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 loff.lnum = curwin->w_topline - 1;
2867#ifdef FEAT_DIFF
2868 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2869 - curwin->w_topfill;
2870#endif
2871 get_scroll_overlap(&loff, 1);
2872
2873 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2874 {
2875 loff.lnum = curbuf->b_ml.ml_line_count;
2876#ifdef FEAT_DIFF
2877 loff.fill = 0;
2878 }
2879 else
2880 {
2881 botline_topline(&loff);
2882#endif
2883 }
2884 curwin->w_cursor.lnum = loff.lnum;
2885
Bram Moolenaar85a20022019-12-21 18:25:54 +01002886 // Find the line just above the new topline to get the right line
2887 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 n = 0;
2889 while (n <= curwin->w_height && loff.lnum >= 1)
2890 {
2891 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002892 if (loff.height == MAXCOL)
2893 n = MAXCOL;
2894 else
2895 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002897 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
2899 curwin->w_topline = 1;
2900#ifdef FEAT_DIFF
2901 max_topfill();
2902#endif
2903 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2904 }
2905 else
2906 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002907 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908#ifdef FEAT_DIFF
2909 topline_botline(&loff);
2910#endif
2911 botline_forw(&loff);
2912 botline_forw(&loff);
2913#ifdef FEAT_DIFF
2914 botline_topline(&loff);
2915#endif
2916#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002917 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2919#endif
2920
Bram Moolenaar85a20022019-12-21 18:25:54 +01002921 // Always scroll at least one line. Avoid getting stuck on
2922 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 if (loff.lnum >= curwin->w_topline
2924#ifdef FEAT_DIFF
2925 && (loff.lnum > curwin->w_topline
2926 || loff.fill >= curwin->w_topfill)
2927#endif
2928 )
2929 {
2930#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 // First try using the maximum number of filler lines. If
2932 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 loff.fill = curwin->w_topfill;
2934 if (curwin->w_topfill < diff_check_fill(curwin,
2935 curwin->w_topline))
2936 max_topfill();
2937 if (curwin->w_topfill == loff.fill)
2938#endif
2939 {
2940 --curwin->w_topline;
2941#ifdef FEAT_DIFF
2942 curwin->w_topfill = 0;
2943#endif
2944 }
2945 comp_botline(curwin);
2946 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002947 curwin->w_valid &=
2948 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950 else
2951 {
2952 curwin->w_topline = loff.lnum;
2953#ifdef FEAT_DIFF
2954 curwin->w_topfill = loff.fill;
2955 check_topfill(curwin, FALSE);
2956#endif
2957 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2958 }
2959 }
2960 }
2961 }
2962#ifdef FEAT_FOLDING
2963 foldAdjustCursor();
2964#endif
2965 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002966 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002967 if (retval == OK)
2968 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2970
Bram Moolenaar907dad72018-07-10 15:07:15 +02002971 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002973 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2974 // But make sure we scroll at least one line (happens with mix of long
2975 // wrapping lines and non-wrapping line).
2976 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002978 scroll_cursor_top(1, FALSE);
2979 if (curwin->w_topline <= old_topline
2980 && old_topline < curbuf->b_ml.ml_line_count)
2981 {
2982 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002984 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2985#endif
2986 }
2987 }
2988#ifdef FEAT_FOLDING
2989 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002994 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 return retval;
2996}
2997
2998/*
2999 * Decide how much overlap to use for page-up or page-down scrolling.
3000 * This is symmetric, so that doing both keeps the same lines displayed.
3001 * Three lines are examined:
3002 *
3003 * before CTRL-F after CTRL-F / before CTRL-B
3004 * etc. l1
3005 * l1 last but one line ------------
3006 * l2 last text line l2 top text line
3007 * ------------- l3 second text line
3008 * l3 etc.
3009 */
3010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003011get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 int h1, h2, h3, h4;
3014 int min_height = curwin->w_height - 2;
3015 lineoff_T loff0, loff1, loff2;
3016
3017#ifdef FEAT_DIFF
3018 if (lp->fill > 0)
3019 lp->height = 1;
3020 else
3021 lp->height = plines_nofill(lp->lnum);
3022#else
3023 lp->height = plines(lp->lnum);
3024#endif
3025 h1 = lp->height;
3026 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003027 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
3029 loff0 = *lp;
3030 if (dir > 0)
3031 botline_forw(lp);
3032 else
3033 topline_back(lp);
3034 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003035 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003037 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 return;
3039 }
3040
3041 loff1 = *lp;
3042 if (dir > 0)
3043 botline_forw(lp);
3044 else
3045 topline_back(lp);
3046 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003047 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003049 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 return;
3051 }
3052
3053 loff2 = *lp;
3054 if (dir > 0)
3055 botline_forw(lp);
3056 else
3057 topline_back(lp);
3058 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01003059 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003060 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003062 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063}
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065/*
3066 * Scroll 'scroll' lines up or down.
3067 */
3068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003069halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 long scrolled = 0;
3072 int i;
3073 int n;
3074 int room;
3075
3076 if (Prenum)
3077 curwin->w_p_scr = (Prenum > curwin->w_height) ?
3078 curwin->w_height : Prenum;
3079 n = (curwin->w_p_scr <= curwin->w_height) ?
3080 curwin->w_p_scr : curwin->w_height;
3081
Bram Moolenaard5d37532017-03-27 23:02:07 +02003082 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 validate_botline();
3084 room = curwin->w_empty_rows;
3085#ifdef FEAT_DIFF
3086 room += curwin->w_filler_rows;
3087#endif
3088 if (flag)
3089 {
3090 /*
3091 * scroll the text up
3092 */
3093 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
3094 {
3095#ifdef FEAT_DIFF
3096 if (curwin->w_topfill > 0)
3097 {
3098 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02003099 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 --curwin->w_topfill;
3101 }
3102 else
3103#endif
3104 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003105 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 n -= i;
3107 if (n < 0 && scrolled > 0)
3108 break;
3109#ifdef FEAT_FOLDING
3110 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
3111#endif
3112 ++curwin->w_topline;
3113#ifdef FEAT_DIFF
3114 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
3115#endif
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3118 {
3119 ++curwin->w_cursor.lnum;
3120 curwin->w_valid &=
3121 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
3124 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
3125 scrolled += i;
3126
3127 /*
3128 * Correct w_botline for changed w_topline.
3129 * Won't work when there are filler lines.
3130 */
3131#ifdef FEAT_DIFF
3132 if (curwin->w_p_diff)
3133 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
3134 else
3135#endif
3136 {
3137 room += i;
3138 do
3139 {
3140 i = plines(curwin->w_botline);
3141 if (i > room)
3142 break;
3143#ifdef FEAT_FOLDING
3144 (void)hasFolding(curwin->w_botline, NULL,
3145 &curwin->w_botline);
3146#endif
3147 ++curwin->w_botline;
3148 room -= i;
3149 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3150 }
3151 }
3152
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 /*
3154 * When hit bottom of the file: move cursor down.
3155 */
3156 if (n > 0)
3157 {
3158# ifdef FEAT_FOLDING
3159 if (hasAnyFolding(curwin))
3160 {
3161 while (--n >= 0
3162 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3163 {
3164 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3165 &curwin->w_cursor.lnum);
3166 ++curwin->w_cursor.lnum;
3167 }
3168 }
3169 else
3170# endif
3171 curwin->w_cursor.lnum += n;
3172 check_cursor_lnum();
3173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175 else
3176 {
3177 /*
3178 * scroll the text down
3179 */
3180 while (n > 0 && curwin->w_topline > 1)
3181 {
3182#ifdef FEAT_DIFF
3183 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3184 {
3185 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003186 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 ++curwin->w_topfill;
3188 }
3189 else
3190#endif
3191 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003192 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 n -= i;
3194 if (n < 0 && scrolled > 0)
3195 break;
3196 --curwin->w_topline;
3197#ifdef FEAT_FOLDING
3198 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3199#endif
3200#ifdef FEAT_DIFF
3201 curwin->w_topfill = 0;
3202#endif
3203 }
3204 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3205 VALID_BOTLINE|VALID_BOTLINE_AP);
3206 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (curwin->w_cursor.lnum > 1)
3208 {
3209 --curwin->w_cursor.lnum;
3210 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 /*
3215 * When hit top of the file: move cursor up.
3216 */
3217 if (n > 0)
3218 {
3219 if (curwin->w_cursor.lnum <= (linenr_T)n)
3220 curwin->w_cursor.lnum = 1;
3221 else
3222# ifdef FEAT_FOLDING
3223 if (hasAnyFolding(curwin))
3224 {
3225 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3226 {
3227 --curwin->w_cursor.lnum;
3228 (void)hasFolding(curwin->w_cursor.lnum,
3229 &curwin->w_cursor.lnum, NULL);
3230 }
3231 }
3232 else
3233# endif
3234 curwin->w_cursor.lnum -= n;
3235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 foldAdjustCursor();
3240# endif
3241#ifdef FEAT_DIFF
3242 check_topfill(curwin, !flag);
3243#endif
3244 cursor_correct();
3245 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003246 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003248
Bram Moolenaar860cae12010-06-05 23:22:07 +02003249 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003250do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003251{
3252 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003253 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003254 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003255 colnr_T curswant = curwin->w_curswant;
3256 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003257 win_T *old_curwin = curwin;
3258 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003259 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003260 int old_VIsual_select = VIsual_select;
3261 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003262
3263 /*
3264 * loop through the cursorbound windows
3265 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003266 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003267 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003268 {
3269 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003270 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003271 if (curwin != old_curwin && curwin->w_p_crb)
3272 {
3273# ifdef FEAT_DIFF
3274 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003275 curwin->w_cursor.lnum =
3276 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003277 else
3278# endif
3279 curwin->w_cursor.lnum = line;
3280 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003281 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003282 curwin->w_curswant = curswant;
3283 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003284
Bram Moolenaar85a20022019-12-21 18:25:54 +01003285 // Make sure the cursor is in a valid position. Temporarily set
3286 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003287 restart_edit_save = restart_edit;
3288 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003289 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003290
3291 // Avoid a scroll here for the cursor position, 'scrollbind' is
3292 // more important.
3293 if (!curwin->w_p_scb)
3294 validate_cursor();
3295
Bram Moolenaar61452852011-02-01 18:01:11 +01003296 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003297 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003298 if (has_mbyte)
3299 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003300 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003301
Bram Moolenaar85a20022019-12-21 18:25:54 +01003302 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003303 if (!curwin->w_p_scb)
3304 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003305 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003306 }
3307 }
3308
3309 /*
3310 * reset current-window
3311 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003312 VIsual_select = old_VIsual_select;
3313 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003314 curwin = old_curwin;
3315 curbuf = old_curbuf;
3316}