blob: 60b76d57cd2663f0ce4e0d91d28f1fc0c8ea8b73 [file] [log] [blame]
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
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/*
11 * drawscreen.c: Code for updating all the windows on the screen.
12 * This is the top level, drawline.c is the middle and screen.c the lower
13 * level.
14 *
15 * update_screen() is the function that updates all windows and status lines.
16 * It is called form the main loop when must_redraw is non-zero. It may be
17 * called from other places when an immediate screen update is needed.
18 *
19 * The part of the buffer that is displayed in a window is set with:
20 * - w_topline (first buffer line in window)
21 * - w_topfill (filler lines above the first line)
22 * - w_leftcol (leftmost window cell in window),
23 * - w_skipcol (skipped window cells of first line)
24 *
25 * Commands that only move the cursor around in a window, do not need to take
26 * action to update the display. The main loop will check if w_topline is
27 * valid and update it (scroll the window) when needed.
28 *
29 * Commands that scroll a window change w_topline and must call
30 * check_cursor() to move the cursor into the visible part of the window, and
Bram Moolenaara4d158b2022-08-14 14:17:45 +010031 * call redraw_later(UPD_VALID) to have the window displayed by update_screen()
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020032 * later.
33 *
34 * Commands that change text in the buffer must call changed_bytes() or
35 * changed_lines() to mark the area that changed and will require updating
36 * later. The main loop will call update_screen(), which will update each
37 * window that shows the changed buffer. This assumes text above the change
38 * can remain displayed as it is. Text after the change may need updating for
39 * scrolling, folding and syntax highlighting.
40 *
41 * Commands that change how a window is displayed (e.g., setting 'list') or
42 * invalidate the contents of a window in another way (e.g., change fold
Bram Moolenaara4d158b2022-08-14 14:17:45 +010043 * settings), must call redraw_later(UPD_NOT_VALID) to have the whole window
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020044 * redisplayed by update_screen() later.
45 *
46 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
Bram Moolenaara4d158b2022-08-14 14:17:45 +010047 * must call redraw_curbuf_later(UPD_NOT_VALID) to have all the windows for the
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020048 * buffer redisplayed by update_screen() later.
49 *
50 * Commands that change highlighting and possibly cause a scroll too must call
Bram Moolenaara4d158b2022-08-14 14:17:45 +010051 * redraw_later(UPD_SOME_VALID) to update the whole window but still use
52 * scrolling to avoid redrawing everything. But the length of displayed lines
53 * must not change, use UPD_NOT_VALID then.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020054 *
Bram Moolenaara4d158b2022-08-14 14:17:45 +010055 * Commands that move the window position must call redraw_later(UPD_NOT_VALID).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020056 * TODO: should minimize redrawing by scrolling when possible.
57 *
58 * Commands that change everything (e.g., resizing the screen) must call
Bram Moolenaara4d158b2022-08-14 14:17:45 +010059 * redraw_all_later(UPD_NOT_VALID) or redraw_all_later(UPD_CLEAR).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020060 *
61 * Things that are handled indirectly:
62 * - When messages scroll the screen up, msg_scrolled will be set and
63 * update_screen() called to redraw.
64 */
65
66#include "vim.h"
67
68static void win_update(win_T *wp);
69#ifdef FEAT_STL_OPT
70static void redraw_custom_statusline(win_T *wp);
71#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +010072#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
73static int did_update_one_window;
74#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020075
76/*
77 * Based on the current value of curwin->w_topline, transfer a screenfull
78 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
79 * Return OK when the screen was updated, FAIL if it was not done.
80 */
81 int
82update_screen(int type_arg)
83{
84 int type = type_arg;
85 win_T *wp;
86 static int did_intro = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020087#ifdef FEAT_GUI
Bram Moolenaare52e0c82020-02-28 22:20:10 +010088 int did_one = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020089 int did_undraw = FALSE;
90 int gui_cursor_col = 0;
91 int gui_cursor_row = 0;
92#endif
93 int no_update = FALSE;
Bram Moolenaare0c03c82021-04-23 21:01:34 +020094 int save_pum_will_redraw = pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020095
96 // Don't do anything if the screen structures are (not yet) valid.
97 if (!screen_valid(TRUE))
98 return FAIL;
99
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100100 if (type == UPD_VALID_NO_UPDATE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200101 {
102 no_update = TRUE;
103 type = 0;
104 }
105
106#ifdef FEAT_EVAL
107 {
108 buf_T *buf;
109
110 // Before updating the screen, notify any listeners of changed text.
111 FOR_ALL_BUFFERS(buf)
112 invoke_listeners(buf);
113 }
114#endif
115
116#ifdef FEAT_DIFF
117 // May have postponed updating diffs.
118 if (need_diff_redraw)
119 diff_redraw(TRUE);
120#endif
121
122 if (must_redraw)
123 {
124 if (type < must_redraw) // use maximal type
125 type = must_redraw;
126
127 // must_redraw is reset here, so that when we run into some weird
128 // reason to redraw while busy redrawing (e.g., asynchronous
129 // scrolling), or update_topline() in win_update() will cause a
130 // scroll, the screen will be redrawn later or in win_update().
131 must_redraw = 0;
132 }
133
134 // May need to update w_lines[].
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100135 if (curwin->w_lines_valid == 0 && type < UPD_NOT_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200136#ifdef FEAT_TERMINAL
137 && !term_do_update_window(curwin)
138#endif
139 )
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100140 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200141
142 // Postpone the redrawing when it's not needed and when being called
143 // recursively.
144 if (!redrawing() || updating_screen)
145 {
146 redraw_later(type); // remember type for next time
147 must_redraw = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100148 if (type > UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200149 curwin->w_lines_valid = 0; // don't use w_lines[].wl_size now
150 return FAIL;
151 }
152 updating_screen = TRUE;
153
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100154#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200155 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
156 // in some windows.
157 may_update_popup_mask(type);
158#endif
159
160#ifdef FEAT_SYN_HL
161 ++display_tick; // let syntax code know we're in a next round of
162 // display updating
163#endif
164 if (no_update)
165 ++no_win_do_lines_ins;
166
167 // if the screen was scrolled up when displaying a message, scroll it down
168 if (msg_scrolled)
169 {
170 clear_cmdline = TRUE;
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100171 if (type != UPD_CLEAR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200172 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100173 if (msg_scrolled > Rows - 5) // redrawing is faster
Bram Moolenaarb13d3402022-08-29 13:44:28 +0100174 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100175 type = UPD_NOT_VALID;
Bram Moolenaarb13d3402022-08-29 13:44:28 +0100176 redraw_as_cleared();
177 }
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100178 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200179 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100180 check_for_delay(FALSE);
181 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
182 == FAIL)
Bram Moolenaarb13d3402022-08-29 13:44:28 +0100183 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100184 type = UPD_NOT_VALID;
Bram Moolenaarb13d3402022-08-29 13:44:28 +0100185 redraw_as_cleared();
186 }
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100187 FOR_ALL_WINDOWS(wp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200188 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100189 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200190 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100191 if (W_WINROW(wp) + wp->w_height > msg_scrolled
192 && wp->w_redr_type < UPD_REDRAW_TOP
193 && wp->w_lines_valid > 0
194 && wp->w_topline == wp->w_lines[0].wl_lnum)
195 {
196 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
197 wp->w_redr_type = UPD_REDRAW_TOP;
198 }
199 else
200 {
201 wp->w_redr_type = UPD_NOT_VALID;
202 if (W_WINROW(wp) + wp->w_height
203 + wp->w_status_height <= msg_scrolled)
204 wp->w_redr_status = TRUE;
205 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200206 }
207 }
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100208 if (!no_update)
209 redraw_cmdline = TRUE;
210 redraw_tabline = TRUE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200211 }
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200212#if defined(FEAT_TABPANEL)
213 redraw_tabpanel = TRUE;
214#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200215 }
216 msg_scrolled = 0;
217 need_wait_return = FALSE;
218 }
219
220 // reset cmdline_row now (may have been changed temporarily)
221 compute_cmdrow();
222
223 // Check for changed highlighting
224 if (need_highlight_changed)
225 highlight_changed();
226
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100227 if (type == UPD_CLEAR) // first clear screen
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200228 {
229 screenclear(); // will reset clear_cmdline
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100230 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200231 // must_redraw may be set indirectly, avoid another redraw later
232 must_redraw = 0;
233 }
234
235 if (clear_cmdline) // going to clear cmdline (done below)
236 check_for_delay(FALSE);
237
238#ifdef FEAT_LINEBREAK
239 // Force redraw when width of 'number' or 'relativenumber' column
240 // changes.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100241 if (curwin->w_redr_type < UPD_NOT_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200242 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
243 ? number_width(curwin) : 0))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100244 curwin->w_redr_type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200245#endif
246
247 // Only start redrawing if there is really something to do.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100248 if (type == UPD_INVERTED)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200249 update_curswant();
250 if (curwin->w_redr_type < type
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100251 && !((type == UPD_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200252 && curwin->w_lines[0].wl_valid
253#ifdef FEAT_DIFF
254 && curwin->w_topfill == curwin->w_old_topfill
255 && curwin->w_botfill == curwin->w_old_botfill
256#endif
257 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100258 || (type == UPD_INVERTED
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200259 && VIsual_active
260 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
261 && curwin->w_old_visual_mode == VIsual_mode
262 && (curwin->w_valid & VALID_VIRTCOL)
263 && curwin->w_old_curswant == curwin->w_curswant)
264 ))
265 curwin->w_redr_type = type;
266
267 // Redraw the tab pages line if needed.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100268 if (redraw_tabline || type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200269 draw_tabline();
270
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200271#if defined(FEAT_TABPANEL)
272 if (redraw_tabpanel || type >= UPD_NOT_VALID)
273 draw_tabpanel();
274#endif
275
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200276#ifdef FEAT_SYN_HL
277 // Correct stored syntax highlighting info for changes in each displayed
278 // buffer. Each buffer must only be done once.
279 FOR_ALL_WINDOWS(wp)
280 {
281 if (wp->w_buffer->b_mod_set)
282 {
283 win_T *wwp;
284
285 // Check if we already did this buffer.
286 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
287 if (wwp->w_buffer == wp->w_buffer)
288 break;
289 if (wwp == wp && syntax_present(wp))
290 syn_stack_apply_changes(wp->w_buffer);
291 }
292 }
293#endif
294
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200295 if (pum_redraw_in_same_position())
296 // Avoid flicker if the popup menu is going to be redrawn in the same
297 // position.
298 pum_will_redraw = TRUE;
299
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200300 // Go from top to bottom through the windows, redrawing the ones that need
301 // it.
302#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100303 did_update_one_window = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200304#endif
305#ifdef FEAT_SEARCH_EXTRA
306 screen_search_hl.rm.regprog = NULL;
307#endif
308 FOR_ALL_WINDOWS(wp)
309 {
310 if (wp->w_redr_type != 0)
311 {
312 cursor_off();
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100313#ifdef FEAT_GUI
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200314 if (!did_one)
315 {
316 did_one = TRUE;
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100317
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200318 // Remove the cursor before starting to do anything, because
319 // scrolling may make it difficult to redraw the text under
320 // it.
Bram Moolenaar09f067f2021-04-11 13:29:18 +0200321 // Also remove the cursor if it needs to be hidden due to an
322 // ongoing cursor-less sleep.
323 if (gui.in_use && (wp == curwin || cursor_is_sleeping()))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200324 {
325 gui_cursor_col = gui.cursor_col;
326 gui_cursor_row = gui.cursor_row;
327 gui_undraw_cursor();
328 did_undraw = TRUE;
329 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200330 }
331#endif
332 win_update(wp);
333 }
334
335 // redraw status line after the window to minimize cursor movement
336 if (wp->w_redr_status)
337 {
338 cursor_off();
339 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
340 }
341 }
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200342#if defined(FEAT_TABPANEL)
343 if (redraw_tabpanel)
344 draw_tabpanel();
345#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200346#if defined(FEAT_SEARCH_EXTRA)
347 end_search_hl();
348#endif
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200349
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200350 // May need to redraw the popup menu.
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200351 pum_will_redraw = save_pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200352 pum_may_redraw();
353
354 // Reset b_mod_set flags. Going through all windows is probably faster
355 // than going through all buffers (there could be many buffers).
356 FOR_ALL_WINDOWS(wp)
357 wp->w_buffer->b_mod_set = FALSE;
358
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100359#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200360 // Display popup windows on top of the windows and command line.
361 update_popups(win_update);
362#endif
363
Bram Moolenaar3194e5b2021-12-13 21:59:09 +0000364#ifdef FEAT_TERMINAL
365 FOR_ALL_WINDOWS(wp)
366 // If this window contains a terminal, after redrawing all windows, the
367 // dirty row range can be reset.
368 term_did_update_window(wp);
369#endif
370
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200371 after_updating_screen(TRUE);
372
373 // Clear or redraw the command line. Done last, because scrolling may
374 // mess up the command line.
375 if (clear_cmdline || redraw_cmdline || redraw_mode)
376 showmode();
377
378 if (no_update)
379 --no_win_do_lines_ins;
380
381 // May put up an introductory message when not editing a file
382 if (!did_intro)
383 maybe_intro_message();
384 did_intro = TRUE;
385
386#ifdef FEAT_GUI
387 // Redraw the cursor and update the scrollbars when all screen updating is
388 // done.
389 if (gui.in_use)
390 {
391 if (did_undraw && !gui_mch_is_blink_off())
392 {
393 mch_disable_flush();
394 out_flush(); // required before updating the cursor
395 mch_enable_flush();
396
397 // Put the GUI position where the cursor was, gui_update_cursor()
398 // uses that.
399 gui.col = gui_cursor_col;
400 gui.row = gui_cursor_row;
401 gui.col = mb_fix_col(gui.col, gui.row);
402 gui_update_cursor(FALSE, FALSE);
403 gui_may_flush();
404 screen_cur_col = gui.col;
405 screen_cur_row = gui.row;
406 }
407 else
408 out_flush();
409 gui_update_scrollbars(FALSE);
410 }
411#endif
412 return OK;
413}
414
415/*
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200416 * Return the row for drawing the statusline and the ruler of window "wp".
417 */
Bram Moolenaar49c51b82021-04-01 16:16:18 +0200418 int
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200419statusline_row(win_T *wp)
420{
421#if defined(FEAT_PROP_POPUP)
422 // If the window is really zero height the winbar isn't displayed.
423 if (wp->w_frame->fr_height == wp->w_status_height && !popup_is_popup(wp))
424 return wp->w_winrow;
425#endif
426 return W_WINROW(wp) + wp->w_height;
427}
428
429/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200430 * Redraw the status line of window wp.
431 *
432 * If inversion is possible we use it. Else '=' characters are used.
433 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
434 * displayed.
435 */
Luuk van Baalba936f62022-12-15 13:15:39 +0000436 void
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200437win_redr_status(win_T *wp, int ignore_pum UNUSED)
438{
439 int row;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200440 int fillchar;
441 int attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200442 static int busy = FALSE;
443
444 // It's possible to get here recursively when 'statusline' (indirectly)
445 // invokes ":redrawstatus". Simply ignore the call then.
446 if (busy)
447 return;
448 busy = TRUE;
449
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200450 row = statusline_row(wp);
451
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200452 wp->w_redr_status = FALSE;
453 if (wp->w_status_height == 0)
454 {
455 // no status line, can only be last window
456 redraw_cmdline = TRUE;
457 }
458 else if (!redrawing()
459 // don't update status line when popup menu is visible and may be
460 // drawn over it, unless it will be redrawn later
461 || (!ignore_pum && pum_visible()))
462 {
463 // Don't redraw right now, do it later.
464 wp->w_redr_status = TRUE;
465 }
466#ifdef FEAT_STL_OPT
467 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
468 {
469 // redraw custom status line
470 redraw_custom_statusline(wp);
471 }
472#endif
473 else
474 {
John Marriotta21240b2025-01-08 20:10:59 +0100475 char_u *p;
476 int plen;
477 int NameBufflen;
478 int this_ru_col;
479 int n; // scratch value
480
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200481 fillchar = fillchar_status(&attr, wp);
482
483 get_trans_bufname(wp->w_buffer);
484 p = NameBuff;
John Marriotta21240b2025-01-08 20:10:59 +0100485 plen = (int)STRLEN(p);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200486
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000487 if ((bt_help(wp->w_buffer)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200488#ifdef FEAT_QUICKFIX
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000489 || wp->w_p_pvw
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200490#endif
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000491 || bufIsChanged(wp->w_buffer)
492 || wp->w_buffer->b_p_ro)
John Marriotta21240b2025-01-08 20:10:59 +0100493 && plen < MAXPATHL - 1)
John Marriott70dfc372025-01-16 18:58:20 +0100494 {
495 *(p + plen++) = ' '; // replace NUL with space
496 *(p + plen) = NUL; // NUL terminate the string
497 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200498 if (bt_help(wp->w_buffer))
John Marriotta21240b2025-01-08 20:10:59 +0100499 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[Help]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200500#ifdef FEAT_QUICKFIX
501 if (wp->w_p_pvw)
John Marriotta21240b2025-01-08 20:10:59 +0100502 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[Preview]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200503#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100504 if (bufIsChanged(wp->w_buffer) && !bt_terminal(wp->w_buffer))
John Marriotta21240b2025-01-08 20:10:59 +0100505 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", "[+]");
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200506 if (wp->w_buffer->b_p_ro)
John Marriotta21240b2025-01-08 20:10:59 +0100507 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[RO]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200508
509 this_ru_col = ru_col - (Columns - wp->w_width);
John Marriotta21240b2025-01-08 20:10:59 +0100510 n = (wp->w_width + 1) / 2;
511 if (this_ru_col < n)
512 this_ru_col = n;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200513 if (this_ru_col <= 1)
514 {
515 p = (char_u *)"<"; // No room for file name!
John Marriotta21240b2025-01-08 20:10:59 +0100516 plen = 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200517 }
518 else if (has_mbyte)
519 {
John Marriotta21240b2025-01-08 20:10:59 +0100520 int i;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200521
522 // Count total number of display cells.
John Marriotta21240b2025-01-08 20:10:59 +0100523 plen = mb_string2cells(p, -1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200524
525 // Find first character that will fit.
526 // Going from start to end is much faster for DBCS.
John Marriotta21240b2025-01-08 20:10:59 +0100527 for (i = 0; p[i] != NUL && plen >= this_ru_col - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200528 i += (*mb_ptr2len)(p + i))
John Marriotta21240b2025-01-08 20:10:59 +0100529 plen -= (*mb_ptr2cells)(p + i);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200530 if (i > 0)
531 {
532 p = p + i - 1;
533 *p = '<';
John Marriotta21240b2025-01-08 20:10:59 +0100534 ++plen;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200535 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200536 }
John Marriotta21240b2025-01-08 20:10:59 +0100537 else if (plen > this_ru_col - 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200538 {
John Marriotta21240b2025-01-08 20:10:59 +0100539 p += plen - (this_ru_col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200540 *p = '<';
John Marriotta21240b2025-01-08 20:10:59 +0100541 plen = this_ru_col - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200542 }
543
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200544 screen_puts(p, row, wp->w_wincol, attr);
545 screen_fill(row, row + 1, plen + wp->w_wincol,
546 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
John Marriotta21240b2025-01-08 20:10:59 +0100547 if ((NameBufflen = get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)) > 0
548 && (this_ru_col - plen) > (NameBufflen + 1))
549 screen_puts(NameBuff, row, (int)(this_ru_col - NameBufflen
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200550 - 1 + wp->w_wincol), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200551
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200552 win_redr_ruler(wp, TRUE, ignore_pum);
Luuk van Baalba936f62022-12-15 13:15:39 +0000553
554 // Draw the 'showcmd' information if 'showcmdloc' == "statusline".
555 if (p_sc && *p_sloc == 's')
556 {
John Marriotta21240b2025-01-08 20:10:59 +0100557 n = this_ru_col - plen - 2; // perform the calculation here so we only do it once
558 int width = MIN(10, n);
Luuk van Baalba936f62022-12-15 13:15:39 +0000559
560 if (width > 0)
561 screen_puts_len(showcmd_buf, width, row,
562 wp->w_wincol + this_ru_col - width - 1, attr);
563 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200564 }
565
566 /*
567 * May need to draw the character below the vertical separator.
568 */
569 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
570 {
571 if (stl_connected(wp))
572 fillchar = fillchar_status(&attr, wp);
573 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100574 fillchar = fillchar_vsep(&attr, wp);
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200575 screen_putchar(fillchar, row, W_ENDCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200576 }
577 busy = FALSE;
578}
579
580#ifdef FEAT_STL_OPT
581/*
582 * Redraw the status line according to 'statusline' and take care of any
583 * errors encountered.
584 */
585 static void
586redraw_custom_statusline(win_T *wp)
587{
588 static int entered = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200589
590 // When called recursively return. This can happen when the statusline
591 // contains an expression that triggers a redraw.
592 if (entered)
593 return;
594 entered = TRUE;
595
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200596 win_redr_custom(wp, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200597 entered = FALSE;
598}
599#endif
600
601/*
602 * Show current status info in ruler and various other places
603 * If always is FALSE, only show ruler if position has changed.
604 */
605 void
606showruler(int always)
607{
608 if (!always && !redrawing())
609 return;
610 if (pum_visible())
611 {
612 // Don't redraw right now, do it later.
613 curwin->w_redr_status = TRUE;
614 return;
615 }
616#if defined(FEAT_STL_OPT)
617 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
618 redraw_custom_statusline(curwin);
619 else
620#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200621 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200622
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200623 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000624#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200625 || (p_icon && (stl_syntax & STL_IN_ICON))
626 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000627#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200628 )
629 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000630
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200631 // Redraw the tab pages line if needed.
632 if (redraw_tabline)
633 draw_tabline();
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200634
635#if defined(FEAT_TABPANEL)
636 if (redraw_tabpanel)
637 draw_tabpanel();
638#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200639}
640
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200641 void
642win_redr_ruler(win_T *wp, int always, int ignore_pum)
643{
John Marriotta21240b2025-01-08 20:10:59 +0100644 int empty_line = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200645
Bram Moolenaara2a89732022-08-31 14:46:18 +0100646 // If 'ruler' off don't do anything
647 if (!p_ru)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200648 return;
649
650 /*
651 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
652 * after deleting lines, before cursor.lnum is corrected.
653 */
654 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
655 return;
656
657 // Don't draw the ruler while doing insert-completion, it might overwrite
658 // the (long) mode message.
659 if (wp == lastwin && lastwin->w_status_height == 0)
660 if (edit_submode != NULL)
661 return;
John Marriotta21240b2025-01-08 20:10:59 +0100662
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200663 // Don't draw the ruler when the popup menu is visible, it may overlap.
664 // Except when the popup menu will be redrawn anyway.
665 if (!ignore_pum && pum_visible())
666 return;
667
668#ifdef FEAT_STL_OPT
Bram Moolenaara2a89732022-08-31 14:46:18 +0100669 if (*p_ruf)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200670 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200671 win_redr_custom(wp, TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200672 return;
673 }
674#endif
675
676 /*
677 * Check if not in Insert mode and the line is empty (will show "0-1").
678 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100679 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200680 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
681 empty_line = TRUE;
682
683 /*
684 * Only draw the ruler when something changed.
685 */
686 validate_virtcol_win(wp);
687 if ( redraw_cmdline
688 || always
689 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
690 || wp->w_cursor.col != wp->w_ru_cursor.col
691 || wp->w_virtcol != wp->w_ru_virtcol
692 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
693 || wp->w_topline != wp->w_ru_topline
694 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
695#ifdef FEAT_DIFF
696 || wp->w_topfill != wp->w_ru_topfill
697#endif
698 || empty_line != wp->w_ru_empty)
699 {
John Marriotta21240b2025-01-08 20:10:59 +0100700 int row;
701 int fillchar;
702 int attr;
703 int off;
704 int width;
705 colnr_T virtcol;
706#define RULER_BUF_LEN 70
707 char_u buffer[RULER_BUF_LEN];
708 int bufferlen;
709 char_u rel_pos[RULER_BUF_LEN];
710 int rel_poslen;
711 int this_ru_col;
712 int n1; // scratch value
713 int n2; // scratch value
714
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200715 cursor_off();
716 if (wp->w_status_height)
717 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200718 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200719 fillchar = fillchar_status(&attr, wp);
720 off = wp->w_wincol;
721 width = wp->w_width;
722 }
723 else
724 {
725 row = Rows - 1;
726 fillchar = ' ';
727 attr = 0;
728 width = Columns;
729 off = 0;
730 }
731
732 // In list mode virtcol needs to be recomputed
733 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100734 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200735 {
736 wp->w_p_list = FALSE;
737 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
738 wp->w_p_list = TRUE;
739 }
740
John Marriotta21240b2025-01-08 20:10:59 +0100741 bufferlen = vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200742 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
743 ? 0L
744 : (long)(wp->w_cursor.lnum));
John Marriotta21240b2025-01-08 20:10:59 +0100745 bufferlen += col_print(buffer + bufferlen, RULER_BUF_LEN - bufferlen,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200746 empty_line ? 0 : (int)wp->w_cursor.col + 1,
747 (int)virtcol + 1);
748
749 /*
750 * Add a "50%" if there is room for it.
751 * On the last line, don't print in the last column (scrolls the
752 * screen up on some terminals).
753 */
John Marriotta21240b2025-01-08 20:10:59 +0100754 rel_poslen = get_rel_pos(wp, rel_pos, RULER_BUF_LEN);
755 n1 = bufferlen + vim_strsize(rel_pos);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200756 if (wp->w_status_height == 0) // can't use last char of screen
John Marriotta21240b2025-01-08 20:10:59 +0100757 ++n1;
758
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200759 this_ru_col = ru_col - (Columns - width);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200760 // Never use more than half the window/screen width, leave the other
761 // half for the filename.
John Marriotta21240b2025-01-08 20:10:59 +0100762 n2 = (width + 1) / 2;
763 if (this_ru_col < n2)
764 this_ru_col = n2;
765 if (this_ru_col + n1 < width)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200766 {
John Marriotta21240b2025-01-08 20:10:59 +0100767 // need at least space for rel_pos + NUL
768 while (this_ru_col + n1 < width
769 && RULER_BUF_LEN > bufferlen + rel_poslen + 1) // +1 for NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200770 {
771 if (has_mbyte)
John Marriotta21240b2025-01-08 20:10:59 +0100772 bufferlen += (*mb_char2bytes)(fillchar, buffer + bufferlen);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200773 else
John Marriotta21240b2025-01-08 20:10:59 +0100774 buffer[bufferlen++] = fillchar;
775 ++n1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200776 }
John Marriotta21240b2025-01-08 20:10:59 +0100777 bufferlen += vim_snprintf((char *)buffer + bufferlen, RULER_BUF_LEN - bufferlen,
778 "%s", rel_pos);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200779 }
780 // Truncate at window boundary.
781 if (has_mbyte)
782 {
John Marriotta21240b2025-01-08 20:10:59 +0100783 for (n1 = 0, n2 = 0; buffer[n1] != NUL; n1 += (*mb_ptr2len)(buffer + n1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200784 {
John Marriotta21240b2025-01-08 20:10:59 +0100785 n2 += (*mb_ptr2cells)(buffer + n1);
786 if (this_ru_col + n2 > width)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200787 {
John Marriotta21240b2025-01-08 20:10:59 +0100788 bufferlen = n1;
789 buffer[bufferlen] = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200790 break;
791 }
792 }
793 }
John Marriotta21240b2025-01-08 20:10:59 +0100794 else if (this_ru_col + bufferlen > width)
795 {
796 bufferlen = width - this_ru_col;
797 buffer[bufferlen] = NUL;
798 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200799
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200800 screen_puts(buffer, row, this_ru_col + off, attr);
John Marriotta21240b2025-01-08 20:10:59 +0100801 n1 = redraw_cmdline;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200802 screen_fill(row, row + 1,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +0200803 this_ru_col + off + bufferlen,
804 (off + width),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200805 fillchar, fillchar, attr);
806 // don't redraw the cmdline because of showing the ruler
John Marriotta21240b2025-01-08 20:10:59 +0100807 redraw_cmdline = n1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200808 wp->w_ru_cursor = wp->w_cursor;
809 wp->w_ru_virtcol = wp->w_virtcol;
810 wp->w_ru_empty = empty_line;
811 wp->w_ru_topline = wp->w_topline;
812 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
813#ifdef FEAT_DIFF
814 wp->w_ru_topfill = wp->w_topfill;
815#endif
816 }
817}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200818
819/*
820 * To be called when "updating_screen" was set before and now the postponed
821 * side effects may take place.
822 */
823 void
824after_updating_screen(int may_resize_shell UNUSED)
825{
826 updating_screen = FALSE;
827#ifdef FEAT_GUI
828 if (may_resize_shell)
829 gui_may_resize_shell();
830#endif
831#ifdef FEAT_TERMINAL
832 term_check_channel_closed_recently();
833#endif
834
835#ifdef HAVE_DROP_FILE
836 // If handle_drop() was called while updating_screen was TRUE need to
837 // handle the drop now.
838 handle_any_postponed_drop();
839#endif
840}
841
842/*
843 * Update all windows that are editing the current buffer.
844 */
845 void
846update_curbuf(int type)
847{
848 redraw_curbuf_later(type);
849 update_screen(type);
850}
851
852#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
853/*
854 * Copy "text" to ScreenLines using "attr".
855 * Returns the next screen column.
856 */
857 static int
858text_to_screenline(win_T *wp, char_u *text, int col)
859{
860 int off = (int)(current_ScreenLine - ScreenLines);
861
862 if (has_mbyte)
863 {
864 int cells;
865 int u8c, u8cc[MAX_MCO];
866 int i;
867 int idx;
868 int c_len;
869 char_u *p;
870# ifdef FEAT_ARABIC
871 int prev_c = 0; // previous Arabic character
872 int prev_c1 = 0; // first composing char for prev_c
873# endif
874
875# ifdef FEAT_RIGHTLEFT
876 if (wp->w_p_rl)
877 idx = off;
878 else
879# endif
880 idx = off + col;
881
882 // Store multibyte characters in ScreenLines[] et al. correctly.
883 for (p = text; *p != NUL; )
884 {
885 cells = (*mb_ptr2cells)(p);
886 c_len = (*mb_ptr2len)(p);
887 if (col + cells > wp->w_width
888# ifdef FEAT_RIGHTLEFT
889 - (wp->w_p_rl ? col : 0)
890# endif
891 )
892 break;
893 ScreenLines[idx] = *p;
894 if (enc_utf8)
895 {
896 u8c = utfc_ptr2char(p, u8cc);
897 if (*p < 0x80 && u8cc[0] == 0)
898 {
899 ScreenLinesUC[idx] = 0;
900#ifdef FEAT_ARABIC
901 prev_c = u8c;
902#endif
903 }
904 else
905 {
906#ifdef FEAT_ARABIC
907 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
908 {
909 // Do Arabic shaping.
910 int pc, pc1, nc;
911 int pcc[MAX_MCO];
912 int firstbyte = *p;
913
914 // The idea of what is the previous and next
915 // character depends on 'rightleft'.
916 if (wp->w_p_rl)
917 {
918 pc = prev_c;
919 pc1 = prev_c1;
920 nc = utf_ptr2char(p + c_len);
921 prev_c1 = u8cc[0];
922 }
923 else
924 {
925 pc = utfc_ptr2char(p + c_len, pcc);
926 nc = prev_c;
927 pc1 = pcc[0];
928 }
929 prev_c = u8c;
930
931 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
932 pc, pc1, nc);
933 ScreenLines[idx] = firstbyte;
934 }
935 else
936 prev_c = u8c;
937#endif
938 // Non-BMP character: display as ? or fullwidth ?.
939 ScreenLinesUC[idx] = u8c;
940 for (i = 0; i < Screen_mco; ++i)
941 {
942 ScreenLinesC[i][idx] = u8cc[i];
943 if (u8cc[i] == 0)
944 break;
945 }
946 }
947 if (cells > 1)
948 ScreenLines[idx + 1] = 0;
949 }
950 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
951 // double-byte single width character
952 ScreenLines2[idx] = p[1];
953 else if (cells > 1)
954 // double-width character
955 ScreenLines[idx + 1] = p[1];
956 col += cells;
957 idx += cells;
958 p += c_len;
959 }
960 }
961 else
962 {
963 int len = (int)STRLEN(text);
John Marriotta21240b2025-01-08 20:10:59 +0100964 int n = wp->w_width - col;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200965
John Marriotta21240b2025-01-08 20:10:59 +0100966 if (len > n)
967 len = n;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200968 if (len > 0)
969 {
970#ifdef FEAT_RIGHTLEFT
971 if (wp->w_p_rl)
972 mch_memmove(current_ScreenLine, text, len);
973 else
974#endif
975 mch_memmove(current_ScreenLine + col, text, len);
976 col += len;
977 }
978 }
979 return col;
980}
981#endif
982
983#ifdef FEAT_MENU
984/*
985 * Draw the window toolbar.
986 */
987 static void
988redraw_win_toolbar(win_T *wp)
989{
990 vimmenu_T *menu;
991 int item_idx = 0;
992 int item_count = 0;
993 int col = 0;
994 int next_col;
995 int off = (int)(current_ScreenLine - ScreenLines);
996 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
997 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
998
999 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001000 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001001 ++item_count;
1002 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
1003
1004 // TODO: use fewer spaces if there is not enough room
1005 for (menu = wp->w_winbar->children;
1006 menu != NULL && col < wp->w_width; menu = menu->next)
1007 {
1008 space_to_screenline(off + col, fill_attr);
1009 if (++col >= wp->w_width)
1010 break;
1011 if (col > 1)
1012 {
1013 space_to_screenline(off + col, fill_attr);
1014 if (++col >= wp->w_width)
1015 break;
1016 }
1017
1018 wp->w_winbar_items[item_idx].wb_startcol = col;
1019 space_to_screenline(off + col, button_attr);
1020 if (++col >= wp->w_width)
1021 break;
1022
1023 next_col = text_to_screenline(wp, menu->name, col);
1024 while (col < next_col)
1025 {
1026 ScreenAttrs[off + col] = button_attr;
1027 ++col;
1028 }
1029 wp->w_winbar_items[item_idx].wb_endcol = col;
1030 wp->w_winbar_items[item_idx].wb_menu = menu;
1031 ++item_idx;
1032
1033 if (col >= wp->w_width)
1034 break;
1035 space_to_screenline(off + col, button_attr);
1036 ++col;
1037 }
1038 while (col < wp->w_width)
1039 {
1040 space_to_screenline(off + col, fill_attr);
1041 ++col;
1042 }
1043 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1044
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001045 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width,
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02001046 wp->w_width, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001047}
1048#endif
1049
1050#if defined(FEAT_FOLDING) || defined(PROTO)
1051/*
1052 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1053 */
1054 static void
1055copy_text_attr(
1056 int off,
1057 char_u *buf,
1058 int len,
1059 int attr)
1060{
1061 int i;
1062
1063 mch_memmove(ScreenLines + off, buf, (size_t)len);
1064 if (enc_utf8)
1065 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1066 for (i = 0; i < len; ++i)
1067 ScreenAttrs[off + i] = attr;
1068}
1069
1070/*
1071 * Display one folded line.
1072 */
1073 static void
1074fold_line(
1075 win_T *wp,
1076 long fold_count,
1077 foldinfo_T *foldinfo,
1078 linenr_T lnum,
1079 int row)
1080{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001081 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1082 // multi-byte character is MAX_MCO.
1083 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001084 pos_T *top, *bot;
1085 linenr_T lnume = lnum + fold_count - 1;
1086 int len;
1087 char_u *text;
1088 int fdc;
1089 int col;
1090 int txtcol;
1091 int off = (int)(current_ScreenLine - ScreenLines);
1092 int ri;
1093
1094 // Build the fold line:
1095 // 1. Add the cmdwin_type for the command-line window
1096 // 2. Add the 'foldcolumn'
1097 // 3. Add the 'number' or 'relativenumber' column
1098 // 4. Compose the text
1099 // 5. Add the text
1100 // 6. set highlighting for the Visual area an other text
1101 col = 0;
1102
1103 // 1. Add the cmdwin_type for the command-line window
1104 // Ignores 'rightleft', this window is never right-left.
Sean Dewar988f7432023-08-16 14:17:36 +01001105 if (wp == cmdwin_win)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001106 {
1107 ScreenLines[off] = cmdwin_type;
1108 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1109 if (enc_utf8)
1110 ScreenLinesUC[off] = 0;
1111 ++col;
1112 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001113
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001114#ifdef FEAT_RIGHTLEFT
1115# define RL_MEMSET(p, v, l) \
1116 do { \
1117 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001118 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001119 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1120 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001121 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001122 ScreenAttrs[off + (p) + ri] = v; \
1123 } while (0)
1124#else
1125# define RL_MEMSET(p, v, l) \
1126 do { \
1127 for (ri = 0; ri < l; ++ri) \
1128 ScreenAttrs[off + (p) + ri] = v; \
1129 } while (0)
1130#endif
1131
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001132 // 2. Add the 'foldcolumn'
1133 // Reduce the width when there is not enough space.
1134 fdc = compute_foldcolumn(wp, col);
1135 if (fdc > 0)
1136 {
1137 char_u *p;
1138 int i;
1139 int idx;
1140
1141 fill_foldcolumn(buf, wp, TRUE, lnum);
1142 p = buf;
1143 for (i = 0; i < fdc; i++)
1144 {
1145 int ch;
1146
1147 if (has_mbyte)
1148 ch = mb_ptr2char_adv(&p);
1149 else
1150 ch = *p++;
1151#ifdef FEAT_RIGHTLEFT
1152 if (wp->w_p_rl)
1153 idx = off + wp->w_width - i - 1 - col;
1154 else
1155#endif
1156 idx = off + col + i;
1157 if (enc_utf8)
1158 {
1159 if (ch >= 0x80)
1160 {
1161 ScreenLinesUC[idx] = ch;
1162 ScreenLinesC[0][idx] = 0;
1163 ScreenLines[idx] = 0x80;
1164 }
1165 else
1166 {
1167 ScreenLines[idx] = ch;
1168 ScreenLinesUC[idx] = 0;
1169 }
1170 }
1171 else
1172 ScreenLines[idx] = ch;
1173 }
1174
1175 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1176 col += fdc;
1177 }
1178
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001179 // Set all attributes of the 'number' or 'relativenumber' column and the
1180 // text
1181 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1182
1183#ifdef FEAT_SIGNS
1184 // If signs are being displayed, add two spaces.
1185 if (signcolumn_on(wp))
1186 {
1187 len = wp->w_width - col;
1188 if (len > 0)
1189 {
1190 if (len > 2)
1191 len = 2;
1192# ifdef FEAT_RIGHTLEFT
1193 if (wp->w_p_rl)
1194 // the line number isn't reversed
1195 copy_text_attr(off + wp->w_width - len - col,
1196 (char_u *)" ", len, HL_ATTR(HLF_FL));
1197 else
1198# endif
1199 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1200 col += len;
1201 }
1202 }
1203#endif
1204
1205 // 3. Add the 'number' or 'relativenumber' column
1206 if (wp->w_p_nu || wp->w_p_rnu)
1207 {
1208 len = wp->w_width - col;
1209 if (len > 0)
1210 {
1211 int w = number_width(wp);
1212 long num;
1213 char *fmt = "%*ld ";
1214
1215 if (len > w + 1)
1216 len = w + 1;
1217
1218 if (wp->w_p_nu && !wp->w_p_rnu)
1219 // 'number' + 'norelativenumber'
1220 num = (long)lnum;
1221 else
1222 {
1223 // 'relativenumber', don't use negative numbers
1224 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1225 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1226 {
1227 // 'number' + 'relativenumber': cursor line shows absolute
1228 // line number
1229 num = lnum;
1230 fmt = "%-*ld ";
1231 }
1232 }
1233
John Marriotta21240b2025-01-08 20:10:59 +01001234 vim_snprintf((char *)buf, sizeof(buf), fmt, w, num);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001235#ifdef FEAT_RIGHTLEFT
1236 if (wp->w_p_rl)
1237 // the line number isn't reversed
1238 copy_text_attr(off + wp->w_width - len - col, buf, len,
1239 HL_ATTR(HLF_FL));
1240 else
1241#endif
1242 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1243 col += len;
1244 }
1245 }
1246
1247 // 4. Compose the folded-line string with 'foldtext', if set.
1248 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1249
1250 txtcol = col; // remember where text starts
1251
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001252 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1253 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001254 // Right-left text is put in columns 0 - number-col, normal text is put
1255 // in columns number-col - window-width.
1256 col = text_to_screenline(wp, text, col);
1257
1258 // Fill the rest of the line with the fold filler
1259#ifdef FEAT_RIGHTLEFT
1260 if (wp->w_p_rl)
1261 col -= txtcol;
1262#endif
1263 while (col < wp->w_width
1264#ifdef FEAT_RIGHTLEFT
1265 - (wp->w_p_rl ? txtcol : 0)
1266#endif
1267 )
1268 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001269 int c = wp->w_fill_chars.fold;
1270
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001271 if (enc_utf8)
1272 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001273 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001274 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001275 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001276 ScreenLinesC[0][off + col] = 0;
1277 ScreenLines[off + col] = 0x80; // avoid storing zero
1278 }
1279 else
1280 {
1281 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001282 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001283 }
1284 col++;
1285 }
1286 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001287 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001288 }
1289
1290 if (text != buf)
1291 vim_free(text);
1292
1293 // 6. set highlighting for the Visual area an other text.
1294 // If all folded lines are in the Visual area, highlight the line.
1295 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1296 {
1297 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1298 {
1299 // Visual is after curwin->w_cursor
1300 top = &curwin->w_cursor;
1301 bot = &VIsual;
1302 }
1303 else
1304 {
1305 // Visual is before curwin->w_cursor
1306 top = &VIsual;
1307 bot = &curwin->w_cursor;
1308 }
1309 if (lnum >= top->lnum
1310 && lnume <= bot->lnum
1311 && (VIsual_mode != 'v'
1312 || ((lnum > top->lnum
1313 || (lnum == top->lnum
1314 && top->col == 0))
1315 && (lnume < bot->lnum
1316 || (lnume == bot->lnum
1317 && (bot->col - (*p_sel == 'e'))
zeertzjq94b7c322024-03-12 21:50:32 +01001318 >= ml_get_buf_len(wp->w_buffer, lnume))))))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001319 {
1320 if (VIsual_mode == Ctrl_V)
1321 {
1322 // Visual block mode: highlight the chars part of the block
1323 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1324 {
1325 if (wp->w_old_cursor_lcol != MAXCOL
1326 && wp->w_old_cursor_lcol + txtcol
1327 < (colnr_T)wp->w_width)
1328 len = wp->w_old_cursor_lcol;
1329 else
1330 len = wp->w_width - txtcol;
1331 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1332 len - (int)wp->w_old_cursor_fcol);
1333 }
1334 }
1335 else
1336 {
1337 // Set all attributes of the text
1338 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1339 }
1340 }
1341 }
1342
1343#ifdef FEAT_SYN_HL
1344 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1345 if (wp->w_p_cc_cols)
1346 {
1347 int i = 0;
1348 int j = wp->w_p_cc_cols[i];
1349 int old_txtcol = txtcol;
1350
1351 while (j > -1)
1352 {
1353 txtcol += j;
1354 if (wp->w_p_wrap)
1355 txtcol -= wp->w_skipcol;
1356 else
1357 txtcol -= wp->w_leftcol;
1358 if (txtcol >= 0 && txtcol < wp->w_width)
1359 ScreenAttrs[off + txtcol] = hl_combine_attr(
1360 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1361 txtcol = old_txtcol;
1362 j = wp->w_p_cc_cols[++i];
1363 }
1364 }
1365
1366 // Show 'cursorcolumn' in the fold line.
1367 if (wp->w_p_cuc)
1368 {
1369 txtcol += wp->w_virtcol;
1370 if (wp->w_p_wrap)
1371 txtcol -= wp->w_skipcol;
1372 else
1373 txtcol -= wp->w_leftcol;
1374 if (txtcol >= 0 && txtcol < wp->w_width)
1375 ScreenAttrs[off + txtcol] = hl_combine_attr(
1376 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1377 }
1378#endif
1379
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02001380 screen_line(wp, row + W_WINROW(wp), wp->w_wincol, wp->w_width, wp->w_width,
1381 -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001382
1383 // Update w_cline_height and w_cline_folded if the cursor line was
1384 // updated (saves a call to plines() later).
1385 if (wp == curwin
1386 && lnum <= curwin->w_cursor.lnum
1387 && lnume >= curwin->w_cursor.lnum)
1388 {
1389 curwin->w_cline_row = row;
1390 curwin->w_cline_height = 1;
1391 curwin->w_cline_folded = TRUE;
1392 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1393 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001394
1395# ifdef FEAT_CONCEAL
1396 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001397 if (wp == curwin
1398 && wp->w_cursor.lnum >= lnum
1399 && wp->w_cursor.lnum <= lnume
1400 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001401 curs_columns(TRUE);
1402# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001403}
1404#endif
1405
1406/*
1407 * Update a single window.
1408 *
1409 * This may cause the windows below it also to be redrawn (when clearing the
1410 * screen or scrolling lines).
1411 *
1412 * How the window is redrawn depends on wp->w_redr_type. Each type also
1413 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001414 * UPD_NOT_VALID redraw the whole window
1415 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1416 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1417 * UPD_VALID
1418 * UPD_INVERTED redraw the changed part of the Visual area
1419 * UPD_INVERTED_ALL redraw the whole Visual area
1420 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001421 * 2. update lines at the top when scrolled down
1422 * 3. redraw changed text:
1423 * - if wp->w_buffer->b_mod_set set, update lines between
1424 * b_mod_top and b_mod_bot.
1425 * - if wp->w_redraw_top non-zero, redraw lines between
zeertzjqf2d90a32024-02-12 20:28:01 +01001426 * wp->w_redraw_top and wp->w_redraw_bot.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001427 * - continue redrawing when syntax status is invalid.
1428 * 4. if scrolled up, update lines at the bottom.
1429 * This results in three areas that may need updating:
1430 * top: from first row to top_end (when scrolled down)
1431 * mid: from mid_start to mid_end (update inversion or changed text)
1432 * bot: from bot_start to last row (when scrolled up)
1433 */
1434 static void
1435win_update(win_T *wp)
1436{
1437 buf_T *buf = wp->w_buffer;
1438 int type;
1439 int top_end = 0; // Below last row of the top area that needs
1440 // updating. 0 when no top area updating.
1441 int mid_start = 999;// first row of the mid area that needs
1442 // updating. 999 when no mid area updating.
1443 int mid_end = 0; // Below last row of the mid area that needs
1444 // updating. 0 when no mid area updating.
1445 int bot_start = 999;// first row of the bot area that needs
1446 // updating. 999 when no bot area updating
1447 int scrolled_down = FALSE; // TRUE when scrolled down when
1448 // w_topline got smaller a bit
1449#ifdef FEAT_SEARCH_EXTRA
1450 int top_to_mod = FALSE; // redraw above mod_top
1451#endif
1452
1453 int row; // current window row to display
1454 linenr_T lnum; // current buffer lnum to display
1455 int idx; // current index in w_lines[]
1456 int srow; // starting row of the current line
1457
1458 int eof = FALSE; // if TRUE, we hit the end of the file
1459 int didline = FALSE; // if TRUE, we finished the last line
1460 int i;
1461 long j;
1462 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001463 linenr_T old_botline = wp->w_botline;
1464#ifdef FEAT_CONCEAL
1465 int old_wrow = wp->w_wrow;
1466 int old_wcol = wp->w_wcol;
1467#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001468#ifdef FEAT_FOLDING
1469 long fold_count;
1470#endif
1471#ifdef FEAT_SYN_HL
1472 // remember what happened to the previous line, to know if
1473 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001474# define DID_NONE 1 // didn't update a line
1475# define DID_LINE 2 // updated a normal line
1476# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001477 int did_update = DID_NONE;
1478 linenr_T syntax_last_parsed = 0; // last parsed text line
1479#endif
1480 linenr_T mod_top = 0;
1481 linenr_T mod_bot = 0;
1482#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1483 int save_got_int;
1484#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001485
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001486#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1487 // This needs to be done only for the first window when update_screen() is
1488 // called.
1489 if (!did_update_one_window)
1490 {
1491 did_update_one_window = TRUE;
1492# ifdef FEAT_SEARCH_EXTRA
1493 start_search_hl();
1494# endif
1495# ifdef FEAT_CLIPBOARD
1496 // When Visual area changed, may have to update selection.
1497 if (clip_star.available && clip_isautosel_star())
1498 clip_update_selection(&clip_star);
1499 if (clip_plus.available && clip_isautosel_plus())
1500 clip_update_selection(&clip_plus);
1501# endif
1502 }
1503#endif
1504
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001505 type = wp->w_redr_type;
1506
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001507 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001508 {
1509 wp->w_redr_status = TRUE;
1510 wp->w_lines_valid = 0;
1511 }
1512
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001513 // Window frame is zero-height: nothing to draw.
1514 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1515 || (wp->w_frame->fr_height == wp->w_status_height
1516#if defined(FEAT_PROP_POPUP)
1517 && !popup_is_popup(wp)
1518#endif
1519 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001520 {
1521 wp->w_redr_type = 0;
1522 return;
1523 }
1524
1525 // Window is zero-width: Only need to draw the separator.
1526 if (wp->w_width == 0)
1527 {
1528 // draw the vertical separator right of this window
1529 draw_vsep_win(wp, 0);
1530 wp->w_redr_type = 0;
1531 return;
1532 }
1533
1534#ifdef FEAT_TERMINAL
1535 // If this window contains a terminal, redraw works completely differently.
1536 if (term_do_update_window(wp))
1537 {
1538 term_update_window(wp);
1539# ifdef FEAT_MENU
1540 // Draw the window toolbar, if there is one.
1541 if (winbar_height(wp) > 0)
1542 redraw_win_toolbar(wp);
1543# endif
1544 wp->w_redr_type = 0;
1545 return;
1546 }
1547#endif
1548
1549#ifdef FEAT_SEARCH_EXTRA
1550 init_search_hl(wp, &screen_search_hl);
1551#endif
1552
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001553 // Make sure skipcol is valid, it depends on various options and the window
1554 // width.
Sean Dewar02fcae02024-02-21 19:40:44 +01001555 if (wp->w_skipcol > 0 && wp->w_width > win_col_off(wp))
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001556 {
1557 int w = 0;
1558 int width1 = wp->w_width - win_col_off(wp);
1559 int width2 = width1 + win_col_off2(wp);
1560 int add = width1;
1561
1562 while (w < wp->w_skipcol)
1563 {
1564 if (w > 0)
1565 add = width2;
1566 w += add;
1567 }
1568 if (w != wp->w_skipcol)
1569 // always round down, the higher value may not be valid
1570 wp->w_skipcol = w - add;
1571 }
1572
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001573#ifdef FEAT_LINEBREAK
1574 // Force redraw when width of 'number' or 'relativenumber' column
1575 // changes.
1576 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1577 if (wp->w_nrwidth != i)
1578 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001579 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001580 wp->w_nrwidth = i;
1581 }
1582 else
1583#endif
1584
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001585 {
1586 // Set mod_top to the first line that needs displaying because of
1587 // changes. Set mod_bot to the first line after the changes.
1588 mod_top = wp->w_redraw_top;
1589 if (wp->w_redraw_bot != 0)
1590 mod_bot = wp->w_redraw_bot + 1;
1591 else
1592 mod_bot = 0;
1593 if (buf->b_mod_set)
1594 {
1595 if (mod_top == 0 || mod_top > buf->b_mod_top)
1596 {
1597 mod_top = buf->b_mod_top;
1598#ifdef FEAT_SYN_HL
1599 // Need to redraw lines above the change that may be included
1600 // in a pattern match.
1601 if (syntax_present(wp))
1602 {
1603 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1604 if (mod_top < 1)
1605 mod_top = 1;
1606 }
1607#endif
1608 }
1609 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1610 mod_bot = buf->b_mod_bot;
1611
1612#ifdef FEAT_SEARCH_EXTRA
1613 // When 'hlsearch' is on and using a multi-line search pattern, a
1614 // change in one line may make the Search highlighting in a
1615 // previous line invalid. Simple solution: redraw all visible
1616 // lines above the change.
1617 // Same for a match pattern.
1618 if (screen_search_hl.rm.regprog != NULL
1619 && re_multiline(screen_search_hl.rm.regprog))
1620 top_to_mod = TRUE;
1621 else
1622 {
1623 matchitem_T *cur = wp->w_match_head;
1624
1625 while (cur != NULL)
1626 {
Bram Moolenaar50faf022022-09-29 12:50:17 +01001627 if (cur->mit_match.regprog != NULL
1628 && re_multiline(cur->mit_match.regprog))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001629 {
1630 top_to_mod = TRUE;
1631 break;
1632 }
Bram Moolenaar50faf022022-09-29 12:50:17 +01001633 cur = cur->mit_next;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001634 }
1635 }
1636#endif
1637 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001638
1639#ifdef FEAT_SEARCH_EXTRA
1640 if (search_hl_has_cursor_lnum > 0)
1641 {
1642 // CurSearch was used last time, need to redraw the line with it to
1643 // avoid having two matches highlighted with CurSearch.
1644 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1645 mod_top = search_hl_has_cursor_lnum;
1646 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1647 mod_bot = search_hl_has_cursor_lnum + 1;
1648 }
1649#endif
1650
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001651#ifdef FEAT_FOLDING
1652 if (mod_top != 0 && hasAnyFolding(wp))
1653 {
1654 linenr_T lnumt, lnumb;
1655
1656 // A change in a line can cause lines above it to become folded or
1657 // unfolded. Find the top most buffer line that may be affected.
1658 // If the line was previously folded and displayed, get the first
1659 // line of that fold. If the line is folded now, get the first
1660 // folded line. Use the minimum of these two.
1661
1662 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1663 // the line below it. If there is no valid entry, use w_topline.
1664 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1665 // to this line. If there is no valid entry, use MAXLNUM.
1666 lnumt = wp->w_topline;
1667 lnumb = MAXLNUM;
1668 for (i = 0; i < wp->w_lines_valid; ++i)
1669 if (wp->w_lines[i].wl_valid)
1670 {
1671 if (wp->w_lines[i].wl_lastlnum < mod_top)
1672 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1673 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1674 {
1675 lnumb = wp->w_lines[i].wl_lnum;
1676 // When there is a fold column it might need updating
1677 // in the next line ("J" just above an open fold).
1678 if (compute_foldcolumn(wp, 0) > 0)
1679 ++lnumb;
1680 }
1681 }
1682
1683 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1684 if (mod_top > lnumt)
1685 mod_top = lnumt;
1686
1687 // Now do the same for the bottom line (one above mod_bot).
1688 --mod_bot;
1689 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1690 ++mod_bot;
1691 if (mod_bot < lnumb)
1692 mod_bot = lnumb;
1693 }
1694#endif
1695
1696 // When a change starts above w_topline and the end is below
1697 // w_topline, start redrawing at w_topline.
1698 // If the end of the change is above w_topline: do like no change was
1699 // made, but redraw the first line to find changes in syntax.
1700 if (mod_top != 0 && mod_top < wp->w_topline)
1701 {
1702 if (mod_bot > wp->w_topline)
1703 mod_top = wp->w_topline;
1704#ifdef FEAT_SYN_HL
1705 else if (syntax_present(wp))
1706 top_end = 1;
1707#endif
1708 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001709 }
1710 wp->w_redraw_top = 0; // reset for next time
1711 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001712#ifdef FEAT_SEARCH_EXTRA
1713 search_hl_has_cursor_lnum = 0;
1714#endif
1715
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001716
1717 // When only displaying the lines at the top, set top_end. Used when
1718 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001719 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001720 {
1721 j = 0;
1722 for (i = 0; i < wp->w_lines_valid; ++i)
1723 {
1724 j += wp->w_lines[i].wl_size;
1725 if (j >= wp->w_upd_rows)
1726 {
1727 top_end = j;
1728 break;
1729 }
1730 }
1731 if (top_end == 0)
1732 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001733 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001734 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001735 // top area defined, the rest is UPD_VALID
1736 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001737 }
1738
1739 // Trick: we want to avoid clearing the screen twice. screenclear() will
1740 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1741 // non-zero and thus not FALSE) will indicate that screenclear() was not
1742 // called.
1743 if (screen_cleared)
1744 screen_cleared = MAYBE;
1745
1746 // If there are no changes on the screen that require a complete redraw,
1747 // handle three cases:
1748 // 1: we are off the top of the screen by a few lines: scroll down
1749 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1750 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1751 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001752 if ((type == UPD_VALID || type == UPD_SOME_VALID
1753 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001754#ifdef FEAT_DIFF
1755 && !wp->w_botfill && !wp->w_old_botfill
1756#endif
1757 )
1758 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001759 if (mod_top != 0
1760 && wp->w_topline == mod_top
1761 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001762 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001763 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001764 // w_topline is the first changed line and window is not scrolled,
1765 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001766 }
1767 else if (wp->w_lines[0].wl_valid
1768 && (wp->w_topline < wp->w_lines[0].wl_lnum
1769#ifdef FEAT_DIFF
1770 || (wp->w_topline == wp->w_lines[0].wl_lnum
1771 && wp->w_topfill > wp->w_old_topfill)
1772#endif
1773 ))
1774 {
1775 // New topline is above old topline: May scroll down.
1776#ifdef FEAT_FOLDING
1777 if (hasAnyFolding(wp))
1778 {
1779 linenr_T ln;
1780
1781 // count the number of lines we are off, counting a sequence
1782 // of folded lines as one
1783 j = 0;
1784 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1785 {
1786 ++j;
1787 if (j >= wp->w_height - 2)
1788 break;
1789 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1790 }
1791 }
1792 else
1793#endif
1794 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1795 if (j < wp->w_height - 2) // not too far off
1796 {
zeertzjqbfe377b2023-08-17 22:58:53 +02001797 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1,
Luuk van Baal32d701f2024-04-28 16:24:02 +02001798 wp->w_height);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001799#ifdef FEAT_DIFF
1800 // insert extra lines for previously invisible filler lines
1801 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1802 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1803 - wp->w_old_topfill;
1804#endif
1805 if (i < wp->w_height - 2) // less than a screen off
1806 {
1807 // Try to insert the correct number of lines.
1808 // If not the last window, delete the lines at the bottom.
1809 // win_ins_lines may fail when the terminal can't do it.
1810 if (i > 0)
1811 check_for_delay(FALSE);
1812 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1813 {
1814 if (wp->w_lines_valid != 0)
1815 {
1816 // Need to update rows that are new, stop at the
1817 // first one that scrolled down.
1818 top_end = i;
1819 scrolled_down = TRUE;
1820
1821 // Move the entries that were scrolled, disable
1822 // the entries for the lines to be redrawn.
1823 if ((wp->w_lines_valid += j) > wp->w_height)
1824 wp->w_lines_valid = wp->w_height;
Bram Moolenaara2a89732022-08-31 14:46:18 +01001825 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001826 wp->w_lines[idx] = wp->w_lines[idx - j];
1827 while (idx >= 0)
1828 wp->w_lines[idx--].wl_valid = FALSE;
1829 }
1830 }
1831 else
1832 mid_start = 0; // redraw all lines
1833 }
1834 else
1835 mid_start = 0; // redraw all lines
1836 }
1837 else
1838 mid_start = 0; // redraw all lines
1839 }
1840 else
1841 {
1842 // New topline is at or below old topline: May scroll up.
1843 // When topline didn't change, find first entry in w_lines[] that
1844 // needs updating.
1845
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001846 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1847 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001848 j = -1;
1849 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001850 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001851 {
1852 if (wp->w_lines[i].wl_valid
1853 && wp->w_lines[i].wl_lnum == wp->w_topline)
1854 {
1855 j = i;
1856 break;
1857 }
1858 row += wp->w_lines[i].wl_size;
1859 }
1860 if (j == -1)
1861 {
1862 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1863 // lines
1864 mid_start = 0;
1865 }
1866 else
1867 {
1868 // Try to delete the correct number of lines.
1869 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1870#ifdef FEAT_DIFF
1871 // If the topline didn't change, delete old filler lines,
1872 // otherwise delete filler lines of the new topline...
1873 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1874 row += wp->w_old_topfill;
1875 else
1876 row += diff_check_fill(wp, wp->w_topline);
1877 // ... but don't delete new filler lines.
1878 row -= wp->w_topfill;
1879#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001880 if (row > Rows) // just in case
1881 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001882 if (row > 0)
1883 {
1884 check_for_delay(FALSE);
1885 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1886 == OK)
1887 bot_start = wp->w_height - row;
1888 else
1889 mid_start = 0; // redraw all lines
1890 }
1891 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1892 {
1893 // Skip the lines (below the deleted lines) that are still
1894 // valid and don't need redrawing. Copy their info
1895 // upwards, to compensate for the deleted lines. Set
1896 // bot_start to the first row that needs redrawing.
1897 bot_start = 0;
1898 idx = 0;
1899 for (;;)
1900 {
1901 wp->w_lines[idx] = wp->w_lines[j];
1902 // stop at line that didn't fit, unless it is still
1903 // valid (no lines deleted)
1904 if (row > 0 && bot_start + row
1905 + (int)wp->w_lines[j].wl_size > wp->w_height)
1906 {
1907 wp->w_lines_valid = idx + 1;
1908 break;
1909 }
1910 bot_start += wp->w_lines[idx++].wl_size;
1911
1912 // stop at the last valid entry in w_lines[].wl_size
1913 if (++j >= wp->w_lines_valid)
1914 {
1915 wp->w_lines_valid = idx;
1916 break;
1917 }
1918 }
1919#ifdef FEAT_DIFF
1920 // Correct the first entry for filler lines at the top
1921 // when it won't get updated below.
1922 if (wp->w_p_diff && bot_start > 0)
zeertzjq47f85842024-10-01 19:35:47 +02001923 {
1924 int n = plines_win_nofill(wp, wp->w_topline, FALSE)
1925 + wp->w_topfill - adjust_plines_for_skipcol(wp);
1926 if (n > wp->w_height)
1927 n = wp->w_height;
1928 wp->w_lines[0].wl_size = n;
1929 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001930#endif
1931 }
1932 }
1933 }
1934
Bram Moolenaar13608d82022-08-29 15:06:50 +01001935 // When starting redraw in the first line, redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001936 if (mid_start == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001937 mid_end = wp->w_height;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001938
1939 // When win_del_lines() or win_ins_lines() caused the screen to be
1940 // cleared (only happens for the first window) or when screenclear()
1941 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001942 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001943 if (screen_cleared == TRUE)
1944 must_redraw = 0;
1945 }
1946 else
1947 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001948 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001949 mid_start = 0;
1950 mid_end = wp->w_height;
1951 }
1952
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001953 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001954 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001955 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001956 mid_start = 0;
1957 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001958 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001959 }
1960
1961 // check if we are updating or removing the inverted part
1962 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001963 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001964 {
1965 linenr_T from, to;
1966
1967 if (VIsual_active)
1968 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001969 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001970 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001971 {
1972 // If the type of Visual selection changed, redraw the whole
1973 // selection. Also when the ownership of the X selection is
1974 // gained or lost.
1975 if (curwin->w_cursor.lnum < VIsual.lnum)
1976 {
1977 from = curwin->w_cursor.lnum;
1978 to = VIsual.lnum;
1979 }
1980 else
1981 {
1982 from = VIsual.lnum;
1983 to = curwin->w_cursor.lnum;
1984 }
1985 // redraw more when the cursor moved as well
1986 if (wp->w_old_cursor_lnum < from)
1987 from = wp->w_old_cursor_lnum;
1988 if (wp->w_old_cursor_lnum > to)
1989 to = wp->w_old_cursor_lnum;
1990 if (wp->w_old_visual_lnum < from)
1991 from = wp->w_old_visual_lnum;
1992 if (wp->w_old_visual_lnum > to)
1993 to = wp->w_old_visual_lnum;
1994 }
1995 else
1996 {
1997 // Find the line numbers that need to be updated: The lines
1998 // between the old cursor position and the current cursor
1999 // position. Also check if the Visual position changed.
2000 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
2001 {
2002 from = curwin->w_cursor.lnum;
2003 to = wp->w_old_cursor_lnum;
2004 }
2005 else
2006 {
2007 from = wp->w_old_cursor_lnum;
2008 to = curwin->w_cursor.lnum;
2009 if (from == 0) // Visual mode just started
2010 from = to;
2011 }
2012
2013 if (VIsual.lnum != wp->w_old_visual_lnum
2014 || VIsual.col != wp->w_old_visual_col)
2015 {
2016 if (wp->w_old_visual_lnum < from
2017 && wp->w_old_visual_lnum != 0)
2018 from = wp->w_old_visual_lnum;
2019 if (wp->w_old_visual_lnum > to)
2020 to = wp->w_old_visual_lnum;
2021 if (VIsual.lnum < from)
2022 from = VIsual.lnum;
2023 if (VIsual.lnum > to)
2024 to = VIsual.lnum;
2025 }
2026 }
2027
2028 // If in block mode and changed column or curwin->w_curswant:
2029 // update all lines.
2030 // First compute the actual start and end column.
2031 if (VIsual_mode == Ctrl_V)
2032 {
2033 colnr_T fromc, toc;
2034#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002035 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002036
2037 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002038 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002039#endif
2040 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002041 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002042#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002043 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002044#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002045 // Highlight to the end of the line, unless 'virtualedit' has
2046 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002047 if (curwin->w_curswant == MAXCOL)
2048 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002049 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002050 {
2051 pos_T pos;
2052 int cursor_above =
2053 curwin->w_cursor.lnum < VIsual.lnum;
2054
2055 // Need to find the longest line.
2056 toc = 0;
2057 pos.coladd = 0;
2058 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2059 ? pos.lnum <= VIsual.lnum
2060 : pos.lnum >= VIsual.lnum;
2061 pos.lnum += cursor_above ? 1 : -1)
2062 {
2063 colnr_T t;
2064
John Marriotta21240b2025-01-08 20:10:59 +01002065 pos.col = (int)ml_get_buf_len(wp->w_buffer, pos.lnum);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002066 getvvcol(wp, &pos, NULL, NULL, &t);
2067 if (toc < t)
2068 toc = t;
2069 }
2070 ++toc;
2071 }
2072 else
2073 toc = MAXCOL;
2074 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002075
2076 if (fromc != wp->w_old_cursor_fcol
2077 || toc != wp->w_old_cursor_lcol)
2078 {
2079 if (from > VIsual.lnum)
2080 from = VIsual.lnum;
2081 if (to < VIsual.lnum)
2082 to = VIsual.lnum;
2083 }
2084 wp->w_old_cursor_fcol = fromc;
2085 wp->w_old_cursor_lcol = toc;
2086 }
2087 }
2088 else
2089 {
2090 // Use the line numbers of the old Visual area.
2091 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2092 {
2093 from = wp->w_old_cursor_lnum;
2094 to = wp->w_old_visual_lnum;
2095 }
2096 else
2097 {
2098 from = wp->w_old_visual_lnum;
2099 to = wp->w_old_cursor_lnum;
2100 }
2101 }
2102
2103 // There is no need to update lines above the top of the window.
2104 if (from < wp->w_topline)
2105 from = wp->w_topline;
2106
2107 // If we know the value of w_botline, use it to restrict the update to
2108 // the lines that are visible in the window.
2109 if (wp->w_valid & VALID_BOTLINE)
2110 {
2111 if (from >= wp->w_botline)
2112 from = wp->w_botline - 1;
2113 if (to >= wp->w_botline)
2114 to = wp->w_botline - 1;
2115 }
2116
2117 // Find the minimal part to be updated.
2118 // Watch out for scrolling that made entries in w_lines[] invalid.
2119 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2120 // top_end; need to redraw from top_end to the "to" line.
2121 // A middle mouse click with a Visual selection may change the text
2122 // above the Visual area and reset wl_valid, do count these for
2123 // mid_end (in srow).
2124 if (mid_start > 0)
2125 {
2126 lnum = wp->w_topline;
2127 idx = 0;
2128 srow = 0;
2129 if (scrolled_down)
2130 mid_start = top_end;
2131 else
2132 mid_start = 0;
2133 while (lnum < from && idx < wp->w_lines_valid) // find start
2134 {
2135 if (wp->w_lines[idx].wl_valid)
2136 mid_start += wp->w_lines[idx].wl_size;
2137 else if (!scrolled_down)
2138 srow += wp->w_lines[idx].wl_size;
2139 ++idx;
2140# ifdef FEAT_FOLDING
2141 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2142 lnum = wp->w_lines[idx].wl_lnum;
2143 else
2144# endif
2145 ++lnum;
2146 }
2147 srow += mid_start;
2148 mid_end = wp->w_height;
2149 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2150 {
2151 if (wp->w_lines[idx].wl_valid
2152 && wp->w_lines[idx].wl_lnum >= to + 1)
2153 {
2154 // Only update until first row of this line
2155 mid_end = srow;
2156 break;
2157 }
2158 srow += wp->w_lines[idx].wl_size;
2159 }
2160 }
2161 }
2162
2163 if (VIsual_active && buf == curwin->w_buffer)
2164 {
2165 wp->w_old_visual_mode = VIsual_mode;
2166 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2167 wp->w_old_visual_lnum = VIsual.lnum;
2168 wp->w_old_visual_col = VIsual.col;
2169 wp->w_old_curswant = curwin->w_curswant;
2170 }
2171 else
2172 {
2173 wp->w_old_visual_mode = 0;
2174 wp->w_old_cursor_lnum = 0;
2175 wp->w_old_visual_lnum = 0;
2176 wp->w_old_visual_col = 0;
2177 }
2178
2179#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2180 // reset got_int, otherwise regexp won't work
2181 save_got_int = got_int;
2182 got_int = 0;
2183#endif
2184#ifdef SYN_TIME_LIMIT
2185 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002186 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002187 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002188#endif
2189#ifdef FEAT_FOLDING
2190 win_foldinfo.fi_level = 0;
2191#endif
2192
2193#ifdef FEAT_MENU
2194 // Draw the window toolbar, if there is one.
2195 // TODO: only when needed.
2196 if (winbar_height(wp) > 0)
2197 redraw_win_toolbar(wp);
2198#endif
2199
Luuk van Baal30805a12023-05-27 22:22:10 +01002200 lnum = wp->w_topline; // first line shown in window
2201
2202 spellvars_T spv;
2203#ifdef FEAT_SPELL
2204 // Initialize spell related variables for the first drawn line.
2205 CLEAR_FIELD(spv);
Luuk van Baale84c7732023-05-31 18:57:36 +01002206 if (spell_check_window(wp))
Luuk van Baal30805a12023-05-27 22:22:10 +01002207 {
Luuk van Baale84c7732023-05-31 18:57:36 +01002208 spv.spv_has_spell = TRUE;
Luuk van Baal30805a12023-05-27 22:22:10 +01002209 spv.spv_unchanged = mod_top == 0;
Luuk van Baal30805a12023-05-27 22:22:10 +01002210 }
2211#endif
2212
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002213 // Update all the window rows.
2214 idx = 0; // first entry in w_lines[].wl_size
2215 row = 0;
2216 srow = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002217 for (;;)
2218 {
2219 // stop updating when reached the end of the window (check for _past_
2220 // the end of the window is at the end of the loop)
2221 if (row == wp->w_height)
2222 {
2223 didline = TRUE;
2224 break;
2225 }
2226
2227 // stop updating when hit the end of the file
2228 if (lnum > buf->b_ml.ml_line_count)
2229 {
2230 eof = TRUE;
2231 break;
2232 }
2233
2234 // Remember the starting row of the line that is going to be dealt
2235 // with. It is used further down when the line doesn't fit.
2236 srow = row;
2237
2238 // Update a line when it is in an area that needs updating, when it
2239 // has changes or w_lines[idx] is invalid.
2240 // "bot_start" may be halfway a wrapped line after using
2241 // win_del_lines(), check if the current line includes it.
2242 // When syntax folding is being used, the saved syntax states will
2243 // already have been updated, we can't see where the syntax state is
2244 // the same again, just update until the end of the window.
2245 if (row < top_end
2246 || (row >= mid_start && row < mid_end)
2247#ifdef FEAT_SEARCH_EXTRA
2248 || top_to_mod
2249#endif
2250 || idx >= wp->w_lines_valid
2251 || (row + wp->w_lines[idx].wl_size > bot_start)
2252 || (mod_top != 0
2253 && (lnum == mod_top
2254 || (lnum >= mod_top
2255 && (lnum < mod_bot
2256#ifdef FEAT_SYN_HL
2257 || did_update == DID_FOLD
2258 || (did_update == DID_LINE
2259 && syntax_present(wp)
2260 && (
2261# ifdef FEAT_FOLDING
2262 (foldmethodIsSyntax(wp)
2263 && hasAnyFolding(wp)) ||
2264# endif
2265 syntax_check_changed(lnum)))
2266#endif
2267#ifdef FEAT_SEARCH_EXTRA
2268 // match in fixed position might need redraw
2269 // if lines were inserted or deleted
2270 || (wp->w_match_head != NULL
zeertzjq9f25a3a2024-11-26 15:08:02 +01002271 && buf->b_mod_set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002272 && buf->b_mod_xlines != 0)
2273#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002274 ))))
2275#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002276 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2277 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002278#endif
2279 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002280 {
2281#ifdef FEAT_SEARCH_EXTRA
2282 if (lnum == mod_top)
2283 top_to_mod = FALSE;
2284#endif
2285
2286 // When at start of changed lines: May scroll following lines
2287 // up or down to minimize redrawing.
2288 // Don't do this when the change continues until the end.
2289 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002290 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002291 if (lnum == mod_top
2292 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002293 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2294 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002295 {
2296 int old_rows = 0;
2297 int new_rows = 0;
2298 int xtra_rows;
2299 linenr_T l;
2300
2301 // Count the old number of window rows, using w_lines[], which
2302 // should still contain the sizes for the lines as they are
2303 // currently displayed.
2304 for (i = idx; i < wp->w_lines_valid; ++i)
2305 {
2306 // Only valid lines have a meaningful wl_lnum. Invalid
2307 // lines are part of the changed area.
2308 if (wp->w_lines[i].wl_valid
2309 && wp->w_lines[i].wl_lnum == mod_bot)
2310 break;
2311 old_rows += wp->w_lines[i].wl_size;
2312#ifdef FEAT_FOLDING
2313 if (wp->w_lines[i].wl_valid
2314 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2315 {
2316 // Must have found the last valid entry above mod_bot.
2317 // Add following invalid entries.
2318 ++i;
2319 while (i < wp->w_lines_valid
2320 && !wp->w_lines[i].wl_valid)
2321 old_rows += wp->w_lines[i++].wl_size;
2322 break;
2323 }
2324#endif
2325 }
2326
2327 if (i >= wp->w_lines_valid)
2328 {
2329 // We can't find a valid line below the changed lines,
2330 // need to redraw until the end of the window.
2331 // Inserting/deleting lines has no use.
2332 bot_start = 0;
2333 }
2334 else
2335 {
2336 // Able to count old number of rows: Count new window
2337 // rows, and may insert/delete lines
2338 j = idx;
2339 for (l = lnum; l < mod_bot; ++l)
2340 {
2341#ifdef FEAT_FOLDING
2342 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2343 ++new_rows;
2344 else
2345#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002346 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002347#ifdef FEAT_DIFF
2348 if (l == wp->w_topline)
Luuk van Baalc8502f92023-05-06 12:40:15 +01002349 {
2350 int n = plines_win_nofill(wp, l, FALSE)
2351 + wp->w_topfill;
zeertzjq6235a102023-08-19 14:12:42 +02002352 n -= adjust_plines_for_skipcol(wp);
Luuk van Baalc8502f92023-05-06 12:40:15 +01002353 if (n > wp->w_height)
2354 n = wp->w_height;
2355 new_rows += n;
2356 }
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002357 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002358#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002359 new_rows += plines_win(wp, l, TRUE);
2360 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002361 ++j;
2362 if (new_rows > wp->w_height - row - 2)
2363 {
2364 // it's getting too much, must redraw the rest
2365 new_rows = 9999;
2366 break;
2367 }
2368 }
2369 xtra_rows = new_rows - old_rows;
2370 if (xtra_rows < 0)
2371 {
2372 // May scroll text up. If there is not enough
2373 // remaining text or scrolling fails, must redraw the
2374 // rest. If scrolling works, must redraw the text
2375 // below the scrolled text.
2376 if (row - xtra_rows >= wp->w_height - 2)
2377 mod_bot = MAXLNUM;
2378 else
2379 {
2380 check_for_delay(FALSE);
2381 if (win_del_lines(wp, row,
2382 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2383 mod_bot = MAXLNUM;
2384 else
2385 bot_start = wp->w_height + xtra_rows;
2386 }
2387 }
2388 else if (xtra_rows > 0)
2389 {
2390 // May scroll text down. If there is not enough
2391 // remaining text of scrolling fails, must redraw the
2392 // rest.
2393 if (row + xtra_rows >= wp->w_height - 2)
2394 mod_bot = MAXLNUM;
2395 else
2396 {
2397 check_for_delay(FALSE);
2398 if (win_ins_lines(wp, row + old_rows,
2399 xtra_rows, FALSE, FALSE) == FAIL)
2400 mod_bot = MAXLNUM;
2401 else if (top_end > row + old_rows)
2402 // Scrolled the part at the top that requires
2403 // updating down.
2404 top_end += xtra_rows;
2405 }
2406 }
2407
2408 // When not updating the rest, may need to move w_lines[]
2409 // entries.
2410 if (mod_bot != MAXLNUM && i != j)
2411 {
2412 if (j < i)
2413 {
2414 int x = row + new_rows;
2415
2416 // move entries in w_lines[] upwards
2417 for (;;)
2418 {
2419 // stop at last valid entry in w_lines[]
2420 if (i >= wp->w_lines_valid)
2421 {
2422 wp->w_lines_valid = j;
2423 break;
2424 }
2425 wp->w_lines[j] = wp->w_lines[i];
2426 // stop at a line that won't fit
2427 if (x + (int)wp->w_lines[j].wl_size
2428 > wp->w_height)
2429 {
2430 wp->w_lines_valid = j + 1;
2431 break;
2432 }
2433 x += wp->w_lines[j++].wl_size;
2434 ++i;
2435 }
2436 if (bot_start > x)
2437 bot_start = x;
2438 }
2439 else // j > i
2440 {
2441 // move entries in w_lines[] downwards
2442 j -= i;
2443 wp->w_lines_valid += j;
2444 if (wp->w_lines_valid > wp->w_height)
2445 wp->w_lines_valid = wp->w_height;
2446 for (i = wp->w_lines_valid; i - j >= idx; --i)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002447 wp->w_lines[i] = wp->w_lines[i - j];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002448
2449 // The w_lines[] entries for inserted lines are
2450 // now invalid, but wl_size may be used above.
2451 // Reset to zero.
2452 while (i >= idx)
2453 {
2454 wp->w_lines[i].wl_size = 0;
2455 wp->w_lines[i--].wl_valid = FALSE;
2456 }
2457 }
2458 }
2459 }
2460 }
2461
2462#ifdef FEAT_FOLDING
2463 // When lines are folded, display one line for all of them.
2464 // Otherwise, display normally (can be several display lines when
2465 // 'wrap' is on).
2466 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2467 if (fold_count != 0)
2468 {
2469 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2470 ++row;
2471 --fold_count;
2472 wp->w_lines[idx].wl_folded = TRUE;
Luuk van Baale84c7732023-05-31 18:57:36 +01002473 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002474# ifdef FEAT_SYN_HL
2475 did_update = DID_FOLD;
2476# endif
Luuk van Baal30805a12023-05-27 22:22:10 +01002477# ifdef FEAT_SPELL
Luuk van Baale84c7732023-05-31 18:57:36 +01002478 spv.spv_capcol_lnum = 0;
Luuk van Baal30805a12023-05-27 22:22:10 +01002479# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002480 }
2481 else
2482#endif
2483 if (idx < wp->w_lines_valid
2484 && wp->w_lines[idx].wl_valid
2485 && wp->w_lines[idx].wl_lnum == lnum
2486 && lnum > wp->w_topline
2487 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2488 && !WIN_IS_POPUP(wp)
2489 && srow + wp->w_lines[idx].wl_size > wp->w_height
2490#ifdef FEAT_DIFF
2491 && diff_check_fill(wp, lnum) == 0
2492#endif
2493 )
2494 {
2495 // This line is not going to fit. Don't draw anything here,
2496 // will draw "@ " lines below.
2497 row = wp->w_height + 1;
2498 }
2499 else
2500 {
2501#ifdef FEAT_SEARCH_EXTRA
2502 prepare_search_hl(wp, &screen_search_hl, lnum);
2503#endif
2504#ifdef FEAT_SYN_HL
2505 // Let the syntax stuff know we skipped a few lines.
2506 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2507 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002508 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002509#endif
2510
2511 // Display one line.
zeertzjqebfd8562024-02-06 10:59:03 +01002512 row = win_line(wp, lnum, srow, wp->w_height, 0, &spv);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002513
2514#ifdef FEAT_FOLDING
2515 wp->w_lines[idx].wl_folded = FALSE;
2516 wp->w_lines[idx].wl_lastlnum = lnum;
2517#endif
2518#ifdef FEAT_SYN_HL
2519 did_update = DID_LINE;
2520 syntax_last_parsed = lnum;
2521#endif
2522 }
2523
2524 wp->w_lines[idx].wl_lnum = lnum;
2525 wp->w_lines[idx].wl_valid = TRUE;
2526
2527 // Past end of the window or end of the screen. Note that after
2528 // resizing wp->w_height may be end up too big. That's a problem
2529 // elsewhere, but prevent a crash here.
Bram Moolenaara2a89732022-08-31 14:46:18 +01002530 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002531 {
2532 // we may need the size of that too long line later on
2533 if (dollar_vcol == -1)
2534 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2535 ++idx;
2536 break;
2537 }
2538 if (dollar_vcol == -1)
2539 wp->w_lines[idx].wl_size = row - srow;
2540 ++idx;
2541#ifdef FEAT_FOLDING
2542 lnum += fold_count + 1;
2543#else
2544 ++lnum;
2545#endif
2546 }
2547 else
2548 {
zeertzjqae07ebc2024-02-08 11:37:40 +01002549 // If:
2550 // - 'number' is set and below inserted/deleted lines, or
2551 // - 'relativenumber' is set and cursor moved vertically,
2552 // the text doesn't need to be redrawn, but the number column does.
zeertzjq9f25a3a2024-11-26 15:08:02 +01002553 if ((wp->w_p_nu && mod_top != 0 && lnum >= mod_bot
2554 && buf->b_mod_set && buf->b_mod_xlines != 0)
zeertzjqae07ebc2024-02-08 11:37:40 +01002555 || (wp->w_p_rnu
2556 && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002557 {
2558#ifdef FEAT_FOLDING
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002559 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2560 if (fold_count != 0)
2561 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2562 else
2563#endif
zeertzjqebfd8562024-02-06 10:59:03 +01002564 (void)win_line(wp, lnum, srow, wp->w_height,
2565 wp->w_lines[idx].wl_size, &spv);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002566 }
2567
2568 // This line does not need to be drawn, advance to the next one.
2569 row += wp->w_lines[idx++].wl_size;
2570 if (row > wp->w_height) // past end of screen
2571 break;
2572#ifdef FEAT_FOLDING
2573 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2574#else
2575 ++lnum;
2576#endif
2577#ifdef FEAT_SYN_HL
2578 did_update = DID_NONE;
2579#endif
Luuk van Baale84c7732023-05-31 18:57:36 +01002580#ifdef FEAT_SPELL
2581 spv.spv_capcol_lnum = 0;
2582#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002583 }
2584
2585 if (lnum > buf->b_ml.ml_line_count)
2586 {
2587 eof = TRUE;
2588 break;
2589 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002590
2591 // Safety check: if any of the wl_size values is wrong we might go over
2592 // the end of w_lines[].
Bram Moolenaara2a89732022-08-31 14:46:18 +01002593 if (idx >= Rows)
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002594 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002595 }
2596
2597 // End of loop over all window lines.
2598
zeertzjqc20e46a2022-03-23 14:55:23 +00002599#ifdef FEAT_SYN_HL
2600 // Now that the window has been redrawn with the old and new cursor line,
2601 // update w_last_cursorline.
2602 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2603#endif
Lewis Russell16246392022-03-29 11:38:17 +01002604 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002605
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002606#ifdef FEAT_VTP
2607 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002608 // See the version that was fixed.
2609 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002610 {
K.Takata54119102022-02-03 13:33:03 +00002611 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002612
K.Takata54119102022-02-03 13:33:03 +00002613 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002614 if (enc_utf8)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02002615 if ((*mb_off2cells)(LineOffset[k] + topframe->fr_width - 2,
K.Takata54119102022-02-03 13:33:03 +00002616 LineOffset[k] + screen_Columns) > 1)
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02002617 screen_draw_rectangle(k, topframe->fr_width - 2, 1, 2,
2618 FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002619 else
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02002620 screen_draw_rectangle(k, topframe->fr_width - 1, 1, 1,
2621 FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002622 else
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02002623 screen_char(LineOffset[k] + topframe->fr_width - 1, k,
2624 Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002625 }
2626#endif
2627
2628 if (idx > wp->w_lines_valid)
2629 wp->w_lines_valid = idx;
2630
2631#ifdef FEAT_SYN_HL
2632 // Let the syntax stuff know we stop parsing here.
2633 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002634 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002635#endif
2636
2637 // If we didn't hit the end of the file, and we didn't finish the last
2638 // line we were working on, then the line didn't fit.
2639 wp->w_empty_rows = 0;
2640#ifdef FEAT_DIFF
2641 wp->w_filler_rows = 0;
2642#endif
2643 if (!eof && !didline)
2644 {
2645 if (lnum == wp->w_topline)
2646 {
2647 // Single line that does not fit!
2648 // Don't overwrite it, it can be edited.
2649 wp->w_botline = lnum + 1;
2650 }
2651#ifdef FEAT_DIFF
2652 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2653 {
2654 // Window ends in filler lines.
2655 wp->w_botline = lnum;
2656 wp->w_filler_rows = wp->w_height - srow;
2657 }
2658#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002659#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002660 else if (WIN_IS_POPUP(wp))
2661 {
2662 // popup line that doesn't fit is left as-is
2663 wp->w_botline = lnum;
2664 }
2665#endif
2666 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2667 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002668 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2669 int symbol = wp->w_fill_chars.lastline;
zeertzjq18b35002022-10-04 20:35:37 +01002670 int charlen;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002671 char_u fillbuf[12]; // 2 characters of 6 bytes
2672
zeertzjq18b35002022-10-04 20:35:37 +01002673 charlen = mb_char2bytes(symbol, &fillbuf[0]);
2674 mb_char2bytes(symbol, &fillbuf[charlen]);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002675
2676 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002677 screen_puts_len(fillbuf,
zeertzjq18b35002022-10-04 20:35:37 +01002678 (wp->w_width > 2 ? 2 : wp->w_width) * charlen,
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002679 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002680 screen_fill(scr_row, scr_row + 1,
2681 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002682 symbol, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002683 set_empty_rows(wp, srow);
2684 wp->w_botline = lnum;
2685 }
2686 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2687 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002688 int start_col = (int)W_ENDCOL(wp) - 3;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002689 int symbol = wp->w_fill_chars.lastline;
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002690
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002691 // Last line isn't finished: Display "@@@" at the end.
2692 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2693 W_WINROW(wp) + wp->w_height,
Hirohito Higashi3b9b95d2025-06-01 20:22:55 +02002694 (start_col < wp->w_wincol ? wp->w_wincol : start_col),
2695 (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002696 symbol, symbol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002697 set_empty_rows(wp, srow);
2698 wp->w_botline = lnum;
2699 }
2700 else
2701 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002702 win_draw_end(wp, wp->w_fill_chars.lastline, ' ', TRUE,
2703 srow, wp->w_height, HLF_AT);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002704 wp->w_botline = lnum;
2705 }
2706 }
2707 else
2708 {
2709 draw_vsep_win(wp, row);
2710 if (eof) // we hit the end of the file
2711 {
2712 wp->w_botline = buf->b_ml.ml_line_count + 1;
2713#ifdef FEAT_DIFF
2714 j = diff_check_fill(wp, wp->w_botline);
2715 if (j > 0 && !wp->w_botfill)
2716 {
2717 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002718 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002719 i = '-';
2720 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002721 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002722 if (row + j > wp->w_height)
2723 j = wp->w_height - row;
2724 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2725 row += j;
2726 }
2727#endif
2728 }
2729 else if (dollar_vcol == -1)
2730 wp->w_botline = lnum;
2731
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002732 // Make sure the rest of the screen is blank.
2733 // write the "eob" character from 'fillchars' to rows that aren't part
2734 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002735 if (WIN_IS_POPUP(wp))
2736 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2737 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002738 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2739 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002740 }
2741
2742#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002743 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002744 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002745#endif
2746
2747 // Reset the type of redrawing required, the window has been updated.
2748 wp->w_redr_type = 0;
2749#ifdef FEAT_DIFF
2750 wp->w_old_topfill = wp->w_topfill;
2751 wp->w_old_botfill = wp->w_botfill;
2752#endif
2753
2754 if (dollar_vcol == -1)
2755 {
2756 // There is a trick with w_botline. If we invalidate it on each
2757 // change that might modify it, this will cause a lot of expensive
2758 // calls to plines() in update_topline() each time. Therefore the
2759 // value of w_botline is often approximated, and this value is used to
2760 // compute the value of w_topline. If the value of w_botline was
2761 // wrong, check that the value of w_topline is correct (cursor is on
2762 // the visible part of the text). If it's not, we need to redraw
2763 // again. Mostly this just means scrolling up a few lines, so it
2764 // doesn't look too bad. Only do this for the current window (where
2765 // changes are relevant).
2766 wp->w_valid |= VALID_BOTLINE;
2767 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2768 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002769 win_T *wwp;
2770#if defined(FEAT_CONCEAL)
2771 linenr_T old_topline = wp->w_topline;
2772 int new_wcol = wp->w_wcol;
2773#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002774 recursive = TRUE;
2775 curwin->w_valid &= ~VALID_TOPLINE;
2776 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002777
2778#if defined(FEAT_CONCEAL)
2779 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2780 != (VALID_WCOL|VALID_WROW))
2781 {
2782 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002783 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002784 // update_topline() the cursor's row or column got invalidated.
2785 // If they are left invalid, setcursor() will recompute them
2786 // but there won't be any further win_line() call to re-fix the
2787 // column and the cursor will end up misplaced. So we call
2788 // cursor validation now and reapply the fix again (or call
2789 // win_line() to do it for us).
2790 validate_cursor();
2791 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2792 && old_topline == wp->w_topline)
2793 wp->w_wcol = new_wcol;
2794 else
2795 redrawWinline(wp, wp->w_cursor.lnum);
2796 }
2797#endif
Luuk van Baal3d5065f2024-09-01 10:33:56 +02002798 // New redraw either due to updated topline, wcol fix or reset skipcol.
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002799 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002800 {
2801 // Don't update for changes in buffer again.
2802 i = curbuf->b_mod_set;
2803 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002804 j = curbuf->b_mod_xlines;
2805 curbuf->b_mod_xlines = 0;
Luuk van Baal3d5065f2024-09-01 10:33:56 +02002806 curs_columns(TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002807 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002808 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002809 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002810 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002811 // Other windows might have w_redr_type raised in update_topline().
2812 must_redraw = 0;
2813 FOR_ALL_WINDOWS(wwp)
2814 if (wwp->w_redr_type > must_redraw)
2815 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002816 recursive = FALSE;
2817 }
2818 }
2819
2820#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2821 // restore got_int, unless CTRL-C was hit while redrawing
2822 if (!got_int)
2823 got_int = save_got_int;
2824#endif
2825}
2826
2827#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2828/*
2829 * Prepare for updating one or more windows.
2830 * Caller must check for "updating_screen" already set to avoid recursiveness.
2831 */
2832 static void
2833update_prepare(void)
2834{
2835 cursor_off();
2836 updating_screen = TRUE;
2837#ifdef FEAT_GUI
2838 // Remove the cursor before starting to do anything, because scrolling may
2839 // make it difficult to redraw the text under it.
2840 if (gui.in_use)
2841 gui_undraw_cursor();
2842#endif
2843#ifdef FEAT_SEARCH_EXTRA
2844 start_search_hl();
2845#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002846#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002847 // Update popup_mask if needed.
2848 may_update_popup_mask(must_redraw);
2849#endif
2850}
2851
2852/*
2853 * Finish updating one or more windows.
2854 */
2855 static void
2856update_finish(void)
2857{
2858 if (redraw_cmdline || redraw_mode)
2859 showmode();
2860
2861# ifdef FEAT_SEARCH_EXTRA
2862 end_search_hl();
2863# endif
2864
2865 after_updating_screen(TRUE);
2866
2867# ifdef FEAT_GUI
2868 // Redraw the cursor and update the scrollbars when all screen updating is
2869 // done.
2870 if (gui.in_use)
2871 {
2872 out_flush_cursor(FALSE, FALSE);
2873 gui_update_scrollbars(FALSE);
2874 }
2875# endif
2876}
2877#endif
2878
2879#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2880 void
2881update_debug_sign(buf_T *buf, linenr_T lnum)
2882{
2883 win_T *wp;
2884 int doit = FALSE;
2885
2886# ifdef FEAT_FOLDING
2887 win_foldinfo.fi_level = 0;
2888# endif
2889
2890 // update/delete a specific sign
2891 redraw_buf_line_later(buf, lnum);
2892
2893 // check if it resulted in the need to redraw a window
2894 FOR_ALL_WINDOWS(wp)
2895 if (wp->w_redr_type != 0)
2896 doit = TRUE;
2897
2898 // Return when there is nothing to do, screen updating is already
2899 // happening (recursive call), messages on the screen or still starting up.
2900 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002901 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002902 || msg_scrolled
2903#ifdef FEAT_GUI
2904 || gui.starting
2905#endif
2906 || starting)
2907 return;
2908
2909 // update all windows that need updating
2910 update_prepare();
2911
2912 FOR_ALL_WINDOWS(wp)
2913 {
2914 if (wp->w_redr_type != 0)
2915 win_update(wp);
2916 if (wp->w_redr_status)
2917 win_redr_status(wp, FALSE);
2918 }
2919
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02002920#if defined(FEAT_TABPANEL)
2921 if (redraw_tabpanel)
2922 draw_tabpanel();
2923#endif
2924
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002925 update_finish();
2926}
2927#endif
2928
2929#if defined(FEAT_GUI) || defined(PROTO)
2930/*
2931 * Update a single window, its status line and maybe the command line msg.
2932 * Used for the GUI scrollbar.
2933 */
2934 void
2935updateWindow(win_T *wp)
2936{
2937 // return if already busy updating
2938 if (updating_screen)
2939 return;
2940
2941 update_prepare();
2942
2943#ifdef FEAT_CLIPBOARD
2944 // When Visual area changed, may have to update selection.
2945 if (clip_star.available && clip_isautosel_star())
2946 clip_update_selection(&clip_star);
2947 if (clip_plus.available && clip_isautosel_plus())
2948 clip_update_selection(&clip_plus);
2949#endif
2950
2951 win_update(wp);
2952
2953 // When the screen was cleared redraw the tab pages line.
2954 if (redraw_tabline)
2955 draw_tabline();
2956
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02002957#if defined(FEAT_TABPANEL)
2958 if (redraw_tabpanel)
2959 draw_tabpanel();
2960#endif
2961
Martin Tournoijba43e762022-10-13 22:12:15 +01002962 if (wp->w_redr_status || p_ru
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002963# ifdef FEAT_STL_OPT
2964 || *p_stl != NUL || *wp->w_p_stl != NUL
2965# endif
2966 )
2967 win_redr_status(wp, FALSE);
2968
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002969#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002970 // Display popup windows on top of everything.
2971 update_popups(win_update);
2972#endif
2973
2974 update_finish();
2975}
2976#endif
2977
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002978/*
2979 * Redraw as soon as possible. When the command line is not scrolled redraw
2980 * right away and restore what was on the command line.
2981 * Return a code indicating what happened.
2982 */
2983 int
2984redraw_asap(int type)
2985{
2986 int rows;
2987 int cols = screen_Columns;
2988 int r;
2989 int ret = 0;
2990 schar_T *screenline; // copy from ScreenLines[]
2991 sattr_T *screenattr; // copy from ScreenAttrs[]
2992 int i;
2993 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2994 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2995 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2996
2997 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002998 if (msg_scrolled
2999 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
Bram Moolenaara2a89732022-08-31 14:46:18 +01003000 || exiting)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003001 return ret;
3002
3003 // Allocate space to save the text displayed in the command line area.
3004 rows = screen_Rows - cmdline_row;
3005 screenline = LALLOC_MULT(schar_T, rows * cols);
3006 screenattr = LALLOC_MULT(sattr_T, rows * cols);
3007 if (screenline == NULL || screenattr == NULL)
3008 ret = 2;
3009 if (enc_utf8)
3010 {
3011 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
3012 if (screenlineUC == NULL)
3013 ret = 2;
3014 for (i = 0; i < p_mco; ++i)
3015 {
3016 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
3017 if (screenlineC[i] == NULL)
3018 ret = 2;
3019 }
3020 }
3021 if (enc_dbcs == DBCS_JPNU)
3022 {
3023 screenline2 = LALLOC_MULT(schar_T, rows * cols);
3024 if (screenline2 == NULL)
3025 ret = 2;
3026 }
3027
3028 if (ret != 2)
3029 {
3030 // Save the text displayed in the command line area.
3031 for (r = 0; r < rows; ++r)
3032 {
3033 mch_memmove(screenline + r * cols,
3034 ScreenLines + LineOffset[cmdline_row + r],
3035 (size_t)cols * sizeof(schar_T));
3036 mch_memmove(screenattr + r * cols,
3037 ScreenAttrs + LineOffset[cmdline_row + r],
3038 (size_t)cols * sizeof(sattr_T));
3039 if (enc_utf8)
3040 {
3041 mch_memmove(screenlineUC + r * cols,
3042 ScreenLinesUC + LineOffset[cmdline_row + r],
3043 (size_t)cols * sizeof(u8char_T));
3044 for (i = 0; i < p_mco; ++i)
3045 mch_memmove(screenlineC[i] + r * cols,
3046 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3047 (size_t)cols * sizeof(u8char_T));
3048 }
3049 if (enc_dbcs == DBCS_JPNU)
3050 mch_memmove(screenline2 + r * cols,
3051 ScreenLines2 + LineOffset[cmdline_row + r],
3052 (size_t)cols * sizeof(schar_T));
3053 }
3054
3055 update_screen(0);
3056 ret = 3;
3057
3058 if (must_redraw == 0)
3059 {
3060 int off = (int)(current_ScreenLine - ScreenLines);
3061
3062 // Restore the text displayed in the command line area.
3063 for (r = 0; r < rows; ++r)
3064 {
3065 mch_memmove(current_ScreenLine,
3066 screenline + r * cols,
3067 (size_t)cols * sizeof(schar_T));
3068 mch_memmove(ScreenAttrs + off,
3069 screenattr + r * cols,
3070 (size_t)cols * sizeof(sattr_T));
3071 if (enc_utf8)
3072 {
3073 mch_memmove(ScreenLinesUC + off,
3074 screenlineUC + r * cols,
3075 (size_t)cols * sizeof(u8char_T));
3076 for (i = 0; i < p_mco; ++i)
3077 mch_memmove(ScreenLinesC[i] + off,
3078 screenlineC[i] + r * cols,
3079 (size_t)cols * sizeof(u8char_T));
3080 }
3081 if (enc_dbcs == DBCS_JPNU)
3082 mch_memmove(ScreenLines2 + off,
3083 screenline2 + r * cols,
3084 (size_t)cols * sizeof(schar_T));
zeertzjqd0c1b772024-03-16 15:03:33 +01003085 screen_line(curwin, cmdline_row + r, 0, cols, cols, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003086 }
3087 ret = 4;
3088 }
3089 }
3090
3091 vim_free(screenline);
3092 vim_free(screenattr);
3093 if (enc_utf8)
3094 {
3095 vim_free(screenlineUC);
3096 for (i = 0; i < p_mco; ++i)
3097 vim_free(screenlineC[i]);
3098 }
3099 if (enc_dbcs == DBCS_JPNU)
3100 vim_free(screenline2);
3101
3102 // Show the intro message when appropriate.
3103 maybe_intro_message();
3104
3105 setcursor();
3106
3107 return ret;
3108}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003109
3110/*
3111 * Invoked after an asynchronous callback is called.
3112 * If an echo command was used the cursor needs to be put back where
3113 * it belongs. If highlighting was changed a redraw is needed.
3114 * If "call_update_screen" is FALSE don't call update_screen() when at the
3115 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003116 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003117 */
3118 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003119redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003120{
3121 ++redrawing_for_callback;
3122
Bram Moolenaar24959102022-05-07 20:01:16 +01003123 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3124 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3125 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003126 {
3127 if (do_message)
3128 repeat_message();
3129 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003130 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003131 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003132 if (pum_visible())
3133 cmdline_pum_display();
Bram Moolenaar54162322022-08-26 16:58:51 +01003134
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003135 // Don't redraw when in prompt_for_number().
3136 if (cmdline_row > 0)
3137 {
3138 // Redrawing only works when the screen didn't scroll. Don't clear
3139 // wildmenu entries.
3140 if (msg_scrolled == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003141 && wild_menu_showing == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003142 && call_update_screen)
3143 update_screen(0);
3144
3145 // Redraw in the same position, so that the user can continue
3146 // editing the command.
3147 redrawcmdline_ex(FALSE);
3148 }
3149 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003150 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003151 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003152 update_topline();
3153 validate_cursor();
3154
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003155 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003156 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003157 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003158
3159 if (msg_scrolled == 0)
3160 {
3161 // don't want a hit-enter prompt when something else is displayed
3162 msg_didany = FALSE;
3163 need_wait_return = FALSE;
3164 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003165 }
3166 cursor_on();
3167#ifdef FEAT_GUI
3168 if (gui.in_use && !gui_mch_is_blink_off())
3169 // Don't update the cursor when it is blinking and off to avoid
3170 // flicker.
3171 out_flush_cursor(FALSE, FALSE);
3172 else
3173#endif
3174 out_flush();
3175
3176 --redrawing_for_callback;
3177}
3178
3179/*
3180 * Redraw the current window later, with update_screen(type).
3181 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003182 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003183 */
3184 void
3185redraw_later(int type)
3186{
3187 redraw_win_later(curwin, type);
3188}
3189
3190 void
3191redraw_win_later(
3192 win_T *wp,
3193 int type)
3194{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003195 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003196 {
3197 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003198 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003199 wp->w_lines_valid = 0;
3200 if (must_redraw < type) // must_redraw is the maximum of all windows
3201 must_redraw = type;
3202 }
3203}
3204
3205/*
3206 * Force a complete redraw later. Also resets the highlighting. To be used
3207 * after executing a shell command that messes up the screen.
3208 */
3209 void
3210redraw_later_clear(void)
3211{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003212 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003213 reset_screen_attr();
3214}
3215
3216/*
Bram Moolenaar13608d82022-08-29 15:06:50 +01003217 * Mark all windows to be redrawn later. Except popup windows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003218 */
3219 void
3220redraw_all_later(int type)
3221{
3222 win_T *wp;
3223
3224 FOR_ALL_WINDOWS(wp)
3225 redraw_win_later(wp, type);
3226 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003227 set_must_redraw(type);
3228}
3229
Bram Moolenaar13608d82022-08-29 15:06:50 +01003230#if 0 // not actually used yet, it probably should
3231/*
3232 * Mark all windows, including popup windows, to be redrawn.
3233 */
3234 void
3235redraw_all_windows_later(int type)
3236{
3237 redraw_all_later(type);
3238#ifdef FEAT_PROP_POPUP
3239 popup_redraw_all(); // redraw all popup windows
3240#endif
3241}
3242#endif
3243
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003244/*
3245 * Set "must_redraw" to "type" unless it already has a higher value
3246 * or it is currently not allowed.
3247 */
3248 void
3249set_must_redraw(int type)
3250{
3251 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003252 must_redraw = type;
3253}
3254
3255/*
3256 * Mark all windows that are editing the current buffer to be updated later.
3257 */
3258 void
3259redraw_curbuf_later(int type)
3260{
3261 redraw_buf_later(curbuf, type);
3262}
3263
3264 void
3265redraw_buf_later(buf_T *buf, int type)
3266{
3267 win_T *wp;
3268
3269 FOR_ALL_WINDOWS(wp)
3270 {
3271 if (wp->w_buffer == buf)
3272 redraw_win_later(wp, type);
3273 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003274#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3275 // terminal in popup window is not in list of windows
3276 if (curwin->w_buffer == buf)
3277 redraw_win_later(curwin, type);
3278#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003279}
3280
3281#if defined(FEAT_SIGNS) || defined(PROTO)
3282 void
3283redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3284{
3285 win_T *wp;
3286
3287 FOR_ALL_WINDOWS(wp)
3288 if (wp->w_buffer == buf && lnum >= wp->w_topline
3289 && lnum < wp->w_botline)
3290 redrawWinline(wp, lnum);
3291}
3292#endif
3293
3294#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3295 void
3296redraw_buf_and_status_later(buf_T *buf, int type)
3297{
3298 win_T *wp;
3299
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003300 if (wild_menu_showing != 0)
3301 // Don't redraw while the command line completion is displayed, it
3302 // would disappear.
3303 return;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003304 FOR_ALL_WINDOWS(wp)
3305 {
3306 if (wp->w_buffer == buf)
3307 {
3308 redraw_win_later(wp, type);
3309 wp->w_redr_status = TRUE;
3310 }
3311 }
3312}
3313#endif
3314
3315/*
3316 * mark all status lines for redraw; used after first :cd
3317 */
3318 void
3319status_redraw_all(void)
3320{
3321 win_T *wp;
3322
3323 FOR_ALL_WINDOWS(wp)
3324 if (wp->w_status_height)
3325 {
3326 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003327 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003328 }
3329}
3330
3331/*
3332 * mark all status lines of the current buffer for redraw
3333 */
3334 void
3335status_redraw_curbuf(void)
3336{
3337 win_T *wp;
3338
3339 FOR_ALL_WINDOWS(wp)
3340 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3341 {
3342 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003343 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003344 }
3345}
3346
3347/*
3348 * Redraw all status lines that need to be redrawn.
3349 */
3350 void
3351redraw_statuslines(void)
3352{
3353 win_T *wp;
3354
3355 FOR_ALL_WINDOWS(wp)
3356 if (wp->w_redr_status)
3357 win_redr_status(wp, FALSE);
3358 if (redraw_tabline)
3359 draw_tabline();
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003360
3361#if defined(FEAT_TABPANEL)
3362 if (redraw_tabpanel)
3363 draw_tabpanel();
3364#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003365}
3366
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003367/*
3368 * Redraw all status lines at the bottom of frame "frp".
3369 */
3370 void
3371win_redraw_last_status(frame_T *frp)
3372{
3373 if (frp->fr_layout == FR_LEAF)
3374 frp->fr_win->w_redr_status = TRUE;
3375 else if (frp->fr_layout == FR_ROW)
3376 {
3377 FOR_ALL_FRAMES(frp, frp->fr_child)
3378 win_redraw_last_status(frp);
3379 }
3380 else // frp->fr_layout == FR_COL
3381 {
3382 frp = frp->fr_child;
3383 while (frp->fr_next != NULL)
3384 frp = frp->fr_next;
3385 win_redraw_last_status(frp);
3386 }
3387}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003388
3389/*
3390 * Changed something in the current window, at buffer line "lnum", that
3391 * requires that line and possibly other lines to be redrawn.
3392 * Used when entering/leaving Insert mode with the cursor on a folded line.
3393 * Used to remove the "$" from a change command.
3394 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3395 * may become invalid and the whole window will have to be redrawn.
3396 */
3397 void
3398redrawWinline(
3399 win_T *wp,
3400 linenr_T lnum)
3401{
Luuk van Baal7bbb0f32025-02-22 09:19:04 +01003402 redraw_win_range_later(wp, lnum, lnum);
3403}
3404
3405 void
3406redraw_win_range_later(
3407 win_T *wp,
3408 linenr_T first,
3409 linenr_T last)
3410{
3411 if (last >= wp->w_topline && first < wp->w_botline)
3412 {
3413 if (wp->w_redraw_top == 0 || wp->w_redraw_top > first)
3414 wp->w_redraw_top = first;
3415 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < last)
3416 wp->w_redraw_bot = last;
3417 redraw_win_later(wp, UPD_VALID);
3418 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003419}