blob: 77f5cb92bc789c71d028525c72281a8be32678be [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 {
269 if (curwin->w_topline > 1)
270 {
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;
278 }
279#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +0100280 // Check if there are more filler lines than allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (!check_topline && curwin->w_topfill > diff_check_fill(curwin,
282 curwin->w_topline))
283 check_topline = TRUE;
284#endif
285
286 if (check_topline)
287 {
288 halfheight = curwin->w_height / 2 - 1;
289 if (halfheight < 2)
290 halfheight = 2;
291
292#ifdef FEAT_FOLDING
293 if (hasAnyFolding(curwin))
294 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100295 // Count the number of logical lines between the cursor and
296 // topline + scrolloff (approximation of how much will be
297 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 n = 0;
299 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100300 lnum < curwin->w_topline + *so_ptr; ++lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {
302 ++n;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100303 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 if (lnum >= curbuf->b_ml.ml_line_count || n >= halfheight)
305 break;
306 (void)hasFolding(lnum, NULL, &lnum);
307 }
308 }
309 else
310#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +0100311 n = curwin->w_topline + *so_ptr - curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
Bram Moolenaar85a20022019-12-21 18:25:54 +0100313 // If we weren't very close to begin with, we scroll to put the
314 // cursor in the middle of the window. Otherwise put the cursor
315 // near the top of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 if (n >= halfheight)
317 scroll_cursor_halfway(FALSE);
318 else
319 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000320 scroll_cursor_top(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 check_botline = TRUE;
322 }
323 }
324
325 else
326 {
327#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100328 // Make sure topline is the first line of a fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
330#endif
331 check_botline = TRUE;
332 }
333 }
334
335 /*
336 * If the cursor is below the bottom of the window, scroll the window
337 * to put the cursor on the window.
338 * When w_botline is invalid, recompute it first, to avoid a redraw later.
339 * If w_botline was approximated, we might need a redraw later in a few
340 * cases, but we don't want to spend (a lot of) time recomputing w_botline
341 * for every small change.
342 */
343 if (check_botline)
344 {
345 if (!(curwin->w_valid & VALID_BOTLINE_AP))
346 validate_botline();
347
348 if (curwin->w_botline <= curbuf->b_ml.ml_line_count)
349 {
Bram Moolenaard4153d42008-11-15 15:06:17 +0000350 if (curwin->w_cursor.lnum < curwin->w_botline)
351 {
352 if (((long)curwin->w_cursor.lnum
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100353 >= (long)curwin->w_botline - *so_ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef FEAT_FOLDING
355 || hasAnyFolding(curwin)
356#endif
357 ))
Bram Moolenaard4153d42008-11-15 15:06:17 +0000358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 lineoff_T loff;
360
Bram Moolenaar85a20022019-12-21 18:25:54 +0100361 // Cursor is (a few lines) above botline, check if there are
362 // 'scrolloff' window lines below the cursor. If not, need to
363 // scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 n = curwin->w_empty_rows;
365 loff.lnum = curwin->w_cursor.lnum;
366#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100367 // In a fold go to its last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 (void)hasFolding(loff.lnum, NULL, &loff.lnum);
369#endif
370#ifdef FEAT_DIFF
371 loff.fill = 0;
372 n += curwin->w_filler_rows;
373#endif
374 loff.height = 0;
375 while (loff.lnum < curwin->w_botline
376#ifdef FEAT_DIFF
377 && (loff.lnum + 1 < curwin->w_botline || loff.fill == 0)
378#endif
379 )
380 {
381 n += loff.height;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100382 if (n >= *so_ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 break;
384 botline_forw(&loff);
385 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100386 if (n >= *so_ptr)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100387 // sufficient context, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 check_botline = FALSE;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000389 }
390 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100391 // sufficient context, no need to scroll
Bram Moolenaard4153d42008-11-15 15:06:17 +0000392 check_botline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 }
394 if (check_botline)
395 {
396#ifdef FEAT_FOLDING
397 if (hasAnyFolding(curwin))
398 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100399 // Count the number of logical lines between the cursor and
400 // botline - scrolloff (approximation of how much will be
401 // scrolled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 line_count = 0;
403 for (lnum = curwin->w_cursor.lnum;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100404 lnum >= curwin->w_botline - *so_ptr; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {
406 ++line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100407 // stop at end of file or when we know we are far off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 if (lnum <= 0 || line_count > curwin->w_height + 1)
409 break;
410 (void)hasFolding(lnum, &lnum, NULL);
411 }
412 }
413 else
414#endif
415 line_count = curwin->w_cursor.lnum - curwin->w_botline
Bram Moolenaar8088ae92022-06-20 11:38:17 +0100416 + 1 + *so_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 if (line_count <= curwin->w_height + 1)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000418 scroll_cursor_bot(scrolljump_value(), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 else
420 scroll_cursor_halfway(FALSE);
421 }
422 }
423 }
424 curwin->w_valid |= VALID_TOPLINE;
425
426 /*
427 * Need to redraw when topline changed.
428 */
429 if (curwin->w_topline != old_topline
430#ifdef FEAT_DIFF
431 || curwin->w_topfill != old_topfill
432#endif
433 )
434 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +0100435 dollar_vcol = -1;
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000436 if (curwin->w_skipcol != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 {
438 curwin->w_skipcol = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100439 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 }
441 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100442 redraw_later(UPD_VALID);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100443 // May need to set w_skipcol when cursor in w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (curwin->w_cursor.lnum == curwin->w_topline)
445 validate_cursor();
446 }
447
Bram Moolenaar375e3392019-01-31 18:26:10 +0100448 *so_ptr = save_so;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449}
450
451/*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000452 * Return the scrolljump value to use for the current window.
453 * When 'scrolljump' is positive use it as-is.
454 * When 'scrolljump' is negative use it as a percentage of the window height.
455 */
456 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100457scrolljump_value(void)
Bram Moolenaar1e015462005-09-25 22:16:38 +0000458{
459 if (p_sj >= 0)
460 return (int)p_sj;
461 return (curwin->w_height * -p_sj) / 100;
462}
463
464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 * Return TRUE when there are not 'scrolloff' lines above the cursor for the
466 * current window.
467 */
468 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100469check_top_offset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 lineoff_T loff;
472 int n;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100473 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474
Bram Moolenaar375e3392019-01-31 18:26:10 +0100475 if (curwin->w_cursor.lnum < curwin->w_topline + so
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476#ifdef FEAT_FOLDING
477 || hasAnyFolding(curwin)
478#endif
479 )
480 {
481 loff.lnum = curwin->w_cursor.lnum;
482#ifdef FEAT_DIFF
483 loff.fill = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100484 n = curwin->w_topfill; // always have this context
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485#else
486 n = 0;
487#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100488 // Count the visible screen lines above the cursor line.
Bram Moolenaar375e3392019-01-31 18:26:10 +0100489 while (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {
491 topline_back(&loff);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100492 // Stop when included a line above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 if (loff.lnum < curwin->w_topline
494#ifdef FEAT_DIFF
495 || (loff.lnum == curwin->w_topline && loff.fill > 0)
496#endif
497 )
498 break;
499 n += loff.height;
500 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100501 if (n < so)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 return TRUE;
503 }
504 return FALSE;
505}
506
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100507/*
508 * Update w_curswant.
509 */
510 void
511update_curswant_force(void)
512{
513 validate_virtcol();
514 curwin->w_curswant = curwin->w_virtcol
515#ifdef FEAT_PROP_POPUP
516 - curwin->w_virtcol_first_char
517#endif
518 ;
519 curwin->w_set_curswant = FALSE;
520}
521
522/*
523 * Update w_curswant if w_set_curswant is set.
524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100526update_curswant(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 if (curwin->w_set_curswant)
Bram Moolenaare24b4ab2022-09-16 20:51:14 +0100529 update_curswant_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530}
531
532/*
533 * Check if the cursor has moved. Set the w_valid flag accordingly.
534 */
535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536check_cursor_moved(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum)
539 {
540 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
Bram Moolenaara91cb982022-05-08 19:39:31 +0100541 |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE
542 |VALID_BOTLINE|VALID_BOTLINE_AP);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 wp->w_valid_cursor = wp->w_cursor;
544 wp->w_valid_leftcol = wp->w_leftcol;
545 }
546 else if (wp->w_cursor.col != wp->w_valid_cursor.col
547 || wp->w_leftcol != wp->w_valid_leftcol
Bram Moolenaar29ddebe2019-01-26 17:28:26 +0100548 || wp->w_cursor.coladd != wp->w_valid_cursor.coladd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 {
550 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
551 wp->w_valid_cursor.col = wp->w_cursor.col;
552 wp->w_valid_leftcol = wp->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 wp->w_valid_cursor.coladd = wp->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555}
556
557/*
558 * Call this function when some window settings have changed, which require
559 * the cursor position, botline and topline to be recomputed and the window to
560 * be redrawn. E.g, when changing the 'wrap' option or folding.
561 */
562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100563changed_window_setting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565 changed_window_setting_win(curwin);
566}
567
568 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100569changed_window_setting_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 wp->w_lines_valid = 0;
572 changed_line_abv_curs_win(wp);
573 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP|VALID_TOPLINE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100574 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575}
576
577/*
578 * Set wp->w_topline to a certain number.
579 */
580 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100581set_topline(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
Bram Moolenaar04626c22021-09-01 16:02:07 +0200583#ifdef FEAT_DIFF
584 linenr_T prev_topline = wp->w_topline;
585#endif
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100588 // go to first of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
590#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100591 // Approximate the value of w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 wp->w_botline += lnum - wp->w_topline;
Bram Moolenaar23999d72020-12-23 14:36:00 +0100593 if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1)
594 wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 wp->w_topline = lnum;
Bram Moolenaard4153d42008-11-15 15:06:17 +0000596 wp->w_topline_was_set = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597#ifdef FEAT_DIFF
Bram Moolenaar04626c22021-09-01 16:02:07 +0200598 if (lnum != prev_topline)
599 // Keep the filler lines when the topline didn't change.
600 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#endif
602 wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_TOPLINE);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100603 // Don't set VALID_TOPLINE here, 'scrolloff' needs to be checked.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100604 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605}
606
607/*
608 * Call this function when the length of the cursor line (in screen
609 * characters) has changed, and the change is before the cursor.
610 * Need to take care of w_botline separately!
611 */
612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100613changed_cline_bef_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
616 |VALID_CHEIGHT|VALID_TOPLINE);
617}
618
619 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100620changed_cline_bef_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
623 |VALID_CHEIGHT|VALID_TOPLINE);
624}
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626/*
627 * Call this function when the length of a line (in screen characters) above
628 * the cursor have changed.
629 * Need to take care of w_botline separately!
630 */
631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100632changed_line_abv_curs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
635 |VALID_CHEIGHT|VALID_TOPLINE);
636}
637
638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100639changed_line_abv_curs_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL|VALID_CROW
642 |VALID_CHEIGHT|VALID_TOPLINE);
643}
644
645/*
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100646 * Display of line has changed for "buf", invalidate cursor position and
647 * w_botline.
648 */
649 void
650changed_line_display_buf(buf_T *buf)
651{
652 win_T *wp;
653
654 FOR_ALL_WINDOWS(wp)
655 if (wp->w_buffer == buf)
656 wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL
657 |VALID_CROW|VALID_CHEIGHT
658 |VALID_TOPLINE|VALID_BOTLINE|VALID_BOTLINE_AP);
659}
660
661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 * Make sure the value of curwin->w_botline is valid.
663 */
664 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100665validate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
Bram Moolenaar23999d72020-12-23 14:36:00 +0100667 validate_botline_win(curwin);
668}
669
670/*
671 * Make sure the value of wp->w_botline is valid.
672 */
673 void
674validate_botline_win(win_T *wp)
675{
676 if (!(wp->w_valid & VALID_BOTLINE))
677 comp_botline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678}
679
680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 * Mark curwin->w_botline as invalid (because of some change in the buffer).
682 */
683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100684invalidate_botline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
687}
688
689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100690invalidate_botline_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
692 wp->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
693}
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100696approximate_botline_win(
697 win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
699 wp->w_valid &= ~VALID_BOTLINE;
700}
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702/*
703 * Return TRUE if curwin->w_wrow and curwin->w_wcol are valid.
704 */
705 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100706cursor_valid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707{
708 check_cursor_moved(curwin);
709 return ((curwin->w_valid & (VALID_WROW|VALID_WCOL)) ==
710 (VALID_WROW|VALID_WCOL));
711}
712
713/*
714 * Validate cursor position. Makes sure w_wrow and w_wcol are valid.
715 * w_topline must be valid, you may need to call update_topline() first!
716 */
717 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100718validate_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
Bram Moolenaard4566c12022-09-24 21:06:39 +0100720 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 check_cursor_moved(curwin);
722 if ((curwin->w_valid & (VALID_WCOL|VALID_WROW)) != (VALID_WCOL|VALID_WROW))
723 curs_columns(TRUE);
724}
725
726#if defined(FEAT_GUI) || defined(PROTO)
727/*
728 * validate w_cline_row.
729 */
730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731validate_cline_row(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733 /*
734 * First make sure that w_topline is valid (after moving the cursor).
735 */
736 update_topline();
737 check_cursor_moved(curwin);
738 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +0100739 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740}
741#endif
742
743/*
744 * Compute wp->w_cline_row and wp->w_cline_height, based on the current value
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200745 * of wp->w_topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 */
747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100748curs_rows(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
750 linenr_T lnum;
751 int i;
752 int all_invalid;
753 int valid;
754#ifdef FEAT_FOLDING
755 long fold_count;
756#endif
757
Bram Moolenaar85a20022019-12-21 18:25:54 +0100758 // Check if wp->w_lines[].wl_size is invalid
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 all_invalid = (!redrawing()
760 || wp->w_lines_valid == 0
761 || wp->w_lines[0].wl_lnum > wp->w_topline);
762 i = 0;
763 wp->w_cline_row = 0;
764 for (lnum = wp->w_topline; lnum < wp->w_cursor.lnum; ++i)
765 {
766 valid = FALSE;
767 if (!all_invalid && i < wp->w_lines_valid)
768 {
769 if (wp->w_lines[i].wl_lnum < lnum || !wp->w_lines[i].wl_valid)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100770 continue; // skip changed or deleted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (wp->w_lines[i].wl_lnum == lnum)
772 {
773#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100774 // Check for newly inserted lines below this row, in which
775 // case we need to check for folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (!wp->w_buffer->b_mod_set
777 || wp->w_lines[i].wl_lastlnum < wp->w_cursor.lnum
778 || wp->w_buffer->b_mod_top
779 > wp->w_lines[i].wl_lastlnum + 1)
780#endif
781 valid = TRUE;
782 }
783 else if (wp->w_lines[i].wl_lnum > lnum)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100784 --i; // hold at inserted lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 if (valid
787#ifdef FEAT_DIFF
788 && (lnum != wp->w_topline || !wp->w_p_diff)
789#endif
790 )
791 {
792#ifdef FEAT_FOLDING
793 lnum = wp->w_lines[i].wl_lastlnum + 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100794 // Cursor inside folded lines, don't count this row
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 if (lnum > wp->w_cursor.lnum)
796 break;
797#else
798 ++lnum;
799#endif
800 wp->w_cline_row += wp->w_lines[i].wl_size;
801 }
802 else
803 {
804#ifdef FEAT_FOLDING
805 fold_count = foldedCount(wp, lnum, NULL);
806 if (fold_count)
807 {
808 lnum += fold_count;
809 if (lnum > wp->w_cursor.lnum)
810 break;
811 ++wp->w_cline_row;
812 }
813 else
814#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100815 {
816 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#ifdef FEAT_DIFF
818 if (lnum == wp->w_topline)
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100819 n = plines_win_nofill(wp, lnum, TRUE) + wp->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 else
821#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +0100822 n = plines_win(wp, lnum, TRUE);
823 if (lnum++ == wp->w_topline)
824 n = adjust_plines_for_skipcol(wp, n);
825 wp->w_cline_row += n;
826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
828 }
829
830 check_cursor_moved(wp);
831 if (!(wp->w_valid & VALID_CHEIGHT))
832 {
833 if (all_invalid
834 || i == wp->w_lines_valid
835 || (i < wp->w_lines_valid
836 && (!wp->w_lines[i].wl_valid
837 || wp->w_lines[i].wl_lnum != wp->w_cursor.lnum)))
838 {
839#ifdef FEAT_DIFF
840 if (wp->w_cursor.lnum == wp->w_topline)
841 wp->w_cline_height = plines_win_nofill(wp, wp->w_cursor.lnum,
842 TRUE) + wp->w_topfill;
843 else
844#endif
845 wp->w_cline_height = plines_win(wp, wp->w_cursor.lnum, TRUE);
846#ifdef FEAT_FOLDING
847 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
848 NULL, NULL, TRUE, NULL);
849#endif
850 }
851 else if (i > wp->w_lines_valid)
852 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100853 // a line that is too long to fit on the last screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 wp->w_cline_height = 0;
855#ifdef FEAT_FOLDING
856 wp->w_cline_folded = hasFoldingWin(wp, wp->w_cursor.lnum,
857 NULL, NULL, TRUE, NULL);
858#endif
859 }
860 else
861 {
862 wp->w_cline_height = wp->w_lines[i].wl_size;
863#ifdef FEAT_FOLDING
864 wp->w_cline_folded = wp->w_lines[i].wl_folded;
865#endif
866 }
867 }
868
Bram Moolenaar3d6db142014-03-28 21:49:32 +0100869 redraw_for_cursorline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 wp->w_valid |= VALID_CROW|VALID_CHEIGHT;
871
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872}
873
874/*
875 * Validate curwin->w_virtcol only.
876 */
877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100878validate_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
880 validate_virtcol_win(curwin);
881}
882
883/*
884 * Validate wp->w_virtcol only.
885 */
886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100887validate_virtcol_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
889 check_cursor_moved(wp);
890 if (!(wp->w_valid & VALID_VIRTCOL))
891 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100892#ifdef FEAT_PROP_POPUP
893 wp->w_virtcol_first_char = 0;
894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000896#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +0100897 redraw_for_cursorcolumn(wp);
Bram Moolenaar2b48ad52006-03-12 21:56:11 +0000898#endif
zeertzjq3e559cd2022-03-27 19:26:55 +0100899 wp->w_valid |= VALID_VIRTCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
901}
902
903/*
904 * Validate curwin->w_cline_height only.
905 */
Bram Moolenaar09dd2bb2019-12-14 18:42:15 +0100906 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100907validate_cheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
909 check_cursor_moved(curwin);
910 if (!(curwin->w_valid & VALID_CHEIGHT))
911 {
912#ifdef FEAT_DIFF
913 if (curwin->w_cursor.lnum == curwin->w_topline)
914 curwin->w_cline_height = plines_nofill(curwin->w_cursor.lnum)
915 + curwin->w_topfill;
916 else
917#endif
918 curwin->w_cline_height = plines(curwin->w_cursor.lnum);
919#ifdef FEAT_FOLDING
920 curwin->w_cline_folded = hasFolding(curwin->w_cursor.lnum, NULL, NULL);
921#endif
922 curwin->w_valid |= VALID_CHEIGHT;
923 }
924}
925
926/*
Bram Moolenaarc236c162008-07-13 17:41:49 +0000927 * Validate w_wcol and w_virtcol only.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 */
929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100930validate_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 colnr_T off;
933 colnr_T col;
Bram Moolenaar6427c602010-02-03 17:43:07 +0100934 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
936 validate_virtcol();
937 if (!(curwin->w_valid & VALID_WCOL))
938 {
939 col = curwin->w_virtcol;
940 off = curwin_col_off();
941 col += off;
Bram Moolenaar02631462017-09-22 15:20:32 +0200942 width = curwin->w_width - off + curwin_col_off2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
Bram Moolenaar85a20022019-12-21 18:25:54 +0100944 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaarc236c162008-07-13 17:41:49 +0000945 if (curwin->w_p_wrap
Bram Moolenaar02631462017-09-22 15:20:32 +0200946 && col >= (colnr_T)curwin->w_width
Bram Moolenaar6427c602010-02-03 17:43:07 +0100947 && width > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100948 // use same formula as what is used in curs_columns()
Bram Moolenaar02631462017-09-22 15:20:32 +0200949 col -= ((col - curwin->w_width) / width + 1) * width;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000950 if (col > (int)curwin->w_leftcol)
951 col -= curwin->w_leftcol;
952 else
953 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 curwin->w_wcol = col;
Bram Moolenaarc236c162008-07-13 17:41:49 +0000955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 curwin->w_valid |= VALID_WCOL;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100957#ifdef FEAT_PROP_POPUP
Bram Moolenaar6a076442020-11-15 20:32:58 +0100958 curwin->w_flags &= ~WFLAG_WCOL_OFF_ADDED;
Bram Moolenaar4792a672020-11-15 21:11:18 +0100959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
961}
962
963/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200964 * Compute offset of a window, occupied by absolute or relative line number,
965 * fold column and sign column (these don't move when scrolling horizontally).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 */
967 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100968win_col_off(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Bram Moolenaar64486672010-05-16 15:46:46 +0200970 return (((wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) + 1 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 + (cmdwin_type == 0 || wp != curwin ? 0 : 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972#ifdef FEAT_FOLDING
973 + wp->w_p_fdc
974#endif
975#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +0200976 + (signcolumn_on(wp) ? 2 : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#endif
978 );
979}
980
981 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100982curwin_col_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 return win_col_off(curwin);
985}
986
987/*
988 * Return the difference in column offset for the second screen line of a
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +0100989 * wrapped line. It's positive if 'number' or 'relativenumber' is on and 'n'
990 * is in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 */
992 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100993win_col_off2(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
Bram Moolenaar64486672010-05-16 15:46:46 +0200995 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) != NULL)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000996 return number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 return 0;
998}
999
1000 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001001curwin_col_off2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 return win_col_off2(curwin);
1004}
1005
1006/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001007 * Compute curwin->w_wcol and curwin->w_virtcol.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 * Also updates curwin->w_wrow and curwin->w_cline_row.
1009 * Also updates curwin->w_leftcol.
1010 */
1011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001012curs_columns(
Bram Moolenaar85a20022019-12-21 18:25:54 +01001013 int may_scroll) // when TRUE, may scroll horizontally
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014{
1015 int diff;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001016 int extra; // offset for first screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 int off_left, off_right;
1018 int n;
1019 int p_lines;
1020 int width = 0;
1021 int textwidth;
1022 int new_leftcol;
1023 colnr_T startcol;
1024 colnr_T endcol;
1025 colnr_T prev_skipcol;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001026 long so = get_scrolloff_value();
1027 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 /*
1030 * First make sure that w_topline is valid (after moving the cursor).
1031 */
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001032 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 /*
1035 * Next make sure that w_cline_row is valid.
1036 */
1037 if (!(curwin->w_valid & VALID_CROW))
Bram Moolenaar3f9be972014-12-13 21:09:57 +01001038 curs_rows(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01001040#ifdef FEAT_PROP_POPUP
1041 // will be set by getvvcol() but not reset
1042 curwin->w_virtcol_first_char = 0;
1043#endif
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 /*
1046 * Compute the number of virtual columns.
1047 */
1048#ifdef FEAT_FOLDING
1049 if (curwin->w_cline_folded)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001050 // In a folded line the cursor is always in the first column
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 startcol = curwin->w_virtcol = endcol = curwin->w_leftcol;
1052 else
1053#endif
1054 getvvcol(curwin, &curwin->w_cursor,
1055 &startcol, &(curwin->w_virtcol), &endcol);
1056
Bram Moolenaar85a20022019-12-21 18:25:54 +01001057 // remove '$' from change command when cursor moves onto it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (startcol > dollar_vcol)
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001059 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
1061 extra = curwin_col_off();
1062 curwin->w_wcol = curwin->w_virtcol + extra;
1063 endcol += extra;
1064
1065 /*
1066 * Now compute w_wrow, counting screen lines from w_cline_row.
1067 */
1068 curwin->w_wrow = curwin->w_cline_row;
1069
Bram Moolenaar02631462017-09-22 15:20:32 +02001070 textwidth = curwin->w_width - extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (textwidth <= 0)
1072 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001073 // No room for text, put cursor in last char of window.
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001074 // If not wrapping, the last non-empty line.
Bram Moolenaar02631462017-09-22 15:20:32 +02001075 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar30441bb2021-07-08 13:19:31 +02001076 if (curwin->w_p_wrap)
1077 curwin->w_wrow = curwin->w_height - 1;
1078 else
1079 curwin->w_wrow = curwin->w_height - 1 - curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02001081 else if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
1083 width = textwidth + curwin_col_off2();
1084
Bram Moolenaar85a20022019-12-21 18:25:54 +01001085 // long line wrapping, adjust curwin->w_wrow
Bram Moolenaar02631462017-09-22 15:20:32 +02001086 if (curwin->w_wcol >= curwin->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001088#ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01001089 char_u *sbr;
Bram Moolenaar439b3ac2019-11-10 01:32:12 +01001090#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01001091
Bram Moolenaar85a20022019-12-21 18:25:54 +01001092 // this same formula is used in validate_cursor_col()
Bram Moolenaar02631462017-09-22 15:20:32 +02001093 n = (curwin->w_wcol - curwin->w_width) / width + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 curwin->w_wcol -= n * width;
1095 curwin->w_wrow += n;
1096
1097#ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +01001098 // When cursor wraps to first char of next line in Insert
1099 // mode, the 'showbreak' string isn't shown, backup to first
1100 // column
Bram Moolenaaree857022019-11-09 23:26:40 +01001101 sbr = get_showbreak_value(curwin);
1102 if (*sbr && *ml_get_cursor() == NUL
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001103 && curwin->w_wcol == vim_strsize(sbr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 curwin->w_wcol = 0;
1105#endif
1106 }
1107 }
1108
Bram Moolenaar85a20022019-12-21 18:25:54 +01001109 // No line wrapping: compute curwin->w_leftcol if scrolling is on and line
1110 // is not folded.
1111 // If scrolling is off, curwin->w_leftcol is assumed to be 0
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001112 else if (may_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef FEAT_FOLDING
1114 && !curwin->w_cline_folded
1115#endif
1116 )
1117 {
Bram Moolenaarc9dc03f2022-09-12 17:51:07 +01001118#ifdef FEAT_PROP_POPUP
1119 if (curwin->w_virtcol_first_char > 0)
1120 {
1121 int cols = (curwin->w_width - extra);
1122 int rows = cols > 0 ? curwin->w_virtcol_first_char / cols : 1;
1123
1124 // each "above" text prop shifts the text one row down
1125 curwin->w_wrow += rows;
1126 curwin->w_wcol -= rows * cols;
1127 endcol -= rows * cols;
1128 curwin->w_cline_height = rows + 1;
1129 }
1130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 /*
1132 * If Cursor is left of the screen, scroll rightwards.
1133 * If Cursor is right of the screen, scroll leftwards
1134 * If we get closer to the edge than 'sidescrolloff', scroll a little
1135 * extra
1136 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01001137 off_left = (int)startcol - (int)curwin->w_leftcol - siso;
Bram Moolenaar02631462017-09-22 15:20:32 +02001138 off_right = (int)endcol - (int)(curwin->w_leftcol + curwin->w_width
Bram Moolenaar375e3392019-01-31 18:26:10 +01001139 - siso) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (off_left < 0 || off_right > 0)
1141 {
1142 if (off_left < 0)
1143 diff = -off_left;
1144 else
1145 diff = off_right;
1146
Bram Moolenaar85a20022019-12-21 18:25:54 +01001147 // When far off or not enough room on either side, put cursor in
1148 // middle of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (p_ss == 0 || diff >= textwidth / 2 || off_right >= off_left)
1150 new_leftcol = curwin->w_wcol - extra - textwidth / 2;
1151 else
1152 {
1153 if (diff < p_ss)
1154 diff = p_ss;
1155 if (off_left < 0)
1156 new_leftcol = curwin->w_leftcol - diff;
1157 else
1158 new_leftcol = curwin->w_leftcol + diff;
1159 }
1160 if (new_leftcol < 0)
1161 new_leftcol = 0;
1162 if (new_leftcol != (int)curwin->w_leftcol)
1163 {
1164 curwin->w_leftcol = new_leftcol;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001165 // screen has to be redrawn with new curwin->w_leftcol
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001166 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 }
1168 }
1169 curwin->w_wcol -= curwin->w_leftcol;
1170 }
1171 else if (curwin->w_wcol > (int)curwin->w_leftcol)
1172 curwin->w_wcol -= curwin->w_leftcol;
1173 else
1174 curwin->w_wcol = 0;
1175
1176#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01001177 // Skip over filler lines. At the top use w_topfill, there
1178 // may be some filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (curwin->w_cursor.lnum == curwin->w_topline)
1180 curwin->w_wrow += curwin->w_topfill;
1181 else
1182 curwin->w_wrow += diff_check_fill(curwin, curwin->w_cursor.lnum);
1183#endif
1184
1185 prev_skipcol = curwin->w_skipcol;
1186
1187 p_lines = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if ((curwin->w_wrow >= curwin->w_height
1190 || ((prev_skipcol > 0
Bram Moolenaar375e3392019-01-31 18:26:10 +01001191 || curwin->w_wrow + so >= curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && (p_lines =
1193#ifdef FEAT_DIFF
1194 plines_win_nofill
1195#else
1196 plines_win
1197#endif
1198 (curwin, curwin->w_cursor.lnum, FALSE))
1199 - 1 >= curwin->w_height))
1200 && curwin->w_height != 0
1201 && curwin->w_cursor.lnum == curwin->w_topline
1202 && width > 0
Bram Moolenaar4033c552017-09-16 20:54:51 +02001203 && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001205 // Cursor past end of screen. Happens with a single line that does
1206 // not fit on screen. Find a skipcol to show the text around the
1207 // cursor. Avoid scrolling all the time. compute value of "extra":
1208 // 1: Less than 'scrolloff' lines above
1209 // 2: Less than 'scrolloff' lines below
1210 // 3: both of them
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 extra = 0;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001212 if (curwin->w_skipcol + so * width > curwin->w_virtcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 extra = 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001214 // Compute last display line of the buffer line that we want at the
1215 // bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (p_lines == 0)
1217 p_lines = plines_win(curwin, curwin->w_cursor.lnum, FALSE);
1218 --p_lines;
Bram Moolenaar375e3392019-01-31 18:26:10 +01001219 if (p_lines > curwin->w_wrow + so)
1220 n = curwin->w_wrow + so;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 else
1222 n = p_lines;
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001223 if ((colnr_T)n >= curwin->w_height + curwin->w_skipcol / width - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 extra += 2;
1225
Bram Moolenaar8f33ebf2021-02-10 21:10:12 +01001226 if (extra == 3 || p_lines <= so * 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001228 // not enough room for 'scrolloff', put cursor in the middle
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 n = curwin->w_virtcol / width;
1230 if (n > curwin->w_height / 2)
1231 n -= curwin->w_height / 2;
1232 else
1233 n = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001234 // don't skip more than necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (n > p_lines - curwin->w_height + 1)
1236 n = p_lines - curwin->w_height + 1;
1237 curwin->w_skipcol = n * width;
1238 }
1239 else if (extra == 1)
1240 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001241 // less than 'scrolloff' lines above, decrease skipcol
Bram Moolenaar375e3392019-01-31 18:26:10 +01001242 extra = (curwin->w_skipcol + so * width - curwin->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 + width - 1) / width;
1244 if (extra > 0)
1245 {
1246 if ((colnr_T)(extra * width) > curwin->w_skipcol)
1247 extra = curwin->w_skipcol / width;
1248 curwin->w_skipcol -= extra * width;
1249 }
1250 }
1251 else if (extra == 2)
1252 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001253 // less than 'scrolloff' lines below, increase skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 endcol = (n - curwin->w_height + 1) * width;
1255 while (endcol > curwin->w_virtcol)
1256 endcol -= width;
1257 if (endcol > curwin->w_skipcol)
1258 curwin->w_skipcol = endcol;
1259 }
1260
1261 curwin->w_wrow -= curwin->w_skipcol / width;
1262 if (curwin->w_wrow >= curwin->w_height)
1263 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001264 // small window, make sure cursor is in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 extra = curwin->w_wrow - curwin->w_height + 1;
1266 curwin->w_skipcol += extra * width;
1267 curwin->w_wrow -= extra;
1268 }
1269
1270 extra = ((int)prev_skipcol - (int)curwin->w_skipcol) / width;
1271 if (extra > 0)
1272 win_ins_lines(curwin, 0, extra, FALSE, FALSE);
1273 else if (extra < 0)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001274 win_del_lines(curwin, 0, -extra, FALSE, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001276 else if (!curwin->w_p_sms)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 curwin->w_skipcol = 0;
1278 if (prev_skipcol != curwin->w_skipcol)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001279 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001281#ifdef FEAT_SYN_HL
zeertzjq3e559cd2022-03-27 19:26:55 +01001282 redraw_for_cursorcolumn(curwin);
Bram Moolenaarb6798752014-03-27 12:11:48 +01001283#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001284#if defined(FEAT_PROP_POPUP) && defined(FEAT_TERMINAL)
1285 if (popup_is_popup(curwin) && curbuf->b_term != NULL)
1286 {
1287 curwin->w_wrow += popup_top_extra(curwin);
1288 curwin->w_wcol += popup_left_extra(curwin);
Bram Moolenaar6a076442020-11-15 20:32:58 +01001289 curwin->w_flags |= WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001290 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01001291 else
1292 curwin->w_flags &= ~(WFLAG_WCOL_OFF_ADDED + WFLAG_WROW_OFF_ADDED);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001293#endif
Bram Moolenaar2b48ad52006-03-12 21:56:11 +00001294
Bram Moolenaar08f23632019-11-16 14:19:33 +01001295 // now w_leftcol is valid, avoid check_cursor_moved() thinking otherwise
1296 curwin->w_valid_leftcol = curwin->w_leftcol;
1297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 curwin->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
1299}
1300
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001301#if (defined(FEAT_EVAL) || defined(FEAT_PROP_POPUP)) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001302/*
1303 * Compute the screen position of text character at "pos" in window "wp"
1304 * The resulting values are one-based, zero when character is not visible.
1305 */
Bram Moolenaar12034e22019-08-25 22:25:02 +02001306 void
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001307textpos2screenpos(
1308 win_T *wp,
1309 pos_T *pos,
1310 int *rowp, // screen row
1311 int *scolp, // start screen column
1312 int *ccolp, // cursor screen column
1313 int *ecolp) // end screen column
1314{
1315 colnr_T scol = 0, ccol = 0, ecol = 0;
1316 int row = 0;
1317 int rowoff = 0;
1318 colnr_T coloff = 0;
1319
Bram Moolenaar189663b2021-07-21 18:04:56 +02001320 if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001321 {
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001322 colnr_T off;
1323 colnr_T col;
1324 int width;
1325 linenr_T lnum = pos->lnum;
1326#ifdef FEAT_FOLDING
1327 int is_folded;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001328
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001329 is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
1330#endif
1331 row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1;
1332#ifdef FEAT_FOLDING
1333 if (is_folded)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001334 {
Bram Moolenaar7924a172022-01-24 16:15:15 +00001335 row += W_WINROW(wp);
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001336 coloff = wp->w_wincol + 1;
Bram Moolenaar7924a172022-01-24 16:15:15 +00001337 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001338 else
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00001339#endif
1340 {
1341 getvcol(wp, pos, &scol, &ccol, &ecol);
1342
1343 // similar to what is done in validate_cursor_col()
1344 col = scol;
1345 off = win_col_off(wp);
1346 col += off;
1347 width = wp->w_width - off + win_col_off2(wp);
1348
1349 // long line wrapping, adjust row
1350 if (wp->w_p_wrap
1351 && col >= (colnr_T)wp->w_width
1352 && width > 0)
1353 {
1354 // use same formula as what is used in curs_columns()
1355 rowoff = ((col - wp->w_width) / width + 1);
1356 col -= rowoff * width;
1357 }
1358 col -= wp->w_leftcol;
1359 if (col >= wp->w_width)
1360 col = -1;
1361 if (col >= 0 && row + rowoff <= wp->w_height)
1362 {
1363 coloff = col - scol + wp->w_wincol + 1;
1364 row += W_WINROW(wp);
1365 }
1366 else
1367 // character is left, right or below of the window
1368 row = rowoff = scol = ccol = ecol = 0;
1369 }
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001370 }
Bram Moolenaar7924a172022-01-24 16:15:15 +00001371 *rowp = row + rowoff;
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001372 *scolp = scol + coloff;
1373 *ccolp = ccol + coloff;
1374 *ecolp = ecol + coloff;
1375}
Bram Moolenaar12034e22019-08-25 22:25:02 +02001376#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001377
Bram Moolenaar12034e22019-08-25 22:25:02 +02001378#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001379/*
1380 * "screenpos({winid}, {lnum}, {col})" function
1381 */
1382 void
1383f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
1384{
1385 dict_T *dict;
1386 win_T *wp;
1387 pos_T pos;
1388 int row = 0;
1389 int scol = 0, ccol = 0, ecol = 0;
1390
Bram Moolenaar93a10962022-06-16 11:42:09 +01001391 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001392 return;
1393 dict = rettv->vval.v_dict;
1394
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001395 if (in_vim9script()
1396 && (check_for_number_arg(argvars, 0) == FAIL
1397 || check_for_number_arg(argvars, 1) == FAIL
1398 || check_for_number_arg(argvars, 2) == FAIL))
1399 return;
1400
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001401 wp = find_win_by_nr_or_id(&argvars[0]);
1402 if (wp == NULL)
1403 return;
1404
1405 pos.lnum = tv_get_number(&argvars[1]);
1406 pos.col = tv_get_number(&argvars[2]) - 1;
1407 pos.coladd = 0;
1408 textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol);
1409
1410 dict_add_number(dict, "row", row);
1411 dict_add_number(dict, "col", scol);
1412 dict_add_number(dict, "curscol", ccol);
1413 dict_add_number(dict, "endcol", ecol);
1414}
Bram Moolenaar5a6ec102022-05-27 21:58:00 +01001415
1416/*
1417 * "virtcol2col({winid}, {lnum}, {col})" function
1418 */
1419 void
1420f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1421{
1422 win_T *wp;
1423 linenr_T lnum;
1424 int screencol;
1425 int error = FALSE;
1426
1427 rettv->vval.v_number = -1;
1428
1429 if (check_for_number_arg(argvars, 0) == FAIL
1430 || check_for_number_arg(argvars, 1) == FAIL
1431 || check_for_number_arg(argvars, 2) == FAIL)
1432 return;
1433
1434 wp = find_win_by_nr_or_id(&argvars[0]);
1435 if (wp == NULL)
1436 return;
1437
1438 lnum = tv_get_number_chk(&argvars[1], &error);
1439 if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1440 return;
1441
1442 screencol = tv_get_number_chk(&argvars[2], &error);
1443 if (error || screencol < 0)
1444 return;
1445
1446 rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1447}
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001448#endif
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450/*
1451 * Scroll the current window down by "line_count" logical lines. "CTRL-Y"
1452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001454scrolldown(
1455 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001456 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001458 long done = 0; // total # of physical lines done
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 int wrow;
1460 int moved = FALSE;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001461 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001462 int width1 = 0;
1463 int width2 = 0;
1464
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001465 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001466 {
1467 width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001468 width2 = width1 + curwin_col_off2();
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
1471#ifdef FEAT_FOLDING
1472 linenr_T first;
1473
Bram Moolenaar85a20022019-12-21 18:25:54 +01001474 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1476#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001477 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001478 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
1480#ifdef FEAT_DIFF
Bram Moolenaarfa316dd2009-11-03 15:23:14 +00001481 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)
1482 && curwin->w_topfill < curwin->w_height - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 ++curwin->w_topfill;
1485 ++done;
1486 }
1487 else
1488#endif
1489 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001490 // break when at the very top
1491 if (curwin->w_topline == 1
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001492 && (!do_sms || curwin->w_skipcol < width1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 break;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001494 if (do_sms && curwin->w_skipcol >= width1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001496 // scroll a screen line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001497 if (curwin->w_skipcol >= width1 + width2)
1498 curwin->w_skipcol -= width2;
1499 else
1500 curwin->w_skipcol -= width1;
1501 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 ++done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 else
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001505 {
Bram Moolenaar8df97482022-10-03 12:11:13 +01001506 // scroll a text line down
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001507 --curwin->w_topline;
1508 curwin->w_skipcol = 0;
1509#ifdef FEAT_DIFF
1510 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001512#ifdef FEAT_FOLDING
1513 // A sequence of folded lines only counts for one logical line
1514 if (hasFolding(curwin->w_topline, &first, NULL))
1515 {
1516 ++done;
1517 if (!byfold)
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001518 todo -= curwin->w_topline - first - 1;
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001519 curwin->w_botline -= curwin->w_topline - first;
1520 curwin->w_topline = first;
1521 }
1522 else
1523#endif
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001524 if (do_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001525 {
1526 int size = win_linetabsize(curwin, curwin->w_topline,
1527 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1528 if (size > width1)
1529 {
1530 curwin->w_skipcol = width1;
1531 size -= width1;
1532 redraw_later(UPD_NOT_VALID);
1533 }
1534 while (size > width2)
1535 {
1536 curwin->w_skipcol += width2;
1537 size -= width2;
1538 }
1539 ++done;
1540 }
1541 else
1542 done += PLINES_NOFILL(curwin->w_topline);
1543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001545 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 invalidate_botline();
1547 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001548 curwin->w_wrow += done; // keep w_wrow updated
1549 curwin->w_cline_row += done; // keep w_cline_row updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
1551#ifdef FEAT_DIFF
1552 if (curwin->w_cursor.lnum == curwin->w_topline)
1553 curwin->w_cline_row = 0;
1554 check_topfill(curwin, TRUE);
1555#endif
1556
1557 /*
1558 * Compute the row number of the last row of the cursor line
1559 * and move the cursor onto the displayed part of the window.
1560 */
1561 wrow = curwin->w_wrow;
Bram Moolenaar4033c552017-09-16 20:54:51 +02001562 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
1564 validate_virtcol();
1565 validate_cheight();
1566 wrow += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001567 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 while (wrow >= curwin->w_height && curwin->w_cursor.lnum > 1)
1570 {
1571#ifdef FEAT_FOLDING
1572 if (hasFolding(curwin->w_cursor.lnum, &first, NULL))
1573 {
1574 --wrow;
1575 if (first == 1)
1576 curwin->w_cursor.lnum = 1;
1577 else
1578 curwin->w_cursor.lnum = first - 1;
1579 }
1580 else
1581#endif
1582 wrow -= plines(curwin->w_cursor.lnum--);
1583 curwin->w_valid &=
1584 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1585 moved = TRUE;
1586 }
1587 if (moved)
1588 {
1589#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01001590 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 foldAdjustCursor();
1592#endif
1593 coladvance(curwin->w_curswant);
1594 }
1595}
1596
1597/*
1598 * Scroll the current window up by "line_count" logical lines. "CTRL-E"
1599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001601scrollup(
1602 long line_count,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 int byfold UNUSED) // TRUE: count a closed fold as one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001605 int do_sms = curwin->w_p_wrap && curwin->w_p_sms;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001607 if (do_sms
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608# ifdef FEAT_FOLDING
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001609 || (byfold && hasAnyFolding(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610# endif
1611# ifdef FEAT_DIFF
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001612 || curwin->w_p_diff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613# endif
1614 )
1615 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001616 int width1 = curwin->w_width - curwin_col_off();
1617 int width2 = width1 + curwin_col_off2();
1618 int size = 0;
1619 linenr_T prev_topline = curwin->w_topline;
1620
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001621 if (do_sms)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001622 size = win_linetabsize(curwin, curwin->w_topline,
1623 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1624
1625 // diff mode: first consume "topfill"
1626 // 'smoothscroll': increase "w_skipcol" until it goes over the end of
1627 // the line, then advance to the next line.
1628 // folding: count each sequence of folded lines as one logical line.
1629 for (int todo = line_count; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
1631# ifdef FEAT_DIFF
1632 if (curwin->w_topfill > 0)
1633 --curwin->w_topfill;
1634 else
1635# endif
1636 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001637 linenr_T lnum = curwin->w_topline;
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639# ifdef FEAT_FOLDING
1640 if (byfold)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001641 // for a closed fold: go to the last line in the fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 (void)hasFolding(lnum, NULL, &lnum);
1643# endif
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001644 if (lnum == curwin->w_topline
1645 && curwin->w_p_wrap && curwin->w_p_sms)
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001646 {
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001647 // 'smoothscroll': increase "w_skipcol" until it goes over
1648 // the end of the line, then advance to the next line.
1649 int add = curwin->w_skipcol > 0 ? width2 : width1;
1650 curwin->w_skipcol += add;
1651 if (curwin->w_skipcol >= size)
1652 {
1653 if (lnum == curbuf->b_ml.ml_line_count)
1654 {
1655 // at the last screen line, can't scroll further
1656 curwin->w_skipcol -= add;
1657 break;
1658 }
1659 ++lnum;
1660 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001661 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001662 else
1663 {
1664 if (lnum >= curbuf->b_ml.ml_line_count)
1665 break;
1666 ++lnum;
1667 }
1668
1669 if (lnum > curwin->w_topline)
1670 {
1671 // approximate w_botline
1672 curwin->w_botline += lnum - curwin->w_topline;
1673 curwin->w_topline = lnum;
1674# ifdef FEAT_DIFF
1675 curwin->w_topfill = diff_check_fill(curwin, lnum);
1676# endif
1677 curwin->w_skipcol = 0;
Bram Moolenaar1a58e1d2022-10-06 13:09:17 +01001678 if (todo > 1 && do_sms)
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001679 size = win_linetabsize(curwin, curwin->w_topline,
1680 ml_get(curwin->w_topline), (colnr_T)MAXCOL);
1681 }
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001682 }
1683 }
Bram Moolenaar6b2d4ff2022-10-03 14:06:02 +01001684
Bram Moolenaarf6196f42022-10-02 21:29:55 +01001685 if (curwin->w_topline == prev_topline)
1686 // need to redraw even though w_topline didn't change
1687 redraw_later(UPD_NOT_VALID);
1688 }
1689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 curwin->w_topline += line_count;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001692 curwin->w_botline += line_count; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
1694
1695 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1696 curwin->w_topline = curbuf->b_ml.ml_line_count;
1697 if (curwin->w_botline > curbuf->b_ml.ml_line_count + 1)
1698 curwin->w_botline = curbuf->b_ml.ml_line_count + 1;
1699
1700#ifdef FEAT_DIFF
1701 check_topfill(curwin, FALSE);
1702#endif
1703
1704#ifdef FEAT_FOLDING
1705 if (hasAnyFolding(curwin))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001706 // Make sure w_topline is at the first of a sequence of folded lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
1708#endif
1709
1710 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1711 if (curwin->w_cursor.lnum < curwin->w_topline)
1712 {
1713 curwin->w_cursor.lnum = curwin->w_topline;
1714 curwin->w_valid &=
1715 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW|VALID_VIRTCOL);
1716 coladvance(curwin->w_curswant);
1717 }
1718}
1719
1720#ifdef FEAT_DIFF
1721/*
1722 * Don't end up with too many filler lines in the window.
1723 */
1724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001725check_topfill(
1726 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001727 int down) // when TRUE scroll down when not enough space
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 int n;
1730
1731 if (wp->w_topfill > 0)
1732 {
1733 n = plines_win_nofill(wp, wp->w_topline, TRUE);
1734 if (wp->w_topfill + n > wp->w_height)
1735 {
1736 if (down && wp->w_topline > 1)
1737 {
1738 --wp->w_topline;
1739 wp->w_topfill = 0;
1740 }
1741 else
1742 {
1743 wp->w_topfill = wp->w_height - n;
1744 if (wp->w_topfill < 0)
1745 wp->w_topfill = 0;
1746 }
1747 }
1748 }
1749}
1750
1751/*
1752 * Use as many filler lines as possible for w_topline. Make sure w_topline
1753 * is still visible.
1754 */
1755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001756max_topfill(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 int n;
1759
1760 n = plines_nofill(curwin->w_topline);
1761 if (n >= curwin->w_height)
1762 curwin->w_topfill = 0;
1763 else
1764 {
1765 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
1766 if (curwin->w_topfill + n > curwin->w_height)
1767 curwin->w_topfill = curwin->w_height - n;
1768 }
1769}
1770#endif
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772/*
1773 * Scroll the screen one line down, but don't do it if it would move the
1774 * cursor off the screen.
1775 */
1776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001777scrolldown_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
1779 int end_row;
1780#ifdef FEAT_DIFF
1781 int can_fill = (curwin->w_topfill
1782 < diff_check_fill(curwin, curwin->w_topline));
1783#endif
1784
1785 if (curwin->w_topline <= 1
1786#ifdef FEAT_DIFF
1787 && !can_fill
1788#endif
1789 )
1790 return;
1791
Bram Moolenaar85a20022019-12-21 18:25:54 +01001792 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794 /*
1795 * Compute the row number of the last row of the cursor line
1796 * and make sure it doesn't go off the screen. Make sure the cursor
1797 * doesn't go past 'scrolloff' lines from the screen end.
1798 */
1799 end_row = curwin->w_wrow;
1800#ifdef FEAT_DIFF
1801 if (can_fill)
1802 ++end_row;
1803 else
1804 end_row += plines_nofill(curwin->w_topline - 1);
1805#else
1806 end_row += plines(curwin->w_topline - 1);
1807#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001808 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810 validate_cheight();
1811 validate_virtcol();
1812 end_row += curwin->w_cline_height - 1 -
Bram Moolenaar02631462017-09-22 15:20:32 +02001813 curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001815 if (end_row < curwin->w_height - get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
1817#ifdef FEAT_DIFF
1818 if (can_fill)
1819 {
1820 ++curwin->w_topfill;
1821 check_topfill(curwin, TRUE);
1822 }
1823 else
1824 {
1825 --curwin->w_topline;
1826 curwin->w_topfill = 0;
1827 }
1828#else
1829 --curwin->w_topline;
1830#endif
1831#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02001832 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001834 --curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1836 }
1837}
1838
1839/*
1840 * Scroll the screen one line up, but don't do it if it would move the cursor
1841 * off the screen.
1842 */
1843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001844scrollup_clamp(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
1846 int start_row;
1847
1848 if (curwin->w_topline == curbuf->b_ml.ml_line_count
1849#ifdef FEAT_DIFF
1850 && curwin->w_topfill == 0
1851#endif
1852 )
1853 return;
1854
Bram Moolenaar85a20022019-12-21 18:25:54 +01001855 validate_cursor(); // w_wrow needs to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857 /*
1858 * Compute the row number of the first row of the cursor line
1859 * and make sure it doesn't go off the screen. Make sure the cursor
1860 * doesn't go before 'scrolloff' lines from the screen start.
1861 */
1862#ifdef FEAT_DIFF
1863 start_row = curwin->w_wrow - plines_nofill(curwin->w_topline)
1864 - curwin->w_topfill;
1865#else
1866 start_row = curwin->w_wrow - plines(curwin->w_topline);
1867#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001868 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 validate_virtcol();
Bram Moolenaar02631462017-09-22 15:20:32 +02001871 start_row -= curwin->w_virtcol / curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
Bram Moolenaar375e3392019-01-31 18:26:10 +01001873 if (start_row >= get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
1875#ifdef FEAT_DIFF
1876 if (curwin->w_topfill > 0)
1877 --curwin->w_topfill;
1878 else
1879#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001880 {
1881#ifdef FEAT_FOLDING
1882 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
1883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 ++curwin->w_topline;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001885 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001886 ++curwin->w_botline; // approximate w_botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
1888 }
1889}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891/*
1892 * Add one line above "lp->lnum". This can be a filler line, a closed fold or
1893 * a (wrapped) text line. Uses and sets "lp->fill".
1894 * Returns the height of the added line in "lp->height".
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01001895 * Lines above the first one are incredibly high: MAXCOL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 */
1897 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001898topline_back(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899{
1900#ifdef FEAT_DIFF
1901 if (lp->fill < diff_check_fill(curwin, lp->lnum))
1902 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001903 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 ++lp->fill;
1905 lp->height = 1;
1906 }
1907 else
1908#endif
1909 {
1910 --lp->lnum;
1911#ifdef FEAT_DIFF
1912 lp->fill = 0;
1913#endif
1914 if (lp->lnum < 1)
1915 lp->height = MAXCOL;
1916 else
1917#ifdef FEAT_FOLDING
1918 if (hasFolding(lp->lnum, &lp->lnum, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001919 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 lp->height = 1;
1921 else
1922#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001923 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925}
1926
1927/*
1928 * Add one line below "lp->lnum". This can be a filler line, a closed fold or
1929 * a (wrapped) text line. Uses and sets "lp->fill".
1930 * Returns the height of the added line in "lp->height".
1931 * Lines below the last one are incredibly high.
1932 */
1933 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001934botline_forw(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935{
1936#ifdef FEAT_DIFF
1937 if (lp->fill < diff_check_fill(curwin, lp->lnum + 1))
1938 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001939 // Add a filler line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 ++lp->fill;
1941 lp->height = 1;
1942 }
1943 else
1944#endif
1945 {
1946 ++lp->lnum;
1947#ifdef FEAT_DIFF
1948 lp->fill = 0;
1949#endif
1950 if (lp->lnum > curbuf->b_ml.ml_line_count)
1951 lp->height = MAXCOL;
1952 else
1953#ifdef FEAT_FOLDING
1954 if (hasFolding(lp->lnum, NULL, &lp->lnum))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001955 // Add a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 lp->height = 1;
1957 else
1958#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02001959 lp->height = PLINES_NOFILL(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961}
1962
1963#ifdef FEAT_DIFF
1964/*
1965 * Switch from including filler lines below lp->lnum to including filler
1966 * lines above loff.lnum + 1. This keeps pointing to the same line.
1967 * When there are no filler lines nothing changes.
1968 */
1969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001970botline_topline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 if (lp->fill > 0)
1973 {
1974 ++lp->lnum;
1975 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1976 }
1977}
1978
1979/*
1980 * Switch from including filler lines above lp->lnum to including filler
1981 * lines below loff.lnum - 1. This keeps pointing to the same line.
1982 * When there are no filler lines nothing changes.
1983 */
1984 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001985topline_botline(lineoff_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 if (lp->fill > 0)
1988 {
1989 lp->fill = diff_check_fill(curwin, lp->lnum) - lp->fill + 1;
1990 --lp->lnum;
1991 }
1992}
1993#endif
1994
1995/*
1996 * Recompute topline to put the cursor at the top of the window.
1997 * Scroll at least "min_scroll" lines.
1998 * If "always" is TRUE, always set topline (for "zt").
1999 */
2000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002001scroll_cursor_top(int min_scroll, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003 int scrolled = 0;
2004 int extra = 0;
2005 int used;
2006 int i;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002007 linenr_T top; // just above displayed lines
2008 linenr_T bot; // just below displayed lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 linenr_T old_topline = curwin->w_topline;
2010#ifdef FEAT_DIFF
2011 linenr_T old_topfill = curwin->w_topfill;
2012#endif
2013 linenr_T new_topline;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002014 int off = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (mouse_dragging > 0)
2017 off = mouse_dragging - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
2019 /*
2020 * Decrease topline until:
2021 * - it has become 1
2022 * - (part of) the cursor line is moved off the screen or
2023 * - moved at least 'scrolljump' lines and
2024 * - at least 'scrolloff' lines above and below the cursor
2025 */
2026 validate_cheight();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002027 used = curwin->w_cline_height; // includes filler lines above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (curwin->w_cursor.lnum < curwin->w_topline)
2029 scrolled = used;
2030
2031#ifdef FEAT_FOLDING
2032 if (hasFolding(curwin->w_cursor.lnum, &top, &bot))
2033 {
2034 --top;
2035 ++bot;
2036 }
2037 else
2038#endif
2039 {
2040 top = curwin->w_cursor.lnum - 1;
2041 bot = curwin->w_cursor.lnum + 1;
2042 }
2043 new_topline = top + 1;
2044
2045#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002046 // "used" already contains the number of filler lines above, don't add it
2047 // again.
2048 // Hide filler lines above cursor line by adding them to "extra".
Bram Moolenaara09a2c52015-09-08 17:31:59 +02002049 extra += diff_check_fill(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050#endif
2051
2052 /*
2053 * Check if the lines from "top" to "bot" fit in the window. If they do,
2054 * set new_topline and advance "top" and "bot" to include more lines.
2055 */
2056 while (top > 0)
2057 {
2058#ifdef FEAT_FOLDING
2059 if (hasFolding(top, &top, NULL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002060 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 i = 1;
2062 else
2063#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002064 i = PLINES_NOFILL(top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 used += i;
2066 if (extra + i <= off && bot < curbuf->b_ml.ml_line_count)
2067 {
2068#ifdef FEAT_FOLDING
2069 if (hasFolding(bot, NULL, &bot))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002070 // count one logical line for a sequence of folded lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 ++used;
2072 else
2073#endif
2074 used += plines(bot);
2075 }
2076 if (used > curwin->w_height)
2077 break;
2078 if (top < curwin->w_topline)
2079 scrolled += i;
2080
2081 /*
2082 * If scrolling is needed, scroll at least 'sj' lines.
2083 */
2084 if ((new_topline >= curwin->w_topline || scrolled > min_scroll)
2085 && extra >= off)
2086 break;
2087
2088 extra += i;
2089 new_topline = top;
2090 --top;
2091 ++bot;
2092 }
2093
2094 /*
2095 * If we don't have enough space, put cursor in the middle.
2096 * This makes sure we get the same position when using "k" and "j"
2097 * in a small window.
2098 */
2099 if (used > curwin->w_height)
2100 scroll_cursor_halfway(FALSE);
2101 else
2102 {
2103 /*
2104 * If "always" is FALSE, only adjust topline to a lower value, higher
2105 * value may happen with wrapping lines
2106 */
2107 if (new_topline < curwin->w_topline || always)
2108 curwin->w_topline = new_topline;
2109 if (curwin->w_topline > curwin->w_cursor.lnum)
2110 curwin->w_topline = curwin->w_cursor.lnum;
2111#ifdef FEAT_DIFF
2112 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2113 if (curwin->w_topfill > 0 && extra > off)
2114 {
2115 curwin->w_topfill -= extra - off;
2116 if (curwin->w_topfill < 0)
2117 curwin->w_topfill = 0;
2118 }
2119 check_topfill(curwin, FALSE);
2120#endif
2121 if (curwin->w_topline != old_topline
2122#ifdef FEAT_DIFF
2123 || curwin->w_topfill != old_topfill
2124#endif
2125 )
2126 curwin->w_valid &=
2127 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2128 curwin->w_valid |= VALID_TOPLINE;
2129 }
2130}
2131
2132/*
2133 * Set w_empty_rows and w_filler_rows for window "wp", having used up "used"
2134 * screen lines for text lines.
2135 */
2136 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002137set_empty_rows(win_T *wp, int used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139#ifdef FEAT_DIFF
2140 wp->w_filler_rows = 0;
2141#endif
2142 if (used == 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002143 wp->w_empty_rows = 0; // single line that doesn't fit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 else
2145 {
2146 wp->w_empty_rows = wp->w_height - used;
2147#ifdef FEAT_DIFF
2148 if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count)
2149 {
2150 wp->w_filler_rows = diff_check_fill(wp, wp->w_botline);
2151 if (wp->w_empty_rows > wp->w_filler_rows)
2152 wp->w_empty_rows -= wp->w_filler_rows;
2153 else
2154 {
2155 wp->w_filler_rows = wp->w_empty_rows;
2156 wp->w_empty_rows = 0;
2157 }
2158 }
2159#endif
2160 }
2161}
2162
2163/*
2164 * Recompute topline to put the cursor at the bottom of the window.
Bram Moolenaar8088ae92022-06-20 11:38:17 +01002165 * When scrolling scroll at least "min_scroll" lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 * If "set_topbot" is TRUE, set topline and botline first (for "zb").
2167 * This is messy stuff!!!
2168 */
2169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002170scroll_cursor_bot(int min_scroll, int set_topbot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 int used;
2173 int scrolled = 0;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002174 int min_scrolled = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 int extra = 0;
2176 int i;
2177 linenr_T line_count;
2178 linenr_T old_topline = curwin->w_topline;
2179 lineoff_T loff;
2180 lineoff_T boff;
2181#ifdef FEAT_DIFF
2182 int old_topfill = curwin->w_topfill;
2183 int fill_below_window;
2184#endif
2185 linenr_T old_botline = curwin->w_botline;
2186 linenr_T old_valid = curwin->w_valid;
2187 int old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002188 linenr_T cln; // Cursor Line Number
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002189 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 cln = curwin->w_cursor.lnum;
2192 if (set_topbot)
2193 {
2194 used = 0;
2195 curwin->w_botline = cln + 1;
2196#ifdef FEAT_DIFF
2197 loff.fill = 0;
2198#endif
2199 for (curwin->w_topline = curwin->w_botline;
2200 curwin->w_topline > 1;
2201 curwin->w_topline = loff.lnum)
2202 {
2203 loff.lnum = curwin->w_topline;
2204 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002205 if (loff.height == MAXCOL || used + loff.height > curwin->w_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 break;
2207 used += loff.height;
2208#ifdef FEAT_DIFF
2209 curwin->w_topfill = loff.fill;
2210#endif
2211 }
2212 set_empty_rows(curwin, used);
2213 curwin->w_valid |= VALID_BOTLINE|VALID_BOTLINE_AP;
2214 if (curwin->w_topline != old_topline
2215#ifdef FEAT_DIFF
2216 || curwin->w_topfill != old_topfill
2217#endif
2218 )
2219 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2220 }
2221 else
2222 validate_botline();
2223
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 // The lines of the cursor line itself are always used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#ifdef FEAT_DIFF
2226 used = plines_nofill(cln);
2227#else
2228 validate_cheight();
2229 used = curwin->w_cline_height;
2230#endif
2231
Bram Moolenaar85a20022019-12-21 18:25:54 +01002232 // If the cursor is below botline, we will at least scroll by the height
2233 // of the cursor line. Correct for empty lines, which are really part of
2234 // botline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (cln >= curwin->w_botline)
2236 {
2237 scrolled = used;
2238 if (cln == curwin->w_botline)
2239 scrolled -= curwin->w_empty_rows;
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002240 min_scrolled = scrolled;
2241 if (cln > curwin->w_botline && curwin->w_p_sms && curwin->w_p_wrap)
2242 for (linenr_T lnum = curwin->w_botline + 1; lnum <= cln; ++lnum)
2243 min_scrolled += plines_nofill(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245
2246 /*
2247 * Stop counting lines to scroll when
2248 * - hitting start of the file
2249 * - scrolled nothing or at least 'sj' lines
Bram Moolenaar375e3392019-01-31 18:26:10 +01002250 * - at least 'scrolloff' lines below the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 * - lines between botline and cursor have been counted
2252 */
2253#ifdef FEAT_FOLDING
2254 if (!hasFolding(curwin->w_cursor.lnum, &loff.lnum, &boff.lnum))
2255#endif
2256 {
2257 loff.lnum = cln;
2258 boff.lnum = cln;
2259 }
2260#ifdef FEAT_DIFF
2261 loff.fill = 0;
2262 boff.fill = 0;
2263 fill_below_window = diff_check_fill(curwin, curwin->w_botline)
2264 - curwin->w_filler_rows;
2265#endif
2266
2267 while (loff.lnum > 1)
2268 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002269 // Stop when scrolled nothing or at least "min_scroll", found "extra"
2270 // context for 'scrolloff' and counted all lines below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if ((((scrolled <= 0 || scrolled >= min_scroll)
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002272 && extra >= (mouse_dragging > 0 ? mouse_dragging - 1 : so))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 || boff.lnum + 1 > curbuf->b_ml.ml_line_count)
2274 && loff.lnum <= curwin->w_botline
2275#ifdef FEAT_DIFF
2276 && (loff.lnum < curwin->w_botline
2277 || loff.fill >= fill_below_window)
2278#endif
2279 )
2280 break;
2281
Bram Moolenaar85a20022019-12-21 18:25:54 +01002282 // Add one line above
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002284 if (loff.height == MAXCOL)
2285 used = MAXCOL;
2286 else
2287 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (used > curwin->w_height)
2289 break;
2290 if (loff.lnum >= curwin->w_botline
2291#ifdef FEAT_DIFF
2292 && (loff.lnum > curwin->w_botline
2293 || loff.fill <= fill_below_window)
2294#endif
2295 )
2296 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 scrolled += loff.height;
2299 if (loff.lnum == curwin->w_botline
2300#ifdef FEAT_DIFF
Bram Moolenaar4e303c82018-11-24 14:27:44 +01002301 && loff.fill == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302#endif
2303 )
2304 scrolled -= curwin->w_empty_rows;
2305 }
2306
2307 if (boff.lnum < curbuf->b_ml.ml_line_count)
2308 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002309 // Add one line below
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 botline_forw(&boff);
2311 used += boff.height;
2312 if (used > curwin->w_height)
2313 break;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002314 if (extra < ( mouse_dragging > 0 ? mouse_dragging - 1 : so)
2315 || scrolled < min_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 extra += boff.height;
2318 if (boff.lnum >= curwin->w_botline
2319#ifdef FEAT_DIFF
2320 || (boff.lnum + 1 == curwin->w_botline
2321 && boff.fill > curwin->w_filler_rows)
2322#endif
2323 )
2324 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002325 // Count screen lines that are below the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 scrolled += boff.height;
2327 if (boff.lnum == curwin->w_botline
2328#ifdef FEAT_DIFF
2329 && boff.fill == 0
2330#endif
2331 )
2332 scrolled -= curwin->w_empty_rows;
2333 }
2334 }
2335 }
2336 }
2337
Bram Moolenaar85a20022019-12-21 18:25:54 +01002338 // curwin->w_empty_rows is larger, no need to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (scrolled <= 0)
2340 line_count = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002341 // more than a screenfull, don't scroll but redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 else if (used > curwin->w_height)
2343 line_count = used;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002344 // scroll minimal number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 else
2346 {
2347 line_count = 0;
2348#ifdef FEAT_DIFF
2349 boff.fill = curwin->w_topfill;
2350#endif
2351 boff.lnum = curwin->w_topline - 1;
2352 for (i = 0; i < scrolled && boff.lnum < curwin->w_botline; )
2353 {
2354 botline_forw(&boff);
2355 i += boff.height;
2356 ++line_count;
2357 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002358 if (i < scrolled) // below curwin->w_botline, don't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 line_count = 9999;
2360 }
2361
2362 /*
2363 * Scroll up if the cursor is off the bottom of the screen a bit.
2364 * Otherwise put it at 1/2 of the screen.
2365 */
2366 if (line_count >= curwin->w_height && line_count > min_scroll)
2367 scroll_cursor_halfway(FALSE);
2368 else
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002369 {
2370 // With 'smoothscroll' scroll at least the height of the cursor line.
2371 if (curwin->w_p_wrap && curwin->w_p_sms && line_count < min_scrolled)
2372 line_count = min_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 scrollup(line_count, TRUE);
Bram Moolenaar9bab7a02022-10-06 14:57:53 +01002374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 /*
2377 * If topline didn't change we need to restore w_botline and w_empty_rows
2378 * (we changed them).
2379 * If topline did change, update_screen() will set botline.
2380 */
2381 if (curwin->w_topline == old_topline && set_topbot)
2382 {
2383 curwin->w_botline = old_botline;
2384 curwin->w_empty_rows = old_empty_rows;
2385 curwin->w_valid = old_valid;
2386 }
2387 curwin->w_valid |= VALID_TOPLINE;
2388}
2389
2390/*
2391 * Recompute topline to put the cursor halfway the window
2392 * If "atend" is TRUE, also put it halfway at the end of the file.
2393 */
2394 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002395scroll_cursor_halfway(int atend)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 int above = 0;
2398 linenr_T topline;
2399#ifdef FEAT_DIFF
2400 int topfill = 0;
2401#endif
2402 int below = 0;
2403 int used;
2404 lineoff_T loff;
2405 lineoff_T boff;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002406#ifdef FEAT_DIFF
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002407 linenr_T old_topline = curwin->w_topline;
Bram Moolenaarb8e23052014-02-11 18:58:09 +01002408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02002410#ifdef FEAT_PROP_POPUP
2411 // if the width changed this needs to be updated first
2412 may_update_popup_position();
2413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 loff.lnum = boff.lnum = curwin->w_cursor.lnum;
2415#ifdef FEAT_FOLDING
2416 (void)hasFolding(loff.lnum, &loff.lnum, &boff.lnum);
2417#endif
2418#ifdef FEAT_DIFF
2419 used = plines_nofill(loff.lnum);
2420 loff.fill = 0;
2421 boff.fill = 0;
2422#else
2423 used = plines(loff.lnum);
2424#endif
2425 topline = loff.lnum;
2426 while (topline > 1)
2427 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002428 if (below <= above) // add a line below the cursor first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 if (boff.lnum < curbuf->b_ml.ml_line_count)
2431 {
2432 botline_forw(&boff);
2433 used += boff.height;
2434 if (used > curwin->w_height)
2435 break;
2436 below += boff.height;
2437 }
2438 else
2439 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002440 ++below; // count a "~" line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (atend)
2442 ++used;
2443 }
2444 }
2445
Bram Moolenaar85a20022019-12-21 18:25:54 +01002446 if (below > above) // add a line above the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002449 if (loff.height == MAXCOL)
2450 used = MAXCOL;
2451 else
2452 used += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (used > curwin->w_height)
2454 break;
2455 above += loff.height;
2456 topline = loff.lnum;
2457#ifdef FEAT_DIFF
2458 topfill = loff.fill;
2459#endif
2460 }
2461 }
2462#ifdef FEAT_FOLDING
2463 if (!hasFolding(topline, &curwin->w_topline, NULL))
2464#endif
2465 curwin->w_topline = topline;
2466#ifdef FEAT_DIFF
2467 curwin->w_topfill = topfill;
Bram Moolenaar12a0f222014-02-11 15:47:46 +01002468 if (old_topline > curwin->w_topline + curwin->w_height)
2469 curwin->w_botfill = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 check_topfill(curwin, FALSE);
2471#endif
2472 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2473 curwin->w_valid |= VALID_TOPLINE;
2474}
2475
2476/*
2477 * Correct the cursor position so that it is in a part of the screen at least
Bram Moolenaar375e3392019-01-31 18:26:10 +01002478 * 'scrolloff' lines from the top and bottom, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 * If not possible, put it at the same position as scroll_cursor_halfway().
2480 * When called topline must be valid!
2481 */
2482 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002483cursor_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002485 int above = 0; // screen lines above topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 linenr_T topline;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002487 int below = 0; // screen lines below botline
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 linenr_T botline;
2489 int above_wanted, below_wanted;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002490 linenr_T cln; // Cursor Line Number
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 int max_off;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002492 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 /*
2495 * How many lines we would like to have above/below the cursor depends on
2496 * whether the first/last line of the file is on screen.
2497 */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002498 above_wanted = so;
2499 below_wanted = so;
Bram Moolenaar9964e462007-05-05 17:54:07 +00002500 if (mouse_dragging > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
2502 above_wanted = mouse_dragging - 1;
2503 below_wanted = mouse_dragging - 1;
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 if (curwin->w_topline == 1)
2506 {
2507 above_wanted = 0;
2508 max_off = curwin->w_height / 2;
2509 if (below_wanted > max_off)
2510 below_wanted = max_off;
2511 }
2512 validate_botline();
2513 if (curwin->w_botline == curbuf->b_ml.ml_line_count + 1
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002514 && mouse_dragging == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 below_wanted = 0;
2517 max_off = (curwin->w_height - 1) / 2;
2518 if (above_wanted > max_off)
2519 above_wanted = max_off;
2520 }
2521
2522 /*
2523 * If there are sufficient file-lines above and below the cursor, we can
2524 * return now.
2525 */
2526 cln = curwin->w_cursor.lnum;
2527 if (cln >= curwin->w_topline + above_wanted
2528 && cln < curwin->w_botline - below_wanted
2529#ifdef FEAT_FOLDING
2530 && !hasAnyFolding(curwin)
2531#endif
2532 )
2533 return;
2534
2535 /*
2536 * Narrow down the area where the cursor can be put by taking lines from
2537 * the top and the bottom until:
2538 * - the desired context lines are found
2539 * - the lines from the top is past the lines from the bottom
2540 */
2541 topline = curwin->w_topline;
2542 botline = curwin->w_botline - 1;
2543#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002544 // count filler lines as context
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 above = curwin->w_topfill;
2546 below = curwin->w_filler_rows;
2547#endif
2548 while ((above < above_wanted || below < below_wanted) && topline < botline)
2549 {
2550 if (below < below_wanted && (below <= above || above >= above_wanted))
2551 {
2552#ifdef FEAT_FOLDING
2553 if (hasFolding(botline, &botline, NULL))
2554 ++below;
2555 else
2556#endif
2557 below += plines(botline);
2558 --botline;
2559 }
2560 if (above < above_wanted && (above < below || below >= below_wanted))
2561 {
2562#ifdef FEAT_FOLDING
2563 if (hasFolding(topline, NULL, &topline))
2564 ++above;
2565 else
2566#endif
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002567 above += PLINES_NOFILL(topline);
2568#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002569 // Count filler lines below this line as context.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (topline < botline)
2571 above += diff_check_fill(curwin, topline + 1);
2572#endif
2573 ++topline;
2574 }
2575 }
2576 if (topline == botline || botline == 0)
2577 curwin->w_cursor.lnum = topline;
2578 else if (topline > botline)
2579 curwin->w_cursor.lnum = botline;
2580 else
2581 {
2582 if (cln < topline && curwin->w_topline > 1)
2583 {
2584 curwin->w_cursor.lnum = topline;
2585 curwin->w_valid &=
2586 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2587 }
2588 if (cln > botline && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2589 {
2590 curwin->w_cursor.lnum = botline;
2591 curwin->w_valid &=
2592 ~(VALID_WROW|VALID_WCOL|VALID_CHEIGHT|VALID_CROW);
2593 }
2594 }
2595 curwin->w_valid |= VALID_TOPLINE;
2596}
2597
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002598static void get_scroll_overlap(lineoff_T *lp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600/*
2601 * move screen 'count' pages up or down and update screen
2602 *
2603 * return FAIL for failure, OK otherwise
2604 */
2605 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002606onepage(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 long n;
2609 int retval = OK;
2610 lineoff_T loff;
2611 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002612 long so = get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
Bram Moolenaar85a20022019-12-21 18:25:54 +01002614 if (curbuf->b_ml.ml_line_count == 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 beep_flush();
2617 return FAIL;
2618 }
2619
2620 for ( ; count > 0; --count)
2621 {
2622 validate_botline();
2623 /*
2624 * It's an error to move a page up when the first line is already on
2625 * the screen. It's an error to move a page down when the last line
2626 * is on the screen and the topline is 'scrolloff' lines from the
2627 * last line.
2628 */
2629 if (dir == FORWARD
Bram Moolenaar375e3392019-01-31 18:26:10 +01002630 ? ((curwin->w_topline >= curbuf->b_ml.ml_line_count - so)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 && curwin->w_botline > curbuf->b_ml.ml_line_count)
2632 : (curwin->w_topline == 1
2633#ifdef FEAT_DIFF
2634 && curwin->w_topfill ==
2635 diff_check_fill(curwin, curwin->w_topline)
2636#endif
2637 ))
2638 {
2639 beep_flush();
2640 retval = FAIL;
2641 break;
2642 }
2643
2644#ifdef FEAT_DIFF
2645 loff.fill = 0;
2646#endif
2647 if (dir == FORWARD)
2648 {
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002649 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 // Vi compatible scrolling
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002652 if (p_window <= 2)
2653 ++curwin->w_topline;
2654 else
2655 curwin->w_topline += p_window - 2;
2656 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
2657 curwin->w_topline = curbuf->b_ml.ml_line_count;
2658 curwin->w_cursor.lnum = curwin->w_topline;
2659 }
2660 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2661 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 // at end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 curwin->w_topline = curbuf->b_ml.ml_line_count;
2664#ifdef FEAT_DIFF
2665 curwin->w_topfill = 0;
2666#endif
2667 curwin->w_valid &= ~(VALID_WROW|VALID_CROW);
2668 }
2669 else
2670 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002671 // For the overlap, start with the line just below the window
2672 // and go upwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 loff.lnum = curwin->w_botline;
2674#ifdef FEAT_DIFF
2675 loff.fill = diff_check_fill(curwin, loff.lnum)
2676 - curwin->w_filler_rows;
2677#endif
2678 get_scroll_overlap(&loff, -1);
2679 curwin->w_topline = loff.lnum;
2680#ifdef FEAT_DIFF
2681 curwin->w_topfill = loff.fill;
2682 check_topfill(curwin, FALSE);
2683#endif
2684 curwin->w_cursor.lnum = curwin->w_topline;
2685 curwin->w_valid &= ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|
2686 VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2687 }
2688 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002689 else // dir == BACKWARDS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
2691#ifdef FEAT_DIFF
2692 if (curwin->w_topline == 1)
2693 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694 // Include max number of filler lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 max_topfill();
2696 continue;
2697 }
2698#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002699 if (ONE_WINDOW && p_window > 0 && p_window < Rows - 1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002700 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002701 // Vi compatible scrolling (sort of)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002702 if (p_window <= 2)
2703 --curwin->w_topline;
2704 else
2705 curwin->w_topline -= p_window - 2;
2706 if (curwin->w_topline < 1)
2707 curwin->w_topline = 1;
2708 curwin->w_cursor.lnum = curwin->w_topline + p_window - 1;
2709 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2710 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2711 continue;
2712 }
2713
Bram Moolenaar85a20022019-12-21 18:25:54 +01002714 // Find the line at the top of the window that is going to be the
2715 // line at the bottom of the window. Make sure this results in
2716 // the same line as before doing CTRL-F.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 loff.lnum = curwin->w_topline - 1;
2718#ifdef FEAT_DIFF
2719 loff.fill = diff_check_fill(curwin, loff.lnum + 1)
2720 - curwin->w_topfill;
2721#endif
2722 get_scroll_overlap(&loff, 1);
2723
2724 if (loff.lnum >= curbuf->b_ml.ml_line_count)
2725 {
2726 loff.lnum = curbuf->b_ml.ml_line_count;
2727#ifdef FEAT_DIFF
2728 loff.fill = 0;
2729 }
2730 else
2731 {
2732 botline_topline(&loff);
2733#endif
2734 }
2735 curwin->w_cursor.lnum = loff.lnum;
2736
Bram Moolenaar85a20022019-12-21 18:25:54 +01002737 // Find the line just above the new topline to get the right line
2738 // at the bottom of the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 n = 0;
2740 while (n <= curwin->w_height && loff.lnum >= 1)
2741 {
2742 topline_back(&loff);
Bram Moolenaarbacd9da2010-02-17 18:20:37 +01002743 if (loff.height == MAXCOL)
2744 n = MAXCOL;
2745 else
2746 n += loff.height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002748 if (loff.lnum < 1) // at begin of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
2750 curwin->w_topline = 1;
2751#ifdef FEAT_DIFF
2752 max_topfill();
2753#endif
2754 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2755 }
2756 else
2757 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002758 // Go two lines forward again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759#ifdef FEAT_DIFF
2760 topline_botline(&loff);
2761#endif
2762 botline_forw(&loff);
2763 botline_forw(&loff);
2764#ifdef FEAT_DIFF
2765 botline_topline(&loff);
2766#endif
2767#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01002768 // We're at the wrong end of a fold now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 (void)hasFolding(loff.lnum, &loff.lnum, NULL);
2770#endif
2771
Bram Moolenaar85a20022019-12-21 18:25:54 +01002772 // Always scroll at least one line. Avoid getting stuck on
2773 // very long lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (loff.lnum >= curwin->w_topline
2775#ifdef FEAT_DIFF
2776 && (loff.lnum > curwin->w_topline
2777 || loff.fill >= curwin->w_topfill)
2778#endif
2779 )
2780 {
2781#ifdef FEAT_DIFF
Bram Moolenaar85a20022019-12-21 18:25:54 +01002782 // First try using the maximum number of filler lines. If
2783 // that's not enough, backup one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 loff.fill = curwin->w_topfill;
2785 if (curwin->w_topfill < diff_check_fill(curwin,
2786 curwin->w_topline))
2787 max_topfill();
2788 if (curwin->w_topfill == loff.fill)
2789#endif
2790 {
2791 --curwin->w_topline;
2792#ifdef FEAT_DIFF
2793 curwin->w_topfill = 0;
2794#endif
2795 }
2796 comp_botline(curwin);
2797 curwin->w_cursor.lnum = curwin->w_botline - 1;
Bram Moolenaar3d6db142014-03-28 21:49:32 +01002798 curwin->w_valid &=
2799 ~(VALID_WCOL|VALID_CHEIGHT|VALID_WROW|VALID_CROW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 else
2802 {
2803 curwin->w_topline = loff.lnum;
2804#ifdef FEAT_DIFF
2805 curwin->w_topfill = loff.fill;
2806 check_topfill(curwin, FALSE);
2807#endif
2808 curwin->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE);
2809 }
2810 }
2811 }
2812 }
2813#ifdef FEAT_FOLDING
2814 foldAdjustCursor();
2815#endif
2816 cursor_correct();
Bram Moolenaarbc54f3f2016-09-04 14:34:28 +02002817 check_cursor_col();
Bram Moolenaar7c626922005-02-07 22:01:03 +00002818 if (retval == OK)
2819 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 curwin->w_valid &= ~(VALID_WCOL|VALID_WROW|VALID_VIRTCOL);
2821
Bram Moolenaar907dad72018-07-10 15:07:15 +02002822 if (retval == OK && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002824 // Avoid the screen jumping up and down when 'scrolloff' is non-zero.
2825 // But make sure we scroll at least one line (happens with mix of long
2826 // wrapping lines and non-wrapping line).
2827 if (check_top_offset())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
Bram Moolenaar907dad72018-07-10 15:07:15 +02002829 scroll_cursor_top(1, FALSE);
2830 if (curwin->w_topline <= old_topline
2831 && old_topline < curbuf->b_ml.ml_line_count)
2832 {
2833 curwin->w_topline = old_topline + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834#ifdef FEAT_FOLDING
Bram Moolenaar907dad72018-07-10 15:07:15 +02002835 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2836#endif
2837 }
2838 }
2839#ifdef FEAT_FOLDING
2840 else if (curwin->w_botline > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
2844
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002845 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 return retval;
2847}
2848
2849/*
2850 * Decide how much overlap to use for page-up or page-down scrolling.
2851 * This is symmetric, so that doing both keeps the same lines displayed.
2852 * Three lines are examined:
2853 *
2854 * before CTRL-F after CTRL-F / before CTRL-B
2855 * etc. l1
2856 * l1 last but one line ------------
2857 * l2 last text line l2 top text line
2858 * ------------- l3 second text line
2859 * l3 etc.
2860 */
2861 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002862get_scroll_overlap(lineoff_T *lp, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 int h1, h2, h3, h4;
2865 int min_height = curwin->w_height - 2;
2866 lineoff_T loff0, loff1, loff2;
2867
2868#ifdef FEAT_DIFF
2869 if (lp->fill > 0)
2870 lp->height = 1;
2871 else
2872 lp->height = plines_nofill(lp->lnum);
2873#else
2874 lp->height = plines(lp->lnum);
2875#endif
2876 h1 = lp->height;
2877 if (h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002878 return; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 loff0 = *lp;
2881 if (dir > 0)
2882 botline_forw(lp);
2883 else
2884 topline_back(lp);
2885 h2 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002886 if (h2 == MAXCOL || h2 + h1 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002888 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 return;
2890 }
2891
2892 loff1 = *lp;
2893 if (dir > 0)
2894 botline_forw(lp);
2895 else
2896 topline_back(lp);
2897 h3 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002898 if (h3 == MAXCOL || h3 + h2 > min_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002900 *lp = loff0; // no overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 return;
2902 }
2903
2904 loff2 = *lp;
2905 if (dir > 0)
2906 botline_forw(lp);
2907 else
2908 topline_back(lp);
2909 h4 = lp->height;
Bram Moolenaarf4f19562012-11-28 18:22:11 +01002910 if (h4 == MAXCOL || h4 + h3 + h2 > min_height || h3 + h2 + h1 > min_height)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002911 *lp = loff1; // 1 line overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002913 *lp = loff2; // 2 lines overlap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914}
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916/*
2917 * Scroll 'scroll' lines up or down.
2918 */
2919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002920halfpage(int flag, linenr_T Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922 long scrolled = 0;
2923 int i;
2924 int n;
2925 int room;
2926
2927 if (Prenum)
2928 curwin->w_p_scr = (Prenum > curwin->w_height) ?
2929 curwin->w_height : Prenum;
2930 n = (curwin->w_p_scr <= curwin->w_height) ?
2931 curwin->w_p_scr : curwin->w_height;
2932
Bram Moolenaard5d37532017-03-27 23:02:07 +02002933 update_topline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 validate_botline();
2935 room = curwin->w_empty_rows;
2936#ifdef FEAT_DIFF
2937 room += curwin->w_filler_rows;
2938#endif
2939 if (flag)
2940 {
2941 /*
2942 * scroll the text up
2943 */
2944 while (n > 0 && curwin->w_botline <= curbuf->b_ml.ml_line_count)
2945 {
2946#ifdef FEAT_DIFF
2947 if (curwin->w_topfill > 0)
2948 {
2949 i = 1;
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02002950 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 --curwin->w_topfill;
2952 }
2953 else
2954#endif
2955 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02002956 i = PLINES_NOFILL(curwin->w_topline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 n -= i;
2958 if (n < 0 && scrolled > 0)
2959 break;
2960#ifdef FEAT_FOLDING
2961 (void)hasFolding(curwin->w_topline, NULL, &curwin->w_topline);
2962#endif
2963 ++curwin->w_topline;
2964#ifdef FEAT_DIFF
2965 curwin->w_topfill = diff_check_fill(curwin, curwin->w_topline);
2966#endif
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2969 {
2970 ++curwin->w_cursor.lnum;
2971 curwin->w_valid &=
2972 ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
2973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 }
2975 curwin->w_valid &= ~(VALID_CROW|VALID_WROW);
2976 scrolled += i;
2977
2978 /*
2979 * Correct w_botline for changed w_topline.
2980 * Won't work when there are filler lines.
2981 */
2982#ifdef FEAT_DIFF
2983 if (curwin->w_p_diff)
2984 curwin->w_valid &= ~(VALID_BOTLINE|VALID_BOTLINE_AP);
2985 else
2986#endif
2987 {
2988 room += i;
2989 do
2990 {
2991 i = plines(curwin->w_botline);
2992 if (i > room)
2993 break;
2994#ifdef FEAT_FOLDING
2995 (void)hasFolding(curwin->w_botline, NULL,
2996 &curwin->w_botline);
2997#endif
2998 ++curwin->w_botline;
2999 room -= i;
3000 } while (curwin->w_botline <= curbuf->b_ml.ml_line_count);
3001 }
3002 }
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 /*
3005 * When hit bottom of the file: move cursor down.
3006 */
3007 if (n > 0)
3008 {
3009# ifdef FEAT_FOLDING
3010 if (hasAnyFolding(curwin))
3011 {
3012 while (--n >= 0
3013 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3014 {
3015 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3016 &curwin->w_cursor.lnum);
3017 ++curwin->w_cursor.lnum;
3018 }
3019 }
3020 else
3021# endif
3022 curwin->w_cursor.lnum += n;
3023 check_cursor_lnum();
3024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026 else
3027 {
3028 /*
3029 * scroll the text down
3030 */
3031 while (n > 0 && curwin->w_topline > 1)
3032 {
3033#ifdef FEAT_DIFF
3034 if (curwin->w_topfill < diff_check_fill(curwin, curwin->w_topline))
3035 {
3036 i = 1;
Bram Moolenaard00e0242019-03-20 21:42:20 +01003037 --n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 ++curwin->w_topfill;
3039 }
3040 else
3041#endif
3042 {
Bram Moolenaar43335ea2015-09-09 20:59:37 +02003043 i = PLINES_NOFILL(curwin->w_topline - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 n -= i;
3045 if (n < 0 && scrolled > 0)
3046 break;
3047 --curwin->w_topline;
3048#ifdef FEAT_FOLDING
3049 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
3050#endif
3051#ifdef FEAT_DIFF
3052 curwin->w_topfill = 0;
3053#endif
3054 }
3055 curwin->w_valid &= ~(VALID_CROW|VALID_WROW|
3056 VALID_BOTLINE|VALID_BOTLINE_AP);
3057 scrolled += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 if (curwin->w_cursor.lnum > 1)
3059 {
3060 --curwin->w_cursor.lnum;
3061 curwin->w_valid &= ~(VALID_VIRTCOL|VALID_CHEIGHT|VALID_WCOL);
3062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
Bram Moolenaard00e0242019-03-20 21:42:20 +01003064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 /*
3066 * When hit top of the file: move cursor up.
3067 */
3068 if (n > 0)
3069 {
3070 if (curwin->w_cursor.lnum <= (linenr_T)n)
3071 curwin->w_cursor.lnum = 1;
3072 else
3073# ifdef FEAT_FOLDING
3074 if (hasAnyFolding(curwin))
3075 {
3076 while (--n >= 0 && curwin->w_cursor.lnum > 1)
3077 {
3078 --curwin->w_cursor.lnum;
3079 (void)hasFolding(curwin->w_cursor.lnum,
3080 &curwin->w_cursor.lnum, NULL);
3081 }
3082 }
3083 else
3084# endif
3085 curwin->w_cursor.lnum -= n;
3086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
3088# ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +01003089 // Move cursor to first line of closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 foldAdjustCursor();
3091# endif
3092#ifdef FEAT_DIFF
3093 check_topfill(curwin, !flag);
3094#endif
3095 cursor_correct();
3096 beginline(BL_SOL | BL_FIX);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003097 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003099
Bram Moolenaar860cae12010-06-05 23:22:07 +02003100 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003101do_check_cursorbind(void)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003102{
3103 linenr_T line = curwin->w_cursor.lnum;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003104 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003105 colnr_T coladd = curwin->w_cursor.coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003106 colnr_T curswant = curwin->w_curswant;
3107 int set_curswant = curwin->w_set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003108 win_T *old_curwin = curwin;
3109 buf_T *old_curbuf = curbuf;
Bram Moolenaar61452852011-02-01 18:01:11 +01003110 int restart_edit_save;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003111 int old_VIsual_select = VIsual_select;
3112 int old_VIsual_active = VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003113
3114 /*
3115 * loop through the cursorbound windows
3116 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003117 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003118 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003119 {
3120 curbuf = curwin->w_buffer;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003121 // skip original window and windows with 'noscrollbind'
Bram Moolenaar860cae12010-06-05 23:22:07 +02003122 if (curwin != old_curwin && curwin->w_p_crb)
3123 {
3124# ifdef FEAT_DIFF
3125 if (curwin->w_p_diff)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003126 curwin->w_cursor.lnum =
3127 diff_get_corresponding_line(old_curbuf, line);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003128 else
3129# endif
3130 curwin->w_cursor.lnum = line;
3131 curwin->w_cursor.col = col;
Bram Moolenaar1ea69b72012-03-16 19:24:26 +01003132 curwin->w_cursor.coladd = coladd;
Bram Moolenaar524780d2012-03-28 14:19:50 +02003133 curwin->w_curswant = curswant;
3134 curwin->w_set_curswant = set_curswant;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003135
Bram Moolenaar85a20022019-12-21 18:25:54 +01003136 // Make sure the cursor is in a valid position. Temporarily set
3137 // "restart_edit" to allow the cursor to be beyond the EOL.
Bram Moolenaar61452852011-02-01 18:01:11 +01003138 restart_edit_save = restart_edit;
3139 restart_edit = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003140 check_cursor();
Bram Moolenaara315ce12022-06-24 12:38:57 +01003141
3142 // Avoid a scroll here for the cursor position, 'scrollbind' is
3143 // more important.
3144 if (!curwin->w_p_scb)
3145 validate_cursor();
3146
Bram Moolenaar61452852011-02-01 18:01:11 +01003147 restart_edit = restart_edit_save;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003148 // Correct cursor for multi-byte character.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003149 if (has_mbyte)
3150 mb_adjust_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003151 redraw_later(UPD_VALID);
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003152
Bram Moolenaar85a20022019-12-21 18:25:54 +01003153 // Only scroll when 'scrollbind' hasn't done this.
Bram Moolenaarf3d419d2011-01-22 21:05:07 +01003154 if (!curwin->w_p_scb)
3155 update_topline();
Bram Moolenaar860cae12010-06-05 23:22:07 +02003156 curwin->w_redr_status = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003157 }
3158 }
3159
3160 /*
3161 * reset current-window
3162 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003163 VIsual_select = old_VIsual_select;
3164 VIsual_active = old_VIsual_active;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003165 curwin = old_curwin;
3166 curbuf = old_curbuf;
3167}