blob: 1e71caef1b5977c8de846fe362d00463f4d4017e [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
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200544 screen_puts(p, row, wp->w_wincol + TPL_LCOL(wp), attr);
545 screen_fill(row, row + 1, plen + wp->w_wincol + TPL_LCOL(wp),
546 this_ru_col + wp->w_wincol + TPL_LCOL(wp), 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
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200550 - 1 + wp->w_wincol + TPL_LCOL(wp)), 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);
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200575 if (W_ENDCOL(wp) < COLUMNS_WITHOUT_TPL())
576 screen_putchar(fillchar, row, W_ENDCOL(wp) + TPL_LCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200577 }
578 busy = FALSE;
579}
580
581#ifdef FEAT_STL_OPT
582/*
583 * Redraw the status line according to 'statusline' and take care of any
584 * errors encountered.
585 */
586 static void
587redraw_custom_statusline(win_T *wp)
588{
589 static int entered = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200590
591 // When called recursively return. This can happen when the statusline
592 // contains an expression that triggers a redraw.
593 if (entered)
594 return;
595 entered = TRUE;
596
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200597 win_redr_custom(wp, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200598 entered = FALSE;
599}
600#endif
601
602/*
603 * Show current status info in ruler and various other places
604 * If always is FALSE, only show ruler if position has changed.
605 */
606 void
607showruler(int always)
608{
609 if (!always && !redrawing())
610 return;
611 if (pum_visible())
612 {
613 // Don't redraw right now, do it later.
614 curwin->w_redr_status = TRUE;
615 return;
616 }
617#if defined(FEAT_STL_OPT)
618 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
619 redraw_custom_statusline(curwin);
620 else
621#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200622 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200623
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200624 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000625#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200626 || (p_icon && (stl_syntax & STL_IN_ICON))
627 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000628#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200629 )
630 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000631
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200632 // Redraw the tab pages line if needed.
633 if (redraw_tabline)
634 draw_tabline();
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200635
636#if defined(FEAT_TABPANEL)
637 if (redraw_tabpanel)
638 draw_tabpanel();
639#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200640}
641
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200642 void
643win_redr_ruler(win_T *wp, int always, int ignore_pum)
644{
John Marriotta21240b2025-01-08 20:10:59 +0100645 int empty_line = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200646
Bram Moolenaara2a89732022-08-31 14:46:18 +0100647 // If 'ruler' off don't do anything
648 if (!p_ru)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200649 return;
650
651 /*
652 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
653 * after deleting lines, before cursor.lnum is corrected.
654 */
655 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
656 return;
657
658 // Don't draw the ruler while doing insert-completion, it might overwrite
659 // the (long) mode message.
660 if (wp == lastwin && lastwin->w_status_height == 0)
661 if (edit_submode != NULL)
662 return;
John Marriotta21240b2025-01-08 20:10:59 +0100663
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200664 // Don't draw the ruler when the popup menu is visible, it may overlap.
665 // Except when the popup menu will be redrawn anyway.
666 if (!ignore_pum && pum_visible())
667 return;
668
669#ifdef FEAT_STL_OPT
Bram Moolenaara2a89732022-08-31 14:46:18 +0100670 if (*p_ruf)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200671 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200672 win_redr_custom(wp, TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200673 return;
674 }
675#endif
676
677 /*
678 * Check if not in Insert mode and the line is empty (will show "0-1").
679 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100680 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200681 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
682 empty_line = TRUE;
683
684 /*
685 * Only draw the ruler when something changed.
686 */
687 validate_virtcol_win(wp);
688 if ( redraw_cmdline
689 || always
690 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
691 || wp->w_cursor.col != wp->w_ru_cursor.col
692 || wp->w_virtcol != wp->w_ru_virtcol
693 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
694 || wp->w_topline != wp->w_ru_topline
695 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
696#ifdef FEAT_DIFF
697 || wp->w_topfill != wp->w_ru_topfill
698#endif
699 || empty_line != wp->w_ru_empty)
700 {
John Marriotta21240b2025-01-08 20:10:59 +0100701 int row;
702 int fillchar;
703 int attr;
704 int off;
705 int width;
706 colnr_T virtcol;
707#define RULER_BUF_LEN 70
708 char_u buffer[RULER_BUF_LEN];
709 int bufferlen;
710 char_u rel_pos[RULER_BUF_LEN];
711 int rel_poslen;
712 int this_ru_col;
713 int n1; // scratch value
714 int n2; // scratch value
715
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200716 cursor_off();
717 if (wp->w_status_height)
718 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200719 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200720 fillchar = fillchar_status(&attr, wp);
721 off = wp->w_wincol;
722 width = wp->w_width;
723 }
724 else
725 {
726 row = Rows - 1;
727 fillchar = ' ';
728 attr = 0;
729 width = Columns;
730 off = 0;
731 }
732
733 // In list mode virtcol needs to be recomputed
734 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100735 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200736 {
737 wp->w_p_list = FALSE;
738 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
739 wp->w_p_list = TRUE;
740 }
741
John Marriotta21240b2025-01-08 20:10:59 +0100742 bufferlen = vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200743 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
744 ? 0L
745 : (long)(wp->w_cursor.lnum));
John Marriotta21240b2025-01-08 20:10:59 +0100746 bufferlen += col_print(buffer + bufferlen, RULER_BUF_LEN - bufferlen,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200747 empty_line ? 0 : (int)wp->w_cursor.col + 1,
748 (int)virtcol + 1);
749
750 /*
751 * Add a "50%" if there is room for it.
752 * On the last line, don't print in the last column (scrolls the
753 * screen up on some terminals).
754 */
John Marriotta21240b2025-01-08 20:10:59 +0100755 rel_poslen = get_rel_pos(wp, rel_pos, RULER_BUF_LEN);
756 n1 = bufferlen + vim_strsize(rel_pos);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200757 if (wp->w_status_height == 0) // can't use last char of screen
John Marriotta21240b2025-01-08 20:10:59 +0100758 ++n1;
759
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200760 this_ru_col = ru_col - (Columns - width);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200761 // Never use more than half the window/screen width, leave the other
762 // half for the filename.
John Marriotta21240b2025-01-08 20:10:59 +0100763 n2 = (width + 1) / 2;
764 if (this_ru_col < n2)
765 this_ru_col = n2;
766 if (this_ru_col + n1 < width)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200767 {
John Marriotta21240b2025-01-08 20:10:59 +0100768 // need at least space for rel_pos + NUL
769 while (this_ru_col + n1 < width
770 && RULER_BUF_LEN > bufferlen + rel_poslen + 1) // +1 for NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200771 {
772 if (has_mbyte)
John Marriotta21240b2025-01-08 20:10:59 +0100773 bufferlen += (*mb_char2bytes)(fillchar, buffer + bufferlen);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200774 else
John Marriotta21240b2025-01-08 20:10:59 +0100775 buffer[bufferlen++] = fillchar;
776 ++n1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200777 }
John Marriotta21240b2025-01-08 20:10:59 +0100778 bufferlen += vim_snprintf((char *)buffer + bufferlen, RULER_BUF_LEN - bufferlen,
779 "%s", rel_pos);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200780 }
781 // Truncate at window boundary.
782 if (has_mbyte)
783 {
John Marriotta21240b2025-01-08 20:10:59 +0100784 for (n1 = 0, n2 = 0; buffer[n1] != NUL; n1 += (*mb_ptr2len)(buffer + n1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200785 {
John Marriotta21240b2025-01-08 20:10:59 +0100786 n2 += (*mb_ptr2cells)(buffer + n1);
787 if (this_ru_col + n2 > width)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200788 {
John Marriotta21240b2025-01-08 20:10:59 +0100789 bufferlen = n1;
790 buffer[bufferlen] = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200791 break;
792 }
793 }
794 }
John Marriotta21240b2025-01-08 20:10:59 +0100795 else if (this_ru_col + bufferlen > width)
796 {
797 bufferlen = width - this_ru_col;
798 buffer[bufferlen] = NUL;
799 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200800
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200801 screen_puts(buffer, row, this_ru_col + off + TPL_LCOL(wp), attr);
John Marriotta21240b2025-01-08 20:10:59 +0100802 n1 = redraw_cmdline;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200803 screen_fill(row, row + 1,
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200804 this_ru_col + off + bufferlen + TPL_LCOL(wp),
805 (off + width) + TPL_LCOL(wp),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200806 fillchar, fillchar, attr);
807 // don't redraw the cmdline because of showing the ruler
John Marriotta21240b2025-01-08 20:10:59 +0100808 redraw_cmdline = n1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200809 wp->w_ru_cursor = wp->w_cursor;
810 wp->w_ru_virtcol = wp->w_virtcol;
811 wp->w_ru_empty = empty_line;
812 wp->w_ru_topline = wp->w_topline;
813 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
814#ifdef FEAT_DIFF
815 wp->w_ru_topfill = wp->w_topfill;
816#endif
817 }
818}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200819
820/*
821 * To be called when "updating_screen" was set before and now the postponed
822 * side effects may take place.
823 */
824 void
825after_updating_screen(int may_resize_shell UNUSED)
826{
827 updating_screen = FALSE;
828#ifdef FEAT_GUI
829 if (may_resize_shell)
830 gui_may_resize_shell();
831#endif
832#ifdef FEAT_TERMINAL
833 term_check_channel_closed_recently();
834#endif
835
836#ifdef HAVE_DROP_FILE
837 // If handle_drop() was called while updating_screen was TRUE need to
838 // handle the drop now.
839 handle_any_postponed_drop();
840#endif
841}
842
843/*
844 * Update all windows that are editing the current buffer.
845 */
846 void
847update_curbuf(int type)
848{
849 redraw_curbuf_later(type);
850 update_screen(type);
851}
852
853#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
854/*
855 * Copy "text" to ScreenLines using "attr".
856 * Returns the next screen column.
857 */
858 static int
859text_to_screenline(win_T *wp, char_u *text, int col)
860{
861 int off = (int)(current_ScreenLine - ScreenLines);
862
863 if (has_mbyte)
864 {
865 int cells;
866 int u8c, u8cc[MAX_MCO];
867 int i;
868 int idx;
869 int c_len;
870 char_u *p;
871# ifdef FEAT_ARABIC
872 int prev_c = 0; // previous Arabic character
873 int prev_c1 = 0; // first composing char for prev_c
874# endif
875
876# ifdef FEAT_RIGHTLEFT
877 if (wp->w_p_rl)
878 idx = off;
879 else
880# endif
881 idx = off + col;
882
883 // Store multibyte characters in ScreenLines[] et al. correctly.
884 for (p = text; *p != NUL; )
885 {
886 cells = (*mb_ptr2cells)(p);
887 c_len = (*mb_ptr2len)(p);
888 if (col + cells > wp->w_width
889# ifdef FEAT_RIGHTLEFT
890 - (wp->w_p_rl ? col : 0)
891# endif
892 )
893 break;
894 ScreenLines[idx] = *p;
895 if (enc_utf8)
896 {
897 u8c = utfc_ptr2char(p, u8cc);
898 if (*p < 0x80 && u8cc[0] == 0)
899 {
900 ScreenLinesUC[idx] = 0;
901#ifdef FEAT_ARABIC
902 prev_c = u8c;
903#endif
904 }
905 else
906 {
907#ifdef FEAT_ARABIC
908 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
909 {
910 // Do Arabic shaping.
911 int pc, pc1, nc;
912 int pcc[MAX_MCO];
913 int firstbyte = *p;
914
915 // The idea of what is the previous and next
916 // character depends on 'rightleft'.
917 if (wp->w_p_rl)
918 {
919 pc = prev_c;
920 pc1 = prev_c1;
921 nc = utf_ptr2char(p + c_len);
922 prev_c1 = u8cc[0];
923 }
924 else
925 {
926 pc = utfc_ptr2char(p + c_len, pcc);
927 nc = prev_c;
928 pc1 = pcc[0];
929 }
930 prev_c = u8c;
931
932 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
933 pc, pc1, nc);
934 ScreenLines[idx] = firstbyte;
935 }
936 else
937 prev_c = u8c;
938#endif
939 // Non-BMP character: display as ? or fullwidth ?.
940 ScreenLinesUC[idx] = u8c;
941 for (i = 0; i < Screen_mco; ++i)
942 {
943 ScreenLinesC[i][idx] = u8cc[i];
944 if (u8cc[i] == 0)
945 break;
946 }
947 }
948 if (cells > 1)
949 ScreenLines[idx + 1] = 0;
950 }
951 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
952 // double-byte single width character
953 ScreenLines2[idx] = p[1];
954 else if (cells > 1)
955 // double-width character
956 ScreenLines[idx + 1] = p[1];
957 col += cells;
958 idx += cells;
959 p += c_len;
960 }
961 }
962 else
963 {
964 int len = (int)STRLEN(text);
John Marriotta21240b2025-01-08 20:10:59 +0100965 int n = wp->w_width - col;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200966
John Marriotta21240b2025-01-08 20:10:59 +0100967 if (len > n)
968 len = n;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200969 if (len > 0)
970 {
971#ifdef FEAT_RIGHTLEFT
972 if (wp->w_p_rl)
973 mch_memmove(current_ScreenLine, text, len);
974 else
975#endif
976 mch_memmove(current_ScreenLine + col, text, len);
977 col += len;
978 }
979 }
980 return col;
981}
982#endif
983
984#ifdef FEAT_MENU
985/*
986 * Draw the window toolbar.
987 */
988 static void
989redraw_win_toolbar(win_T *wp)
990{
991 vimmenu_T *menu;
992 int item_idx = 0;
993 int item_count = 0;
994 int col = 0;
995 int next_col;
996 int off = (int)(current_ScreenLine - ScreenLines);
997 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
998 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
999
1000 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001001 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001002 ++item_count;
1003 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
1004
1005 // TODO: use fewer spaces if there is not enough room
1006 for (menu = wp->w_winbar->children;
1007 menu != NULL && col < wp->w_width; menu = menu->next)
1008 {
1009 space_to_screenline(off + col, fill_attr);
1010 if (++col >= wp->w_width)
1011 break;
1012 if (col > 1)
1013 {
1014 space_to_screenline(off + col, fill_attr);
1015 if (++col >= wp->w_width)
1016 break;
1017 }
1018
1019 wp->w_winbar_items[item_idx].wb_startcol = col;
1020 space_to_screenline(off + col, button_attr);
1021 if (++col >= wp->w_width)
1022 break;
1023
1024 next_col = text_to_screenline(wp, menu->name, col);
1025 while (col < next_col)
1026 {
1027 ScreenAttrs[off + col] = button_attr;
1028 ++col;
1029 }
1030 wp->w_winbar_items[item_idx].wb_endcol = col;
1031 wp->w_winbar_items[item_idx].wb_menu = menu;
1032 ++item_idx;
1033
1034 if (col >= wp->w_width)
1035 break;
1036 space_to_screenline(off + col, button_attr);
1037 ++col;
1038 }
1039 while (col < wp->w_width)
1040 {
1041 space_to_screenline(off + col, fill_attr);
1042 ++col;
1043 }
1044 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1045
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02001046 screen_line(wp, wp->w_winrow, wp->w_wincol + TPL_LCOL(wp), wp->w_width,
1047 wp->w_width, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001048}
1049#endif
1050
1051#if defined(FEAT_FOLDING) || defined(PROTO)
1052/*
1053 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1054 */
1055 static void
1056copy_text_attr(
1057 int off,
1058 char_u *buf,
1059 int len,
1060 int attr)
1061{
1062 int i;
1063
1064 mch_memmove(ScreenLines + off, buf, (size_t)len);
1065 if (enc_utf8)
1066 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1067 for (i = 0; i < len; ++i)
1068 ScreenAttrs[off + i] = attr;
1069}
1070
1071/*
1072 * Display one folded line.
1073 */
1074 static void
1075fold_line(
1076 win_T *wp,
1077 long fold_count,
1078 foldinfo_T *foldinfo,
1079 linenr_T lnum,
1080 int row)
1081{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001082 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1083 // multi-byte character is MAX_MCO.
1084 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001085 pos_T *top, *bot;
1086 linenr_T lnume = lnum + fold_count - 1;
1087 int len;
1088 char_u *text;
1089 int fdc;
1090 int col;
1091 int txtcol;
1092 int off = (int)(current_ScreenLine - ScreenLines);
1093 int ri;
1094
1095 // Build the fold line:
1096 // 1. Add the cmdwin_type for the command-line window
1097 // 2. Add the 'foldcolumn'
1098 // 3. Add the 'number' or 'relativenumber' column
1099 // 4. Compose the text
1100 // 5. Add the text
1101 // 6. set highlighting for the Visual area an other text
1102 col = 0;
1103
1104 // 1. Add the cmdwin_type for the command-line window
1105 // Ignores 'rightleft', this window is never right-left.
Sean Dewar988f7432023-08-16 14:17:36 +01001106 if (wp == cmdwin_win)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001107 {
1108 ScreenLines[off] = cmdwin_type;
1109 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1110 if (enc_utf8)
1111 ScreenLinesUC[off] = 0;
1112 ++col;
1113 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001114
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001115#ifdef FEAT_RIGHTLEFT
1116# define RL_MEMSET(p, v, l) \
1117 do { \
1118 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001119 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001120 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1121 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001122 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001123 ScreenAttrs[off + (p) + ri] = v; \
1124 } while (0)
1125#else
1126# define RL_MEMSET(p, v, l) \
1127 do { \
1128 for (ri = 0; ri < l; ++ri) \
1129 ScreenAttrs[off + (p) + ri] = v; \
1130 } while (0)
1131#endif
1132
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001133 // 2. Add the 'foldcolumn'
1134 // Reduce the width when there is not enough space.
1135 fdc = compute_foldcolumn(wp, col);
1136 if (fdc > 0)
1137 {
1138 char_u *p;
1139 int i;
1140 int idx;
1141
1142 fill_foldcolumn(buf, wp, TRUE, lnum);
1143 p = buf;
1144 for (i = 0; i < fdc; i++)
1145 {
1146 int ch;
1147
1148 if (has_mbyte)
1149 ch = mb_ptr2char_adv(&p);
1150 else
1151 ch = *p++;
1152#ifdef FEAT_RIGHTLEFT
1153 if (wp->w_p_rl)
1154 idx = off + wp->w_width - i - 1 - col;
1155 else
1156#endif
1157 idx = off + col + i;
1158 if (enc_utf8)
1159 {
1160 if (ch >= 0x80)
1161 {
1162 ScreenLinesUC[idx] = ch;
1163 ScreenLinesC[0][idx] = 0;
1164 ScreenLines[idx] = 0x80;
1165 }
1166 else
1167 {
1168 ScreenLines[idx] = ch;
1169 ScreenLinesUC[idx] = 0;
1170 }
1171 }
1172 else
1173 ScreenLines[idx] = ch;
1174 }
1175
1176 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1177 col += fdc;
1178 }
1179
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001180 // Set all attributes of the 'number' or 'relativenumber' column and the
1181 // text
1182 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1183
1184#ifdef FEAT_SIGNS
1185 // If signs are being displayed, add two spaces.
1186 if (signcolumn_on(wp))
1187 {
1188 len = wp->w_width - col;
1189 if (len > 0)
1190 {
1191 if (len > 2)
1192 len = 2;
1193# ifdef FEAT_RIGHTLEFT
1194 if (wp->w_p_rl)
1195 // the line number isn't reversed
1196 copy_text_attr(off + wp->w_width - len - col,
1197 (char_u *)" ", len, HL_ATTR(HLF_FL));
1198 else
1199# endif
1200 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1201 col += len;
1202 }
1203 }
1204#endif
1205
1206 // 3. Add the 'number' or 'relativenumber' column
1207 if (wp->w_p_nu || wp->w_p_rnu)
1208 {
1209 len = wp->w_width - col;
1210 if (len > 0)
1211 {
1212 int w = number_width(wp);
1213 long num;
1214 char *fmt = "%*ld ";
1215
1216 if (len > w + 1)
1217 len = w + 1;
1218
1219 if (wp->w_p_nu && !wp->w_p_rnu)
1220 // 'number' + 'norelativenumber'
1221 num = (long)lnum;
1222 else
1223 {
1224 // 'relativenumber', don't use negative numbers
1225 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1226 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1227 {
1228 // 'number' + 'relativenumber': cursor line shows absolute
1229 // line number
1230 num = lnum;
1231 fmt = "%-*ld ";
1232 }
1233 }
1234
John Marriotta21240b2025-01-08 20:10:59 +01001235 vim_snprintf((char *)buf, sizeof(buf), fmt, w, num);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001236#ifdef FEAT_RIGHTLEFT
1237 if (wp->w_p_rl)
1238 // the line number isn't reversed
1239 copy_text_attr(off + wp->w_width - len - col, buf, len,
1240 HL_ATTR(HLF_FL));
1241 else
1242#endif
1243 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1244 col += len;
1245 }
1246 }
1247
1248 // 4. Compose the folded-line string with 'foldtext', if set.
1249 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1250
1251 txtcol = col; // remember where text starts
1252
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001253 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1254 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001255 // Right-left text is put in columns 0 - number-col, normal text is put
1256 // in columns number-col - window-width.
1257 col = text_to_screenline(wp, text, col);
1258
1259 // Fill the rest of the line with the fold filler
1260#ifdef FEAT_RIGHTLEFT
1261 if (wp->w_p_rl)
1262 col -= txtcol;
1263#endif
1264 while (col < wp->w_width
1265#ifdef FEAT_RIGHTLEFT
1266 - (wp->w_p_rl ? txtcol : 0)
1267#endif
1268 )
1269 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001270 int c = wp->w_fill_chars.fold;
1271
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001272 if (enc_utf8)
1273 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001274 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001275 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001276 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001277 ScreenLinesC[0][off + col] = 0;
1278 ScreenLines[off + col] = 0x80; // avoid storing zero
1279 }
1280 else
1281 {
1282 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001283 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001284 }
1285 col++;
1286 }
1287 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001288 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001289 }
1290
1291 if (text != buf)
1292 vim_free(text);
1293
1294 // 6. set highlighting for the Visual area an other text.
1295 // If all folded lines are in the Visual area, highlight the line.
1296 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1297 {
1298 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1299 {
1300 // Visual is after curwin->w_cursor
1301 top = &curwin->w_cursor;
1302 bot = &VIsual;
1303 }
1304 else
1305 {
1306 // Visual is before curwin->w_cursor
1307 top = &VIsual;
1308 bot = &curwin->w_cursor;
1309 }
1310 if (lnum >= top->lnum
1311 && lnume <= bot->lnum
1312 && (VIsual_mode != 'v'
1313 || ((lnum > top->lnum
1314 || (lnum == top->lnum
1315 && top->col == 0))
1316 && (lnume < bot->lnum
1317 || (lnume == bot->lnum
1318 && (bot->col - (*p_sel == 'e'))
zeertzjq94b7c322024-03-12 21:50:32 +01001319 >= ml_get_buf_len(wp->w_buffer, lnume))))))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001320 {
1321 if (VIsual_mode == Ctrl_V)
1322 {
1323 // Visual block mode: highlight the chars part of the block
1324 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1325 {
1326 if (wp->w_old_cursor_lcol != MAXCOL
1327 && wp->w_old_cursor_lcol + txtcol
1328 < (colnr_T)wp->w_width)
1329 len = wp->w_old_cursor_lcol;
1330 else
1331 len = wp->w_width - txtcol;
1332 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1333 len - (int)wp->w_old_cursor_fcol);
1334 }
1335 }
1336 else
1337 {
1338 // Set all attributes of the text
1339 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1340 }
1341 }
1342 }
1343
1344#ifdef FEAT_SYN_HL
1345 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1346 if (wp->w_p_cc_cols)
1347 {
1348 int i = 0;
1349 int j = wp->w_p_cc_cols[i];
1350 int old_txtcol = txtcol;
1351
1352 while (j > -1)
1353 {
1354 txtcol += j;
1355 if (wp->w_p_wrap)
1356 txtcol -= wp->w_skipcol;
1357 else
1358 txtcol -= wp->w_leftcol;
1359 if (txtcol >= 0 && txtcol < wp->w_width)
1360 ScreenAttrs[off + txtcol] = hl_combine_attr(
1361 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1362 txtcol = old_txtcol;
1363 j = wp->w_p_cc_cols[++i];
1364 }
1365 }
1366
1367 // Show 'cursorcolumn' in the fold line.
1368 if (wp->w_p_cuc)
1369 {
1370 txtcol += wp->w_virtcol;
1371 if (wp->w_p_wrap)
1372 txtcol -= wp->w_skipcol;
1373 else
1374 txtcol -= wp->w_leftcol;
1375 if (txtcol >= 0 && txtcol < wp->w_width)
1376 ScreenAttrs[off + txtcol] = hl_combine_attr(
1377 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1378 }
1379#endif
1380
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02001381 screen_line(wp, row + W_WINROW(wp), wp->w_wincol + TPL_LCOL(wp),
1382 wp->w_width, wp->w_width, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001383
1384 // Update w_cline_height and w_cline_folded if the cursor line was
1385 // updated (saves a call to plines() later).
1386 if (wp == curwin
1387 && lnum <= curwin->w_cursor.lnum
1388 && lnume >= curwin->w_cursor.lnum)
1389 {
1390 curwin->w_cline_row = row;
1391 curwin->w_cline_height = 1;
1392 curwin->w_cline_folded = TRUE;
1393 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1394 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001395
1396# ifdef FEAT_CONCEAL
1397 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001398 if (wp == curwin
1399 && wp->w_cursor.lnum >= lnum
1400 && wp->w_cursor.lnum <= lnume
1401 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001402 curs_columns(TRUE);
1403# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001404}
1405#endif
1406
1407/*
1408 * Update a single window.
1409 *
1410 * This may cause the windows below it also to be redrawn (when clearing the
1411 * screen or scrolling lines).
1412 *
1413 * How the window is redrawn depends on wp->w_redr_type. Each type also
1414 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001415 * UPD_NOT_VALID redraw the whole window
1416 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1417 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1418 * UPD_VALID
1419 * UPD_INVERTED redraw the changed part of the Visual area
1420 * UPD_INVERTED_ALL redraw the whole Visual area
1421 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001422 * 2. update lines at the top when scrolled down
1423 * 3. redraw changed text:
1424 * - if wp->w_buffer->b_mod_set set, update lines between
1425 * b_mod_top and b_mod_bot.
1426 * - if wp->w_redraw_top non-zero, redraw lines between
zeertzjqf2d90a32024-02-12 20:28:01 +01001427 * wp->w_redraw_top and wp->w_redraw_bot.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001428 * - continue redrawing when syntax status is invalid.
1429 * 4. if scrolled up, update lines at the bottom.
1430 * This results in three areas that may need updating:
1431 * top: from first row to top_end (when scrolled down)
1432 * mid: from mid_start to mid_end (update inversion or changed text)
1433 * bot: from bot_start to last row (when scrolled up)
1434 */
1435 static void
1436win_update(win_T *wp)
1437{
1438 buf_T *buf = wp->w_buffer;
1439 int type;
1440 int top_end = 0; // Below last row of the top area that needs
1441 // updating. 0 when no top area updating.
1442 int mid_start = 999;// first row of the mid area that needs
1443 // updating. 999 when no mid area updating.
1444 int mid_end = 0; // Below last row of the mid area that needs
1445 // updating. 0 when no mid area updating.
1446 int bot_start = 999;// first row of the bot area that needs
1447 // updating. 999 when no bot area updating
1448 int scrolled_down = FALSE; // TRUE when scrolled down when
1449 // w_topline got smaller a bit
1450#ifdef FEAT_SEARCH_EXTRA
1451 int top_to_mod = FALSE; // redraw above mod_top
1452#endif
1453
1454 int row; // current window row to display
1455 linenr_T lnum; // current buffer lnum to display
1456 int idx; // current index in w_lines[]
1457 int srow; // starting row of the current line
1458
1459 int eof = FALSE; // if TRUE, we hit the end of the file
1460 int didline = FALSE; // if TRUE, we finished the last line
1461 int i;
1462 long j;
1463 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001464 linenr_T old_botline = wp->w_botline;
1465#ifdef FEAT_CONCEAL
1466 int old_wrow = wp->w_wrow;
1467 int old_wcol = wp->w_wcol;
1468#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001469#ifdef FEAT_FOLDING
1470 long fold_count;
1471#endif
1472#ifdef FEAT_SYN_HL
1473 // remember what happened to the previous line, to know if
1474 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001475# define DID_NONE 1 // didn't update a line
1476# define DID_LINE 2 // updated a normal line
1477# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001478 int did_update = DID_NONE;
1479 linenr_T syntax_last_parsed = 0; // last parsed text line
1480#endif
1481 linenr_T mod_top = 0;
1482 linenr_T mod_bot = 0;
1483#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1484 int save_got_int;
1485#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001486
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001487#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1488 // This needs to be done only for the first window when update_screen() is
1489 // called.
1490 if (!did_update_one_window)
1491 {
1492 did_update_one_window = TRUE;
1493# ifdef FEAT_SEARCH_EXTRA
1494 start_search_hl();
1495# endif
1496# ifdef FEAT_CLIPBOARD
1497 // When Visual area changed, may have to update selection.
1498 if (clip_star.available && clip_isautosel_star())
1499 clip_update_selection(&clip_star);
1500 if (clip_plus.available && clip_isautosel_plus())
1501 clip_update_selection(&clip_plus);
1502# endif
1503 }
1504#endif
1505
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001506 type = wp->w_redr_type;
1507
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001508 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001509 {
1510 wp->w_redr_status = TRUE;
1511 wp->w_lines_valid = 0;
1512 }
1513
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001514 // Window frame is zero-height: nothing to draw.
1515 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1516 || (wp->w_frame->fr_height == wp->w_status_height
1517#if defined(FEAT_PROP_POPUP)
1518 && !popup_is_popup(wp)
1519#endif
1520 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001521 {
1522 wp->w_redr_type = 0;
1523 return;
1524 }
1525
1526 // Window is zero-width: Only need to draw the separator.
1527 if (wp->w_width == 0)
1528 {
1529 // draw the vertical separator right of this window
1530 draw_vsep_win(wp, 0);
1531 wp->w_redr_type = 0;
1532 return;
1533 }
1534
1535#ifdef FEAT_TERMINAL
1536 // If this window contains a terminal, redraw works completely differently.
1537 if (term_do_update_window(wp))
1538 {
1539 term_update_window(wp);
1540# ifdef FEAT_MENU
1541 // Draw the window toolbar, if there is one.
1542 if (winbar_height(wp) > 0)
1543 redraw_win_toolbar(wp);
1544# endif
1545 wp->w_redr_type = 0;
1546 return;
1547 }
1548#endif
1549
1550#ifdef FEAT_SEARCH_EXTRA
1551 init_search_hl(wp, &screen_search_hl);
1552#endif
1553
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001554 // Make sure skipcol is valid, it depends on various options and the window
1555 // width.
Sean Dewar02fcae02024-02-21 19:40:44 +01001556 if (wp->w_skipcol > 0 && wp->w_width > win_col_off(wp))
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001557 {
1558 int w = 0;
1559 int width1 = wp->w_width - win_col_off(wp);
1560 int width2 = width1 + win_col_off2(wp);
1561 int add = width1;
1562
1563 while (w < wp->w_skipcol)
1564 {
1565 if (w > 0)
1566 add = width2;
1567 w += add;
1568 }
1569 if (w != wp->w_skipcol)
1570 // always round down, the higher value may not be valid
1571 wp->w_skipcol = w - add;
1572 }
1573
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001574#ifdef FEAT_LINEBREAK
1575 // Force redraw when width of 'number' or 'relativenumber' column
1576 // changes.
1577 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1578 if (wp->w_nrwidth != i)
1579 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001580 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001581 wp->w_nrwidth = i;
1582 }
1583 else
1584#endif
1585
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001586 {
1587 // Set mod_top to the first line that needs displaying because of
1588 // changes. Set mod_bot to the first line after the changes.
1589 mod_top = wp->w_redraw_top;
1590 if (wp->w_redraw_bot != 0)
1591 mod_bot = wp->w_redraw_bot + 1;
1592 else
1593 mod_bot = 0;
1594 if (buf->b_mod_set)
1595 {
1596 if (mod_top == 0 || mod_top > buf->b_mod_top)
1597 {
1598 mod_top = buf->b_mod_top;
1599#ifdef FEAT_SYN_HL
1600 // Need to redraw lines above the change that may be included
1601 // in a pattern match.
1602 if (syntax_present(wp))
1603 {
1604 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1605 if (mod_top < 1)
1606 mod_top = 1;
1607 }
1608#endif
1609 }
1610 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1611 mod_bot = buf->b_mod_bot;
1612
1613#ifdef FEAT_SEARCH_EXTRA
1614 // When 'hlsearch' is on and using a multi-line search pattern, a
1615 // change in one line may make the Search highlighting in a
1616 // previous line invalid. Simple solution: redraw all visible
1617 // lines above the change.
1618 // Same for a match pattern.
1619 if (screen_search_hl.rm.regprog != NULL
1620 && re_multiline(screen_search_hl.rm.regprog))
1621 top_to_mod = TRUE;
1622 else
1623 {
1624 matchitem_T *cur = wp->w_match_head;
1625
1626 while (cur != NULL)
1627 {
Bram Moolenaar50faf022022-09-29 12:50:17 +01001628 if (cur->mit_match.regprog != NULL
1629 && re_multiline(cur->mit_match.regprog))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001630 {
1631 top_to_mod = TRUE;
1632 break;
1633 }
Bram Moolenaar50faf022022-09-29 12:50:17 +01001634 cur = cur->mit_next;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001635 }
1636 }
1637#endif
1638 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001639
1640#ifdef FEAT_SEARCH_EXTRA
1641 if (search_hl_has_cursor_lnum > 0)
1642 {
1643 // CurSearch was used last time, need to redraw the line with it to
1644 // avoid having two matches highlighted with CurSearch.
1645 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1646 mod_top = search_hl_has_cursor_lnum;
1647 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1648 mod_bot = search_hl_has_cursor_lnum + 1;
1649 }
1650#endif
1651
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001652#ifdef FEAT_FOLDING
1653 if (mod_top != 0 && hasAnyFolding(wp))
1654 {
1655 linenr_T lnumt, lnumb;
1656
1657 // A change in a line can cause lines above it to become folded or
1658 // unfolded. Find the top most buffer line that may be affected.
1659 // If the line was previously folded and displayed, get the first
1660 // line of that fold. If the line is folded now, get the first
1661 // folded line. Use the minimum of these two.
1662
1663 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1664 // the line below it. If there is no valid entry, use w_topline.
1665 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1666 // to this line. If there is no valid entry, use MAXLNUM.
1667 lnumt = wp->w_topline;
1668 lnumb = MAXLNUM;
1669 for (i = 0; i < wp->w_lines_valid; ++i)
1670 if (wp->w_lines[i].wl_valid)
1671 {
1672 if (wp->w_lines[i].wl_lastlnum < mod_top)
1673 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1674 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1675 {
1676 lnumb = wp->w_lines[i].wl_lnum;
1677 // When there is a fold column it might need updating
1678 // in the next line ("J" just above an open fold).
1679 if (compute_foldcolumn(wp, 0) > 0)
1680 ++lnumb;
1681 }
1682 }
1683
1684 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1685 if (mod_top > lnumt)
1686 mod_top = lnumt;
1687
1688 // Now do the same for the bottom line (one above mod_bot).
1689 --mod_bot;
1690 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1691 ++mod_bot;
1692 if (mod_bot < lnumb)
1693 mod_bot = lnumb;
1694 }
1695#endif
1696
1697 // When a change starts above w_topline and the end is below
1698 // w_topline, start redrawing at w_topline.
1699 // If the end of the change is above w_topline: do like no change was
1700 // made, but redraw the first line to find changes in syntax.
1701 if (mod_top != 0 && mod_top < wp->w_topline)
1702 {
1703 if (mod_bot > wp->w_topline)
1704 mod_top = wp->w_topline;
1705#ifdef FEAT_SYN_HL
1706 else if (syntax_present(wp))
1707 top_end = 1;
1708#endif
1709 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001710 }
1711 wp->w_redraw_top = 0; // reset for next time
1712 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001713#ifdef FEAT_SEARCH_EXTRA
1714 search_hl_has_cursor_lnum = 0;
1715#endif
1716
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001717
1718 // When only displaying the lines at the top, set top_end. Used when
1719 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001720 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001721 {
1722 j = 0;
1723 for (i = 0; i < wp->w_lines_valid; ++i)
1724 {
1725 j += wp->w_lines[i].wl_size;
1726 if (j >= wp->w_upd_rows)
1727 {
1728 top_end = j;
1729 break;
1730 }
1731 }
1732 if (top_end == 0)
1733 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001734 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001735 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001736 // top area defined, the rest is UPD_VALID
1737 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001738 }
1739
1740 // Trick: we want to avoid clearing the screen twice. screenclear() will
1741 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1742 // non-zero and thus not FALSE) will indicate that screenclear() was not
1743 // called.
1744 if (screen_cleared)
1745 screen_cleared = MAYBE;
1746
1747 // If there are no changes on the screen that require a complete redraw,
1748 // handle three cases:
1749 // 1: we are off the top of the screen by a few lines: scroll down
1750 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1751 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1752 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001753 if ((type == UPD_VALID || type == UPD_SOME_VALID
1754 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001755#ifdef FEAT_DIFF
1756 && !wp->w_botfill && !wp->w_old_botfill
1757#endif
1758 )
1759 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001760 if (mod_top != 0
1761 && wp->w_topline == mod_top
1762 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001763 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001764 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001765 // w_topline is the first changed line and window is not scrolled,
1766 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001767 }
1768 else if (wp->w_lines[0].wl_valid
1769 && (wp->w_topline < wp->w_lines[0].wl_lnum
1770#ifdef FEAT_DIFF
1771 || (wp->w_topline == wp->w_lines[0].wl_lnum
1772 && wp->w_topfill > wp->w_old_topfill)
1773#endif
1774 ))
1775 {
1776 // New topline is above old topline: May scroll down.
1777#ifdef FEAT_FOLDING
1778 if (hasAnyFolding(wp))
1779 {
1780 linenr_T ln;
1781
1782 // count the number of lines we are off, counting a sequence
1783 // of folded lines as one
1784 j = 0;
1785 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1786 {
1787 ++j;
1788 if (j >= wp->w_height - 2)
1789 break;
1790 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1791 }
1792 }
1793 else
1794#endif
1795 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1796 if (j < wp->w_height - 2) // not too far off
1797 {
zeertzjqbfe377b2023-08-17 22:58:53 +02001798 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1,
Luuk van Baal32d701f2024-04-28 16:24:02 +02001799 wp->w_height);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001800#ifdef FEAT_DIFF
1801 // insert extra lines for previously invisible filler lines
1802 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1803 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1804 - wp->w_old_topfill;
1805#endif
1806 if (i < wp->w_height - 2) // less than a screen off
1807 {
1808 // Try to insert the correct number of lines.
1809 // If not the last window, delete the lines at the bottom.
1810 // win_ins_lines may fail when the terminal can't do it.
1811 if (i > 0)
1812 check_for_delay(FALSE);
1813 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1814 {
1815 if (wp->w_lines_valid != 0)
1816 {
1817 // Need to update rows that are new, stop at the
1818 // first one that scrolled down.
1819 top_end = i;
1820 scrolled_down = TRUE;
1821
1822 // Move the entries that were scrolled, disable
1823 // the entries for the lines to be redrawn.
1824 if ((wp->w_lines_valid += j) > wp->w_height)
1825 wp->w_lines_valid = wp->w_height;
Bram Moolenaara2a89732022-08-31 14:46:18 +01001826 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001827 wp->w_lines[idx] = wp->w_lines[idx - j];
1828 while (idx >= 0)
1829 wp->w_lines[idx--].wl_valid = FALSE;
1830 }
1831 }
1832 else
1833 mid_start = 0; // redraw all lines
1834 }
1835 else
1836 mid_start = 0; // redraw all lines
1837 }
1838 else
1839 mid_start = 0; // redraw all lines
1840 }
1841 else
1842 {
1843 // New topline is at or below old topline: May scroll up.
1844 // When topline didn't change, find first entry in w_lines[] that
1845 // needs updating.
1846
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001847 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1848 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001849 j = -1;
1850 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001851 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001852 {
1853 if (wp->w_lines[i].wl_valid
1854 && wp->w_lines[i].wl_lnum == wp->w_topline)
1855 {
1856 j = i;
1857 break;
1858 }
1859 row += wp->w_lines[i].wl_size;
1860 }
1861 if (j == -1)
1862 {
1863 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1864 // lines
1865 mid_start = 0;
1866 }
1867 else
1868 {
1869 // Try to delete the correct number of lines.
1870 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1871#ifdef FEAT_DIFF
1872 // If the topline didn't change, delete old filler lines,
1873 // otherwise delete filler lines of the new topline...
1874 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1875 row += wp->w_old_topfill;
1876 else
1877 row += diff_check_fill(wp, wp->w_topline);
1878 // ... but don't delete new filler lines.
1879 row -= wp->w_topfill;
1880#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001881 if (row > Rows) // just in case
1882 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001883 if (row > 0)
1884 {
1885 check_for_delay(FALSE);
1886 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1887 == OK)
1888 bot_start = wp->w_height - row;
1889 else
1890 mid_start = 0; // redraw all lines
1891 }
1892 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1893 {
1894 // Skip the lines (below the deleted lines) that are still
1895 // valid and don't need redrawing. Copy their info
1896 // upwards, to compensate for the deleted lines. Set
1897 // bot_start to the first row that needs redrawing.
1898 bot_start = 0;
1899 idx = 0;
1900 for (;;)
1901 {
1902 wp->w_lines[idx] = wp->w_lines[j];
1903 // stop at line that didn't fit, unless it is still
1904 // valid (no lines deleted)
1905 if (row > 0 && bot_start + row
1906 + (int)wp->w_lines[j].wl_size > wp->w_height)
1907 {
1908 wp->w_lines_valid = idx + 1;
1909 break;
1910 }
1911 bot_start += wp->w_lines[idx++].wl_size;
1912
1913 // stop at the last valid entry in w_lines[].wl_size
1914 if (++j >= wp->w_lines_valid)
1915 {
1916 wp->w_lines_valid = idx;
1917 break;
1918 }
1919 }
1920#ifdef FEAT_DIFF
1921 // Correct the first entry for filler lines at the top
1922 // when it won't get updated below.
1923 if (wp->w_p_diff && bot_start > 0)
zeertzjq47f85842024-10-01 19:35:47 +02001924 {
1925 int n = plines_win_nofill(wp, wp->w_topline, FALSE)
1926 + wp->w_topfill - adjust_plines_for_skipcol(wp);
1927 if (n > wp->w_height)
1928 n = wp->w_height;
1929 wp->w_lines[0].wl_size = n;
1930 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001931#endif
1932 }
1933 }
1934 }
1935
Bram Moolenaar13608d82022-08-29 15:06:50 +01001936 // When starting redraw in the first line, redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001937 if (mid_start == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001938 mid_end = wp->w_height;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001939
1940 // When win_del_lines() or win_ins_lines() caused the screen to be
1941 // cleared (only happens for the first window) or when screenclear()
1942 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001943 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001944 if (screen_cleared == TRUE)
1945 must_redraw = 0;
1946 }
1947 else
1948 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001949 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001950 mid_start = 0;
1951 mid_end = wp->w_height;
1952 }
1953
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001954 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001955 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001956 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001957 mid_start = 0;
1958 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001959 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001960 }
1961
1962 // check if we are updating or removing the inverted part
1963 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001964 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001965 {
1966 linenr_T from, to;
1967
1968 if (VIsual_active)
1969 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001970 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001971 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001972 {
1973 // If the type of Visual selection changed, redraw the whole
1974 // selection. Also when the ownership of the X selection is
1975 // gained or lost.
1976 if (curwin->w_cursor.lnum < VIsual.lnum)
1977 {
1978 from = curwin->w_cursor.lnum;
1979 to = VIsual.lnum;
1980 }
1981 else
1982 {
1983 from = VIsual.lnum;
1984 to = curwin->w_cursor.lnum;
1985 }
1986 // redraw more when the cursor moved as well
1987 if (wp->w_old_cursor_lnum < from)
1988 from = wp->w_old_cursor_lnum;
1989 if (wp->w_old_cursor_lnum > to)
1990 to = wp->w_old_cursor_lnum;
1991 if (wp->w_old_visual_lnum < from)
1992 from = wp->w_old_visual_lnum;
1993 if (wp->w_old_visual_lnum > to)
1994 to = wp->w_old_visual_lnum;
1995 }
1996 else
1997 {
1998 // Find the line numbers that need to be updated: The lines
1999 // between the old cursor position and the current cursor
2000 // position. Also check if the Visual position changed.
2001 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
2002 {
2003 from = curwin->w_cursor.lnum;
2004 to = wp->w_old_cursor_lnum;
2005 }
2006 else
2007 {
2008 from = wp->w_old_cursor_lnum;
2009 to = curwin->w_cursor.lnum;
2010 if (from == 0) // Visual mode just started
2011 from = to;
2012 }
2013
2014 if (VIsual.lnum != wp->w_old_visual_lnum
2015 || VIsual.col != wp->w_old_visual_col)
2016 {
2017 if (wp->w_old_visual_lnum < from
2018 && wp->w_old_visual_lnum != 0)
2019 from = wp->w_old_visual_lnum;
2020 if (wp->w_old_visual_lnum > to)
2021 to = wp->w_old_visual_lnum;
2022 if (VIsual.lnum < from)
2023 from = VIsual.lnum;
2024 if (VIsual.lnum > to)
2025 to = VIsual.lnum;
2026 }
2027 }
2028
2029 // If in block mode and changed column or curwin->w_curswant:
2030 // update all lines.
2031 // First compute the actual start and end column.
2032 if (VIsual_mode == Ctrl_V)
2033 {
2034 colnr_T fromc, toc;
2035#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002036 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002037
2038 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002039 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002040#endif
2041 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002042 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002043#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002044 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002045#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002046 // Highlight to the end of the line, unless 'virtualedit' has
2047 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002048 if (curwin->w_curswant == MAXCOL)
2049 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002050 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002051 {
2052 pos_T pos;
2053 int cursor_above =
2054 curwin->w_cursor.lnum < VIsual.lnum;
2055
2056 // Need to find the longest line.
2057 toc = 0;
2058 pos.coladd = 0;
2059 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2060 ? pos.lnum <= VIsual.lnum
2061 : pos.lnum >= VIsual.lnum;
2062 pos.lnum += cursor_above ? 1 : -1)
2063 {
2064 colnr_T t;
2065
John Marriotta21240b2025-01-08 20:10:59 +01002066 pos.col = (int)ml_get_buf_len(wp->w_buffer, pos.lnum);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002067 getvvcol(wp, &pos, NULL, NULL, &t);
2068 if (toc < t)
2069 toc = t;
2070 }
2071 ++toc;
2072 }
2073 else
2074 toc = MAXCOL;
2075 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002076
2077 if (fromc != wp->w_old_cursor_fcol
2078 || toc != wp->w_old_cursor_lcol)
2079 {
2080 if (from > VIsual.lnum)
2081 from = VIsual.lnum;
2082 if (to < VIsual.lnum)
2083 to = VIsual.lnum;
2084 }
2085 wp->w_old_cursor_fcol = fromc;
2086 wp->w_old_cursor_lcol = toc;
2087 }
2088 }
2089 else
2090 {
2091 // Use the line numbers of the old Visual area.
2092 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2093 {
2094 from = wp->w_old_cursor_lnum;
2095 to = wp->w_old_visual_lnum;
2096 }
2097 else
2098 {
2099 from = wp->w_old_visual_lnum;
2100 to = wp->w_old_cursor_lnum;
2101 }
2102 }
2103
2104 // There is no need to update lines above the top of the window.
2105 if (from < wp->w_topline)
2106 from = wp->w_topline;
2107
2108 // If we know the value of w_botline, use it to restrict the update to
2109 // the lines that are visible in the window.
2110 if (wp->w_valid & VALID_BOTLINE)
2111 {
2112 if (from >= wp->w_botline)
2113 from = wp->w_botline - 1;
2114 if (to >= wp->w_botline)
2115 to = wp->w_botline - 1;
2116 }
2117
2118 // Find the minimal part to be updated.
2119 // Watch out for scrolling that made entries in w_lines[] invalid.
2120 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2121 // top_end; need to redraw from top_end to the "to" line.
2122 // A middle mouse click with a Visual selection may change the text
2123 // above the Visual area and reset wl_valid, do count these for
2124 // mid_end (in srow).
2125 if (mid_start > 0)
2126 {
2127 lnum = wp->w_topline;
2128 idx = 0;
2129 srow = 0;
2130 if (scrolled_down)
2131 mid_start = top_end;
2132 else
2133 mid_start = 0;
2134 while (lnum < from && idx < wp->w_lines_valid) // find start
2135 {
2136 if (wp->w_lines[idx].wl_valid)
2137 mid_start += wp->w_lines[idx].wl_size;
2138 else if (!scrolled_down)
2139 srow += wp->w_lines[idx].wl_size;
2140 ++idx;
2141# ifdef FEAT_FOLDING
2142 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2143 lnum = wp->w_lines[idx].wl_lnum;
2144 else
2145# endif
2146 ++lnum;
2147 }
2148 srow += mid_start;
2149 mid_end = wp->w_height;
2150 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2151 {
2152 if (wp->w_lines[idx].wl_valid
2153 && wp->w_lines[idx].wl_lnum >= to + 1)
2154 {
2155 // Only update until first row of this line
2156 mid_end = srow;
2157 break;
2158 }
2159 srow += wp->w_lines[idx].wl_size;
2160 }
2161 }
2162 }
2163
2164 if (VIsual_active && buf == curwin->w_buffer)
2165 {
2166 wp->w_old_visual_mode = VIsual_mode;
2167 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2168 wp->w_old_visual_lnum = VIsual.lnum;
2169 wp->w_old_visual_col = VIsual.col;
2170 wp->w_old_curswant = curwin->w_curswant;
2171 }
2172 else
2173 {
2174 wp->w_old_visual_mode = 0;
2175 wp->w_old_cursor_lnum = 0;
2176 wp->w_old_visual_lnum = 0;
2177 wp->w_old_visual_col = 0;
2178 }
2179
2180#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2181 // reset got_int, otherwise regexp won't work
2182 save_got_int = got_int;
2183 got_int = 0;
2184#endif
2185#ifdef SYN_TIME_LIMIT
2186 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002187 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002188 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002189#endif
2190#ifdef FEAT_FOLDING
2191 win_foldinfo.fi_level = 0;
2192#endif
2193
2194#ifdef FEAT_MENU
2195 // Draw the window toolbar, if there is one.
2196 // TODO: only when needed.
2197 if (winbar_height(wp) > 0)
2198 redraw_win_toolbar(wp);
2199#endif
2200
Luuk van Baal30805a12023-05-27 22:22:10 +01002201 lnum = wp->w_topline; // first line shown in window
2202
2203 spellvars_T spv;
2204#ifdef FEAT_SPELL
2205 // Initialize spell related variables for the first drawn line.
2206 CLEAR_FIELD(spv);
Luuk van Baale84c7732023-05-31 18:57:36 +01002207 if (spell_check_window(wp))
Luuk van Baal30805a12023-05-27 22:22:10 +01002208 {
Luuk van Baale84c7732023-05-31 18:57:36 +01002209 spv.spv_has_spell = TRUE;
Luuk van Baal30805a12023-05-27 22:22:10 +01002210 spv.spv_unchanged = mod_top == 0;
Luuk van Baal30805a12023-05-27 22:22:10 +01002211 }
2212#endif
2213
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002214 // Update all the window rows.
2215 idx = 0; // first entry in w_lines[].wl_size
2216 row = 0;
2217 srow = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002218 for (;;)
2219 {
2220 // stop updating when reached the end of the window (check for _past_
2221 // the end of the window is at the end of the loop)
2222 if (row == wp->w_height)
2223 {
2224 didline = TRUE;
2225 break;
2226 }
2227
2228 // stop updating when hit the end of the file
2229 if (lnum > buf->b_ml.ml_line_count)
2230 {
2231 eof = TRUE;
2232 break;
2233 }
2234
2235 // Remember the starting row of the line that is going to be dealt
2236 // with. It is used further down when the line doesn't fit.
2237 srow = row;
2238
2239 // Update a line when it is in an area that needs updating, when it
2240 // has changes or w_lines[idx] is invalid.
2241 // "bot_start" may be halfway a wrapped line after using
2242 // win_del_lines(), check if the current line includes it.
2243 // When syntax folding is being used, the saved syntax states will
2244 // already have been updated, we can't see where the syntax state is
2245 // the same again, just update until the end of the window.
2246 if (row < top_end
2247 || (row >= mid_start && row < mid_end)
2248#ifdef FEAT_SEARCH_EXTRA
2249 || top_to_mod
2250#endif
2251 || idx >= wp->w_lines_valid
2252 || (row + wp->w_lines[idx].wl_size > bot_start)
2253 || (mod_top != 0
2254 && (lnum == mod_top
2255 || (lnum >= mod_top
2256 && (lnum < mod_bot
2257#ifdef FEAT_SYN_HL
2258 || did_update == DID_FOLD
2259 || (did_update == DID_LINE
2260 && syntax_present(wp)
2261 && (
2262# ifdef FEAT_FOLDING
2263 (foldmethodIsSyntax(wp)
2264 && hasAnyFolding(wp)) ||
2265# endif
2266 syntax_check_changed(lnum)))
2267#endif
2268#ifdef FEAT_SEARCH_EXTRA
2269 // match in fixed position might need redraw
2270 // if lines were inserted or deleted
2271 || (wp->w_match_head != NULL
zeertzjq9f25a3a2024-11-26 15:08:02 +01002272 && buf->b_mod_set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002273 && buf->b_mod_xlines != 0)
2274#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002275 ))))
2276#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002277 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2278 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002279#endif
2280 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002281 {
2282#ifdef FEAT_SEARCH_EXTRA
2283 if (lnum == mod_top)
2284 top_to_mod = FALSE;
2285#endif
2286
2287 // When at start of changed lines: May scroll following lines
2288 // up or down to minimize redrawing.
2289 // Don't do this when the change continues until the end.
2290 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002291 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002292 if (lnum == mod_top
2293 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002294 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2295 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002296 {
2297 int old_rows = 0;
2298 int new_rows = 0;
2299 int xtra_rows;
2300 linenr_T l;
2301
2302 // Count the old number of window rows, using w_lines[], which
2303 // should still contain the sizes for the lines as they are
2304 // currently displayed.
2305 for (i = idx; i < wp->w_lines_valid; ++i)
2306 {
2307 // Only valid lines have a meaningful wl_lnum. Invalid
2308 // lines are part of the changed area.
2309 if (wp->w_lines[i].wl_valid
2310 && wp->w_lines[i].wl_lnum == mod_bot)
2311 break;
2312 old_rows += wp->w_lines[i].wl_size;
2313#ifdef FEAT_FOLDING
2314 if (wp->w_lines[i].wl_valid
2315 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2316 {
2317 // Must have found the last valid entry above mod_bot.
2318 // Add following invalid entries.
2319 ++i;
2320 while (i < wp->w_lines_valid
2321 && !wp->w_lines[i].wl_valid)
2322 old_rows += wp->w_lines[i++].wl_size;
2323 break;
2324 }
2325#endif
2326 }
2327
2328 if (i >= wp->w_lines_valid)
2329 {
2330 // We can't find a valid line below the changed lines,
2331 // need to redraw until the end of the window.
2332 // Inserting/deleting lines has no use.
2333 bot_start = 0;
2334 }
2335 else
2336 {
2337 // Able to count old number of rows: Count new window
2338 // rows, and may insert/delete lines
2339 j = idx;
2340 for (l = lnum; l < mod_bot; ++l)
2341 {
2342#ifdef FEAT_FOLDING
2343 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2344 ++new_rows;
2345 else
2346#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002347 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002348#ifdef FEAT_DIFF
2349 if (l == wp->w_topline)
Luuk van Baalc8502f92023-05-06 12:40:15 +01002350 {
2351 int n = plines_win_nofill(wp, l, FALSE)
2352 + wp->w_topfill;
zeertzjq6235a102023-08-19 14:12:42 +02002353 n -= adjust_plines_for_skipcol(wp);
Luuk van Baalc8502f92023-05-06 12:40:15 +01002354 if (n > wp->w_height)
2355 n = wp->w_height;
2356 new_rows += n;
2357 }
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002358 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002359#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002360 new_rows += plines_win(wp, l, TRUE);
2361 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002362 ++j;
2363 if (new_rows > wp->w_height - row - 2)
2364 {
2365 // it's getting too much, must redraw the rest
2366 new_rows = 9999;
2367 break;
2368 }
2369 }
2370 xtra_rows = new_rows - old_rows;
2371 if (xtra_rows < 0)
2372 {
2373 // May scroll text up. If there is not enough
2374 // remaining text or scrolling fails, must redraw the
2375 // rest. If scrolling works, must redraw the text
2376 // below the scrolled text.
2377 if (row - xtra_rows >= wp->w_height - 2)
2378 mod_bot = MAXLNUM;
2379 else
2380 {
2381 check_for_delay(FALSE);
2382 if (win_del_lines(wp, row,
2383 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2384 mod_bot = MAXLNUM;
2385 else
2386 bot_start = wp->w_height + xtra_rows;
2387 }
2388 }
2389 else if (xtra_rows > 0)
2390 {
2391 // May scroll text down. If there is not enough
2392 // remaining text of scrolling fails, must redraw the
2393 // rest.
2394 if (row + xtra_rows >= wp->w_height - 2)
2395 mod_bot = MAXLNUM;
2396 else
2397 {
2398 check_for_delay(FALSE);
2399 if (win_ins_lines(wp, row + old_rows,
2400 xtra_rows, FALSE, FALSE) == FAIL)
2401 mod_bot = MAXLNUM;
2402 else if (top_end > row + old_rows)
2403 // Scrolled the part at the top that requires
2404 // updating down.
2405 top_end += xtra_rows;
2406 }
2407 }
2408
2409 // When not updating the rest, may need to move w_lines[]
2410 // entries.
2411 if (mod_bot != MAXLNUM && i != j)
2412 {
2413 if (j < i)
2414 {
2415 int x = row + new_rows;
2416
2417 // move entries in w_lines[] upwards
2418 for (;;)
2419 {
2420 // stop at last valid entry in w_lines[]
2421 if (i >= wp->w_lines_valid)
2422 {
2423 wp->w_lines_valid = j;
2424 break;
2425 }
2426 wp->w_lines[j] = wp->w_lines[i];
2427 // stop at a line that won't fit
2428 if (x + (int)wp->w_lines[j].wl_size
2429 > wp->w_height)
2430 {
2431 wp->w_lines_valid = j + 1;
2432 break;
2433 }
2434 x += wp->w_lines[j++].wl_size;
2435 ++i;
2436 }
2437 if (bot_start > x)
2438 bot_start = x;
2439 }
2440 else // j > i
2441 {
2442 // move entries in w_lines[] downwards
2443 j -= i;
2444 wp->w_lines_valid += j;
2445 if (wp->w_lines_valid > wp->w_height)
2446 wp->w_lines_valid = wp->w_height;
2447 for (i = wp->w_lines_valid; i - j >= idx; --i)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002448 wp->w_lines[i] = wp->w_lines[i - j];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002449
2450 // The w_lines[] entries for inserted lines are
2451 // now invalid, but wl_size may be used above.
2452 // Reset to zero.
2453 while (i >= idx)
2454 {
2455 wp->w_lines[i].wl_size = 0;
2456 wp->w_lines[i--].wl_valid = FALSE;
2457 }
2458 }
2459 }
2460 }
2461 }
2462
2463#ifdef FEAT_FOLDING
2464 // When lines are folded, display one line for all of them.
2465 // Otherwise, display normally (can be several display lines when
2466 // 'wrap' is on).
2467 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2468 if (fold_count != 0)
2469 {
2470 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2471 ++row;
2472 --fold_count;
2473 wp->w_lines[idx].wl_folded = TRUE;
Luuk van Baale84c7732023-05-31 18:57:36 +01002474 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002475# ifdef FEAT_SYN_HL
2476 did_update = DID_FOLD;
2477# endif
Luuk van Baal30805a12023-05-27 22:22:10 +01002478# ifdef FEAT_SPELL
Luuk van Baale84c7732023-05-31 18:57:36 +01002479 spv.spv_capcol_lnum = 0;
Luuk van Baal30805a12023-05-27 22:22:10 +01002480# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002481 }
2482 else
2483#endif
2484 if (idx < wp->w_lines_valid
2485 && wp->w_lines[idx].wl_valid
2486 && wp->w_lines[idx].wl_lnum == lnum
2487 && lnum > wp->w_topline
2488 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2489 && !WIN_IS_POPUP(wp)
2490 && srow + wp->w_lines[idx].wl_size > wp->w_height
2491#ifdef FEAT_DIFF
2492 && diff_check_fill(wp, lnum) == 0
2493#endif
2494 )
2495 {
2496 // This line is not going to fit. Don't draw anything here,
2497 // will draw "@ " lines below.
2498 row = wp->w_height + 1;
2499 }
2500 else
2501 {
2502#ifdef FEAT_SEARCH_EXTRA
2503 prepare_search_hl(wp, &screen_search_hl, lnum);
2504#endif
2505#ifdef FEAT_SYN_HL
2506 // Let the syntax stuff know we skipped a few lines.
2507 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2508 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002509 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002510#endif
2511
2512 // Display one line.
zeertzjqebfd8562024-02-06 10:59:03 +01002513 row = win_line(wp, lnum, srow, wp->w_height, 0, &spv);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002514
2515#ifdef FEAT_FOLDING
2516 wp->w_lines[idx].wl_folded = FALSE;
2517 wp->w_lines[idx].wl_lastlnum = lnum;
2518#endif
2519#ifdef FEAT_SYN_HL
2520 did_update = DID_LINE;
2521 syntax_last_parsed = lnum;
2522#endif
2523 }
2524
2525 wp->w_lines[idx].wl_lnum = lnum;
2526 wp->w_lines[idx].wl_valid = TRUE;
2527
2528 // Past end of the window or end of the screen. Note that after
2529 // resizing wp->w_height may be end up too big. That's a problem
2530 // elsewhere, but prevent a crash here.
Bram Moolenaara2a89732022-08-31 14:46:18 +01002531 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002532 {
2533 // we may need the size of that too long line later on
2534 if (dollar_vcol == -1)
2535 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2536 ++idx;
2537 break;
2538 }
2539 if (dollar_vcol == -1)
2540 wp->w_lines[idx].wl_size = row - srow;
2541 ++idx;
2542#ifdef FEAT_FOLDING
2543 lnum += fold_count + 1;
2544#else
2545 ++lnum;
2546#endif
2547 }
2548 else
2549 {
zeertzjqae07ebc2024-02-08 11:37:40 +01002550 // If:
2551 // - 'number' is set and below inserted/deleted lines, or
2552 // - 'relativenumber' is set and cursor moved vertically,
2553 // the text doesn't need to be redrawn, but the number column does.
zeertzjq9f25a3a2024-11-26 15:08:02 +01002554 if ((wp->w_p_nu && mod_top != 0 && lnum >= mod_bot
2555 && buf->b_mod_set && buf->b_mod_xlines != 0)
zeertzjqae07ebc2024-02-08 11:37:40 +01002556 || (wp->w_p_rnu
2557 && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002558 {
2559#ifdef FEAT_FOLDING
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002560 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2561 if (fold_count != 0)
2562 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2563 else
2564#endif
zeertzjqebfd8562024-02-06 10:59:03 +01002565 (void)win_line(wp, lnum, srow, wp->w_height,
2566 wp->w_lines[idx].wl_size, &spv);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002567 }
2568
2569 // This line does not need to be drawn, advance to the next one.
2570 row += wp->w_lines[idx++].wl_size;
2571 if (row > wp->w_height) // past end of screen
2572 break;
2573#ifdef FEAT_FOLDING
2574 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2575#else
2576 ++lnum;
2577#endif
2578#ifdef FEAT_SYN_HL
2579 did_update = DID_NONE;
2580#endif
Luuk van Baale84c7732023-05-31 18:57:36 +01002581#ifdef FEAT_SPELL
2582 spv.spv_capcol_lnum = 0;
2583#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002584 }
2585
2586 if (lnum > buf->b_ml.ml_line_count)
2587 {
2588 eof = TRUE;
2589 break;
2590 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002591
2592 // Safety check: if any of the wl_size values is wrong we might go over
2593 // the end of w_lines[].
Bram Moolenaara2a89732022-08-31 14:46:18 +01002594 if (idx >= Rows)
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002595 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002596 }
2597
2598 // End of loop over all window lines.
2599
zeertzjqc20e46a2022-03-23 14:55:23 +00002600#ifdef FEAT_SYN_HL
2601 // Now that the window has been redrawn with the old and new cursor line,
2602 // update w_last_cursorline.
2603 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2604#endif
Lewis Russell16246392022-03-29 11:38:17 +01002605 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002606
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002607#ifdef FEAT_VTP
2608 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002609 // See the version that was fixed.
2610 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002611 {
K.Takata54119102022-02-03 13:33:03 +00002612 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002613
K.Takata54119102022-02-03 13:33:03 +00002614 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002615 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002616 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2617 LineOffset[k] + screen_Columns) > 1)
2618 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002619 else
K.Takata54119102022-02-03 13:33:03 +00002620 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002621 else
K.Takata54119102022-02-03 13:33:03 +00002622 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002623 }
2624#endif
2625
2626 if (idx > wp->w_lines_valid)
2627 wp->w_lines_valid = idx;
2628
2629#ifdef FEAT_SYN_HL
2630 // Let the syntax stuff know we stop parsing here.
2631 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002632 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002633#endif
2634
2635 // If we didn't hit the end of the file, and we didn't finish the last
2636 // line we were working on, then the line didn't fit.
2637 wp->w_empty_rows = 0;
2638#ifdef FEAT_DIFF
2639 wp->w_filler_rows = 0;
2640#endif
2641 if (!eof && !didline)
2642 {
2643 if (lnum == wp->w_topline)
2644 {
2645 // Single line that does not fit!
2646 // Don't overwrite it, it can be edited.
2647 wp->w_botline = lnum + 1;
2648 }
2649#ifdef FEAT_DIFF
2650 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2651 {
2652 // Window ends in filler lines.
2653 wp->w_botline = lnum;
2654 wp->w_filler_rows = wp->w_height - srow;
2655 }
2656#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002657#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002658 else if (WIN_IS_POPUP(wp))
2659 {
2660 // popup line that doesn't fit is left as-is
2661 wp->w_botline = lnum;
2662 }
2663#endif
2664 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2665 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002666 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2667 int symbol = wp->w_fill_chars.lastline;
zeertzjq18b35002022-10-04 20:35:37 +01002668 int charlen;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002669 char_u fillbuf[12]; // 2 characters of 6 bytes
2670
zeertzjq18b35002022-10-04 20:35:37 +01002671 charlen = mb_char2bytes(symbol, &fillbuf[0]);
2672 mb_char2bytes(symbol, &fillbuf[charlen]);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002673
2674 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002675 screen_puts_len(fillbuf,
zeertzjq18b35002022-10-04 20:35:37 +01002676 (wp->w_width > 2 ? 2 : wp->w_width) * charlen,
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002677 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002678 screen_fill(scr_row, scr_row + 1,
2679 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002680 symbol, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002681 set_empty_rows(wp, srow);
2682 wp->w_botline = lnum;
2683 }
2684 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2685 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002686 int start_col = (int)W_ENDCOL(wp) - 3;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002687 int symbol = wp->w_fill_chars.lastline;
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002688
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002689 // Last line isn't finished: Display "@@@" at the end.
2690 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2691 W_WINROW(wp) + wp->w_height,
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02002692 (start_col < wp->w_wincol ? wp->w_wincol : start_col) + TPL_LCOL(wp),
2693 (int)W_ENDCOL(wp) + TPL_LCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002694 symbol, symbol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002695 set_empty_rows(wp, srow);
2696 wp->w_botline = lnum;
2697 }
2698 else
2699 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002700 win_draw_end(wp, wp->w_fill_chars.lastline, ' ', TRUE,
2701 srow, wp->w_height, HLF_AT);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002702 wp->w_botline = lnum;
2703 }
2704 }
2705 else
2706 {
2707 draw_vsep_win(wp, row);
2708 if (eof) // we hit the end of the file
2709 {
2710 wp->w_botline = buf->b_ml.ml_line_count + 1;
2711#ifdef FEAT_DIFF
2712 j = diff_check_fill(wp, wp->w_botline);
2713 if (j > 0 && !wp->w_botfill)
2714 {
2715 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002716 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002717 i = '-';
2718 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002719 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002720 if (row + j > wp->w_height)
2721 j = wp->w_height - row;
2722 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2723 row += j;
2724 }
2725#endif
2726 }
2727 else if (dollar_vcol == -1)
2728 wp->w_botline = lnum;
2729
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002730 // Make sure the rest of the screen is blank.
2731 // write the "eob" character from 'fillchars' to rows that aren't part
2732 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002733 if (WIN_IS_POPUP(wp))
2734 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2735 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002736 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2737 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002738 }
2739
2740#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002741 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002742 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002743#endif
2744
2745 // Reset the type of redrawing required, the window has been updated.
2746 wp->w_redr_type = 0;
2747#ifdef FEAT_DIFF
2748 wp->w_old_topfill = wp->w_topfill;
2749 wp->w_old_botfill = wp->w_botfill;
2750#endif
2751
2752 if (dollar_vcol == -1)
2753 {
2754 // There is a trick with w_botline. If we invalidate it on each
2755 // change that might modify it, this will cause a lot of expensive
2756 // calls to plines() in update_topline() each time. Therefore the
2757 // value of w_botline is often approximated, and this value is used to
2758 // compute the value of w_topline. If the value of w_botline was
2759 // wrong, check that the value of w_topline is correct (cursor is on
2760 // the visible part of the text). If it's not, we need to redraw
2761 // again. Mostly this just means scrolling up a few lines, so it
2762 // doesn't look too bad. Only do this for the current window (where
2763 // changes are relevant).
2764 wp->w_valid |= VALID_BOTLINE;
2765 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2766 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002767 win_T *wwp;
2768#if defined(FEAT_CONCEAL)
2769 linenr_T old_topline = wp->w_topline;
2770 int new_wcol = wp->w_wcol;
2771#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002772 recursive = TRUE;
2773 curwin->w_valid &= ~VALID_TOPLINE;
2774 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002775
2776#if defined(FEAT_CONCEAL)
2777 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2778 != (VALID_WCOL|VALID_WROW))
2779 {
2780 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002781 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002782 // update_topline() the cursor's row or column got invalidated.
2783 // If they are left invalid, setcursor() will recompute them
2784 // but there won't be any further win_line() call to re-fix the
2785 // column and the cursor will end up misplaced. So we call
2786 // cursor validation now and reapply the fix again (or call
2787 // win_line() to do it for us).
2788 validate_cursor();
2789 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2790 && old_topline == wp->w_topline)
2791 wp->w_wcol = new_wcol;
2792 else
2793 redrawWinline(wp, wp->w_cursor.lnum);
2794 }
2795#endif
Luuk van Baal3d5065f2024-09-01 10:33:56 +02002796 // New redraw either due to updated topline, wcol fix or reset skipcol.
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002797 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002798 {
2799 // Don't update for changes in buffer again.
2800 i = curbuf->b_mod_set;
2801 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002802 j = curbuf->b_mod_xlines;
2803 curbuf->b_mod_xlines = 0;
Luuk van Baal3d5065f2024-09-01 10:33:56 +02002804 curs_columns(TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002805 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002806 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002807 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002808 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002809 // Other windows might have w_redr_type raised in update_topline().
2810 must_redraw = 0;
2811 FOR_ALL_WINDOWS(wwp)
2812 if (wwp->w_redr_type > must_redraw)
2813 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002814 recursive = FALSE;
2815 }
2816 }
2817
2818#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2819 // restore got_int, unless CTRL-C was hit while redrawing
2820 if (!got_int)
2821 got_int = save_got_int;
2822#endif
2823}
2824
2825#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2826/*
2827 * Prepare for updating one or more windows.
2828 * Caller must check for "updating_screen" already set to avoid recursiveness.
2829 */
2830 static void
2831update_prepare(void)
2832{
2833 cursor_off();
2834 updating_screen = TRUE;
2835#ifdef FEAT_GUI
2836 // Remove the cursor before starting to do anything, because scrolling may
2837 // make it difficult to redraw the text under it.
2838 if (gui.in_use)
2839 gui_undraw_cursor();
2840#endif
2841#ifdef FEAT_SEARCH_EXTRA
2842 start_search_hl();
2843#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002844#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002845 // Update popup_mask if needed.
2846 may_update_popup_mask(must_redraw);
2847#endif
2848}
2849
2850/*
2851 * Finish updating one or more windows.
2852 */
2853 static void
2854update_finish(void)
2855{
2856 if (redraw_cmdline || redraw_mode)
2857 showmode();
2858
2859# ifdef FEAT_SEARCH_EXTRA
2860 end_search_hl();
2861# endif
2862
2863 after_updating_screen(TRUE);
2864
2865# ifdef FEAT_GUI
2866 // Redraw the cursor and update the scrollbars when all screen updating is
2867 // done.
2868 if (gui.in_use)
2869 {
2870 out_flush_cursor(FALSE, FALSE);
2871 gui_update_scrollbars(FALSE);
2872 }
2873# endif
2874}
2875#endif
2876
2877#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2878 void
2879update_debug_sign(buf_T *buf, linenr_T lnum)
2880{
2881 win_T *wp;
2882 int doit = FALSE;
2883
2884# ifdef FEAT_FOLDING
2885 win_foldinfo.fi_level = 0;
2886# endif
2887
2888 // update/delete a specific sign
2889 redraw_buf_line_later(buf, lnum);
2890
2891 // check if it resulted in the need to redraw a window
2892 FOR_ALL_WINDOWS(wp)
2893 if (wp->w_redr_type != 0)
2894 doit = TRUE;
2895
2896 // Return when there is nothing to do, screen updating is already
2897 // happening (recursive call), messages on the screen or still starting up.
2898 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002899 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002900 || msg_scrolled
2901#ifdef FEAT_GUI
2902 || gui.starting
2903#endif
2904 || starting)
2905 return;
2906
2907 // update all windows that need updating
2908 update_prepare();
2909
2910 FOR_ALL_WINDOWS(wp)
2911 {
2912 if (wp->w_redr_type != 0)
2913 win_update(wp);
2914 if (wp->w_redr_status)
2915 win_redr_status(wp, FALSE);
2916 }
2917
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02002918#if defined(FEAT_TABPANEL)
2919 if (redraw_tabpanel)
2920 draw_tabpanel();
2921#endif
2922
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002923 update_finish();
2924}
2925#endif
2926
2927#if defined(FEAT_GUI) || defined(PROTO)
2928/*
2929 * Update a single window, its status line and maybe the command line msg.
2930 * Used for the GUI scrollbar.
2931 */
2932 void
2933updateWindow(win_T *wp)
2934{
2935 // return if already busy updating
2936 if (updating_screen)
2937 return;
2938
2939 update_prepare();
2940
2941#ifdef FEAT_CLIPBOARD
2942 // When Visual area changed, may have to update selection.
2943 if (clip_star.available && clip_isautosel_star())
2944 clip_update_selection(&clip_star);
2945 if (clip_plus.available && clip_isautosel_plus())
2946 clip_update_selection(&clip_plus);
2947#endif
2948
2949 win_update(wp);
2950
2951 // When the screen was cleared redraw the tab pages line.
2952 if (redraw_tabline)
2953 draw_tabline();
2954
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02002955#if defined(FEAT_TABPANEL)
2956 if (redraw_tabpanel)
2957 draw_tabpanel();
2958#endif
2959
Martin Tournoijba43e762022-10-13 22:12:15 +01002960 if (wp->w_redr_status || p_ru
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002961# ifdef FEAT_STL_OPT
2962 || *p_stl != NUL || *wp->w_p_stl != NUL
2963# endif
2964 )
2965 win_redr_status(wp, FALSE);
2966
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002967#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002968 // Display popup windows on top of everything.
2969 update_popups(win_update);
2970#endif
2971
2972 update_finish();
2973}
2974#endif
2975
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002976/*
2977 * Redraw as soon as possible. When the command line is not scrolled redraw
2978 * right away and restore what was on the command line.
2979 * Return a code indicating what happened.
2980 */
2981 int
2982redraw_asap(int type)
2983{
2984 int rows;
2985 int cols = screen_Columns;
2986 int r;
2987 int ret = 0;
2988 schar_T *screenline; // copy from ScreenLines[]
2989 sattr_T *screenattr; // copy from ScreenAttrs[]
2990 int i;
2991 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2992 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2993 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2994
2995 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002996 if (msg_scrolled
2997 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002998 || exiting)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002999 return ret;
3000
3001 // Allocate space to save the text displayed in the command line area.
3002 rows = screen_Rows - cmdline_row;
3003 screenline = LALLOC_MULT(schar_T, rows * cols);
3004 screenattr = LALLOC_MULT(sattr_T, rows * cols);
3005 if (screenline == NULL || screenattr == NULL)
3006 ret = 2;
3007 if (enc_utf8)
3008 {
3009 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
3010 if (screenlineUC == NULL)
3011 ret = 2;
3012 for (i = 0; i < p_mco; ++i)
3013 {
3014 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
3015 if (screenlineC[i] == NULL)
3016 ret = 2;
3017 }
3018 }
3019 if (enc_dbcs == DBCS_JPNU)
3020 {
3021 screenline2 = LALLOC_MULT(schar_T, rows * cols);
3022 if (screenline2 == NULL)
3023 ret = 2;
3024 }
3025
3026 if (ret != 2)
3027 {
3028 // Save the text displayed in the command line area.
3029 for (r = 0; r < rows; ++r)
3030 {
3031 mch_memmove(screenline + r * cols,
3032 ScreenLines + LineOffset[cmdline_row + r],
3033 (size_t)cols * sizeof(schar_T));
3034 mch_memmove(screenattr + r * cols,
3035 ScreenAttrs + LineOffset[cmdline_row + r],
3036 (size_t)cols * sizeof(sattr_T));
3037 if (enc_utf8)
3038 {
3039 mch_memmove(screenlineUC + r * cols,
3040 ScreenLinesUC + LineOffset[cmdline_row + r],
3041 (size_t)cols * sizeof(u8char_T));
3042 for (i = 0; i < p_mco; ++i)
3043 mch_memmove(screenlineC[i] + r * cols,
3044 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3045 (size_t)cols * sizeof(u8char_T));
3046 }
3047 if (enc_dbcs == DBCS_JPNU)
3048 mch_memmove(screenline2 + r * cols,
3049 ScreenLines2 + LineOffset[cmdline_row + r],
3050 (size_t)cols * sizeof(schar_T));
3051 }
3052
3053 update_screen(0);
3054 ret = 3;
3055
3056 if (must_redraw == 0)
3057 {
3058 int off = (int)(current_ScreenLine - ScreenLines);
3059
3060 // Restore the text displayed in the command line area.
3061 for (r = 0; r < rows; ++r)
3062 {
3063 mch_memmove(current_ScreenLine,
3064 screenline + r * cols,
3065 (size_t)cols * sizeof(schar_T));
3066 mch_memmove(ScreenAttrs + off,
3067 screenattr + r * cols,
3068 (size_t)cols * sizeof(sattr_T));
3069 if (enc_utf8)
3070 {
3071 mch_memmove(ScreenLinesUC + off,
3072 screenlineUC + r * cols,
3073 (size_t)cols * sizeof(u8char_T));
3074 for (i = 0; i < p_mco; ++i)
3075 mch_memmove(ScreenLinesC[i] + off,
3076 screenlineC[i] + r * cols,
3077 (size_t)cols * sizeof(u8char_T));
3078 }
3079 if (enc_dbcs == DBCS_JPNU)
3080 mch_memmove(ScreenLines2 + off,
3081 screenline2 + r * cols,
3082 (size_t)cols * sizeof(schar_T));
zeertzjqd0c1b772024-03-16 15:03:33 +01003083 screen_line(curwin, cmdline_row + r, 0, cols, cols, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003084 }
3085 ret = 4;
3086 }
3087 }
3088
3089 vim_free(screenline);
3090 vim_free(screenattr);
3091 if (enc_utf8)
3092 {
3093 vim_free(screenlineUC);
3094 for (i = 0; i < p_mco; ++i)
3095 vim_free(screenlineC[i]);
3096 }
3097 if (enc_dbcs == DBCS_JPNU)
3098 vim_free(screenline2);
3099
3100 // Show the intro message when appropriate.
3101 maybe_intro_message();
3102
3103 setcursor();
3104
3105 return ret;
3106}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003107
3108/*
3109 * Invoked after an asynchronous callback is called.
3110 * If an echo command was used the cursor needs to be put back where
3111 * it belongs. If highlighting was changed a redraw is needed.
3112 * If "call_update_screen" is FALSE don't call update_screen() when at the
3113 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003114 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003115 */
3116 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003117redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003118{
3119 ++redrawing_for_callback;
3120
Bram Moolenaar24959102022-05-07 20:01:16 +01003121 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3122 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3123 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003124 {
3125 if (do_message)
3126 repeat_message();
3127 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003128 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003129 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003130 if (pum_visible())
3131 cmdline_pum_display();
Bram Moolenaar54162322022-08-26 16:58:51 +01003132
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003133 // Don't redraw when in prompt_for_number().
3134 if (cmdline_row > 0)
3135 {
3136 // Redrawing only works when the screen didn't scroll. Don't clear
3137 // wildmenu entries.
3138 if (msg_scrolled == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003139 && wild_menu_showing == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003140 && call_update_screen)
3141 update_screen(0);
3142
3143 // Redraw in the same position, so that the user can continue
3144 // editing the command.
3145 redrawcmdline_ex(FALSE);
3146 }
3147 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003148 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003149 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003150 update_topline();
3151 validate_cursor();
3152
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003153 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003154 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003155 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003156
3157 if (msg_scrolled == 0)
3158 {
3159 // don't want a hit-enter prompt when something else is displayed
3160 msg_didany = FALSE;
3161 need_wait_return = FALSE;
3162 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003163 }
3164 cursor_on();
3165#ifdef FEAT_GUI
3166 if (gui.in_use && !gui_mch_is_blink_off())
3167 // Don't update the cursor when it is blinking and off to avoid
3168 // flicker.
3169 out_flush_cursor(FALSE, FALSE);
3170 else
3171#endif
3172 out_flush();
3173
3174 --redrawing_for_callback;
3175}
3176
3177/*
3178 * Redraw the current window later, with update_screen(type).
3179 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003180 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003181 */
3182 void
3183redraw_later(int type)
3184{
3185 redraw_win_later(curwin, type);
3186}
3187
3188 void
3189redraw_win_later(
3190 win_T *wp,
3191 int type)
3192{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003193 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003194 {
3195 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003196 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003197 wp->w_lines_valid = 0;
3198 if (must_redraw < type) // must_redraw is the maximum of all windows
3199 must_redraw = type;
3200 }
3201}
3202
3203/*
3204 * Force a complete redraw later. Also resets the highlighting. To be used
3205 * after executing a shell command that messes up the screen.
3206 */
3207 void
3208redraw_later_clear(void)
3209{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003210 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003211 reset_screen_attr();
3212}
3213
3214/*
Bram Moolenaar13608d82022-08-29 15:06:50 +01003215 * Mark all windows to be redrawn later. Except popup windows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003216 */
3217 void
3218redraw_all_later(int type)
3219{
3220 win_T *wp;
3221
3222 FOR_ALL_WINDOWS(wp)
3223 redraw_win_later(wp, type);
3224 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003225 set_must_redraw(type);
3226}
3227
Bram Moolenaar13608d82022-08-29 15:06:50 +01003228#if 0 // not actually used yet, it probably should
3229/*
3230 * Mark all windows, including popup windows, to be redrawn.
3231 */
3232 void
3233redraw_all_windows_later(int type)
3234{
3235 redraw_all_later(type);
3236#ifdef FEAT_PROP_POPUP
3237 popup_redraw_all(); // redraw all popup windows
3238#endif
3239}
3240#endif
3241
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003242/*
3243 * Set "must_redraw" to "type" unless it already has a higher value
3244 * or it is currently not allowed.
3245 */
3246 void
3247set_must_redraw(int type)
3248{
3249 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003250 must_redraw = type;
3251}
3252
3253/*
3254 * Mark all windows that are editing the current buffer to be updated later.
3255 */
3256 void
3257redraw_curbuf_later(int type)
3258{
3259 redraw_buf_later(curbuf, type);
3260}
3261
3262 void
3263redraw_buf_later(buf_T *buf, int type)
3264{
3265 win_T *wp;
3266
3267 FOR_ALL_WINDOWS(wp)
3268 {
3269 if (wp->w_buffer == buf)
3270 redraw_win_later(wp, type);
3271 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003272#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3273 // terminal in popup window is not in list of windows
3274 if (curwin->w_buffer == buf)
3275 redraw_win_later(curwin, type);
3276#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003277}
3278
3279#if defined(FEAT_SIGNS) || defined(PROTO)
3280 void
3281redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3282{
3283 win_T *wp;
3284
3285 FOR_ALL_WINDOWS(wp)
3286 if (wp->w_buffer == buf && lnum >= wp->w_topline
3287 && lnum < wp->w_botline)
3288 redrawWinline(wp, lnum);
3289}
3290#endif
3291
3292#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3293 void
3294redraw_buf_and_status_later(buf_T *buf, int type)
3295{
3296 win_T *wp;
3297
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003298 if (wild_menu_showing != 0)
3299 // Don't redraw while the command line completion is displayed, it
3300 // would disappear.
3301 return;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003302 FOR_ALL_WINDOWS(wp)
3303 {
3304 if (wp->w_buffer == buf)
3305 {
3306 redraw_win_later(wp, type);
3307 wp->w_redr_status = TRUE;
3308 }
3309 }
3310}
3311#endif
3312
3313/*
3314 * mark all status lines for redraw; used after first :cd
3315 */
3316 void
3317status_redraw_all(void)
3318{
3319 win_T *wp;
3320
3321 FOR_ALL_WINDOWS(wp)
3322 if (wp->w_status_height)
3323 {
3324 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003325 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003326 }
3327}
3328
3329/*
3330 * mark all status lines of the current buffer for redraw
3331 */
3332 void
3333status_redraw_curbuf(void)
3334{
3335 win_T *wp;
3336
3337 FOR_ALL_WINDOWS(wp)
3338 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3339 {
3340 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003341 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003342 }
3343}
3344
3345/*
3346 * Redraw all status lines that need to be redrawn.
3347 */
3348 void
3349redraw_statuslines(void)
3350{
3351 win_T *wp;
3352
3353 FOR_ALL_WINDOWS(wp)
3354 if (wp->w_redr_status)
3355 win_redr_status(wp, FALSE);
3356 if (redraw_tabline)
3357 draw_tabline();
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003358
3359#if defined(FEAT_TABPANEL)
3360 if (redraw_tabpanel)
3361 draw_tabpanel();
3362#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003363}
3364
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003365/*
3366 * Redraw all status lines at the bottom of frame "frp".
3367 */
3368 void
3369win_redraw_last_status(frame_T *frp)
3370{
3371 if (frp->fr_layout == FR_LEAF)
3372 frp->fr_win->w_redr_status = TRUE;
3373 else if (frp->fr_layout == FR_ROW)
3374 {
3375 FOR_ALL_FRAMES(frp, frp->fr_child)
3376 win_redraw_last_status(frp);
3377 }
3378 else // frp->fr_layout == FR_COL
3379 {
3380 frp = frp->fr_child;
3381 while (frp->fr_next != NULL)
3382 frp = frp->fr_next;
3383 win_redraw_last_status(frp);
3384 }
3385}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003386
3387/*
3388 * Changed something in the current window, at buffer line "lnum", that
3389 * requires that line and possibly other lines to be redrawn.
3390 * Used when entering/leaving Insert mode with the cursor on a folded line.
3391 * Used to remove the "$" from a change command.
3392 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3393 * may become invalid and the whole window will have to be redrawn.
3394 */
3395 void
3396redrawWinline(
3397 win_T *wp,
3398 linenr_T lnum)
3399{
Luuk van Baal7bbb0f32025-02-22 09:19:04 +01003400 redraw_win_range_later(wp, lnum, lnum);
3401}
3402
3403 void
3404redraw_win_range_later(
3405 win_T *wp,
3406 linenr_T first,
3407 linenr_T last)
3408{
3409 if (last >= wp->w_topline && first < wp->w_botline)
3410 {
3411 if (wp->w_redraw_top == 0 || wp->w_redraw_top > first)
3412 wp->w_redraw_top = first;
3413 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < last)
3414 wp->w_redraw_bot = last;
3415 redraw_win_later(wp, UPD_VALID);
3416 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003417}