blob: 529792f06cff0edd579a57a6346e424003ecbb0e [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;
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100173 if (type != UPD_CLEAR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200174 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100175 if (msg_scrolled > Rows - 5) // redrawing is faster
176 type = UPD_NOT_VALID;
177 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200178 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100179 check_for_delay(FALSE);
180 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
181 == FAIL)
182 type = UPD_NOT_VALID;
183 FOR_ALL_WINDOWS(wp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200184 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100185 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200186 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100187 if (W_WINROW(wp) + wp->w_height > msg_scrolled
188 && wp->w_redr_type < UPD_REDRAW_TOP
189 && wp->w_lines_valid > 0
190 && wp->w_topline == wp->w_lines[0].wl_lnum)
191 {
192 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
193 wp->w_redr_type = UPD_REDRAW_TOP;
194 }
195 else
196 {
197 wp->w_redr_type = UPD_NOT_VALID;
198 if (W_WINROW(wp) + wp->w_height
199 + wp->w_status_height <= msg_scrolled)
200 wp->w_redr_status = TRUE;
201 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200202 }
203 }
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100204 if (!no_update)
205 redraw_cmdline = TRUE;
206 redraw_tabline = TRUE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200207 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200208 }
209 msg_scrolled = 0;
210 need_wait_return = FALSE;
211 }
212
213 // reset cmdline_row now (may have been changed temporarily)
214 compute_cmdrow();
215
216 // Check for changed highlighting
217 if (need_highlight_changed)
218 highlight_changed();
219
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100220 if (type == UPD_CLEAR) // first clear screen
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200221 {
222 screenclear(); // will reset clear_cmdline
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100223 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200224 // must_redraw may be set indirectly, avoid another redraw later
225 must_redraw = 0;
226 }
227
228 if (clear_cmdline) // going to clear cmdline (done below)
229 check_for_delay(FALSE);
230
231#ifdef FEAT_LINEBREAK
232 // Force redraw when width of 'number' or 'relativenumber' column
233 // changes.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100234 if (curwin->w_redr_type < UPD_NOT_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200235 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
236 ? number_width(curwin) : 0))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100237 curwin->w_redr_type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200238#endif
239
240 // Only start redrawing if there is really something to do.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100241 if (type == UPD_INVERTED)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200242 update_curswant();
243 if (curwin->w_redr_type < type
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100244 && !((type == UPD_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200245 && curwin->w_lines[0].wl_valid
246#ifdef FEAT_DIFF
247 && curwin->w_topfill == curwin->w_old_topfill
248 && curwin->w_botfill == curwin->w_old_botfill
249#endif
250 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100251 || (type == UPD_INVERTED
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200252 && VIsual_active
253 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
254 && curwin->w_old_visual_mode == VIsual_mode
255 && (curwin->w_valid & VALID_VIRTCOL)
256 && curwin->w_old_curswant == curwin->w_curswant)
257 ))
258 curwin->w_redr_type = type;
259
260 // Redraw the tab pages line if needed.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100261 if (redraw_tabline || type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200262 draw_tabline();
263
264#ifdef FEAT_SYN_HL
265 // Correct stored syntax highlighting info for changes in each displayed
266 // buffer. Each buffer must only be done once.
267 FOR_ALL_WINDOWS(wp)
268 {
269 if (wp->w_buffer->b_mod_set)
270 {
271 win_T *wwp;
272
273 // Check if we already did this buffer.
274 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
275 if (wwp->w_buffer == wp->w_buffer)
276 break;
277 if (wwp == wp && syntax_present(wp))
278 syn_stack_apply_changes(wp->w_buffer);
279 }
280 }
281#endif
282
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200283 if (pum_redraw_in_same_position())
284 // Avoid flicker if the popup menu is going to be redrawn in the same
285 // position.
286 pum_will_redraw = TRUE;
287
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200288 // Go from top to bottom through the windows, redrawing the ones that need
289 // it.
290#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100291 did_update_one_window = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200292#endif
293#ifdef FEAT_SEARCH_EXTRA
294 screen_search_hl.rm.regprog = NULL;
295#endif
296 FOR_ALL_WINDOWS(wp)
297 {
298 if (wp->w_redr_type != 0)
299 {
300 cursor_off();
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100301#ifdef FEAT_GUI
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200302 if (!did_one)
303 {
304 did_one = TRUE;
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100305
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200306 // Remove the cursor before starting to do anything, because
307 // scrolling may make it difficult to redraw the text under
308 // it.
Bram Moolenaar09f067f2021-04-11 13:29:18 +0200309 // Also remove the cursor if it needs to be hidden due to an
310 // ongoing cursor-less sleep.
311 if (gui.in_use && (wp == curwin || cursor_is_sleeping()))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200312 {
313 gui_cursor_col = gui.cursor_col;
314 gui_cursor_row = gui.cursor_row;
315 gui_undraw_cursor();
316 did_undraw = TRUE;
317 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200318 }
319#endif
320 win_update(wp);
321 }
322
323 // redraw status line after the window to minimize cursor movement
324 if (wp->w_redr_status)
325 {
326 cursor_off();
327 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
328 }
329 }
330#if defined(FEAT_SEARCH_EXTRA)
331 end_search_hl();
332#endif
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200333
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200334 // May need to redraw the popup menu.
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200335 pum_will_redraw = save_pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200336 pum_may_redraw();
337
338 // Reset b_mod_set flags. Going through all windows is probably faster
339 // than going through all buffers (there could be many buffers).
340 FOR_ALL_WINDOWS(wp)
341 wp->w_buffer->b_mod_set = FALSE;
342
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100343#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200344 // Display popup windows on top of the windows and command line.
345 update_popups(win_update);
346#endif
347
Bram Moolenaar3194e5b2021-12-13 21:59:09 +0000348#ifdef FEAT_TERMINAL
349 FOR_ALL_WINDOWS(wp)
350 // If this window contains a terminal, after redrawing all windows, the
351 // dirty row range can be reset.
352 term_did_update_window(wp);
353#endif
354
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200355 after_updating_screen(TRUE);
356
357 // Clear or redraw the command line. Done last, because scrolling may
358 // mess up the command line.
359 if (clear_cmdline || redraw_cmdline || redraw_mode)
360 showmode();
361
362 if (no_update)
363 --no_win_do_lines_ins;
364
365 // May put up an introductory message when not editing a file
366 if (!did_intro)
367 maybe_intro_message();
368 did_intro = TRUE;
369
370#ifdef FEAT_GUI
371 // Redraw the cursor and update the scrollbars when all screen updating is
372 // done.
373 if (gui.in_use)
374 {
375 if (did_undraw && !gui_mch_is_blink_off())
376 {
377 mch_disable_flush();
378 out_flush(); // required before updating the cursor
379 mch_enable_flush();
380
381 // Put the GUI position where the cursor was, gui_update_cursor()
382 // uses that.
383 gui.col = gui_cursor_col;
384 gui.row = gui_cursor_row;
385 gui.col = mb_fix_col(gui.col, gui.row);
386 gui_update_cursor(FALSE, FALSE);
387 gui_may_flush();
388 screen_cur_col = gui.col;
389 screen_cur_row = gui.row;
390 }
391 else
392 out_flush();
393 gui_update_scrollbars(FALSE);
394 }
395#endif
396 return OK;
397}
398
399/*
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200400 * Return the row for drawing the statusline and the ruler of window "wp".
401 */
Bram Moolenaar49c51b82021-04-01 16:16:18 +0200402 int
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200403statusline_row(win_T *wp)
404{
405#if defined(FEAT_PROP_POPUP)
406 // If the window is really zero height the winbar isn't displayed.
407 if (wp->w_frame->fr_height == wp->w_status_height && !popup_is_popup(wp))
408 return wp->w_winrow;
409#endif
410 return W_WINROW(wp) + wp->w_height;
411}
412
413/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200414 * Redraw the status line of window wp.
415 *
416 * If inversion is possible we use it. Else '=' characters are used.
417 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
418 * displayed.
419 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200420 static void
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200421win_redr_status(win_T *wp, int ignore_pum UNUSED)
422{
423 int row;
424 char_u *p;
425 int len;
426 int fillchar;
427 int attr;
428 int this_ru_col;
429 static int busy = FALSE;
430
431 // It's possible to get here recursively when 'statusline' (indirectly)
432 // invokes ":redrawstatus". Simply ignore the call then.
433 if (busy)
434 return;
435 busy = TRUE;
436
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200437 row = statusline_row(wp);
438
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200439 wp->w_redr_status = FALSE;
440 if (wp->w_status_height == 0)
441 {
442 // no status line, can only be last window
443 redraw_cmdline = TRUE;
444 }
445 else if (!redrawing()
446 // don't update status line when popup menu is visible and may be
447 // drawn over it, unless it will be redrawn later
448 || (!ignore_pum && pum_visible()))
449 {
450 // Don't redraw right now, do it later.
451 wp->w_redr_status = TRUE;
452 }
453#ifdef FEAT_STL_OPT
454 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
455 {
456 // redraw custom status line
457 redraw_custom_statusline(wp);
458 }
459#endif
460 else
461 {
462 fillchar = fillchar_status(&attr, wp);
463
464 get_trans_bufname(wp->w_buffer);
465 p = NameBuff;
466 len = (int)STRLEN(p);
467
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000468 if ((bt_help(wp->w_buffer)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200469#ifdef FEAT_QUICKFIX
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000470 || wp->w_p_pvw
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200471#endif
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000472 || bufIsChanged(wp->w_buffer)
473 || wp->w_buffer->b_p_ro)
474 && len < MAXPATHL - 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200475 *(p + len++) = ' ';
476 if (bt_help(wp->w_buffer))
477 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100478 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Help]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200479 len += (int)STRLEN(p + len);
480 }
481#ifdef FEAT_QUICKFIX
482 if (wp->w_p_pvw)
483 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100484 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Preview]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200485 len += (int)STRLEN(p + len);
486 }
487#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100488 if (bufIsChanged(wp->w_buffer) && !bt_terminal(wp->w_buffer))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200489 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100490 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", "[+]");
491 len += (int)STRLEN(p + len);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200492 }
493 if (wp->w_buffer->b_p_ro)
494 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100495 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[RO]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200496 len += (int)STRLEN(p + len);
497 }
498
499 this_ru_col = ru_col - (Columns - wp->w_width);
500 if (this_ru_col < (wp->w_width + 1) / 2)
501 this_ru_col = (wp->w_width + 1) / 2;
502 if (this_ru_col <= 1)
503 {
504 p = (char_u *)"<"; // No room for file name!
505 len = 1;
506 }
507 else if (has_mbyte)
508 {
509 int clen = 0, i;
510
511 // Count total number of display cells.
512 clen = mb_string2cells(p, -1);
513
514 // Find first character that will fit.
515 // Going from start to end is much faster for DBCS.
516 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
517 i += (*mb_ptr2len)(p + i))
518 clen -= (*mb_ptr2cells)(p + i);
519 len = clen;
520 if (i > 0)
521 {
522 p = p + i - 1;
523 *p = '<';
524 ++len;
525 }
526
527 }
528 else if (len > this_ru_col - 1)
529 {
530 p += len - (this_ru_col - 1);
531 *p = '<';
532 len = this_ru_col - 1;
533 }
534
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200535 screen_puts(p, row, wp->w_wincol, attr);
536 screen_fill(row, row + 1, len + wp->w_wincol,
537 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
538
539 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000540 && (this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200541 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
542 - 1 + wp->w_wincol), attr);
543
544#ifdef FEAT_CMDL_INFO
545 win_redr_ruler(wp, TRUE, ignore_pum);
546#endif
547 }
548
549 /*
550 * May need to draw the character below the vertical separator.
551 */
552 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
553 {
554 if (stl_connected(wp))
555 fillchar = fillchar_status(&attr, wp);
556 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100557 fillchar = fillchar_vsep(&attr, wp);
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200558 screen_putchar(fillchar, row, W_ENDCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200559 }
560 busy = FALSE;
561}
562
563#ifdef FEAT_STL_OPT
564/*
565 * Redraw the status line according to 'statusline' and take care of any
566 * errors encountered.
567 */
568 static void
569redraw_custom_statusline(win_T *wp)
570{
571 static int entered = FALSE;
572 int saved_did_emsg = did_emsg;
573
574 // When called recursively return. This can happen when the statusline
575 // contains an expression that triggers a redraw.
576 if (entered)
577 return;
578 entered = TRUE;
579
580 did_emsg = FALSE;
581 win_redr_custom(wp, FALSE);
582 if (did_emsg)
583 {
584 // When there is an error disable the statusline, otherwise the
585 // display is messed up with errors and a redraw triggers the problem
586 // again and again.
587 set_string_option_direct((char_u *)"statusline", -1,
588 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
589 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
590 }
591 did_emsg |= saved_did_emsg;
592 entered = FALSE;
593}
594#endif
595
596/*
597 * Show current status info in ruler and various other places
598 * If always is FALSE, only show ruler if position has changed.
599 */
600 void
601showruler(int always)
602{
603 if (!always && !redrawing())
604 return;
605 if (pum_visible())
606 {
607 // Don't redraw right now, do it later.
608 curwin->w_redr_status = TRUE;
609 return;
610 }
611#if defined(FEAT_STL_OPT)
612 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
613 redraw_custom_statusline(curwin);
614 else
615#endif
616#ifdef FEAT_CMDL_INFO
617 win_redr_ruler(curwin, always, FALSE);
618#endif
619
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200620 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000621#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200622 || (p_icon && (stl_syntax & STL_IN_ICON))
623 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000624#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200625 )
626 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000627
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200628 // Redraw the tab pages line if needed.
629 if (redraw_tabline)
630 draw_tabline();
631}
632
633#if defined(FEAT_CMDL_INFO) || defined(PROTO)
634 void
635win_redr_ruler(win_T *wp, int always, int ignore_pum)
636{
637#define RULER_BUF_LEN 70
638 char_u buffer[RULER_BUF_LEN];
639 int row;
640 int fillchar;
641 int attr;
642 int empty_line = FALSE;
643 colnr_T virtcol;
644 int i;
645 size_t len;
646 int o;
647 int this_ru_col;
648 int off = 0;
649 int width;
650
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100651 // If 'ruler' off or messages area disabled, don't do anything
652 if (!p_ru || (wp->w_status_height == 0 && p_ch == 0))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200653 return;
654
655 /*
656 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
657 * after deleting lines, before cursor.lnum is corrected.
658 */
659 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
660 return;
661
662 // Don't draw the ruler while doing insert-completion, it might overwrite
663 // the (long) mode message.
664 if (wp == lastwin && lastwin->w_status_height == 0)
665 if (edit_submode != NULL)
666 return;
667 // Don't draw the ruler when the popup menu is visible, it may overlap.
668 // Except when the popup menu will be redrawn anyway.
669 if (!ignore_pum && pum_visible())
670 return;
671
672#ifdef FEAT_STL_OPT
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100673 if (*p_ruf && p_ch > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200674 {
Bram Moolenaar53989552019-12-23 22:59:18 +0100675 int called_emsg_before = called_emsg;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200676
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200677 win_redr_custom(wp, TRUE);
Bram Moolenaar53989552019-12-23 22:59:18 +0100678 if (called_emsg > called_emsg_before)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200679 set_string_option_direct((char_u *)"rulerformat", -1,
680 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200681 return;
682 }
683#endif
684
685 /*
686 * Check if not in Insert mode and the line is empty (will show "0-1").
687 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100688 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200689 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
690 empty_line = TRUE;
691
692 /*
693 * Only draw the ruler when something changed.
694 */
695 validate_virtcol_win(wp);
696 if ( redraw_cmdline
697 || always
698 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
699 || wp->w_cursor.col != wp->w_ru_cursor.col
700 || wp->w_virtcol != wp->w_ru_virtcol
701 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
702 || wp->w_topline != wp->w_ru_topline
703 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
704#ifdef FEAT_DIFF
705 || wp->w_topfill != wp->w_ru_topfill
706#endif
707 || empty_line != wp->w_ru_empty)
708 {
709 cursor_off();
710 if (wp->w_status_height)
711 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200712 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200713 fillchar = fillchar_status(&attr, wp);
714 off = wp->w_wincol;
715 width = wp->w_width;
716 }
717 else
718 {
719 row = Rows - 1;
720 fillchar = ' ';
721 attr = 0;
722 width = Columns;
723 off = 0;
724 }
725
726 // In list mode virtcol needs to be recomputed
727 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100728 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200729 {
730 wp->w_p_list = FALSE;
731 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
732 wp->w_p_list = TRUE;
733 }
734
735 /*
736 * Some sprintfs return the length, some return a pointer.
737 * To avoid portability problems we use strlen() here.
738 */
739 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
740 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
741 ? 0L
742 : (long)(wp->w_cursor.lnum));
743 len = STRLEN(buffer);
744 col_print(buffer + len, RULER_BUF_LEN - len,
745 empty_line ? 0 : (int)wp->w_cursor.col + 1,
746 (int)virtcol + 1);
747
748 /*
749 * Add a "50%" if there is room for it.
750 * On the last line, don't print in the last column (scrolls the
751 * screen up on some terminals).
752 */
753 i = (int)STRLEN(buffer);
754 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
755 o = i + vim_strsize(buffer + i + 1);
756 if (wp->w_status_height == 0) // can't use last char of screen
757 ++o;
758 this_ru_col = ru_col - (Columns - width);
759 if (this_ru_col < 0)
760 this_ru_col = 0;
761 // Never use more than half the window/screen width, leave the other
762 // half for the filename.
763 if (this_ru_col < (width + 1) / 2)
764 this_ru_col = (width + 1) / 2;
765 if (this_ru_col + o < width)
766 {
767 // need at least 3 chars left for get_rel_pos() + NUL
768 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
769 {
770 if (has_mbyte)
771 i += (*mb_char2bytes)(fillchar, buffer + i);
772 else
773 buffer[i++] = fillchar;
774 ++o;
775 }
776 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
777 }
778 // Truncate at window boundary.
779 if (has_mbyte)
780 {
781 o = 0;
782 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
783 {
784 o += (*mb_ptr2cells)(buffer + i);
785 if (this_ru_col + o > width)
786 {
787 buffer[i] = NUL;
788 break;
789 }
790 }
791 }
792 else if (this_ru_col + (int)STRLEN(buffer) > width)
793 buffer[width - this_ru_col] = NUL;
794
795 screen_puts(buffer, row, this_ru_col + off, attr);
796 i = redraw_cmdline;
797 screen_fill(row, row + 1,
798 this_ru_col + off + (int)STRLEN(buffer),
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000799 (off + width),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200800 fillchar, fillchar, attr);
801 // don't redraw the cmdline because of showing the ruler
802 redraw_cmdline = i;
803 wp->w_ru_cursor = wp->w_cursor;
804 wp->w_ru_virtcol = wp->w_virtcol;
805 wp->w_ru_empty = empty_line;
806 wp->w_ru_topline = wp->w_topline;
807 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
808#ifdef FEAT_DIFF
809 wp->w_ru_topfill = wp->w_topfill;
810#endif
811 }
812}
813#endif
814
815/*
816 * To be called when "updating_screen" was set before and now the postponed
817 * side effects may take place.
818 */
819 void
820after_updating_screen(int may_resize_shell UNUSED)
821{
822 updating_screen = FALSE;
823#ifdef FEAT_GUI
824 if (may_resize_shell)
825 gui_may_resize_shell();
826#endif
827#ifdef FEAT_TERMINAL
828 term_check_channel_closed_recently();
829#endif
830
831#ifdef HAVE_DROP_FILE
832 // If handle_drop() was called while updating_screen was TRUE need to
833 // handle the drop now.
834 handle_any_postponed_drop();
835#endif
Bram Moolenaar7a99da42022-08-28 22:21:01 +0100836
Bram Moolenaar926218b2022-08-28 23:36:52 +0100837 if (p_ch == 0)
838 // in case it was changed in dont_use_message_window()
839 cmdline_row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200840}
841
842/*
843 * Update all windows that are editing the current buffer.
844 */
845 void
846update_curbuf(int type)
847{
848 redraw_curbuf_later(type);
849 update_screen(type);
850}
851
852#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
853/*
854 * Copy "text" to ScreenLines using "attr".
855 * Returns the next screen column.
856 */
857 static int
858text_to_screenline(win_T *wp, char_u *text, int col)
859{
860 int off = (int)(current_ScreenLine - ScreenLines);
861
862 if (has_mbyte)
863 {
864 int cells;
865 int u8c, u8cc[MAX_MCO];
866 int i;
867 int idx;
868 int c_len;
869 char_u *p;
870# ifdef FEAT_ARABIC
871 int prev_c = 0; // previous Arabic character
872 int prev_c1 = 0; // first composing char for prev_c
873# endif
874
875# ifdef FEAT_RIGHTLEFT
876 if (wp->w_p_rl)
877 idx = off;
878 else
879# endif
880 idx = off + col;
881
882 // Store multibyte characters in ScreenLines[] et al. correctly.
883 for (p = text; *p != NUL; )
884 {
885 cells = (*mb_ptr2cells)(p);
886 c_len = (*mb_ptr2len)(p);
887 if (col + cells > wp->w_width
888# ifdef FEAT_RIGHTLEFT
889 - (wp->w_p_rl ? col : 0)
890# endif
891 )
892 break;
893 ScreenLines[idx] = *p;
894 if (enc_utf8)
895 {
896 u8c = utfc_ptr2char(p, u8cc);
897 if (*p < 0x80 && u8cc[0] == 0)
898 {
899 ScreenLinesUC[idx] = 0;
900#ifdef FEAT_ARABIC
901 prev_c = u8c;
902#endif
903 }
904 else
905 {
906#ifdef FEAT_ARABIC
907 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
908 {
909 // Do Arabic shaping.
910 int pc, pc1, nc;
911 int pcc[MAX_MCO];
912 int firstbyte = *p;
913
914 // The idea of what is the previous and next
915 // character depends on 'rightleft'.
916 if (wp->w_p_rl)
917 {
918 pc = prev_c;
919 pc1 = prev_c1;
920 nc = utf_ptr2char(p + c_len);
921 prev_c1 = u8cc[0];
922 }
923 else
924 {
925 pc = utfc_ptr2char(p + c_len, pcc);
926 nc = prev_c;
927 pc1 = pcc[0];
928 }
929 prev_c = u8c;
930
931 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
932 pc, pc1, nc);
933 ScreenLines[idx] = firstbyte;
934 }
935 else
936 prev_c = u8c;
937#endif
938 // Non-BMP character: display as ? or fullwidth ?.
939 ScreenLinesUC[idx] = u8c;
940 for (i = 0; i < Screen_mco; ++i)
941 {
942 ScreenLinesC[i][idx] = u8cc[i];
943 if (u8cc[i] == 0)
944 break;
945 }
946 }
947 if (cells > 1)
948 ScreenLines[idx + 1] = 0;
949 }
950 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
951 // double-byte single width character
952 ScreenLines2[idx] = p[1];
953 else if (cells > 1)
954 // double-width character
955 ScreenLines[idx + 1] = p[1];
956 col += cells;
957 idx += cells;
958 p += c_len;
959 }
960 }
961 else
962 {
963 int len = (int)STRLEN(text);
964
965 if (len > wp->w_width - col)
966 len = wp->w_width - col;
967 if (len > 0)
968 {
969#ifdef FEAT_RIGHTLEFT
970 if (wp->w_p_rl)
971 mch_memmove(current_ScreenLine, text, len);
972 else
973#endif
974 mch_memmove(current_ScreenLine + col, text, len);
975 col += len;
976 }
977 }
978 return col;
979}
980#endif
981
982#ifdef FEAT_MENU
983/*
984 * Draw the window toolbar.
985 */
986 static void
987redraw_win_toolbar(win_T *wp)
988{
989 vimmenu_T *menu;
990 int item_idx = 0;
991 int item_count = 0;
992 int col = 0;
993 int next_col;
994 int off = (int)(current_ScreenLine - ScreenLines);
995 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
996 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
997
998 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200999 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001000 ++item_count;
1001 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
1002
1003 // TODO: use fewer spaces if there is not enough room
1004 for (menu = wp->w_winbar->children;
1005 menu != NULL && col < wp->w_width; menu = menu->next)
1006 {
1007 space_to_screenline(off + col, fill_attr);
1008 if (++col >= wp->w_width)
1009 break;
1010 if (col > 1)
1011 {
1012 space_to_screenline(off + col, fill_attr);
1013 if (++col >= wp->w_width)
1014 break;
1015 }
1016
1017 wp->w_winbar_items[item_idx].wb_startcol = col;
1018 space_to_screenline(off + col, button_attr);
1019 if (++col >= wp->w_width)
1020 break;
1021
1022 next_col = text_to_screenline(wp, menu->name, col);
1023 while (col < next_col)
1024 {
1025 ScreenAttrs[off + col] = button_attr;
1026 ++col;
1027 }
1028 wp->w_winbar_items[item_idx].wb_endcol = col;
1029 wp->w_winbar_items[item_idx].wb_menu = menu;
1030 ++item_idx;
1031
1032 if (col >= wp->w_width)
1033 break;
1034 space_to_screenline(off + col, button_attr);
1035 ++col;
1036 }
1037 while (col < wp->w_width)
1038 {
1039 space_to_screenline(off + col, fill_attr);
1040 ++col;
1041 }
1042 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1043
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001044 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001045}
1046#endif
1047
1048#if defined(FEAT_FOLDING) || defined(PROTO)
1049/*
1050 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1051 */
1052 static void
1053copy_text_attr(
1054 int off,
1055 char_u *buf,
1056 int len,
1057 int attr)
1058{
1059 int i;
1060
1061 mch_memmove(ScreenLines + off, buf, (size_t)len);
1062 if (enc_utf8)
1063 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1064 for (i = 0; i < len; ++i)
1065 ScreenAttrs[off + i] = attr;
1066}
1067
1068/*
1069 * Display one folded line.
1070 */
1071 static void
1072fold_line(
1073 win_T *wp,
1074 long fold_count,
1075 foldinfo_T *foldinfo,
1076 linenr_T lnum,
1077 int row)
1078{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001079 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1080 // multi-byte character is MAX_MCO.
1081 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001082 pos_T *top, *bot;
1083 linenr_T lnume = lnum + fold_count - 1;
1084 int len;
1085 char_u *text;
1086 int fdc;
1087 int col;
1088 int txtcol;
1089 int off = (int)(current_ScreenLine - ScreenLines);
1090 int ri;
1091
1092 // Build the fold line:
1093 // 1. Add the cmdwin_type for the command-line window
1094 // 2. Add the 'foldcolumn'
1095 // 3. Add the 'number' or 'relativenumber' column
1096 // 4. Compose the text
1097 // 5. Add the text
1098 // 6. set highlighting for the Visual area an other text
1099 col = 0;
1100
1101 // 1. Add the cmdwin_type for the command-line window
1102 // Ignores 'rightleft', this window is never right-left.
1103#ifdef FEAT_CMDWIN
1104 if (cmdwin_type != 0 && wp == curwin)
1105 {
1106 ScreenLines[off] = cmdwin_type;
1107 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1108 if (enc_utf8)
1109 ScreenLinesUC[off] = 0;
1110 ++col;
1111 }
1112#endif
1113
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001114#ifdef FEAT_RIGHTLEFT
1115# define RL_MEMSET(p, v, l) \
1116 do { \
1117 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001118 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001119 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1120 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001121 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001122 ScreenAttrs[off + (p) + ri] = v; \
1123 } while (0)
1124#else
1125# define RL_MEMSET(p, v, l) \
1126 do { \
1127 for (ri = 0; ri < l; ++ri) \
1128 ScreenAttrs[off + (p) + ri] = v; \
1129 } while (0)
1130#endif
1131
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001132 // 2. Add the 'foldcolumn'
1133 // Reduce the width when there is not enough space.
1134 fdc = compute_foldcolumn(wp, col);
1135 if (fdc > 0)
1136 {
1137 char_u *p;
1138 int i;
1139 int idx;
1140
1141 fill_foldcolumn(buf, wp, TRUE, lnum);
1142 p = buf;
1143 for (i = 0; i < fdc; i++)
1144 {
1145 int ch;
1146
1147 if (has_mbyte)
1148 ch = mb_ptr2char_adv(&p);
1149 else
1150 ch = *p++;
1151#ifdef FEAT_RIGHTLEFT
1152 if (wp->w_p_rl)
1153 idx = off + wp->w_width - i - 1 - col;
1154 else
1155#endif
1156 idx = off + col + i;
1157 if (enc_utf8)
1158 {
1159 if (ch >= 0x80)
1160 {
1161 ScreenLinesUC[idx] = ch;
1162 ScreenLinesC[0][idx] = 0;
1163 ScreenLines[idx] = 0x80;
1164 }
1165 else
1166 {
1167 ScreenLines[idx] = ch;
1168 ScreenLinesUC[idx] = 0;
1169 }
1170 }
1171 else
1172 ScreenLines[idx] = ch;
1173 }
1174
1175 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1176 col += fdc;
1177 }
1178
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001179 // Set all attributes of the 'number' or 'relativenumber' column and the
1180 // text
1181 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1182
1183#ifdef FEAT_SIGNS
1184 // If signs are being displayed, add two spaces.
1185 if (signcolumn_on(wp))
1186 {
1187 len = wp->w_width - col;
1188 if (len > 0)
1189 {
1190 if (len > 2)
1191 len = 2;
1192# ifdef FEAT_RIGHTLEFT
1193 if (wp->w_p_rl)
1194 // the line number isn't reversed
1195 copy_text_attr(off + wp->w_width - len - col,
1196 (char_u *)" ", len, HL_ATTR(HLF_FL));
1197 else
1198# endif
1199 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1200 col += len;
1201 }
1202 }
1203#endif
1204
1205 // 3. Add the 'number' or 'relativenumber' column
1206 if (wp->w_p_nu || wp->w_p_rnu)
1207 {
1208 len = wp->w_width - col;
1209 if (len > 0)
1210 {
1211 int w = number_width(wp);
1212 long num;
1213 char *fmt = "%*ld ";
1214
1215 if (len > w + 1)
1216 len = w + 1;
1217
1218 if (wp->w_p_nu && !wp->w_p_rnu)
1219 // 'number' + 'norelativenumber'
1220 num = (long)lnum;
1221 else
1222 {
1223 // 'relativenumber', don't use negative numbers
1224 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1225 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1226 {
1227 // 'number' + 'relativenumber': cursor line shows absolute
1228 // line number
1229 num = lnum;
1230 fmt = "%-*ld ";
1231 }
1232 }
1233
1234 sprintf((char *)buf, fmt, w, num);
1235#ifdef FEAT_RIGHTLEFT
1236 if (wp->w_p_rl)
1237 // the line number isn't reversed
1238 copy_text_attr(off + wp->w_width - len - col, buf, len,
1239 HL_ATTR(HLF_FL));
1240 else
1241#endif
1242 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1243 col += len;
1244 }
1245 }
1246
1247 // 4. Compose the folded-line string with 'foldtext', if set.
1248 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1249
1250 txtcol = col; // remember where text starts
1251
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001252 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1253 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001254 // Right-left text is put in columns 0 - number-col, normal text is put
1255 // in columns number-col - window-width.
1256 col = text_to_screenline(wp, text, col);
1257
1258 // Fill the rest of the line with the fold filler
1259#ifdef FEAT_RIGHTLEFT
1260 if (wp->w_p_rl)
1261 col -= txtcol;
1262#endif
1263 while (col < wp->w_width
1264#ifdef FEAT_RIGHTLEFT
1265 - (wp->w_p_rl ? txtcol : 0)
1266#endif
1267 )
1268 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001269 int c = wp->w_fill_chars.fold;
1270
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001271 if (enc_utf8)
1272 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001273 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001274 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001275 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001276 ScreenLinesC[0][off + col] = 0;
1277 ScreenLines[off + col] = 0x80; // avoid storing zero
1278 }
1279 else
1280 {
1281 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001282 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001283 }
1284 col++;
1285 }
1286 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001287 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001288 }
1289
1290 if (text != buf)
1291 vim_free(text);
1292
1293 // 6. set highlighting for the Visual area an other text.
1294 // If all folded lines are in the Visual area, highlight the line.
1295 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1296 {
1297 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1298 {
1299 // Visual is after curwin->w_cursor
1300 top = &curwin->w_cursor;
1301 bot = &VIsual;
1302 }
1303 else
1304 {
1305 // Visual is before curwin->w_cursor
1306 top = &VIsual;
1307 bot = &curwin->w_cursor;
1308 }
1309 if (lnum >= top->lnum
1310 && lnume <= bot->lnum
1311 && (VIsual_mode != 'v'
1312 || ((lnum > top->lnum
1313 || (lnum == top->lnum
1314 && top->col == 0))
1315 && (lnume < bot->lnum
1316 || (lnume == bot->lnum
1317 && (bot->col - (*p_sel == 'e'))
1318 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
1319 {
1320 if (VIsual_mode == Ctrl_V)
1321 {
1322 // Visual block mode: highlight the chars part of the block
1323 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1324 {
1325 if (wp->w_old_cursor_lcol != MAXCOL
1326 && wp->w_old_cursor_lcol + txtcol
1327 < (colnr_T)wp->w_width)
1328 len = wp->w_old_cursor_lcol;
1329 else
1330 len = wp->w_width - txtcol;
1331 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1332 len - (int)wp->w_old_cursor_fcol);
1333 }
1334 }
1335 else
1336 {
1337 // Set all attributes of the text
1338 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1339 }
1340 }
1341 }
1342
1343#ifdef FEAT_SYN_HL
1344 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1345 if (wp->w_p_cc_cols)
1346 {
1347 int i = 0;
1348 int j = wp->w_p_cc_cols[i];
1349 int old_txtcol = txtcol;
1350
1351 while (j > -1)
1352 {
1353 txtcol += j;
1354 if (wp->w_p_wrap)
1355 txtcol -= wp->w_skipcol;
1356 else
1357 txtcol -= wp->w_leftcol;
1358 if (txtcol >= 0 && txtcol < wp->w_width)
1359 ScreenAttrs[off + txtcol] = hl_combine_attr(
1360 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1361 txtcol = old_txtcol;
1362 j = wp->w_p_cc_cols[++i];
1363 }
1364 }
1365
1366 // Show 'cursorcolumn' in the fold line.
1367 if (wp->w_p_cuc)
1368 {
1369 txtcol += wp->w_virtcol;
1370 if (wp->w_p_wrap)
1371 txtcol -= wp->w_skipcol;
1372 else
1373 txtcol -= wp->w_leftcol;
1374 if (txtcol >= 0 && txtcol < wp->w_width)
1375 ScreenAttrs[off + txtcol] = hl_combine_attr(
1376 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1377 }
1378#endif
1379
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001380 screen_line(wp, row + W_WINROW(wp), wp->w_wincol,
1381 wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001382
1383 // Update w_cline_height and w_cline_folded if the cursor line was
1384 // updated (saves a call to plines() later).
1385 if (wp == curwin
1386 && lnum <= curwin->w_cursor.lnum
1387 && lnume >= curwin->w_cursor.lnum)
1388 {
1389 curwin->w_cline_row = row;
1390 curwin->w_cline_height = 1;
1391 curwin->w_cline_folded = TRUE;
1392 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1393 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001394
1395# ifdef FEAT_CONCEAL
1396 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001397 if (wp == curwin
1398 && wp->w_cursor.lnum >= lnum
1399 && wp->w_cursor.lnum <= lnume
1400 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001401 curs_columns(TRUE);
1402# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001403}
1404#endif
1405
1406/*
1407 * Update a single window.
1408 *
1409 * This may cause the windows below it also to be redrawn (when clearing the
1410 * screen or scrolling lines).
1411 *
1412 * How the window is redrawn depends on wp->w_redr_type. Each type also
1413 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001414 * UPD_NOT_VALID redraw the whole window
1415 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1416 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1417 * UPD_VALID
1418 * UPD_INVERTED redraw the changed part of the Visual area
1419 * UPD_INVERTED_ALL redraw the whole Visual area
1420 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001421 * 2. update lines at the top when scrolled down
1422 * 3. redraw changed text:
1423 * - if wp->w_buffer->b_mod_set set, update lines between
1424 * b_mod_top and b_mod_bot.
1425 * - if wp->w_redraw_top non-zero, redraw lines between
1426 * wp->w_redraw_top and wp->w_redr_bot.
1427 * - continue redrawing when syntax status is invalid.
1428 * 4. if scrolled up, update lines at the bottom.
1429 * This results in three areas that may need updating:
1430 * top: from first row to top_end (when scrolled down)
1431 * mid: from mid_start to mid_end (update inversion or changed text)
1432 * bot: from bot_start to last row (when scrolled up)
1433 */
1434 static void
1435win_update(win_T *wp)
1436{
1437 buf_T *buf = wp->w_buffer;
1438 int type;
1439 int top_end = 0; // Below last row of the top area that needs
1440 // updating. 0 when no top area updating.
1441 int mid_start = 999;// first row of the mid area that needs
1442 // updating. 999 when no mid area updating.
1443 int mid_end = 0; // Below last row of the mid area that needs
1444 // updating. 0 when no mid area updating.
1445 int bot_start = 999;// first row of the bot area that needs
1446 // updating. 999 when no bot area updating
1447 int scrolled_down = FALSE; // TRUE when scrolled down when
1448 // w_topline got smaller a bit
1449#ifdef FEAT_SEARCH_EXTRA
1450 int top_to_mod = FALSE; // redraw above mod_top
1451#endif
1452
1453 int row; // current window row to display
1454 linenr_T lnum; // current buffer lnum to display
1455 int idx; // current index in w_lines[]
1456 int srow; // starting row of the current line
1457
1458 int eof = FALSE; // if TRUE, we hit the end of the file
1459 int didline = FALSE; // if TRUE, we finished the last line
1460 int i;
1461 long j;
1462 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001463 linenr_T old_botline = wp->w_botline;
1464#ifdef FEAT_CONCEAL
1465 int old_wrow = wp->w_wrow;
1466 int old_wcol = wp->w_wcol;
1467#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001468#ifdef FEAT_FOLDING
1469 long fold_count;
1470#endif
1471#ifdef FEAT_SYN_HL
1472 // remember what happened to the previous line, to know if
1473 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001474# define DID_NONE 1 // didn't update a line
1475# define DID_LINE 2 // updated a normal line
1476# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001477 int did_update = DID_NONE;
1478 linenr_T syntax_last_parsed = 0; // last parsed text line
1479#endif
1480 linenr_T mod_top = 0;
1481 linenr_T mod_bot = 0;
1482#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1483 int save_got_int;
1484#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001485
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001486#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1487 // This needs to be done only for the first window when update_screen() is
1488 // called.
1489 if (!did_update_one_window)
1490 {
1491 did_update_one_window = TRUE;
1492# ifdef FEAT_SEARCH_EXTRA
1493 start_search_hl();
1494# endif
1495# ifdef FEAT_CLIPBOARD
1496 // When Visual area changed, may have to update selection.
1497 if (clip_star.available && clip_isautosel_star())
1498 clip_update_selection(&clip_star);
1499 if (clip_plus.available && clip_isautosel_plus())
1500 clip_update_selection(&clip_plus);
1501# endif
1502 }
1503#endif
1504
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001505 type = wp->w_redr_type;
1506
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001507 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001508 {
1509 wp->w_redr_status = TRUE;
1510 wp->w_lines_valid = 0;
1511 }
1512
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001513 // Window frame is zero-height: nothing to draw.
1514 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1515 || (wp->w_frame->fr_height == wp->w_status_height
1516#if defined(FEAT_PROP_POPUP)
1517 && !popup_is_popup(wp)
1518#endif
1519 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001520 {
1521 wp->w_redr_type = 0;
1522 return;
1523 }
1524
1525 // Window is zero-width: Only need to draw the separator.
1526 if (wp->w_width == 0)
1527 {
1528 // draw the vertical separator right of this window
1529 draw_vsep_win(wp, 0);
1530 wp->w_redr_type = 0;
1531 return;
1532 }
1533
1534#ifdef FEAT_TERMINAL
1535 // If this window contains a terminal, redraw works completely differently.
1536 if (term_do_update_window(wp))
1537 {
1538 term_update_window(wp);
1539# ifdef FEAT_MENU
1540 // Draw the window toolbar, if there is one.
1541 if (winbar_height(wp) > 0)
1542 redraw_win_toolbar(wp);
1543# endif
1544 wp->w_redr_type = 0;
1545 return;
1546 }
1547#endif
1548
1549#ifdef FEAT_SEARCH_EXTRA
1550 init_search_hl(wp, &screen_search_hl);
1551#endif
1552
1553#ifdef FEAT_LINEBREAK
1554 // Force redraw when width of 'number' or 'relativenumber' column
1555 // changes.
1556 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1557 if (wp->w_nrwidth != i)
1558 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001559 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001560 wp->w_nrwidth = i;
1561 }
1562 else
1563#endif
1564
1565 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1566 {
1567 // When there are both inserted/deleted lines and specific lines to be
1568 // redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1569 // everything (only happens when redrawing is off for while).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001570 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001571 }
1572 else
1573 {
1574 // Set mod_top to the first line that needs displaying because of
1575 // changes. Set mod_bot to the first line after the changes.
1576 mod_top = wp->w_redraw_top;
1577 if (wp->w_redraw_bot != 0)
1578 mod_bot = wp->w_redraw_bot + 1;
1579 else
1580 mod_bot = 0;
1581 if (buf->b_mod_set)
1582 {
1583 if (mod_top == 0 || mod_top > buf->b_mod_top)
1584 {
1585 mod_top = buf->b_mod_top;
1586#ifdef FEAT_SYN_HL
1587 // Need to redraw lines above the change that may be included
1588 // in a pattern match.
1589 if (syntax_present(wp))
1590 {
1591 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1592 if (mod_top < 1)
1593 mod_top = 1;
1594 }
1595#endif
1596 }
1597 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1598 mod_bot = buf->b_mod_bot;
1599
1600#ifdef FEAT_SEARCH_EXTRA
1601 // When 'hlsearch' is on and using a multi-line search pattern, a
1602 // change in one line may make the Search highlighting in a
1603 // previous line invalid. Simple solution: redraw all visible
1604 // lines above the change.
1605 // Same for a match pattern.
1606 if (screen_search_hl.rm.regprog != NULL
1607 && re_multiline(screen_search_hl.rm.regprog))
1608 top_to_mod = TRUE;
1609 else
1610 {
1611 matchitem_T *cur = wp->w_match_head;
1612
1613 while (cur != NULL)
1614 {
1615 if (cur->match.regprog != NULL
1616 && re_multiline(cur->match.regprog))
1617 {
1618 top_to_mod = TRUE;
1619 break;
1620 }
1621 cur = cur->next;
1622 }
1623 }
1624#endif
1625 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001626
1627#ifdef FEAT_SEARCH_EXTRA
1628 if (search_hl_has_cursor_lnum > 0)
1629 {
1630 // CurSearch was used last time, need to redraw the line with it to
1631 // avoid having two matches highlighted with CurSearch.
1632 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1633 mod_top = search_hl_has_cursor_lnum;
1634 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1635 mod_bot = search_hl_has_cursor_lnum + 1;
1636 }
1637#endif
1638
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001639#ifdef FEAT_FOLDING
1640 if (mod_top != 0 && hasAnyFolding(wp))
1641 {
1642 linenr_T lnumt, lnumb;
1643
1644 // A change in a line can cause lines above it to become folded or
1645 // unfolded. Find the top most buffer line that may be affected.
1646 // If the line was previously folded and displayed, get the first
1647 // line of that fold. If the line is folded now, get the first
1648 // folded line. Use the minimum of these two.
1649
1650 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1651 // the line below it. If there is no valid entry, use w_topline.
1652 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1653 // to this line. If there is no valid entry, use MAXLNUM.
1654 lnumt = wp->w_topline;
1655 lnumb = MAXLNUM;
1656 for (i = 0; i < wp->w_lines_valid; ++i)
1657 if (wp->w_lines[i].wl_valid)
1658 {
1659 if (wp->w_lines[i].wl_lastlnum < mod_top)
1660 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1661 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1662 {
1663 lnumb = wp->w_lines[i].wl_lnum;
1664 // When there is a fold column it might need updating
1665 // in the next line ("J" just above an open fold).
1666 if (compute_foldcolumn(wp, 0) > 0)
1667 ++lnumb;
1668 }
1669 }
1670
1671 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1672 if (mod_top > lnumt)
1673 mod_top = lnumt;
1674
1675 // Now do the same for the bottom line (one above mod_bot).
1676 --mod_bot;
1677 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1678 ++mod_bot;
1679 if (mod_bot < lnumb)
1680 mod_bot = lnumb;
1681 }
1682#endif
1683
1684 // When a change starts above w_topline and the end is below
1685 // w_topline, start redrawing at w_topline.
1686 // If the end of the change is above w_topline: do like no change was
1687 // made, but redraw the first line to find changes in syntax.
1688 if (mod_top != 0 && mod_top < wp->w_topline)
1689 {
1690 if (mod_bot > wp->w_topline)
1691 mod_top = wp->w_topline;
1692#ifdef FEAT_SYN_HL
1693 else if (syntax_present(wp))
1694 top_end = 1;
1695#endif
1696 }
1697
1698 // When line numbers are displayed need to redraw all lines below
1699 // inserted/deleted lines.
1700 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1701 mod_bot = MAXLNUM;
1702 }
1703 wp->w_redraw_top = 0; // reset for next time
1704 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001705#ifdef FEAT_SEARCH_EXTRA
1706 search_hl_has_cursor_lnum = 0;
1707#endif
1708
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001709
1710 // When only displaying the lines at the top, set top_end. Used when
1711 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001712 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001713 {
1714 j = 0;
1715 for (i = 0; i < wp->w_lines_valid; ++i)
1716 {
1717 j += wp->w_lines[i].wl_size;
1718 if (j >= wp->w_upd_rows)
1719 {
1720 top_end = j;
1721 break;
1722 }
1723 }
1724 if (top_end == 0)
1725 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001726 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001727 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001728 // top area defined, the rest is UPD_VALID
1729 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001730 }
1731
1732 // Trick: we want to avoid clearing the screen twice. screenclear() will
1733 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1734 // non-zero and thus not FALSE) will indicate that screenclear() was not
1735 // called.
1736 if (screen_cleared)
1737 screen_cleared = MAYBE;
1738
1739 // If there are no changes on the screen that require a complete redraw,
1740 // handle three cases:
1741 // 1: we are off the top of the screen by a few lines: scroll down
1742 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1743 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1744 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001745 if ((type == UPD_VALID || type == UPD_SOME_VALID
1746 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001747#ifdef FEAT_DIFF
1748 && !wp->w_botfill && !wp->w_old_botfill
1749#endif
1750 )
1751 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001752 if (mod_top != 0
1753 && wp->w_topline == mod_top
1754 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001755 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001756 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001757 // w_topline is the first changed line and window is not scrolled,
1758 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001759 }
1760 else if (wp->w_lines[0].wl_valid
1761 && (wp->w_topline < wp->w_lines[0].wl_lnum
1762#ifdef FEAT_DIFF
1763 || (wp->w_topline == wp->w_lines[0].wl_lnum
1764 && wp->w_topfill > wp->w_old_topfill)
1765#endif
1766 ))
1767 {
1768 // New topline is above old topline: May scroll down.
1769#ifdef FEAT_FOLDING
1770 if (hasAnyFolding(wp))
1771 {
1772 linenr_T ln;
1773
1774 // count the number of lines we are off, counting a sequence
1775 // of folded lines as one
1776 j = 0;
1777 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1778 {
1779 ++j;
1780 if (j >= wp->w_height - 2)
1781 break;
1782 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1783 }
1784 }
1785 else
1786#endif
1787 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1788 if (j < wp->w_height - 2) // not too far off
1789 {
1790 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1791#ifdef FEAT_DIFF
1792 // insert extra lines for previously invisible filler lines
1793 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1794 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1795 - wp->w_old_topfill;
1796#endif
1797 if (i < wp->w_height - 2) // less than a screen off
1798 {
1799 // Try to insert the correct number of lines.
1800 // If not the last window, delete the lines at the bottom.
1801 // win_ins_lines may fail when the terminal can't do it.
1802 if (i > 0)
1803 check_for_delay(FALSE);
1804 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1805 {
1806 if (wp->w_lines_valid != 0)
1807 {
1808 // Need to update rows that are new, stop at the
1809 // first one that scrolled down.
1810 top_end = i;
1811 scrolled_down = TRUE;
1812
1813 // Move the entries that were scrolled, disable
1814 // the entries for the lines to be redrawn.
Bram Moolenaarfdc5d172022-08-11 15:52:14 +01001815 // Avoid using a wrong index when 'cmdheight' is
1816 // zero and wp->w_height == Rows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001817 if ((wp->w_lines_valid += j) > wp->w_height)
1818 wp->w_lines_valid = wp->w_height;
Bram Moolenaarfdc5d172022-08-11 15:52:14 +01001819 for (idx = wp->w_lines_valid >= wp->w_height
1820 ? wp->w_height - 1 : wp->w_lines_valid;
1821 idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001822 wp->w_lines[idx] = wp->w_lines[idx - j];
1823 while (idx >= 0)
1824 wp->w_lines[idx--].wl_valid = FALSE;
1825 }
1826 }
1827 else
1828 mid_start = 0; // redraw all lines
1829 }
1830 else
1831 mid_start = 0; // redraw all lines
1832 }
1833 else
1834 mid_start = 0; // redraw all lines
1835 }
1836 else
1837 {
1838 // New topline is at or below old topline: May scroll up.
1839 // When topline didn't change, find first entry in w_lines[] that
1840 // needs updating.
1841
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001842 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1843 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001844 j = -1;
1845 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001846 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001847 {
1848 if (wp->w_lines[i].wl_valid
1849 && wp->w_lines[i].wl_lnum == wp->w_topline)
1850 {
1851 j = i;
1852 break;
1853 }
1854 row += wp->w_lines[i].wl_size;
1855 }
1856 if (j == -1)
1857 {
1858 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1859 // lines
1860 mid_start = 0;
1861 }
1862 else
1863 {
1864 // Try to delete the correct number of lines.
1865 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1866#ifdef FEAT_DIFF
1867 // If the topline didn't change, delete old filler lines,
1868 // otherwise delete filler lines of the new topline...
1869 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1870 row += wp->w_old_topfill;
1871 else
1872 row += diff_check_fill(wp, wp->w_topline);
1873 // ... but don't delete new filler lines.
1874 row -= wp->w_topfill;
1875#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001876 if (row > Rows) // just in case
1877 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001878 if (row > 0)
1879 {
1880 check_for_delay(FALSE);
1881 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1882 == OK)
1883 bot_start = wp->w_height - row;
1884 else
1885 mid_start = 0; // redraw all lines
1886 }
1887 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1888 {
1889 // Skip the lines (below the deleted lines) that are still
1890 // valid and don't need redrawing. Copy their info
1891 // upwards, to compensate for the deleted lines. Set
1892 // bot_start to the first row that needs redrawing.
1893 bot_start = 0;
1894 idx = 0;
1895 for (;;)
1896 {
1897 wp->w_lines[idx] = wp->w_lines[j];
1898 // stop at line that didn't fit, unless it is still
1899 // valid (no lines deleted)
1900 if (row > 0 && bot_start + row
1901 + (int)wp->w_lines[j].wl_size > wp->w_height)
1902 {
1903 wp->w_lines_valid = idx + 1;
1904 break;
1905 }
1906 bot_start += wp->w_lines[idx++].wl_size;
1907
1908 // stop at the last valid entry in w_lines[].wl_size
1909 if (++j >= wp->w_lines_valid)
1910 {
1911 wp->w_lines_valid = idx;
1912 break;
1913 }
1914 }
1915#ifdef FEAT_DIFF
1916 // Correct the first entry for filler lines at the top
1917 // when it won't get updated below.
1918 if (wp->w_p_diff && bot_start > 0)
1919 wp->w_lines[0].wl_size =
1920 plines_win_nofill(wp, wp->w_topline, TRUE)
1921 + wp->w_topfill;
1922#endif
1923 }
1924 }
1925 }
1926
1927 // When starting redraw in the first line, redraw all lines. When
1928 // there is only one window it's probably faster to clear the screen
1929 // first.
1930 if (mid_start == 0)
1931 {
1932 mid_end = wp->w_height;
1933 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
1934 {
1935 // Clear the screen when it was not done by win_del_lines() or
1936 // win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1937 // then.
1938 if (screen_cleared != TRUE)
1939 screenclear();
1940 // The screen was cleared, redraw the tab pages line.
1941 if (redraw_tabline)
1942 draw_tabline();
1943 }
1944 }
1945
1946 // When win_del_lines() or win_ins_lines() caused the screen to be
1947 // cleared (only happens for the first window) or when screenclear()
1948 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001949 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001950 if (screen_cleared == TRUE)
1951 must_redraw = 0;
1952 }
1953 else
1954 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001955 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001956 mid_start = 0;
1957 mid_end = wp->w_height;
1958 }
1959
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001960 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001961 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001962 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001963 mid_start = 0;
1964 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001965 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001966 }
1967
1968 // check if we are updating or removing the inverted part
1969 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001970 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001971 {
1972 linenr_T from, to;
1973
1974 if (VIsual_active)
1975 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001976 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001977 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001978 {
1979 // If the type of Visual selection changed, redraw the whole
1980 // selection. Also when the ownership of the X selection is
1981 // gained or lost.
1982 if (curwin->w_cursor.lnum < VIsual.lnum)
1983 {
1984 from = curwin->w_cursor.lnum;
1985 to = VIsual.lnum;
1986 }
1987 else
1988 {
1989 from = VIsual.lnum;
1990 to = curwin->w_cursor.lnum;
1991 }
1992 // redraw more when the cursor moved as well
1993 if (wp->w_old_cursor_lnum < from)
1994 from = wp->w_old_cursor_lnum;
1995 if (wp->w_old_cursor_lnum > to)
1996 to = wp->w_old_cursor_lnum;
1997 if (wp->w_old_visual_lnum < from)
1998 from = wp->w_old_visual_lnum;
1999 if (wp->w_old_visual_lnum > to)
2000 to = wp->w_old_visual_lnum;
2001 }
2002 else
2003 {
2004 // Find the line numbers that need to be updated: The lines
2005 // between the old cursor position and the current cursor
2006 // position. Also check if the Visual position changed.
2007 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
2008 {
2009 from = curwin->w_cursor.lnum;
2010 to = wp->w_old_cursor_lnum;
2011 }
2012 else
2013 {
2014 from = wp->w_old_cursor_lnum;
2015 to = curwin->w_cursor.lnum;
2016 if (from == 0) // Visual mode just started
2017 from = to;
2018 }
2019
2020 if (VIsual.lnum != wp->w_old_visual_lnum
2021 || VIsual.col != wp->w_old_visual_col)
2022 {
2023 if (wp->w_old_visual_lnum < from
2024 && wp->w_old_visual_lnum != 0)
2025 from = wp->w_old_visual_lnum;
2026 if (wp->w_old_visual_lnum > to)
2027 to = wp->w_old_visual_lnum;
2028 if (VIsual.lnum < from)
2029 from = VIsual.lnum;
2030 if (VIsual.lnum > to)
2031 to = VIsual.lnum;
2032 }
2033 }
2034
2035 // If in block mode and changed column or curwin->w_curswant:
2036 // update all lines.
2037 // First compute the actual start and end column.
2038 if (VIsual_mode == Ctrl_V)
2039 {
2040 colnr_T fromc, toc;
2041#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002042 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002043
2044 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002045 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002046#endif
2047 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002048 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002049#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002050 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002051#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002052 // Highlight to the end of the line, unless 'virtualedit' has
2053 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002054 if (curwin->w_curswant == MAXCOL)
2055 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002056 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002057 {
2058 pos_T pos;
2059 int cursor_above =
2060 curwin->w_cursor.lnum < VIsual.lnum;
2061
2062 // Need to find the longest line.
2063 toc = 0;
2064 pos.coladd = 0;
2065 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2066 ? pos.lnum <= VIsual.lnum
2067 : pos.lnum >= VIsual.lnum;
2068 pos.lnum += cursor_above ? 1 : -1)
2069 {
2070 colnr_T t;
2071
Bram Moolenaar6bcb1822021-07-09 15:54:00 +02002072 pos.col = (int)STRLEN(ml_get_buf(wp->w_buffer,
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002073 pos.lnum, FALSE));
2074 getvvcol(wp, &pos, NULL, NULL, &t);
2075 if (toc < t)
2076 toc = t;
2077 }
2078 ++toc;
2079 }
2080 else
2081 toc = MAXCOL;
2082 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002083
2084 if (fromc != wp->w_old_cursor_fcol
2085 || toc != wp->w_old_cursor_lcol)
2086 {
2087 if (from > VIsual.lnum)
2088 from = VIsual.lnum;
2089 if (to < VIsual.lnum)
2090 to = VIsual.lnum;
2091 }
2092 wp->w_old_cursor_fcol = fromc;
2093 wp->w_old_cursor_lcol = toc;
2094 }
2095 }
2096 else
2097 {
2098 // Use the line numbers of the old Visual area.
2099 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2100 {
2101 from = wp->w_old_cursor_lnum;
2102 to = wp->w_old_visual_lnum;
2103 }
2104 else
2105 {
2106 from = wp->w_old_visual_lnum;
2107 to = wp->w_old_cursor_lnum;
2108 }
2109 }
2110
2111 // There is no need to update lines above the top of the window.
2112 if (from < wp->w_topline)
2113 from = wp->w_topline;
2114
2115 // If we know the value of w_botline, use it to restrict the update to
2116 // the lines that are visible in the window.
2117 if (wp->w_valid & VALID_BOTLINE)
2118 {
2119 if (from >= wp->w_botline)
2120 from = wp->w_botline - 1;
2121 if (to >= wp->w_botline)
2122 to = wp->w_botline - 1;
2123 }
2124
2125 // Find the minimal part to be updated.
2126 // Watch out for scrolling that made entries in w_lines[] invalid.
2127 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2128 // top_end; need to redraw from top_end to the "to" line.
2129 // A middle mouse click with a Visual selection may change the text
2130 // above the Visual area and reset wl_valid, do count these for
2131 // mid_end (in srow).
2132 if (mid_start > 0)
2133 {
2134 lnum = wp->w_topline;
2135 idx = 0;
2136 srow = 0;
2137 if (scrolled_down)
2138 mid_start = top_end;
2139 else
2140 mid_start = 0;
2141 while (lnum < from && idx < wp->w_lines_valid) // find start
2142 {
2143 if (wp->w_lines[idx].wl_valid)
2144 mid_start += wp->w_lines[idx].wl_size;
2145 else if (!scrolled_down)
2146 srow += wp->w_lines[idx].wl_size;
2147 ++idx;
2148# ifdef FEAT_FOLDING
2149 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2150 lnum = wp->w_lines[idx].wl_lnum;
2151 else
2152# endif
2153 ++lnum;
2154 }
2155 srow += mid_start;
2156 mid_end = wp->w_height;
2157 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2158 {
2159 if (wp->w_lines[idx].wl_valid
2160 && wp->w_lines[idx].wl_lnum >= to + 1)
2161 {
2162 // Only update until first row of this line
2163 mid_end = srow;
2164 break;
2165 }
2166 srow += wp->w_lines[idx].wl_size;
2167 }
2168 }
2169 }
2170
2171 if (VIsual_active && buf == curwin->w_buffer)
2172 {
2173 wp->w_old_visual_mode = VIsual_mode;
2174 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2175 wp->w_old_visual_lnum = VIsual.lnum;
2176 wp->w_old_visual_col = VIsual.col;
2177 wp->w_old_curswant = curwin->w_curswant;
2178 }
2179 else
2180 {
2181 wp->w_old_visual_mode = 0;
2182 wp->w_old_cursor_lnum = 0;
2183 wp->w_old_visual_lnum = 0;
2184 wp->w_old_visual_col = 0;
2185 }
2186
2187#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2188 // reset got_int, otherwise regexp won't work
2189 save_got_int = got_int;
2190 got_int = 0;
2191#endif
2192#ifdef SYN_TIME_LIMIT
2193 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002194 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002195 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002196#endif
2197#ifdef FEAT_FOLDING
2198 win_foldinfo.fi_level = 0;
2199#endif
2200
2201#ifdef FEAT_MENU
2202 // Draw the window toolbar, if there is one.
2203 // TODO: only when needed.
2204 if (winbar_height(wp) > 0)
2205 redraw_win_toolbar(wp);
2206#endif
2207
2208 // Update all the window rows.
2209 idx = 0; // first entry in w_lines[].wl_size
2210 row = 0;
2211 srow = 0;
2212 lnum = wp->w_topline; // first line shown in window
2213 for (;;)
2214 {
2215 // stop updating when reached the end of the window (check for _past_
2216 // the end of the window is at the end of the loop)
2217 if (row == wp->w_height)
2218 {
2219 didline = TRUE;
2220 break;
2221 }
2222
2223 // stop updating when hit the end of the file
2224 if (lnum > buf->b_ml.ml_line_count)
2225 {
2226 eof = TRUE;
2227 break;
2228 }
2229
2230 // Remember the starting row of the line that is going to be dealt
2231 // with. It is used further down when the line doesn't fit.
2232 srow = row;
2233
2234 // Update a line when it is in an area that needs updating, when it
2235 // has changes or w_lines[idx] is invalid.
2236 // "bot_start" may be halfway a wrapped line after using
2237 // win_del_lines(), check if the current line includes it.
2238 // When syntax folding is being used, the saved syntax states will
2239 // already have been updated, we can't see where the syntax state is
2240 // the same again, just update until the end of the window.
2241 if (row < top_end
2242 || (row >= mid_start && row < mid_end)
2243#ifdef FEAT_SEARCH_EXTRA
2244 || top_to_mod
2245#endif
2246 || idx >= wp->w_lines_valid
2247 || (row + wp->w_lines[idx].wl_size > bot_start)
2248 || (mod_top != 0
2249 && (lnum == mod_top
2250 || (lnum >= mod_top
2251 && (lnum < mod_bot
2252#ifdef FEAT_SYN_HL
2253 || did_update == DID_FOLD
2254 || (did_update == DID_LINE
2255 && syntax_present(wp)
2256 && (
2257# ifdef FEAT_FOLDING
2258 (foldmethodIsSyntax(wp)
2259 && hasAnyFolding(wp)) ||
2260# endif
2261 syntax_check_changed(lnum)))
2262#endif
2263#ifdef FEAT_SEARCH_EXTRA
2264 // match in fixed position might need redraw
2265 // if lines were inserted or deleted
2266 || (wp->w_match_head != NULL
2267 && buf->b_mod_xlines != 0)
2268#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002269 ))))
2270#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002271 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2272 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002273#endif
2274 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002275 {
2276#ifdef FEAT_SEARCH_EXTRA
2277 if (lnum == mod_top)
2278 top_to_mod = FALSE;
2279#endif
2280
2281 // When at start of changed lines: May scroll following lines
2282 // up or down to minimize redrawing.
2283 // Don't do this when the change continues until the end.
2284 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002285 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002286 if (lnum == mod_top
2287 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002288 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2289 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002290 {
2291 int old_rows = 0;
2292 int new_rows = 0;
2293 int xtra_rows;
2294 linenr_T l;
2295
2296 // Count the old number of window rows, using w_lines[], which
2297 // should still contain the sizes for the lines as they are
2298 // currently displayed.
2299 for (i = idx; i < wp->w_lines_valid; ++i)
2300 {
2301 // Only valid lines have a meaningful wl_lnum. Invalid
2302 // lines are part of the changed area.
2303 if (wp->w_lines[i].wl_valid
2304 && wp->w_lines[i].wl_lnum == mod_bot)
2305 break;
2306 old_rows += wp->w_lines[i].wl_size;
2307#ifdef FEAT_FOLDING
2308 if (wp->w_lines[i].wl_valid
2309 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2310 {
2311 // Must have found the last valid entry above mod_bot.
2312 // Add following invalid entries.
2313 ++i;
2314 while (i < wp->w_lines_valid
2315 && !wp->w_lines[i].wl_valid)
2316 old_rows += wp->w_lines[i++].wl_size;
2317 break;
2318 }
2319#endif
2320 }
2321
2322 if (i >= wp->w_lines_valid)
2323 {
2324 // We can't find a valid line below the changed lines,
2325 // need to redraw until the end of the window.
2326 // Inserting/deleting lines has no use.
2327 bot_start = 0;
2328 }
2329 else
2330 {
2331 // Able to count old number of rows: Count new window
2332 // rows, and may insert/delete lines
2333 j = idx;
2334 for (l = lnum; l < mod_bot; ++l)
2335 {
2336#ifdef FEAT_FOLDING
2337 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2338 ++new_rows;
2339 else
2340#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002341 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002342#ifdef FEAT_DIFF
2343 if (l == wp->w_topline)
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002344 new_rows += plines_win_nofill(wp, l, TRUE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002345 + wp->w_topfill;
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002346 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002347#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002348 new_rows += plines_win(wp, l, TRUE);
2349 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002350 ++j;
2351 if (new_rows > wp->w_height - row - 2)
2352 {
2353 // it's getting too much, must redraw the rest
2354 new_rows = 9999;
2355 break;
2356 }
2357 }
2358 xtra_rows = new_rows - old_rows;
2359 if (xtra_rows < 0)
2360 {
2361 // May scroll text up. If there is not enough
2362 // remaining text or scrolling fails, must redraw the
2363 // rest. If scrolling works, must redraw the text
2364 // below the scrolled text.
2365 if (row - xtra_rows >= wp->w_height - 2)
2366 mod_bot = MAXLNUM;
2367 else
2368 {
2369 check_for_delay(FALSE);
2370 if (win_del_lines(wp, row,
2371 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2372 mod_bot = MAXLNUM;
2373 else
2374 bot_start = wp->w_height + xtra_rows;
2375 }
2376 }
2377 else if (xtra_rows > 0)
2378 {
2379 // May scroll text down. If there is not enough
2380 // remaining text of scrolling fails, must redraw the
2381 // rest.
2382 if (row + xtra_rows >= wp->w_height - 2)
2383 mod_bot = MAXLNUM;
2384 else
2385 {
2386 check_for_delay(FALSE);
2387 if (win_ins_lines(wp, row + old_rows,
2388 xtra_rows, FALSE, FALSE) == FAIL)
2389 mod_bot = MAXLNUM;
2390 else if (top_end > row + old_rows)
2391 // Scrolled the part at the top that requires
2392 // updating down.
2393 top_end += xtra_rows;
2394 }
2395 }
2396
2397 // When not updating the rest, may need to move w_lines[]
2398 // entries.
2399 if (mod_bot != MAXLNUM && i != j)
2400 {
2401 if (j < i)
2402 {
2403 int x = row + new_rows;
2404
2405 // move entries in w_lines[] upwards
2406 for (;;)
2407 {
2408 // stop at last valid entry in w_lines[]
2409 if (i >= wp->w_lines_valid)
2410 {
2411 wp->w_lines_valid = j;
2412 break;
2413 }
2414 wp->w_lines[j] = wp->w_lines[i];
2415 // stop at a line that won't fit
2416 if (x + (int)wp->w_lines[j].wl_size
2417 > wp->w_height)
2418 {
2419 wp->w_lines_valid = j + 1;
2420 break;
2421 }
2422 x += wp->w_lines[j++].wl_size;
2423 ++i;
2424 }
2425 if (bot_start > x)
2426 bot_start = x;
2427 }
2428 else // j > i
2429 {
2430 // move entries in w_lines[] downwards
2431 j -= i;
2432 wp->w_lines_valid += j;
2433 if (wp->w_lines_valid > wp->w_height)
2434 wp->w_lines_valid = wp->w_height;
2435 for (i = wp->w_lines_valid; i - j >= idx; --i)
Bram Moolenaar7a99da42022-08-28 22:21:01 +01002436 if (i < Rows)
2437 wp->w_lines[i] = wp->w_lines[i - j];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002438
2439 // The w_lines[] entries for inserted lines are
2440 // now invalid, but wl_size may be used above.
2441 // Reset to zero.
2442 while (i >= idx)
2443 {
2444 wp->w_lines[i].wl_size = 0;
2445 wp->w_lines[i--].wl_valid = FALSE;
2446 }
2447 }
2448 }
2449 }
2450 }
2451
2452#ifdef FEAT_FOLDING
2453 // When lines are folded, display one line for all of them.
2454 // Otherwise, display normally (can be several display lines when
2455 // 'wrap' is on).
2456 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2457 if (fold_count != 0)
2458 {
2459 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2460 ++row;
2461 --fold_count;
2462 wp->w_lines[idx].wl_folded = TRUE;
2463 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2464# ifdef FEAT_SYN_HL
2465 did_update = DID_FOLD;
2466# endif
2467 }
2468 else
2469#endif
2470 if (idx < wp->w_lines_valid
2471 && wp->w_lines[idx].wl_valid
2472 && wp->w_lines[idx].wl_lnum == lnum
2473 && lnum > wp->w_topline
2474 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2475 && !WIN_IS_POPUP(wp)
2476 && srow + wp->w_lines[idx].wl_size > wp->w_height
2477#ifdef FEAT_DIFF
2478 && diff_check_fill(wp, lnum) == 0
2479#endif
2480 )
2481 {
2482 // This line is not going to fit. Don't draw anything here,
2483 // will draw "@ " lines below.
2484 row = wp->w_height + 1;
2485 }
2486 else
2487 {
2488#ifdef FEAT_SEARCH_EXTRA
2489 prepare_search_hl(wp, &screen_search_hl, lnum);
2490#endif
2491#ifdef FEAT_SYN_HL
2492 // Let the syntax stuff know we skipped a few lines.
2493 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2494 && syntax_present(wp))
2495 syntax_end_parsing(syntax_last_parsed + 1);
2496#endif
2497
2498 // Display one line.
2499 row = win_line(wp, lnum, srow, wp->w_height,
2500 mod_top == 0, FALSE);
2501
2502#ifdef FEAT_FOLDING
2503 wp->w_lines[idx].wl_folded = FALSE;
2504 wp->w_lines[idx].wl_lastlnum = lnum;
2505#endif
2506#ifdef FEAT_SYN_HL
2507 did_update = DID_LINE;
2508 syntax_last_parsed = lnum;
2509#endif
2510 }
2511
2512 wp->w_lines[idx].wl_lnum = lnum;
2513 wp->w_lines[idx].wl_valid = TRUE;
2514
2515 // Past end of the window or end of the screen. Note that after
2516 // resizing wp->w_height may be end up too big. That's a problem
2517 // elsewhere, but prevent a crash here.
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002518 if (row > wp->w_height
2519 || row + wp->w_winrow >= (p_ch > 0 ? Rows : Rows + 1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002520 {
2521 // we may need the size of that too long line later on
2522 if (dollar_vcol == -1)
2523 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2524 ++idx;
2525 break;
2526 }
2527 if (dollar_vcol == -1)
2528 wp->w_lines[idx].wl_size = row - srow;
2529 ++idx;
2530#ifdef FEAT_FOLDING
2531 lnum += fold_count + 1;
2532#else
2533 ++lnum;
2534#endif
2535 }
2536 else
2537 {
Lewis Russell16246392022-03-29 11:38:17 +01002538 if (wp->w_p_rnu && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002539 {
2540#ifdef FEAT_FOLDING
Lewis Russell16246392022-03-29 11:38:17 +01002541 // 'relativenumber' set and the cursor moved vertically: The
2542 // text doesn't need to be drawn, but the number column does.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002543 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2544 if (fold_count != 0)
2545 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2546 else
2547#endif
2548 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
2549 }
2550
2551 // This line does not need to be drawn, advance to the next one.
2552 row += wp->w_lines[idx++].wl_size;
2553 if (row > wp->w_height) // past end of screen
2554 break;
2555#ifdef FEAT_FOLDING
2556 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2557#else
2558 ++lnum;
2559#endif
2560#ifdef FEAT_SYN_HL
2561 did_update = DID_NONE;
2562#endif
2563 }
2564
2565 if (lnum > buf->b_ml.ml_line_count)
2566 {
2567 eof = TRUE;
2568 break;
2569 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002570
2571 // Safety check: if any of the wl_size values is wrong we might go over
2572 // the end of w_lines[].
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002573 if (idx >= (p_ch > 0 ? Rows : Rows + 1))
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002574 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002575 }
2576
2577 // End of loop over all window lines.
2578
zeertzjqc20e46a2022-03-23 14:55:23 +00002579#ifdef FEAT_SYN_HL
2580 // Now that the window has been redrawn with the old and new cursor line,
2581 // update w_last_cursorline.
2582 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2583#endif
Lewis Russell16246392022-03-29 11:38:17 +01002584 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002585
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002586#ifdef FEAT_VTP
2587 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002588 // See the version that was fixed.
2589 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002590 {
K.Takata54119102022-02-03 13:33:03 +00002591 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002592
K.Takata54119102022-02-03 13:33:03 +00002593 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002594 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002595 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2596 LineOffset[k] + screen_Columns) > 1)
2597 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002598 else
K.Takata54119102022-02-03 13:33:03 +00002599 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002600 else
K.Takata54119102022-02-03 13:33:03 +00002601 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002602 }
2603#endif
2604
2605 if (idx > wp->w_lines_valid)
2606 wp->w_lines_valid = idx;
2607
2608#ifdef FEAT_SYN_HL
2609 // Let the syntax stuff know we stop parsing here.
2610 if (syntax_last_parsed != 0 && syntax_present(wp))
2611 syntax_end_parsing(syntax_last_parsed + 1);
2612#endif
2613
2614 // If we didn't hit the end of the file, and we didn't finish the last
2615 // line we were working on, then the line didn't fit.
2616 wp->w_empty_rows = 0;
2617#ifdef FEAT_DIFF
2618 wp->w_filler_rows = 0;
2619#endif
2620 if (!eof && !didline)
2621 {
2622 if (lnum == wp->w_topline)
2623 {
2624 // Single line that does not fit!
2625 // Don't overwrite it, it can be edited.
2626 wp->w_botline = lnum + 1;
2627 }
2628#ifdef FEAT_DIFF
2629 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2630 {
2631 // Window ends in filler lines.
2632 wp->w_botline = lnum;
2633 wp->w_filler_rows = wp->w_height - srow;
2634 }
2635#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002636#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002637 else if (WIN_IS_POPUP(wp))
2638 {
2639 // popup line that doesn't fit is left as-is
2640 wp->w_botline = lnum;
2641 }
2642#endif
2643 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2644 {
2645 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2646
2647 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002648 screen_puts_len((char_u *)"@@", wp->w_width > 2 ? 2 : wp->w_width,
2649 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002650 screen_fill(scr_row, scr_row + 1,
2651 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
2652 '@', ' ', HL_ATTR(HLF_AT));
2653 set_empty_rows(wp, srow);
2654 wp->w_botline = lnum;
2655 }
2656 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2657 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002658 int start_col = (int)W_ENDCOL(wp) - 3;
2659
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002660 // Last line isn't finished: Display "@@@" at the end.
2661 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2662 W_WINROW(wp) + wp->w_height,
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002663 start_col < wp->w_wincol ? wp->w_wincol : start_col,
2664 (int)W_ENDCOL(wp),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002665 '@', '@', HL_ATTR(HLF_AT));
2666 set_empty_rows(wp, srow);
2667 wp->w_botline = lnum;
2668 }
2669 else
2670 {
2671 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
2672 wp->w_botline = lnum;
2673 }
2674 }
2675 else
2676 {
2677 draw_vsep_win(wp, row);
2678 if (eof) // we hit the end of the file
2679 {
2680 wp->w_botline = buf->b_ml.ml_line_count + 1;
2681#ifdef FEAT_DIFF
2682 j = diff_check_fill(wp, wp->w_botline);
2683 if (j > 0 && !wp->w_botfill)
2684 {
2685 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002686 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002687 i = '-';
2688 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002689 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002690 if (row + j > wp->w_height)
2691 j = wp->w_height - row;
2692 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2693 row += j;
2694 }
2695#endif
2696 }
2697 else if (dollar_vcol == -1)
2698 wp->w_botline = lnum;
2699
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002700 // Make sure the rest of the screen is blank.
2701 // write the "eob" character from 'fillchars' to rows that aren't part
2702 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002703 if (WIN_IS_POPUP(wp))
2704 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2705 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002706 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2707 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002708 }
2709
2710#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002711 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002712 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002713#endif
2714
2715 // Reset the type of redrawing required, the window has been updated.
2716 wp->w_redr_type = 0;
2717#ifdef FEAT_DIFF
2718 wp->w_old_topfill = wp->w_topfill;
2719 wp->w_old_botfill = wp->w_botfill;
2720#endif
2721
2722 if (dollar_vcol == -1)
2723 {
2724 // There is a trick with w_botline. If we invalidate it on each
2725 // change that might modify it, this will cause a lot of expensive
2726 // calls to plines() in update_topline() each time. Therefore the
2727 // value of w_botline is often approximated, and this value is used to
2728 // compute the value of w_topline. If the value of w_botline was
2729 // wrong, check that the value of w_topline is correct (cursor is on
2730 // the visible part of the text). If it's not, we need to redraw
2731 // again. Mostly this just means scrolling up a few lines, so it
2732 // doesn't look too bad. Only do this for the current window (where
2733 // changes are relevant).
2734 wp->w_valid |= VALID_BOTLINE;
2735 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2736 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002737 win_T *wwp;
2738#if defined(FEAT_CONCEAL)
2739 linenr_T old_topline = wp->w_topline;
2740 int new_wcol = wp->w_wcol;
2741#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002742 recursive = TRUE;
2743 curwin->w_valid &= ~VALID_TOPLINE;
2744 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002745
2746#if defined(FEAT_CONCEAL)
2747 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2748 != (VALID_WCOL|VALID_WROW))
2749 {
2750 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002751 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002752 // update_topline() the cursor's row or column got invalidated.
2753 // If they are left invalid, setcursor() will recompute them
2754 // but there won't be any further win_line() call to re-fix the
2755 // column and the cursor will end up misplaced. So we call
2756 // cursor validation now and reapply the fix again (or call
2757 // win_line() to do it for us).
2758 validate_cursor();
2759 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2760 && old_topline == wp->w_topline)
2761 wp->w_wcol = new_wcol;
2762 else
2763 redrawWinline(wp, wp->w_cursor.lnum);
2764 }
2765#endif
2766 // New redraw either due to updated topline or due to wcol fix.
2767 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002768 {
2769 // Don't update for changes in buffer again.
2770 i = curbuf->b_mod_set;
2771 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002772 j = curbuf->b_mod_xlines;
2773 curbuf->b_mod_xlines = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002774 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002775 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002776 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002777 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002778 // Other windows might have w_redr_type raised in update_topline().
2779 must_redraw = 0;
2780 FOR_ALL_WINDOWS(wwp)
2781 if (wwp->w_redr_type > must_redraw)
2782 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002783 recursive = FALSE;
2784 }
2785 }
2786
2787#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2788 // restore got_int, unless CTRL-C was hit while redrawing
2789 if (!got_int)
2790 got_int = save_got_int;
2791#endif
2792}
2793
2794#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2795/*
2796 * Prepare for updating one or more windows.
2797 * Caller must check for "updating_screen" already set to avoid recursiveness.
2798 */
2799 static void
2800update_prepare(void)
2801{
2802 cursor_off();
2803 updating_screen = TRUE;
2804#ifdef FEAT_GUI
2805 // Remove the cursor before starting to do anything, because scrolling may
2806 // make it difficult to redraw the text under it.
2807 if (gui.in_use)
2808 gui_undraw_cursor();
2809#endif
2810#ifdef FEAT_SEARCH_EXTRA
2811 start_search_hl();
2812#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002813#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002814 // Update popup_mask if needed.
2815 may_update_popup_mask(must_redraw);
2816#endif
2817}
2818
2819/*
2820 * Finish updating one or more windows.
2821 */
2822 static void
2823update_finish(void)
2824{
2825 if (redraw_cmdline || redraw_mode)
2826 showmode();
2827
2828# ifdef FEAT_SEARCH_EXTRA
2829 end_search_hl();
2830# endif
2831
2832 after_updating_screen(TRUE);
2833
2834# ifdef FEAT_GUI
2835 // Redraw the cursor and update the scrollbars when all screen updating is
2836 // done.
2837 if (gui.in_use)
2838 {
2839 out_flush_cursor(FALSE, FALSE);
2840 gui_update_scrollbars(FALSE);
2841 }
2842# endif
2843}
2844#endif
2845
2846#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2847 void
2848update_debug_sign(buf_T *buf, linenr_T lnum)
2849{
2850 win_T *wp;
2851 int doit = FALSE;
2852
2853# ifdef FEAT_FOLDING
2854 win_foldinfo.fi_level = 0;
2855# endif
2856
2857 // update/delete a specific sign
2858 redraw_buf_line_later(buf, lnum);
2859
2860 // check if it resulted in the need to redraw a window
2861 FOR_ALL_WINDOWS(wp)
2862 if (wp->w_redr_type != 0)
2863 doit = TRUE;
2864
2865 // Return when there is nothing to do, screen updating is already
2866 // happening (recursive call), messages on the screen or still starting up.
2867 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002868 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002869 || msg_scrolled
2870#ifdef FEAT_GUI
2871 || gui.starting
2872#endif
2873 || starting)
2874 return;
2875
2876 // update all windows that need updating
2877 update_prepare();
2878
2879 FOR_ALL_WINDOWS(wp)
2880 {
2881 if (wp->w_redr_type != 0)
2882 win_update(wp);
2883 if (wp->w_redr_status)
2884 win_redr_status(wp, FALSE);
2885 }
2886
2887 update_finish();
2888}
2889#endif
2890
2891#if defined(FEAT_GUI) || defined(PROTO)
2892/*
2893 * Update a single window, its status line and maybe the command line msg.
2894 * Used for the GUI scrollbar.
2895 */
2896 void
2897updateWindow(win_T *wp)
2898{
2899 // return if already busy updating
2900 if (updating_screen)
2901 return;
2902
2903 update_prepare();
2904
2905#ifdef FEAT_CLIPBOARD
2906 // When Visual area changed, may have to update selection.
2907 if (clip_star.available && clip_isautosel_star())
2908 clip_update_selection(&clip_star);
2909 if (clip_plus.available && clip_isautosel_plus())
2910 clip_update_selection(&clip_plus);
2911#endif
2912
2913 win_update(wp);
2914
2915 // When the screen was cleared redraw the tab pages line.
2916 if (redraw_tabline)
2917 draw_tabline();
2918
2919 if (wp->w_redr_status
2920# ifdef FEAT_CMDL_INFO
2921 || p_ru
2922# endif
2923# ifdef FEAT_STL_OPT
2924 || *p_stl != NUL || *wp->w_p_stl != NUL
2925# endif
2926 )
2927 win_redr_status(wp, FALSE);
2928
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002929#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002930 // Display popup windows on top of everything.
2931 update_popups(win_update);
2932#endif
2933
2934 update_finish();
2935}
2936#endif
2937
2938#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2939/*
2940 * Redraw as soon as possible. When the command line is not scrolled redraw
2941 * right away and restore what was on the command line.
2942 * Return a code indicating what happened.
2943 */
2944 int
2945redraw_asap(int type)
2946{
2947 int rows;
2948 int cols = screen_Columns;
2949 int r;
2950 int ret = 0;
2951 schar_T *screenline; // copy from ScreenLines[]
2952 sattr_T *screenattr; // copy from ScreenAttrs[]
2953 int i;
2954 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2955 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2956 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2957
2958 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002959 if (msg_scrolled
2960 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
2961 || exiting
2962 || p_ch == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002963 return ret;
2964
2965 // Allocate space to save the text displayed in the command line area.
2966 rows = screen_Rows - cmdline_row;
2967 screenline = LALLOC_MULT(schar_T, rows * cols);
2968 screenattr = LALLOC_MULT(sattr_T, rows * cols);
2969 if (screenline == NULL || screenattr == NULL)
2970 ret = 2;
2971 if (enc_utf8)
2972 {
2973 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
2974 if (screenlineUC == NULL)
2975 ret = 2;
2976 for (i = 0; i < p_mco; ++i)
2977 {
2978 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
2979 if (screenlineC[i] == NULL)
2980 ret = 2;
2981 }
2982 }
2983 if (enc_dbcs == DBCS_JPNU)
2984 {
2985 screenline2 = LALLOC_MULT(schar_T, rows * cols);
2986 if (screenline2 == NULL)
2987 ret = 2;
2988 }
2989
2990 if (ret != 2)
2991 {
2992 // Save the text displayed in the command line area.
2993 for (r = 0; r < rows; ++r)
2994 {
2995 mch_memmove(screenline + r * cols,
2996 ScreenLines + LineOffset[cmdline_row + r],
2997 (size_t)cols * sizeof(schar_T));
2998 mch_memmove(screenattr + r * cols,
2999 ScreenAttrs + LineOffset[cmdline_row + r],
3000 (size_t)cols * sizeof(sattr_T));
3001 if (enc_utf8)
3002 {
3003 mch_memmove(screenlineUC + r * cols,
3004 ScreenLinesUC + LineOffset[cmdline_row + r],
3005 (size_t)cols * sizeof(u8char_T));
3006 for (i = 0; i < p_mco; ++i)
3007 mch_memmove(screenlineC[i] + r * cols,
3008 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3009 (size_t)cols * sizeof(u8char_T));
3010 }
3011 if (enc_dbcs == DBCS_JPNU)
3012 mch_memmove(screenline2 + r * cols,
3013 ScreenLines2 + LineOffset[cmdline_row + r],
3014 (size_t)cols * sizeof(schar_T));
3015 }
3016
3017 update_screen(0);
3018 ret = 3;
3019
3020 if (must_redraw == 0)
3021 {
3022 int off = (int)(current_ScreenLine - ScreenLines);
3023
3024 // Restore the text displayed in the command line area.
3025 for (r = 0; r < rows; ++r)
3026 {
3027 mch_memmove(current_ScreenLine,
3028 screenline + r * cols,
3029 (size_t)cols * sizeof(schar_T));
3030 mch_memmove(ScreenAttrs + off,
3031 screenattr + r * cols,
3032 (size_t)cols * sizeof(sattr_T));
3033 if (enc_utf8)
3034 {
3035 mch_memmove(ScreenLinesUC + off,
3036 screenlineUC + r * cols,
3037 (size_t)cols * sizeof(u8char_T));
3038 for (i = 0; i < p_mco; ++i)
3039 mch_memmove(ScreenLinesC[i] + off,
3040 screenlineC[i] + r * cols,
3041 (size_t)cols * sizeof(u8char_T));
3042 }
3043 if (enc_dbcs == DBCS_JPNU)
3044 mch_memmove(ScreenLines2 + off,
3045 screenline2 + r * cols,
3046 (size_t)cols * sizeof(schar_T));
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003047 screen_line(curwin, cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003048 }
3049 ret = 4;
3050 }
3051 }
3052
3053 vim_free(screenline);
3054 vim_free(screenattr);
3055 if (enc_utf8)
3056 {
3057 vim_free(screenlineUC);
3058 for (i = 0; i < p_mco; ++i)
3059 vim_free(screenlineC[i]);
3060 }
3061 if (enc_dbcs == DBCS_JPNU)
3062 vim_free(screenline2);
3063
3064 // Show the intro message when appropriate.
3065 maybe_intro_message();
3066
3067 setcursor();
3068
3069 return ret;
3070}
3071#endif
3072
3073/*
3074 * Invoked after an asynchronous callback is called.
3075 * If an echo command was used the cursor needs to be put back where
3076 * it belongs. If highlighting was changed a redraw is needed.
3077 * If "call_update_screen" is FALSE don't call update_screen() when at the
3078 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003079 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003080 */
3081 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003082redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003083{
3084 ++redrawing_for_callback;
3085
Bram Moolenaar24959102022-05-07 20:01:16 +01003086 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3087 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3088 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003089 {
3090 if (do_message)
3091 repeat_message();
3092 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003093 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003094 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003095 if (pum_visible())
3096 cmdline_pum_display();
Bram Moolenaar54162322022-08-26 16:58:51 +01003097
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003098 // Don't redraw when in prompt_for_number().
3099 if (cmdline_row > 0)
3100 {
3101 // Redrawing only works when the screen didn't scroll. Don't clear
3102 // wildmenu entries.
3103 if (msg_scrolled == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003104 && wild_menu_showing == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003105 && call_update_screen)
3106 update_screen(0);
3107
3108 // Redraw in the same position, so that the user can continue
3109 // editing the command.
3110 redrawcmdline_ex(FALSE);
3111 }
3112 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003113 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003114 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003115 update_topline();
3116 validate_cursor();
3117
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003118 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003119 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003120 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003121
3122 if (msg_scrolled == 0)
3123 {
3124 // don't want a hit-enter prompt when something else is displayed
3125 msg_didany = FALSE;
3126 need_wait_return = FALSE;
3127 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003128 }
3129 cursor_on();
3130#ifdef FEAT_GUI
3131 if (gui.in_use && !gui_mch_is_blink_off())
3132 // Don't update the cursor when it is blinking and off to avoid
3133 // flicker.
3134 out_flush_cursor(FALSE, FALSE);
3135 else
3136#endif
3137 out_flush();
3138
3139 --redrawing_for_callback;
3140}
3141
3142/*
3143 * Redraw the current window later, with update_screen(type).
3144 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003145 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003146 */
3147 void
3148redraw_later(int type)
3149{
3150 redraw_win_later(curwin, type);
3151}
3152
3153 void
3154redraw_win_later(
3155 win_T *wp,
3156 int type)
3157{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003158 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003159 {
3160 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003161 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003162 wp->w_lines_valid = 0;
3163 if (must_redraw < type) // must_redraw is the maximum of all windows
3164 must_redraw = type;
3165 }
3166}
3167
3168/*
3169 * Force a complete redraw later. Also resets the highlighting. To be used
3170 * after executing a shell command that messes up the screen.
3171 */
3172 void
3173redraw_later_clear(void)
3174{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003175 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003176 reset_screen_attr();
3177}
3178
3179/*
3180 * Mark all windows to be redrawn later.
3181 */
3182 void
3183redraw_all_later(int type)
3184{
3185 win_T *wp;
3186
3187 FOR_ALL_WINDOWS(wp)
3188 redraw_win_later(wp, type);
3189 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003190 set_must_redraw(type);
3191}
3192
3193/*
3194 * Set "must_redraw" to "type" unless it already has a higher value
3195 * or it is currently not allowed.
3196 */
3197 void
3198set_must_redraw(int type)
3199{
3200 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003201 must_redraw = type;
3202}
3203
3204/*
3205 * Mark all windows that are editing the current buffer to be updated later.
3206 */
3207 void
3208redraw_curbuf_later(int type)
3209{
3210 redraw_buf_later(curbuf, type);
3211}
3212
3213 void
3214redraw_buf_later(buf_T *buf, int type)
3215{
3216 win_T *wp;
3217
3218 FOR_ALL_WINDOWS(wp)
3219 {
3220 if (wp->w_buffer == buf)
3221 redraw_win_later(wp, type);
3222 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003223#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3224 // terminal in popup window is not in list of windows
3225 if (curwin->w_buffer == buf)
3226 redraw_win_later(curwin, type);
3227#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003228}
3229
3230#if defined(FEAT_SIGNS) || defined(PROTO)
3231 void
3232redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3233{
3234 win_T *wp;
3235
3236 FOR_ALL_WINDOWS(wp)
3237 if (wp->w_buffer == buf && lnum >= wp->w_topline
3238 && lnum < wp->w_botline)
3239 redrawWinline(wp, lnum);
3240}
3241#endif
3242
3243#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3244 void
3245redraw_buf_and_status_later(buf_T *buf, int type)
3246{
3247 win_T *wp;
3248
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003249 if (wild_menu_showing != 0)
3250 // Don't redraw while the command line completion is displayed, it
3251 // would disappear.
3252 return;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003253 FOR_ALL_WINDOWS(wp)
3254 {
3255 if (wp->w_buffer == buf)
3256 {
3257 redraw_win_later(wp, type);
3258 wp->w_redr_status = TRUE;
3259 }
3260 }
3261}
3262#endif
3263
3264/*
3265 * mark all status lines for redraw; used after first :cd
3266 */
3267 void
3268status_redraw_all(void)
3269{
3270 win_T *wp;
3271
3272 FOR_ALL_WINDOWS(wp)
3273 if (wp->w_status_height)
3274 {
3275 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003276 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003277 }
3278}
3279
3280/*
3281 * mark all status lines of the current buffer for redraw
3282 */
3283 void
3284status_redraw_curbuf(void)
3285{
3286 win_T *wp;
3287
3288 FOR_ALL_WINDOWS(wp)
3289 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3290 {
3291 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003292 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003293 }
3294}
3295
3296/*
3297 * Redraw all status lines that need to be redrawn.
3298 */
3299 void
3300redraw_statuslines(void)
3301{
3302 win_T *wp;
3303
3304 FOR_ALL_WINDOWS(wp)
3305 if (wp->w_redr_status)
3306 win_redr_status(wp, FALSE);
3307 if (redraw_tabline)
3308 draw_tabline();
3309}
3310
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003311/*
3312 * Redraw all status lines at the bottom of frame "frp".
3313 */
3314 void
3315win_redraw_last_status(frame_T *frp)
3316{
3317 if (frp->fr_layout == FR_LEAF)
3318 frp->fr_win->w_redr_status = TRUE;
3319 else if (frp->fr_layout == FR_ROW)
3320 {
3321 FOR_ALL_FRAMES(frp, frp->fr_child)
3322 win_redraw_last_status(frp);
3323 }
3324 else // frp->fr_layout == FR_COL
3325 {
3326 frp = frp->fr_child;
3327 while (frp->fr_next != NULL)
3328 frp = frp->fr_next;
3329 win_redraw_last_status(frp);
3330 }
3331}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003332
3333/*
3334 * Changed something in the current window, at buffer line "lnum", that
3335 * requires that line and possibly other lines to be redrawn.
3336 * Used when entering/leaving Insert mode with the cursor on a folded line.
3337 * Used to remove the "$" from a change command.
3338 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3339 * may become invalid and the whole window will have to be redrawn.
3340 */
3341 void
3342redrawWinline(
3343 win_T *wp,
3344 linenr_T lnum)
3345{
3346 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
3347 wp->w_redraw_top = lnum;
3348 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
3349 wp->w_redraw_bot = lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003350 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003351}