blob: aa5b8f7eb7dddf0dd801b1e75387a960e1d008f8 [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
Bram Moolenaar7a99da42022-08-28 22:21:01 +0100833
834 // in case it was changed in dont_use_message_window()
835 cmdline_row = Rows - p_ch;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200836}
837
838/*
839 * Update all windows that are editing the current buffer.
840 */
841 void
842update_curbuf(int type)
843{
844 redraw_curbuf_later(type);
845 update_screen(type);
846}
847
848#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
849/*
850 * Copy "text" to ScreenLines using "attr".
851 * Returns the next screen column.
852 */
853 static int
854text_to_screenline(win_T *wp, char_u *text, int col)
855{
856 int off = (int)(current_ScreenLine - ScreenLines);
857
858 if (has_mbyte)
859 {
860 int cells;
861 int u8c, u8cc[MAX_MCO];
862 int i;
863 int idx;
864 int c_len;
865 char_u *p;
866# ifdef FEAT_ARABIC
867 int prev_c = 0; // previous Arabic character
868 int prev_c1 = 0; // first composing char for prev_c
869# endif
870
871# ifdef FEAT_RIGHTLEFT
872 if (wp->w_p_rl)
873 idx = off;
874 else
875# endif
876 idx = off + col;
877
878 // Store multibyte characters in ScreenLines[] et al. correctly.
879 for (p = text; *p != NUL; )
880 {
881 cells = (*mb_ptr2cells)(p);
882 c_len = (*mb_ptr2len)(p);
883 if (col + cells > wp->w_width
884# ifdef FEAT_RIGHTLEFT
885 - (wp->w_p_rl ? col : 0)
886# endif
887 )
888 break;
889 ScreenLines[idx] = *p;
890 if (enc_utf8)
891 {
892 u8c = utfc_ptr2char(p, u8cc);
893 if (*p < 0x80 && u8cc[0] == 0)
894 {
895 ScreenLinesUC[idx] = 0;
896#ifdef FEAT_ARABIC
897 prev_c = u8c;
898#endif
899 }
900 else
901 {
902#ifdef FEAT_ARABIC
903 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
904 {
905 // Do Arabic shaping.
906 int pc, pc1, nc;
907 int pcc[MAX_MCO];
908 int firstbyte = *p;
909
910 // The idea of what is the previous and next
911 // character depends on 'rightleft'.
912 if (wp->w_p_rl)
913 {
914 pc = prev_c;
915 pc1 = prev_c1;
916 nc = utf_ptr2char(p + c_len);
917 prev_c1 = u8cc[0];
918 }
919 else
920 {
921 pc = utfc_ptr2char(p + c_len, pcc);
922 nc = prev_c;
923 pc1 = pcc[0];
924 }
925 prev_c = u8c;
926
927 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
928 pc, pc1, nc);
929 ScreenLines[idx] = firstbyte;
930 }
931 else
932 prev_c = u8c;
933#endif
934 // Non-BMP character: display as ? or fullwidth ?.
935 ScreenLinesUC[idx] = u8c;
936 for (i = 0; i < Screen_mco; ++i)
937 {
938 ScreenLinesC[i][idx] = u8cc[i];
939 if (u8cc[i] == 0)
940 break;
941 }
942 }
943 if (cells > 1)
944 ScreenLines[idx + 1] = 0;
945 }
946 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
947 // double-byte single width character
948 ScreenLines2[idx] = p[1];
949 else if (cells > 1)
950 // double-width character
951 ScreenLines[idx + 1] = p[1];
952 col += cells;
953 idx += cells;
954 p += c_len;
955 }
956 }
957 else
958 {
959 int len = (int)STRLEN(text);
960
961 if (len > wp->w_width - col)
962 len = wp->w_width - col;
963 if (len > 0)
964 {
965#ifdef FEAT_RIGHTLEFT
966 if (wp->w_p_rl)
967 mch_memmove(current_ScreenLine, text, len);
968 else
969#endif
970 mch_memmove(current_ScreenLine + col, text, len);
971 col += len;
972 }
973 }
974 return col;
975}
976#endif
977
978#ifdef FEAT_MENU
979/*
980 * Draw the window toolbar.
981 */
982 static void
983redraw_win_toolbar(win_T *wp)
984{
985 vimmenu_T *menu;
986 int item_idx = 0;
987 int item_count = 0;
988 int col = 0;
989 int next_col;
990 int off = (int)(current_ScreenLine - ScreenLines);
991 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
992 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
993
994 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200995 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200996 ++item_count;
997 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
998
999 // TODO: use fewer spaces if there is not enough room
1000 for (menu = wp->w_winbar->children;
1001 menu != NULL && col < wp->w_width; menu = menu->next)
1002 {
1003 space_to_screenline(off + col, fill_attr);
1004 if (++col >= wp->w_width)
1005 break;
1006 if (col > 1)
1007 {
1008 space_to_screenline(off + col, fill_attr);
1009 if (++col >= wp->w_width)
1010 break;
1011 }
1012
1013 wp->w_winbar_items[item_idx].wb_startcol = col;
1014 space_to_screenline(off + col, button_attr);
1015 if (++col >= wp->w_width)
1016 break;
1017
1018 next_col = text_to_screenline(wp, menu->name, col);
1019 while (col < next_col)
1020 {
1021 ScreenAttrs[off + col] = button_attr;
1022 ++col;
1023 }
1024 wp->w_winbar_items[item_idx].wb_endcol = col;
1025 wp->w_winbar_items[item_idx].wb_menu = menu;
1026 ++item_idx;
1027
1028 if (col >= wp->w_width)
1029 break;
1030 space_to_screenline(off + col, button_attr);
1031 ++col;
1032 }
1033 while (col < wp->w_width)
1034 {
1035 space_to_screenline(off + col, fill_attr);
1036 ++col;
1037 }
1038 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1039
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001040 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001041}
1042#endif
1043
1044#if defined(FEAT_FOLDING) || defined(PROTO)
1045/*
1046 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1047 */
1048 static void
1049copy_text_attr(
1050 int off,
1051 char_u *buf,
1052 int len,
1053 int attr)
1054{
1055 int i;
1056
1057 mch_memmove(ScreenLines + off, buf, (size_t)len);
1058 if (enc_utf8)
1059 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1060 for (i = 0; i < len; ++i)
1061 ScreenAttrs[off + i] = attr;
1062}
1063
1064/*
1065 * Display one folded line.
1066 */
1067 static void
1068fold_line(
1069 win_T *wp,
1070 long fold_count,
1071 foldinfo_T *foldinfo,
1072 linenr_T lnum,
1073 int row)
1074{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001075 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1076 // multi-byte character is MAX_MCO.
1077 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001078 pos_T *top, *bot;
1079 linenr_T lnume = lnum + fold_count - 1;
1080 int len;
1081 char_u *text;
1082 int fdc;
1083 int col;
1084 int txtcol;
1085 int off = (int)(current_ScreenLine - ScreenLines);
1086 int ri;
1087
1088 // Build the fold line:
1089 // 1. Add the cmdwin_type for the command-line window
1090 // 2. Add the 'foldcolumn'
1091 // 3. Add the 'number' or 'relativenumber' column
1092 // 4. Compose the text
1093 // 5. Add the text
1094 // 6. set highlighting for the Visual area an other text
1095 col = 0;
1096
1097 // 1. Add the cmdwin_type for the command-line window
1098 // Ignores 'rightleft', this window is never right-left.
1099#ifdef FEAT_CMDWIN
1100 if (cmdwin_type != 0 && wp == curwin)
1101 {
1102 ScreenLines[off] = cmdwin_type;
1103 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1104 if (enc_utf8)
1105 ScreenLinesUC[off] = 0;
1106 ++col;
1107 }
1108#endif
1109
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001110#ifdef FEAT_RIGHTLEFT
1111# define RL_MEMSET(p, v, l) \
1112 do { \
1113 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001114 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001115 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1116 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001117 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001118 ScreenAttrs[off + (p) + ri] = v; \
1119 } while (0)
1120#else
1121# define RL_MEMSET(p, v, l) \
1122 do { \
1123 for (ri = 0; ri < l; ++ri) \
1124 ScreenAttrs[off + (p) + ri] = v; \
1125 } while (0)
1126#endif
1127
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001128 // 2. Add the 'foldcolumn'
1129 // Reduce the width when there is not enough space.
1130 fdc = compute_foldcolumn(wp, col);
1131 if (fdc > 0)
1132 {
1133 char_u *p;
1134 int i;
1135 int idx;
1136
1137 fill_foldcolumn(buf, wp, TRUE, lnum);
1138 p = buf;
1139 for (i = 0; i < fdc; i++)
1140 {
1141 int ch;
1142
1143 if (has_mbyte)
1144 ch = mb_ptr2char_adv(&p);
1145 else
1146 ch = *p++;
1147#ifdef FEAT_RIGHTLEFT
1148 if (wp->w_p_rl)
1149 idx = off + wp->w_width - i - 1 - col;
1150 else
1151#endif
1152 idx = off + col + i;
1153 if (enc_utf8)
1154 {
1155 if (ch >= 0x80)
1156 {
1157 ScreenLinesUC[idx] = ch;
1158 ScreenLinesC[0][idx] = 0;
1159 ScreenLines[idx] = 0x80;
1160 }
1161 else
1162 {
1163 ScreenLines[idx] = ch;
1164 ScreenLinesUC[idx] = 0;
1165 }
1166 }
1167 else
1168 ScreenLines[idx] = ch;
1169 }
1170
1171 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1172 col += fdc;
1173 }
1174
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001175 // Set all attributes of the 'number' or 'relativenumber' column and the
1176 // text
1177 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1178
1179#ifdef FEAT_SIGNS
1180 // If signs are being displayed, add two spaces.
1181 if (signcolumn_on(wp))
1182 {
1183 len = wp->w_width - col;
1184 if (len > 0)
1185 {
1186 if (len > 2)
1187 len = 2;
1188# ifdef FEAT_RIGHTLEFT
1189 if (wp->w_p_rl)
1190 // the line number isn't reversed
1191 copy_text_attr(off + wp->w_width - len - col,
1192 (char_u *)" ", len, HL_ATTR(HLF_FL));
1193 else
1194# endif
1195 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1196 col += len;
1197 }
1198 }
1199#endif
1200
1201 // 3. Add the 'number' or 'relativenumber' column
1202 if (wp->w_p_nu || wp->w_p_rnu)
1203 {
1204 len = wp->w_width - col;
1205 if (len > 0)
1206 {
1207 int w = number_width(wp);
1208 long num;
1209 char *fmt = "%*ld ";
1210
1211 if (len > w + 1)
1212 len = w + 1;
1213
1214 if (wp->w_p_nu && !wp->w_p_rnu)
1215 // 'number' + 'norelativenumber'
1216 num = (long)lnum;
1217 else
1218 {
1219 // 'relativenumber', don't use negative numbers
1220 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1221 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1222 {
1223 // 'number' + 'relativenumber': cursor line shows absolute
1224 // line number
1225 num = lnum;
1226 fmt = "%-*ld ";
1227 }
1228 }
1229
1230 sprintf((char *)buf, fmt, w, num);
1231#ifdef FEAT_RIGHTLEFT
1232 if (wp->w_p_rl)
1233 // the line number isn't reversed
1234 copy_text_attr(off + wp->w_width - len - col, buf, len,
1235 HL_ATTR(HLF_FL));
1236 else
1237#endif
1238 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1239 col += len;
1240 }
1241 }
1242
1243 // 4. Compose the folded-line string with 'foldtext', if set.
1244 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1245
1246 txtcol = col; // remember where text starts
1247
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001248 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1249 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001250 // Right-left text is put in columns 0 - number-col, normal text is put
1251 // in columns number-col - window-width.
1252 col = text_to_screenline(wp, text, col);
1253
1254 // Fill the rest of the line with the fold filler
1255#ifdef FEAT_RIGHTLEFT
1256 if (wp->w_p_rl)
1257 col -= txtcol;
1258#endif
1259 while (col < wp->w_width
1260#ifdef FEAT_RIGHTLEFT
1261 - (wp->w_p_rl ? txtcol : 0)
1262#endif
1263 )
1264 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001265 int c = wp->w_fill_chars.fold;
1266
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001267 if (enc_utf8)
1268 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001269 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001270 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001271 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001272 ScreenLinesC[0][off + col] = 0;
1273 ScreenLines[off + col] = 0x80; // avoid storing zero
1274 }
1275 else
1276 {
1277 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001278 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001279 }
1280 col++;
1281 }
1282 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001283 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001284 }
1285
1286 if (text != buf)
1287 vim_free(text);
1288
1289 // 6. set highlighting for the Visual area an other text.
1290 // If all folded lines are in the Visual area, highlight the line.
1291 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1292 {
1293 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1294 {
1295 // Visual is after curwin->w_cursor
1296 top = &curwin->w_cursor;
1297 bot = &VIsual;
1298 }
1299 else
1300 {
1301 // Visual is before curwin->w_cursor
1302 top = &VIsual;
1303 bot = &curwin->w_cursor;
1304 }
1305 if (lnum >= top->lnum
1306 && lnume <= bot->lnum
1307 && (VIsual_mode != 'v'
1308 || ((lnum > top->lnum
1309 || (lnum == top->lnum
1310 && top->col == 0))
1311 && (lnume < bot->lnum
1312 || (lnume == bot->lnum
1313 && (bot->col - (*p_sel == 'e'))
1314 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
1315 {
1316 if (VIsual_mode == Ctrl_V)
1317 {
1318 // Visual block mode: highlight the chars part of the block
1319 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1320 {
1321 if (wp->w_old_cursor_lcol != MAXCOL
1322 && wp->w_old_cursor_lcol + txtcol
1323 < (colnr_T)wp->w_width)
1324 len = wp->w_old_cursor_lcol;
1325 else
1326 len = wp->w_width - txtcol;
1327 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1328 len - (int)wp->w_old_cursor_fcol);
1329 }
1330 }
1331 else
1332 {
1333 // Set all attributes of the text
1334 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1335 }
1336 }
1337 }
1338
1339#ifdef FEAT_SYN_HL
1340 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1341 if (wp->w_p_cc_cols)
1342 {
1343 int i = 0;
1344 int j = wp->w_p_cc_cols[i];
1345 int old_txtcol = txtcol;
1346
1347 while (j > -1)
1348 {
1349 txtcol += j;
1350 if (wp->w_p_wrap)
1351 txtcol -= wp->w_skipcol;
1352 else
1353 txtcol -= wp->w_leftcol;
1354 if (txtcol >= 0 && txtcol < wp->w_width)
1355 ScreenAttrs[off + txtcol] = hl_combine_attr(
1356 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1357 txtcol = old_txtcol;
1358 j = wp->w_p_cc_cols[++i];
1359 }
1360 }
1361
1362 // Show 'cursorcolumn' in the fold line.
1363 if (wp->w_p_cuc)
1364 {
1365 txtcol += wp->w_virtcol;
1366 if (wp->w_p_wrap)
1367 txtcol -= wp->w_skipcol;
1368 else
1369 txtcol -= wp->w_leftcol;
1370 if (txtcol >= 0 && txtcol < wp->w_width)
1371 ScreenAttrs[off + txtcol] = hl_combine_attr(
1372 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1373 }
1374#endif
1375
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001376 screen_line(wp, row + W_WINROW(wp), wp->w_wincol,
1377 wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001378
1379 // Update w_cline_height and w_cline_folded if the cursor line was
1380 // updated (saves a call to plines() later).
1381 if (wp == curwin
1382 && lnum <= curwin->w_cursor.lnum
1383 && lnume >= curwin->w_cursor.lnum)
1384 {
1385 curwin->w_cline_row = row;
1386 curwin->w_cline_height = 1;
1387 curwin->w_cline_folded = TRUE;
1388 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1389 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001390
1391# ifdef FEAT_CONCEAL
1392 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001393 if (wp == curwin
1394 && wp->w_cursor.lnum >= lnum
1395 && wp->w_cursor.lnum <= lnume
1396 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001397 curs_columns(TRUE);
1398# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001399}
1400#endif
1401
1402/*
1403 * Update a single window.
1404 *
1405 * This may cause the windows below it also to be redrawn (when clearing the
1406 * screen or scrolling lines).
1407 *
1408 * How the window is redrawn depends on wp->w_redr_type. Each type also
1409 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001410 * UPD_NOT_VALID redraw the whole window
1411 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1412 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1413 * UPD_VALID
1414 * UPD_INVERTED redraw the changed part of the Visual area
1415 * UPD_INVERTED_ALL redraw the whole Visual area
1416 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001417 * 2. update lines at the top when scrolled down
1418 * 3. redraw changed text:
1419 * - if wp->w_buffer->b_mod_set set, update lines between
1420 * b_mod_top and b_mod_bot.
1421 * - if wp->w_redraw_top non-zero, redraw lines between
1422 * wp->w_redraw_top and wp->w_redr_bot.
1423 * - continue redrawing when syntax status is invalid.
1424 * 4. if scrolled up, update lines at the bottom.
1425 * This results in three areas that may need updating:
1426 * top: from first row to top_end (when scrolled down)
1427 * mid: from mid_start to mid_end (update inversion or changed text)
1428 * bot: from bot_start to last row (when scrolled up)
1429 */
1430 static void
1431win_update(win_T *wp)
1432{
1433 buf_T *buf = wp->w_buffer;
1434 int type;
1435 int top_end = 0; // Below last row of the top area that needs
1436 // updating. 0 when no top area updating.
1437 int mid_start = 999;// first row of the mid area that needs
1438 // updating. 999 when no mid area updating.
1439 int mid_end = 0; // Below last row of the mid area that needs
1440 // updating. 0 when no mid area updating.
1441 int bot_start = 999;// first row of the bot area that needs
1442 // updating. 999 when no bot area updating
1443 int scrolled_down = FALSE; // TRUE when scrolled down when
1444 // w_topline got smaller a bit
1445#ifdef FEAT_SEARCH_EXTRA
1446 int top_to_mod = FALSE; // redraw above mod_top
1447#endif
1448
1449 int row; // current window row to display
1450 linenr_T lnum; // current buffer lnum to display
1451 int idx; // current index in w_lines[]
1452 int srow; // starting row of the current line
1453
1454 int eof = FALSE; // if TRUE, we hit the end of the file
1455 int didline = FALSE; // if TRUE, we finished the last line
1456 int i;
1457 long j;
1458 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001459 linenr_T old_botline = wp->w_botline;
1460#ifdef FEAT_CONCEAL
1461 int old_wrow = wp->w_wrow;
1462 int old_wcol = wp->w_wcol;
1463#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001464#ifdef FEAT_FOLDING
1465 long fold_count;
1466#endif
1467#ifdef FEAT_SYN_HL
1468 // remember what happened to the previous line, to know if
1469 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001470# define DID_NONE 1 // didn't update a line
1471# define DID_LINE 2 // updated a normal line
1472# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001473 int did_update = DID_NONE;
1474 linenr_T syntax_last_parsed = 0; // last parsed text line
1475#endif
1476 linenr_T mod_top = 0;
1477 linenr_T mod_bot = 0;
1478#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1479 int save_got_int;
1480#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001481
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001482#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1483 // This needs to be done only for the first window when update_screen() is
1484 // called.
1485 if (!did_update_one_window)
1486 {
1487 did_update_one_window = TRUE;
1488# ifdef FEAT_SEARCH_EXTRA
1489 start_search_hl();
1490# endif
1491# ifdef FEAT_CLIPBOARD
1492 // When Visual area changed, may have to update selection.
1493 if (clip_star.available && clip_isautosel_star())
1494 clip_update_selection(&clip_star);
1495 if (clip_plus.available && clip_isautosel_plus())
1496 clip_update_selection(&clip_plus);
1497# endif
1498 }
1499#endif
1500
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001501 type = wp->w_redr_type;
1502
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001503 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001504 {
1505 wp->w_redr_status = TRUE;
1506 wp->w_lines_valid = 0;
1507 }
1508
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001509 // Window frame is zero-height: nothing to draw.
1510 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1511 || (wp->w_frame->fr_height == wp->w_status_height
1512#if defined(FEAT_PROP_POPUP)
1513 && !popup_is_popup(wp)
1514#endif
1515 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001516 {
1517 wp->w_redr_type = 0;
1518 return;
1519 }
1520
1521 // Window is zero-width: Only need to draw the separator.
1522 if (wp->w_width == 0)
1523 {
1524 // draw the vertical separator right of this window
1525 draw_vsep_win(wp, 0);
1526 wp->w_redr_type = 0;
1527 return;
1528 }
1529
1530#ifdef FEAT_TERMINAL
1531 // If this window contains a terminal, redraw works completely differently.
1532 if (term_do_update_window(wp))
1533 {
1534 term_update_window(wp);
1535# ifdef FEAT_MENU
1536 // Draw the window toolbar, if there is one.
1537 if (winbar_height(wp) > 0)
1538 redraw_win_toolbar(wp);
1539# endif
1540 wp->w_redr_type = 0;
1541 return;
1542 }
1543#endif
1544
1545#ifdef FEAT_SEARCH_EXTRA
1546 init_search_hl(wp, &screen_search_hl);
1547#endif
1548
1549#ifdef FEAT_LINEBREAK
1550 // Force redraw when width of 'number' or 'relativenumber' column
1551 // changes.
1552 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1553 if (wp->w_nrwidth != i)
1554 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001555 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001556 wp->w_nrwidth = i;
1557 }
1558 else
1559#endif
1560
1561 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1562 {
1563 // When there are both inserted/deleted lines and specific lines to be
1564 // redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1565 // everything (only happens when redrawing is off for while).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001566 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001567 }
1568 else
1569 {
1570 // Set mod_top to the first line that needs displaying because of
1571 // changes. Set mod_bot to the first line after the changes.
1572 mod_top = wp->w_redraw_top;
1573 if (wp->w_redraw_bot != 0)
1574 mod_bot = wp->w_redraw_bot + 1;
1575 else
1576 mod_bot = 0;
1577 if (buf->b_mod_set)
1578 {
1579 if (mod_top == 0 || mod_top > buf->b_mod_top)
1580 {
1581 mod_top = buf->b_mod_top;
1582#ifdef FEAT_SYN_HL
1583 // Need to redraw lines above the change that may be included
1584 // in a pattern match.
1585 if (syntax_present(wp))
1586 {
1587 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1588 if (mod_top < 1)
1589 mod_top = 1;
1590 }
1591#endif
1592 }
1593 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1594 mod_bot = buf->b_mod_bot;
1595
1596#ifdef FEAT_SEARCH_EXTRA
1597 // When 'hlsearch' is on and using a multi-line search pattern, a
1598 // change in one line may make the Search highlighting in a
1599 // previous line invalid. Simple solution: redraw all visible
1600 // lines above the change.
1601 // Same for a match pattern.
1602 if (screen_search_hl.rm.regprog != NULL
1603 && re_multiline(screen_search_hl.rm.regprog))
1604 top_to_mod = TRUE;
1605 else
1606 {
1607 matchitem_T *cur = wp->w_match_head;
1608
1609 while (cur != NULL)
1610 {
1611 if (cur->match.regprog != NULL
1612 && re_multiline(cur->match.regprog))
1613 {
1614 top_to_mod = TRUE;
1615 break;
1616 }
1617 cur = cur->next;
1618 }
1619 }
1620#endif
1621 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001622
1623#ifdef FEAT_SEARCH_EXTRA
1624 if (search_hl_has_cursor_lnum > 0)
1625 {
1626 // CurSearch was used last time, need to redraw the line with it to
1627 // avoid having two matches highlighted with CurSearch.
1628 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1629 mod_top = search_hl_has_cursor_lnum;
1630 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1631 mod_bot = search_hl_has_cursor_lnum + 1;
1632 }
1633#endif
1634
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001635#ifdef FEAT_FOLDING
1636 if (mod_top != 0 && hasAnyFolding(wp))
1637 {
1638 linenr_T lnumt, lnumb;
1639
1640 // A change in a line can cause lines above it to become folded or
1641 // unfolded. Find the top most buffer line that may be affected.
1642 // If the line was previously folded and displayed, get the first
1643 // line of that fold. If the line is folded now, get the first
1644 // folded line. Use the minimum of these two.
1645
1646 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1647 // the line below it. If there is no valid entry, use w_topline.
1648 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1649 // to this line. If there is no valid entry, use MAXLNUM.
1650 lnumt = wp->w_topline;
1651 lnumb = MAXLNUM;
1652 for (i = 0; i < wp->w_lines_valid; ++i)
1653 if (wp->w_lines[i].wl_valid)
1654 {
1655 if (wp->w_lines[i].wl_lastlnum < mod_top)
1656 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1657 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1658 {
1659 lnumb = wp->w_lines[i].wl_lnum;
1660 // When there is a fold column it might need updating
1661 // in the next line ("J" just above an open fold).
1662 if (compute_foldcolumn(wp, 0) > 0)
1663 ++lnumb;
1664 }
1665 }
1666
1667 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1668 if (mod_top > lnumt)
1669 mod_top = lnumt;
1670
1671 // Now do the same for the bottom line (one above mod_bot).
1672 --mod_bot;
1673 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1674 ++mod_bot;
1675 if (mod_bot < lnumb)
1676 mod_bot = lnumb;
1677 }
1678#endif
1679
1680 // When a change starts above w_topline and the end is below
1681 // w_topline, start redrawing at w_topline.
1682 // If the end of the change is above w_topline: do like no change was
1683 // made, but redraw the first line to find changes in syntax.
1684 if (mod_top != 0 && mod_top < wp->w_topline)
1685 {
1686 if (mod_bot > wp->w_topline)
1687 mod_top = wp->w_topline;
1688#ifdef FEAT_SYN_HL
1689 else if (syntax_present(wp))
1690 top_end = 1;
1691#endif
1692 }
1693
1694 // When line numbers are displayed need to redraw all lines below
1695 // inserted/deleted lines.
1696 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1697 mod_bot = MAXLNUM;
1698 }
1699 wp->w_redraw_top = 0; // reset for next time
1700 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001701#ifdef FEAT_SEARCH_EXTRA
1702 search_hl_has_cursor_lnum = 0;
1703#endif
1704
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001705
1706 // When only displaying the lines at the top, set top_end. Used when
1707 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001708 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001709 {
1710 j = 0;
1711 for (i = 0; i < wp->w_lines_valid; ++i)
1712 {
1713 j += wp->w_lines[i].wl_size;
1714 if (j >= wp->w_upd_rows)
1715 {
1716 top_end = j;
1717 break;
1718 }
1719 }
1720 if (top_end == 0)
1721 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001722 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001723 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001724 // top area defined, the rest is UPD_VALID
1725 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001726 }
1727
1728 // Trick: we want to avoid clearing the screen twice. screenclear() will
1729 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1730 // non-zero and thus not FALSE) will indicate that screenclear() was not
1731 // called.
1732 if (screen_cleared)
1733 screen_cleared = MAYBE;
1734
1735 // If there are no changes on the screen that require a complete redraw,
1736 // handle three cases:
1737 // 1: we are off the top of the screen by a few lines: scroll down
1738 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1739 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1740 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001741 if ((type == UPD_VALID || type == UPD_SOME_VALID
1742 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001743#ifdef FEAT_DIFF
1744 && !wp->w_botfill && !wp->w_old_botfill
1745#endif
1746 )
1747 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001748 if (mod_top != 0
1749 && wp->w_topline == mod_top
1750 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001751 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001752 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001753 // w_topline is the first changed line and window is not scrolled,
1754 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001755 }
1756 else if (wp->w_lines[0].wl_valid
1757 && (wp->w_topline < wp->w_lines[0].wl_lnum
1758#ifdef FEAT_DIFF
1759 || (wp->w_topline == wp->w_lines[0].wl_lnum
1760 && wp->w_topfill > wp->w_old_topfill)
1761#endif
1762 ))
1763 {
1764 // New topline is above old topline: May scroll down.
1765#ifdef FEAT_FOLDING
1766 if (hasAnyFolding(wp))
1767 {
1768 linenr_T ln;
1769
1770 // count the number of lines we are off, counting a sequence
1771 // of folded lines as one
1772 j = 0;
1773 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1774 {
1775 ++j;
1776 if (j >= wp->w_height - 2)
1777 break;
1778 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1779 }
1780 }
1781 else
1782#endif
1783 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1784 if (j < wp->w_height - 2) // not too far off
1785 {
1786 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1787#ifdef FEAT_DIFF
1788 // insert extra lines for previously invisible filler lines
1789 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1790 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1791 - wp->w_old_topfill;
1792#endif
1793 if (i < wp->w_height - 2) // less than a screen off
1794 {
1795 // Try to insert the correct number of lines.
1796 // If not the last window, delete the lines at the bottom.
1797 // win_ins_lines may fail when the terminal can't do it.
1798 if (i > 0)
1799 check_for_delay(FALSE);
1800 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1801 {
1802 if (wp->w_lines_valid != 0)
1803 {
1804 // Need to update rows that are new, stop at the
1805 // first one that scrolled down.
1806 top_end = i;
1807 scrolled_down = TRUE;
1808
1809 // Move the entries that were scrolled, disable
1810 // the entries for the lines to be redrawn.
Bram Moolenaarfdc5d172022-08-11 15:52:14 +01001811 // Avoid using a wrong index when 'cmdheight' is
1812 // zero and wp->w_height == Rows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001813 if ((wp->w_lines_valid += j) > wp->w_height)
1814 wp->w_lines_valid = wp->w_height;
Bram Moolenaarfdc5d172022-08-11 15:52:14 +01001815 for (idx = wp->w_lines_valid >= wp->w_height
1816 ? wp->w_height - 1 : wp->w_lines_valid;
1817 idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001818 wp->w_lines[idx] = wp->w_lines[idx - j];
1819 while (idx >= 0)
1820 wp->w_lines[idx--].wl_valid = FALSE;
1821 }
1822 }
1823 else
1824 mid_start = 0; // redraw all lines
1825 }
1826 else
1827 mid_start = 0; // redraw all lines
1828 }
1829 else
1830 mid_start = 0; // redraw all lines
1831 }
1832 else
1833 {
1834 // New topline is at or below old topline: May scroll up.
1835 // When topline didn't change, find first entry in w_lines[] that
1836 // needs updating.
1837
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001838 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1839 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001840 j = -1;
1841 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001842 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001843 {
1844 if (wp->w_lines[i].wl_valid
1845 && wp->w_lines[i].wl_lnum == wp->w_topline)
1846 {
1847 j = i;
1848 break;
1849 }
1850 row += wp->w_lines[i].wl_size;
1851 }
1852 if (j == -1)
1853 {
1854 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1855 // lines
1856 mid_start = 0;
1857 }
1858 else
1859 {
1860 // Try to delete the correct number of lines.
1861 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1862#ifdef FEAT_DIFF
1863 // If the topline didn't change, delete old filler lines,
1864 // otherwise delete filler lines of the new topline...
1865 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1866 row += wp->w_old_topfill;
1867 else
1868 row += diff_check_fill(wp, wp->w_topline);
1869 // ... but don't delete new filler lines.
1870 row -= wp->w_topfill;
1871#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001872 if (row > Rows) // just in case
1873 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001874 if (row > 0)
1875 {
1876 check_for_delay(FALSE);
1877 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1878 == OK)
1879 bot_start = wp->w_height - row;
1880 else
1881 mid_start = 0; // redraw all lines
1882 }
1883 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1884 {
1885 // Skip the lines (below the deleted lines) that are still
1886 // valid and don't need redrawing. Copy their info
1887 // upwards, to compensate for the deleted lines. Set
1888 // bot_start to the first row that needs redrawing.
1889 bot_start = 0;
1890 idx = 0;
1891 for (;;)
1892 {
1893 wp->w_lines[idx] = wp->w_lines[j];
1894 // stop at line that didn't fit, unless it is still
1895 // valid (no lines deleted)
1896 if (row > 0 && bot_start + row
1897 + (int)wp->w_lines[j].wl_size > wp->w_height)
1898 {
1899 wp->w_lines_valid = idx + 1;
1900 break;
1901 }
1902 bot_start += wp->w_lines[idx++].wl_size;
1903
1904 // stop at the last valid entry in w_lines[].wl_size
1905 if (++j >= wp->w_lines_valid)
1906 {
1907 wp->w_lines_valid = idx;
1908 break;
1909 }
1910 }
1911#ifdef FEAT_DIFF
1912 // Correct the first entry for filler lines at the top
1913 // when it won't get updated below.
1914 if (wp->w_p_diff && bot_start > 0)
1915 wp->w_lines[0].wl_size =
1916 plines_win_nofill(wp, wp->w_topline, TRUE)
1917 + wp->w_topfill;
1918#endif
1919 }
1920 }
1921 }
1922
1923 // When starting redraw in the first line, redraw all lines. When
1924 // there is only one window it's probably faster to clear the screen
1925 // first.
1926 if (mid_start == 0)
1927 {
1928 mid_end = wp->w_height;
1929 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
1930 {
1931 // Clear the screen when it was not done by win_del_lines() or
1932 // win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1933 // then.
1934 if (screen_cleared != TRUE)
1935 screenclear();
1936 // The screen was cleared, redraw the tab pages line.
1937 if (redraw_tabline)
1938 draw_tabline();
1939 }
1940 }
1941
1942 // When win_del_lines() or win_ins_lines() caused the screen to be
1943 // cleared (only happens for the first window) or when screenclear()
1944 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001945 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001946 if (screen_cleared == TRUE)
1947 must_redraw = 0;
1948 }
1949 else
1950 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001951 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001952 mid_start = 0;
1953 mid_end = wp->w_height;
1954 }
1955
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001956 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001957 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001958 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001959 mid_start = 0;
1960 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001961 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001962 }
1963
1964 // check if we are updating or removing the inverted part
1965 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001966 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001967 {
1968 linenr_T from, to;
1969
1970 if (VIsual_active)
1971 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001972 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001973 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001974 {
1975 // If the type of Visual selection changed, redraw the whole
1976 // selection. Also when the ownership of the X selection is
1977 // gained or lost.
1978 if (curwin->w_cursor.lnum < VIsual.lnum)
1979 {
1980 from = curwin->w_cursor.lnum;
1981 to = VIsual.lnum;
1982 }
1983 else
1984 {
1985 from = VIsual.lnum;
1986 to = curwin->w_cursor.lnum;
1987 }
1988 // redraw more when the cursor moved as well
1989 if (wp->w_old_cursor_lnum < from)
1990 from = wp->w_old_cursor_lnum;
1991 if (wp->w_old_cursor_lnum > to)
1992 to = wp->w_old_cursor_lnum;
1993 if (wp->w_old_visual_lnum < from)
1994 from = wp->w_old_visual_lnum;
1995 if (wp->w_old_visual_lnum > to)
1996 to = wp->w_old_visual_lnum;
1997 }
1998 else
1999 {
2000 // Find the line numbers that need to be updated: The lines
2001 // between the old cursor position and the current cursor
2002 // position. Also check if the Visual position changed.
2003 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
2004 {
2005 from = curwin->w_cursor.lnum;
2006 to = wp->w_old_cursor_lnum;
2007 }
2008 else
2009 {
2010 from = wp->w_old_cursor_lnum;
2011 to = curwin->w_cursor.lnum;
2012 if (from == 0) // Visual mode just started
2013 from = to;
2014 }
2015
2016 if (VIsual.lnum != wp->w_old_visual_lnum
2017 || VIsual.col != wp->w_old_visual_col)
2018 {
2019 if (wp->w_old_visual_lnum < from
2020 && wp->w_old_visual_lnum != 0)
2021 from = wp->w_old_visual_lnum;
2022 if (wp->w_old_visual_lnum > to)
2023 to = wp->w_old_visual_lnum;
2024 if (VIsual.lnum < from)
2025 from = VIsual.lnum;
2026 if (VIsual.lnum > to)
2027 to = VIsual.lnum;
2028 }
2029 }
2030
2031 // If in block mode and changed column or curwin->w_curswant:
2032 // update all lines.
2033 // First compute the actual start and end column.
2034 if (VIsual_mode == Ctrl_V)
2035 {
2036 colnr_T fromc, toc;
2037#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002038 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002039
2040 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002041 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002042#endif
2043 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002044 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002045#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002046 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002047#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002048 // Highlight to the end of the line, unless 'virtualedit' has
2049 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002050 if (curwin->w_curswant == MAXCOL)
2051 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002052 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002053 {
2054 pos_T pos;
2055 int cursor_above =
2056 curwin->w_cursor.lnum < VIsual.lnum;
2057
2058 // Need to find the longest line.
2059 toc = 0;
2060 pos.coladd = 0;
2061 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2062 ? pos.lnum <= VIsual.lnum
2063 : pos.lnum >= VIsual.lnum;
2064 pos.lnum += cursor_above ? 1 : -1)
2065 {
2066 colnr_T t;
2067
Bram Moolenaar6bcb1822021-07-09 15:54:00 +02002068 pos.col = (int)STRLEN(ml_get_buf(wp->w_buffer,
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002069 pos.lnum, FALSE));
2070 getvvcol(wp, &pos, NULL, NULL, &t);
2071 if (toc < t)
2072 toc = t;
2073 }
2074 ++toc;
2075 }
2076 else
2077 toc = MAXCOL;
2078 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002079
2080 if (fromc != wp->w_old_cursor_fcol
2081 || toc != wp->w_old_cursor_lcol)
2082 {
2083 if (from > VIsual.lnum)
2084 from = VIsual.lnum;
2085 if (to < VIsual.lnum)
2086 to = VIsual.lnum;
2087 }
2088 wp->w_old_cursor_fcol = fromc;
2089 wp->w_old_cursor_lcol = toc;
2090 }
2091 }
2092 else
2093 {
2094 // Use the line numbers of the old Visual area.
2095 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2096 {
2097 from = wp->w_old_cursor_lnum;
2098 to = wp->w_old_visual_lnum;
2099 }
2100 else
2101 {
2102 from = wp->w_old_visual_lnum;
2103 to = wp->w_old_cursor_lnum;
2104 }
2105 }
2106
2107 // There is no need to update lines above the top of the window.
2108 if (from < wp->w_topline)
2109 from = wp->w_topline;
2110
2111 // If we know the value of w_botline, use it to restrict the update to
2112 // the lines that are visible in the window.
2113 if (wp->w_valid & VALID_BOTLINE)
2114 {
2115 if (from >= wp->w_botline)
2116 from = wp->w_botline - 1;
2117 if (to >= wp->w_botline)
2118 to = wp->w_botline - 1;
2119 }
2120
2121 // Find the minimal part to be updated.
2122 // Watch out for scrolling that made entries in w_lines[] invalid.
2123 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2124 // top_end; need to redraw from top_end to the "to" line.
2125 // A middle mouse click with a Visual selection may change the text
2126 // above the Visual area and reset wl_valid, do count these for
2127 // mid_end (in srow).
2128 if (mid_start > 0)
2129 {
2130 lnum = wp->w_topline;
2131 idx = 0;
2132 srow = 0;
2133 if (scrolled_down)
2134 mid_start = top_end;
2135 else
2136 mid_start = 0;
2137 while (lnum < from && idx < wp->w_lines_valid) // find start
2138 {
2139 if (wp->w_lines[idx].wl_valid)
2140 mid_start += wp->w_lines[idx].wl_size;
2141 else if (!scrolled_down)
2142 srow += wp->w_lines[idx].wl_size;
2143 ++idx;
2144# ifdef FEAT_FOLDING
2145 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2146 lnum = wp->w_lines[idx].wl_lnum;
2147 else
2148# endif
2149 ++lnum;
2150 }
2151 srow += mid_start;
2152 mid_end = wp->w_height;
2153 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2154 {
2155 if (wp->w_lines[idx].wl_valid
2156 && wp->w_lines[idx].wl_lnum >= to + 1)
2157 {
2158 // Only update until first row of this line
2159 mid_end = srow;
2160 break;
2161 }
2162 srow += wp->w_lines[idx].wl_size;
2163 }
2164 }
2165 }
2166
2167 if (VIsual_active && buf == curwin->w_buffer)
2168 {
2169 wp->w_old_visual_mode = VIsual_mode;
2170 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2171 wp->w_old_visual_lnum = VIsual.lnum;
2172 wp->w_old_visual_col = VIsual.col;
2173 wp->w_old_curswant = curwin->w_curswant;
2174 }
2175 else
2176 {
2177 wp->w_old_visual_mode = 0;
2178 wp->w_old_cursor_lnum = 0;
2179 wp->w_old_visual_lnum = 0;
2180 wp->w_old_visual_col = 0;
2181 }
2182
2183#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2184 // reset got_int, otherwise regexp won't work
2185 save_got_int = got_int;
2186 got_int = 0;
2187#endif
2188#ifdef SYN_TIME_LIMIT
2189 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002190 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002191 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002192#endif
2193#ifdef FEAT_FOLDING
2194 win_foldinfo.fi_level = 0;
2195#endif
2196
2197#ifdef FEAT_MENU
2198 // Draw the window toolbar, if there is one.
2199 // TODO: only when needed.
2200 if (winbar_height(wp) > 0)
2201 redraw_win_toolbar(wp);
2202#endif
2203
2204 // Update all the window rows.
2205 idx = 0; // first entry in w_lines[].wl_size
2206 row = 0;
2207 srow = 0;
2208 lnum = wp->w_topline; // first line shown in window
2209 for (;;)
2210 {
2211 // stop updating when reached the end of the window (check for _past_
2212 // the end of the window is at the end of the loop)
2213 if (row == wp->w_height)
2214 {
2215 didline = TRUE;
2216 break;
2217 }
2218
2219 // stop updating when hit the end of the file
2220 if (lnum > buf->b_ml.ml_line_count)
2221 {
2222 eof = TRUE;
2223 break;
2224 }
2225
2226 // Remember the starting row of the line that is going to be dealt
2227 // with. It is used further down when the line doesn't fit.
2228 srow = row;
2229
2230 // Update a line when it is in an area that needs updating, when it
2231 // has changes or w_lines[idx] is invalid.
2232 // "bot_start" may be halfway a wrapped line after using
2233 // win_del_lines(), check if the current line includes it.
2234 // When syntax folding is being used, the saved syntax states will
2235 // already have been updated, we can't see where the syntax state is
2236 // the same again, just update until the end of the window.
2237 if (row < top_end
2238 || (row >= mid_start && row < mid_end)
2239#ifdef FEAT_SEARCH_EXTRA
2240 || top_to_mod
2241#endif
2242 || idx >= wp->w_lines_valid
2243 || (row + wp->w_lines[idx].wl_size > bot_start)
2244 || (mod_top != 0
2245 && (lnum == mod_top
2246 || (lnum >= mod_top
2247 && (lnum < mod_bot
2248#ifdef FEAT_SYN_HL
2249 || did_update == DID_FOLD
2250 || (did_update == DID_LINE
2251 && syntax_present(wp)
2252 && (
2253# ifdef FEAT_FOLDING
2254 (foldmethodIsSyntax(wp)
2255 && hasAnyFolding(wp)) ||
2256# endif
2257 syntax_check_changed(lnum)))
2258#endif
2259#ifdef FEAT_SEARCH_EXTRA
2260 // match in fixed position might need redraw
2261 // if lines were inserted or deleted
2262 || (wp->w_match_head != NULL
2263 && buf->b_mod_xlines != 0)
2264#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002265 ))))
2266#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002267 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2268 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002269#endif
2270 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002271 {
2272#ifdef FEAT_SEARCH_EXTRA
2273 if (lnum == mod_top)
2274 top_to_mod = FALSE;
2275#endif
2276
2277 // When at start of changed lines: May scroll following lines
2278 // up or down to minimize redrawing.
2279 // Don't do this when the change continues until the end.
2280 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002281 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002282 if (lnum == mod_top
2283 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002284 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2285 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002286 {
2287 int old_rows = 0;
2288 int new_rows = 0;
2289 int xtra_rows;
2290 linenr_T l;
2291
2292 // Count the old number of window rows, using w_lines[], which
2293 // should still contain the sizes for the lines as they are
2294 // currently displayed.
2295 for (i = idx; i < wp->w_lines_valid; ++i)
2296 {
2297 // Only valid lines have a meaningful wl_lnum. Invalid
2298 // lines are part of the changed area.
2299 if (wp->w_lines[i].wl_valid
2300 && wp->w_lines[i].wl_lnum == mod_bot)
2301 break;
2302 old_rows += wp->w_lines[i].wl_size;
2303#ifdef FEAT_FOLDING
2304 if (wp->w_lines[i].wl_valid
2305 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2306 {
2307 // Must have found the last valid entry above mod_bot.
2308 // Add following invalid entries.
2309 ++i;
2310 while (i < wp->w_lines_valid
2311 && !wp->w_lines[i].wl_valid)
2312 old_rows += wp->w_lines[i++].wl_size;
2313 break;
2314 }
2315#endif
2316 }
2317
2318 if (i >= wp->w_lines_valid)
2319 {
2320 // We can't find a valid line below the changed lines,
2321 // need to redraw until the end of the window.
2322 // Inserting/deleting lines has no use.
2323 bot_start = 0;
2324 }
2325 else
2326 {
2327 // Able to count old number of rows: Count new window
2328 // rows, and may insert/delete lines
2329 j = idx;
2330 for (l = lnum; l < mod_bot; ++l)
2331 {
2332#ifdef FEAT_FOLDING
2333 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2334 ++new_rows;
2335 else
2336#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002337 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002338#ifdef FEAT_DIFF
2339 if (l == wp->w_topline)
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002340 new_rows += plines_win_nofill(wp, l, TRUE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002341 + wp->w_topfill;
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002342 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002343#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002344 new_rows += plines_win(wp, l, TRUE);
2345 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002346 ++j;
2347 if (new_rows > wp->w_height - row - 2)
2348 {
2349 // it's getting too much, must redraw the rest
2350 new_rows = 9999;
2351 break;
2352 }
2353 }
2354 xtra_rows = new_rows - old_rows;
2355 if (xtra_rows < 0)
2356 {
2357 // May scroll text up. If there is not enough
2358 // remaining text or scrolling fails, must redraw the
2359 // rest. If scrolling works, must redraw the text
2360 // below the scrolled text.
2361 if (row - xtra_rows >= wp->w_height - 2)
2362 mod_bot = MAXLNUM;
2363 else
2364 {
2365 check_for_delay(FALSE);
2366 if (win_del_lines(wp, row,
2367 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2368 mod_bot = MAXLNUM;
2369 else
2370 bot_start = wp->w_height + xtra_rows;
2371 }
2372 }
2373 else if (xtra_rows > 0)
2374 {
2375 // May scroll text down. If there is not enough
2376 // remaining text of scrolling fails, must redraw the
2377 // rest.
2378 if (row + xtra_rows >= wp->w_height - 2)
2379 mod_bot = MAXLNUM;
2380 else
2381 {
2382 check_for_delay(FALSE);
2383 if (win_ins_lines(wp, row + old_rows,
2384 xtra_rows, FALSE, FALSE) == FAIL)
2385 mod_bot = MAXLNUM;
2386 else if (top_end > row + old_rows)
2387 // Scrolled the part at the top that requires
2388 // updating down.
2389 top_end += xtra_rows;
2390 }
2391 }
2392
2393 // When not updating the rest, may need to move w_lines[]
2394 // entries.
2395 if (mod_bot != MAXLNUM && i != j)
2396 {
2397 if (j < i)
2398 {
2399 int x = row + new_rows;
2400
2401 // move entries in w_lines[] upwards
2402 for (;;)
2403 {
2404 // stop at last valid entry in w_lines[]
2405 if (i >= wp->w_lines_valid)
2406 {
2407 wp->w_lines_valid = j;
2408 break;
2409 }
2410 wp->w_lines[j] = wp->w_lines[i];
2411 // stop at a line that won't fit
2412 if (x + (int)wp->w_lines[j].wl_size
2413 > wp->w_height)
2414 {
2415 wp->w_lines_valid = j + 1;
2416 break;
2417 }
2418 x += wp->w_lines[j++].wl_size;
2419 ++i;
2420 }
2421 if (bot_start > x)
2422 bot_start = x;
2423 }
2424 else // j > i
2425 {
2426 // move entries in w_lines[] downwards
2427 j -= i;
2428 wp->w_lines_valid += j;
2429 if (wp->w_lines_valid > wp->w_height)
2430 wp->w_lines_valid = wp->w_height;
2431 for (i = wp->w_lines_valid; i - j >= idx; --i)
Bram Moolenaar7a99da42022-08-28 22:21:01 +01002432 if (i < Rows)
2433 wp->w_lines[i] = wp->w_lines[i - j];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002434
2435 // The w_lines[] entries for inserted lines are
2436 // now invalid, but wl_size may be used above.
2437 // Reset to zero.
2438 while (i >= idx)
2439 {
2440 wp->w_lines[i].wl_size = 0;
2441 wp->w_lines[i--].wl_valid = FALSE;
2442 }
2443 }
2444 }
2445 }
2446 }
2447
2448#ifdef FEAT_FOLDING
2449 // When lines are folded, display one line for all of them.
2450 // Otherwise, display normally (can be several display lines when
2451 // 'wrap' is on).
2452 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2453 if (fold_count != 0)
2454 {
2455 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2456 ++row;
2457 --fold_count;
2458 wp->w_lines[idx].wl_folded = TRUE;
2459 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2460# ifdef FEAT_SYN_HL
2461 did_update = DID_FOLD;
2462# endif
2463 }
2464 else
2465#endif
2466 if (idx < wp->w_lines_valid
2467 && wp->w_lines[idx].wl_valid
2468 && wp->w_lines[idx].wl_lnum == lnum
2469 && lnum > wp->w_topline
2470 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2471 && !WIN_IS_POPUP(wp)
2472 && srow + wp->w_lines[idx].wl_size > wp->w_height
2473#ifdef FEAT_DIFF
2474 && diff_check_fill(wp, lnum) == 0
2475#endif
2476 )
2477 {
2478 // This line is not going to fit. Don't draw anything here,
2479 // will draw "@ " lines below.
2480 row = wp->w_height + 1;
2481 }
2482 else
2483 {
2484#ifdef FEAT_SEARCH_EXTRA
2485 prepare_search_hl(wp, &screen_search_hl, lnum);
2486#endif
2487#ifdef FEAT_SYN_HL
2488 // Let the syntax stuff know we skipped a few lines.
2489 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2490 && syntax_present(wp))
2491 syntax_end_parsing(syntax_last_parsed + 1);
2492#endif
2493
2494 // Display one line.
2495 row = win_line(wp, lnum, srow, wp->w_height,
2496 mod_top == 0, FALSE);
2497
2498#ifdef FEAT_FOLDING
2499 wp->w_lines[idx].wl_folded = FALSE;
2500 wp->w_lines[idx].wl_lastlnum = lnum;
2501#endif
2502#ifdef FEAT_SYN_HL
2503 did_update = DID_LINE;
2504 syntax_last_parsed = lnum;
2505#endif
2506 }
2507
2508 wp->w_lines[idx].wl_lnum = lnum;
2509 wp->w_lines[idx].wl_valid = TRUE;
2510
2511 // Past end of the window or end of the screen. Note that after
2512 // resizing wp->w_height may be end up too big. That's a problem
2513 // elsewhere, but prevent a crash here.
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002514 if (row > wp->w_height
2515 || row + wp->w_winrow >= (p_ch > 0 ? Rows : Rows + 1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002516 {
2517 // we may need the size of that too long line later on
2518 if (dollar_vcol == -1)
2519 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2520 ++idx;
2521 break;
2522 }
2523 if (dollar_vcol == -1)
2524 wp->w_lines[idx].wl_size = row - srow;
2525 ++idx;
2526#ifdef FEAT_FOLDING
2527 lnum += fold_count + 1;
2528#else
2529 ++lnum;
2530#endif
2531 }
2532 else
2533 {
Lewis Russell16246392022-03-29 11:38:17 +01002534 if (wp->w_p_rnu && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002535 {
2536#ifdef FEAT_FOLDING
Lewis Russell16246392022-03-29 11:38:17 +01002537 // 'relativenumber' set and the cursor moved vertically: The
2538 // text doesn't need to be drawn, but the number column does.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002539 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2540 if (fold_count != 0)
2541 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2542 else
2543#endif
2544 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
2545 }
2546
2547 // This line does not need to be drawn, advance to the next one.
2548 row += wp->w_lines[idx++].wl_size;
2549 if (row > wp->w_height) // past end of screen
2550 break;
2551#ifdef FEAT_FOLDING
2552 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2553#else
2554 ++lnum;
2555#endif
2556#ifdef FEAT_SYN_HL
2557 did_update = DID_NONE;
2558#endif
2559 }
2560
2561 if (lnum > buf->b_ml.ml_line_count)
2562 {
2563 eof = TRUE;
2564 break;
2565 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002566
2567 // Safety check: if any of the wl_size values is wrong we might go over
2568 // the end of w_lines[].
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002569 if (idx >= (p_ch > 0 ? Rows : Rows + 1))
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002570 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002571 }
2572
2573 // End of loop over all window lines.
2574
zeertzjqc20e46a2022-03-23 14:55:23 +00002575#ifdef FEAT_SYN_HL
2576 // Now that the window has been redrawn with the old and new cursor line,
2577 // update w_last_cursorline.
2578 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2579#endif
Lewis Russell16246392022-03-29 11:38:17 +01002580 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002581
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002582#ifdef FEAT_VTP
2583 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002584 // See the version that was fixed.
2585 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002586 {
K.Takata54119102022-02-03 13:33:03 +00002587 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002588
K.Takata54119102022-02-03 13:33:03 +00002589 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002590 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002591 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2592 LineOffset[k] + screen_Columns) > 1)
2593 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002594 else
K.Takata54119102022-02-03 13:33:03 +00002595 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002596 else
K.Takata54119102022-02-03 13:33:03 +00002597 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002598 }
2599#endif
2600
2601 if (idx > wp->w_lines_valid)
2602 wp->w_lines_valid = idx;
2603
2604#ifdef FEAT_SYN_HL
2605 // Let the syntax stuff know we stop parsing here.
2606 if (syntax_last_parsed != 0 && syntax_present(wp))
2607 syntax_end_parsing(syntax_last_parsed + 1);
2608#endif
2609
2610 // If we didn't hit the end of the file, and we didn't finish the last
2611 // line we were working on, then the line didn't fit.
2612 wp->w_empty_rows = 0;
2613#ifdef FEAT_DIFF
2614 wp->w_filler_rows = 0;
2615#endif
2616 if (!eof && !didline)
2617 {
2618 if (lnum == wp->w_topline)
2619 {
2620 // Single line that does not fit!
2621 // Don't overwrite it, it can be edited.
2622 wp->w_botline = lnum + 1;
2623 }
2624#ifdef FEAT_DIFF
2625 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2626 {
2627 // Window ends in filler lines.
2628 wp->w_botline = lnum;
2629 wp->w_filler_rows = wp->w_height - srow;
2630 }
2631#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002632#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002633 else if (WIN_IS_POPUP(wp))
2634 {
2635 // popup line that doesn't fit is left as-is
2636 wp->w_botline = lnum;
2637 }
2638#endif
2639 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2640 {
2641 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2642
2643 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002644 screen_puts_len((char_u *)"@@", wp->w_width > 2 ? 2 : wp->w_width,
2645 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002646 screen_fill(scr_row, scr_row + 1,
2647 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
2648 '@', ' ', HL_ATTR(HLF_AT));
2649 set_empty_rows(wp, srow);
2650 wp->w_botline = lnum;
2651 }
2652 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2653 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002654 int start_col = (int)W_ENDCOL(wp) - 3;
2655
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002656 // Last line isn't finished: Display "@@@" at the end.
2657 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2658 W_WINROW(wp) + wp->w_height,
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002659 start_col < wp->w_wincol ? wp->w_wincol : start_col,
2660 (int)W_ENDCOL(wp),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002661 '@', '@', HL_ATTR(HLF_AT));
2662 set_empty_rows(wp, srow);
2663 wp->w_botline = lnum;
2664 }
2665 else
2666 {
2667 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
2668 wp->w_botline = lnum;
2669 }
2670 }
2671 else
2672 {
2673 draw_vsep_win(wp, row);
2674 if (eof) // we hit the end of the file
2675 {
2676 wp->w_botline = buf->b_ml.ml_line_count + 1;
2677#ifdef FEAT_DIFF
2678 j = diff_check_fill(wp, wp->w_botline);
2679 if (j > 0 && !wp->w_botfill)
2680 {
2681 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002682 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002683 i = '-';
2684 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002685 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002686 if (row + j > wp->w_height)
2687 j = wp->w_height - row;
2688 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2689 row += j;
2690 }
2691#endif
2692 }
2693 else if (dollar_vcol == -1)
2694 wp->w_botline = lnum;
2695
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002696 // Make sure the rest of the screen is blank.
2697 // write the "eob" character from 'fillchars' to rows that aren't part
2698 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002699 if (WIN_IS_POPUP(wp))
2700 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2701 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002702 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2703 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002704 }
2705
2706#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002707 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002708 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002709#endif
2710
2711 // Reset the type of redrawing required, the window has been updated.
2712 wp->w_redr_type = 0;
2713#ifdef FEAT_DIFF
2714 wp->w_old_topfill = wp->w_topfill;
2715 wp->w_old_botfill = wp->w_botfill;
2716#endif
2717
2718 if (dollar_vcol == -1)
2719 {
2720 // There is a trick with w_botline. If we invalidate it on each
2721 // change that might modify it, this will cause a lot of expensive
2722 // calls to plines() in update_topline() each time. Therefore the
2723 // value of w_botline is often approximated, and this value is used to
2724 // compute the value of w_topline. If the value of w_botline was
2725 // wrong, check that the value of w_topline is correct (cursor is on
2726 // the visible part of the text). If it's not, we need to redraw
2727 // again. Mostly this just means scrolling up a few lines, so it
2728 // doesn't look too bad. Only do this for the current window (where
2729 // changes are relevant).
2730 wp->w_valid |= VALID_BOTLINE;
2731 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2732 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002733 win_T *wwp;
2734#if defined(FEAT_CONCEAL)
2735 linenr_T old_topline = wp->w_topline;
2736 int new_wcol = wp->w_wcol;
2737#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002738 recursive = TRUE;
2739 curwin->w_valid &= ~VALID_TOPLINE;
2740 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002741
2742#if defined(FEAT_CONCEAL)
2743 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2744 != (VALID_WCOL|VALID_WROW))
2745 {
2746 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002747 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002748 // update_topline() the cursor's row or column got invalidated.
2749 // If they are left invalid, setcursor() will recompute them
2750 // but there won't be any further win_line() call to re-fix the
2751 // column and the cursor will end up misplaced. So we call
2752 // cursor validation now and reapply the fix again (or call
2753 // win_line() to do it for us).
2754 validate_cursor();
2755 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2756 && old_topline == wp->w_topline)
2757 wp->w_wcol = new_wcol;
2758 else
2759 redrawWinline(wp, wp->w_cursor.lnum);
2760 }
2761#endif
2762 // New redraw either due to updated topline or due to wcol fix.
2763 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002764 {
2765 // Don't update for changes in buffer again.
2766 i = curbuf->b_mod_set;
2767 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002768 j = curbuf->b_mod_xlines;
2769 curbuf->b_mod_xlines = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002770 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002771 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002772 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002773 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002774 // Other windows might have w_redr_type raised in update_topline().
2775 must_redraw = 0;
2776 FOR_ALL_WINDOWS(wwp)
2777 if (wwp->w_redr_type > must_redraw)
2778 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002779 recursive = FALSE;
2780 }
2781 }
2782
2783#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2784 // restore got_int, unless CTRL-C was hit while redrawing
2785 if (!got_int)
2786 got_int = save_got_int;
2787#endif
2788}
2789
2790#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2791/*
2792 * Prepare for updating one or more windows.
2793 * Caller must check for "updating_screen" already set to avoid recursiveness.
2794 */
2795 static void
2796update_prepare(void)
2797{
2798 cursor_off();
2799 updating_screen = TRUE;
2800#ifdef FEAT_GUI
2801 // Remove the cursor before starting to do anything, because scrolling may
2802 // make it difficult to redraw the text under it.
2803 if (gui.in_use)
2804 gui_undraw_cursor();
2805#endif
2806#ifdef FEAT_SEARCH_EXTRA
2807 start_search_hl();
2808#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002809#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002810 // Update popup_mask if needed.
2811 may_update_popup_mask(must_redraw);
2812#endif
2813}
2814
2815/*
2816 * Finish updating one or more windows.
2817 */
2818 static void
2819update_finish(void)
2820{
2821 if (redraw_cmdline || redraw_mode)
2822 showmode();
2823
2824# ifdef FEAT_SEARCH_EXTRA
2825 end_search_hl();
2826# endif
2827
2828 after_updating_screen(TRUE);
2829
2830# ifdef FEAT_GUI
2831 // Redraw the cursor and update the scrollbars when all screen updating is
2832 // done.
2833 if (gui.in_use)
2834 {
2835 out_flush_cursor(FALSE, FALSE);
2836 gui_update_scrollbars(FALSE);
2837 }
2838# endif
2839}
2840#endif
2841
2842#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2843 void
2844update_debug_sign(buf_T *buf, linenr_T lnum)
2845{
2846 win_T *wp;
2847 int doit = FALSE;
2848
2849# ifdef FEAT_FOLDING
2850 win_foldinfo.fi_level = 0;
2851# endif
2852
2853 // update/delete a specific sign
2854 redraw_buf_line_later(buf, lnum);
2855
2856 // check if it resulted in the need to redraw a window
2857 FOR_ALL_WINDOWS(wp)
2858 if (wp->w_redr_type != 0)
2859 doit = TRUE;
2860
2861 // Return when there is nothing to do, screen updating is already
2862 // happening (recursive call), messages on the screen or still starting up.
2863 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002864 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002865 || msg_scrolled
2866#ifdef FEAT_GUI
2867 || gui.starting
2868#endif
2869 || starting)
2870 return;
2871
2872 // update all windows that need updating
2873 update_prepare();
2874
2875 FOR_ALL_WINDOWS(wp)
2876 {
2877 if (wp->w_redr_type != 0)
2878 win_update(wp);
2879 if (wp->w_redr_status)
2880 win_redr_status(wp, FALSE);
2881 }
2882
2883 update_finish();
2884}
2885#endif
2886
2887#if defined(FEAT_GUI) || defined(PROTO)
2888/*
2889 * Update a single window, its status line and maybe the command line msg.
2890 * Used for the GUI scrollbar.
2891 */
2892 void
2893updateWindow(win_T *wp)
2894{
2895 // return if already busy updating
2896 if (updating_screen)
2897 return;
2898
2899 update_prepare();
2900
2901#ifdef FEAT_CLIPBOARD
2902 // When Visual area changed, may have to update selection.
2903 if (clip_star.available && clip_isautosel_star())
2904 clip_update_selection(&clip_star);
2905 if (clip_plus.available && clip_isautosel_plus())
2906 clip_update_selection(&clip_plus);
2907#endif
2908
2909 win_update(wp);
2910
2911 // When the screen was cleared redraw the tab pages line.
2912 if (redraw_tabline)
2913 draw_tabline();
2914
2915 if (wp->w_redr_status
2916# ifdef FEAT_CMDL_INFO
2917 || p_ru
2918# endif
2919# ifdef FEAT_STL_OPT
2920 || *p_stl != NUL || *wp->w_p_stl != NUL
2921# endif
2922 )
2923 win_redr_status(wp, FALSE);
2924
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002925#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002926 // Display popup windows on top of everything.
2927 update_popups(win_update);
2928#endif
2929
2930 update_finish();
2931}
2932#endif
2933
2934#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2935/*
2936 * Redraw as soon as possible. When the command line is not scrolled redraw
2937 * right away and restore what was on the command line.
2938 * Return a code indicating what happened.
2939 */
2940 int
2941redraw_asap(int type)
2942{
2943 int rows;
2944 int cols = screen_Columns;
2945 int r;
2946 int ret = 0;
2947 schar_T *screenline; // copy from ScreenLines[]
2948 sattr_T *screenattr; // copy from ScreenAttrs[]
2949 int i;
2950 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2951 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2952 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2953
2954 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002955 if (msg_scrolled
2956 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
2957 || exiting
2958 || p_ch == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002959 return ret;
2960
2961 // Allocate space to save the text displayed in the command line area.
2962 rows = screen_Rows - cmdline_row;
2963 screenline = LALLOC_MULT(schar_T, rows * cols);
2964 screenattr = LALLOC_MULT(sattr_T, rows * cols);
2965 if (screenline == NULL || screenattr == NULL)
2966 ret = 2;
2967 if (enc_utf8)
2968 {
2969 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
2970 if (screenlineUC == NULL)
2971 ret = 2;
2972 for (i = 0; i < p_mco; ++i)
2973 {
2974 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
2975 if (screenlineC[i] == NULL)
2976 ret = 2;
2977 }
2978 }
2979 if (enc_dbcs == DBCS_JPNU)
2980 {
2981 screenline2 = LALLOC_MULT(schar_T, rows * cols);
2982 if (screenline2 == NULL)
2983 ret = 2;
2984 }
2985
2986 if (ret != 2)
2987 {
2988 // Save the text displayed in the command line area.
2989 for (r = 0; r < rows; ++r)
2990 {
2991 mch_memmove(screenline + r * cols,
2992 ScreenLines + LineOffset[cmdline_row + r],
2993 (size_t)cols * sizeof(schar_T));
2994 mch_memmove(screenattr + r * cols,
2995 ScreenAttrs + LineOffset[cmdline_row + r],
2996 (size_t)cols * sizeof(sattr_T));
2997 if (enc_utf8)
2998 {
2999 mch_memmove(screenlineUC + r * cols,
3000 ScreenLinesUC + LineOffset[cmdline_row + r],
3001 (size_t)cols * sizeof(u8char_T));
3002 for (i = 0; i < p_mco; ++i)
3003 mch_memmove(screenlineC[i] + r * cols,
3004 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3005 (size_t)cols * sizeof(u8char_T));
3006 }
3007 if (enc_dbcs == DBCS_JPNU)
3008 mch_memmove(screenline2 + r * cols,
3009 ScreenLines2 + LineOffset[cmdline_row + r],
3010 (size_t)cols * sizeof(schar_T));
3011 }
3012
3013 update_screen(0);
3014 ret = 3;
3015
3016 if (must_redraw == 0)
3017 {
3018 int off = (int)(current_ScreenLine - ScreenLines);
3019
3020 // Restore the text displayed in the command line area.
3021 for (r = 0; r < rows; ++r)
3022 {
3023 mch_memmove(current_ScreenLine,
3024 screenline + r * cols,
3025 (size_t)cols * sizeof(schar_T));
3026 mch_memmove(ScreenAttrs + off,
3027 screenattr + r * cols,
3028 (size_t)cols * sizeof(sattr_T));
3029 if (enc_utf8)
3030 {
3031 mch_memmove(ScreenLinesUC + off,
3032 screenlineUC + r * cols,
3033 (size_t)cols * sizeof(u8char_T));
3034 for (i = 0; i < p_mco; ++i)
3035 mch_memmove(ScreenLinesC[i] + off,
3036 screenlineC[i] + r * cols,
3037 (size_t)cols * sizeof(u8char_T));
3038 }
3039 if (enc_dbcs == DBCS_JPNU)
3040 mch_memmove(ScreenLines2 + off,
3041 screenline2 + r * cols,
3042 (size_t)cols * sizeof(schar_T));
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003043 screen_line(curwin, cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003044 }
3045 ret = 4;
3046 }
3047 }
3048
3049 vim_free(screenline);
3050 vim_free(screenattr);
3051 if (enc_utf8)
3052 {
3053 vim_free(screenlineUC);
3054 for (i = 0; i < p_mco; ++i)
3055 vim_free(screenlineC[i]);
3056 }
3057 if (enc_dbcs == DBCS_JPNU)
3058 vim_free(screenline2);
3059
3060 // Show the intro message when appropriate.
3061 maybe_intro_message();
3062
3063 setcursor();
3064
3065 return ret;
3066}
3067#endif
3068
3069/*
3070 * Invoked after an asynchronous callback is called.
3071 * If an echo command was used the cursor needs to be put back where
3072 * it belongs. If highlighting was changed a redraw is needed.
3073 * If "call_update_screen" is FALSE don't call update_screen() when at the
3074 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003075 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003076 */
3077 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003078redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003079{
3080 ++redrawing_for_callback;
3081
Bram Moolenaar24959102022-05-07 20:01:16 +01003082 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3083 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3084 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003085 {
3086 if (do_message)
3087 repeat_message();
3088 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003089 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003090 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003091 if (pum_visible())
3092 cmdline_pum_display();
Bram Moolenaar54162322022-08-26 16:58:51 +01003093
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003094 // Don't redraw when in prompt_for_number().
3095 if (cmdline_row > 0)
3096 {
3097 // Redrawing only works when the screen didn't scroll. Don't clear
3098 // wildmenu entries.
3099 if (msg_scrolled == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003100 && wild_menu_showing == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003101 && call_update_screen)
3102 update_screen(0);
3103
3104 // Redraw in the same position, so that the user can continue
3105 // editing the command.
3106 redrawcmdline_ex(FALSE);
3107 }
3108 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003109 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003110 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003111 update_topline();
3112 validate_cursor();
3113
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003114 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003115 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003116 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003117
3118 if (msg_scrolled == 0)
3119 {
3120 // don't want a hit-enter prompt when something else is displayed
3121 msg_didany = FALSE;
3122 need_wait_return = FALSE;
3123 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003124 }
3125 cursor_on();
3126#ifdef FEAT_GUI
3127 if (gui.in_use && !gui_mch_is_blink_off())
3128 // Don't update the cursor when it is blinking and off to avoid
3129 // flicker.
3130 out_flush_cursor(FALSE, FALSE);
3131 else
3132#endif
3133 out_flush();
3134
3135 --redrawing_for_callback;
3136}
3137
3138/*
3139 * Redraw the current window later, with update_screen(type).
3140 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003141 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003142 */
3143 void
3144redraw_later(int type)
3145{
3146 redraw_win_later(curwin, type);
3147}
3148
3149 void
3150redraw_win_later(
3151 win_T *wp,
3152 int type)
3153{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003154 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003155 {
3156 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003157 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003158 wp->w_lines_valid = 0;
3159 if (must_redraw < type) // must_redraw is the maximum of all windows
3160 must_redraw = type;
3161 }
3162}
3163
3164/*
3165 * Force a complete redraw later. Also resets the highlighting. To be used
3166 * after executing a shell command that messes up the screen.
3167 */
3168 void
3169redraw_later_clear(void)
3170{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003171 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003172 reset_screen_attr();
3173}
3174
3175/*
3176 * Mark all windows to be redrawn later.
3177 */
3178 void
3179redraw_all_later(int type)
3180{
3181 win_T *wp;
3182
3183 FOR_ALL_WINDOWS(wp)
3184 redraw_win_later(wp, type);
3185 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003186 set_must_redraw(type);
3187}
3188
3189/*
3190 * Set "must_redraw" to "type" unless it already has a higher value
3191 * or it is currently not allowed.
3192 */
3193 void
3194set_must_redraw(int type)
3195{
3196 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003197 must_redraw = type;
3198}
3199
3200/*
3201 * Mark all windows that are editing the current buffer to be updated later.
3202 */
3203 void
3204redraw_curbuf_later(int type)
3205{
3206 redraw_buf_later(curbuf, type);
3207}
3208
3209 void
3210redraw_buf_later(buf_T *buf, int type)
3211{
3212 win_T *wp;
3213
3214 FOR_ALL_WINDOWS(wp)
3215 {
3216 if (wp->w_buffer == buf)
3217 redraw_win_later(wp, type);
3218 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003219#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3220 // terminal in popup window is not in list of windows
3221 if (curwin->w_buffer == buf)
3222 redraw_win_later(curwin, type);
3223#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003224}
3225
3226#if defined(FEAT_SIGNS) || defined(PROTO)
3227 void
3228redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3229{
3230 win_T *wp;
3231
3232 FOR_ALL_WINDOWS(wp)
3233 if (wp->w_buffer == buf && lnum >= wp->w_topline
3234 && lnum < wp->w_botline)
3235 redrawWinline(wp, lnum);
3236}
3237#endif
3238
3239#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3240 void
3241redraw_buf_and_status_later(buf_T *buf, int type)
3242{
3243 win_T *wp;
3244
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003245 if (wild_menu_showing != 0)
3246 // Don't redraw while the command line completion is displayed, it
3247 // would disappear.
3248 return;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003249 FOR_ALL_WINDOWS(wp)
3250 {
3251 if (wp->w_buffer == buf)
3252 {
3253 redraw_win_later(wp, type);
3254 wp->w_redr_status = TRUE;
3255 }
3256 }
3257}
3258#endif
3259
3260/*
3261 * mark all status lines for redraw; used after first :cd
3262 */
3263 void
3264status_redraw_all(void)
3265{
3266 win_T *wp;
3267
3268 FOR_ALL_WINDOWS(wp)
3269 if (wp->w_status_height)
3270 {
3271 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003272 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003273 }
3274}
3275
3276/*
3277 * mark all status lines of the current buffer for redraw
3278 */
3279 void
3280status_redraw_curbuf(void)
3281{
3282 win_T *wp;
3283
3284 FOR_ALL_WINDOWS(wp)
3285 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3286 {
3287 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003288 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003289 }
3290}
3291
3292/*
3293 * Redraw all status lines that need to be redrawn.
3294 */
3295 void
3296redraw_statuslines(void)
3297{
3298 win_T *wp;
3299
3300 FOR_ALL_WINDOWS(wp)
3301 if (wp->w_redr_status)
3302 win_redr_status(wp, FALSE);
3303 if (redraw_tabline)
3304 draw_tabline();
3305}
3306
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003307/*
3308 * Redraw all status lines at the bottom of frame "frp".
3309 */
3310 void
3311win_redraw_last_status(frame_T *frp)
3312{
3313 if (frp->fr_layout == FR_LEAF)
3314 frp->fr_win->w_redr_status = TRUE;
3315 else if (frp->fr_layout == FR_ROW)
3316 {
3317 FOR_ALL_FRAMES(frp, frp->fr_child)
3318 win_redraw_last_status(frp);
3319 }
3320 else // frp->fr_layout == FR_COL
3321 {
3322 frp = frp->fr_child;
3323 while (frp->fr_next != NULL)
3324 frp = frp->fr_next;
3325 win_redraw_last_status(frp);
3326 }
3327}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003328
3329/*
3330 * Changed something in the current window, at buffer line "lnum", that
3331 * requires that line and possibly other lines to be redrawn.
3332 * Used when entering/leaving Insert mode with the cursor on a folded line.
3333 * Used to remove the "$" from a change command.
3334 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3335 * may become invalid and the whole window will have to be redrawn.
3336 */
3337 void
3338redrawWinline(
3339 win_T *wp,
3340 linenr_T lnum)
3341{
3342 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
3343 wp->w_redraw_top = lnum;
3344 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
3345 wp->w_redraw_bot = lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003346 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003347}