blob: 36034cc9dfc86b19092cad5c3eb8e50f2666ef17 [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)
482 *(p + plen++) = ' ';
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200483 if (bt_help(wp->w_buffer))
John Marriotta21240b2025-01-08 20:10:59 +0100484 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[Help]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200485#ifdef FEAT_QUICKFIX
486 if (wp->w_p_pvw)
John Marriotta21240b2025-01-08 20:10:59 +0100487 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[Preview]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200488#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100489 if (bufIsChanged(wp->w_buffer) && !bt_terminal(wp->w_buffer))
John Marriotta21240b2025-01-08 20:10:59 +0100490 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", "[+]");
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200491 if (wp->w_buffer->b_p_ro)
John Marriotta21240b2025-01-08 20:10:59 +0100492 plen += vim_snprintf((char *)p + plen, MAXPATHL - plen, "%s", _("[RO]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200493
494 this_ru_col = ru_col - (Columns - wp->w_width);
John Marriotta21240b2025-01-08 20:10:59 +0100495 n = (wp->w_width + 1) / 2;
496 if (this_ru_col < n)
497 this_ru_col = n;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200498 if (this_ru_col <= 1)
499 {
500 p = (char_u *)"<"; // No room for file name!
John Marriotta21240b2025-01-08 20:10:59 +0100501 plen = 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200502 }
503 else if (has_mbyte)
504 {
John Marriotta21240b2025-01-08 20:10:59 +0100505 int i;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200506
507 // Count total number of display cells.
John Marriotta21240b2025-01-08 20:10:59 +0100508 plen = mb_string2cells(p, -1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200509
510 // Find first character that will fit.
511 // Going from start to end is much faster for DBCS.
John Marriotta21240b2025-01-08 20:10:59 +0100512 for (i = 0; p[i] != NUL && plen >= this_ru_col - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200513 i += (*mb_ptr2len)(p + i))
John Marriotta21240b2025-01-08 20:10:59 +0100514 plen -= (*mb_ptr2cells)(p + i);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200515 if (i > 0)
516 {
517 p = p + i - 1;
518 *p = '<';
John Marriotta21240b2025-01-08 20:10:59 +0100519 ++plen;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200520 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200521 }
John Marriotta21240b2025-01-08 20:10:59 +0100522 else if (plen > this_ru_col - 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200523 {
John Marriotta21240b2025-01-08 20:10:59 +0100524 p += plen - (this_ru_col - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200525 *p = '<';
John Marriotta21240b2025-01-08 20:10:59 +0100526 plen = this_ru_col - 1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200527 }
528
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200529 screen_puts(p, row, wp->w_wincol, attr);
John Marriotta21240b2025-01-08 20:10:59 +0100530 screen_fill(row, row + 1, plen + wp->w_wincol,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200531 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
532
John Marriotta21240b2025-01-08 20:10:59 +0100533 if ((NameBufflen = get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)) > 0
534 && (this_ru_col - plen) > (NameBufflen + 1))
535 screen_puts(NameBuff, row, (int)(this_ru_col - NameBufflen
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200536 - 1 + wp->w_wincol), attr);
537
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200538 win_redr_ruler(wp, TRUE, ignore_pum);
Luuk van Baalba936f62022-12-15 13:15:39 +0000539
540 // Draw the 'showcmd' information if 'showcmdloc' == "statusline".
541 if (p_sc && *p_sloc == 's')
542 {
John Marriotta21240b2025-01-08 20:10:59 +0100543 n = this_ru_col - plen - 2; // perform the calculation here so we only do it once
544 int width = MIN(10, n);
Luuk van Baalba936f62022-12-15 13:15:39 +0000545
546 if (width > 0)
547 screen_puts_len(showcmd_buf, width, row,
548 wp->w_wincol + this_ru_col - width - 1, attr);
549 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200550 }
551
552 /*
553 * May need to draw the character below the vertical separator.
554 */
555 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
556 {
557 if (stl_connected(wp))
558 fillchar = fillchar_status(&attr, wp);
559 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100560 fillchar = fillchar_vsep(&attr, wp);
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200561 screen_putchar(fillchar, row, W_ENDCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200562 }
563 busy = FALSE;
564}
565
566#ifdef FEAT_STL_OPT
567/*
568 * Redraw the status line according to 'statusline' and take care of any
569 * errors encountered.
570 */
571 static void
572redraw_custom_statusline(win_T *wp)
573{
574 static int entered = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200575
576 // When called recursively return. This can happen when the statusline
577 // contains an expression that triggers a redraw.
578 if (entered)
579 return;
580 entered = TRUE;
581
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200582 win_redr_custom(wp, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200583 entered = FALSE;
584}
585#endif
586
587/*
588 * Show current status info in ruler and various other places
589 * If always is FALSE, only show ruler if position has changed.
590 */
591 void
592showruler(int always)
593{
594 if (!always && !redrawing())
595 return;
596 if (pum_visible())
597 {
598 // Don't redraw right now, do it later.
599 curwin->w_redr_status = TRUE;
600 return;
601 }
602#if defined(FEAT_STL_OPT)
603 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
604 redraw_custom_statusline(curwin);
605 else
606#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200607 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200608
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200609 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000610#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200611 || (p_icon && (stl_syntax & STL_IN_ICON))
612 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000613#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200614 )
615 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000616
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200617 // Redraw the tab pages line if needed.
618 if (redraw_tabline)
619 draw_tabline();
620}
621
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200622 void
623win_redr_ruler(win_T *wp, int always, int ignore_pum)
624{
John Marriotta21240b2025-01-08 20:10:59 +0100625 int empty_line = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200626
Bram Moolenaara2a89732022-08-31 14:46:18 +0100627 // If 'ruler' off don't do anything
628 if (!p_ru)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200629 return;
630
631 /*
632 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
633 * after deleting lines, before cursor.lnum is corrected.
634 */
635 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
636 return;
637
638 // Don't draw the ruler while doing insert-completion, it might overwrite
639 // the (long) mode message.
640 if (wp == lastwin && lastwin->w_status_height == 0)
641 if (edit_submode != NULL)
642 return;
John Marriotta21240b2025-01-08 20:10:59 +0100643
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200644 // Don't draw the ruler when the popup menu is visible, it may overlap.
645 // Except when the popup menu will be redrawn anyway.
646 if (!ignore_pum && pum_visible())
647 return;
648
649#ifdef FEAT_STL_OPT
Bram Moolenaara2a89732022-08-31 14:46:18 +0100650 if (*p_ruf)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200651 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200652 win_redr_custom(wp, TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200653 return;
654 }
655#endif
656
657 /*
658 * Check if not in Insert mode and the line is empty (will show "0-1").
659 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100660 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200661 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
662 empty_line = TRUE;
663
664 /*
665 * Only draw the ruler when something changed.
666 */
667 validate_virtcol_win(wp);
668 if ( redraw_cmdline
669 || always
670 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
671 || wp->w_cursor.col != wp->w_ru_cursor.col
672 || wp->w_virtcol != wp->w_ru_virtcol
673 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
674 || wp->w_topline != wp->w_ru_topline
675 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
676#ifdef FEAT_DIFF
677 || wp->w_topfill != wp->w_ru_topfill
678#endif
679 || empty_line != wp->w_ru_empty)
680 {
John Marriotta21240b2025-01-08 20:10:59 +0100681 int row;
682 int fillchar;
683 int attr;
684 int off;
685 int width;
686 colnr_T virtcol;
687#define RULER_BUF_LEN 70
688 char_u buffer[RULER_BUF_LEN];
689 int bufferlen;
690 char_u rel_pos[RULER_BUF_LEN];
691 int rel_poslen;
692 int this_ru_col;
693 int n1; // scratch value
694 int n2; // scratch value
695
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200696 cursor_off();
697 if (wp->w_status_height)
698 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200699 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200700 fillchar = fillchar_status(&attr, wp);
701 off = wp->w_wincol;
702 width = wp->w_width;
703 }
704 else
705 {
706 row = Rows - 1;
707 fillchar = ' ';
708 attr = 0;
709 width = Columns;
710 off = 0;
711 }
712
713 // In list mode virtcol needs to be recomputed
714 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100715 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200716 {
717 wp->w_p_list = FALSE;
718 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
719 wp->w_p_list = TRUE;
720 }
721
John Marriotta21240b2025-01-08 20:10:59 +0100722 bufferlen = vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200723 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
724 ? 0L
725 : (long)(wp->w_cursor.lnum));
John Marriotta21240b2025-01-08 20:10:59 +0100726 bufferlen += col_print(buffer + bufferlen, RULER_BUF_LEN - bufferlen,
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200727 empty_line ? 0 : (int)wp->w_cursor.col + 1,
728 (int)virtcol + 1);
729
730 /*
731 * Add a "50%" if there is room for it.
732 * On the last line, don't print in the last column (scrolls the
733 * screen up on some terminals).
734 */
John Marriotta21240b2025-01-08 20:10:59 +0100735 rel_poslen = get_rel_pos(wp, rel_pos, RULER_BUF_LEN);
736 n1 = bufferlen + vim_strsize(rel_pos);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200737 if (wp->w_status_height == 0) // can't use last char of screen
John Marriotta21240b2025-01-08 20:10:59 +0100738 ++n1;
739
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200740 this_ru_col = ru_col - (Columns - width);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200741 // Never use more than half the window/screen width, leave the other
742 // half for the filename.
John Marriotta21240b2025-01-08 20:10:59 +0100743 n2 = (width + 1) / 2;
744 if (this_ru_col < n2)
745 this_ru_col = n2;
746 if (this_ru_col + n1 < width)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200747 {
John Marriotta21240b2025-01-08 20:10:59 +0100748 // need at least space for rel_pos + NUL
749 while (this_ru_col + n1 < width
750 && RULER_BUF_LEN > bufferlen + rel_poslen + 1) // +1 for NUL
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200751 {
752 if (has_mbyte)
John Marriotta21240b2025-01-08 20:10:59 +0100753 bufferlen += (*mb_char2bytes)(fillchar, buffer + bufferlen);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200754 else
John Marriotta21240b2025-01-08 20:10:59 +0100755 buffer[bufferlen++] = fillchar;
756 ++n1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200757 }
John Marriotta21240b2025-01-08 20:10:59 +0100758 bufferlen += vim_snprintf((char *)buffer + bufferlen, RULER_BUF_LEN - bufferlen,
759 "%s", rel_pos);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200760 }
761 // Truncate at window boundary.
762 if (has_mbyte)
763 {
John Marriotta21240b2025-01-08 20:10:59 +0100764 for (n1 = 0, n2 = 0; buffer[n1] != NUL; n1 += (*mb_ptr2len)(buffer + n1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200765 {
John Marriotta21240b2025-01-08 20:10:59 +0100766 n2 += (*mb_ptr2cells)(buffer + n1);
767 if (this_ru_col + n2 > width)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200768 {
John Marriotta21240b2025-01-08 20:10:59 +0100769 bufferlen = n1;
770 buffer[bufferlen] = NUL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200771 break;
772 }
773 }
774 }
John Marriotta21240b2025-01-08 20:10:59 +0100775 else if (this_ru_col + bufferlen > width)
776 {
777 bufferlen = width - this_ru_col;
778 buffer[bufferlen] = NUL;
779 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200780
781 screen_puts(buffer, row, this_ru_col + off, attr);
John Marriotta21240b2025-01-08 20:10:59 +0100782 n1 = redraw_cmdline;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200783 screen_fill(row, row + 1,
John Marriotta21240b2025-01-08 20:10:59 +0100784 this_ru_col + off + bufferlen,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000785 (off + width),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200786 fillchar, fillchar, attr);
787 // don't redraw the cmdline because of showing the ruler
John Marriotta21240b2025-01-08 20:10:59 +0100788 redraw_cmdline = n1;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200789 wp->w_ru_cursor = wp->w_cursor;
790 wp->w_ru_virtcol = wp->w_virtcol;
791 wp->w_ru_empty = empty_line;
792 wp->w_ru_topline = wp->w_topline;
793 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
794#ifdef FEAT_DIFF
795 wp->w_ru_topfill = wp->w_topfill;
796#endif
797 }
798}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200799
800/*
801 * To be called when "updating_screen" was set before and now the postponed
802 * side effects may take place.
803 */
804 void
805after_updating_screen(int may_resize_shell UNUSED)
806{
807 updating_screen = FALSE;
808#ifdef FEAT_GUI
809 if (may_resize_shell)
810 gui_may_resize_shell();
811#endif
812#ifdef FEAT_TERMINAL
813 term_check_channel_closed_recently();
814#endif
815
816#ifdef HAVE_DROP_FILE
817 // If handle_drop() was called while updating_screen was TRUE need to
818 // handle the drop now.
819 handle_any_postponed_drop();
820#endif
821}
822
823/*
824 * Update all windows that are editing the current buffer.
825 */
826 void
827update_curbuf(int type)
828{
829 redraw_curbuf_later(type);
830 update_screen(type);
831}
832
833#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
834/*
835 * Copy "text" to ScreenLines using "attr".
836 * Returns the next screen column.
837 */
838 static int
839text_to_screenline(win_T *wp, char_u *text, int col)
840{
841 int off = (int)(current_ScreenLine - ScreenLines);
842
843 if (has_mbyte)
844 {
845 int cells;
846 int u8c, u8cc[MAX_MCO];
847 int i;
848 int idx;
849 int c_len;
850 char_u *p;
851# ifdef FEAT_ARABIC
852 int prev_c = 0; // previous Arabic character
853 int prev_c1 = 0; // first composing char for prev_c
854# endif
855
856# ifdef FEAT_RIGHTLEFT
857 if (wp->w_p_rl)
858 idx = off;
859 else
860# endif
861 idx = off + col;
862
863 // Store multibyte characters in ScreenLines[] et al. correctly.
864 for (p = text; *p != NUL; )
865 {
866 cells = (*mb_ptr2cells)(p);
867 c_len = (*mb_ptr2len)(p);
868 if (col + cells > wp->w_width
869# ifdef FEAT_RIGHTLEFT
870 - (wp->w_p_rl ? col : 0)
871# endif
872 )
873 break;
874 ScreenLines[idx] = *p;
875 if (enc_utf8)
876 {
877 u8c = utfc_ptr2char(p, u8cc);
878 if (*p < 0x80 && u8cc[0] == 0)
879 {
880 ScreenLinesUC[idx] = 0;
881#ifdef FEAT_ARABIC
882 prev_c = u8c;
883#endif
884 }
885 else
886 {
887#ifdef FEAT_ARABIC
888 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
889 {
890 // Do Arabic shaping.
891 int pc, pc1, nc;
892 int pcc[MAX_MCO];
893 int firstbyte = *p;
894
895 // The idea of what is the previous and next
896 // character depends on 'rightleft'.
897 if (wp->w_p_rl)
898 {
899 pc = prev_c;
900 pc1 = prev_c1;
901 nc = utf_ptr2char(p + c_len);
902 prev_c1 = u8cc[0];
903 }
904 else
905 {
906 pc = utfc_ptr2char(p + c_len, pcc);
907 nc = prev_c;
908 pc1 = pcc[0];
909 }
910 prev_c = u8c;
911
912 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
913 pc, pc1, nc);
914 ScreenLines[idx] = firstbyte;
915 }
916 else
917 prev_c = u8c;
918#endif
919 // Non-BMP character: display as ? or fullwidth ?.
920 ScreenLinesUC[idx] = u8c;
921 for (i = 0; i < Screen_mco; ++i)
922 {
923 ScreenLinesC[i][idx] = u8cc[i];
924 if (u8cc[i] == 0)
925 break;
926 }
927 }
928 if (cells > 1)
929 ScreenLines[idx + 1] = 0;
930 }
931 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
932 // double-byte single width character
933 ScreenLines2[idx] = p[1];
934 else if (cells > 1)
935 // double-width character
936 ScreenLines[idx + 1] = p[1];
937 col += cells;
938 idx += cells;
939 p += c_len;
940 }
941 }
942 else
943 {
944 int len = (int)STRLEN(text);
John Marriotta21240b2025-01-08 20:10:59 +0100945 int n = wp->w_width - col;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200946
John Marriotta21240b2025-01-08 20:10:59 +0100947 if (len > n)
948 len = n;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200949 if (len > 0)
950 {
951#ifdef FEAT_RIGHTLEFT
952 if (wp->w_p_rl)
953 mch_memmove(current_ScreenLine, text, len);
954 else
955#endif
956 mch_memmove(current_ScreenLine + col, text, len);
957 col += len;
958 }
959 }
960 return col;
961}
962#endif
963
964#ifdef FEAT_MENU
965/*
966 * Draw the window toolbar.
967 */
968 static void
969redraw_win_toolbar(win_T *wp)
970{
971 vimmenu_T *menu;
972 int item_idx = 0;
973 int item_count = 0;
974 int col = 0;
975 int next_col;
976 int off = (int)(current_ScreenLine - ScreenLines);
977 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
978 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
979
980 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200981 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200982 ++item_count;
983 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
984
985 // TODO: use fewer spaces if there is not enough room
986 for (menu = wp->w_winbar->children;
987 menu != NULL && col < wp->w_width; menu = menu->next)
988 {
989 space_to_screenline(off + col, fill_attr);
990 if (++col >= wp->w_width)
991 break;
992 if (col > 1)
993 {
994 space_to_screenline(off + col, fill_attr);
995 if (++col >= wp->w_width)
996 break;
997 }
998
999 wp->w_winbar_items[item_idx].wb_startcol = col;
1000 space_to_screenline(off + col, button_attr);
1001 if (++col >= wp->w_width)
1002 break;
1003
1004 next_col = text_to_screenline(wp, menu->name, col);
1005 while (col < next_col)
1006 {
1007 ScreenAttrs[off + col] = button_attr;
1008 ++col;
1009 }
1010 wp->w_winbar_items[item_idx].wb_endcol = col;
1011 wp->w_winbar_items[item_idx].wb_menu = menu;
1012 ++item_idx;
1013
1014 if (col >= wp->w_width)
1015 break;
1016 space_to_screenline(off + col, button_attr);
1017 ++col;
1018 }
1019 while (col < wp->w_width)
1020 {
1021 space_to_screenline(off + col, fill_attr);
1022 ++col;
1023 }
1024 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1025
zeertzjqd0c1b772024-03-16 15:03:33 +01001026 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width, wp->w_width, -1,
1027 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001028}
1029#endif
1030
1031#if defined(FEAT_FOLDING) || defined(PROTO)
1032/*
1033 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1034 */
1035 static void
1036copy_text_attr(
1037 int off,
1038 char_u *buf,
1039 int len,
1040 int attr)
1041{
1042 int i;
1043
1044 mch_memmove(ScreenLines + off, buf, (size_t)len);
1045 if (enc_utf8)
1046 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1047 for (i = 0; i < len; ++i)
1048 ScreenAttrs[off + i] = attr;
1049}
1050
1051/*
1052 * Display one folded line.
1053 */
1054 static void
1055fold_line(
1056 win_T *wp,
1057 long fold_count,
1058 foldinfo_T *foldinfo,
1059 linenr_T lnum,
1060 int row)
1061{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001062 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1063 // multi-byte character is MAX_MCO.
1064 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001065 pos_T *top, *bot;
1066 linenr_T lnume = lnum + fold_count - 1;
1067 int len;
1068 char_u *text;
1069 int fdc;
1070 int col;
1071 int txtcol;
1072 int off = (int)(current_ScreenLine - ScreenLines);
1073 int ri;
1074
1075 // Build the fold line:
1076 // 1. Add the cmdwin_type for the command-line window
1077 // 2. Add the 'foldcolumn'
1078 // 3. Add the 'number' or 'relativenumber' column
1079 // 4. Compose the text
1080 // 5. Add the text
1081 // 6. set highlighting for the Visual area an other text
1082 col = 0;
1083
1084 // 1. Add the cmdwin_type for the command-line window
1085 // Ignores 'rightleft', this window is never right-left.
Sean Dewar988f7432023-08-16 14:17:36 +01001086 if (wp == cmdwin_win)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001087 {
1088 ScreenLines[off] = cmdwin_type;
1089 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1090 if (enc_utf8)
1091 ScreenLinesUC[off] = 0;
1092 ++col;
1093 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001094
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001095#ifdef FEAT_RIGHTLEFT
1096# define RL_MEMSET(p, v, l) \
1097 do { \
1098 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001099 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001100 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1101 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001102 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001103 ScreenAttrs[off + (p) + ri] = v; \
1104 } while (0)
1105#else
1106# define RL_MEMSET(p, v, l) \
1107 do { \
1108 for (ri = 0; ri < l; ++ri) \
1109 ScreenAttrs[off + (p) + ri] = v; \
1110 } while (0)
1111#endif
1112
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001113 // 2. Add the 'foldcolumn'
1114 // Reduce the width when there is not enough space.
1115 fdc = compute_foldcolumn(wp, col);
1116 if (fdc > 0)
1117 {
1118 char_u *p;
1119 int i;
1120 int idx;
1121
1122 fill_foldcolumn(buf, wp, TRUE, lnum);
1123 p = buf;
1124 for (i = 0; i < fdc; i++)
1125 {
1126 int ch;
1127
1128 if (has_mbyte)
1129 ch = mb_ptr2char_adv(&p);
1130 else
1131 ch = *p++;
1132#ifdef FEAT_RIGHTLEFT
1133 if (wp->w_p_rl)
1134 idx = off + wp->w_width - i - 1 - col;
1135 else
1136#endif
1137 idx = off + col + i;
1138 if (enc_utf8)
1139 {
1140 if (ch >= 0x80)
1141 {
1142 ScreenLinesUC[idx] = ch;
1143 ScreenLinesC[0][idx] = 0;
1144 ScreenLines[idx] = 0x80;
1145 }
1146 else
1147 {
1148 ScreenLines[idx] = ch;
1149 ScreenLinesUC[idx] = 0;
1150 }
1151 }
1152 else
1153 ScreenLines[idx] = ch;
1154 }
1155
1156 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1157 col += fdc;
1158 }
1159
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001160 // Set all attributes of the 'number' or 'relativenumber' column and the
1161 // text
1162 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1163
1164#ifdef FEAT_SIGNS
1165 // If signs are being displayed, add two spaces.
1166 if (signcolumn_on(wp))
1167 {
1168 len = wp->w_width - col;
1169 if (len > 0)
1170 {
1171 if (len > 2)
1172 len = 2;
1173# ifdef FEAT_RIGHTLEFT
1174 if (wp->w_p_rl)
1175 // the line number isn't reversed
1176 copy_text_attr(off + wp->w_width - len - col,
1177 (char_u *)" ", len, HL_ATTR(HLF_FL));
1178 else
1179# endif
1180 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1181 col += len;
1182 }
1183 }
1184#endif
1185
1186 // 3. Add the 'number' or 'relativenumber' column
1187 if (wp->w_p_nu || wp->w_p_rnu)
1188 {
1189 len = wp->w_width - col;
1190 if (len > 0)
1191 {
1192 int w = number_width(wp);
1193 long num;
1194 char *fmt = "%*ld ";
1195
1196 if (len > w + 1)
1197 len = w + 1;
1198
1199 if (wp->w_p_nu && !wp->w_p_rnu)
1200 // 'number' + 'norelativenumber'
1201 num = (long)lnum;
1202 else
1203 {
1204 // 'relativenumber', don't use negative numbers
1205 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1206 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1207 {
1208 // 'number' + 'relativenumber': cursor line shows absolute
1209 // line number
1210 num = lnum;
1211 fmt = "%-*ld ";
1212 }
1213 }
1214
John Marriotta21240b2025-01-08 20:10:59 +01001215 vim_snprintf((char *)buf, sizeof(buf), fmt, w, num);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001216#ifdef FEAT_RIGHTLEFT
1217 if (wp->w_p_rl)
1218 // the line number isn't reversed
1219 copy_text_attr(off + wp->w_width - len - col, buf, len,
1220 HL_ATTR(HLF_FL));
1221 else
1222#endif
1223 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1224 col += len;
1225 }
1226 }
1227
1228 // 4. Compose the folded-line string with 'foldtext', if set.
1229 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1230
1231 txtcol = col; // remember where text starts
1232
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001233 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1234 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001235 // Right-left text is put in columns 0 - number-col, normal text is put
1236 // in columns number-col - window-width.
1237 col = text_to_screenline(wp, text, col);
1238
1239 // Fill the rest of the line with the fold filler
1240#ifdef FEAT_RIGHTLEFT
1241 if (wp->w_p_rl)
1242 col -= txtcol;
1243#endif
1244 while (col < wp->w_width
1245#ifdef FEAT_RIGHTLEFT
1246 - (wp->w_p_rl ? txtcol : 0)
1247#endif
1248 )
1249 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001250 int c = wp->w_fill_chars.fold;
1251
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001252 if (enc_utf8)
1253 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001254 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001255 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001256 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001257 ScreenLinesC[0][off + col] = 0;
1258 ScreenLines[off + col] = 0x80; // avoid storing zero
1259 }
1260 else
1261 {
1262 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001263 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001264 }
1265 col++;
1266 }
1267 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001268 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001269 }
1270
1271 if (text != buf)
1272 vim_free(text);
1273
1274 // 6. set highlighting for the Visual area an other text.
1275 // If all folded lines are in the Visual area, highlight the line.
1276 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1277 {
1278 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1279 {
1280 // Visual is after curwin->w_cursor
1281 top = &curwin->w_cursor;
1282 bot = &VIsual;
1283 }
1284 else
1285 {
1286 // Visual is before curwin->w_cursor
1287 top = &VIsual;
1288 bot = &curwin->w_cursor;
1289 }
1290 if (lnum >= top->lnum
1291 && lnume <= bot->lnum
1292 && (VIsual_mode != 'v'
1293 || ((lnum > top->lnum
1294 || (lnum == top->lnum
1295 && top->col == 0))
1296 && (lnume < bot->lnum
1297 || (lnume == bot->lnum
1298 && (bot->col - (*p_sel == 'e'))
zeertzjq94b7c322024-03-12 21:50:32 +01001299 >= ml_get_buf_len(wp->w_buffer, lnume))))))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001300 {
1301 if (VIsual_mode == Ctrl_V)
1302 {
1303 // Visual block mode: highlight the chars part of the block
1304 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1305 {
1306 if (wp->w_old_cursor_lcol != MAXCOL
1307 && wp->w_old_cursor_lcol + txtcol
1308 < (colnr_T)wp->w_width)
1309 len = wp->w_old_cursor_lcol;
1310 else
1311 len = wp->w_width - txtcol;
1312 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1313 len - (int)wp->w_old_cursor_fcol);
1314 }
1315 }
1316 else
1317 {
1318 // Set all attributes of the text
1319 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1320 }
1321 }
1322 }
1323
1324#ifdef FEAT_SYN_HL
1325 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1326 if (wp->w_p_cc_cols)
1327 {
1328 int i = 0;
1329 int j = wp->w_p_cc_cols[i];
1330 int old_txtcol = txtcol;
1331
1332 while (j > -1)
1333 {
1334 txtcol += j;
1335 if (wp->w_p_wrap)
1336 txtcol -= wp->w_skipcol;
1337 else
1338 txtcol -= wp->w_leftcol;
1339 if (txtcol >= 0 && txtcol < wp->w_width)
1340 ScreenAttrs[off + txtcol] = hl_combine_attr(
1341 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1342 txtcol = old_txtcol;
1343 j = wp->w_p_cc_cols[++i];
1344 }
1345 }
1346
1347 // Show 'cursorcolumn' in the fold line.
1348 if (wp->w_p_cuc)
1349 {
1350 txtcol += wp->w_virtcol;
1351 if (wp->w_p_wrap)
1352 txtcol -= wp->w_skipcol;
1353 else
1354 txtcol -= wp->w_leftcol;
1355 if (txtcol >= 0 && txtcol < wp->w_width)
1356 ScreenAttrs[off + txtcol] = hl_combine_attr(
1357 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1358 }
1359#endif
1360
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001361 screen_line(wp, row + W_WINROW(wp), wp->w_wincol,
zeertzjqd0c1b772024-03-16 15:03:33 +01001362 wp->w_width, wp->w_width, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001363
1364 // Update w_cline_height and w_cline_folded if the cursor line was
1365 // updated (saves a call to plines() later).
1366 if (wp == curwin
1367 && lnum <= curwin->w_cursor.lnum
1368 && lnume >= curwin->w_cursor.lnum)
1369 {
1370 curwin->w_cline_row = row;
1371 curwin->w_cline_height = 1;
1372 curwin->w_cline_folded = TRUE;
1373 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1374 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001375
1376# ifdef FEAT_CONCEAL
1377 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001378 if (wp == curwin
1379 && wp->w_cursor.lnum >= lnum
1380 && wp->w_cursor.lnum <= lnume
1381 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001382 curs_columns(TRUE);
1383# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001384}
1385#endif
1386
1387/*
1388 * Update a single window.
1389 *
1390 * This may cause the windows below it also to be redrawn (when clearing the
1391 * screen or scrolling lines).
1392 *
1393 * How the window is redrawn depends on wp->w_redr_type. Each type also
1394 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001395 * UPD_NOT_VALID redraw the whole window
1396 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1397 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1398 * UPD_VALID
1399 * UPD_INVERTED redraw the changed part of the Visual area
1400 * UPD_INVERTED_ALL redraw the whole Visual area
1401 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001402 * 2. update lines at the top when scrolled down
1403 * 3. redraw changed text:
1404 * - if wp->w_buffer->b_mod_set set, update lines between
1405 * b_mod_top and b_mod_bot.
1406 * - if wp->w_redraw_top non-zero, redraw lines between
zeertzjqf2d90a32024-02-12 20:28:01 +01001407 * wp->w_redraw_top and wp->w_redraw_bot.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001408 * - continue redrawing when syntax status is invalid.
1409 * 4. if scrolled up, update lines at the bottom.
1410 * This results in three areas that may need updating:
1411 * top: from first row to top_end (when scrolled down)
1412 * mid: from mid_start to mid_end (update inversion or changed text)
1413 * bot: from bot_start to last row (when scrolled up)
1414 */
1415 static void
1416win_update(win_T *wp)
1417{
1418 buf_T *buf = wp->w_buffer;
1419 int type;
1420 int top_end = 0; // Below last row of the top area that needs
1421 // updating. 0 when no top area updating.
1422 int mid_start = 999;// first row of the mid area that needs
1423 // updating. 999 when no mid area updating.
1424 int mid_end = 0; // Below last row of the mid area that needs
1425 // updating. 0 when no mid area updating.
1426 int bot_start = 999;// first row of the bot area that needs
1427 // updating. 999 when no bot area updating
1428 int scrolled_down = FALSE; // TRUE when scrolled down when
1429 // w_topline got smaller a bit
1430#ifdef FEAT_SEARCH_EXTRA
1431 int top_to_mod = FALSE; // redraw above mod_top
1432#endif
1433
1434 int row; // current window row to display
1435 linenr_T lnum; // current buffer lnum to display
1436 int idx; // current index in w_lines[]
1437 int srow; // starting row of the current line
1438
1439 int eof = FALSE; // if TRUE, we hit the end of the file
1440 int didline = FALSE; // if TRUE, we finished the last line
1441 int i;
1442 long j;
1443 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001444 linenr_T old_botline = wp->w_botline;
1445#ifdef FEAT_CONCEAL
1446 int old_wrow = wp->w_wrow;
1447 int old_wcol = wp->w_wcol;
1448#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001449#ifdef FEAT_FOLDING
1450 long fold_count;
1451#endif
1452#ifdef FEAT_SYN_HL
1453 // remember what happened to the previous line, to know if
1454 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001455# define DID_NONE 1 // didn't update a line
1456# define DID_LINE 2 // updated a normal line
1457# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001458 int did_update = DID_NONE;
1459 linenr_T syntax_last_parsed = 0; // last parsed text line
1460#endif
1461 linenr_T mod_top = 0;
1462 linenr_T mod_bot = 0;
1463#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1464 int save_got_int;
1465#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001466
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001467#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1468 // This needs to be done only for the first window when update_screen() is
1469 // called.
1470 if (!did_update_one_window)
1471 {
1472 did_update_one_window = TRUE;
1473# ifdef FEAT_SEARCH_EXTRA
1474 start_search_hl();
1475# endif
1476# ifdef FEAT_CLIPBOARD
1477 // When Visual area changed, may have to update selection.
1478 if (clip_star.available && clip_isautosel_star())
1479 clip_update_selection(&clip_star);
1480 if (clip_plus.available && clip_isautosel_plus())
1481 clip_update_selection(&clip_plus);
1482# endif
1483 }
1484#endif
1485
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001486 type = wp->w_redr_type;
1487
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001488 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001489 {
1490 wp->w_redr_status = TRUE;
1491 wp->w_lines_valid = 0;
1492 }
1493
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001494 // Window frame is zero-height: nothing to draw.
1495 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1496 || (wp->w_frame->fr_height == wp->w_status_height
1497#if defined(FEAT_PROP_POPUP)
1498 && !popup_is_popup(wp)
1499#endif
1500 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001501 {
1502 wp->w_redr_type = 0;
1503 return;
1504 }
1505
1506 // Window is zero-width: Only need to draw the separator.
1507 if (wp->w_width == 0)
1508 {
1509 // draw the vertical separator right of this window
1510 draw_vsep_win(wp, 0);
1511 wp->w_redr_type = 0;
1512 return;
1513 }
1514
1515#ifdef FEAT_TERMINAL
1516 // If this window contains a terminal, redraw works completely differently.
1517 if (term_do_update_window(wp))
1518 {
1519 term_update_window(wp);
1520# ifdef FEAT_MENU
1521 // Draw the window toolbar, if there is one.
1522 if (winbar_height(wp) > 0)
1523 redraw_win_toolbar(wp);
1524# endif
1525 wp->w_redr_type = 0;
1526 return;
1527 }
1528#endif
1529
1530#ifdef FEAT_SEARCH_EXTRA
1531 init_search_hl(wp, &screen_search_hl);
1532#endif
1533
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001534 // Make sure skipcol is valid, it depends on various options and the window
1535 // width.
Sean Dewar02fcae02024-02-21 19:40:44 +01001536 if (wp->w_skipcol > 0 && wp->w_width > win_col_off(wp))
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001537 {
1538 int w = 0;
1539 int width1 = wp->w_width - win_col_off(wp);
1540 int width2 = width1 + win_col_off2(wp);
1541 int add = width1;
1542
1543 while (w < wp->w_skipcol)
1544 {
1545 if (w > 0)
1546 add = width2;
1547 w += add;
1548 }
1549 if (w != wp->w_skipcol)
1550 // always round down, the higher value may not be valid
1551 wp->w_skipcol = w - add;
1552 }
1553
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001554#ifdef FEAT_LINEBREAK
1555 // Force redraw when width of 'number' or 'relativenumber' column
1556 // changes.
1557 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1558 if (wp->w_nrwidth != i)
1559 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001560 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001561 wp->w_nrwidth = i;
1562 }
1563 else
1564#endif
1565
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001566 {
1567 // Set mod_top to the first line that needs displaying because of
1568 // changes. Set mod_bot to the first line after the changes.
1569 mod_top = wp->w_redraw_top;
1570 if (wp->w_redraw_bot != 0)
1571 mod_bot = wp->w_redraw_bot + 1;
1572 else
1573 mod_bot = 0;
1574 if (buf->b_mod_set)
1575 {
1576 if (mod_top == 0 || mod_top > buf->b_mod_top)
1577 {
1578 mod_top = buf->b_mod_top;
1579#ifdef FEAT_SYN_HL
1580 // Need to redraw lines above the change that may be included
1581 // in a pattern match.
1582 if (syntax_present(wp))
1583 {
1584 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1585 if (mod_top < 1)
1586 mod_top = 1;
1587 }
1588#endif
1589 }
1590 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1591 mod_bot = buf->b_mod_bot;
1592
1593#ifdef FEAT_SEARCH_EXTRA
1594 // When 'hlsearch' is on and using a multi-line search pattern, a
1595 // change in one line may make the Search highlighting in a
1596 // previous line invalid. Simple solution: redraw all visible
1597 // lines above the change.
1598 // Same for a match pattern.
1599 if (screen_search_hl.rm.regprog != NULL
1600 && re_multiline(screen_search_hl.rm.regprog))
1601 top_to_mod = TRUE;
1602 else
1603 {
1604 matchitem_T *cur = wp->w_match_head;
1605
1606 while (cur != NULL)
1607 {
Bram Moolenaar50faf022022-09-29 12:50:17 +01001608 if (cur->mit_match.regprog != NULL
1609 && re_multiline(cur->mit_match.regprog))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001610 {
1611 top_to_mod = TRUE;
1612 break;
1613 }
Bram Moolenaar50faf022022-09-29 12:50:17 +01001614 cur = cur->mit_next;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001615 }
1616 }
1617#endif
1618 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001619
1620#ifdef FEAT_SEARCH_EXTRA
1621 if (search_hl_has_cursor_lnum > 0)
1622 {
1623 // CurSearch was used last time, need to redraw the line with it to
1624 // avoid having two matches highlighted with CurSearch.
1625 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1626 mod_top = search_hl_has_cursor_lnum;
1627 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1628 mod_bot = search_hl_has_cursor_lnum + 1;
1629 }
1630#endif
1631
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001632#ifdef FEAT_FOLDING
1633 if (mod_top != 0 && hasAnyFolding(wp))
1634 {
1635 linenr_T lnumt, lnumb;
1636
1637 // A change in a line can cause lines above it to become folded or
1638 // unfolded. Find the top most buffer line that may be affected.
1639 // If the line was previously folded and displayed, get the first
1640 // line of that fold. If the line is folded now, get the first
1641 // folded line. Use the minimum of these two.
1642
1643 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1644 // the line below it. If there is no valid entry, use w_topline.
1645 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1646 // to this line. If there is no valid entry, use MAXLNUM.
1647 lnumt = wp->w_topline;
1648 lnumb = MAXLNUM;
1649 for (i = 0; i < wp->w_lines_valid; ++i)
1650 if (wp->w_lines[i].wl_valid)
1651 {
1652 if (wp->w_lines[i].wl_lastlnum < mod_top)
1653 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1654 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1655 {
1656 lnumb = wp->w_lines[i].wl_lnum;
1657 // When there is a fold column it might need updating
1658 // in the next line ("J" just above an open fold).
1659 if (compute_foldcolumn(wp, 0) > 0)
1660 ++lnumb;
1661 }
1662 }
1663
1664 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1665 if (mod_top > lnumt)
1666 mod_top = lnumt;
1667
1668 // Now do the same for the bottom line (one above mod_bot).
1669 --mod_bot;
1670 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1671 ++mod_bot;
1672 if (mod_bot < lnumb)
1673 mod_bot = lnumb;
1674 }
1675#endif
1676
1677 // When a change starts above w_topline and the end is below
1678 // w_topline, start redrawing at w_topline.
1679 // If the end of the change is above w_topline: do like no change was
1680 // made, but redraw the first line to find changes in syntax.
1681 if (mod_top != 0 && mod_top < wp->w_topline)
1682 {
1683 if (mod_bot > wp->w_topline)
1684 mod_top = wp->w_topline;
1685#ifdef FEAT_SYN_HL
1686 else if (syntax_present(wp))
1687 top_end = 1;
1688#endif
1689 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001690 }
1691 wp->w_redraw_top = 0; // reset for next time
1692 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001693#ifdef FEAT_SEARCH_EXTRA
1694 search_hl_has_cursor_lnum = 0;
1695#endif
1696
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001697
1698 // When only displaying the lines at the top, set top_end. Used when
1699 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001700 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001701 {
1702 j = 0;
1703 for (i = 0; i < wp->w_lines_valid; ++i)
1704 {
1705 j += wp->w_lines[i].wl_size;
1706 if (j >= wp->w_upd_rows)
1707 {
1708 top_end = j;
1709 break;
1710 }
1711 }
1712 if (top_end == 0)
1713 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001714 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001715 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001716 // top area defined, the rest is UPD_VALID
1717 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001718 }
1719
1720 // Trick: we want to avoid clearing the screen twice. screenclear() will
1721 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1722 // non-zero and thus not FALSE) will indicate that screenclear() was not
1723 // called.
1724 if (screen_cleared)
1725 screen_cleared = MAYBE;
1726
1727 // If there are no changes on the screen that require a complete redraw,
1728 // handle three cases:
1729 // 1: we are off the top of the screen by a few lines: scroll down
1730 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1731 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1732 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001733 if ((type == UPD_VALID || type == UPD_SOME_VALID
1734 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001735#ifdef FEAT_DIFF
1736 && !wp->w_botfill && !wp->w_old_botfill
1737#endif
1738 )
1739 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001740 if (mod_top != 0
1741 && wp->w_topline == mod_top
1742 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001743 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001744 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001745 // w_topline is the first changed line and window is not scrolled,
1746 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001747 }
1748 else if (wp->w_lines[0].wl_valid
1749 && (wp->w_topline < wp->w_lines[0].wl_lnum
1750#ifdef FEAT_DIFF
1751 || (wp->w_topline == wp->w_lines[0].wl_lnum
1752 && wp->w_topfill > wp->w_old_topfill)
1753#endif
1754 ))
1755 {
1756 // New topline is above old topline: May scroll down.
1757#ifdef FEAT_FOLDING
1758 if (hasAnyFolding(wp))
1759 {
1760 linenr_T ln;
1761
1762 // count the number of lines we are off, counting a sequence
1763 // of folded lines as one
1764 j = 0;
1765 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1766 {
1767 ++j;
1768 if (j >= wp->w_height - 2)
1769 break;
1770 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1771 }
1772 }
1773 else
1774#endif
1775 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1776 if (j < wp->w_height - 2) // not too far off
1777 {
zeertzjqbfe377b2023-08-17 22:58:53 +02001778 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1,
Luuk van Baal32d701f2024-04-28 16:24:02 +02001779 wp->w_height);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001780#ifdef FEAT_DIFF
1781 // insert extra lines for previously invisible filler lines
1782 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1783 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1784 - wp->w_old_topfill;
1785#endif
1786 if (i < wp->w_height - 2) // less than a screen off
1787 {
1788 // Try to insert the correct number of lines.
1789 // If not the last window, delete the lines at the bottom.
1790 // win_ins_lines may fail when the terminal can't do it.
1791 if (i > 0)
1792 check_for_delay(FALSE);
1793 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1794 {
1795 if (wp->w_lines_valid != 0)
1796 {
1797 // Need to update rows that are new, stop at the
1798 // first one that scrolled down.
1799 top_end = i;
1800 scrolled_down = TRUE;
1801
1802 // Move the entries that were scrolled, disable
1803 // the entries for the lines to be redrawn.
1804 if ((wp->w_lines_valid += j) > wp->w_height)
1805 wp->w_lines_valid = wp->w_height;
Bram Moolenaara2a89732022-08-31 14:46:18 +01001806 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001807 wp->w_lines[idx] = wp->w_lines[idx - j];
1808 while (idx >= 0)
1809 wp->w_lines[idx--].wl_valid = FALSE;
1810 }
1811 }
1812 else
1813 mid_start = 0; // redraw all lines
1814 }
1815 else
1816 mid_start = 0; // redraw all lines
1817 }
1818 else
1819 mid_start = 0; // redraw all lines
1820 }
1821 else
1822 {
1823 // New topline is at or below old topline: May scroll up.
1824 // When topline didn't change, find first entry in w_lines[] that
1825 // needs updating.
1826
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001827 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1828 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001829 j = -1;
1830 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001831 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001832 {
1833 if (wp->w_lines[i].wl_valid
1834 && wp->w_lines[i].wl_lnum == wp->w_topline)
1835 {
1836 j = i;
1837 break;
1838 }
1839 row += wp->w_lines[i].wl_size;
1840 }
1841 if (j == -1)
1842 {
1843 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1844 // lines
1845 mid_start = 0;
1846 }
1847 else
1848 {
1849 // Try to delete the correct number of lines.
1850 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1851#ifdef FEAT_DIFF
1852 // If the topline didn't change, delete old filler lines,
1853 // otherwise delete filler lines of the new topline...
1854 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1855 row += wp->w_old_topfill;
1856 else
1857 row += diff_check_fill(wp, wp->w_topline);
1858 // ... but don't delete new filler lines.
1859 row -= wp->w_topfill;
1860#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001861 if (row > Rows) // just in case
1862 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001863 if (row > 0)
1864 {
1865 check_for_delay(FALSE);
1866 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1867 == OK)
1868 bot_start = wp->w_height - row;
1869 else
1870 mid_start = 0; // redraw all lines
1871 }
1872 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1873 {
1874 // Skip the lines (below the deleted lines) that are still
1875 // valid and don't need redrawing. Copy their info
1876 // upwards, to compensate for the deleted lines. Set
1877 // bot_start to the first row that needs redrawing.
1878 bot_start = 0;
1879 idx = 0;
1880 for (;;)
1881 {
1882 wp->w_lines[idx] = wp->w_lines[j];
1883 // stop at line that didn't fit, unless it is still
1884 // valid (no lines deleted)
1885 if (row > 0 && bot_start + row
1886 + (int)wp->w_lines[j].wl_size > wp->w_height)
1887 {
1888 wp->w_lines_valid = idx + 1;
1889 break;
1890 }
1891 bot_start += wp->w_lines[idx++].wl_size;
1892
1893 // stop at the last valid entry in w_lines[].wl_size
1894 if (++j >= wp->w_lines_valid)
1895 {
1896 wp->w_lines_valid = idx;
1897 break;
1898 }
1899 }
1900#ifdef FEAT_DIFF
1901 // Correct the first entry for filler lines at the top
1902 // when it won't get updated below.
1903 if (wp->w_p_diff && bot_start > 0)
zeertzjq47f85842024-10-01 19:35:47 +02001904 {
1905 int n = plines_win_nofill(wp, wp->w_topline, FALSE)
1906 + wp->w_topfill - adjust_plines_for_skipcol(wp);
1907 if (n > wp->w_height)
1908 n = wp->w_height;
1909 wp->w_lines[0].wl_size = n;
1910 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001911#endif
1912 }
1913 }
1914 }
1915
Bram Moolenaar13608d82022-08-29 15:06:50 +01001916 // When starting redraw in the first line, redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001917 if (mid_start == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001918 mid_end = wp->w_height;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001919
1920 // When win_del_lines() or win_ins_lines() caused the screen to be
1921 // cleared (only happens for the first window) or when screenclear()
1922 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001923 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001924 if (screen_cleared == TRUE)
1925 must_redraw = 0;
1926 }
1927 else
1928 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001929 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001930 mid_start = 0;
1931 mid_end = wp->w_height;
1932 }
1933
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001934 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001935 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001936 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001937 mid_start = 0;
1938 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001939 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001940 }
1941
1942 // check if we are updating or removing the inverted part
1943 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001944 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001945 {
1946 linenr_T from, to;
1947
1948 if (VIsual_active)
1949 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001950 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001951 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001952 {
1953 // If the type of Visual selection changed, redraw the whole
1954 // selection. Also when the ownership of the X selection is
1955 // gained or lost.
1956 if (curwin->w_cursor.lnum < VIsual.lnum)
1957 {
1958 from = curwin->w_cursor.lnum;
1959 to = VIsual.lnum;
1960 }
1961 else
1962 {
1963 from = VIsual.lnum;
1964 to = curwin->w_cursor.lnum;
1965 }
1966 // redraw more when the cursor moved as well
1967 if (wp->w_old_cursor_lnum < from)
1968 from = wp->w_old_cursor_lnum;
1969 if (wp->w_old_cursor_lnum > to)
1970 to = wp->w_old_cursor_lnum;
1971 if (wp->w_old_visual_lnum < from)
1972 from = wp->w_old_visual_lnum;
1973 if (wp->w_old_visual_lnum > to)
1974 to = wp->w_old_visual_lnum;
1975 }
1976 else
1977 {
1978 // Find the line numbers that need to be updated: The lines
1979 // between the old cursor position and the current cursor
1980 // position. Also check if the Visual position changed.
1981 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1982 {
1983 from = curwin->w_cursor.lnum;
1984 to = wp->w_old_cursor_lnum;
1985 }
1986 else
1987 {
1988 from = wp->w_old_cursor_lnum;
1989 to = curwin->w_cursor.lnum;
1990 if (from == 0) // Visual mode just started
1991 from = to;
1992 }
1993
1994 if (VIsual.lnum != wp->w_old_visual_lnum
1995 || VIsual.col != wp->w_old_visual_col)
1996 {
1997 if (wp->w_old_visual_lnum < from
1998 && wp->w_old_visual_lnum != 0)
1999 from = wp->w_old_visual_lnum;
2000 if (wp->w_old_visual_lnum > to)
2001 to = wp->w_old_visual_lnum;
2002 if (VIsual.lnum < from)
2003 from = VIsual.lnum;
2004 if (VIsual.lnum > to)
2005 to = VIsual.lnum;
2006 }
2007 }
2008
2009 // If in block mode and changed column or curwin->w_curswant:
2010 // update all lines.
2011 // First compute the actual start and end column.
2012 if (VIsual_mode == Ctrl_V)
2013 {
2014 colnr_T fromc, toc;
2015#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002016 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002017
2018 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002019 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002020#endif
2021 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002022 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002023#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002024 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002025#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002026 // Highlight to the end of the line, unless 'virtualedit' has
2027 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002028 if (curwin->w_curswant == MAXCOL)
2029 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002030 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002031 {
2032 pos_T pos;
2033 int cursor_above =
2034 curwin->w_cursor.lnum < VIsual.lnum;
2035
2036 // Need to find the longest line.
2037 toc = 0;
2038 pos.coladd = 0;
2039 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2040 ? pos.lnum <= VIsual.lnum
2041 : pos.lnum >= VIsual.lnum;
2042 pos.lnum += cursor_above ? 1 : -1)
2043 {
2044 colnr_T t;
2045
John Marriotta21240b2025-01-08 20:10:59 +01002046 pos.col = (int)ml_get_buf_len(wp->w_buffer, pos.lnum);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002047 getvvcol(wp, &pos, NULL, NULL, &t);
2048 if (toc < t)
2049 toc = t;
2050 }
2051 ++toc;
2052 }
2053 else
2054 toc = MAXCOL;
2055 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002056
2057 if (fromc != wp->w_old_cursor_fcol
2058 || toc != wp->w_old_cursor_lcol)
2059 {
2060 if (from > VIsual.lnum)
2061 from = VIsual.lnum;
2062 if (to < VIsual.lnum)
2063 to = VIsual.lnum;
2064 }
2065 wp->w_old_cursor_fcol = fromc;
2066 wp->w_old_cursor_lcol = toc;
2067 }
2068 }
2069 else
2070 {
2071 // Use the line numbers of the old Visual area.
2072 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2073 {
2074 from = wp->w_old_cursor_lnum;
2075 to = wp->w_old_visual_lnum;
2076 }
2077 else
2078 {
2079 from = wp->w_old_visual_lnum;
2080 to = wp->w_old_cursor_lnum;
2081 }
2082 }
2083
2084 // There is no need to update lines above the top of the window.
2085 if (from < wp->w_topline)
2086 from = wp->w_topline;
2087
2088 // If we know the value of w_botline, use it to restrict the update to
2089 // the lines that are visible in the window.
2090 if (wp->w_valid & VALID_BOTLINE)
2091 {
2092 if (from >= wp->w_botline)
2093 from = wp->w_botline - 1;
2094 if (to >= wp->w_botline)
2095 to = wp->w_botline - 1;
2096 }
2097
2098 // Find the minimal part to be updated.
2099 // Watch out for scrolling that made entries in w_lines[] invalid.
2100 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2101 // top_end; need to redraw from top_end to the "to" line.
2102 // A middle mouse click with a Visual selection may change the text
2103 // above the Visual area and reset wl_valid, do count these for
2104 // mid_end (in srow).
2105 if (mid_start > 0)
2106 {
2107 lnum = wp->w_topline;
2108 idx = 0;
2109 srow = 0;
2110 if (scrolled_down)
2111 mid_start = top_end;
2112 else
2113 mid_start = 0;
2114 while (lnum < from && idx < wp->w_lines_valid) // find start
2115 {
2116 if (wp->w_lines[idx].wl_valid)
2117 mid_start += wp->w_lines[idx].wl_size;
2118 else if (!scrolled_down)
2119 srow += wp->w_lines[idx].wl_size;
2120 ++idx;
2121# ifdef FEAT_FOLDING
2122 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2123 lnum = wp->w_lines[idx].wl_lnum;
2124 else
2125# endif
2126 ++lnum;
2127 }
2128 srow += mid_start;
2129 mid_end = wp->w_height;
2130 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2131 {
2132 if (wp->w_lines[idx].wl_valid
2133 && wp->w_lines[idx].wl_lnum >= to + 1)
2134 {
2135 // Only update until first row of this line
2136 mid_end = srow;
2137 break;
2138 }
2139 srow += wp->w_lines[idx].wl_size;
2140 }
2141 }
2142 }
2143
2144 if (VIsual_active && buf == curwin->w_buffer)
2145 {
2146 wp->w_old_visual_mode = VIsual_mode;
2147 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2148 wp->w_old_visual_lnum = VIsual.lnum;
2149 wp->w_old_visual_col = VIsual.col;
2150 wp->w_old_curswant = curwin->w_curswant;
2151 }
2152 else
2153 {
2154 wp->w_old_visual_mode = 0;
2155 wp->w_old_cursor_lnum = 0;
2156 wp->w_old_visual_lnum = 0;
2157 wp->w_old_visual_col = 0;
2158 }
2159
2160#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2161 // reset got_int, otherwise regexp won't work
2162 save_got_int = got_int;
2163 got_int = 0;
2164#endif
2165#ifdef SYN_TIME_LIMIT
2166 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002167 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002168 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002169#endif
2170#ifdef FEAT_FOLDING
2171 win_foldinfo.fi_level = 0;
2172#endif
2173
2174#ifdef FEAT_MENU
2175 // Draw the window toolbar, if there is one.
2176 // TODO: only when needed.
2177 if (winbar_height(wp) > 0)
2178 redraw_win_toolbar(wp);
2179#endif
2180
Luuk van Baal30805a12023-05-27 22:22:10 +01002181 lnum = wp->w_topline; // first line shown in window
2182
2183 spellvars_T spv;
2184#ifdef FEAT_SPELL
2185 // Initialize spell related variables for the first drawn line.
2186 CLEAR_FIELD(spv);
Luuk van Baale84c7732023-05-31 18:57:36 +01002187 if (spell_check_window(wp))
Luuk van Baal30805a12023-05-27 22:22:10 +01002188 {
Luuk van Baale84c7732023-05-31 18:57:36 +01002189 spv.spv_has_spell = TRUE;
Luuk van Baal30805a12023-05-27 22:22:10 +01002190 spv.spv_unchanged = mod_top == 0;
Luuk van Baal30805a12023-05-27 22:22:10 +01002191 }
2192#endif
2193
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002194 // Update all the window rows.
2195 idx = 0; // first entry in w_lines[].wl_size
2196 row = 0;
2197 srow = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002198 for (;;)
2199 {
2200 // stop updating when reached the end of the window (check for _past_
2201 // the end of the window is at the end of the loop)
2202 if (row == wp->w_height)
2203 {
2204 didline = TRUE;
2205 break;
2206 }
2207
2208 // stop updating when hit the end of the file
2209 if (lnum > buf->b_ml.ml_line_count)
2210 {
2211 eof = TRUE;
2212 break;
2213 }
2214
2215 // Remember the starting row of the line that is going to be dealt
2216 // with. It is used further down when the line doesn't fit.
2217 srow = row;
2218
2219 // Update a line when it is in an area that needs updating, when it
2220 // has changes or w_lines[idx] is invalid.
2221 // "bot_start" may be halfway a wrapped line after using
2222 // win_del_lines(), check if the current line includes it.
2223 // When syntax folding is being used, the saved syntax states will
2224 // already have been updated, we can't see where the syntax state is
2225 // the same again, just update until the end of the window.
2226 if (row < top_end
2227 || (row >= mid_start && row < mid_end)
2228#ifdef FEAT_SEARCH_EXTRA
2229 || top_to_mod
2230#endif
2231 || idx >= wp->w_lines_valid
2232 || (row + wp->w_lines[idx].wl_size > bot_start)
2233 || (mod_top != 0
2234 && (lnum == mod_top
2235 || (lnum >= mod_top
2236 && (lnum < mod_bot
2237#ifdef FEAT_SYN_HL
2238 || did_update == DID_FOLD
2239 || (did_update == DID_LINE
2240 && syntax_present(wp)
2241 && (
2242# ifdef FEAT_FOLDING
2243 (foldmethodIsSyntax(wp)
2244 && hasAnyFolding(wp)) ||
2245# endif
2246 syntax_check_changed(lnum)))
2247#endif
2248#ifdef FEAT_SEARCH_EXTRA
2249 // match in fixed position might need redraw
2250 // if lines were inserted or deleted
2251 || (wp->w_match_head != NULL
zeertzjq9f25a3a2024-11-26 15:08:02 +01002252 && buf->b_mod_set
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002253 && buf->b_mod_xlines != 0)
2254#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002255 ))))
2256#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002257 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2258 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002259#endif
2260 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002261 {
2262#ifdef FEAT_SEARCH_EXTRA
2263 if (lnum == mod_top)
2264 top_to_mod = FALSE;
2265#endif
2266
2267 // When at start of changed lines: May scroll following lines
2268 // up or down to minimize redrawing.
2269 // Don't do this when the change continues until the end.
2270 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002271 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002272 if (lnum == mod_top
2273 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002274 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2275 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002276 {
2277 int old_rows = 0;
2278 int new_rows = 0;
2279 int xtra_rows;
2280 linenr_T l;
2281
2282 // Count the old number of window rows, using w_lines[], which
2283 // should still contain the sizes for the lines as they are
2284 // currently displayed.
2285 for (i = idx; i < wp->w_lines_valid; ++i)
2286 {
2287 // Only valid lines have a meaningful wl_lnum. Invalid
2288 // lines are part of the changed area.
2289 if (wp->w_lines[i].wl_valid
2290 && wp->w_lines[i].wl_lnum == mod_bot)
2291 break;
2292 old_rows += wp->w_lines[i].wl_size;
2293#ifdef FEAT_FOLDING
2294 if (wp->w_lines[i].wl_valid
2295 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2296 {
2297 // Must have found the last valid entry above mod_bot.
2298 // Add following invalid entries.
2299 ++i;
2300 while (i < wp->w_lines_valid
2301 && !wp->w_lines[i].wl_valid)
2302 old_rows += wp->w_lines[i++].wl_size;
2303 break;
2304 }
2305#endif
2306 }
2307
2308 if (i >= wp->w_lines_valid)
2309 {
2310 // We can't find a valid line below the changed lines,
2311 // need to redraw until the end of the window.
2312 // Inserting/deleting lines has no use.
2313 bot_start = 0;
2314 }
2315 else
2316 {
2317 // Able to count old number of rows: Count new window
2318 // rows, and may insert/delete lines
2319 j = idx;
2320 for (l = lnum; l < mod_bot; ++l)
2321 {
2322#ifdef FEAT_FOLDING
2323 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2324 ++new_rows;
2325 else
2326#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002327 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002328#ifdef FEAT_DIFF
2329 if (l == wp->w_topline)
Luuk van Baalc8502f92023-05-06 12:40:15 +01002330 {
2331 int n = plines_win_nofill(wp, l, FALSE)
2332 + wp->w_topfill;
zeertzjq6235a102023-08-19 14:12:42 +02002333 n -= adjust_plines_for_skipcol(wp);
Luuk van Baalc8502f92023-05-06 12:40:15 +01002334 if (n > wp->w_height)
2335 n = wp->w_height;
2336 new_rows += n;
2337 }
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002338 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002339#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002340 new_rows += plines_win(wp, l, TRUE);
2341 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002342 ++j;
2343 if (new_rows > wp->w_height - row - 2)
2344 {
2345 // it's getting too much, must redraw the rest
2346 new_rows = 9999;
2347 break;
2348 }
2349 }
2350 xtra_rows = new_rows - old_rows;
2351 if (xtra_rows < 0)
2352 {
2353 // May scroll text up. If there is not enough
2354 // remaining text or scrolling fails, must redraw the
2355 // rest. If scrolling works, must redraw the text
2356 // below the scrolled text.
2357 if (row - xtra_rows >= wp->w_height - 2)
2358 mod_bot = MAXLNUM;
2359 else
2360 {
2361 check_for_delay(FALSE);
2362 if (win_del_lines(wp, row,
2363 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2364 mod_bot = MAXLNUM;
2365 else
2366 bot_start = wp->w_height + xtra_rows;
2367 }
2368 }
2369 else if (xtra_rows > 0)
2370 {
2371 // May scroll text down. If there is not enough
2372 // remaining text of scrolling fails, must redraw the
2373 // rest.
2374 if (row + xtra_rows >= wp->w_height - 2)
2375 mod_bot = MAXLNUM;
2376 else
2377 {
2378 check_for_delay(FALSE);
2379 if (win_ins_lines(wp, row + old_rows,
2380 xtra_rows, FALSE, FALSE) == FAIL)
2381 mod_bot = MAXLNUM;
2382 else if (top_end > row + old_rows)
2383 // Scrolled the part at the top that requires
2384 // updating down.
2385 top_end += xtra_rows;
2386 }
2387 }
2388
2389 // When not updating the rest, may need to move w_lines[]
2390 // entries.
2391 if (mod_bot != MAXLNUM && i != j)
2392 {
2393 if (j < i)
2394 {
2395 int x = row + new_rows;
2396
2397 // move entries in w_lines[] upwards
2398 for (;;)
2399 {
2400 // stop at last valid entry in w_lines[]
2401 if (i >= wp->w_lines_valid)
2402 {
2403 wp->w_lines_valid = j;
2404 break;
2405 }
2406 wp->w_lines[j] = wp->w_lines[i];
2407 // stop at a line that won't fit
2408 if (x + (int)wp->w_lines[j].wl_size
2409 > wp->w_height)
2410 {
2411 wp->w_lines_valid = j + 1;
2412 break;
2413 }
2414 x += wp->w_lines[j++].wl_size;
2415 ++i;
2416 }
2417 if (bot_start > x)
2418 bot_start = x;
2419 }
2420 else // j > i
2421 {
2422 // move entries in w_lines[] downwards
2423 j -= i;
2424 wp->w_lines_valid += j;
2425 if (wp->w_lines_valid > wp->w_height)
2426 wp->w_lines_valid = wp->w_height;
2427 for (i = wp->w_lines_valid; i - j >= idx; --i)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002428 wp->w_lines[i] = wp->w_lines[i - j];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002429
2430 // The w_lines[] entries for inserted lines are
2431 // now invalid, but wl_size may be used above.
2432 // Reset to zero.
2433 while (i >= idx)
2434 {
2435 wp->w_lines[i].wl_size = 0;
2436 wp->w_lines[i--].wl_valid = FALSE;
2437 }
2438 }
2439 }
2440 }
2441 }
2442
2443#ifdef FEAT_FOLDING
2444 // When lines are folded, display one line for all of them.
2445 // Otherwise, display normally (can be several display lines when
2446 // 'wrap' is on).
2447 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2448 if (fold_count != 0)
2449 {
2450 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2451 ++row;
2452 --fold_count;
2453 wp->w_lines[idx].wl_folded = TRUE;
Luuk van Baale84c7732023-05-31 18:57:36 +01002454 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002455# ifdef FEAT_SYN_HL
2456 did_update = DID_FOLD;
2457# endif
Luuk van Baal30805a12023-05-27 22:22:10 +01002458# ifdef FEAT_SPELL
Luuk van Baale84c7732023-05-31 18:57:36 +01002459 spv.spv_capcol_lnum = 0;
Luuk van Baal30805a12023-05-27 22:22:10 +01002460# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002461 }
2462 else
2463#endif
2464 if (idx < wp->w_lines_valid
2465 && wp->w_lines[idx].wl_valid
2466 && wp->w_lines[idx].wl_lnum == lnum
2467 && lnum > wp->w_topline
2468 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2469 && !WIN_IS_POPUP(wp)
2470 && srow + wp->w_lines[idx].wl_size > wp->w_height
2471#ifdef FEAT_DIFF
2472 && diff_check_fill(wp, lnum) == 0
2473#endif
2474 )
2475 {
2476 // This line is not going to fit. Don't draw anything here,
2477 // will draw "@ " lines below.
2478 row = wp->w_height + 1;
2479 }
2480 else
2481 {
2482#ifdef FEAT_SEARCH_EXTRA
2483 prepare_search_hl(wp, &screen_search_hl, lnum);
2484#endif
2485#ifdef FEAT_SYN_HL
2486 // Let the syntax stuff know we skipped a few lines.
2487 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2488 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002489 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002490#endif
2491
2492 // Display one line.
zeertzjqebfd8562024-02-06 10:59:03 +01002493 row = win_line(wp, lnum, srow, wp->w_height, 0, &spv);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002494
2495#ifdef FEAT_FOLDING
2496 wp->w_lines[idx].wl_folded = FALSE;
2497 wp->w_lines[idx].wl_lastlnum = lnum;
2498#endif
2499#ifdef FEAT_SYN_HL
2500 did_update = DID_LINE;
2501 syntax_last_parsed = lnum;
2502#endif
2503 }
2504
2505 wp->w_lines[idx].wl_lnum = lnum;
2506 wp->w_lines[idx].wl_valid = TRUE;
2507
2508 // Past end of the window or end of the screen. Note that after
2509 // resizing wp->w_height may be end up too big. That's a problem
2510 // elsewhere, but prevent a crash here.
Bram Moolenaara2a89732022-08-31 14:46:18 +01002511 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002512 {
2513 // we may need the size of that too long line later on
2514 if (dollar_vcol == -1)
2515 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2516 ++idx;
2517 break;
2518 }
2519 if (dollar_vcol == -1)
2520 wp->w_lines[idx].wl_size = row - srow;
2521 ++idx;
2522#ifdef FEAT_FOLDING
2523 lnum += fold_count + 1;
2524#else
2525 ++lnum;
2526#endif
2527 }
2528 else
2529 {
zeertzjqae07ebc2024-02-08 11:37:40 +01002530 // If:
2531 // - 'number' is set and below inserted/deleted lines, or
2532 // - 'relativenumber' is set and cursor moved vertically,
2533 // the text doesn't need to be redrawn, but the number column does.
zeertzjq9f25a3a2024-11-26 15:08:02 +01002534 if ((wp->w_p_nu && mod_top != 0 && lnum >= mod_bot
2535 && buf->b_mod_set && buf->b_mod_xlines != 0)
zeertzjqae07ebc2024-02-08 11:37:40 +01002536 || (wp->w_p_rnu
2537 && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002538 {
2539#ifdef FEAT_FOLDING
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002540 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2541 if (fold_count != 0)
2542 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2543 else
2544#endif
zeertzjqebfd8562024-02-06 10:59:03 +01002545 (void)win_line(wp, lnum, srow, wp->w_height,
2546 wp->w_lines[idx].wl_size, &spv);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002547 }
2548
2549 // This line does not need to be drawn, advance to the next one.
2550 row += wp->w_lines[idx++].wl_size;
2551 if (row > wp->w_height) // past end of screen
2552 break;
2553#ifdef FEAT_FOLDING
2554 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2555#else
2556 ++lnum;
2557#endif
2558#ifdef FEAT_SYN_HL
2559 did_update = DID_NONE;
2560#endif
Luuk van Baale84c7732023-05-31 18:57:36 +01002561#ifdef FEAT_SPELL
2562 spv.spv_capcol_lnum = 0;
2563#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002564 }
2565
2566 if (lnum > buf->b_ml.ml_line_count)
2567 {
2568 eof = TRUE;
2569 break;
2570 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002571
2572 // Safety check: if any of the wl_size values is wrong we might go over
2573 // the end of w_lines[].
Bram Moolenaara2a89732022-08-31 14:46:18 +01002574 if (idx >= Rows)
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002575 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002576 }
2577
2578 // End of loop over all window lines.
2579
zeertzjqc20e46a2022-03-23 14:55:23 +00002580#ifdef FEAT_SYN_HL
2581 // Now that the window has been redrawn with the old and new cursor line,
2582 // update w_last_cursorline.
2583 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2584#endif
Lewis Russell16246392022-03-29 11:38:17 +01002585 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002586
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002587#ifdef FEAT_VTP
2588 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002589 // See the version that was fixed.
2590 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002591 {
K.Takata54119102022-02-03 13:33:03 +00002592 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002593
K.Takata54119102022-02-03 13:33:03 +00002594 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002595 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002596 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2597 LineOffset[k] + screen_Columns) > 1)
2598 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002599 else
K.Takata54119102022-02-03 13:33:03 +00002600 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002601 else
K.Takata54119102022-02-03 13:33:03 +00002602 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002603 }
2604#endif
2605
2606 if (idx > wp->w_lines_valid)
2607 wp->w_lines_valid = idx;
2608
2609#ifdef FEAT_SYN_HL
2610 // Let the syntax stuff know we stop parsing here.
2611 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002612 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002613#endif
2614
2615 // If we didn't hit the end of the file, and we didn't finish the last
2616 // line we were working on, then the line didn't fit.
2617 wp->w_empty_rows = 0;
2618#ifdef FEAT_DIFF
2619 wp->w_filler_rows = 0;
2620#endif
2621 if (!eof && !didline)
2622 {
2623 if (lnum == wp->w_topline)
2624 {
2625 // Single line that does not fit!
2626 // Don't overwrite it, it can be edited.
2627 wp->w_botline = lnum + 1;
2628 }
2629#ifdef FEAT_DIFF
2630 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2631 {
2632 // Window ends in filler lines.
2633 wp->w_botline = lnum;
2634 wp->w_filler_rows = wp->w_height - srow;
2635 }
2636#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002637#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002638 else if (WIN_IS_POPUP(wp))
2639 {
2640 // popup line that doesn't fit is left as-is
2641 wp->w_botline = lnum;
2642 }
2643#endif
2644 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2645 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002646 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2647 int symbol = wp->w_fill_chars.lastline;
zeertzjq18b35002022-10-04 20:35:37 +01002648 int charlen;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002649 char_u fillbuf[12]; // 2 characters of 6 bytes
2650
zeertzjq18b35002022-10-04 20:35:37 +01002651 charlen = mb_char2bytes(symbol, &fillbuf[0]);
2652 mb_char2bytes(symbol, &fillbuf[charlen]);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002653
2654 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002655 screen_puts_len(fillbuf,
zeertzjq18b35002022-10-04 20:35:37 +01002656 (wp->w_width > 2 ? 2 : wp->w_width) * charlen,
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002657 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002658 screen_fill(scr_row, scr_row + 1,
2659 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002660 symbol, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002661 set_empty_rows(wp, srow);
2662 wp->w_botline = lnum;
2663 }
2664 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2665 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002666 int start_col = (int)W_ENDCOL(wp) - 3;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002667 int symbol = wp->w_fill_chars.lastline;
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002668
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002669 // Last line isn't finished: Display "@@@" at the end.
2670 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2671 W_WINROW(wp) + wp->w_height,
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002672 start_col < wp->w_wincol ? wp->w_wincol : start_col,
2673 (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002674 symbol, symbol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002675 set_empty_rows(wp, srow);
2676 wp->w_botline = lnum;
2677 }
2678 else
2679 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002680 win_draw_end(wp, wp->w_fill_chars.lastline, ' ', TRUE,
2681 srow, wp->w_height, HLF_AT);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002682 wp->w_botline = lnum;
2683 }
2684 }
2685 else
2686 {
2687 draw_vsep_win(wp, row);
2688 if (eof) // we hit the end of the file
2689 {
2690 wp->w_botline = buf->b_ml.ml_line_count + 1;
2691#ifdef FEAT_DIFF
2692 j = diff_check_fill(wp, wp->w_botline);
2693 if (j > 0 && !wp->w_botfill)
2694 {
2695 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002696 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002697 i = '-';
2698 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002699 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002700 if (row + j > wp->w_height)
2701 j = wp->w_height - row;
2702 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2703 row += j;
2704 }
2705#endif
2706 }
2707 else if (dollar_vcol == -1)
2708 wp->w_botline = lnum;
2709
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002710 // Make sure the rest of the screen is blank.
2711 // write the "eob" character from 'fillchars' to rows that aren't part
2712 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002713 if (WIN_IS_POPUP(wp))
2714 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2715 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002716 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2717 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002718 }
2719
2720#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002721 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002722 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002723#endif
2724
2725 // Reset the type of redrawing required, the window has been updated.
2726 wp->w_redr_type = 0;
2727#ifdef FEAT_DIFF
2728 wp->w_old_topfill = wp->w_topfill;
2729 wp->w_old_botfill = wp->w_botfill;
2730#endif
2731
2732 if (dollar_vcol == -1)
2733 {
2734 // There is a trick with w_botline. If we invalidate it on each
2735 // change that might modify it, this will cause a lot of expensive
2736 // calls to plines() in update_topline() each time. Therefore the
2737 // value of w_botline is often approximated, and this value is used to
2738 // compute the value of w_topline. If the value of w_botline was
2739 // wrong, check that the value of w_topline is correct (cursor is on
2740 // the visible part of the text). If it's not, we need to redraw
2741 // again. Mostly this just means scrolling up a few lines, so it
2742 // doesn't look too bad. Only do this for the current window (where
2743 // changes are relevant).
2744 wp->w_valid |= VALID_BOTLINE;
2745 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2746 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002747 win_T *wwp;
2748#if defined(FEAT_CONCEAL)
2749 linenr_T old_topline = wp->w_topline;
2750 int new_wcol = wp->w_wcol;
2751#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002752 recursive = TRUE;
2753 curwin->w_valid &= ~VALID_TOPLINE;
2754 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002755
2756#if defined(FEAT_CONCEAL)
2757 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2758 != (VALID_WCOL|VALID_WROW))
2759 {
2760 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002761 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002762 // update_topline() the cursor's row or column got invalidated.
2763 // If they are left invalid, setcursor() will recompute them
2764 // but there won't be any further win_line() call to re-fix the
2765 // column and the cursor will end up misplaced. So we call
2766 // cursor validation now and reapply the fix again (or call
2767 // win_line() to do it for us).
2768 validate_cursor();
2769 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2770 && old_topline == wp->w_topline)
2771 wp->w_wcol = new_wcol;
2772 else
2773 redrawWinline(wp, wp->w_cursor.lnum);
2774 }
2775#endif
Luuk van Baal3d5065f2024-09-01 10:33:56 +02002776 // New redraw either due to updated topline, wcol fix or reset skipcol.
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002777 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002778 {
2779 // Don't update for changes in buffer again.
2780 i = curbuf->b_mod_set;
2781 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002782 j = curbuf->b_mod_xlines;
2783 curbuf->b_mod_xlines = 0;
Luuk van Baal3d5065f2024-09-01 10:33:56 +02002784 curs_columns(TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002785 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002786 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002787 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002788 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002789 // Other windows might have w_redr_type raised in update_topline().
2790 must_redraw = 0;
2791 FOR_ALL_WINDOWS(wwp)
2792 if (wwp->w_redr_type > must_redraw)
2793 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002794 recursive = FALSE;
2795 }
2796 }
2797
2798#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2799 // restore got_int, unless CTRL-C was hit while redrawing
2800 if (!got_int)
2801 got_int = save_got_int;
2802#endif
2803}
2804
2805#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2806/*
2807 * Prepare for updating one or more windows.
2808 * Caller must check for "updating_screen" already set to avoid recursiveness.
2809 */
2810 static void
2811update_prepare(void)
2812{
2813 cursor_off();
2814 updating_screen = TRUE;
2815#ifdef FEAT_GUI
2816 // Remove the cursor before starting to do anything, because scrolling may
2817 // make it difficult to redraw the text under it.
2818 if (gui.in_use)
2819 gui_undraw_cursor();
2820#endif
2821#ifdef FEAT_SEARCH_EXTRA
2822 start_search_hl();
2823#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002824#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002825 // Update popup_mask if needed.
2826 may_update_popup_mask(must_redraw);
2827#endif
2828}
2829
2830/*
2831 * Finish updating one or more windows.
2832 */
2833 static void
2834update_finish(void)
2835{
2836 if (redraw_cmdline || redraw_mode)
2837 showmode();
2838
2839# ifdef FEAT_SEARCH_EXTRA
2840 end_search_hl();
2841# endif
2842
2843 after_updating_screen(TRUE);
2844
2845# ifdef FEAT_GUI
2846 // Redraw the cursor and update the scrollbars when all screen updating is
2847 // done.
2848 if (gui.in_use)
2849 {
2850 out_flush_cursor(FALSE, FALSE);
2851 gui_update_scrollbars(FALSE);
2852 }
2853# endif
2854}
2855#endif
2856
2857#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2858 void
2859update_debug_sign(buf_T *buf, linenr_T lnum)
2860{
2861 win_T *wp;
2862 int doit = FALSE;
2863
2864# ifdef FEAT_FOLDING
2865 win_foldinfo.fi_level = 0;
2866# endif
2867
2868 // update/delete a specific sign
2869 redraw_buf_line_later(buf, lnum);
2870
2871 // check if it resulted in the need to redraw a window
2872 FOR_ALL_WINDOWS(wp)
2873 if (wp->w_redr_type != 0)
2874 doit = TRUE;
2875
2876 // Return when there is nothing to do, screen updating is already
2877 // happening (recursive call), messages on the screen or still starting up.
2878 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002879 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002880 || msg_scrolled
2881#ifdef FEAT_GUI
2882 || gui.starting
2883#endif
2884 || starting)
2885 return;
2886
2887 // update all windows that need updating
2888 update_prepare();
2889
2890 FOR_ALL_WINDOWS(wp)
2891 {
2892 if (wp->w_redr_type != 0)
2893 win_update(wp);
2894 if (wp->w_redr_status)
2895 win_redr_status(wp, FALSE);
2896 }
2897
2898 update_finish();
2899}
2900#endif
2901
2902#if defined(FEAT_GUI) || defined(PROTO)
2903/*
2904 * Update a single window, its status line and maybe the command line msg.
2905 * Used for the GUI scrollbar.
2906 */
2907 void
2908updateWindow(win_T *wp)
2909{
2910 // return if already busy updating
2911 if (updating_screen)
2912 return;
2913
2914 update_prepare();
2915
2916#ifdef FEAT_CLIPBOARD
2917 // When Visual area changed, may have to update selection.
2918 if (clip_star.available && clip_isautosel_star())
2919 clip_update_selection(&clip_star);
2920 if (clip_plus.available && clip_isautosel_plus())
2921 clip_update_selection(&clip_plus);
2922#endif
2923
2924 win_update(wp);
2925
2926 // When the screen was cleared redraw the tab pages line.
2927 if (redraw_tabline)
2928 draw_tabline();
2929
Martin Tournoijba43e762022-10-13 22:12:15 +01002930 if (wp->w_redr_status || p_ru
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002931# ifdef FEAT_STL_OPT
2932 || *p_stl != NUL || *wp->w_p_stl != NUL
2933# endif
2934 )
2935 win_redr_status(wp, FALSE);
2936
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002937#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002938 // Display popup windows on top of everything.
2939 update_popups(win_update);
2940#endif
2941
2942 update_finish();
2943}
2944#endif
2945
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002946/*
2947 * Redraw as soon as possible. When the command line is not scrolled redraw
2948 * right away and restore what was on the command line.
2949 * Return a code indicating what happened.
2950 */
2951 int
2952redraw_asap(int type)
2953{
2954 int rows;
2955 int cols = screen_Columns;
2956 int r;
2957 int ret = 0;
2958 schar_T *screenline; // copy from ScreenLines[]
2959 sattr_T *screenattr; // copy from ScreenAttrs[]
2960 int i;
2961 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2962 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2963 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2964
2965 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002966 if (msg_scrolled
2967 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002968 || exiting)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002969 return ret;
2970
2971 // Allocate space to save the text displayed in the command line area.
2972 rows = screen_Rows - cmdline_row;
2973 screenline = LALLOC_MULT(schar_T, rows * cols);
2974 screenattr = LALLOC_MULT(sattr_T, rows * cols);
2975 if (screenline == NULL || screenattr == NULL)
2976 ret = 2;
2977 if (enc_utf8)
2978 {
2979 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
2980 if (screenlineUC == NULL)
2981 ret = 2;
2982 for (i = 0; i < p_mco; ++i)
2983 {
2984 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
2985 if (screenlineC[i] == NULL)
2986 ret = 2;
2987 }
2988 }
2989 if (enc_dbcs == DBCS_JPNU)
2990 {
2991 screenline2 = LALLOC_MULT(schar_T, rows * cols);
2992 if (screenline2 == NULL)
2993 ret = 2;
2994 }
2995
2996 if (ret != 2)
2997 {
2998 // Save the text displayed in the command line area.
2999 for (r = 0; r < rows; ++r)
3000 {
3001 mch_memmove(screenline + r * cols,
3002 ScreenLines + LineOffset[cmdline_row + r],
3003 (size_t)cols * sizeof(schar_T));
3004 mch_memmove(screenattr + r * cols,
3005 ScreenAttrs + LineOffset[cmdline_row + r],
3006 (size_t)cols * sizeof(sattr_T));
3007 if (enc_utf8)
3008 {
3009 mch_memmove(screenlineUC + r * cols,
3010 ScreenLinesUC + LineOffset[cmdline_row + r],
3011 (size_t)cols * sizeof(u8char_T));
3012 for (i = 0; i < p_mco; ++i)
3013 mch_memmove(screenlineC[i] + r * cols,
3014 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3015 (size_t)cols * sizeof(u8char_T));
3016 }
3017 if (enc_dbcs == DBCS_JPNU)
3018 mch_memmove(screenline2 + r * cols,
3019 ScreenLines2 + LineOffset[cmdline_row + r],
3020 (size_t)cols * sizeof(schar_T));
3021 }
3022
3023 update_screen(0);
3024 ret = 3;
3025
3026 if (must_redraw == 0)
3027 {
3028 int off = (int)(current_ScreenLine - ScreenLines);
3029
3030 // Restore the text displayed in the command line area.
3031 for (r = 0; r < rows; ++r)
3032 {
3033 mch_memmove(current_ScreenLine,
3034 screenline + r * cols,
3035 (size_t)cols * sizeof(schar_T));
3036 mch_memmove(ScreenAttrs + off,
3037 screenattr + r * cols,
3038 (size_t)cols * sizeof(sattr_T));
3039 if (enc_utf8)
3040 {
3041 mch_memmove(ScreenLinesUC + off,
3042 screenlineUC + r * cols,
3043 (size_t)cols * sizeof(u8char_T));
3044 for (i = 0; i < p_mco; ++i)
3045 mch_memmove(ScreenLinesC[i] + off,
3046 screenlineC[i] + r * cols,
3047 (size_t)cols * sizeof(u8char_T));
3048 }
3049 if (enc_dbcs == DBCS_JPNU)
3050 mch_memmove(ScreenLines2 + off,
3051 screenline2 + r * cols,
3052 (size_t)cols * sizeof(schar_T));
zeertzjqd0c1b772024-03-16 15:03:33 +01003053 screen_line(curwin, cmdline_row + r, 0, cols, cols, -1, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003054 }
3055 ret = 4;
3056 }
3057 }
3058
3059 vim_free(screenline);
3060 vim_free(screenattr);
3061 if (enc_utf8)
3062 {
3063 vim_free(screenlineUC);
3064 for (i = 0; i < p_mco; ++i)
3065 vim_free(screenlineC[i]);
3066 }
3067 if (enc_dbcs == DBCS_JPNU)
3068 vim_free(screenline2);
3069
3070 // Show the intro message when appropriate.
3071 maybe_intro_message();
3072
3073 setcursor();
3074
3075 return ret;
3076}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003077
3078/*
3079 * Invoked after an asynchronous callback is called.
3080 * If an echo command was used the cursor needs to be put back where
3081 * it belongs. If highlighting was changed a redraw is needed.
3082 * If "call_update_screen" is FALSE don't call update_screen() when at the
3083 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003084 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003085 */
3086 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003087redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003088{
3089 ++redrawing_for_callback;
3090
Bram Moolenaar24959102022-05-07 20:01:16 +01003091 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3092 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3093 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003094 {
3095 if (do_message)
3096 repeat_message();
3097 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003098 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003099 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003100 if (pum_visible())
3101 cmdline_pum_display();
Bram Moolenaar54162322022-08-26 16:58:51 +01003102
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003103 // Don't redraw when in prompt_for_number().
3104 if (cmdline_row > 0)
3105 {
3106 // Redrawing only works when the screen didn't scroll. Don't clear
3107 // wildmenu entries.
3108 if (msg_scrolled == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003109 && wild_menu_showing == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003110 && call_update_screen)
3111 update_screen(0);
3112
3113 // Redraw in the same position, so that the user can continue
3114 // editing the command.
3115 redrawcmdline_ex(FALSE);
3116 }
3117 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003118 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003119 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003120 update_topline();
3121 validate_cursor();
3122
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003123 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003124 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003125 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003126
3127 if (msg_scrolled == 0)
3128 {
3129 // don't want a hit-enter prompt when something else is displayed
3130 msg_didany = FALSE;
3131 need_wait_return = FALSE;
3132 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003133 }
3134 cursor_on();
3135#ifdef FEAT_GUI
3136 if (gui.in_use && !gui_mch_is_blink_off())
3137 // Don't update the cursor when it is blinking and off to avoid
3138 // flicker.
3139 out_flush_cursor(FALSE, FALSE);
3140 else
3141#endif
3142 out_flush();
3143
3144 --redrawing_for_callback;
3145}
3146
3147/*
3148 * Redraw the current window later, with update_screen(type).
3149 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003150 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003151 */
3152 void
3153redraw_later(int type)
3154{
3155 redraw_win_later(curwin, type);
3156}
3157
3158 void
3159redraw_win_later(
3160 win_T *wp,
3161 int type)
3162{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003163 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003164 {
3165 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003166 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003167 wp->w_lines_valid = 0;
3168 if (must_redraw < type) // must_redraw is the maximum of all windows
3169 must_redraw = type;
3170 }
3171}
3172
3173/*
3174 * Force a complete redraw later. Also resets the highlighting. To be used
3175 * after executing a shell command that messes up the screen.
3176 */
3177 void
3178redraw_later_clear(void)
3179{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003180 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003181 reset_screen_attr();
3182}
3183
3184/*
Bram Moolenaar13608d82022-08-29 15:06:50 +01003185 * Mark all windows to be redrawn later. Except popup windows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003186 */
3187 void
3188redraw_all_later(int type)
3189{
3190 win_T *wp;
3191
3192 FOR_ALL_WINDOWS(wp)
3193 redraw_win_later(wp, type);
3194 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003195 set_must_redraw(type);
3196}
3197
Bram Moolenaar13608d82022-08-29 15:06:50 +01003198#if 0 // not actually used yet, it probably should
3199/*
3200 * Mark all windows, including popup windows, to be redrawn.
3201 */
3202 void
3203redraw_all_windows_later(int type)
3204{
3205 redraw_all_later(type);
3206#ifdef FEAT_PROP_POPUP
3207 popup_redraw_all(); // redraw all popup windows
3208#endif
3209}
3210#endif
3211
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003212/*
3213 * Set "must_redraw" to "type" unless it already has a higher value
3214 * or it is currently not allowed.
3215 */
3216 void
3217set_must_redraw(int type)
3218{
3219 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003220 must_redraw = type;
3221}
3222
3223/*
3224 * Mark all windows that are editing the current buffer to be updated later.
3225 */
3226 void
3227redraw_curbuf_later(int type)
3228{
3229 redraw_buf_later(curbuf, type);
3230}
3231
3232 void
3233redraw_buf_later(buf_T *buf, int type)
3234{
3235 win_T *wp;
3236
3237 FOR_ALL_WINDOWS(wp)
3238 {
3239 if (wp->w_buffer == buf)
3240 redraw_win_later(wp, type);
3241 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003242#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3243 // terminal in popup window is not in list of windows
3244 if (curwin->w_buffer == buf)
3245 redraw_win_later(curwin, type);
3246#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003247}
3248
3249#if defined(FEAT_SIGNS) || defined(PROTO)
3250 void
3251redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3252{
3253 win_T *wp;
3254
3255 FOR_ALL_WINDOWS(wp)
3256 if (wp->w_buffer == buf && lnum >= wp->w_topline
3257 && lnum < wp->w_botline)
3258 redrawWinline(wp, lnum);
3259}
3260#endif
3261
3262#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3263 void
3264redraw_buf_and_status_later(buf_T *buf, int type)
3265{
3266 win_T *wp;
3267
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003268 if (wild_menu_showing != 0)
3269 // Don't redraw while the command line completion is displayed, it
3270 // would disappear.
3271 return;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003272 FOR_ALL_WINDOWS(wp)
3273 {
3274 if (wp->w_buffer == buf)
3275 {
3276 redraw_win_later(wp, type);
3277 wp->w_redr_status = TRUE;
3278 }
3279 }
3280}
3281#endif
3282
3283/*
3284 * mark all status lines for redraw; used after first :cd
3285 */
3286 void
3287status_redraw_all(void)
3288{
3289 win_T *wp;
3290
3291 FOR_ALL_WINDOWS(wp)
3292 if (wp->w_status_height)
3293 {
3294 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003295 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003296 }
3297}
3298
3299/*
3300 * mark all status lines of the current buffer for redraw
3301 */
3302 void
3303status_redraw_curbuf(void)
3304{
3305 win_T *wp;
3306
3307 FOR_ALL_WINDOWS(wp)
3308 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3309 {
3310 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003311 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003312 }
3313}
3314
3315/*
3316 * Redraw all status lines that need to be redrawn.
3317 */
3318 void
3319redraw_statuslines(void)
3320{
3321 win_T *wp;
3322
3323 FOR_ALL_WINDOWS(wp)
3324 if (wp->w_redr_status)
3325 win_redr_status(wp, FALSE);
3326 if (redraw_tabline)
3327 draw_tabline();
3328}
3329
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003330/*
3331 * Redraw all status lines at the bottom of frame "frp".
3332 */
3333 void
3334win_redraw_last_status(frame_T *frp)
3335{
3336 if (frp->fr_layout == FR_LEAF)
3337 frp->fr_win->w_redr_status = TRUE;
3338 else if (frp->fr_layout == FR_ROW)
3339 {
3340 FOR_ALL_FRAMES(frp, frp->fr_child)
3341 win_redraw_last_status(frp);
3342 }
3343 else // frp->fr_layout == FR_COL
3344 {
3345 frp = frp->fr_child;
3346 while (frp->fr_next != NULL)
3347 frp = frp->fr_next;
3348 win_redraw_last_status(frp);
3349 }
3350}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003351
3352/*
3353 * Changed something in the current window, at buffer line "lnum", that
3354 * requires that line and possibly other lines to be redrawn.
3355 * Used when entering/leaving Insert mode with the cursor on a folded line.
3356 * Used to remove the "$" from a change command.
3357 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3358 * may become invalid and the whole window will have to be redrawn.
3359 */
3360 void
3361redrawWinline(
3362 win_T *wp,
3363 linenr_T lnum)
3364{
3365 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
3366 wp->w_redraw_top = lnum;
3367 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
3368 wp->w_redraw_bot = lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003369 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003370}