blob: ff0f2eaebd695abc11dd0bb7d008c84d7b557a77 [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
Bram Moolenaarbdff0122020-04-05 18:56:05 +020076static void win_redr_status(win_T *wp, int ignore_pum);
77
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020078/*
79 * Based on the current value of curwin->w_topline, transfer a screenfull
80 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
81 * Return OK when the screen was updated, FAIL if it was not done.
82 */
83 int
84update_screen(int type_arg)
85{
86 int type = type_arg;
87 win_T *wp;
88 static int did_intro = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020089#ifdef FEAT_GUI
Bram Moolenaare52e0c82020-02-28 22:20:10 +010090 int did_one = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020091 int did_undraw = FALSE;
92 int gui_cursor_col = 0;
93 int gui_cursor_row = 0;
94#endif
95 int no_update = FALSE;
Bram Moolenaare0c03c82021-04-23 21:01:34 +020096 int save_pum_will_redraw = pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020097
98 // Don't do anything if the screen structures are (not yet) valid.
99 if (!screen_valid(TRUE))
100 return FAIL;
101
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100102 if (type == UPD_VALID_NO_UPDATE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200103 {
104 no_update = TRUE;
105 type = 0;
106 }
107
108#ifdef FEAT_EVAL
109 {
110 buf_T *buf;
111
112 // Before updating the screen, notify any listeners of changed text.
113 FOR_ALL_BUFFERS(buf)
114 invoke_listeners(buf);
115 }
116#endif
117
118#ifdef FEAT_DIFF
119 // May have postponed updating diffs.
120 if (need_diff_redraw)
121 diff_redraw(TRUE);
122#endif
123
124 if (must_redraw)
125 {
126 if (type < must_redraw) // use maximal type
127 type = must_redraw;
128
129 // must_redraw is reset here, so that when we run into some weird
130 // reason to redraw while busy redrawing (e.g., asynchronous
131 // scrolling), or update_topline() in win_update() will cause a
132 // scroll, the screen will be redrawn later or in win_update().
133 must_redraw = 0;
134 }
135
136 // May need to update w_lines[].
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100137 if (curwin->w_lines_valid == 0 && type < UPD_NOT_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200138#ifdef FEAT_TERMINAL
139 && !term_do_update_window(curwin)
140#endif
141 )
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100142 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200143
144 // Postpone the redrawing when it's not needed and when being called
145 // recursively.
146 if (!redrawing() || updating_screen)
147 {
148 redraw_later(type); // remember type for next time
149 must_redraw = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100150 if (type > UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200151 curwin->w_lines_valid = 0; // don't use w_lines[].wl_size now
152 return FAIL;
153 }
154 updating_screen = TRUE;
155
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100156#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200157 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
158 // in some windows.
159 may_update_popup_mask(type);
160#endif
161
162#ifdef FEAT_SYN_HL
163 ++display_tick; // let syntax code know we're in a next round of
164 // display updating
165#endif
166 if (no_update)
167 ++no_win_do_lines_ins;
168
169 // if the screen was scrolled up when displaying a message, scroll it down
170 if (msg_scrolled)
171 {
172 clear_cmdline = TRUE;
173 if (msg_scrolled > Rows - 5) // clearing is faster
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100174 type = UPD_CLEAR;
175 else if (type != UPD_CLEAR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200176 {
177 check_for_delay(FALSE);
178 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
179 == FAIL)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100180 type = UPD_CLEAR;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200181 FOR_ALL_WINDOWS(wp)
182 {
183 if (wp->w_winrow < msg_scrolled)
184 {
185 if (W_WINROW(wp) + wp->w_height > msg_scrolled
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100186 && wp->w_redr_type < UPD_REDRAW_TOP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200187 && wp->w_lines_valid > 0
188 && wp->w_topline == wp->w_lines[0].wl_lnum)
189 {
190 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100191 wp->w_redr_type = UPD_REDRAW_TOP;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200192 }
193 else
194 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100195 wp->w_redr_type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200196 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
197 <= msg_scrolled)
198 wp->w_redr_status = TRUE;
199 }
200 }
201 }
202 if (!no_update)
203 redraw_cmdline = TRUE;
204 redraw_tabline = TRUE;
205 }
206 msg_scrolled = 0;
207 need_wait_return = FALSE;
208 }
209
210 // reset cmdline_row now (may have been changed temporarily)
211 compute_cmdrow();
212
213 // Check for changed highlighting
214 if (need_highlight_changed)
215 highlight_changed();
216
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100217 if (type == UPD_CLEAR) // first clear screen
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200218 {
219 screenclear(); // will reset clear_cmdline
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100220 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200221 // must_redraw may be set indirectly, avoid another redraw later
222 must_redraw = 0;
223 }
224
225 if (clear_cmdline) // going to clear cmdline (done below)
226 check_for_delay(FALSE);
227
228#ifdef FEAT_LINEBREAK
229 // Force redraw when width of 'number' or 'relativenumber' column
230 // changes.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100231 if (curwin->w_redr_type < UPD_NOT_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200232 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
233 ? number_width(curwin) : 0))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100234 curwin->w_redr_type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200235#endif
236
237 // Only start redrawing if there is really something to do.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100238 if (type == UPD_INVERTED)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200239 update_curswant();
240 if (curwin->w_redr_type < type
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100241 && !((type == UPD_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200242 && curwin->w_lines[0].wl_valid
243#ifdef FEAT_DIFF
244 && curwin->w_topfill == curwin->w_old_topfill
245 && curwin->w_botfill == curwin->w_old_botfill
246#endif
247 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100248 || (type == UPD_INVERTED
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200249 && VIsual_active
250 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
251 && curwin->w_old_visual_mode == VIsual_mode
252 && (curwin->w_valid & VALID_VIRTCOL)
253 && curwin->w_old_curswant == curwin->w_curswant)
254 ))
255 curwin->w_redr_type = type;
256
257 // Redraw the tab pages line if needed.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100258 if (redraw_tabline || type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200259 draw_tabline();
260
261#ifdef FEAT_SYN_HL
262 // Correct stored syntax highlighting info for changes in each displayed
263 // buffer. Each buffer must only be done once.
264 FOR_ALL_WINDOWS(wp)
265 {
266 if (wp->w_buffer->b_mod_set)
267 {
268 win_T *wwp;
269
270 // Check if we already did this buffer.
271 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
272 if (wwp->w_buffer == wp->w_buffer)
273 break;
274 if (wwp == wp && syntax_present(wp))
275 syn_stack_apply_changes(wp->w_buffer);
276 }
277 }
278#endif
279
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200280 if (pum_redraw_in_same_position())
281 // Avoid flicker if the popup menu is going to be redrawn in the same
282 // position.
283 pum_will_redraw = TRUE;
284
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200285 // Go from top to bottom through the windows, redrawing the ones that need
286 // it.
287#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100288 did_update_one_window = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200289#endif
290#ifdef FEAT_SEARCH_EXTRA
291 screen_search_hl.rm.regprog = NULL;
292#endif
293 FOR_ALL_WINDOWS(wp)
294 {
295 if (wp->w_redr_type != 0)
296 {
297 cursor_off();
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100298#ifdef FEAT_GUI
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200299 if (!did_one)
300 {
301 did_one = TRUE;
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100302
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200303 // Remove the cursor before starting to do anything, because
304 // scrolling may make it difficult to redraw the text under
305 // it.
Bram Moolenaar09f067f2021-04-11 13:29:18 +0200306 // Also remove the cursor if it needs to be hidden due to an
307 // ongoing cursor-less sleep.
308 if (gui.in_use && (wp == curwin || cursor_is_sleeping()))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200309 {
310 gui_cursor_col = gui.cursor_col;
311 gui_cursor_row = gui.cursor_row;
312 gui_undraw_cursor();
313 did_undraw = TRUE;
314 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200315 }
316#endif
317 win_update(wp);
318 }
319
320 // redraw status line after the window to minimize cursor movement
321 if (wp->w_redr_status)
322 {
323 cursor_off();
324 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
325 }
326 }
327#if defined(FEAT_SEARCH_EXTRA)
328 end_search_hl();
329#endif
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200330
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200331 // May need to redraw the popup menu.
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200332 pum_will_redraw = save_pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200333 pum_may_redraw();
334
335 // Reset b_mod_set flags. Going through all windows is probably faster
336 // than going through all buffers (there could be many buffers).
337 FOR_ALL_WINDOWS(wp)
338 wp->w_buffer->b_mod_set = FALSE;
339
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100340#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200341 // Display popup windows on top of the windows and command line.
342 update_popups(win_update);
343#endif
344
Bram Moolenaar3194e5b2021-12-13 21:59:09 +0000345#ifdef FEAT_TERMINAL
346 FOR_ALL_WINDOWS(wp)
347 // If this window contains a terminal, after redrawing all windows, the
348 // dirty row range can be reset.
349 term_did_update_window(wp);
350#endif
351
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200352 after_updating_screen(TRUE);
353
354 // Clear or redraw the command line. Done last, because scrolling may
355 // mess up the command line.
356 if (clear_cmdline || redraw_cmdline || redraw_mode)
357 showmode();
358
359 if (no_update)
360 --no_win_do_lines_ins;
361
362 // May put up an introductory message when not editing a file
363 if (!did_intro)
364 maybe_intro_message();
365 did_intro = TRUE;
366
367#ifdef FEAT_GUI
368 // Redraw the cursor and update the scrollbars when all screen updating is
369 // done.
370 if (gui.in_use)
371 {
372 if (did_undraw && !gui_mch_is_blink_off())
373 {
374 mch_disable_flush();
375 out_flush(); // required before updating the cursor
376 mch_enable_flush();
377
378 // Put the GUI position where the cursor was, gui_update_cursor()
379 // uses that.
380 gui.col = gui_cursor_col;
381 gui.row = gui_cursor_row;
382 gui.col = mb_fix_col(gui.col, gui.row);
383 gui_update_cursor(FALSE, FALSE);
384 gui_may_flush();
385 screen_cur_col = gui.col;
386 screen_cur_row = gui.row;
387 }
388 else
389 out_flush();
390 gui_update_scrollbars(FALSE);
391 }
392#endif
393 return OK;
394}
395
396/*
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200397 * Return the row for drawing the statusline and the ruler of window "wp".
398 */
Bram Moolenaar49c51b82021-04-01 16:16:18 +0200399 int
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200400statusline_row(win_T *wp)
401{
402#if defined(FEAT_PROP_POPUP)
403 // If the window is really zero height the winbar isn't displayed.
404 if (wp->w_frame->fr_height == wp->w_status_height && !popup_is_popup(wp))
405 return wp->w_winrow;
406#endif
407 return W_WINROW(wp) + wp->w_height;
408}
409
410/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200411 * Redraw the status line of window wp.
412 *
413 * If inversion is possible we use it. Else '=' characters are used.
414 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
415 * displayed.
416 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200417 static void
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200418win_redr_status(win_T *wp, int ignore_pum UNUSED)
419{
420 int row;
421 char_u *p;
422 int len;
423 int fillchar;
424 int attr;
425 int this_ru_col;
426 static int busy = FALSE;
427
428 // It's possible to get here recursively when 'statusline' (indirectly)
429 // invokes ":redrawstatus". Simply ignore the call then.
430 if (busy)
431 return;
432 busy = TRUE;
433
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200434 row = statusline_row(wp);
435
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200436 wp->w_redr_status = FALSE;
437 if (wp->w_status_height == 0)
438 {
439 // no status line, can only be last window
440 redraw_cmdline = TRUE;
441 }
442 else if (!redrawing()
443 // don't update status line when popup menu is visible and may be
444 // drawn over it, unless it will be redrawn later
445 || (!ignore_pum && pum_visible()))
446 {
447 // Don't redraw right now, do it later.
448 wp->w_redr_status = TRUE;
449 }
450#ifdef FEAT_STL_OPT
451 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
452 {
453 // redraw custom status line
454 redraw_custom_statusline(wp);
455 }
456#endif
457 else
458 {
459 fillchar = fillchar_status(&attr, wp);
460
461 get_trans_bufname(wp->w_buffer);
462 p = NameBuff;
463 len = (int)STRLEN(p);
464
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000465 if ((bt_help(wp->w_buffer)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200466#ifdef FEAT_QUICKFIX
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000467 || wp->w_p_pvw
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200468#endif
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000469 || bufIsChanged(wp->w_buffer)
470 || wp->w_buffer->b_p_ro)
471 && len < MAXPATHL - 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200472 *(p + len++) = ' ';
473 if (bt_help(wp->w_buffer))
474 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100475 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Help]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200476 len += (int)STRLEN(p + len);
477 }
478#ifdef FEAT_QUICKFIX
479 if (wp->w_p_pvw)
480 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100481 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Preview]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200482 len += (int)STRLEN(p + len);
483 }
484#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100485 if (bufIsChanged(wp->w_buffer) && !bt_terminal(wp->w_buffer))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200486 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100487 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", "[+]");
488 len += (int)STRLEN(p + len);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200489 }
490 if (wp->w_buffer->b_p_ro)
491 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100492 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[RO]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200493 len += (int)STRLEN(p + len);
494 }
495
496 this_ru_col = ru_col - (Columns - wp->w_width);
497 if (this_ru_col < (wp->w_width + 1) / 2)
498 this_ru_col = (wp->w_width + 1) / 2;
499 if (this_ru_col <= 1)
500 {
501 p = (char_u *)"<"; // No room for file name!
502 len = 1;
503 }
504 else if (has_mbyte)
505 {
506 int clen = 0, i;
507
508 // Count total number of display cells.
509 clen = mb_string2cells(p, -1);
510
511 // Find first character that will fit.
512 // Going from start to end is much faster for DBCS.
513 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
514 i += (*mb_ptr2len)(p + i))
515 clen -= (*mb_ptr2cells)(p + i);
516 len = clen;
517 if (i > 0)
518 {
519 p = p + i - 1;
520 *p = '<';
521 ++len;
522 }
523
524 }
525 else if (len > this_ru_col - 1)
526 {
527 p += len - (this_ru_col - 1);
528 *p = '<';
529 len = this_ru_col - 1;
530 }
531
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200532 screen_puts(p, row, wp->w_wincol, attr);
533 screen_fill(row, row + 1, len + wp->w_wincol,
534 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
535
536 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000537 && (this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200538 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
539 - 1 + wp->w_wincol), attr);
540
541#ifdef FEAT_CMDL_INFO
542 win_redr_ruler(wp, TRUE, ignore_pum);
543#endif
544 }
545
546 /*
547 * May need to draw the character below the vertical separator.
548 */
549 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
550 {
551 if (stl_connected(wp))
552 fillchar = fillchar_status(&attr, wp);
553 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100554 fillchar = fillchar_vsep(&attr, wp);
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200555 screen_putchar(fillchar, row, W_ENDCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200556 }
557 busy = FALSE;
558}
559
560#ifdef FEAT_STL_OPT
561/*
562 * Redraw the status line according to 'statusline' and take care of any
563 * errors encountered.
564 */
565 static void
566redraw_custom_statusline(win_T *wp)
567{
568 static int entered = FALSE;
569 int saved_did_emsg = did_emsg;
570
571 // When called recursively return. This can happen when the statusline
572 // contains an expression that triggers a redraw.
573 if (entered)
574 return;
575 entered = TRUE;
576
577 did_emsg = FALSE;
578 win_redr_custom(wp, FALSE);
579 if (did_emsg)
580 {
581 // When there is an error disable the statusline, otherwise the
582 // display is messed up with errors and a redraw triggers the problem
583 // again and again.
584 set_string_option_direct((char_u *)"statusline", -1,
585 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
586 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
587 }
588 did_emsg |= saved_did_emsg;
589 entered = FALSE;
590}
591#endif
592
593/*
594 * Show current status info in ruler and various other places
595 * If always is FALSE, only show ruler if position has changed.
596 */
597 void
598showruler(int always)
599{
600 if (!always && !redrawing())
601 return;
602 if (pum_visible())
603 {
604 // Don't redraw right now, do it later.
605 curwin->w_redr_status = TRUE;
606 return;
607 }
608#if defined(FEAT_STL_OPT)
609 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
610 redraw_custom_statusline(curwin);
611 else
612#endif
613#ifdef FEAT_CMDL_INFO
614 win_redr_ruler(curwin, always, FALSE);
615#endif
616
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200617 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000618#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200619 || (p_icon && (stl_syntax & STL_IN_ICON))
620 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000621#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200622 )
623 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000624
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200625 // Redraw the tab pages line if needed.
626 if (redraw_tabline)
627 draw_tabline();
628}
629
630#if defined(FEAT_CMDL_INFO) || defined(PROTO)
631 void
632win_redr_ruler(win_T *wp, int always, int ignore_pum)
633{
634#define RULER_BUF_LEN 70
635 char_u buffer[RULER_BUF_LEN];
636 int row;
637 int fillchar;
638 int attr;
639 int empty_line = FALSE;
640 colnr_T virtcol;
641 int i;
642 size_t len;
643 int o;
644 int this_ru_col;
645 int off = 0;
646 int width;
647
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100648 // If 'ruler' off or messages area disabled, don't do anything
649 if (!p_ru || (wp->w_status_height == 0 && p_ch == 0))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200650 return;
651
652 /*
653 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
654 * after deleting lines, before cursor.lnum is corrected.
655 */
656 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
657 return;
658
659 // Don't draw the ruler while doing insert-completion, it might overwrite
660 // the (long) mode message.
661 if (wp == lastwin && lastwin->w_status_height == 0)
662 if (edit_submode != NULL)
663 return;
664 // Don't draw the ruler when the popup menu is visible, it may overlap.
665 // Except when the popup menu will be redrawn anyway.
666 if (!ignore_pum && pum_visible())
667 return;
668
669#ifdef FEAT_STL_OPT
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100670 if (*p_ruf && p_ch > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200671 {
Bram Moolenaar53989552019-12-23 22:59:18 +0100672 int called_emsg_before = called_emsg;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200673
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200674 win_redr_custom(wp, TRUE);
Bram Moolenaar53989552019-12-23 22:59:18 +0100675 if (called_emsg > called_emsg_before)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200676 set_string_option_direct((char_u *)"rulerformat", -1,
677 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200678 return;
679 }
680#endif
681
682 /*
683 * Check if not in Insert mode and the line is empty (will show "0-1").
684 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100685 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200686 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
687 empty_line = TRUE;
688
689 /*
690 * Only draw the ruler when something changed.
691 */
692 validate_virtcol_win(wp);
693 if ( redraw_cmdline
694 || always
695 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
696 || wp->w_cursor.col != wp->w_ru_cursor.col
697 || wp->w_virtcol != wp->w_ru_virtcol
698 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
699 || wp->w_topline != wp->w_ru_topline
700 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
701#ifdef FEAT_DIFF
702 || wp->w_topfill != wp->w_ru_topfill
703#endif
704 || empty_line != wp->w_ru_empty)
705 {
706 cursor_off();
707 if (wp->w_status_height)
708 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200709 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200710 fillchar = fillchar_status(&attr, wp);
711 off = wp->w_wincol;
712 width = wp->w_width;
713 }
714 else
715 {
716 row = Rows - 1;
717 fillchar = ' ';
718 attr = 0;
719 width = Columns;
720 off = 0;
721 }
722
723 // In list mode virtcol needs to be recomputed
724 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100725 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200726 {
727 wp->w_p_list = FALSE;
728 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
729 wp->w_p_list = TRUE;
730 }
731
732 /*
733 * Some sprintfs return the length, some return a pointer.
734 * To avoid portability problems we use strlen() here.
735 */
736 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
737 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
738 ? 0L
739 : (long)(wp->w_cursor.lnum));
740 len = STRLEN(buffer);
741 col_print(buffer + len, RULER_BUF_LEN - len,
742 empty_line ? 0 : (int)wp->w_cursor.col + 1,
743 (int)virtcol + 1);
744
745 /*
746 * Add a "50%" if there is room for it.
747 * On the last line, don't print in the last column (scrolls the
748 * screen up on some terminals).
749 */
750 i = (int)STRLEN(buffer);
751 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
752 o = i + vim_strsize(buffer + i + 1);
753 if (wp->w_status_height == 0) // can't use last char of screen
754 ++o;
755 this_ru_col = ru_col - (Columns - width);
756 if (this_ru_col < 0)
757 this_ru_col = 0;
758 // Never use more than half the window/screen width, leave the other
759 // half for the filename.
760 if (this_ru_col < (width + 1) / 2)
761 this_ru_col = (width + 1) / 2;
762 if (this_ru_col + o < width)
763 {
764 // need at least 3 chars left for get_rel_pos() + NUL
765 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
766 {
767 if (has_mbyte)
768 i += (*mb_char2bytes)(fillchar, buffer + i);
769 else
770 buffer[i++] = fillchar;
771 ++o;
772 }
773 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
774 }
775 // Truncate at window boundary.
776 if (has_mbyte)
777 {
778 o = 0;
779 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
780 {
781 o += (*mb_ptr2cells)(buffer + i);
782 if (this_ru_col + o > width)
783 {
784 buffer[i] = NUL;
785 break;
786 }
787 }
788 }
789 else if (this_ru_col + (int)STRLEN(buffer) > width)
790 buffer[width - this_ru_col] = NUL;
791
792 screen_puts(buffer, row, this_ru_col + off, attr);
793 i = redraw_cmdline;
794 screen_fill(row, row + 1,
795 this_ru_col + off + (int)STRLEN(buffer),
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000796 (off + width),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200797 fillchar, fillchar, attr);
798 // don't redraw the cmdline because of showing the ruler
799 redraw_cmdline = i;
800 wp->w_ru_cursor = wp->w_cursor;
801 wp->w_ru_virtcol = wp->w_virtcol;
802 wp->w_ru_empty = empty_line;
803 wp->w_ru_topline = wp->w_topline;
804 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
805#ifdef FEAT_DIFF
806 wp->w_ru_topfill = wp->w_topfill;
807#endif
808 }
809}
810#endif
811
812/*
813 * To be called when "updating_screen" was set before and now the postponed
814 * side effects may take place.
815 */
816 void
817after_updating_screen(int may_resize_shell UNUSED)
818{
819 updating_screen = FALSE;
820#ifdef FEAT_GUI
821 if (may_resize_shell)
822 gui_may_resize_shell();
823#endif
824#ifdef FEAT_TERMINAL
825 term_check_channel_closed_recently();
826#endif
827
828#ifdef HAVE_DROP_FILE
829 // If handle_drop() was called while updating_screen was TRUE need to
830 // handle the drop now.
831 handle_any_postponed_drop();
832#endif
833}
834
835/*
836 * Update all windows that are editing the current buffer.
837 */
838 void
839update_curbuf(int type)
840{
841 redraw_curbuf_later(type);
842 update_screen(type);
843}
844
845#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
846/*
847 * Copy "text" to ScreenLines using "attr".
848 * Returns the next screen column.
849 */
850 static int
851text_to_screenline(win_T *wp, char_u *text, int col)
852{
853 int off = (int)(current_ScreenLine - ScreenLines);
854
855 if (has_mbyte)
856 {
857 int cells;
858 int u8c, u8cc[MAX_MCO];
859 int i;
860 int idx;
861 int c_len;
862 char_u *p;
863# ifdef FEAT_ARABIC
864 int prev_c = 0; // previous Arabic character
865 int prev_c1 = 0; // first composing char for prev_c
866# endif
867
868# ifdef FEAT_RIGHTLEFT
869 if (wp->w_p_rl)
870 idx = off;
871 else
872# endif
873 idx = off + col;
874
875 // Store multibyte characters in ScreenLines[] et al. correctly.
876 for (p = text; *p != NUL; )
877 {
878 cells = (*mb_ptr2cells)(p);
879 c_len = (*mb_ptr2len)(p);
880 if (col + cells > wp->w_width
881# ifdef FEAT_RIGHTLEFT
882 - (wp->w_p_rl ? col : 0)
883# endif
884 )
885 break;
886 ScreenLines[idx] = *p;
887 if (enc_utf8)
888 {
889 u8c = utfc_ptr2char(p, u8cc);
890 if (*p < 0x80 && u8cc[0] == 0)
891 {
892 ScreenLinesUC[idx] = 0;
893#ifdef FEAT_ARABIC
894 prev_c = u8c;
895#endif
896 }
897 else
898 {
899#ifdef FEAT_ARABIC
900 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
901 {
902 // Do Arabic shaping.
903 int pc, pc1, nc;
904 int pcc[MAX_MCO];
905 int firstbyte = *p;
906
907 // The idea of what is the previous and next
908 // character depends on 'rightleft'.
909 if (wp->w_p_rl)
910 {
911 pc = prev_c;
912 pc1 = prev_c1;
913 nc = utf_ptr2char(p + c_len);
914 prev_c1 = u8cc[0];
915 }
916 else
917 {
918 pc = utfc_ptr2char(p + c_len, pcc);
919 nc = prev_c;
920 pc1 = pcc[0];
921 }
922 prev_c = u8c;
923
924 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
925 pc, pc1, nc);
926 ScreenLines[idx] = firstbyte;
927 }
928 else
929 prev_c = u8c;
930#endif
931 // Non-BMP character: display as ? or fullwidth ?.
932 ScreenLinesUC[idx] = u8c;
933 for (i = 0; i < Screen_mco; ++i)
934 {
935 ScreenLinesC[i][idx] = u8cc[i];
936 if (u8cc[i] == 0)
937 break;
938 }
939 }
940 if (cells > 1)
941 ScreenLines[idx + 1] = 0;
942 }
943 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
944 // double-byte single width character
945 ScreenLines2[idx] = p[1];
946 else if (cells > 1)
947 // double-width character
948 ScreenLines[idx + 1] = p[1];
949 col += cells;
950 idx += cells;
951 p += c_len;
952 }
953 }
954 else
955 {
956 int len = (int)STRLEN(text);
957
958 if (len > wp->w_width - col)
959 len = wp->w_width - col;
960 if (len > 0)
961 {
962#ifdef FEAT_RIGHTLEFT
963 if (wp->w_p_rl)
964 mch_memmove(current_ScreenLine, text, len);
965 else
966#endif
967 mch_memmove(current_ScreenLine + col, text, len);
968 col += len;
969 }
970 }
971 return col;
972}
973#endif
974
975#ifdef FEAT_MENU
976/*
977 * Draw the window toolbar.
978 */
979 static void
980redraw_win_toolbar(win_T *wp)
981{
982 vimmenu_T *menu;
983 int item_idx = 0;
984 int item_count = 0;
985 int col = 0;
986 int next_col;
987 int off = (int)(current_ScreenLine - ScreenLines);
988 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
989 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
990
991 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200992 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200993 ++item_count;
994 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
995
996 // TODO: use fewer spaces if there is not enough room
997 for (menu = wp->w_winbar->children;
998 menu != NULL && col < wp->w_width; menu = menu->next)
999 {
1000 space_to_screenline(off + col, fill_attr);
1001 if (++col >= wp->w_width)
1002 break;
1003 if (col > 1)
1004 {
1005 space_to_screenline(off + col, fill_attr);
1006 if (++col >= wp->w_width)
1007 break;
1008 }
1009
1010 wp->w_winbar_items[item_idx].wb_startcol = col;
1011 space_to_screenline(off + col, button_attr);
1012 if (++col >= wp->w_width)
1013 break;
1014
1015 next_col = text_to_screenline(wp, menu->name, col);
1016 while (col < next_col)
1017 {
1018 ScreenAttrs[off + col] = button_attr;
1019 ++col;
1020 }
1021 wp->w_winbar_items[item_idx].wb_endcol = col;
1022 wp->w_winbar_items[item_idx].wb_menu = menu;
1023 ++item_idx;
1024
1025 if (col >= wp->w_width)
1026 break;
1027 space_to_screenline(off + col, button_attr);
1028 ++col;
1029 }
1030 while (col < wp->w_width)
1031 {
1032 space_to_screenline(off + col, fill_attr);
1033 ++col;
1034 }
1035 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1036
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001037 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001038}
1039#endif
1040
1041#if defined(FEAT_FOLDING) || defined(PROTO)
1042/*
1043 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1044 */
1045 static void
1046copy_text_attr(
1047 int off,
1048 char_u *buf,
1049 int len,
1050 int attr)
1051{
1052 int i;
1053
1054 mch_memmove(ScreenLines + off, buf, (size_t)len);
1055 if (enc_utf8)
1056 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1057 for (i = 0; i < len; ++i)
1058 ScreenAttrs[off + i] = attr;
1059}
1060
1061/*
1062 * Display one folded line.
1063 */
1064 static void
1065fold_line(
1066 win_T *wp,
1067 long fold_count,
1068 foldinfo_T *foldinfo,
1069 linenr_T lnum,
1070 int row)
1071{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001072 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1073 // multi-byte character is MAX_MCO.
1074 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001075 pos_T *top, *bot;
1076 linenr_T lnume = lnum + fold_count - 1;
1077 int len;
1078 char_u *text;
1079 int fdc;
1080 int col;
1081 int txtcol;
1082 int off = (int)(current_ScreenLine - ScreenLines);
1083 int ri;
1084
1085 // Build the fold line:
1086 // 1. Add the cmdwin_type for the command-line window
1087 // 2. Add the 'foldcolumn'
1088 // 3. Add the 'number' or 'relativenumber' column
1089 // 4. Compose the text
1090 // 5. Add the text
1091 // 6. set highlighting for the Visual area an other text
1092 col = 0;
1093
1094 // 1. Add the cmdwin_type for the command-line window
1095 // Ignores 'rightleft', this window is never right-left.
1096#ifdef FEAT_CMDWIN
1097 if (cmdwin_type != 0 && wp == curwin)
1098 {
1099 ScreenLines[off] = cmdwin_type;
1100 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1101 if (enc_utf8)
1102 ScreenLinesUC[off] = 0;
1103 ++col;
1104 }
1105#endif
1106
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001107#ifdef FEAT_RIGHTLEFT
1108# define RL_MEMSET(p, v, l) \
1109 do { \
1110 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001111 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001112 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1113 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001114 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001115 ScreenAttrs[off + (p) + ri] = v; \
1116 } while (0)
1117#else
1118# define RL_MEMSET(p, v, l) \
1119 do { \
1120 for (ri = 0; ri < l; ++ri) \
1121 ScreenAttrs[off + (p) + ri] = v; \
1122 } while (0)
1123#endif
1124
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001125 // 2. Add the 'foldcolumn'
1126 // Reduce the width when there is not enough space.
1127 fdc = compute_foldcolumn(wp, col);
1128 if (fdc > 0)
1129 {
1130 char_u *p;
1131 int i;
1132 int idx;
1133
1134 fill_foldcolumn(buf, wp, TRUE, lnum);
1135 p = buf;
1136 for (i = 0; i < fdc; i++)
1137 {
1138 int ch;
1139
1140 if (has_mbyte)
1141 ch = mb_ptr2char_adv(&p);
1142 else
1143 ch = *p++;
1144#ifdef FEAT_RIGHTLEFT
1145 if (wp->w_p_rl)
1146 idx = off + wp->w_width - i - 1 - col;
1147 else
1148#endif
1149 idx = off + col + i;
1150 if (enc_utf8)
1151 {
1152 if (ch >= 0x80)
1153 {
1154 ScreenLinesUC[idx] = ch;
1155 ScreenLinesC[0][idx] = 0;
1156 ScreenLines[idx] = 0x80;
1157 }
1158 else
1159 {
1160 ScreenLines[idx] = ch;
1161 ScreenLinesUC[idx] = 0;
1162 }
1163 }
1164 else
1165 ScreenLines[idx] = ch;
1166 }
1167
1168 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1169 col += fdc;
1170 }
1171
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001172 // Set all attributes of the 'number' or 'relativenumber' column and the
1173 // text
1174 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1175
1176#ifdef FEAT_SIGNS
1177 // If signs are being displayed, add two spaces.
1178 if (signcolumn_on(wp))
1179 {
1180 len = wp->w_width - col;
1181 if (len > 0)
1182 {
1183 if (len > 2)
1184 len = 2;
1185# ifdef FEAT_RIGHTLEFT
1186 if (wp->w_p_rl)
1187 // the line number isn't reversed
1188 copy_text_attr(off + wp->w_width - len - col,
1189 (char_u *)" ", len, HL_ATTR(HLF_FL));
1190 else
1191# endif
1192 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1193 col += len;
1194 }
1195 }
1196#endif
1197
1198 // 3. Add the 'number' or 'relativenumber' column
1199 if (wp->w_p_nu || wp->w_p_rnu)
1200 {
1201 len = wp->w_width - col;
1202 if (len > 0)
1203 {
1204 int w = number_width(wp);
1205 long num;
1206 char *fmt = "%*ld ";
1207
1208 if (len > w + 1)
1209 len = w + 1;
1210
1211 if (wp->w_p_nu && !wp->w_p_rnu)
1212 // 'number' + 'norelativenumber'
1213 num = (long)lnum;
1214 else
1215 {
1216 // 'relativenumber', don't use negative numbers
1217 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1218 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1219 {
1220 // 'number' + 'relativenumber': cursor line shows absolute
1221 // line number
1222 num = lnum;
1223 fmt = "%-*ld ";
1224 }
1225 }
1226
1227 sprintf((char *)buf, fmt, w, num);
1228#ifdef FEAT_RIGHTLEFT
1229 if (wp->w_p_rl)
1230 // the line number isn't reversed
1231 copy_text_attr(off + wp->w_width - len - col, buf, len,
1232 HL_ATTR(HLF_FL));
1233 else
1234#endif
1235 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1236 col += len;
1237 }
1238 }
1239
1240 // 4. Compose the folded-line string with 'foldtext', if set.
1241 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1242
1243 txtcol = col; // remember where text starts
1244
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001245 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1246 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001247 // Right-left text is put in columns 0 - number-col, normal text is put
1248 // in columns number-col - window-width.
1249 col = text_to_screenline(wp, text, col);
1250
1251 // Fill the rest of the line with the fold filler
1252#ifdef FEAT_RIGHTLEFT
1253 if (wp->w_p_rl)
1254 col -= txtcol;
1255#endif
1256 while (col < wp->w_width
1257#ifdef FEAT_RIGHTLEFT
1258 - (wp->w_p_rl ? txtcol : 0)
1259#endif
1260 )
1261 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001262 int c = wp->w_fill_chars.fold;
1263
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001264 if (enc_utf8)
1265 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001266 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001267 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001268 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001269 ScreenLinesC[0][off + col] = 0;
1270 ScreenLines[off + col] = 0x80; // avoid storing zero
1271 }
1272 else
1273 {
1274 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001275 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001276 }
1277 col++;
1278 }
1279 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001280 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001281 }
1282
1283 if (text != buf)
1284 vim_free(text);
1285
1286 // 6. set highlighting for the Visual area an other text.
1287 // If all folded lines are in the Visual area, highlight the line.
1288 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1289 {
1290 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1291 {
1292 // Visual is after curwin->w_cursor
1293 top = &curwin->w_cursor;
1294 bot = &VIsual;
1295 }
1296 else
1297 {
1298 // Visual is before curwin->w_cursor
1299 top = &VIsual;
1300 bot = &curwin->w_cursor;
1301 }
1302 if (lnum >= top->lnum
1303 && lnume <= bot->lnum
1304 && (VIsual_mode != 'v'
1305 || ((lnum > top->lnum
1306 || (lnum == top->lnum
1307 && top->col == 0))
1308 && (lnume < bot->lnum
1309 || (lnume == bot->lnum
1310 && (bot->col - (*p_sel == 'e'))
1311 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
1312 {
1313 if (VIsual_mode == Ctrl_V)
1314 {
1315 // Visual block mode: highlight the chars part of the block
1316 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1317 {
1318 if (wp->w_old_cursor_lcol != MAXCOL
1319 && wp->w_old_cursor_lcol + txtcol
1320 < (colnr_T)wp->w_width)
1321 len = wp->w_old_cursor_lcol;
1322 else
1323 len = wp->w_width - txtcol;
1324 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1325 len - (int)wp->w_old_cursor_fcol);
1326 }
1327 }
1328 else
1329 {
1330 // Set all attributes of the text
1331 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1332 }
1333 }
1334 }
1335
1336#ifdef FEAT_SYN_HL
1337 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1338 if (wp->w_p_cc_cols)
1339 {
1340 int i = 0;
1341 int j = wp->w_p_cc_cols[i];
1342 int old_txtcol = txtcol;
1343
1344 while (j > -1)
1345 {
1346 txtcol += j;
1347 if (wp->w_p_wrap)
1348 txtcol -= wp->w_skipcol;
1349 else
1350 txtcol -= wp->w_leftcol;
1351 if (txtcol >= 0 && txtcol < wp->w_width)
1352 ScreenAttrs[off + txtcol] = hl_combine_attr(
1353 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1354 txtcol = old_txtcol;
1355 j = wp->w_p_cc_cols[++i];
1356 }
1357 }
1358
1359 // Show 'cursorcolumn' in the fold line.
1360 if (wp->w_p_cuc)
1361 {
1362 txtcol += wp->w_virtcol;
1363 if (wp->w_p_wrap)
1364 txtcol -= wp->w_skipcol;
1365 else
1366 txtcol -= wp->w_leftcol;
1367 if (txtcol >= 0 && txtcol < wp->w_width)
1368 ScreenAttrs[off + txtcol] = hl_combine_attr(
1369 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1370 }
1371#endif
1372
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001373 screen_line(wp, row + W_WINROW(wp), wp->w_wincol,
1374 wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001375
1376 // Update w_cline_height and w_cline_folded if the cursor line was
1377 // updated (saves a call to plines() later).
1378 if (wp == curwin
1379 && lnum <= curwin->w_cursor.lnum
1380 && lnume >= curwin->w_cursor.lnum)
1381 {
1382 curwin->w_cline_row = row;
1383 curwin->w_cline_height = 1;
1384 curwin->w_cline_folded = TRUE;
1385 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1386 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001387
1388# ifdef FEAT_CONCEAL
1389 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001390 if (wp == curwin
1391 && wp->w_cursor.lnum >= lnum
1392 && wp->w_cursor.lnum <= lnume
1393 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001394 curs_columns(TRUE);
1395# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001396}
1397#endif
1398
1399/*
1400 * Update a single window.
1401 *
1402 * This may cause the windows below it also to be redrawn (when clearing the
1403 * screen or scrolling lines).
1404 *
1405 * How the window is redrawn depends on wp->w_redr_type. Each type also
1406 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001407 * UPD_NOT_VALID redraw the whole window
1408 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1409 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1410 * UPD_VALID
1411 * UPD_INVERTED redraw the changed part of the Visual area
1412 * UPD_INVERTED_ALL redraw the whole Visual area
1413 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001414 * 2. update lines at the top when scrolled down
1415 * 3. redraw changed text:
1416 * - if wp->w_buffer->b_mod_set set, update lines between
1417 * b_mod_top and b_mod_bot.
1418 * - if wp->w_redraw_top non-zero, redraw lines between
1419 * wp->w_redraw_top and wp->w_redr_bot.
1420 * - continue redrawing when syntax status is invalid.
1421 * 4. if scrolled up, update lines at the bottom.
1422 * This results in three areas that may need updating:
1423 * top: from first row to top_end (when scrolled down)
1424 * mid: from mid_start to mid_end (update inversion or changed text)
1425 * bot: from bot_start to last row (when scrolled up)
1426 */
1427 static void
1428win_update(win_T *wp)
1429{
1430 buf_T *buf = wp->w_buffer;
1431 int type;
1432 int top_end = 0; // Below last row of the top area that needs
1433 // updating. 0 when no top area updating.
1434 int mid_start = 999;// first row of the mid area that needs
1435 // updating. 999 when no mid area updating.
1436 int mid_end = 0; // Below last row of the mid area that needs
1437 // updating. 0 when no mid area updating.
1438 int bot_start = 999;// first row of the bot area that needs
1439 // updating. 999 when no bot area updating
1440 int scrolled_down = FALSE; // TRUE when scrolled down when
1441 // w_topline got smaller a bit
1442#ifdef FEAT_SEARCH_EXTRA
1443 int top_to_mod = FALSE; // redraw above mod_top
1444#endif
1445
1446 int row; // current window row to display
1447 linenr_T lnum; // current buffer lnum to display
1448 int idx; // current index in w_lines[]
1449 int srow; // starting row of the current line
1450
1451 int eof = FALSE; // if TRUE, we hit the end of the file
1452 int didline = FALSE; // if TRUE, we finished the last line
1453 int i;
1454 long j;
1455 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001456 linenr_T old_botline = wp->w_botline;
1457#ifdef FEAT_CONCEAL
1458 int old_wrow = wp->w_wrow;
1459 int old_wcol = wp->w_wcol;
1460#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001461#ifdef FEAT_FOLDING
1462 long fold_count;
1463#endif
1464#ifdef FEAT_SYN_HL
1465 // remember what happened to the previous line, to know if
1466 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001467# define DID_NONE 1 // didn't update a line
1468# define DID_LINE 2 // updated a normal line
1469# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001470 int did_update = DID_NONE;
1471 linenr_T syntax_last_parsed = 0; // last parsed text line
1472#endif
1473 linenr_T mod_top = 0;
1474 linenr_T mod_bot = 0;
1475#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1476 int save_got_int;
1477#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001478
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001479#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1480 // This needs to be done only for the first window when update_screen() is
1481 // called.
1482 if (!did_update_one_window)
1483 {
1484 did_update_one_window = TRUE;
1485# ifdef FEAT_SEARCH_EXTRA
1486 start_search_hl();
1487# endif
1488# ifdef FEAT_CLIPBOARD
1489 // When Visual area changed, may have to update selection.
1490 if (clip_star.available && clip_isautosel_star())
1491 clip_update_selection(&clip_star);
1492 if (clip_plus.available && clip_isautosel_plus())
1493 clip_update_selection(&clip_plus);
1494# endif
1495 }
1496#endif
1497
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001498 type = wp->w_redr_type;
1499
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001500 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001501 {
1502 wp->w_redr_status = TRUE;
1503 wp->w_lines_valid = 0;
1504 }
1505
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001506 // Window frame is zero-height: nothing to draw.
1507 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1508 || (wp->w_frame->fr_height == wp->w_status_height
1509#if defined(FEAT_PROP_POPUP)
1510 && !popup_is_popup(wp)
1511#endif
1512 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001513 {
1514 wp->w_redr_type = 0;
1515 return;
1516 }
1517
1518 // Window is zero-width: Only need to draw the separator.
1519 if (wp->w_width == 0)
1520 {
1521 // draw the vertical separator right of this window
1522 draw_vsep_win(wp, 0);
1523 wp->w_redr_type = 0;
1524 return;
1525 }
1526
1527#ifdef FEAT_TERMINAL
1528 // If this window contains a terminal, redraw works completely differently.
1529 if (term_do_update_window(wp))
1530 {
1531 term_update_window(wp);
1532# ifdef FEAT_MENU
1533 // Draw the window toolbar, if there is one.
1534 if (winbar_height(wp) > 0)
1535 redraw_win_toolbar(wp);
1536# endif
1537 wp->w_redr_type = 0;
1538 return;
1539 }
1540#endif
1541
1542#ifdef FEAT_SEARCH_EXTRA
1543 init_search_hl(wp, &screen_search_hl);
1544#endif
1545
1546#ifdef FEAT_LINEBREAK
1547 // Force redraw when width of 'number' or 'relativenumber' column
1548 // changes.
1549 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1550 if (wp->w_nrwidth != i)
1551 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001552 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001553 wp->w_nrwidth = i;
1554 }
1555 else
1556#endif
1557
1558 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1559 {
1560 // When there are both inserted/deleted lines and specific lines to be
1561 // redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1562 // everything (only happens when redrawing is off for while).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001563 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001564 }
1565 else
1566 {
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 {
1608 if (cur->match.regprog != NULL
1609 && re_multiline(cur->match.regprog))
1610 {
1611 top_to_mod = TRUE;
1612 break;
1613 }
1614 cur = cur->next;
1615 }
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 }
1690
1691 // When line numbers are displayed need to redraw all lines below
1692 // inserted/deleted lines.
1693 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1694 mod_bot = MAXLNUM;
1695 }
1696 wp->w_redraw_top = 0; // reset for next time
1697 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001698#ifdef FEAT_SEARCH_EXTRA
1699 search_hl_has_cursor_lnum = 0;
1700#endif
1701
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001702
1703 // When only displaying the lines at the top, set top_end. Used when
1704 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001705 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001706 {
1707 j = 0;
1708 for (i = 0; i < wp->w_lines_valid; ++i)
1709 {
1710 j += wp->w_lines[i].wl_size;
1711 if (j >= wp->w_upd_rows)
1712 {
1713 top_end = j;
1714 break;
1715 }
1716 }
1717 if (top_end == 0)
1718 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001719 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001720 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001721 // top area defined, the rest is UPD_VALID
1722 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001723 }
1724
1725 // Trick: we want to avoid clearing the screen twice. screenclear() will
1726 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1727 // non-zero and thus not FALSE) will indicate that screenclear() was not
1728 // called.
1729 if (screen_cleared)
1730 screen_cleared = MAYBE;
1731
1732 // If there are no changes on the screen that require a complete redraw,
1733 // handle three cases:
1734 // 1: we are off the top of the screen by a few lines: scroll down
1735 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1736 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1737 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001738 if ((type == UPD_VALID || type == UPD_SOME_VALID
1739 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001740#ifdef FEAT_DIFF
1741 && !wp->w_botfill && !wp->w_old_botfill
1742#endif
1743 )
1744 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001745 if (mod_top != 0
1746 && wp->w_topline == mod_top
1747 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001748 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001749 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001750 // w_topline is the first changed line and window is not scrolled,
1751 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001752 }
1753 else if (wp->w_lines[0].wl_valid
1754 && (wp->w_topline < wp->w_lines[0].wl_lnum
1755#ifdef FEAT_DIFF
1756 || (wp->w_topline == wp->w_lines[0].wl_lnum
1757 && wp->w_topfill > wp->w_old_topfill)
1758#endif
1759 ))
1760 {
1761 // New topline is above old topline: May scroll down.
1762#ifdef FEAT_FOLDING
1763 if (hasAnyFolding(wp))
1764 {
1765 linenr_T ln;
1766
1767 // count the number of lines we are off, counting a sequence
1768 // of folded lines as one
1769 j = 0;
1770 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1771 {
1772 ++j;
1773 if (j >= wp->w_height - 2)
1774 break;
1775 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1776 }
1777 }
1778 else
1779#endif
1780 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1781 if (j < wp->w_height - 2) // not too far off
1782 {
1783 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1784#ifdef FEAT_DIFF
1785 // insert extra lines for previously invisible filler lines
1786 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1787 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1788 - wp->w_old_topfill;
1789#endif
1790 if (i < wp->w_height - 2) // less than a screen off
1791 {
1792 // Try to insert the correct number of lines.
1793 // If not the last window, delete the lines at the bottom.
1794 // win_ins_lines may fail when the terminal can't do it.
1795 if (i > 0)
1796 check_for_delay(FALSE);
1797 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1798 {
1799 if (wp->w_lines_valid != 0)
1800 {
1801 // Need to update rows that are new, stop at the
1802 // first one that scrolled down.
1803 top_end = i;
1804 scrolled_down = TRUE;
1805
1806 // Move the entries that were scrolled, disable
1807 // the entries for the lines to be redrawn.
Bram Moolenaarfdc5d172022-08-11 15:52:14 +01001808 // Avoid using a wrong index when 'cmdheight' is
1809 // zero and wp->w_height == Rows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001810 if ((wp->w_lines_valid += j) > wp->w_height)
1811 wp->w_lines_valid = wp->w_height;
Bram Moolenaarfdc5d172022-08-11 15:52:14 +01001812 for (idx = wp->w_lines_valid >= wp->w_height
1813 ? wp->w_height - 1 : wp->w_lines_valid;
1814 idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001815 wp->w_lines[idx] = wp->w_lines[idx - j];
1816 while (idx >= 0)
1817 wp->w_lines[idx--].wl_valid = FALSE;
1818 }
1819 }
1820 else
1821 mid_start = 0; // redraw all lines
1822 }
1823 else
1824 mid_start = 0; // redraw all lines
1825 }
1826 else
1827 mid_start = 0; // redraw all lines
1828 }
1829 else
1830 {
1831 // New topline is at or below old topline: May scroll up.
1832 // When topline didn't change, find first entry in w_lines[] that
1833 // needs updating.
1834
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001835 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1836 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001837 j = -1;
1838 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001839 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001840 {
1841 if (wp->w_lines[i].wl_valid
1842 && wp->w_lines[i].wl_lnum == wp->w_topline)
1843 {
1844 j = i;
1845 break;
1846 }
1847 row += wp->w_lines[i].wl_size;
1848 }
1849 if (j == -1)
1850 {
1851 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1852 // lines
1853 mid_start = 0;
1854 }
1855 else
1856 {
1857 // Try to delete the correct number of lines.
1858 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1859#ifdef FEAT_DIFF
1860 // If the topline didn't change, delete old filler lines,
1861 // otherwise delete filler lines of the new topline...
1862 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1863 row += wp->w_old_topfill;
1864 else
1865 row += diff_check_fill(wp, wp->w_topline);
1866 // ... but don't delete new filler lines.
1867 row -= wp->w_topfill;
1868#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001869 if (row > Rows) // just in case
1870 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001871 if (row > 0)
1872 {
1873 check_for_delay(FALSE);
1874 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1875 == OK)
1876 bot_start = wp->w_height - row;
1877 else
1878 mid_start = 0; // redraw all lines
1879 }
1880 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1881 {
1882 // Skip the lines (below the deleted lines) that are still
1883 // valid and don't need redrawing. Copy their info
1884 // upwards, to compensate for the deleted lines. Set
1885 // bot_start to the first row that needs redrawing.
1886 bot_start = 0;
1887 idx = 0;
1888 for (;;)
1889 {
1890 wp->w_lines[idx] = wp->w_lines[j];
1891 // stop at line that didn't fit, unless it is still
1892 // valid (no lines deleted)
1893 if (row > 0 && bot_start + row
1894 + (int)wp->w_lines[j].wl_size > wp->w_height)
1895 {
1896 wp->w_lines_valid = idx + 1;
1897 break;
1898 }
1899 bot_start += wp->w_lines[idx++].wl_size;
1900
1901 // stop at the last valid entry in w_lines[].wl_size
1902 if (++j >= wp->w_lines_valid)
1903 {
1904 wp->w_lines_valid = idx;
1905 break;
1906 }
1907 }
1908#ifdef FEAT_DIFF
1909 // Correct the first entry for filler lines at the top
1910 // when it won't get updated below.
1911 if (wp->w_p_diff && bot_start > 0)
1912 wp->w_lines[0].wl_size =
1913 plines_win_nofill(wp, wp->w_topline, TRUE)
1914 + wp->w_topfill;
1915#endif
1916 }
1917 }
1918 }
1919
1920 // When starting redraw in the first line, redraw all lines. When
1921 // there is only one window it's probably faster to clear the screen
1922 // first.
1923 if (mid_start == 0)
1924 {
1925 mid_end = wp->w_height;
1926 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
1927 {
1928 // Clear the screen when it was not done by win_del_lines() or
1929 // win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1930 // then.
1931 if (screen_cleared != TRUE)
1932 screenclear();
1933 // The screen was cleared, redraw the tab pages line.
1934 if (redraw_tabline)
1935 draw_tabline();
1936 }
1937 }
1938
1939 // When win_del_lines() or win_ins_lines() caused the screen to be
1940 // cleared (only happens for the first window) or when screenclear()
1941 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001942 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001943 if (screen_cleared == TRUE)
1944 must_redraw = 0;
1945 }
1946 else
1947 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001948 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001949 mid_start = 0;
1950 mid_end = wp->w_height;
1951 }
1952
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001953 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001954 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001955 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001956 mid_start = 0;
1957 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001958 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001959 }
1960
1961 // check if we are updating or removing the inverted part
1962 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001963 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001964 {
1965 linenr_T from, to;
1966
1967 if (VIsual_active)
1968 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001969 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001970 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001971 {
1972 // If the type of Visual selection changed, redraw the whole
1973 // selection. Also when the ownership of the X selection is
1974 // gained or lost.
1975 if (curwin->w_cursor.lnum < VIsual.lnum)
1976 {
1977 from = curwin->w_cursor.lnum;
1978 to = VIsual.lnum;
1979 }
1980 else
1981 {
1982 from = VIsual.lnum;
1983 to = curwin->w_cursor.lnum;
1984 }
1985 // redraw more when the cursor moved as well
1986 if (wp->w_old_cursor_lnum < from)
1987 from = wp->w_old_cursor_lnum;
1988 if (wp->w_old_cursor_lnum > to)
1989 to = wp->w_old_cursor_lnum;
1990 if (wp->w_old_visual_lnum < from)
1991 from = wp->w_old_visual_lnum;
1992 if (wp->w_old_visual_lnum > to)
1993 to = wp->w_old_visual_lnum;
1994 }
1995 else
1996 {
1997 // Find the line numbers that need to be updated: The lines
1998 // between the old cursor position and the current cursor
1999 // position. Also check if the Visual position changed.
2000 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
2001 {
2002 from = curwin->w_cursor.lnum;
2003 to = wp->w_old_cursor_lnum;
2004 }
2005 else
2006 {
2007 from = wp->w_old_cursor_lnum;
2008 to = curwin->w_cursor.lnum;
2009 if (from == 0) // Visual mode just started
2010 from = to;
2011 }
2012
2013 if (VIsual.lnum != wp->w_old_visual_lnum
2014 || VIsual.col != wp->w_old_visual_col)
2015 {
2016 if (wp->w_old_visual_lnum < from
2017 && wp->w_old_visual_lnum != 0)
2018 from = wp->w_old_visual_lnum;
2019 if (wp->w_old_visual_lnum > to)
2020 to = wp->w_old_visual_lnum;
2021 if (VIsual.lnum < from)
2022 from = VIsual.lnum;
2023 if (VIsual.lnum > to)
2024 to = VIsual.lnum;
2025 }
2026 }
2027
2028 // If in block mode and changed column or curwin->w_curswant:
2029 // update all lines.
2030 // First compute the actual start and end column.
2031 if (VIsual_mode == Ctrl_V)
2032 {
2033 colnr_T fromc, toc;
2034#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002035 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002036
2037 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002038 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002039#endif
2040 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002041 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002042#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002043 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002044#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002045 // Highlight to the end of the line, unless 'virtualedit' has
2046 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002047 if (curwin->w_curswant == MAXCOL)
2048 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002049 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002050 {
2051 pos_T pos;
2052 int cursor_above =
2053 curwin->w_cursor.lnum < VIsual.lnum;
2054
2055 // Need to find the longest line.
2056 toc = 0;
2057 pos.coladd = 0;
2058 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2059 ? pos.lnum <= VIsual.lnum
2060 : pos.lnum >= VIsual.lnum;
2061 pos.lnum += cursor_above ? 1 : -1)
2062 {
2063 colnr_T t;
2064
Bram Moolenaar6bcb1822021-07-09 15:54:00 +02002065 pos.col = (int)STRLEN(ml_get_buf(wp->w_buffer,
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002066 pos.lnum, FALSE));
2067 getvvcol(wp, &pos, NULL, NULL, &t);
2068 if (toc < t)
2069 toc = t;
2070 }
2071 ++toc;
2072 }
2073 else
2074 toc = MAXCOL;
2075 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002076
2077 if (fromc != wp->w_old_cursor_fcol
2078 || toc != wp->w_old_cursor_lcol)
2079 {
2080 if (from > VIsual.lnum)
2081 from = VIsual.lnum;
2082 if (to < VIsual.lnum)
2083 to = VIsual.lnum;
2084 }
2085 wp->w_old_cursor_fcol = fromc;
2086 wp->w_old_cursor_lcol = toc;
2087 }
2088 }
2089 else
2090 {
2091 // Use the line numbers of the old Visual area.
2092 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2093 {
2094 from = wp->w_old_cursor_lnum;
2095 to = wp->w_old_visual_lnum;
2096 }
2097 else
2098 {
2099 from = wp->w_old_visual_lnum;
2100 to = wp->w_old_cursor_lnum;
2101 }
2102 }
2103
2104 // There is no need to update lines above the top of the window.
2105 if (from < wp->w_topline)
2106 from = wp->w_topline;
2107
2108 // If we know the value of w_botline, use it to restrict the update to
2109 // the lines that are visible in the window.
2110 if (wp->w_valid & VALID_BOTLINE)
2111 {
2112 if (from >= wp->w_botline)
2113 from = wp->w_botline - 1;
2114 if (to >= wp->w_botline)
2115 to = wp->w_botline - 1;
2116 }
2117
2118 // Find the minimal part to be updated.
2119 // Watch out for scrolling that made entries in w_lines[] invalid.
2120 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2121 // top_end; need to redraw from top_end to the "to" line.
2122 // A middle mouse click with a Visual selection may change the text
2123 // above the Visual area and reset wl_valid, do count these for
2124 // mid_end (in srow).
2125 if (mid_start > 0)
2126 {
2127 lnum = wp->w_topline;
2128 idx = 0;
2129 srow = 0;
2130 if (scrolled_down)
2131 mid_start = top_end;
2132 else
2133 mid_start = 0;
2134 while (lnum < from && idx < wp->w_lines_valid) // find start
2135 {
2136 if (wp->w_lines[idx].wl_valid)
2137 mid_start += wp->w_lines[idx].wl_size;
2138 else if (!scrolled_down)
2139 srow += wp->w_lines[idx].wl_size;
2140 ++idx;
2141# ifdef FEAT_FOLDING
2142 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2143 lnum = wp->w_lines[idx].wl_lnum;
2144 else
2145# endif
2146 ++lnum;
2147 }
2148 srow += mid_start;
2149 mid_end = wp->w_height;
2150 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2151 {
2152 if (wp->w_lines[idx].wl_valid
2153 && wp->w_lines[idx].wl_lnum >= to + 1)
2154 {
2155 // Only update until first row of this line
2156 mid_end = srow;
2157 break;
2158 }
2159 srow += wp->w_lines[idx].wl_size;
2160 }
2161 }
2162 }
2163
2164 if (VIsual_active && buf == curwin->w_buffer)
2165 {
2166 wp->w_old_visual_mode = VIsual_mode;
2167 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2168 wp->w_old_visual_lnum = VIsual.lnum;
2169 wp->w_old_visual_col = VIsual.col;
2170 wp->w_old_curswant = curwin->w_curswant;
2171 }
2172 else
2173 {
2174 wp->w_old_visual_mode = 0;
2175 wp->w_old_cursor_lnum = 0;
2176 wp->w_old_visual_lnum = 0;
2177 wp->w_old_visual_col = 0;
2178 }
2179
2180#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2181 // reset got_int, otherwise regexp won't work
2182 save_got_int = got_int;
2183 got_int = 0;
2184#endif
2185#ifdef SYN_TIME_LIMIT
2186 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002187 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002188 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002189#endif
2190#ifdef FEAT_FOLDING
2191 win_foldinfo.fi_level = 0;
2192#endif
2193
2194#ifdef FEAT_MENU
2195 // Draw the window toolbar, if there is one.
2196 // TODO: only when needed.
2197 if (winbar_height(wp) > 0)
2198 redraw_win_toolbar(wp);
2199#endif
2200
2201 // Update all the window rows.
2202 idx = 0; // first entry in w_lines[].wl_size
2203 row = 0;
2204 srow = 0;
2205 lnum = wp->w_topline; // first line shown in window
2206 for (;;)
2207 {
2208 // stop updating when reached the end of the window (check for _past_
2209 // the end of the window is at the end of the loop)
2210 if (row == wp->w_height)
2211 {
2212 didline = TRUE;
2213 break;
2214 }
2215
2216 // stop updating when hit the end of the file
2217 if (lnum > buf->b_ml.ml_line_count)
2218 {
2219 eof = TRUE;
2220 break;
2221 }
2222
2223 // Remember the starting row of the line that is going to be dealt
2224 // with. It is used further down when the line doesn't fit.
2225 srow = row;
2226
2227 // Update a line when it is in an area that needs updating, when it
2228 // has changes or w_lines[idx] is invalid.
2229 // "bot_start" may be halfway a wrapped line after using
2230 // win_del_lines(), check if the current line includes it.
2231 // When syntax folding is being used, the saved syntax states will
2232 // already have been updated, we can't see where the syntax state is
2233 // the same again, just update until the end of the window.
2234 if (row < top_end
2235 || (row >= mid_start && row < mid_end)
2236#ifdef FEAT_SEARCH_EXTRA
2237 || top_to_mod
2238#endif
2239 || idx >= wp->w_lines_valid
2240 || (row + wp->w_lines[idx].wl_size > bot_start)
2241 || (mod_top != 0
2242 && (lnum == mod_top
2243 || (lnum >= mod_top
2244 && (lnum < mod_bot
2245#ifdef FEAT_SYN_HL
2246 || did_update == DID_FOLD
2247 || (did_update == DID_LINE
2248 && syntax_present(wp)
2249 && (
2250# ifdef FEAT_FOLDING
2251 (foldmethodIsSyntax(wp)
2252 && hasAnyFolding(wp)) ||
2253# endif
2254 syntax_check_changed(lnum)))
2255#endif
2256#ifdef FEAT_SEARCH_EXTRA
2257 // match in fixed position might need redraw
2258 // if lines were inserted or deleted
2259 || (wp->w_match_head != NULL
2260 && buf->b_mod_xlines != 0)
2261#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002262 ))))
2263#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002264 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2265 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002266#endif
2267 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002268 {
2269#ifdef FEAT_SEARCH_EXTRA
2270 if (lnum == mod_top)
2271 top_to_mod = FALSE;
2272#endif
2273
2274 // When at start of changed lines: May scroll following lines
2275 // up or down to minimize redrawing.
2276 // Don't do this when the change continues until the end.
2277 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002278 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002279 if (lnum == mod_top
2280 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002281 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2282 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002283 {
2284 int old_rows = 0;
2285 int new_rows = 0;
2286 int xtra_rows;
2287 linenr_T l;
2288
2289 // Count the old number of window rows, using w_lines[], which
2290 // should still contain the sizes for the lines as they are
2291 // currently displayed.
2292 for (i = idx; i < wp->w_lines_valid; ++i)
2293 {
2294 // Only valid lines have a meaningful wl_lnum. Invalid
2295 // lines are part of the changed area.
2296 if (wp->w_lines[i].wl_valid
2297 && wp->w_lines[i].wl_lnum == mod_bot)
2298 break;
2299 old_rows += wp->w_lines[i].wl_size;
2300#ifdef FEAT_FOLDING
2301 if (wp->w_lines[i].wl_valid
2302 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2303 {
2304 // Must have found the last valid entry above mod_bot.
2305 // Add following invalid entries.
2306 ++i;
2307 while (i < wp->w_lines_valid
2308 && !wp->w_lines[i].wl_valid)
2309 old_rows += wp->w_lines[i++].wl_size;
2310 break;
2311 }
2312#endif
2313 }
2314
2315 if (i >= wp->w_lines_valid)
2316 {
2317 // We can't find a valid line below the changed lines,
2318 // need to redraw until the end of the window.
2319 // Inserting/deleting lines has no use.
2320 bot_start = 0;
2321 }
2322 else
2323 {
2324 // Able to count old number of rows: Count new window
2325 // rows, and may insert/delete lines
2326 j = idx;
2327 for (l = lnum; l < mod_bot; ++l)
2328 {
2329#ifdef FEAT_FOLDING
2330 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2331 ++new_rows;
2332 else
2333#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002334 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002335#ifdef FEAT_DIFF
2336 if (l == wp->w_topline)
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002337 new_rows += plines_win_nofill(wp, l, TRUE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002338 + wp->w_topfill;
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002339 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002340#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002341 new_rows += plines_win(wp, l, TRUE);
2342 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002343 ++j;
2344 if (new_rows > wp->w_height - row - 2)
2345 {
2346 // it's getting too much, must redraw the rest
2347 new_rows = 9999;
2348 break;
2349 }
2350 }
2351 xtra_rows = new_rows - old_rows;
2352 if (xtra_rows < 0)
2353 {
2354 // May scroll text up. If there is not enough
2355 // remaining text or scrolling fails, must redraw the
2356 // rest. If scrolling works, must redraw the text
2357 // below the scrolled text.
2358 if (row - xtra_rows >= wp->w_height - 2)
2359 mod_bot = MAXLNUM;
2360 else
2361 {
2362 check_for_delay(FALSE);
2363 if (win_del_lines(wp, row,
2364 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2365 mod_bot = MAXLNUM;
2366 else
2367 bot_start = wp->w_height + xtra_rows;
2368 }
2369 }
2370 else if (xtra_rows > 0)
2371 {
2372 // May scroll text down. If there is not enough
2373 // remaining text of scrolling fails, must redraw the
2374 // rest.
2375 if (row + xtra_rows >= wp->w_height - 2)
2376 mod_bot = MAXLNUM;
2377 else
2378 {
2379 check_for_delay(FALSE);
2380 if (win_ins_lines(wp, row + old_rows,
2381 xtra_rows, FALSE, FALSE) == FAIL)
2382 mod_bot = MAXLNUM;
2383 else if (top_end > row + old_rows)
2384 // Scrolled the part at the top that requires
2385 // updating down.
2386 top_end += xtra_rows;
2387 }
2388 }
2389
2390 // When not updating the rest, may need to move w_lines[]
2391 // entries.
2392 if (mod_bot != MAXLNUM && i != j)
2393 {
2394 if (j < i)
2395 {
2396 int x = row + new_rows;
2397
2398 // move entries in w_lines[] upwards
2399 for (;;)
2400 {
2401 // stop at last valid entry in w_lines[]
2402 if (i >= wp->w_lines_valid)
2403 {
2404 wp->w_lines_valid = j;
2405 break;
2406 }
2407 wp->w_lines[j] = wp->w_lines[i];
2408 // stop at a line that won't fit
2409 if (x + (int)wp->w_lines[j].wl_size
2410 > wp->w_height)
2411 {
2412 wp->w_lines_valid = j + 1;
2413 break;
2414 }
2415 x += wp->w_lines[j++].wl_size;
2416 ++i;
2417 }
2418 if (bot_start > x)
2419 bot_start = x;
2420 }
2421 else // j > i
2422 {
2423 // move entries in w_lines[] downwards
2424 j -= i;
2425 wp->w_lines_valid += j;
2426 if (wp->w_lines_valid > wp->w_height)
2427 wp->w_lines_valid = wp->w_height;
2428 for (i = wp->w_lines_valid; i - j >= idx; --i)
2429 wp->w_lines[i] = wp->w_lines[i - j];
2430
2431 // The w_lines[] entries for inserted lines are
2432 // now invalid, but wl_size may be used above.
2433 // Reset to zero.
2434 while (i >= idx)
2435 {
2436 wp->w_lines[i].wl_size = 0;
2437 wp->w_lines[i--].wl_valid = FALSE;
2438 }
2439 }
2440 }
2441 }
2442 }
2443
2444#ifdef FEAT_FOLDING
2445 // When lines are folded, display one line for all of them.
2446 // Otherwise, display normally (can be several display lines when
2447 // 'wrap' is on).
2448 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2449 if (fold_count != 0)
2450 {
2451 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2452 ++row;
2453 --fold_count;
2454 wp->w_lines[idx].wl_folded = TRUE;
2455 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2456# ifdef FEAT_SYN_HL
2457 did_update = DID_FOLD;
2458# endif
2459 }
2460 else
2461#endif
2462 if (idx < wp->w_lines_valid
2463 && wp->w_lines[idx].wl_valid
2464 && wp->w_lines[idx].wl_lnum == lnum
2465 && lnum > wp->w_topline
2466 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2467 && !WIN_IS_POPUP(wp)
2468 && srow + wp->w_lines[idx].wl_size > wp->w_height
2469#ifdef FEAT_DIFF
2470 && diff_check_fill(wp, lnum) == 0
2471#endif
2472 )
2473 {
2474 // This line is not going to fit. Don't draw anything here,
2475 // will draw "@ " lines below.
2476 row = wp->w_height + 1;
2477 }
2478 else
2479 {
2480#ifdef FEAT_SEARCH_EXTRA
2481 prepare_search_hl(wp, &screen_search_hl, lnum);
2482#endif
2483#ifdef FEAT_SYN_HL
2484 // Let the syntax stuff know we skipped a few lines.
2485 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2486 && syntax_present(wp))
2487 syntax_end_parsing(syntax_last_parsed + 1);
2488#endif
2489
2490 // Display one line.
2491 row = win_line(wp, lnum, srow, wp->w_height,
2492 mod_top == 0, FALSE);
2493
2494#ifdef FEAT_FOLDING
2495 wp->w_lines[idx].wl_folded = FALSE;
2496 wp->w_lines[idx].wl_lastlnum = lnum;
2497#endif
2498#ifdef FEAT_SYN_HL
2499 did_update = DID_LINE;
2500 syntax_last_parsed = lnum;
2501#endif
2502 }
2503
2504 wp->w_lines[idx].wl_lnum = lnum;
2505 wp->w_lines[idx].wl_valid = TRUE;
2506
2507 // Past end of the window or end of the screen. Note that after
2508 // resizing wp->w_height may be end up too big. That's a problem
2509 // elsewhere, but prevent a crash here.
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002510 if (row > wp->w_height
2511 || row + wp->w_winrow >= (p_ch > 0 ? Rows : Rows + 1))
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 {
Lewis Russell16246392022-03-29 11:38:17 +01002530 if (wp->w_p_rnu && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002531 {
2532#ifdef FEAT_FOLDING
Lewis Russell16246392022-03-29 11:38:17 +01002533 // 'relativenumber' set and the cursor moved vertically: The
2534 // text doesn't need to be drawn, but the number column does.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002535 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2536 if (fold_count != 0)
2537 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2538 else
2539#endif
2540 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
2541 }
2542
2543 // This line does not need to be drawn, advance to the next one.
2544 row += wp->w_lines[idx++].wl_size;
2545 if (row > wp->w_height) // past end of screen
2546 break;
2547#ifdef FEAT_FOLDING
2548 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2549#else
2550 ++lnum;
2551#endif
2552#ifdef FEAT_SYN_HL
2553 did_update = DID_NONE;
2554#endif
2555 }
2556
2557 if (lnum > buf->b_ml.ml_line_count)
2558 {
2559 eof = TRUE;
2560 break;
2561 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002562
2563 // Safety check: if any of the wl_size values is wrong we might go over
2564 // the end of w_lines[].
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002565 if (idx >= (p_ch > 0 ? Rows : Rows + 1))
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002566 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002567 }
2568
2569 // End of loop over all window lines.
2570
zeertzjqc20e46a2022-03-23 14:55:23 +00002571#ifdef FEAT_SYN_HL
2572 // Now that the window has been redrawn with the old and new cursor line,
2573 // update w_last_cursorline.
2574 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2575#endif
Lewis Russell16246392022-03-29 11:38:17 +01002576 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002577
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002578#ifdef FEAT_VTP
2579 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002580 // See the version that was fixed.
2581 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002582 {
K.Takata54119102022-02-03 13:33:03 +00002583 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002584
K.Takata54119102022-02-03 13:33:03 +00002585 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002586 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002587 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2588 LineOffset[k] + screen_Columns) > 1)
2589 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002590 else
K.Takata54119102022-02-03 13:33:03 +00002591 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002592 else
K.Takata54119102022-02-03 13:33:03 +00002593 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002594 }
2595#endif
2596
2597 if (idx > wp->w_lines_valid)
2598 wp->w_lines_valid = idx;
2599
2600#ifdef FEAT_SYN_HL
2601 // Let the syntax stuff know we stop parsing here.
2602 if (syntax_last_parsed != 0 && syntax_present(wp))
2603 syntax_end_parsing(syntax_last_parsed + 1);
2604#endif
2605
2606 // If we didn't hit the end of the file, and we didn't finish the last
2607 // line we were working on, then the line didn't fit.
2608 wp->w_empty_rows = 0;
2609#ifdef FEAT_DIFF
2610 wp->w_filler_rows = 0;
2611#endif
2612 if (!eof && !didline)
2613 {
2614 if (lnum == wp->w_topline)
2615 {
2616 // Single line that does not fit!
2617 // Don't overwrite it, it can be edited.
2618 wp->w_botline = lnum + 1;
2619 }
2620#ifdef FEAT_DIFF
2621 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2622 {
2623 // Window ends in filler lines.
2624 wp->w_botline = lnum;
2625 wp->w_filler_rows = wp->w_height - srow;
2626 }
2627#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002628#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002629 else if (WIN_IS_POPUP(wp))
2630 {
2631 // popup line that doesn't fit is left as-is
2632 wp->w_botline = lnum;
2633 }
2634#endif
2635 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2636 {
2637 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2638
2639 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002640 screen_puts_len((char_u *)"@@", wp->w_width > 2 ? 2 : wp->w_width,
2641 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002642 screen_fill(scr_row, scr_row + 1,
2643 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
2644 '@', ' ', HL_ATTR(HLF_AT));
2645 set_empty_rows(wp, srow);
2646 wp->w_botline = lnum;
2647 }
2648 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2649 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002650 int start_col = (int)W_ENDCOL(wp) - 3;
2651
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002652 // Last line isn't finished: Display "@@@" at the end.
2653 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2654 W_WINROW(wp) + wp->w_height,
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002655 start_col < wp->w_wincol ? wp->w_wincol : start_col,
2656 (int)W_ENDCOL(wp),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002657 '@', '@', HL_ATTR(HLF_AT));
2658 set_empty_rows(wp, srow);
2659 wp->w_botline = lnum;
2660 }
2661 else
2662 {
2663 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
2664 wp->w_botline = lnum;
2665 }
2666 }
2667 else
2668 {
2669 draw_vsep_win(wp, row);
2670 if (eof) // we hit the end of the file
2671 {
2672 wp->w_botline = buf->b_ml.ml_line_count + 1;
2673#ifdef FEAT_DIFF
2674 j = diff_check_fill(wp, wp->w_botline);
2675 if (j > 0 && !wp->w_botfill)
2676 {
2677 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002678 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002679 i = '-';
2680 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002681 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002682 if (row + j > wp->w_height)
2683 j = wp->w_height - row;
2684 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2685 row += j;
2686 }
2687#endif
2688 }
2689 else if (dollar_vcol == -1)
2690 wp->w_botline = lnum;
2691
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002692 // Make sure the rest of the screen is blank.
2693 // write the "eob" character from 'fillchars' to rows that aren't part
2694 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002695 if (WIN_IS_POPUP(wp))
2696 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2697 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002698 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2699 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002700 }
2701
2702#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002703 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002704 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002705#endif
2706
2707 // Reset the type of redrawing required, the window has been updated.
2708 wp->w_redr_type = 0;
2709#ifdef FEAT_DIFF
2710 wp->w_old_topfill = wp->w_topfill;
2711 wp->w_old_botfill = wp->w_botfill;
2712#endif
2713
2714 if (dollar_vcol == -1)
2715 {
2716 // There is a trick with w_botline. If we invalidate it on each
2717 // change that might modify it, this will cause a lot of expensive
2718 // calls to plines() in update_topline() each time. Therefore the
2719 // value of w_botline is often approximated, and this value is used to
2720 // compute the value of w_topline. If the value of w_botline was
2721 // wrong, check that the value of w_topline is correct (cursor is on
2722 // the visible part of the text). If it's not, we need to redraw
2723 // again. Mostly this just means scrolling up a few lines, so it
2724 // doesn't look too bad. Only do this for the current window (where
2725 // changes are relevant).
2726 wp->w_valid |= VALID_BOTLINE;
2727 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2728 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002729 win_T *wwp;
2730#if defined(FEAT_CONCEAL)
2731 linenr_T old_topline = wp->w_topline;
2732 int new_wcol = wp->w_wcol;
2733#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002734 recursive = TRUE;
2735 curwin->w_valid &= ~VALID_TOPLINE;
2736 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002737
2738#if defined(FEAT_CONCEAL)
2739 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2740 != (VALID_WCOL|VALID_WROW))
2741 {
2742 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002743 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002744 // update_topline() the cursor's row or column got invalidated.
2745 // If they are left invalid, setcursor() will recompute them
2746 // but there won't be any further win_line() call to re-fix the
2747 // column and the cursor will end up misplaced. So we call
2748 // cursor validation now and reapply the fix again (or call
2749 // win_line() to do it for us).
2750 validate_cursor();
2751 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2752 && old_topline == wp->w_topline)
2753 wp->w_wcol = new_wcol;
2754 else
2755 redrawWinline(wp, wp->w_cursor.lnum);
2756 }
2757#endif
2758 // New redraw either due to updated topline or due to wcol fix.
2759 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002760 {
2761 // Don't update for changes in buffer again.
2762 i = curbuf->b_mod_set;
2763 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002764 j = curbuf->b_mod_xlines;
2765 curbuf->b_mod_xlines = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002766 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002767 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002768 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002769 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002770 // Other windows might have w_redr_type raised in update_topline().
2771 must_redraw = 0;
2772 FOR_ALL_WINDOWS(wwp)
2773 if (wwp->w_redr_type > must_redraw)
2774 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002775 recursive = FALSE;
2776 }
2777 }
2778
2779#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2780 // restore got_int, unless CTRL-C was hit while redrawing
2781 if (!got_int)
2782 got_int = save_got_int;
2783#endif
2784}
2785
2786#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2787/*
2788 * Prepare for updating one or more windows.
2789 * Caller must check for "updating_screen" already set to avoid recursiveness.
2790 */
2791 static void
2792update_prepare(void)
2793{
2794 cursor_off();
2795 updating_screen = TRUE;
2796#ifdef FEAT_GUI
2797 // Remove the cursor before starting to do anything, because scrolling may
2798 // make it difficult to redraw the text under it.
2799 if (gui.in_use)
2800 gui_undraw_cursor();
2801#endif
2802#ifdef FEAT_SEARCH_EXTRA
2803 start_search_hl();
2804#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002805#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002806 // Update popup_mask if needed.
2807 may_update_popup_mask(must_redraw);
2808#endif
2809}
2810
2811/*
2812 * Finish updating one or more windows.
2813 */
2814 static void
2815update_finish(void)
2816{
2817 if (redraw_cmdline || redraw_mode)
2818 showmode();
2819
2820# ifdef FEAT_SEARCH_EXTRA
2821 end_search_hl();
2822# endif
2823
2824 after_updating_screen(TRUE);
2825
2826# ifdef FEAT_GUI
2827 // Redraw the cursor and update the scrollbars when all screen updating is
2828 // done.
2829 if (gui.in_use)
2830 {
2831 out_flush_cursor(FALSE, FALSE);
2832 gui_update_scrollbars(FALSE);
2833 }
2834# endif
2835}
2836#endif
2837
2838#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2839 void
2840update_debug_sign(buf_T *buf, linenr_T lnum)
2841{
2842 win_T *wp;
2843 int doit = FALSE;
2844
2845# ifdef FEAT_FOLDING
2846 win_foldinfo.fi_level = 0;
2847# endif
2848
2849 // update/delete a specific sign
2850 redraw_buf_line_later(buf, lnum);
2851
2852 // check if it resulted in the need to redraw a window
2853 FOR_ALL_WINDOWS(wp)
2854 if (wp->w_redr_type != 0)
2855 doit = TRUE;
2856
2857 // Return when there is nothing to do, screen updating is already
2858 // happening (recursive call), messages on the screen or still starting up.
2859 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002860 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002861 || msg_scrolled
2862#ifdef FEAT_GUI
2863 || gui.starting
2864#endif
2865 || starting)
2866 return;
2867
2868 // update all windows that need updating
2869 update_prepare();
2870
2871 FOR_ALL_WINDOWS(wp)
2872 {
2873 if (wp->w_redr_type != 0)
2874 win_update(wp);
2875 if (wp->w_redr_status)
2876 win_redr_status(wp, FALSE);
2877 }
2878
2879 update_finish();
2880}
2881#endif
2882
2883#if defined(FEAT_GUI) || defined(PROTO)
2884/*
2885 * Update a single window, its status line and maybe the command line msg.
2886 * Used for the GUI scrollbar.
2887 */
2888 void
2889updateWindow(win_T *wp)
2890{
2891 // return if already busy updating
2892 if (updating_screen)
2893 return;
2894
2895 update_prepare();
2896
2897#ifdef FEAT_CLIPBOARD
2898 // When Visual area changed, may have to update selection.
2899 if (clip_star.available && clip_isautosel_star())
2900 clip_update_selection(&clip_star);
2901 if (clip_plus.available && clip_isautosel_plus())
2902 clip_update_selection(&clip_plus);
2903#endif
2904
2905 win_update(wp);
2906
2907 // When the screen was cleared redraw the tab pages line.
2908 if (redraw_tabline)
2909 draw_tabline();
2910
2911 if (wp->w_redr_status
2912# ifdef FEAT_CMDL_INFO
2913 || p_ru
2914# endif
2915# ifdef FEAT_STL_OPT
2916 || *p_stl != NUL || *wp->w_p_stl != NUL
2917# endif
2918 )
2919 win_redr_status(wp, FALSE);
2920
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002921#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002922 // Display popup windows on top of everything.
2923 update_popups(win_update);
2924#endif
2925
2926 update_finish();
2927}
2928#endif
2929
2930#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2931/*
2932 * Redraw as soon as possible. When the command line is not scrolled redraw
2933 * right away and restore what was on the command line.
2934 * Return a code indicating what happened.
2935 */
2936 int
2937redraw_asap(int type)
2938{
2939 int rows;
2940 int cols = screen_Columns;
2941 int r;
2942 int ret = 0;
2943 schar_T *screenline; // copy from ScreenLines[]
2944 sattr_T *screenattr; // copy from ScreenAttrs[]
2945 int i;
2946 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2947 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2948 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2949
2950 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002951 if (msg_scrolled
2952 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
2953 || exiting
2954 || p_ch == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002955 return ret;
2956
2957 // Allocate space to save the text displayed in the command line area.
2958 rows = screen_Rows - cmdline_row;
2959 screenline = LALLOC_MULT(schar_T, rows * cols);
2960 screenattr = LALLOC_MULT(sattr_T, rows * cols);
2961 if (screenline == NULL || screenattr == NULL)
2962 ret = 2;
2963 if (enc_utf8)
2964 {
2965 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
2966 if (screenlineUC == NULL)
2967 ret = 2;
2968 for (i = 0; i < p_mco; ++i)
2969 {
2970 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
2971 if (screenlineC[i] == NULL)
2972 ret = 2;
2973 }
2974 }
2975 if (enc_dbcs == DBCS_JPNU)
2976 {
2977 screenline2 = LALLOC_MULT(schar_T, rows * cols);
2978 if (screenline2 == NULL)
2979 ret = 2;
2980 }
2981
2982 if (ret != 2)
2983 {
2984 // Save the text displayed in the command line area.
2985 for (r = 0; r < rows; ++r)
2986 {
2987 mch_memmove(screenline + r * cols,
2988 ScreenLines + LineOffset[cmdline_row + r],
2989 (size_t)cols * sizeof(schar_T));
2990 mch_memmove(screenattr + r * cols,
2991 ScreenAttrs + LineOffset[cmdline_row + r],
2992 (size_t)cols * sizeof(sattr_T));
2993 if (enc_utf8)
2994 {
2995 mch_memmove(screenlineUC + r * cols,
2996 ScreenLinesUC + LineOffset[cmdline_row + r],
2997 (size_t)cols * sizeof(u8char_T));
2998 for (i = 0; i < p_mco; ++i)
2999 mch_memmove(screenlineC[i] + r * cols,
3000 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3001 (size_t)cols * sizeof(u8char_T));
3002 }
3003 if (enc_dbcs == DBCS_JPNU)
3004 mch_memmove(screenline2 + r * cols,
3005 ScreenLines2 + LineOffset[cmdline_row + r],
3006 (size_t)cols * sizeof(schar_T));
3007 }
3008
3009 update_screen(0);
3010 ret = 3;
3011
3012 if (must_redraw == 0)
3013 {
3014 int off = (int)(current_ScreenLine - ScreenLines);
3015
3016 // Restore the text displayed in the command line area.
3017 for (r = 0; r < rows; ++r)
3018 {
3019 mch_memmove(current_ScreenLine,
3020 screenline + r * cols,
3021 (size_t)cols * sizeof(schar_T));
3022 mch_memmove(ScreenAttrs + off,
3023 screenattr + r * cols,
3024 (size_t)cols * sizeof(sattr_T));
3025 if (enc_utf8)
3026 {
3027 mch_memmove(ScreenLinesUC + off,
3028 screenlineUC + r * cols,
3029 (size_t)cols * sizeof(u8char_T));
3030 for (i = 0; i < p_mco; ++i)
3031 mch_memmove(ScreenLinesC[i] + off,
3032 screenlineC[i] + r * cols,
3033 (size_t)cols * sizeof(u8char_T));
3034 }
3035 if (enc_dbcs == DBCS_JPNU)
3036 mch_memmove(ScreenLines2 + off,
3037 screenline2 + r * cols,
3038 (size_t)cols * sizeof(schar_T));
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003039 screen_line(curwin, cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003040 }
3041 ret = 4;
3042 }
3043 }
3044
3045 vim_free(screenline);
3046 vim_free(screenattr);
3047 if (enc_utf8)
3048 {
3049 vim_free(screenlineUC);
3050 for (i = 0; i < p_mco; ++i)
3051 vim_free(screenlineC[i]);
3052 }
3053 if (enc_dbcs == DBCS_JPNU)
3054 vim_free(screenline2);
3055
3056 // Show the intro message when appropriate.
3057 maybe_intro_message();
3058
3059 setcursor();
3060
3061 return ret;
3062}
3063#endif
3064
3065/*
3066 * Invoked after an asynchronous callback is called.
3067 * If an echo command was used the cursor needs to be put back where
3068 * it belongs. If highlighting was changed a redraw is needed.
3069 * If "call_update_screen" is FALSE don't call update_screen() when at the
3070 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003071 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003072 */
3073 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003074redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003075{
3076 ++redrawing_for_callback;
3077
Bram Moolenaar24959102022-05-07 20:01:16 +01003078 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3079 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3080 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003081 {
3082 if (do_message)
3083 repeat_message();
3084 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003085 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003086 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003087#ifdef FEAT_WILDMENU
3088 if (pum_visible())
3089 cmdline_pum_display();
3090#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003091 // Don't redraw when in prompt_for_number().
3092 if (cmdline_row > 0)
3093 {
3094 // Redrawing only works when the screen didn't scroll. Don't clear
3095 // wildmenu entries.
3096 if (msg_scrolled == 0
3097#ifdef FEAT_WILDMENU
3098 && wild_menu_showing == 0
3099#endif
3100 && call_update_screen)
3101 update_screen(0);
3102
3103 // Redraw in the same position, so that the user can continue
3104 // editing the command.
3105 redrawcmdline_ex(FALSE);
3106 }
3107 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003108 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003109 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003110 update_topline();
3111 validate_cursor();
3112
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003113 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003114 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003115 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003116
3117 if (msg_scrolled == 0)
3118 {
3119 // don't want a hit-enter prompt when something else is displayed
3120 msg_didany = FALSE;
3121 need_wait_return = FALSE;
3122 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003123 }
3124 cursor_on();
3125#ifdef FEAT_GUI
3126 if (gui.in_use && !gui_mch_is_blink_off())
3127 // Don't update the cursor when it is blinking and off to avoid
3128 // flicker.
3129 out_flush_cursor(FALSE, FALSE);
3130 else
3131#endif
3132 out_flush();
3133
3134 --redrawing_for_callback;
3135}
3136
3137/*
3138 * Redraw the current window later, with update_screen(type).
3139 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003140 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003141 */
3142 void
3143redraw_later(int type)
3144{
3145 redraw_win_later(curwin, type);
3146}
3147
3148 void
3149redraw_win_later(
3150 win_T *wp,
3151 int type)
3152{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003153 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003154 {
3155 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003156 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003157 wp->w_lines_valid = 0;
3158 if (must_redraw < type) // must_redraw is the maximum of all windows
3159 must_redraw = type;
3160 }
3161}
3162
3163/*
3164 * Force a complete redraw later. Also resets the highlighting. To be used
3165 * after executing a shell command that messes up the screen.
3166 */
3167 void
3168redraw_later_clear(void)
3169{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003170 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003171 reset_screen_attr();
3172}
3173
3174/*
3175 * Mark all windows to be redrawn later.
3176 */
3177 void
3178redraw_all_later(int type)
3179{
3180 win_T *wp;
3181
3182 FOR_ALL_WINDOWS(wp)
3183 redraw_win_later(wp, type);
3184 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003185 set_must_redraw(type);
3186}
3187
3188/*
3189 * Set "must_redraw" to "type" unless it already has a higher value
3190 * or it is currently not allowed.
3191 */
3192 void
3193set_must_redraw(int type)
3194{
3195 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003196 must_redraw = type;
3197}
3198
3199/*
3200 * Mark all windows that are editing the current buffer to be updated later.
3201 */
3202 void
3203redraw_curbuf_later(int type)
3204{
3205 redraw_buf_later(curbuf, type);
3206}
3207
3208 void
3209redraw_buf_later(buf_T *buf, int type)
3210{
3211 win_T *wp;
3212
3213 FOR_ALL_WINDOWS(wp)
3214 {
3215 if (wp->w_buffer == buf)
3216 redraw_win_later(wp, type);
3217 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003218#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3219 // terminal in popup window is not in list of windows
3220 if (curwin->w_buffer == buf)
3221 redraw_win_later(curwin, type);
3222#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003223}
3224
3225#if defined(FEAT_SIGNS) || defined(PROTO)
3226 void
3227redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3228{
3229 win_T *wp;
3230
3231 FOR_ALL_WINDOWS(wp)
3232 if (wp->w_buffer == buf && lnum >= wp->w_topline
3233 && lnum < wp->w_botline)
3234 redrawWinline(wp, lnum);
3235}
3236#endif
3237
3238#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3239 void
3240redraw_buf_and_status_later(buf_T *buf, int type)
3241{
3242 win_T *wp;
3243
3244#ifdef FEAT_WILDMENU
3245 if (wild_menu_showing != 0)
3246 // Don't redraw while the command line completion is displayed, it
3247 // would disappear.
3248 return;
3249#endif
3250 FOR_ALL_WINDOWS(wp)
3251 {
3252 if (wp->w_buffer == buf)
3253 {
3254 redraw_win_later(wp, type);
3255 wp->w_redr_status = TRUE;
3256 }
3257 }
3258}
3259#endif
3260
3261/*
3262 * mark all status lines for redraw; used after first :cd
3263 */
3264 void
3265status_redraw_all(void)
3266{
3267 win_T *wp;
3268
3269 FOR_ALL_WINDOWS(wp)
3270 if (wp->w_status_height)
3271 {
3272 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003273 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003274 }
3275}
3276
3277/*
3278 * mark all status lines of the current buffer for redraw
3279 */
3280 void
3281status_redraw_curbuf(void)
3282{
3283 win_T *wp;
3284
3285 FOR_ALL_WINDOWS(wp)
3286 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3287 {
3288 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003289 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003290 }
3291}
3292
3293/*
3294 * Redraw all status lines that need to be redrawn.
3295 */
3296 void
3297redraw_statuslines(void)
3298{
3299 win_T *wp;
3300
3301 FOR_ALL_WINDOWS(wp)
3302 if (wp->w_redr_status)
3303 win_redr_status(wp, FALSE);
3304 if (redraw_tabline)
3305 draw_tabline();
3306}
3307
3308#if defined(FEAT_WILDMENU) || defined(PROTO)
3309/*
3310 * Redraw all status lines at the bottom of frame "frp".
3311 */
3312 void
3313win_redraw_last_status(frame_T *frp)
3314{
3315 if (frp->fr_layout == FR_LEAF)
3316 frp->fr_win->w_redr_status = TRUE;
3317 else if (frp->fr_layout == FR_ROW)
3318 {
3319 FOR_ALL_FRAMES(frp, frp->fr_child)
3320 win_redraw_last_status(frp);
3321 }
3322 else // frp->fr_layout == FR_COL
3323 {
3324 frp = frp->fr_child;
3325 while (frp->fr_next != NULL)
3326 frp = frp->fr_next;
3327 win_redraw_last_status(frp);
3328 }
3329}
3330#endif
3331
3332/*
3333 * Changed something in the current window, at buffer line "lnum", that
3334 * requires that line and possibly other lines to be redrawn.
3335 * Used when entering/leaving Insert mode with the cursor on a folded line.
3336 * Used to remove the "$" from a change command.
3337 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3338 * may become invalid and the whole window will have to be redrawn.
3339 */
3340 void
3341redrawWinline(
3342 win_T *wp,
3343 linenr_T lnum)
3344{
3345 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
3346 wp->w_redraw_top = lnum;
3347 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
3348 wp->w_redraw_bot = lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003349 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003350}