blob: 4736bf11201dc2a2eb742c5068ea42a773f96cfa [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 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200212 }
213 msg_scrolled = 0;
214 need_wait_return = FALSE;
215 }
216
217 // reset cmdline_row now (may have been changed temporarily)
218 compute_cmdrow();
219
220 // Check for changed highlighting
221 if (need_highlight_changed)
222 highlight_changed();
223
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100224 if (type == UPD_CLEAR) // first clear screen
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200225 {
226 screenclear(); // will reset clear_cmdline
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100227 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200228 // must_redraw may be set indirectly, avoid another redraw later
229 must_redraw = 0;
230 }
231
232 if (clear_cmdline) // going to clear cmdline (done below)
233 check_for_delay(FALSE);
234
235#ifdef FEAT_LINEBREAK
236 // Force redraw when width of 'number' or 'relativenumber' column
237 // changes.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100238 if (curwin->w_redr_type < UPD_NOT_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200239 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
240 ? number_width(curwin) : 0))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100241 curwin->w_redr_type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200242#endif
243
244 // Only start redrawing if there is really something to do.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100245 if (type == UPD_INVERTED)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200246 update_curswant();
247 if (curwin->w_redr_type < type
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100248 && !((type == UPD_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200249 && curwin->w_lines[0].wl_valid
250#ifdef FEAT_DIFF
251 && curwin->w_topfill == curwin->w_old_topfill
252 && curwin->w_botfill == curwin->w_old_botfill
253#endif
254 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100255 || (type == UPD_INVERTED
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200256 && VIsual_active
257 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
258 && curwin->w_old_visual_mode == VIsual_mode
259 && (curwin->w_valid & VALID_VIRTCOL)
260 && curwin->w_old_curswant == curwin->w_curswant)
261 ))
262 curwin->w_redr_type = type;
263
264 // Redraw the tab pages line if needed.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100265 if (redraw_tabline || type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200266 draw_tabline();
267
268#ifdef FEAT_SYN_HL
269 // Correct stored syntax highlighting info for changes in each displayed
270 // buffer. Each buffer must only be done once.
271 FOR_ALL_WINDOWS(wp)
272 {
273 if (wp->w_buffer->b_mod_set)
274 {
275 win_T *wwp;
276
277 // Check if we already did this buffer.
278 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
279 if (wwp->w_buffer == wp->w_buffer)
280 break;
281 if (wwp == wp && syntax_present(wp))
282 syn_stack_apply_changes(wp->w_buffer);
283 }
284 }
285#endif
286
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200287 if (pum_redraw_in_same_position())
288 // Avoid flicker if the popup menu is going to be redrawn in the same
289 // position.
290 pum_will_redraw = TRUE;
291
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200292 // Go from top to bottom through the windows, redrawing the ones that need
293 // it.
294#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100295 did_update_one_window = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200296#endif
297#ifdef FEAT_SEARCH_EXTRA
298 screen_search_hl.rm.regprog = NULL;
299#endif
300 FOR_ALL_WINDOWS(wp)
301 {
302 if (wp->w_redr_type != 0)
303 {
304 cursor_off();
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100305#ifdef FEAT_GUI
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200306 if (!did_one)
307 {
308 did_one = TRUE;
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100309
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200310 // Remove the cursor before starting to do anything, because
311 // scrolling may make it difficult to redraw the text under
312 // it.
Bram Moolenaar09f067f2021-04-11 13:29:18 +0200313 // Also remove the cursor if it needs to be hidden due to an
314 // ongoing cursor-less sleep.
315 if (gui.in_use && (wp == curwin || cursor_is_sleeping()))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200316 {
317 gui_cursor_col = gui.cursor_col;
318 gui_cursor_row = gui.cursor_row;
319 gui_undraw_cursor();
320 did_undraw = TRUE;
321 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200322 }
323#endif
324 win_update(wp);
325 }
326
327 // redraw status line after the window to minimize cursor movement
328 if (wp->w_redr_status)
329 {
330 cursor_off();
331 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
332 }
333 }
334#if defined(FEAT_SEARCH_EXTRA)
335 end_search_hl();
336#endif
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200337
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200338 // May need to redraw the popup menu.
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200339 pum_will_redraw = save_pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200340 pum_may_redraw();
341
342 // Reset b_mod_set flags. Going through all windows is probably faster
343 // than going through all buffers (there could be many buffers).
344 FOR_ALL_WINDOWS(wp)
345 wp->w_buffer->b_mod_set = FALSE;
346
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100347#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200348 // Display popup windows on top of the windows and command line.
349 update_popups(win_update);
350#endif
351
Bram Moolenaar3194e5b2021-12-13 21:59:09 +0000352#ifdef FEAT_TERMINAL
353 FOR_ALL_WINDOWS(wp)
354 // If this window contains a terminal, after redrawing all windows, the
355 // dirty row range can be reset.
356 term_did_update_window(wp);
357#endif
358
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200359 after_updating_screen(TRUE);
360
361 // Clear or redraw the command line. Done last, because scrolling may
362 // mess up the command line.
363 if (clear_cmdline || redraw_cmdline || redraw_mode)
364 showmode();
365
366 if (no_update)
367 --no_win_do_lines_ins;
368
369 // May put up an introductory message when not editing a file
370 if (!did_intro)
371 maybe_intro_message();
372 did_intro = TRUE;
373
374#ifdef FEAT_GUI
375 // Redraw the cursor and update the scrollbars when all screen updating is
376 // done.
377 if (gui.in_use)
378 {
379 if (did_undraw && !gui_mch_is_blink_off())
380 {
381 mch_disable_flush();
382 out_flush(); // required before updating the cursor
383 mch_enable_flush();
384
385 // Put the GUI position where the cursor was, gui_update_cursor()
386 // uses that.
387 gui.col = gui_cursor_col;
388 gui.row = gui_cursor_row;
389 gui.col = mb_fix_col(gui.col, gui.row);
390 gui_update_cursor(FALSE, FALSE);
391 gui_may_flush();
392 screen_cur_col = gui.col;
393 screen_cur_row = gui.row;
394 }
395 else
396 out_flush();
397 gui_update_scrollbars(FALSE);
398 }
399#endif
400 return OK;
401}
402
403/*
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200404 * Return the row for drawing the statusline and the ruler of window "wp".
405 */
Bram Moolenaar49c51b82021-04-01 16:16:18 +0200406 int
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200407statusline_row(win_T *wp)
408{
409#if defined(FEAT_PROP_POPUP)
410 // If the window is really zero height the winbar isn't displayed.
411 if (wp->w_frame->fr_height == wp->w_status_height && !popup_is_popup(wp))
412 return wp->w_winrow;
413#endif
414 return W_WINROW(wp) + wp->w_height;
415}
416
417/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200418 * Redraw the status line of window wp.
419 *
420 * If inversion is possible we use it. Else '=' characters are used.
421 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
422 * displayed.
423 */
Luuk van Baalba936f62022-12-15 13:15:39 +0000424 void
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200425win_redr_status(win_T *wp, int ignore_pum UNUSED)
426{
427 int row;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200428 int fillchar;
429 int attr;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200430 static int busy = FALSE;
431
432 // It's possible to get here recursively when 'statusline' (indirectly)
433 // invokes ":redrawstatus". Simply ignore the call then.
434 if (busy)
435 return;
436 busy = TRUE;
437
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200438 row = statusline_row(wp);
439
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200440 wp->w_redr_status = FALSE;
441 if (wp->w_status_height == 0)
442 {
443 // no status line, can only be last window
444 redraw_cmdline = TRUE;
445 }
446 else if (!redrawing()
447 // don't update status line when popup menu is visible and may be
448 // drawn over it, unless it will be redrawn later
449 || (!ignore_pum && pum_visible()))
450 {
451 // Don't redraw right now, do it later.
452 wp->w_redr_status = TRUE;
453 }
454#ifdef FEAT_STL_OPT
455 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
456 {
457 // redraw custom status line
458 redraw_custom_statusline(wp);
459 }
460#endif
461 else
462 {
John Marriotta21240b2025-01-08 20:10:59 +0100463 char_u *p;
464 int plen;
465 int NameBufflen;
466 int this_ru_col;
467 int n; // scratch value
468
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200469 fillchar = fillchar_status(&attr, wp);
470
471 get_trans_bufname(wp->w_buffer);
472 p = NameBuff;
John Marriotta21240b2025-01-08 20:10:59 +0100473 plen = (int)STRLEN(p);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200474
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000475 if ((bt_help(wp->w_buffer)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200476#ifdef FEAT_QUICKFIX
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000477 || wp->w_p_pvw
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200478#endif
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000479 || bufIsChanged(wp->w_buffer)
480 || wp->w_buffer->b_p_ro)
John Marriotta21240b2025-01-08 20:10:59 +0100481 && plen < MAXPATHL - 1)
John Marriott70dfc372025-01-16 18:58:20 +0100482 {
483 *(p + plen++) = ' '; // replace NUL with space
484 *(p + plen) = NUL; // NUL terminate the string
485 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200486 if (bt_help(wp->w_buffer))
John Marriotta21240b2025-01-08 20:10:59 +0100487 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[Help]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200488#ifdef FEAT_QUICKFIX
489 if (wp->w_p_pvw)
John Marriotta21240b2025-01-08 20:10:59 +0100490 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[Preview]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200491#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100492 if (bufIsChanged(wp->w_buffer) && !bt_terminal(wp->w_buffer))
John Marriotta21240b2025-01-08 20:10:59 +0100493 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", "[+]");
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200494 if (wp->w_buffer->b_p_ro)
John Marriotta21240b2025-01-08 20:10:59 +0100495 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[RO]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200496
497 this_ru_col = ru_col - (Columns - wp->w_width);
John Marriotta21240b2025-01-08 20:10:59 +0100498 n = (wp->w_width + 1) / 2;
499 if (this_ru_col < n)
500 this_ru_col = n;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200501 if (this_ru_col <= 1)
502 {
503 p = (char_u *)"<"; // No room for file name!
John Marriotta21240b2025-01-08 20:10:59 +0100504 plen = 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200505 }
506 else if (has_mbyte)
507 {
John Marriotta21240b2025-01-08 20:10:59 +0100508 int i;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200509
510 // Count total number of display cells.
John Marriotta21240b2025-01-08 20:10:59 +0100511 plen = mb_string2cells(p, -1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200512
513 // Find first character that will fit.
514 // Going from start to end is much faster for DBCS.
John Marriotta21240b2025-01-08 20:10:59 +0100515 for (i = 0; p[i] != NUL && plen >= this_ru_col - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200516 i += (*mb_ptr2len)(p + i))
John Marriotta21240b2025-01-08 20:10:59 +0100517 plen -= (*mb_ptr2cells)(p + i);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200518 if (i > 0)
519 {
520 p = p + i - 1;
521 *p = '<';
John Marriotta21240b2025-01-08 20:10:59 +0100522 ++plen;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200523 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200524 }
John Marriotta21240b2025-01-08 20:10:59 +0100525 else if (plen > this_ru_col - 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200526 {
John Marriotta21240b2025-01-08 20:10:59 +0100527 p += plen - (this_ru_col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200528 *p = '<';
John Marriotta21240b2025-01-08 20:10:59 +0100529 plen = this_ru_col - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200530 }
531
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200532 screen_puts(p, row, wp->w_wincol, attr);
John Marriotta21240b2025-01-08 20:10:59 +0100533 screen_fill(row, row + 1, plen + wp->w_wincol,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200534 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
535
John Marriotta21240b2025-01-08 20:10:59 +0100536 if ((NameBufflen = get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)) > 0
537 && (this_ru_col - plen) > (NameBufflen + 1))
538 screen_puts(NameBuff, row, (int)(this_ru_col - NameBufflen
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200539 - 1 + wp->w_wincol), attr);
540
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200541 win_redr_ruler(wp, TRUE, ignore_pum);
Luuk van Baalba936f62022-12-15 13:15:39 +0000542
543 // Draw the 'showcmd' information if 'showcmdloc' == "statusline".
544 if (p_sc && *p_sloc == 's')
545 {
John Marriotta21240b2025-01-08 20:10:59 +0100546 n = this_ru_col - plen - 2; // perform the calculation here so we only do it once
547 int width = MIN(10, n);
Luuk van Baalba936f62022-12-15 13:15:39 +0000548
549 if (width > 0)
550 screen_puts_len(showcmd_buf, width, row,
551 wp->w_wincol + this_ru_col - width - 1, attr);
552 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200553 }
554
555 /*
556 * May need to draw the character below the vertical separator.
557 */
558 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
559 {
560 if (stl_connected(wp))
561 fillchar = fillchar_status(&attr, wp);
562 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100563 fillchar = fillchar_vsep(&attr, wp);
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200564 screen_putchar(fillchar, row, W_ENDCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200565 }
566 busy = FALSE;
567}
568
569#ifdef FEAT_STL_OPT
570/*
571 * Redraw the status line according to 'statusline' and take care of any
572 * errors encountered.
573 */
574 static void
575redraw_custom_statusline(win_T *wp)
576{
577 static int entered = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200578
579 // When called recursively return. This can happen when the statusline
580 // contains an expression that triggers a redraw.
581 if (entered)
582 return;
583 entered = TRUE;
584
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200585 win_redr_custom(wp, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200586 entered = FALSE;
587}
588#endif
589
590/*
591 * Show current status info in ruler and various other places
592 * If always is FALSE, only show ruler if position has changed.
593 */
594 void
595showruler(int always)
596{
597 if (!always && !redrawing())
598 return;
599 if (pum_visible())
600 {
601 // Don't redraw right now, do it later.
602 curwin->w_redr_status = TRUE;
603 return;
604 }
605#if defined(FEAT_STL_OPT)
606 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
607 redraw_custom_statusline(curwin);
608 else
609#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200610 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200611
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200612 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000613#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200614 || (p_icon && (stl_syntax & STL_IN_ICON))
615 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000616#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200617 )
618 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000619
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200620 // Redraw the tab pages line if needed.
621 if (redraw_tabline)
622 draw_tabline();
623}
624
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200625 void
626win_redr_ruler(win_T *wp, int always, int ignore_pum)
627{
John Marriotta21240b2025-01-08 20:10:59 +0100628 int empty_line = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200629
Bram Moolenaara2a89732022-08-31 14:46:18 +0100630 // If 'ruler' off don't do anything
631 if (!p_ru)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200632 return;
633
634 /*
635 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
636 * after deleting lines, before cursor.lnum is corrected.
637 */
638 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
639 return;
640
641 // Don't draw the ruler while doing insert-completion, it might overwrite
642 // the (long) mode message.
643 if (wp == lastwin && lastwin->w_status_height == 0)
644 if (edit_submode != NULL)
645 return;
John Marriotta21240b2025-01-08 20:10:59 +0100646
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200647 // Don't draw the ruler when the popup menu is visible, it may overlap.
648 // Except when the popup menu will be redrawn anyway.
649 if (!ignore_pum && pum_visible())
650 return;
651
652#ifdef FEAT_STL_OPT
Bram Moolenaara2a89732022-08-31 14:46:18 +0100653 if (*p_ruf)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200654 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200655 win_redr_custom(wp, TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200656 return;
657 }
658#endif
659
660 /*
661 * Check if not in Insert mode and the line is empty (will show "0-1").
662 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100663 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200664 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
665 empty_line = TRUE;
666
667 /*
668 * Only draw the ruler when something changed.
669 */
670 validate_virtcol_win(wp);
671 if ( redraw_cmdline
672 || always
673 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
674 || wp->w_cursor.col != wp->w_ru_cursor.col
675 || wp->w_virtcol != wp->w_ru_virtcol
676 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
677 || wp->w_topline != wp->w_ru_topline
678 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
679#ifdef FEAT_DIFF
680 || wp->w_topfill != wp->w_ru_topfill
681#endif
682 || empty_line != wp->w_ru_empty)
683 {
John Marriotta21240b2025-01-08 20:10:59 +0100684 int row;
685 int fillchar;
686 int attr;
687 int off;
688 int width;
689 colnr_T virtcol;
690#define RULER_BUF_LEN 70
691 char_u buffer[RULER_BUF_LEN];
692 int bufferlen;
693 char_u rel_pos[RULER_BUF_LEN];
694 int rel_poslen;
695 int this_ru_col;
696 int n1; // scratch value
697 int n2; // scratch value
698
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200699 cursor_off();
700 if (wp->w_status_height)
701 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200702 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200703 fillchar = fillchar_status(&attr, wp);
704 off = wp->w_wincol;
705 width = wp->w_width;
706 }
707 else
708 {
709 row = Rows - 1;
710 fillchar = ' ';
711 attr = 0;
712 width = Columns;
713 off = 0;
714 }
715
716 // In list mode virtcol needs to be recomputed
717 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100718 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200719 {
720 wp->w_p_list = FALSE;
721 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
722 wp->w_p_list = TRUE;
723 }
724
John Marriotta21240b2025-01-08 20:10:59 +0100725 bufferlen = vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200726 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
727 ? 0L
728 : (long)(wp->w_cursor.lnum));
John Marriotta21240b2025-01-08 20:10:59 +0100729 bufferlen += col_print(buffer + bufferlen, RULER_BUF_LEN - bufferlen,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200730 empty_line ? 0 : (int)wp->w_cursor.col + 1,
731 (int)virtcol + 1);
732
733 /*
734 * Add a "50%" if there is room for it.
735 * On the last line, don't print in the last column (scrolls the
736 * screen up on some terminals).
737 */
John Marriotta21240b2025-01-08 20:10:59 +0100738 rel_poslen = get_rel_pos(wp, rel_pos, RULER_BUF_LEN);
739 n1 = bufferlen + vim_strsize(rel_pos);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200740 if (wp->w_status_height == 0) // can't use last char of screen
John Marriotta21240b2025-01-08 20:10:59 +0100741 ++n1;
742
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200743 this_ru_col = ru_col - (Columns - width);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200744 // Never use more than half the window/screen width, leave the other
745 // half for the filename.
John Marriotta21240b2025-01-08 20:10:59 +0100746 n2 = (width + 1) / 2;
747 if (this_ru_col < n2)
748 this_ru_col = n2;
749 if (this_ru_col + n1 < width)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200750 {
John Marriotta21240b2025-01-08 20:10:59 +0100751 // need at least space for rel_pos + NUL
752 while (this_ru_col + n1 < width
753 && RULER_BUF_LEN > bufferlen + rel_poslen + 1) // +1 for NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200754 {
755 if (has_mbyte)
John Marriotta21240b2025-01-08 20:10:59 +0100756 bufferlen += (*mb_char2bytes)(fillchar, buffer + bufferlen);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200757 else
John Marriotta21240b2025-01-08 20:10:59 +0100758 buffer[bufferlen++] = fillchar;
759 ++n1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200760 }
John Marriotta21240b2025-01-08 20:10:59 +0100761 bufferlen += vim_snprintf((char *)buffer + bufferlen, RULER_BUF_LEN - bufferlen,
762 "%s", rel_pos);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200763 }
764 // Truncate at window boundary.
765 if (has_mbyte)
766 {
John Marriotta21240b2025-01-08 20:10:59 +0100767 for (n1 = 0, n2 = 0; buffer[n1] != NUL; n1 += (*mb_ptr2len)(buffer + n1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200768 {
John Marriotta21240b2025-01-08 20:10:59 +0100769 n2 += (*mb_ptr2cells)(buffer + n1);
770 if (this_ru_col + n2 > width)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200771 {
John Marriotta21240b2025-01-08 20:10:59 +0100772 bufferlen = n1;
773 buffer[bufferlen] = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200774 break;
775 }
776 }
777 }
John Marriotta21240b2025-01-08 20:10:59 +0100778 else if (this_ru_col + bufferlen > width)
779 {
780 bufferlen = width - this_ru_col;
781 buffer[bufferlen] = NUL;
782 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200783
784 screen_puts(buffer, row, this_ru_col + off, attr);
John Marriotta21240b2025-01-08 20:10:59 +0100785 n1 = redraw_cmdline;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200786 screen_fill(row, row + 1,
John Marriotta21240b2025-01-08 20:10:59 +0100787 this_ru_col + off + bufferlen,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000788 (off + width),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200789 fillchar, fillchar, attr);
790 // don't redraw the cmdline because of showing the ruler
John Marriotta21240b2025-01-08 20:10:59 +0100791 redraw_cmdline = n1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200792 wp->w_ru_cursor = wp->w_cursor;
793 wp->w_ru_virtcol = wp->w_virtcol;
794 wp->w_ru_empty = empty_line;
795 wp->w_ru_topline = wp->w_topline;
796 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
797#ifdef FEAT_DIFF
798 wp->w_ru_topfill = wp->w_topfill;
799#endif
800 }
801}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200802
803/*
804 * To be called when "updating_screen" was set before and now the postponed
805 * side effects may take place.
806 */
807 void
808after_updating_screen(int may_resize_shell UNUSED)
809{
810 updating_screen = FALSE;
811#ifdef FEAT_GUI
812 if (may_resize_shell)
813 gui_may_resize_shell();
814#endif
815#ifdef FEAT_TERMINAL
816 term_check_channel_closed_recently();
817#endif
818
819#ifdef HAVE_DROP_FILE
820 // If handle_drop() was called while updating_screen was TRUE need to
821 // handle the drop now.
822 handle_any_postponed_drop();
823#endif
824}
825
826/*
827 * Update all windows that are editing the current buffer.
828 */
829 void
830update_curbuf(int type)
831{
832 redraw_curbuf_later(type);
833 update_screen(type);
834}
835
836#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
837/*
838 * Copy "text" to ScreenLines using "attr".
839 * Returns the next screen column.
840 */
841 static int
842text_to_screenline(win_T *wp, char_u *text, int col)
843{
844 int off = (int)(current_ScreenLine - ScreenLines);
845
846 if (has_mbyte)
847 {
848 int cells;
849 int u8c, u8cc[MAX_MCO];
850 int i;
851 int idx;
852 int c_len;
853 char_u *p;
854# ifdef FEAT_ARABIC
855 int prev_c = 0; // previous Arabic character
856 int prev_c1 = 0; // first composing char for prev_c
857# endif
858
859# ifdef FEAT_RIGHTLEFT
860 if (wp->w_p_rl)
861 idx = off;
862 else
863# endif
864 idx = off + col;
865
866 // Store multibyte characters in ScreenLines[] et al. correctly.
867 for (p = text; *p != NUL; )
868 {
869 cells = (*mb_ptr2cells)(p);
870 c_len = (*mb_ptr2len)(p);
871 if (col + cells > wp->w_width
872# ifdef FEAT_RIGHTLEFT
873 - (wp->w_p_rl ? col : 0)
874# endif
875 )
876 break;
877 ScreenLines[idx] = *p;
878 if (enc_utf8)
879 {
880 u8c = utfc_ptr2char(p, u8cc);
881 if (*p < 0x80 && u8cc[0] == 0)
882 {
883 ScreenLinesUC[idx] = 0;
884#ifdef FEAT_ARABIC
885 prev_c = u8c;
886#endif
887 }
888 else
889 {
890#ifdef FEAT_ARABIC
891 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
892 {
893 // Do Arabic shaping.
894 int pc, pc1, nc;
895 int pcc[MAX_MCO];
896 int firstbyte = *p;
897
898 // The idea of what is the previous and next
899 // character depends on 'rightleft'.
900 if (wp->w_p_rl)
901 {
902 pc = prev_c;
903 pc1 = prev_c1;
904 nc = utf_ptr2char(p + c_len);
905 prev_c1 = u8cc[0];
906 }
907 else
908 {
909 pc = utfc_ptr2char(p + c_len, pcc);
910 nc = prev_c;
911 pc1 = pcc[0];
912 }
913 prev_c = u8c;
914
915 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
916 pc, pc1, nc);
917 ScreenLines[idx] = firstbyte;
918 }
919 else
920 prev_c = u8c;
921#endif
922 // Non-BMP character: display as ? or fullwidth ?.
923 ScreenLinesUC[idx] = u8c;
924 for (i = 0; i < Screen_mco; ++i)
925 {
926 ScreenLinesC[i][idx] = u8cc[i];
927 if (u8cc[i] == 0)
928 break;
929 }
930 }
931 if (cells > 1)
932 ScreenLines[idx + 1] = 0;
933 }
934 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
935 // double-byte single width character
936 ScreenLines2[idx] = p[1];
937 else if (cells > 1)
938 // double-width character
939 ScreenLines[idx + 1] = p[1];
940 col += cells;
941 idx += cells;
942 p += c_len;
943 }
944 }
945 else
946 {
947 int len = (int)STRLEN(text);
John Marriotta21240b2025-01-08 20:10:59 +0100948 int n = wp->w_width - col;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200949
John Marriotta21240b2025-01-08 20:10:59 +0100950 if (len > n)
951 len = n;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200952 if (len > 0)
953 {
954#ifdef FEAT_RIGHTLEFT
955 if (wp->w_p_rl)
956 mch_memmove(current_ScreenLine, text, len);
957 else
958#endif
959 mch_memmove(current_ScreenLine + col, text, len);
960 col += len;
961 }
962 }
963 return col;
964}
965#endif
966
967#ifdef FEAT_MENU
968/*
969 * Draw the window toolbar.
970 */
971 static void
972redraw_win_toolbar(win_T *wp)
973{
974 vimmenu_T *menu;
975 int item_idx = 0;
976 int item_count = 0;
977 int col = 0;
978 int next_col;
979 int off = (int)(current_ScreenLine - ScreenLines);
980 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
981 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
982
983 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200984 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200985 ++item_count;
986 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
987
988 // TODO: use fewer spaces if there is not enough room
989 for (menu = wp->w_winbar->children;
990 menu != NULL && col < wp->w_width; menu = menu->next)
991 {
992 space_to_screenline(off + col, fill_attr);
993 if (++col >= wp->w_width)
994 break;
995 if (col > 1)
996 {
997 space_to_screenline(off + col, fill_attr);
998 if (++col >= wp->w_width)
999 break;
1000 }
1001
1002 wp->w_winbar_items[item_idx].wb_startcol = col;
1003 space_to_screenline(off + col, button_attr);
1004 if (++col >= wp->w_width)
1005 break;
1006
1007 next_col = text_to_screenline(wp, menu->name, col);
1008 while (col < next_col)
1009 {
1010 ScreenAttrs[off + col] = button_attr;
1011 ++col;
1012 }
1013 wp->w_winbar_items[item_idx].wb_endcol = col;
1014 wp->w_winbar_items[item_idx].wb_menu = menu;
1015 ++item_idx;
1016
1017 if (col >= wp->w_width)
1018 break;
1019 space_to_screenline(off + col, button_attr);
1020 ++col;
1021 }
1022 while (col < wp->w_width)
1023 {
1024 space_to_screenline(off + col, fill_attr);
1025 ++col;
1026 }
1027 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1028
zeertzjqd0c1b772024-03-16 15:03:33 +01001029 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width, wp->w_width, -1,
1030 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001031}
1032#endif
1033
1034#if defined(FEAT_FOLDING) || defined(PROTO)
1035/*
1036 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1037 */
1038 static void
1039copy_text_attr(
1040 int off,
1041 char_u *buf,
1042 int len,
1043 int attr)
1044{
1045 int i;
1046
1047 mch_memmove(ScreenLines + off, buf, (size_t)len);
1048 if (enc_utf8)
1049 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1050 for (i = 0; i < len; ++i)
1051 ScreenAttrs[off + i] = attr;
1052}
1053
1054/*
1055 * Display one folded line.
1056 */
1057 static void
1058fold_line(
1059 win_T *wp,
1060 long fold_count,
1061 foldinfo_T *foldinfo,
1062 linenr_T lnum,
1063 int row)
1064{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001065 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1066 // multi-byte character is MAX_MCO.
1067 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001068 pos_T *top, *bot;
1069 linenr_T lnume = lnum + fold_count - 1;
1070 int len;
1071 char_u *text;
1072 int fdc;
1073 int col;
1074 int txtcol;
1075 int off = (int)(current_ScreenLine - ScreenLines);
1076 int ri;
1077
1078 // Build the fold line:
1079 // 1. Add the cmdwin_type for the command-line window
1080 // 2. Add the 'foldcolumn'
1081 // 3. Add the 'number' or 'relativenumber' column
1082 // 4. Compose the text
1083 // 5. Add the text
1084 // 6. set highlighting for the Visual area an other text
1085 col = 0;
1086
1087 // 1. Add the cmdwin_type for the command-line window
1088 // Ignores 'rightleft', this window is never right-left.
Sean Dewar988f7432023-08-16 14:17:36 +01001089 if (wp == cmdwin_win)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001090 {
1091 ScreenLines[off] = cmdwin_type;
1092 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1093 if (enc_utf8)
1094 ScreenLinesUC[off] = 0;
1095 ++col;
1096 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001097
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001098#ifdef FEAT_RIGHTLEFT
1099# define RL_MEMSET(p, v, l) \
1100 do { \
1101 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001102 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001103 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1104 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001105 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001106 ScreenAttrs[off + (p) + ri] = v; \
1107 } while (0)
1108#else
1109# define RL_MEMSET(p, v, l) \
1110 do { \
1111 for (ri = 0; ri < l; ++ri) \
1112 ScreenAttrs[off + (p) + ri] = v; \
1113 } while (0)
1114#endif
1115
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001116 // 2. Add the 'foldcolumn'
1117 // Reduce the width when there is not enough space.
1118 fdc = compute_foldcolumn(wp, col);
1119 if (fdc > 0)
1120 {
1121 char_u *p;
1122 int i;
1123 int idx;
1124
1125 fill_foldcolumn(buf, wp, TRUE, lnum);
1126 p = buf;
1127 for (i = 0; i < fdc; i++)
1128 {
1129 int ch;
1130
1131 if (has_mbyte)
1132 ch = mb_ptr2char_adv(&p);
1133 else
1134 ch = *p++;
1135#ifdef FEAT_RIGHTLEFT
1136 if (wp->w_p_rl)
1137 idx = off + wp->w_width - i - 1 - col;
1138 else
1139#endif
1140 idx = off + col + i;
1141 if (enc_utf8)
1142 {
1143 if (ch >= 0x80)
1144 {
1145 ScreenLinesUC[idx] = ch;
1146 ScreenLinesC[0][idx] = 0;
1147 ScreenLines[idx] = 0x80;
1148 }
1149 else
1150 {
1151 ScreenLines[idx] = ch;
1152 ScreenLinesUC[idx] = 0;
1153 }
1154 }
1155 else
1156 ScreenLines[idx] = ch;
1157 }
1158
1159 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1160 col += fdc;
1161 }
1162
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001163 // Set all attributes of the 'number' or 'relativenumber' column and the
1164 // text
1165 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1166
1167#ifdef FEAT_SIGNS
1168 // If signs are being displayed, add two spaces.
1169 if (signcolumn_on(wp))
1170 {
1171 len = wp->w_width - col;
1172 if (len > 0)
1173 {
1174 if (len > 2)
1175 len = 2;
1176# ifdef FEAT_RIGHTLEFT
1177 if (wp->w_p_rl)
1178 // the line number isn't reversed
1179 copy_text_attr(off + wp->w_width - len - col,
1180 (char_u *)" ", len, HL_ATTR(HLF_FL));
1181 else
1182# endif
1183 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1184 col += len;
1185 }
1186 }
1187#endif
1188
1189 // 3. Add the 'number' or 'relativenumber' column
1190 if (wp->w_p_nu || wp->w_p_rnu)
1191 {
1192 len = wp->w_width - col;
1193 if (len > 0)
1194 {
1195 int w = number_width(wp);
1196 long num;
1197 char *fmt = "%*ld ";
1198
1199 if (len > w + 1)
1200 len = w + 1;
1201
1202 if (wp->w_p_nu && !wp->w_p_rnu)
1203 // 'number' + 'norelativenumber'
1204 num = (long)lnum;
1205 else
1206 {
1207 // 'relativenumber', don't use negative numbers
1208 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1209 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1210 {
1211 // 'number' + 'relativenumber': cursor line shows absolute
1212 // line number
1213 num = lnum;
1214 fmt = "%-*ld ";
1215 }
1216 }
1217
John Marriotta21240b2025-01-08 20:10:59 +01001218 vim_snprintf((char *)buf, sizeof(buf), fmt, w, num);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001219#ifdef FEAT_RIGHTLEFT
1220 if (wp->w_p_rl)
1221 // the line number isn't reversed
1222 copy_text_attr(off + wp->w_width - len - col, buf, len,
1223 HL_ATTR(HLF_FL));
1224 else
1225#endif
1226 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1227 col += len;
1228 }
1229 }
1230
1231 // 4. Compose the folded-line string with 'foldtext', if set.
1232 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1233
1234 txtcol = col; // remember where text starts
1235
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001236 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1237 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001238 // Right-left text is put in columns 0 - number-col, normal text is put
1239 // in columns number-col - window-width.
1240 col = text_to_screenline(wp, text, col);
1241
1242 // Fill the rest of the line with the fold filler
1243#ifdef FEAT_RIGHTLEFT
1244 if (wp->w_p_rl)
1245 col -= txtcol;
1246#endif
1247 while (col < wp->w_width
1248#ifdef FEAT_RIGHTLEFT
1249 - (wp->w_p_rl ? txtcol : 0)
1250#endif
1251 )
1252 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001253 int c = wp->w_fill_chars.fold;
1254
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001255 if (enc_utf8)
1256 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001257 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001258 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001259 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001260 ScreenLinesC[0][off + col] = 0;
1261 ScreenLines[off + col] = 0x80; // avoid storing zero
1262 }
1263 else
1264 {
1265 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001266 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001267 }
1268 col++;
1269 }
1270 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001271 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001272 }
1273
1274 if (text != buf)
1275 vim_free(text);
1276
1277 // 6. set highlighting for the Visual area an other text.
1278 // If all folded lines are in the Visual area, highlight the line.
1279 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1280 {
1281 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1282 {
1283 // Visual is after curwin->w_cursor
1284 top = &curwin->w_cursor;
1285 bot = &VIsual;
1286 }
1287 else
1288 {
1289 // Visual is before curwin->w_cursor
1290 top = &VIsual;
1291 bot = &curwin->w_cursor;
1292 }
1293 if (lnum >= top->lnum
1294 && lnume <= bot->lnum
1295 && (VIsual_mode != 'v'
1296 || ((lnum > top->lnum
1297 || (lnum == top->lnum
1298 && top->col == 0))
1299 && (lnume < bot->lnum
1300 || (lnume == bot->lnum
1301 && (bot->col - (*p_sel == 'e'))
zeertzjq94b7c322024-03-12 21:50:32 +01001302 >= ml_get_buf_len(wp->w_buffer, lnume))))))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001303 {
1304 if (VIsual_mode == Ctrl_V)
1305 {
1306 // Visual block mode: highlight the chars part of the block
1307 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1308 {
1309 if (wp->w_old_cursor_lcol != MAXCOL
1310 && wp->w_old_cursor_lcol + txtcol
1311 < (colnr_T)wp->w_width)
1312 len = wp->w_old_cursor_lcol;
1313 else
1314 len = wp->w_width - txtcol;
1315 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1316 len - (int)wp->w_old_cursor_fcol);
1317 }
1318 }
1319 else
1320 {
1321 // Set all attributes of the text
1322 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1323 }
1324 }
1325 }
1326
1327#ifdef FEAT_SYN_HL
1328 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1329 if (wp->w_p_cc_cols)
1330 {
1331 int i = 0;
1332 int j = wp->w_p_cc_cols[i];
1333 int old_txtcol = txtcol;
1334
1335 while (j > -1)
1336 {
1337 txtcol += j;
1338 if (wp->w_p_wrap)
1339 txtcol -= wp->w_skipcol;
1340 else
1341 txtcol -= wp->w_leftcol;
1342 if (txtcol >= 0 && txtcol < wp->w_width)
1343 ScreenAttrs[off + txtcol] = hl_combine_attr(
1344 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1345 txtcol = old_txtcol;
1346 j = wp->w_p_cc_cols[++i];
1347 }
1348 }
1349
1350 // Show 'cursorcolumn' in the fold line.
1351 if (wp->w_p_cuc)
1352 {
1353 txtcol += wp->w_virtcol;
1354 if (wp->w_p_wrap)
1355 txtcol -= wp->w_skipcol;
1356 else
1357 txtcol -= wp->w_leftcol;
1358 if (txtcol >= 0 && txtcol < wp->w_width)
1359 ScreenAttrs[off + txtcol] = hl_combine_attr(
1360 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1361 }
1362#endif
1363
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001364 screen_line(wp, row + W_WINROW(wp), wp->w_wincol,
zeertzjqd0c1b772024-03-16 15:03:33 +01001365 wp->w_width, wp->w_width, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001366
1367 // Update w_cline_height and w_cline_folded if the cursor line was
1368 // updated (saves a call to plines() later).
1369 if (wp == curwin
1370 && lnum <= curwin->w_cursor.lnum
1371 && lnume >= curwin->w_cursor.lnum)
1372 {
1373 curwin->w_cline_row = row;
1374 curwin->w_cline_height = 1;
1375 curwin->w_cline_folded = TRUE;
1376 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1377 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001378
1379# ifdef FEAT_CONCEAL
1380 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001381 if (wp == curwin
1382 && wp->w_cursor.lnum >= lnum
1383 && wp->w_cursor.lnum <= lnume
1384 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001385 curs_columns(TRUE);
1386# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001387}
1388#endif
1389
1390/*
1391 * Update a single window.
1392 *
1393 * This may cause the windows below it also to be redrawn (when clearing the
1394 * screen or scrolling lines).
1395 *
1396 * How the window is redrawn depends on wp->w_redr_type. Each type also
1397 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001398 * UPD_NOT_VALID redraw the whole window
1399 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1400 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1401 * UPD_VALID
1402 * UPD_INVERTED redraw the changed part of the Visual area
1403 * UPD_INVERTED_ALL redraw the whole Visual area
1404 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001405 * 2. update lines at the top when scrolled down
1406 * 3. redraw changed text:
1407 * - if wp->w_buffer->b_mod_set set, update lines between
1408 * b_mod_top and b_mod_bot.
1409 * - if wp->w_redraw_top non-zero, redraw lines between
zeertzjqf2d90a32024-02-12 20:28:01 +01001410 * wp->w_redraw_top and wp->w_redraw_bot.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001411 * - continue redrawing when syntax status is invalid.
1412 * 4. if scrolled up, update lines at the bottom.
1413 * This results in three areas that may need updating:
1414 * top: from first row to top_end (when scrolled down)
1415 * mid: from mid_start to mid_end (update inversion or changed text)
1416 * bot: from bot_start to last row (when scrolled up)
1417 */
1418 static void
1419win_update(win_T *wp)
1420{
1421 buf_T *buf = wp->w_buffer;
1422 int type;
1423 int top_end = 0; // Below last row of the top area that needs
1424 // updating. 0 when no top area updating.
1425 int mid_start = 999;// first row of the mid area that needs
1426 // updating. 999 when no mid area updating.
1427 int mid_end = 0; // Below last row of the mid area that needs
1428 // updating. 0 when no mid area updating.
1429 int bot_start = 999;// first row of the bot area that needs
1430 // updating. 999 when no bot area updating
1431 int scrolled_down = FALSE; // TRUE when scrolled down when
1432 // w_topline got smaller a bit
1433#ifdef FEAT_SEARCH_EXTRA
1434 int top_to_mod = FALSE; // redraw above mod_top
1435#endif
1436
1437 int row; // current window row to display
1438 linenr_T lnum; // current buffer lnum to display
1439 int idx; // current index in w_lines[]
1440 int srow; // starting row of the current line
1441
1442 int eof = FALSE; // if TRUE, we hit the end of the file
1443 int didline = FALSE; // if TRUE, we finished the last line
1444 int i;
1445 long j;
1446 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001447 linenr_T old_botline = wp->w_botline;
1448#ifdef FEAT_CONCEAL
1449 int old_wrow = wp->w_wrow;
1450 int old_wcol = wp->w_wcol;
1451#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001452#ifdef FEAT_FOLDING
1453 long fold_count;
1454#endif
1455#ifdef FEAT_SYN_HL
1456 // remember what happened to the previous line, to know if
1457 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001458# define DID_NONE 1 // didn't update a line
1459# define DID_LINE 2 // updated a normal line
1460# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001461 int did_update = DID_NONE;
1462 linenr_T syntax_last_parsed = 0; // last parsed text line
1463#endif
1464 linenr_T mod_top = 0;
1465 linenr_T mod_bot = 0;
1466#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1467 int save_got_int;
1468#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001469
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001470#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1471 // This needs to be done only for the first window when update_screen() is
1472 // called.
1473 if (!did_update_one_window)
1474 {
1475 did_update_one_window = TRUE;
1476# ifdef FEAT_SEARCH_EXTRA
1477 start_search_hl();
1478# endif
1479# ifdef FEAT_CLIPBOARD
1480 // When Visual area changed, may have to update selection.
1481 if (clip_star.available && clip_isautosel_star())
1482 clip_update_selection(&clip_star);
1483 if (clip_plus.available && clip_isautosel_plus())
1484 clip_update_selection(&clip_plus);
1485# endif
1486 }
1487#endif
1488
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001489 type = wp->w_redr_type;
1490
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001491 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001492 {
1493 wp->w_redr_status = TRUE;
1494 wp->w_lines_valid = 0;
1495 }
1496
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001497 // Window frame is zero-height: nothing to draw.
1498 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1499 || (wp->w_frame->fr_height == wp->w_status_height
1500#if defined(FEAT_PROP_POPUP)
1501 && !popup_is_popup(wp)
1502#endif
1503 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001504 {
1505 wp->w_redr_type = 0;
1506 return;
1507 }
1508
1509 // Window is zero-width: Only need to draw the separator.
1510 if (wp->w_width == 0)
1511 {
1512 // draw the vertical separator right of this window
1513 draw_vsep_win(wp, 0);
1514 wp->w_redr_type = 0;
1515 return;
1516 }
1517
1518#ifdef FEAT_TERMINAL
1519 // If this window contains a terminal, redraw works completely differently.
1520 if (term_do_update_window(wp))
1521 {
1522 term_update_window(wp);
1523# ifdef FEAT_MENU
1524 // Draw the window toolbar, if there is one.
1525 if (winbar_height(wp) > 0)
1526 redraw_win_toolbar(wp);
1527# endif
1528 wp->w_redr_type = 0;
1529 return;
1530 }
1531#endif
1532
1533#ifdef FEAT_SEARCH_EXTRA
1534 init_search_hl(wp, &screen_search_hl);
1535#endif
1536
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001537 // Make sure skipcol is valid, it depends on various options and the window
1538 // width.
Sean Dewar02fcae02024-02-21 19:40:44 +01001539 if (wp->w_skipcol > 0 && wp->w_width > win_col_off(wp))
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001540 {
1541 int w = 0;
1542 int width1 = wp->w_width - win_col_off(wp);
1543 int width2 = width1 + win_col_off2(wp);
1544 int add = width1;
1545
1546 while (w < wp->w_skipcol)
1547 {
1548 if (w > 0)
1549 add = width2;
1550 w += add;
1551 }
1552 if (w != wp->w_skipcol)
1553 // always round down, the higher value may not be valid
1554 wp->w_skipcol = w - add;
1555 }
1556
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001557#ifdef FEAT_LINEBREAK
1558 // Force redraw when width of 'number' or 'relativenumber' column
1559 // changes.
1560 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1561 if (wp->w_nrwidth != i)
1562 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001563 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001564 wp->w_nrwidth = i;
1565 }
1566 else
1567#endif
1568
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001569 {
1570 // Set mod_top to the first line that needs displaying because of
1571 // changes. Set mod_bot to the first line after the changes.
1572 mod_top = wp->w_redraw_top;
1573 if (wp->w_redraw_bot != 0)
1574 mod_bot = wp->w_redraw_bot + 1;
1575 else
1576 mod_bot = 0;
1577 if (buf->b_mod_set)
1578 {
1579 if (mod_top == 0 || mod_top > buf->b_mod_top)
1580 {
1581 mod_top = buf->b_mod_top;
1582#ifdef FEAT_SYN_HL
1583 // Need to redraw lines above the change that may be included
1584 // in a pattern match.
1585 if (syntax_present(wp))
1586 {
1587 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1588 if (mod_top < 1)
1589 mod_top = 1;
1590 }
1591#endif
1592 }
1593 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1594 mod_bot = buf->b_mod_bot;
1595
1596#ifdef FEAT_SEARCH_EXTRA
1597 // When 'hlsearch' is on and using a multi-line search pattern, a
1598 // change in one line may make the Search highlighting in a
1599 // previous line invalid. Simple solution: redraw all visible
1600 // lines above the change.
1601 // Same for a match pattern.
1602 if (screen_search_hl.rm.regprog != NULL
1603 && re_multiline(screen_search_hl.rm.regprog))
1604 top_to_mod = TRUE;
1605 else
1606 {
1607 matchitem_T *cur = wp->w_match_head;
1608
1609 while (cur != NULL)
1610 {
Bram Moolenaar50faf022022-09-29 12:50:17 +01001611 if (cur->mit_match.regprog != NULL
1612 && re_multiline(cur->mit_match.regprog))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001613 {
1614 top_to_mod = TRUE;
1615 break;
1616 }
Bram Moolenaar50faf022022-09-29 12:50:17 +01001617 cur = cur->mit_next;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001618 }
1619 }
1620#endif
1621 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001622
1623#ifdef FEAT_SEARCH_EXTRA
1624 if (search_hl_has_cursor_lnum > 0)
1625 {
1626 // CurSearch was used last time, need to redraw the line with it to
1627 // avoid having two matches highlighted with CurSearch.
1628 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1629 mod_top = search_hl_has_cursor_lnum;
1630 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1631 mod_bot = search_hl_has_cursor_lnum + 1;
1632 }
1633#endif
1634
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001635#ifdef FEAT_FOLDING
1636 if (mod_top != 0 && hasAnyFolding(wp))
1637 {
1638 linenr_T lnumt, lnumb;
1639
1640 // A change in a line can cause lines above it to become folded or
1641 // unfolded. Find the top most buffer line that may be affected.
1642 // If the line was previously folded and displayed, get the first
1643 // line of that fold. If the line is folded now, get the first
1644 // folded line. Use the minimum of these two.
1645
1646 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1647 // the line below it. If there is no valid entry, use w_topline.
1648 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1649 // to this line. If there is no valid entry, use MAXLNUM.
1650 lnumt = wp->w_topline;
1651 lnumb = MAXLNUM;
1652 for (i = 0; i < wp->w_lines_valid; ++i)
1653 if (wp->w_lines[i].wl_valid)
1654 {
1655 if (wp->w_lines[i].wl_lastlnum < mod_top)
1656 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1657 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1658 {
1659 lnumb = wp->w_lines[i].wl_lnum;
1660 // When there is a fold column it might need updating
1661 // in the next line ("J" just above an open fold).
1662 if (compute_foldcolumn(wp, 0) > 0)
1663 ++lnumb;
1664 }
1665 }
1666
1667 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1668 if (mod_top > lnumt)
1669 mod_top = lnumt;
1670
1671 // Now do the same for the bottom line (one above mod_bot).
1672 --mod_bot;
1673 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1674 ++mod_bot;
1675 if (mod_bot < lnumb)
1676 mod_bot = lnumb;
1677 }
1678#endif
1679
1680 // When a change starts above w_topline and the end is below
1681 // w_topline, start redrawing at w_topline.
1682 // If the end of the change is above w_topline: do like no change was
1683 // made, but redraw the first line to find changes in syntax.
1684 if (mod_top != 0 && mod_top < wp->w_topline)
1685 {
1686 if (mod_bot > wp->w_topline)
1687 mod_top = wp->w_topline;
1688#ifdef FEAT_SYN_HL
1689 else if (syntax_present(wp))
1690 top_end = 1;
1691#endif
1692 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001693 }
1694 wp->w_redraw_top = 0; // reset for next time
1695 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001696#ifdef FEAT_SEARCH_EXTRA
1697 search_hl_has_cursor_lnum = 0;
1698#endif
1699
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001700
1701 // When only displaying the lines at the top, set top_end. Used when
1702 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001703 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001704 {
1705 j = 0;
1706 for (i = 0; i < wp->w_lines_valid; ++i)
1707 {
1708 j += wp->w_lines[i].wl_size;
1709 if (j >= wp->w_upd_rows)
1710 {
1711 top_end = j;
1712 break;
1713 }
1714 }
1715 if (top_end == 0)
1716 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001717 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001718 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001719 // top area defined, the rest is UPD_VALID
1720 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001721 }
1722
1723 // Trick: we want to avoid clearing the screen twice. screenclear() will
1724 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1725 // non-zero and thus not FALSE) will indicate that screenclear() was not
1726 // called.
1727 if (screen_cleared)
1728 screen_cleared = MAYBE;
1729
1730 // If there are no changes on the screen that require a complete redraw,
1731 // handle three cases:
1732 // 1: we are off the top of the screen by a few lines: scroll down
1733 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1734 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1735 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001736 if ((type == UPD_VALID || type == UPD_SOME_VALID
1737 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001738#ifdef FEAT_DIFF
1739 && !wp->w_botfill && !wp->w_old_botfill
1740#endif
1741 )
1742 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001743 if (mod_top != 0
1744 && wp->w_topline == mod_top
1745 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001746 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001747 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001748 // w_topline is the first changed line and window is not scrolled,
1749 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001750 }
1751 else if (wp->w_lines[0].wl_valid
1752 && (wp->w_topline < wp->w_lines[0].wl_lnum
1753#ifdef FEAT_DIFF
1754 || (wp->w_topline == wp->w_lines[0].wl_lnum
1755 && wp->w_topfill > wp->w_old_topfill)
1756#endif
1757 ))
1758 {
1759 // New topline is above old topline: May scroll down.
1760#ifdef FEAT_FOLDING
1761 if (hasAnyFolding(wp))
1762 {
1763 linenr_T ln;
1764
1765 // count the number of lines we are off, counting a sequence
1766 // of folded lines as one
1767 j = 0;
1768 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1769 {
1770 ++j;
1771 if (j >= wp->w_height - 2)
1772 break;
1773 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1774 }
1775 }
1776 else
1777#endif
1778 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1779 if (j < wp->w_height - 2) // not too far off
1780 {
zeertzjqbfe377b2023-08-17 22:58:53 +02001781 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1,
Luuk van Baal32d701f2024-04-28 16:24:02 +02001782 wp->w_height);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001783#ifdef FEAT_DIFF
1784 // insert extra lines for previously invisible filler lines
1785 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1786 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1787 - wp->w_old_topfill;
1788#endif
1789 if (i < wp->w_height - 2) // less than a screen off
1790 {
1791 // Try to insert the correct number of lines.
1792 // If not the last window, delete the lines at the bottom.
1793 // win_ins_lines may fail when the terminal can't do it.
1794 if (i > 0)
1795 check_for_delay(FALSE);
1796 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1797 {
1798 if (wp->w_lines_valid != 0)
1799 {
1800 // Need to update rows that are new, stop at the
1801 // first one that scrolled down.
1802 top_end = i;
1803 scrolled_down = TRUE;
1804
1805 // Move the entries that were scrolled, disable
1806 // the entries for the lines to be redrawn.
1807 if ((wp->w_lines_valid += j) > wp->w_height)
1808 wp->w_lines_valid = wp->w_height;
Bram Moolenaara2a89732022-08-31 14:46:18 +01001809 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001810 wp->w_lines[idx] = wp->w_lines[idx - j];
1811 while (idx >= 0)
1812 wp->w_lines[idx--].wl_valid = FALSE;
1813 }
1814 }
1815 else
1816 mid_start = 0; // redraw all lines
1817 }
1818 else
1819 mid_start = 0; // redraw all lines
1820 }
1821 else
1822 mid_start = 0; // redraw all lines
1823 }
1824 else
1825 {
1826 // New topline is at or below old topline: May scroll up.
1827 // When topline didn't change, find first entry in w_lines[] that
1828 // needs updating.
1829
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001830 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1831 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001832 j = -1;
1833 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001834 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001835 {
1836 if (wp->w_lines[i].wl_valid
1837 && wp->w_lines[i].wl_lnum == wp->w_topline)
1838 {
1839 j = i;
1840 break;
1841 }
1842 row += wp->w_lines[i].wl_size;
1843 }
1844 if (j == -1)
1845 {
1846 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1847 // lines
1848 mid_start = 0;
1849 }
1850 else
1851 {
1852 // Try to delete the correct number of lines.
1853 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1854#ifdef FEAT_DIFF
1855 // If the topline didn't change, delete old filler lines,
1856 // otherwise delete filler lines of the new topline...
1857 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1858 row += wp->w_old_topfill;
1859 else
1860 row += diff_check_fill(wp, wp->w_topline);
1861 // ... but don't delete new filler lines.
1862 row -= wp->w_topfill;
1863#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001864 if (row > Rows) // just in case
1865 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001866 if (row > 0)
1867 {
1868 check_for_delay(FALSE);
1869 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1870 == OK)
1871 bot_start = wp->w_height - row;
1872 else
1873 mid_start = 0; // redraw all lines
1874 }
1875 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1876 {
1877 // Skip the lines (below the deleted lines) that are still
1878 // valid and don't need redrawing. Copy their info
1879 // upwards, to compensate for the deleted lines. Set
1880 // bot_start to the first row that needs redrawing.
1881 bot_start = 0;
1882 idx = 0;
1883 for (;;)
1884 {
1885 wp->w_lines[idx] = wp->w_lines[j];
1886 // stop at line that didn't fit, unless it is still
1887 // valid (no lines deleted)
1888 if (row > 0 && bot_start + row
1889 + (int)wp->w_lines[j].wl_size > wp->w_height)
1890 {
1891 wp->w_lines_valid = idx + 1;
1892 break;
1893 }
1894 bot_start += wp->w_lines[idx++].wl_size;
1895
1896 // stop at the last valid entry in w_lines[].wl_size
1897 if (++j >= wp->w_lines_valid)
1898 {
1899 wp->w_lines_valid = idx;
1900 break;
1901 }
1902 }
1903#ifdef FEAT_DIFF
1904 // Correct the first entry for filler lines at the top
1905 // when it won't get updated below.
1906 if (wp->w_p_diff && bot_start > 0)
zeertzjq47f85842024-10-01 19:35:47 +02001907 {
1908 int n = plines_win_nofill(wp, wp->w_topline, FALSE)
1909 + wp->w_topfill - adjust_plines_for_skipcol(wp);
1910 if (n > wp->w_height)
1911 n = wp->w_height;
1912 wp->w_lines[0].wl_size = n;
1913 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001914#endif
1915 }
1916 }
1917 }
1918
Bram Moolenaar13608d82022-08-29 15:06:50 +01001919 // When starting redraw in the first line, redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001920 if (mid_start == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001921 mid_end = wp->w_height;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001922
1923 // When win_del_lines() or win_ins_lines() caused the screen to be
1924 // cleared (only happens for the first window) or when screenclear()
1925 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001926 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001927 if (screen_cleared == TRUE)
1928 must_redraw = 0;
1929 }
1930 else
1931 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001932 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001933 mid_start = 0;
1934 mid_end = wp->w_height;
1935 }
1936
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001937 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001938 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001939 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001940 mid_start = 0;
1941 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001942 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001943 }
1944
1945 // check if we are updating or removing the inverted part
1946 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001947 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001948 {
1949 linenr_T from, to;
1950
1951 if (VIsual_active)
1952 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001953 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001954 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001955 {
1956 // If the type of Visual selection changed, redraw the whole
1957 // selection. Also when the ownership of the X selection is
1958 // gained or lost.
1959 if (curwin->w_cursor.lnum < VIsual.lnum)
1960 {
1961 from = curwin->w_cursor.lnum;
1962 to = VIsual.lnum;
1963 }
1964 else
1965 {
1966 from = VIsual.lnum;
1967 to = curwin->w_cursor.lnum;
1968 }
1969 // redraw more when the cursor moved as well
1970 if (wp->w_old_cursor_lnum < from)
1971 from = wp->w_old_cursor_lnum;
1972 if (wp->w_old_cursor_lnum > to)
1973 to = wp->w_old_cursor_lnum;
1974 if (wp->w_old_visual_lnum < from)
1975 from = wp->w_old_visual_lnum;
1976 if (wp->w_old_visual_lnum > to)
1977 to = wp->w_old_visual_lnum;
1978 }
1979 else
1980 {
1981 // Find the line numbers that need to be updated: The lines
1982 // between the old cursor position and the current cursor
1983 // position. Also check if the Visual position changed.
1984 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1985 {
1986 from = curwin->w_cursor.lnum;
1987 to = wp->w_old_cursor_lnum;
1988 }
1989 else
1990 {
1991 from = wp->w_old_cursor_lnum;
1992 to = curwin->w_cursor.lnum;
1993 if (from == 0) // Visual mode just started
1994 from = to;
1995 }
1996
1997 if (VIsual.lnum != wp->w_old_visual_lnum
1998 || VIsual.col != wp->w_old_visual_col)
1999 {
2000 if (wp->w_old_visual_lnum < from
2001 && wp->w_old_visual_lnum != 0)
2002 from = wp->w_old_visual_lnum;
2003 if (wp->w_old_visual_lnum > to)
2004 to = wp->w_old_visual_lnum;
2005 if (VIsual.lnum < from)
2006 from = VIsual.lnum;
2007 if (VIsual.lnum > to)
2008 to = VIsual.lnum;
2009 }
2010 }
2011
2012 // If in block mode and changed column or curwin->w_curswant:
2013 // update all lines.
2014 // First compute the actual start and end column.
2015 if (VIsual_mode == Ctrl_V)
2016 {
2017 colnr_T fromc, toc;
2018#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002019 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002020
2021 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002022 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002023#endif
2024 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002025 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002026#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002027 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002028#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002029 // Highlight to the end of the line, unless 'virtualedit' has
2030 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002031 if (curwin->w_curswant == MAXCOL)
2032 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002033 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002034 {
2035 pos_T pos;
2036 int cursor_above =
2037 curwin->w_cursor.lnum < VIsual.lnum;
2038
2039 // Need to find the longest line.
2040 toc = 0;
2041 pos.coladd = 0;
2042 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2043 ? pos.lnum <= VIsual.lnum
2044 : pos.lnum >= VIsual.lnum;
2045 pos.lnum += cursor_above ? 1 : -1)
2046 {
2047 colnr_T t;
2048
John Marriotta21240b2025-01-08 20:10:59 +01002049 pos.col = (int)ml_get_buf_len(wp->w_buffer, pos.lnum);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002050 getvvcol(wp, &pos, NULL, NULL, &t);
2051 if (toc < t)
2052 toc = t;
2053 }
2054 ++toc;
2055 }
2056 else
2057 toc = MAXCOL;
2058 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002059
2060 if (fromc != wp->w_old_cursor_fcol
2061 || toc != wp->w_old_cursor_lcol)
2062 {
2063 if (from > VIsual.lnum)
2064 from = VIsual.lnum;
2065 if (to < VIsual.lnum)
2066 to = VIsual.lnum;
2067 }
2068 wp->w_old_cursor_fcol = fromc;
2069 wp->w_old_cursor_lcol = toc;
2070 }
2071 }
2072 else
2073 {
2074 // Use the line numbers of the old Visual area.
2075 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2076 {
2077 from = wp->w_old_cursor_lnum;
2078 to = wp->w_old_visual_lnum;
2079 }
2080 else
2081 {
2082 from = wp->w_old_visual_lnum;
2083 to = wp->w_old_cursor_lnum;
2084 }
2085 }
2086
2087 // There is no need to update lines above the top of the window.
2088 if (from < wp->w_topline)
2089 from = wp->w_topline;
2090
2091 // If we know the value of w_botline, use it to restrict the update to
2092 // the lines that are visible in the window.
2093 if (wp->w_valid & VALID_BOTLINE)
2094 {
2095 if (from >= wp->w_botline)
2096 from = wp->w_botline - 1;
2097 if (to >= wp->w_botline)
2098 to = wp->w_botline - 1;
2099 }
2100
2101 // Find the minimal part to be updated.
2102 // Watch out for scrolling that made entries in w_lines[] invalid.
2103 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2104 // top_end; need to redraw from top_end to the "to" line.
2105 // A middle mouse click with a Visual selection may change the text
2106 // above the Visual area and reset wl_valid, do count these for
2107 // mid_end (in srow).
2108 if (mid_start > 0)
2109 {
2110 lnum = wp->w_topline;
2111 idx = 0;
2112 srow = 0;
2113 if (scrolled_down)
2114 mid_start = top_end;
2115 else
2116 mid_start = 0;
2117 while (lnum < from && idx < wp->w_lines_valid) // find start
2118 {
2119 if (wp->w_lines[idx].wl_valid)
2120 mid_start += wp->w_lines[idx].wl_size;
2121 else if (!scrolled_down)
2122 srow += wp->w_lines[idx].wl_size;
2123 ++idx;
2124# ifdef FEAT_FOLDING
2125 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2126 lnum = wp->w_lines[idx].wl_lnum;
2127 else
2128# endif
2129 ++lnum;
2130 }
2131 srow += mid_start;
2132 mid_end = wp->w_height;
2133 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2134 {
2135 if (wp->w_lines[idx].wl_valid
2136 && wp->w_lines[idx].wl_lnum >= to + 1)
2137 {
2138 // Only update until first row of this line
2139 mid_end = srow;
2140 break;
2141 }
2142 srow += wp->w_lines[idx].wl_size;
2143 }
2144 }
2145 }
2146
2147 if (VIsual_active && buf == curwin->w_buffer)
2148 {
2149 wp->w_old_visual_mode = VIsual_mode;
2150 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2151 wp->w_old_visual_lnum = VIsual.lnum;
2152 wp->w_old_visual_col = VIsual.col;
2153 wp->w_old_curswant = curwin->w_curswant;
2154 }
2155 else
2156 {
2157 wp->w_old_visual_mode = 0;
2158 wp->w_old_cursor_lnum = 0;
2159 wp->w_old_visual_lnum = 0;
2160 wp->w_old_visual_col = 0;
2161 }
2162
2163#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2164 // reset got_int, otherwise regexp won't work
2165 save_got_int = got_int;
2166 got_int = 0;
2167#endif
2168#ifdef SYN_TIME_LIMIT
2169 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002170 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002171 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002172#endif
2173#ifdef FEAT_FOLDING
2174 win_foldinfo.fi_level = 0;
2175#endif
2176
2177#ifdef FEAT_MENU
2178 // Draw the window toolbar, if there is one.
2179 // TODO: only when needed.
2180 if (winbar_height(wp) > 0)
2181 redraw_win_toolbar(wp);
2182#endif
2183
Luuk van Baal30805a12023-05-27 22:22:10 +01002184 lnum = wp->w_topline; // first line shown in window
2185
2186 spellvars_T spv;
2187#ifdef FEAT_SPELL
2188 // Initialize spell related variables for the first drawn line.
2189 CLEAR_FIELD(spv);
Luuk van Baale84c7732023-05-31 18:57:36 +01002190 if (spell_check_window(wp))
Luuk van Baal30805a12023-05-27 22:22:10 +01002191 {
Luuk van Baale84c7732023-05-31 18:57:36 +01002192 spv.spv_has_spell = TRUE;
Luuk van Baal30805a12023-05-27 22:22:10 +01002193 spv.spv_unchanged = mod_top == 0;
Luuk van Baal30805a12023-05-27 22:22:10 +01002194 }
2195#endif
2196
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002197 // Update all the window rows.
2198 idx = 0; // first entry in w_lines[].wl_size
2199 row = 0;
2200 srow = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002201 for (;;)
2202 {
2203 // stop updating when reached the end of the window (check for _past_
2204 // the end of the window is at the end of the loop)
2205 if (row == wp->w_height)
2206 {
2207 didline = TRUE;
2208 break;
2209 }
2210
2211 // stop updating when hit the end of the file
2212 if (lnum > buf->b_ml.ml_line_count)
2213 {
2214 eof = TRUE;
2215 break;
2216 }
2217
2218 // Remember the starting row of the line that is going to be dealt
2219 // with. It is used further down when the line doesn't fit.
2220 srow = row;
2221
2222 // Update a line when it is in an area that needs updating, when it
2223 // has changes or w_lines[idx] is invalid.
2224 // "bot_start" may be halfway a wrapped line after using
2225 // win_del_lines(), check if the current line includes it.
2226 // When syntax folding is being used, the saved syntax states will
2227 // already have been updated, we can't see where the syntax state is
2228 // the same again, just update until the end of the window.
2229 if (row < top_end
2230 || (row >= mid_start && row < mid_end)
2231#ifdef FEAT_SEARCH_EXTRA
2232 || top_to_mod
2233#endif
2234 || idx >= wp->w_lines_valid
2235 || (row + wp->w_lines[idx].wl_size > bot_start)
2236 || (mod_top != 0
2237 && (lnum == mod_top
2238 || (lnum >= mod_top
2239 && (lnum < mod_bot
2240#ifdef FEAT_SYN_HL
2241 || did_update == DID_FOLD
2242 || (did_update == DID_LINE
2243 && syntax_present(wp)
2244 && (
2245# ifdef FEAT_FOLDING
2246 (foldmethodIsSyntax(wp)
2247 && hasAnyFolding(wp)) ||
2248# endif
2249 syntax_check_changed(lnum)))
2250#endif
2251#ifdef FEAT_SEARCH_EXTRA
2252 // match in fixed position might need redraw
2253 // if lines were inserted or deleted
2254 || (wp->w_match_head != NULL
zeertzjq9f25a3a2024-11-26 15:08:02 +01002255 && buf->b_mod_set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002256 && buf->b_mod_xlines != 0)
2257#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002258 ))))
2259#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002260 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2261 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002262#endif
2263 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002264 {
2265#ifdef FEAT_SEARCH_EXTRA
2266 if (lnum == mod_top)
2267 top_to_mod = FALSE;
2268#endif
2269
2270 // When at start of changed lines: May scroll following lines
2271 // up or down to minimize redrawing.
2272 // Don't do this when the change continues until the end.
2273 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002274 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002275 if (lnum == mod_top
2276 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002277 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2278 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002279 {
2280 int old_rows = 0;
2281 int new_rows = 0;
2282 int xtra_rows;
2283 linenr_T l;
2284
2285 // Count the old number of window rows, using w_lines[], which
2286 // should still contain the sizes for the lines as they are
2287 // currently displayed.
2288 for (i = idx; i < wp->w_lines_valid; ++i)
2289 {
2290 // Only valid lines have a meaningful wl_lnum. Invalid
2291 // lines are part of the changed area.
2292 if (wp->w_lines[i].wl_valid
2293 && wp->w_lines[i].wl_lnum == mod_bot)
2294 break;
2295 old_rows += wp->w_lines[i].wl_size;
2296#ifdef FEAT_FOLDING
2297 if (wp->w_lines[i].wl_valid
2298 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2299 {
2300 // Must have found the last valid entry above mod_bot.
2301 // Add following invalid entries.
2302 ++i;
2303 while (i < wp->w_lines_valid
2304 && !wp->w_lines[i].wl_valid)
2305 old_rows += wp->w_lines[i++].wl_size;
2306 break;
2307 }
2308#endif
2309 }
2310
2311 if (i >= wp->w_lines_valid)
2312 {
2313 // We can't find a valid line below the changed lines,
2314 // need to redraw until the end of the window.
2315 // Inserting/deleting lines has no use.
2316 bot_start = 0;
2317 }
2318 else
2319 {
2320 // Able to count old number of rows: Count new window
2321 // rows, and may insert/delete lines
2322 j = idx;
2323 for (l = lnum; l < mod_bot; ++l)
2324 {
2325#ifdef FEAT_FOLDING
2326 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2327 ++new_rows;
2328 else
2329#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002330 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002331#ifdef FEAT_DIFF
2332 if (l == wp->w_topline)
Luuk van Baalc8502f92023-05-06 12:40:15 +01002333 {
2334 int n = plines_win_nofill(wp, l, FALSE)
2335 + wp->w_topfill;
zeertzjq6235a102023-08-19 14:12:42 +02002336 n -= adjust_plines_for_skipcol(wp);
Luuk van Baalc8502f92023-05-06 12:40:15 +01002337 if (n > wp->w_height)
2338 n = wp->w_height;
2339 new_rows += n;
2340 }
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002341 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002342#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002343 new_rows += plines_win(wp, l, TRUE);
2344 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002345 ++j;
2346 if (new_rows > wp->w_height - row - 2)
2347 {
2348 // it's getting too much, must redraw the rest
2349 new_rows = 9999;
2350 break;
2351 }
2352 }
2353 xtra_rows = new_rows - old_rows;
2354 if (xtra_rows < 0)
2355 {
2356 // May scroll text up. If there is not enough
2357 // remaining text or scrolling fails, must redraw the
2358 // rest. If scrolling works, must redraw the text
2359 // below the scrolled text.
2360 if (row - xtra_rows >= wp->w_height - 2)
2361 mod_bot = MAXLNUM;
2362 else
2363 {
2364 check_for_delay(FALSE);
2365 if (win_del_lines(wp, row,
2366 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2367 mod_bot = MAXLNUM;
2368 else
2369 bot_start = wp->w_height + xtra_rows;
2370 }
2371 }
2372 else if (xtra_rows > 0)
2373 {
2374 // May scroll text down. If there is not enough
2375 // remaining text of scrolling fails, must redraw the
2376 // rest.
2377 if (row + xtra_rows >= wp->w_height - 2)
2378 mod_bot = MAXLNUM;
2379 else
2380 {
2381 check_for_delay(FALSE);
2382 if (win_ins_lines(wp, row + old_rows,
2383 xtra_rows, FALSE, FALSE) == FAIL)
2384 mod_bot = MAXLNUM;
2385 else if (top_end > row + old_rows)
2386 // Scrolled the part at the top that requires
2387 // updating down.
2388 top_end += xtra_rows;
2389 }
2390 }
2391
2392 // When not updating the rest, may need to move w_lines[]
2393 // entries.
2394 if (mod_bot != MAXLNUM && i != j)
2395 {
2396 if (j < i)
2397 {
2398 int x = row + new_rows;
2399
2400 // move entries in w_lines[] upwards
2401 for (;;)
2402 {
2403 // stop at last valid entry in w_lines[]
2404 if (i >= wp->w_lines_valid)
2405 {
2406 wp->w_lines_valid = j;
2407 break;
2408 }
2409 wp->w_lines[j] = wp->w_lines[i];
2410 // stop at a line that won't fit
2411 if (x + (int)wp->w_lines[j].wl_size
2412 > wp->w_height)
2413 {
2414 wp->w_lines_valid = j + 1;
2415 break;
2416 }
2417 x += wp->w_lines[j++].wl_size;
2418 ++i;
2419 }
2420 if (bot_start > x)
2421 bot_start = x;
2422 }
2423 else // j > i
2424 {
2425 // move entries in w_lines[] downwards
2426 j -= i;
2427 wp->w_lines_valid += j;
2428 if (wp->w_lines_valid > wp->w_height)
2429 wp->w_lines_valid = wp->w_height;
2430 for (i = wp->w_lines_valid; i - j >= idx; --i)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002431 wp->w_lines[i] = wp->w_lines[i - j];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002432
2433 // The w_lines[] entries for inserted lines are
2434 // now invalid, but wl_size may be used above.
2435 // Reset to zero.
2436 while (i >= idx)
2437 {
2438 wp->w_lines[i].wl_size = 0;
2439 wp->w_lines[i--].wl_valid = FALSE;
2440 }
2441 }
2442 }
2443 }
2444 }
2445
2446#ifdef FEAT_FOLDING
2447 // When lines are folded, display one line for all of them.
2448 // Otherwise, display normally (can be several display lines when
2449 // 'wrap' is on).
2450 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2451 if (fold_count != 0)
2452 {
2453 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2454 ++row;
2455 --fold_count;
2456 wp->w_lines[idx].wl_folded = TRUE;
Luuk van Baale84c7732023-05-31 18:57:36 +01002457 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002458# ifdef FEAT_SYN_HL
2459 did_update = DID_FOLD;
2460# endif
Luuk van Baal30805a12023-05-27 22:22:10 +01002461# ifdef FEAT_SPELL
Luuk van Baale84c7732023-05-31 18:57:36 +01002462 spv.spv_capcol_lnum = 0;
Luuk van Baal30805a12023-05-27 22:22:10 +01002463# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002464 }
2465 else
2466#endif
2467 if (idx < wp->w_lines_valid
2468 && wp->w_lines[idx].wl_valid
2469 && wp->w_lines[idx].wl_lnum == lnum
2470 && lnum > wp->w_topline
2471 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2472 && !WIN_IS_POPUP(wp)
2473 && srow + wp->w_lines[idx].wl_size > wp->w_height
2474#ifdef FEAT_DIFF
2475 && diff_check_fill(wp, lnum) == 0
2476#endif
2477 )
2478 {
2479 // This line is not going to fit. Don't draw anything here,
2480 // will draw "@ " lines below.
2481 row = wp->w_height + 1;
2482 }
2483 else
2484 {
2485#ifdef FEAT_SEARCH_EXTRA
2486 prepare_search_hl(wp, &screen_search_hl, lnum);
2487#endif
2488#ifdef FEAT_SYN_HL
2489 // Let the syntax stuff know we skipped a few lines.
2490 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2491 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002492 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002493#endif
2494
2495 // Display one line.
zeertzjqebfd8562024-02-06 10:59:03 +01002496 row = win_line(wp, lnum, srow, wp->w_height, 0, &spv);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002497
2498#ifdef FEAT_FOLDING
2499 wp->w_lines[idx].wl_folded = FALSE;
2500 wp->w_lines[idx].wl_lastlnum = lnum;
2501#endif
2502#ifdef FEAT_SYN_HL
2503 did_update = DID_LINE;
2504 syntax_last_parsed = lnum;
2505#endif
2506 }
2507
2508 wp->w_lines[idx].wl_lnum = lnum;
2509 wp->w_lines[idx].wl_valid = TRUE;
2510
2511 // Past end of the window or end of the screen. Note that after
2512 // resizing wp->w_height may be end up too big. That's a problem
2513 // elsewhere, but prevent a crash here.
Bram Moolenaara2a89732022-08-31 14:46:18 +01002514 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002515 {
2516 // we may need the size of that too long line later on
2517 if (dollar_vcol == -1)
2518 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2519 ++idx;
2520 break;
2521 }
2522 if (dollar_vcol == -1)
2523 wp->w_lines[idx].wl_size = row - srow;
2524 ++idx;
2525#ifdef FEAT_FOLDING
2526 lnum += fold_count + 1;
2527#else
2528 ++lnum;
2529#endif
2530 }
2531 else
2532 {
zeertzjqae07ebc2024-02-08 11:37:40 +01002533 // If:
2534 // - 'number' is set and below inserted/deleted lines, or
2535 // - 'relativenumber' is set and cursor moved vertically,
2536 // the text doesn't need to be redrawn, but the number column does.
zeertzjq9f25a3a2024-11-26 15:08:02 +01002537 if ((wp->w_p_nu && mod_top != 0 && lnum >= mod_bot
2538 && buf->b_mod_set && buf->b_mod_xlines != 0)
zeertzjqae07ebc2024-02-08 11:37:40 +01002539 || (wp->w_p_rnu
2540 && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002541 {
2542#ifdef FEAT_FOLDING
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002543 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2544 if (fold_count != 0)
2545 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2546 else
2547#endif
zeertzjqebfd8562024-02-06 10:59:03 +01002548 (void)win_line(wp, lnum, srow, wp->w_height,
2549 wp->w_lines[idx].wl_size, &spv);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002550 }
2551
2552 // This line does not need to be drawn, advance to the next one.
2553 row += wp->w_lines[idx++].wl_size;
2554 if (row > wp->w_height) // past end of screen
2555 break;
2556#ifdef FEAT_FOLDING
2557 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2558#else
2559 ++lnum;
2560#endif
2561#ifdef FEAT_SYN_HL
2562 did_update = DID_NONE;
2563#endif
Luuk van Baale84c7732023-05-31 18:57:36 +01002564#ifdef FEAT_SPELL
2565 spv.spv_capcol_lnum = 0;
2566#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002567 }
2568
2569 if (lnum > buf->b_ml.ml_line_count)
2570 {
2571 eof = TRUE;
2572 break;
2573 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002574
2575 // Safety check: if any of the wl_size values is wrong we might go over
2576 // the end of w_lines[].
Bram Moolenaara2a89732022-08-31 14:46:18 +01002577 if (idx >= Rows)
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002578 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002579 }
2580
2581 // End of loop over all window lines.
2582
zeertzjqc20e46a2022-03-23 14:55:23 +00002583#ifdef FEAT_SYN_HL
2584 // Now that the window has been redrawn with the old and new cursor line,
2585 // update w_last_cursorline.
2586 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2587#endif
Lewis Russell16246392022-03-29 11:38:17 +01002588 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002589
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002590#ifdef FEAT_VTP
2591 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002592 // See the version that was fixed.
2593 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002594 {
K.Takata54119102022-02-03 13:33:03 +00002595 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002596
K.Takata54119102022-02-03 13:33:03 +00002597 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002598 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002599 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2600 LineOffset[k] + screen_Columns) > 1)
2601 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002602 else
K.Takata54119102022-02-03 13:33:03 +00002603 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002604 else
K.Takata54119102022-02-03 13:33:03 +00002605 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002606 }
2607#endif
2608
2609 if (idx > wp->w_lines_valid)
2610 wp->w_lines_valid = idx;
2611
2612#ifdef FEAT_SYN_HL
2613 // Let the syntax stuff know we stop parsing here.
2614 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002615 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002616#endif
2617
2618 // If we didn't hit the end of the file, and we didn't finish the last
2619 // line we were working on, then the line didn't fit.
2620 wp->w_empty_rows = 0;
2621#ifdef FEAT_DIFF
2622 wp->w_filler_rows = 0;
2623#endif
2624 if (!eof && !didline)
2625 {
2626 if (lnum == wp->w_topline)
2627 {
2628 // Single line that does not fit!
2629 // Don't overwrite it, it can be edited.
2630 wp->w_botline = lnum + 1;
2631 }
2632#ifdef FEAT_DIFF
2633 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2634 {
2635 // Window ends in filler lines.
2636 wp->w_botline = lnum;
2637 wp->w_filler_rows = wp->w_height - srow;
2638 }
2639#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002640#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002641 else if (WIN_IS_POPUP(wp))
2642 {
2643 // popup line that doesn't fit is left as-is
2644 wp->w_botline = lnum;
2645 }
2646#endif
2647 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2648 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002649 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2650 int symbol = wp->w_fill_chars.lastline;
zeertzjq18b35002022-10-04 20:35:37 +01002651 int charlen;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002652 char_u fillbuf[12]; // 2 characters of 6 bytes
2653
zeertzjq18b35002022-10-04 20:35:37 +01002654 charlen = mb_char2bytes(symbol, &fillbuf[0]);
2655 mb_char2bytes(symbol, &fillbuf[charlen]);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002656
2657 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002658 screen_puts_len(fillbuf,
zeertzjq18b35002022-10-04 20:35:37 +01002659 (wp->w_width > 2 ? 2 : wp->w_width) * charlen,
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002660 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002661 screen_fill(scr_row, scr_row + 1,
2662 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002663 symbol, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002664 set_empty_rows(wp, srow);
2665 wp->w_botline = lnum;
2666 }
2667 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2668 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002669 int start_col = (int)W_ENDCOL(wp) - 3;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002670 int symbol = wp->w_fill_chars.lastline;
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002671
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002672 // Last line isn't finished: Display "@@@" at the end.
2673 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2674 W_WINROW(wp) + wp->w_height,
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002675 start_col < wp->w_wincol ? wp->w_wincol : start_col,
2676 (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002677 symbol, symbol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002678 set_empty_rows(wp, srow);
2679 wp->w_botline = lnum;
2680 }
2681 else
2682 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002683 win_draw_end(wp, wp->w_fill_chars.lastline, ' ', TRUE,
2684 srow, wp->w_height, HLF_AT);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002685 wp->w_botline = lnum;
2686 }
2687 }
2688 else
2689 {
2690 draw_vsep_win(wp, row);
2691 if (eof) // we hit the end of the file
2692 {
2693 wp->w_botline = buf->b_ml.ml_line_count + 1;
2694#ifdef FEAT_DIFF
2695 j = diff_check_fill(wp, wp->w_botline);
2696 if (j > 0 && !wp->w_botfill)
2697 {
2698 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002699 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002700 i = '-';
2701 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002702 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002703 if (row + j > wp->w_height)
2704 j = wp->w_height - row;
2705 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2706 row += j;
2707 }
2708#endif
2709 }
2710 else if (dollar_vcol == -1)
2711 wp->w_botline = lnum;
2712
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002713 // Make sure the rest of the screen is blank.
2714 // write the "eob" character from 'fillchars' to rows that aren't part
2715 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002716 if (WIN_IS_POPUP(wp))
2717 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2718 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002719 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2720 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002721 }
2722
2723#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002724 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002725 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002726#endif
2727
2728 // Reset the type of redrawing required, the window has been updated.
2729 wp->w_redr_type = 0;
2730#ifdef FEAT_DIFF
2731 wp->w_old_topfill = wp->w_topfill;
2732 wp->w_old_botfill = wp->w_botfill;
2733#endif
2734
2735 if (dollar_vcol == -1)
2736 {
2737 // There is a trick with w_botline. If we invalidate it on each
2738 // change that might modify it, this will cause a lot of expensive
2739 // calls to plines() in update_topline() each time. Therefore the
2740 // value of w_botline is often approximated, and this value is used to
2741 // compute the value of w_topline. If the value of w_botline was
2742 // wrong, check that the value of w_topline is correct (cursor is on
2743 // the visible part of the text). If it's not, we need to redraw
2744 // again. Mostly this just means scrolling up a few lines, so it
2745 // doesn't look too bad. Only do this for the current window (where
2746 // changes are relevant).
2747 wp->w_valid |= VALID_BOTLINE;
2748 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2749 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002750 win_T *wwp;
2751#if defined(FEAT_CONCEAL)
2752 linenr_T old_topline = wp->w_topline;
2753 int new_wcol = wp->w_wcol;
2754#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002755 recursive = TRUE;
2756 curwin->w_valid &= ~VALID_TOPLINE;
2757 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002758
2759#if defined(FEAT_CONCEAL)
2760 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2761 != (VALID_WCOL|VALID_WROW))
2762 {
2763 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002764 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002765 // update_topline() the cursor's row or column got invalidated.
2766 // If they are left invalid, setcursor() will recompute them
2767 // but there won't be any further win_line() call to re-fix the
2768 // column and the cursor will end up misplaced. So we call
2769 // cursor validation now and reapply the fix again (or call
2770 // win_line() to do it for us).
2771 validate_cursor();
2772 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2773 && old_topline == wp->w_topline)
2774 wp->w_wcol = new_wcol;
2775 else
2776 redrawWinline(wp, wp->w_cursor.lnum);
2777 }
2778#endif
Luuk van Baal3d5065f2024-09-01 10:33:56 +02002779 // New redraw either due to updated topline, wcol fix or reset skipcol.
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002780 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002781 {
2782 // Don't update for changes in buffer again.
2783 i = curbuf->b_mod_set;
2784 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002785 j = curbuf->b_mod_xlines;
2786 curbuf->b_mod_xlines = 0;
Luuk van Baal3d5065f2024-09-01 10:33:56 +02002787 curs_columns(TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002788 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002789 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002790 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002791 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002792 // Other windows might have w_redr_type raised in update_topline().
2793 must_redraw = 0;
2794 FOR_ALL_WINDOWS(wwp)
2795 if (wwp->w_redr_type > must_redraw)
2796 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002797 recursive = FALSE;
2798 }
2799 }
2800
2801#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2802 // restore got_int, unless CTRL-C was hit while redrawing
2803 if (!got_int)
2804 got_int = save_got_int;
2805#endif
2806}
2807
2808#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2809/*
2810 * Prepare for updating one or more windows.
2811 * Caller must check for "updating_screen" already set to avoid recursiveness.
2812 */
2813 static void
2814update_prepare(void)
2815{
2816 cursor_off();
2817 updating_screen = TRUE;
2818#ifdef FEAT_GUI
2819 // Remove the cursor before starting to do anything, because scrolling may
2820 // make it difficult to redraw the text under it.
2821 if (gui.in_use)
2822 gui_undraw_cursor();
2823#endif
2824#ifdef FEAT_SEARCH_EXTRA
2825 start_search_hl();
2826#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002827#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002828 // Update popup_mask if needed.
2829 may_update_popup_mask(must_redraw);
2830#endif
2831}
2832
2833/*
2834 * Finish updating one or more windows.
2835 */
2836 static void
2837update_finish(void)
2838{
2839 if (redraw_cmdline || redraw_mode)
2840 showmode();
2841
2842# ifdef FEAT_SEARCH_EXTRA
2843 end_search_hl();
2844# endif
2845
2846 after_updating_screen(TRUE);
2847
2848# ifdef FEAT_GUI
2849 // Redraw the cursor and update the scrollbars when all screen updating is
2850 // done.
2851 if (gui.in_use)
2852 {
2853 out_flush_cursor(FALSE, FALSE);
2854 gui_update_scrollbars(FALSE);
2855 }
2856# endif
2857}
2858#endif
2859
2860#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2861 void
2862update_debug_sign(buf_T *buf, linenr_T lnum)
2863{
2864 win_T *wp;
2865 int doit = FALSE;
2866
2867# ifdef FEAT_FOLDING
2868 win_foldinfo.fi_level = 0;
2869# endif
2870
2871 // update/delete a specific sign
2872 redraw_buf_line_later(buf, lnum);
2873
2874 // check if it resulted in the need to redraw a window
2875 FOR_ALL_WINDOWS(wp)
2876 if (wp->w_redr_type != 0)
2877 doit = TRUE;
2878
2879 // Return when there is nothing to do, screen updating is already
2880 // happening (recursive call), messages on the screen or still starting up.
2881 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002882 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002883 || msg_scrolled
2884#ifdef FEAT_GUI
2885 || gui.starting
2886#endif
2887 || starting)
2888 return;
2889
2890 // update all windows that need updating
2891 update_prepare();
2892
2893 FOR_ALL_WINDOWS(wp)
2894 {
2895 if (wp->w_redr_type != 0)
2896 win_update(wp);
2897 if (wp->w_redr_status)
2898 win_redr_status(wp, FALSE);
2899 }
2900
2901 update_finish();
2902}
2903#endif
2904
2905#if defined(FEAT_GUI) || defined(PROTO)
2906/*
2907 * Update a single window, its status line and maybe the command line msg.
2908 * Used for the GUI scrollbar.
2909 */
2910 void
2911updateWindow(win_T *wp)
2912{
2913 // return if already busy updating
2914 if (updating_screen)
2915 return;
2916
2917 update_prepare();
2918
2919#ifdef FEAT_CLIPBOARD
2920 // When Visual area changed, may have to update selection.
2921 if (clip_star.available && clip_isautosel_star())
2922 clip_update_selection(&clip_star);
2923 if (clip_plus.available && clip_isautosel_plus())
2924 clip_update_selection(&clip_plus);
2925#endif
2926
2927 win_update(wp);
2928
2929 // When the screen was cleared redraw the tab pages line.
2930 if (redraw_tabline)
2931 draw_tabline();
2932
Martin Tournoijba43e762022-10-13 22:12:15 +01002933 if (wp->w_redr_status || p_ru
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002934# ifdef FEAT_STL_OPT
2935 || *p_stl != NUL || *wp->w_p_stl != NUL
2936# endif
2937 )
2938 win_redr_status(wp, FALSE);
2939
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002940#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002941 // Display popup windows on top of everything.
2942 update_popups(win_update);
2943#endif
2944
2945 update_finish();
2946}
2947#endif
2948
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002949/*
2950 * Redraw as soon as possible. When the command line is not scrolled redraw
2951 * right away and restore what was on the command line.
2952 * Return a code indicating what happened.
2953 */
2954 int
2955redraw_asap(int type)
2956{
2957 int rows;
2958 int cols = screen_Columns;
2959 int r;
2960 int ret = 0;
2961 schar_T *screenline; // copy from ScreenLines[]
2962 sattr_T *screenattr; // copy from ScreenAttrs[]
2963 int i;
2964 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2965 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2966 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2967
2968 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002969 if (msg_scrolled
2970 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002971 || exiting)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002972 return ret;
2973
2974 // Allocate space to save the text displayed in the command line area.
2975 rows = screen_Rows - cmdline_row;
2976 screenline = LALLOC_MULT(schar_T, rows * cols);
2977 screenattr = LALLOC_MULT(sattr_T, rows * cols);
2978 if (screenline == NULL || screenattr == NULL)
2979 ret = 2;
2980 if (enc_utf8)
2981 {
2982 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
2983 if (screenlineUC == NULL)
2984 ret = 2;
2985 for (i = 0; i < p_mco; ++i)
2986 {
2987 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
2988 if (screenlineC[i] == NULL)
2989 ret = 2;
2990 }
2991 }
2992 if (enc_dbcs == DBCS_JPNU)
2993 {
2994 screenline2 = LALLOC_MULT(schar_T, rows * cols);
2995 if (screenline2 == NULL)
2996 ret = 2;
2997 }
2998
2999 if (ret != 2)
3000 {
3001 // Save the text displayed in the command line area.
3002 for (r = 0; r < rows; ++r)
3003 {
3004 mch_memmove(screenline + r * cols,
3005 ScreenLines + LineOffset[cmdline_row + r],
3006 (size_t)cols * sizeof(schar_T));
3007 mch_memmove(screenattr + r * cols,
3008 ScreenAttrs + LineOffset[cmdline_row + r],
3009 (size_t)cols * sizeof(sattr_T));
3010 if (enc_utf8)
3011 {
3012 mch_memmove(screenlineUC + r * cols,
3013 ScreenLinesUC + LineOffset[cmdline_row + r],
3014 (size_t)cols * sizeof(u8char_T));
3015 for (i = 0; i < p_mco; ++i)
3016 mch_memmove(screenlineC[i] + r * cols,
3017 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3018 (size_t)cols * sizeof(u8char_T));
3019 }
3020 if (enc_dbcs == DBCS_JPNU)
3021 mch_memmove(screenline2 + r * cols,
3022 ScreenLines2 + LineOffset[cmdline_row + r],
3023 (size_t)cols * sizeof(schar_T));
3024 }
3025
3026 update_screen(0);
3027 ret = 3;
3028
3029 if (must_redraw == 0)
3030 {
3031 int off = (int)(current_ScreenLine - ScreenLines);
3032
3033 // Restore the text displayed in the command line area.
3034 for (r = 0; r < rows; ++r)
3035 {
3036 mch_memmove(current_ScreenLine,
3037 screenline + r * cols,
3038 (size_t)cols * sizeof(schar_T));
3039 mch_memmove(ScreenAttrs + off,
3040 screenattr + r * cols,
3041 (size_t)cols * sizeof(sattr_T));
3042 if (enc_utf8)
3043 {
3044 mch_memmove(ScreenLinesUC + off,
3045 screenlineUC + r * cols,
3046 (size_t)cols * sizeof(u8char_T));
3047 for (i = 0; i < p_mco; ++i)
3048 mch_memmove(ScreenLinesC[i] + off,
3049 screenlineC[i] + r * cols,
3050 (size_t)cols * sizeof(u8char_T));
3051 }
3052 if (enc_dbcs == DBCS_JPNU)
3053 mch_memmove(ScreenLines2 + off,
3054 screenline2 + r * cols,
3055 (size_t)cols * sizeof(schar_T));
zeertzjqd0c1b772024-03-16 15:03:33 +01003056 screen_line(curwin, cmdline_row + r, 0, cols, cols, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003057 }
3058 ret = 4;
3059 }
3060 }
3061
3062 vim_free(screenline);
3063 vim_free(screenattr);
3064 if (enc_utf8)
3065 {
3066 vim_free(screenlineUC);
3067 for (i = 0; i < p_mco; ++i)
3068 vim_free(screenlineC[i]);
3069 }
3070 if (enc_dbcs == DBCS_JPNU)
3071 vim_free(screenline2);
3072
3073 // Show the intro message when appropriate.
3074 maybe_intro_message();
3075
3076 setcursor();
3077
3078 return ret;
3079}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003080
3081/*
3082 * Invoked after an asynchronous callback is called.
3083 * If an echo command was used the cursor needs to be put back where
3084 * it belongs. If highlighting was changed a redraw is needed.
3085 * If "call_update_screen" is FALSE don't call update_screen() when at the
3086 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003087 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003088 */
3089 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003090redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003091{
3092 ++redrawing_for_callback;
3093
Bram Moolenaar24959102022-05-07 20:01:16 +01003094 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3095 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3096 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003097 {
3098 if (do_message)
3099 repeat_message();
3100 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003101 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003102 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003103 if (pum_visible())
3104 cmdline_pum_display();
Bram Moolenaar54162322022-08-26 16:58:51 +01003105
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003106 // Don't redraw when in prompt_for_number().
3107 if (cmdline_row > 0)
3108 {
3109 // Redrawing only works when the screen didn't scroll. Don't clear
3110 // wildmenu entries.
3111 if (msg_scrolled == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003112 && wild_menu_showing == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003113 && call_update_screen)
3114 update_screen(0);
3115
3116 // Redraw in the same position, so that the user can continue
3117 // editing the command.
3118 redrawcmdline_ex(FALSE);
3119 }
3120 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003121 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003122 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003123 update_topline();
3124 validate_cursor();
3125
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003126 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003127 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003128 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003129
3130 if (msg_scrolled == 0)
3131 {
3132 // don't want a hit-enter prompt when something else is displayed
3133 msg_didany = FALSE;
3134 need_wait_return = FALSE;
3135 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003136 }
3137 cursor_on();
3138#ifdef FEAT_GUI
3139 if (gui.in_use && !gui_mch_is_blink_off())
3140 // Don't update the cursor when it is blinking and off to avoid
3141 // flicker.
3142 out_flush_cursor(FALSE, FALSE);
3143 else
3144#endif
3145 out_flush();
3146
3147 --redrawing_for_callback;
3148}
3149
3150/*
3151 * Redraw the current window later, with update_screen(type).
3152 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003153 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003154 */
3155 void
3156redraw_later(int type)
3157{
3158 redraw_win_later(curwin, type);
3159}
3160
3161 void
3162redraw_win_later(
3163 win_T *wp,
3164 int type)
3165{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003166 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003167 {
3168 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003169 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003170 wp->w_lines_valid = 0;
3171 if (must_redraw < type) // must_redraw is the maximum of all windows
3172 must_redraw = type;
3173 }
3174}
3175
3176/*
3177 * Force a complete redraw later. Also resets the highlighting. To be used
3178 * after executing a shell command that messes up the screen.
3179 */
3180 void
3181redraw_later_clear(void)
3182{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003183 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003184 reset_screen_attr();
3185}
3186
3187/*
Bram Moolenaar13608d82022-08-29 15:06:50 +01003188 * Mark all windows to be redrawn later. Except popup windows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003189 */
3190 void
3191redraw_all_later(int type)
3192{
3193 win_T *wp;
3194
3195 FOR_ALL_WINDOWS(wp)
3196 redraw_win_later(wp, type);
3197 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003198 set_must_redraw(type);
3199}
3200
Bram Moolenaar13608d82022-08-29 15:06:50 +01003201#if 0 // not actually used yet, it probably should
3202/*
3203 * Mark all windows, including popup windows, to be redrawn.
3204 */
3205 void
3206redraw_all_windows_later(int type)
3207{
3208 redraw_all_later(type);
3209#ifdef FEAT_PROP_POPUP
3210 popup_redraw_all(); // redraw all popup windows
3211#endif
3212}
3213#endif
3214
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003215/*
3216 * Set "must_redraw" to "type" unless it already has a higher value
3217 * or it is currently not allowed.
3218 */
3219 void
3220set_must_redraw(int type)
3221{
3222 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003223 must_redraw = type;
3224}
3225
3226/*
3227 * Mark all windows that are editing the current buffer to be updated later.
3228 */
3229 void
3230redraw_curbuf_later(int type)
3231{
3232 redraw_buf_later(curbuf, type);
3233}
3234
3235 void
3236redraw_buf_later(buf_T *buf, int type)
3237{
3238 win_T *wp;
3239
3240 FOR_ALL_WINDOWS(wp)
3241 {
3242 if (wp->w_buffer == buf)
3243 redraw_win_later(wp, type);
3244 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003245#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3246 // terminal in popup window is not in list of windows
3247 if (curwin->w_buffer == buf)
3248 redraw_win_later(curwin, type);
3249#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003250}
3251
3252#if defined(FEAT_SIGNS) || defined(PROTO)
3253 void
3254redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3255{
3256 win_T *wp;
3257
3258 FOR_ALL_WINDOWS(wp)
3259 if (wp->w_buffer == buf && lnum >= wp->w_topline
3260 && lnum < wp->w_botline)
3261 redrawWinline(wp, lnum);
3262}
3263#endif
3264
3265#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3266 void
3267redraw_buf_and_status_later(buf_T *buf, int type)
3268{
3269 win_T *wp;
3270
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003271 if (wild_menu_showing != 0)
3272 // Don't redraw while the command line completion is displayed, it
3273 // would disappear.
3274 return;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003275 FOR_ALL_WINDOWS(wp)
3276 {
3277 if (wp->w_buffer == buf)
3278 {
3279 redraw_win_later(wp, type);
3280 wp->w_redr_status = TRUE;
3281 }
3282 }
3283}
3284#endif
3285
3286/*
3287 * mark all status lines for redraw; used after first :cd
3288 */
3289 void
3290status_redraw_all(void)
3291{
3292 win_T *wp;
3293
3294 FOR_ALL_WINDOWS(wp)
3295 if (wp->w_status_height)
3296 {
3297 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003298 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003299 }
3300}
3301
3302/*
3303 * mark all status lines of the current buffer for redraw
3304 */
3305 void
3306status_redraw_curbuf(void)
3307{
3308 win_T *wp;
3309
3310 FOR_ALL_WINDOWS(wp)
3311 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3312 {
3313 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003314 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003315 }
3316}
3317
3318/*
3319 * Redraw all status lines that need to be redrawn.
3320 */
3321 void
3322redraw_statuslines(void)
3323{
3324 win_T *wp;
3325
3326 FOR_ALL_WINDOWS(wp)
3327 if (wp->w_redr_status)
3328 win_redr_status(wp, FALSE);
3329 if (redraw_tabline)
3330 draw_tabline();
3331}
3332
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003333/*
3334 * Redraw all status lines at the bottom of frame "frp".
3335 */
3336 void
3337win_redraw_last_status(frame_T *frp)
3338{
3339 if (frp->fr_layout == FR_LEAF)
3340 frp->fr_win->w_redr_status = TRUE;
3341 else if (frp->fr_layout == FR_ROW)
3342 {
3343 FOR_ALL_FRAMES(frp, frp->fr_child)
3344 win_redraw_last_status(frp);
3345 }
3346 else // frp->fr_layout == FR_COL
3347 {
3348 frp = frp->fr_child;
3349 while (frp->fr_next != NULL)
3350 frp = frp->fr_next;
3351 win_redraw_last_status(frp);
3352 }
3353}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003354
3355/*
3356 * Changed something in the current window, at buffer line "lnum", that
3357 * requires that line and possibly other lines to be redrawn.
3358 * Used when entering/leaving Insert mode with the cursor on a folded line.
3359 * Used to remove the "$" from a change command.
3360 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3361 * may become invalid and the whole window will have to be redrawn.
3362 */
3363 void
3364redrawWinline(
3365 win_T *wp,
3366 linenr_T lnum)
3367{
Luuk van Baal7bbb0f32025-02-22 09:19:04 +01003368 redraw_win_range_later(wp, lnum, lnum);
3369}
3370
3371 void
3372redraw_win_range_later(
3373 win_T *wp,
3374 linenr_T first,
3375 linenr_T last)
3376{
3377 if (last >= wp->w_topline && first < wp->w_botline)
3378 {
3379 if (wp->w_redraw_top == 0 || wp->w_redraw_top > first)
3380 wp->w_redraw_top = first;
3381 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < last)
3382 wp->w_redraw_bot = last;
3383 redraw_win_later(wp, UPD_VALID);
3384 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003385}