blob: 578c66f2b9d39240ff08bbfc10a46be1479930b1 [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
31 * call redraw_later(VALID) to have the window displayed by update_screen()
32 * 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
43 * settings), must call redraw_later(NOT_VALID) to have the whole window
44 * redisplayed by update_screen() later.
45 *
46 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
47 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
48 * buffer redisplayed by update_screen() later.
49 *
50 * Commands that change highlighting and possibly cause a scroll too must call
51 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
52 * to avoid redrawing everything. But the length of displayed lines must not
53 * change, use NOT_VALID then.
54 *
55 * Commands that move the window position must call redraw_later(NOT_VALID).
56 * TODO: should minimize redrawing by scrolling when possible.
57 *
58 * Commands that change everything (e.g., resizing the screen) must call
59 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
60 *
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
102 if (type == VALID_NO_UPDATE)
103 {
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[].
137 if (curwin->w_lines_valid == 0 && type < NOT_VALID
138#ifdef FEAT_TERMINAL
139 && !term_do_update_window(curwin)
140#endif
141 )
142 type = NOT_VALID;
143
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;
150 if (type > INVERTED_ALL)
151 curwin->w_lines_valid = 0; // don't use w_lines[].wl_size now
152 return FAIL;
153 }
154 updating_screen = TRUE;
155
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100156#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200157 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
158 // in some windows.
159 may_update_popup_mask(type);
160#endif
161
162#ifdef FEAT_SYN_HL
163 ++display_tick; // let syntax code know we're in a next round of
164 // display updating
165#endif
166 if (no_update)
167 ++no_win_do_lines_ins;
168
169 // if the screen was scrolled up when displaying a message, scroll it down
170 if (msg_scrolled)
171 {
172 clear_cmdline = TRUE;
173 if (msg_scrolled > Rows - 5) // clearing is faster
174 type = CLEAR;
175 else if (type != CLEAR)
176 {
177 check_for_delay(FALSE);
178 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
179 == FAIL)
180 type = CLEAR;
181 FOR_ALL_WINDOWS(wp)
182 {
183 if (wp->w_winrow < msg_scrolled)
184 {
185 if (W_WINROW(wp) + wp->w_height > msg_scrolled
186 && wp->w_redr_type < REDRAW_TOP
187 && wp->w_lines_valid > 0
188 && wp->w_topline == wp->w_lines[0].wl_lnum)
189 {
190 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
191 wp->w_redr_type = REDRAW_TOP;
192 }
193 else
194 {
195 wp->w_redr_type = NOT_VALID;
196 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
197 <= msg_scrolled)
198 wp->w_redr_status = TRUE;
199 }
200 }
201 }
202 if (!no_update)
203 redraw_cmdline = TRUE;
204 redraw_tabline = TRUE;
205 }
206 msg_scrolled = 0;
207 need_wait_return = FALSE;
208 }
209
210 // reset cmdline_row now (may have been changed temporarily)
211 compute_cmdrow();
212
213 // Check for changed highlighting
214 if (need_highlight_changed)
215 highlight_changed();
216
217 if (type == CLEAR) // first clear screen
218 {
219 screenclear(); // will reset clear_cmdline
220 type = NOT_VALID;
221 // must_redraw may be set indirectly, avoid another redraw later
222 must_redraw = 0;
223 }
224
225 if (clear_cmdline) // going to clear cmdline (done below)
226 check_for_delay(FALSE);
227
228#ifdef FEAT_LINEBREAK
229 // Force redraw when width of 'number' or 'relativenumber' column
230 // changes.
231 if (curwin->w_redr_type < NOT_VALID
232 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
233 ? number_width(curwin) : 0))
234 curwin->w_redr_type = NOT_VALID;
235#endif
236
237 // Only start redrawing if there is really something to do.
238 if (type == INVERTED)
239 update_curswant();
240 if (curwin->w_redr_type < type
241 && !((type == VALID
242 && curwin->w_lines[0].wl_valid
243#ifdef FEAT_DIFF
244 && curwin->w_topfill == curwin->w_old_topfill
245 && curwin->w_botfill == curwin->w_old_botfill
246#endif
247 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
248 || (type == INVERTED
249 && VIsual_active
250 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
251 && curwin->w_old_visual_mode == VIsual_mode
252 && (curwin->w_valid & VALID_VIRTCOL)
253 && curwin->w_old_curswant == curwin->w_curswant)
254 ))
255 curwin->w_redr_type = type;
256
257 // Redraw the tab pages line if needed.
258 if (redraw_tabline || type >= NOT_VALID)
259 draw_tabline();
260
261#ifdef FEAT_SYN_HL
262 // Correct stored syntax highlighting info for changes in each displayed
263 // buffer. Each buffer must only be done once.
264 FOR_ALL_WINDOWS(wp)
265 {
266 if (wp->w_buffer->b_mod_set)
267 {
268 win_T *wwp;
269
270 // Check if we already did this buffer.
271 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
272 if (wwp->w_buffer == wp->w_buffer)
273 break;
274 if (wwp == wp && syntax_present(wp))
275 syn_stack_apply_changes(wp->w_buffer);
276 }
277 }
278#endif
279
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200280 if (pum_redraw_in_same_position())
281 // Avoid flicker if the popup menu is going to be redrawn in the same
282 // position.
283 pum_will_redraw = TRUE;
284
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200285 // Go from top to bottom through the windows, redrawing the ones that need
286 // it.
287#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100288 did_update_one_window = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200289#endif
290#ifdef FEAT_SEARCH_EXTRA
291 screen_search_hl.rm.regprog = NULL;
292#endif
293 FOR_ALL_WINDOWS(wp)
294 {
295 if (wp->w_redr_type != 0)
296 {
297 cursor_off();
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100298#ifdef FEAT_GUI
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200299 if (!did_one)
300 {
301 did_one = TRUE;
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100302
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200303 // Remove the cursor before starting to do anything, because
304 // scrolling may make it difficult to redraw the text under
305 // it.
Bram Moolenaar09f067f2021-04-11 13:29:18 +0200306 // Also remove the cursor if it needs to be hidden due to an
307 // ongoing cursor-less sleep.
308 if (gui.in_use && (wp == curwin || cursor_is_sleeping()))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200309 {
310 gui_cursor_col = gui.cursor_col;
311 gui_cursor_row = gui.cursor_row;
312 gui_undraw_cursor();
313 did_undraw = TRUE;
314 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200315 }
316#endif
317 win_update(wp);
318 }
319
320 // redraw status line after the window to minimize cursor movement
321 if (wp->w_redr_status)
322 {
323 cursor_off();
324 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
325 }
326 }
327#if defined(FEAT_SEARCH_EXTRA)
328 end_search_hl();
329#endif
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200330
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200331 // May need to redraw the popup menu.
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200332 pum_will_redraw = save_pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200333 pum_may_redraw();
334
335 // Reset b_mod_set flags. Going through all windows is probably faster
336 // than going through all buffers (there could be many buffers).
337 FOR_ALL_WINDOWS(wp)
338 wp->w_buffer->b_mod_set = FALSE;
339
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100340#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200341 // Display popup windows on top of the windows and command line.
342 update_popups(win_update);
343#endif
344
Bram Moolenaar3194e5b2021-12-13 21:59:09 +0000345#ifdef FEAT_TERMINAL
346 FOR_ALL_WINDOWS(wp)
347 // If this window contains a terminal, after redrawing all windows, the
348 // dirty row range can be reset.
349 term_did_update_window(wp);
350#endif
351
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200352 after_updating_screen(TRUE);
353
354 // Clear or redraw the command line. Done last, because scrolling may
355 // mess up the command line.
356 if (clear_cmdline || redraw_cmdline || redraw_mode)
357 showmode();
358
359 if (no_update)
360 --no_win_do_lines_ins;
361
362 // May put up an introductory message when not editing a file
363 if (!did_intro)
364 maybe_intro_message();
365 did_intro = TRUE;
366
367#ifdef FEAT_GUI
368 // Redraw the cursor and update the scrollbars when all screen updating is
369 // done.
370 if (gui.in_use)
371 {
372 if (did_undraw && !gui_mch_is_blink_off())
373 {
374 mch_disable_flush();
375 out_flush(); // required before updating the cursor
376 mch_enable_flush();
377
378 // Put the GUI position where the cursor was, gui_update_cursor()
379 // uses that.
380 gui.col = gui_cursor_col;
381 gui.row = gui_cursor_row;
382 gui.col = mb_fix_col(gui.col, gui.row);
383 gui_update_cursor(FALSE, FALSE);
384 gui_may_flush();
385 screen_cur_col = gui.col;
386 screen_cur_row = gui.row;
387 }
388 else
389 out_flush();
390 gui_update_scrollbars(FALSE);
391 }
392#endif
393 return OK;
394}
395
396/*
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200397 * Return the row for drawing the statusline and the ruler of window "wp".
398 */
Bram Moolenaar49c51b82021-04-01 16:16:18 +0200399 int
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200400statusline_row(win_T *wp)
401{
402#if defined(FEAT_PROP_POPUP)
403 // If the window is really zero height the winbar isn't displayed.
404 if (wp->w_frame->fr_height == wp->w_status_height && !popup_is_popup(wp))
405 return wp->w_winrow;
406#endif
407 return W_WINROW(wp) + wp->w_height;
408}
409
410/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200411 * Redraw the status line of window wp.
412 *
413 * If inversion is possible we use it. Else '=' characters are used.
414 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
415 * displayed.
416 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200417 static void
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200418win_redr_status(win_T *wp, int ignore_pum UNUSED)
419{
420 int row;
421 char_u *p;
422 int len;
423 int fillchar;
424 int attr;
425 int this_ru_col;
426 static int busy = FALSE;
427
428 // It's possible to get here recursively when 'statusline' (indirectly)
429 // invokes ":redrawstatus". Simply ignore the call then.
430 if (busy)
431 return;
432 busy = TRUE;
433
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200434 row = statusline_row(wp);
435
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200436 wp->w_redr_status = FALSE;
437 if (wp->w_status_height == 0)
438 {
439 // no status line, can only be last window
440 redraw_cmdline = TRUE;
441 }
442 else if (!redrawing()
443 // don't update status line when popup menu is visible and may be
444 // drawn over it, unless it will be redrawn later
445 || (!ignore_pum && pum_visible()))
446 {
447 // Don't redraw right now, do it later.
448 wp->w_redr_status = TRUE;
449 }
450#ifdef FEAT_STL_OPT
451 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
452 {
453 // redraw custom status line
454 redraw_custom_statusline(wp);
455 }
456#endif
457 else
458 {
459 fillchar = fillchar_status(&attr, wp);
460
461 get_trans_bufname(wp->w_buffer);
462 p = NameBuff;
463 len = (int)STRLEN(p);
464
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000465 if ((bt_help(wp->w_buffer)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200466#ifdef FEAT_QUICKFIX
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000467 || wp->w_p_pvw
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200468#endif
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000469 || bufIsChanged(wp->w_buffer)
470 || wp->w_buffer->b_p_ro)
471 && len < MAXPATHL - 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200472 *(p + len++) = ' ';
473 if (bt_help(wp->w_buffer))
474 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100475 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Help]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200476 len += (int)STRLEN(p + len);
477 }
478#ifdef FEAT_QUICKFIX
479 if (wp->w_p_pvw)
480 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100481 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Preview]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200482 len += (int)STRLEN(p + len);
483 }
484#endif
485 if (bufIsChanged(wp->w_buffer)
486#ifdef FEAT_TERMINAL
487 && !bt_terminal(wp->w_buffer)
488#endif
489 )
490 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100491 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", "[+]");
492 len += (int)STRLEN(p + len);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200493 }
494 if (wp->w_buffer->b_p_ro)
495 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100496 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[RO]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200497 len += (int)STRLEN(p + len);
498 }
499
500 this_ru_col = ru_col - (Columns - wp->w_width);
501 if (this_ru_col < (wp->w_width + 1) / 2)
502 this_ru_col = (wp->w_width + 1) / 2;
503 if (this_ru_col <= 1)
504 {
505 p = (char_u *)"<"; // No room for file name!
506 len = 1;
507 }
508 else if (has_mbyte)
509 {
510 int clen = 0, i;
511
512 // Count total number of display cells.
513 clen = mb_string2cells(p, -1);
514
515 // Find first character that will fit.
516 // Going from start to end is much faster for DBCS.
517 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
518 i += (*mb_ptr2len)(p + i))
519 clen -= (*mb_ptr2cells)(p + i);
520 len = clen;
521 if (i > 0)
522 {
523 p = p + i - 1;
524 *p = '<';
525 ++len;
526 }
527
528 }
529 else if (len > this_ru_col - 1)
530 {
531 p += len - (this_ru_col - 1);
532 *p = '<';
533 len = this_ru_col - 1;
534 }
535
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200536 screen_puts(p, row, wp->w_wincol, attr);
537 screen_fill(row, row + 1, len + wp->w_wincol,
538 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
539
540 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000541 && (this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200542 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
543 - 1 + wp->w_wincol), attr);
544
545#ifdef FEAT_CMDL_INFO
546 win_redr_ruler(wp, TRUE, ignore_pum);
547#endif
548 }
549
550 /*
551 * May need to draw the character below the vertical separator.
552 */
553 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
554 {
555 if (stl_connected(wp))
556 fillchar = fillchar_status(&attr, wp);
557 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100558 fillchar = fillchar_vsep(&attr, wp);
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200559 screen_putchar(fillchar, row, W_ENDCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200560 }
561 busy = FALSE;
562}
563
564#ifdef FEAT_STL_OPT
565/*
566 * Redraw the status line according to 'statusline' and take care of any
567 * errors encountered.
568 */
569 static void
570redraw_custom_statusline(win_T *wp)
571{
572 static int entered = FALSE;
573 int saved_did_emsg = did_emsg;
574
575 // When called recursively return. This can happen when the statusline
576 // contains an expression that triggers a redraw.
577 if (entered)
578 return;
579 entered = TRUE;
580
581 did_emsg = FALSE;
582 win_redr_custom(wp, FALSE);
583 if (did_emsg)
584 {
585 // When there is an error disable the statusline, otherwise the
586 // display is messed up with errors and a redraw triggers the problem
587 // again and again.
588 set_string_option_direct((char_u *)"statusline", -1,
589 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
590 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
591 }
592 did_emsg |= saved_did_emsg;
593 entered = FALSE;
594}
595#endif
596
597/*
598 * Show current status info in ruler and various other places
599 * If always is FALSE, only show ruler if position has changed.
600 */
601 void
602showruler(int always)
603{
604 if (!always && !redrawing())
605 return;
606 if (pum_visible())
607 {
608 // Don't redraw right now, do it later.
609 curwin->w_redr_status = TRUE;
610 return;
611 }
612#if defined(FEAT_STL_OPT)
613 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
614 redraw_custom_statusline(curwin);
615 else
616#endif
617#ifdef FEAT_CMDL_INFO
618 win_redr_ruler(curwin, always, FALSE);
619#endif
620
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200621 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000622#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200623 || (p_icon && (stl_syntax & STL_IN_ICON))
624 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000625#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200626 )
627 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000628
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200629 // Redraw the tab pages line if needed.
630 if (redraw_tabline)
631 draw_tabline();
632}
633
634#if defined(FEAT_CMDL_INFO) || defined(PROTO)
635 void
636win_redr_ruler(win_T *wp, int always, int ignore_pum)
637{
638#define RULER_BUF_LEN 70
639 char_u buffer[RULER_BUF_LEN];
640 int row;
641 int fillchar;
642 int attr;
643 int empty_line = FALSE;
644 colnr_T virtcol;
645 int i;
646 size_t len;
647 int o;
648 int this_ru_col;
649 int off = 0;
650 int width;
651
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100652 // If 'ruler' off or messages area disabled, don't do anything
653 if (!p_ru || (wp->w_status_height == 0 && p_ch == 0))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200654 return;
655
656 /*
657 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
658 * after deleting lines, before cursor.lnum is corrected.
659 */
660 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
661 return;
662
663 // Don't draw the ruler while doing insert-completion, it might overwrite
664 // the (long) mode message.
665 if (wp == lastwin && lastwin->w_status_height == 0)
666 if (edit_submode != NULL)
667 return;
668 // Don't draw the ruler when the popup menu is visible, it may overlap.
669 // Except when the popup menu will be redrawn anyway.
670 if (!ignore_pum && pum_visible())
671 return;
672
673#ifdef FEAT_STL_OPT
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100674 if (*p_ruf && p_ch > 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200675 {
Bram Moolenaar53989552019-12-23 22:59:18 +0100676 int called_emsg_before = called_emsg;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200677
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200678 win_redr_custom(wp, TRUE);
Bram Moolenaar53989552019-12-23 22:59:18 +0100679 if (called_emsg > called_emsg_before)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200680 set_string_option_direct((char_u *)"rulerformat", -1,
681 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200682 return;
683 }
684#endif
685
686 /*
687 * Check if not in Insert mode and the line is empty (will show "0-1").
688 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100689 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200690 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
691 empty_line = TRUE;
692
693 /*
694 * Only draw the ruler when something changed.
695 */
696 validate_virtcol_win(wp);
697 if ( redraw_cmdline
698 || always
699 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
700 || wp->w_cursor.col != wp->w_ru_cursor.col
701 || wp->w_virtcol != wp->w_ru_virtcol
702 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
703 || wp->w_topline != wp->w_ru_topline
704 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
705#ifdef FEAT_DIFF
706 || wp->w_topfill != wp->w_ru_topfill
707#endif
708 || empty_line != wp->w_ru_empty)
709 {
710 cursor_off();
711 if (wp->w_status_height)
712 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200713 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200714 fillchar = fillchar_status(&attr, wp);
715 off = wp->w_wincol;
716 width = wp->w_width;
717 }
718 else
719 {
720 row = Rows - 1;
721 fillchar = ' ';
722 attr = 0;
723 width = Columns;
724 off = 0;
725 }
726
727 // In list mode virtcol needs to be recomputed
728 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100729 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200730 {
731 wp->w_p_list = FALSE;
732 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
733 wp->w_p_list = TRUE;
734 }
735
736 /*
737 * Some sprintfs return the length, some return a pointer.
738 * To avoid portability problems we use strlen() here.
739 */
740 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
741 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
742 ? 0L
743 : (long)(wp->w_cursor.lnum));
744 len = STRLEN(buffer);
745 col_print(buffer + len, RULER_BUF_LEN - len,
746 empty_line ? 0 : (int)wp->w_cursor.col + 1,
747 (int)virtcol + 1);
748
749 /*
750 * Add a "50%" if there is room for it.
751 * On the last line, don't print in the last column (scrolls the
752 * screen up on some terminals).
753 */
754 i = (int)STRLEN(buffer);
755 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
756 o = i + vim_strsize(buffer + i + 1);
757 if (wp->w_status_height == 0) // can't use last char of screen
758 ++o;
759 this_ru_col = ru_col - (Columns - width);
760 if (this_ru_col < 0)
761 this_ru_col = 0;
762 // Never use more than half the window/screen width, leave the other
763 // half for the filename.
764 if (this_ru_col < (width + 1) / 2)
765 this_ru_col = (width + 1) / 2;
766 if (this_ru_col + o < width)
767 {
768 // need at least 3 chars left for get_rel_pos() + NUL
769 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
770 {
771 if (has_mbyte)
772 i += (*mb_char2bytes)(fillchar, buffer + i);
773 else
774 buffer[i++] = fillchar;
775 ++o;
776 }
777 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
778 }
779 // Truncate at window boundary.
780 if (has_mbyte)
781 {
782 o = 0;
783 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
784 {
785 o += (*mb_ptr2cells)(buffer + i);
786 if (this_ru_col + o > width)
787 {
788 buffer[i] = NUL;
789 break;
790 }
791 }
792 }
793 else if (this_ru_col + (int)STRLEN(buffer) > width)
794 buffer[width - this_ru_col] = NUL;
795
796 screen_puts(buffer, row, this_ru_col + off, attr);
797 i = redraw_cmdline;
798 screen_fill(row, row + 1,
799 this_ru_col + off + (int)STRLEN(buffer),
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000800 (off + width),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200801 fillchar, fillchar, attr);
802 // don't redraw the cmdline because of showing the ruler
803 redraw_cmdline = i;
804 wp->w_ru_cursor = wp->w_cursor;
805 wp->w_ru_virtcol = wp->w_virtcol;
806 wp->w_ru_empty = empty_line;
807 wp->w_ru_topline = wp->w_topline;
808 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
809#ifdef FEAT_DIFF
810 wp->w_ru_topfill = wp->w_topfill;
811#endif
812 }
813}
814#endif
815
816/*
817 * To be called when "updating_screen" was set before and now the postponed
818 * side effects may take place.
819 */
820 void
821after_updating_screen(int may_resize_shell UNUSED)
822{
823 updating_screen = FALSE;
824#ifdef FEAT_GUI
825 if (may_resize_shell)
826 gui_may_resize_shell();
827#endif
828#ifdef FEAT_TERMINAL
829 term_check_channel_closed_recently();
830#endif
831
832#ifdef HAVE_DROP_FILE
833 // If handle_drop() was called while updating_screen was TRUE need to
834 // handle the drop now.
835 handle_any_postponed_drop();
836#endif
837}
838
839/*
840 * Update all windows that are editing the current buffer.
841 */
842 void
843update_curbuf(int type)
844{
845 redraw_curbuf_later(type);
846 update_screen(type);
847}
848
849#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
850/*
851 * Copy "text" to ScreenLines using "attr".
852 * Returns the next screen column.
853 */
854 static int
855text_to_screenline(win_T *wp, char_u *text, int col)
856{
857 int off = (int)(current_ScreenLine - ScreenLines);
858
859 if (has_mbyte)
860 {
861 int cells;
862 int u8c, u8cc[MAX_MCO];
863 int i;
864 int idx;
865 int c_len;
866 char_u *p;
867# ifdef FEAT_ARABIC
868 int prev_c = 0; // previous Arabic character
869 int prev_c1 = 0; // first composing char for prev_c
870# endif
871
872# ifdef FEAT_RIGHTLEFT
873 if (wp->w_p_rl)
874 idx = off;
875 else
876# endif
877 idx = off + col;
878
879 // Store multibyte characters in ScreenLines[] et al. correctly.
880 for (p = text; *p != NUL; )
881 {
882 cells = (*mb_ptr2cells)(p);
883 c_len = (*mb_ptr2len)(p);
884 if (col + cells > wp->w_width
885# ifdef FEAT_RIGHTLEFT
886 - (wp->w_p_rl ? col : 0)
887# endif
888 )
889 break;
890 ScreenLines[idx] = *p;
891 if (enc_utf8)
892 {
893 u8c = utfc_ptr2char(p, u8cc);
894 if (*p < 0x80 && u8cc[0] == 0)
895 {
896 ScreenLinesUC[idx] = 0;
897#ifdef FEAT_ARABIC
898 prev_c = u8c;
899#endif
900 }
901 else
902 {
903#ifdef FEAT_ARABIC
904 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
905 {
906 // Do Arabic shaping.
907 int pc, pc1, nc;
908 int pcc[MAX_MCO];
909 int firstbyte = *p;
910
911 // The idea of what is the previous and next
912 // character depends on 'rightleft'.
913 if (wp->w_p_rl)
914 {
915 pc = prev_c;
916 pc1 = prev_c1;
917 nc = utf_ptr2char(p + c_len);
918 prev_c1 = u8cc[0];
919 }
920 else
921 {
922 pc = utfc_ptr2char(p + c_len, pcc);
923 nc = prev_c;
924 pc1 = pcc[0];
925 }
926 prev_c = u8c;
927
928 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
929 pc, pc1, nc);
930 ScreenLines[idx] = firstbyte;
931 }
932 else
933 prev_c = u8c;
934#endif
935 // Non-BMP character: display as ? or fullwidth ?.
936 ScreenLinesUC[idx] = u8c;
937 for (i = 0; i < Screen_mco; ++i)
938 {
939 ScreenLinesC[i][idx] = u8cc[i];
940 if (u8cc[i] == 0)
941 break;
942 }
943 }
944 if (cells > 1)
945 ScreenLines[idx + 1] = 0;
946 }
947 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
948 // double-byte single width character
949 ScreenLines2[idx] = p[1];
950 else if (cells > 1)
951 // double-width character
952 ScreenLines[idx + 1] = p[1];
953 col += cells;
954 idx += cells;
955 p += c_len;
956 }
957 }
958 else
959 {
960 int len = (int)STRLEN(text);
961
962 if (len > wp->w_width - col)
963 len = wp->w_width - col;
964 if (len > 0)
965 {
966#ifdef FEAT_RIGHTLEFT
967 if (wp->w_p_rl)
968 mch_memmove(current_ScreenLine, text, len);
969 else
970#endif
971 mch_memmove(current_ScreenLine + col, text, len);
972 col += len;
973 }
974 }
975 return col;
976}
977#endif
978
979#ifdef FEAT_MENU
980/*
981 * Draw the window toolbar.
982 */
983 static void
984redraw_win_toolbar(win_T *wp)
985{
986 vimmenu_T *menu;
987 int item_idx = 0;
988 int item_count = 0;
989 int col = 0;
990 int next_col;
991 int off = (int)(current_ScreenLine - ScreenLines);
992 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
993 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
994
995 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200996 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200997 ++item_count;
998 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
999
1000 // TODO: use fewer spaces if there is not enough room
1001 for (menu = wp->w_winbar->children;
1002 menu != NULL && col < wp->w_width; menu = menu->next)
1003 {
1004 space_to_screenline(off + col, fill_attr);
1005 if (++col >= wp->w_width)
1006 break;
1007 if (col > 1)
1008 {
1009 space_to_screenline(off + col, fill_attr);
1010 if (++col >= wp->w_width)
1011 break;
1012 }
1013
1014 wp->w_winbar_items[item_idx].wb_startcol = col;
1015 space_to_screenline(off + col, button_attr);
1016 if (++col >= wp->w_width)
1017 break;
1018
1019 next_col = text_to_screenline(wp, menu->name, col);
1020 while (col < next_col)
1021 {
1022 ScreenAttrs[off + col] = button_attr;
1023 ++col;
1024 }
1025 wp->w_winbar_items[item_idx].wb_endcol = col;
1026 wp->w_winbar_items[item_idx].wb_menu = menu;
1027 ++item_idx;
1028
1029 if (col >= wp->w_width)
1030 break;
1031 space_to_screenline(off + col, button_attr);
1032 ++col;
1033 }
1034 while (col < wp->w_width)
1035 {
1036 space_to_screenline(off + col, fill_attr);
1037 ++col;
1038 }
1039 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1040
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001041 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001042}
1043#endif
1044
1045#if defined(FEAT_FOLDING) || defined(PROTO)
1046/*
1047 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1048 */
1049 static void
1050copy_text_attr(
1051 int off,
1052 char_u *buf,
1053 int len,
1054 int attr)
1055{
1056 int i;
1057
1058 mch_memmove(ScreenLines + off, buf, (size_t)len);
1059 if (enc_utf8)
1060 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1061 for (i = 0; i < len; ++i)
1062 ScreenAttrs[off + i] = attr;
1063}
1064
1065/*
1066 * Display one folded line.
1067 */
1068 static void
1069fold_line(
1070 win_T *wp,
1071 long fold_count,
1072 foldinfo_T *foldinfo,
1073 linenr_T lnum,
1074 int row)
1075{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001076 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1077 // multi-byte character is MAX_MCO.
1078 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001079 pos_T *top, *bot;
1080 linenr_T lnume = lnum + fold_count - 1;
1081 int len;
1082 char_u *text;
1083 int fdc;
1084 int col;
1085 int txtcol;
1086 int off = (int)(current_ScreenLine - ScreenLines);
1087 int ri;
1088
1089 // Build the fold line:
1090 // 1. Add the cmdwin_type for the command-line window
1091 // 2. Add the 'foldcolumn'
1092 // 3. Add the 'number' or 'relativenumber' column
1093 // 4. Compose the text
1094 // 5. Add the text
1095 // 6. set highlighting for the Visual area an other text
1096 col = 0;
1097
1098 // 1. Add the cmdwin_type for the command-line window
1099 // Ignores 'rightleft', this window is never right-left.
1100#ifdef FEAT_CMDWIN
1101 if (cmdwin_type != 0 && wp == curwin)
1102 {
1103 ScreenLines[off] = cmdwin_type;
1104 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1105 if (enc_utf8)
1106 ScreenLinesUC[off] = 0;
1107 ++col;
1108 }
1109#endif
1110
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001111#ifdef FEAT_RIGHTLEFT
1112# define RL_MEMSET(p, v, l) \
1113 do { \
1114 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001115 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001116 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1117 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001118 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001119 ScreenAttrs[off + (p) + ri] = v; \
1120 } while (0)
1121#else
1122# define RL_MEMSET(p, v, l) \
1123 do { \
1124 for (ri = 0; ri < l; ++ri) \
1125 ScreenAttrs[off + (p) + ri] = v; \
1126 } while (0)
1127#endif
1128
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001129 // 2. Add the 'foldcolumn'
1130 // Reduce the width when there is not enough space.
1131 fdc = compute_foldcolumn(wp, col);
1132 if (fdc > 0)
1133 {
1134 char_u *p;
1135 int i;
1136 int idx;
1137
1138 fill_foldcolumn(buf, wp, TRUE, lnum);
1139 p = buf;
1140 for (i = 0; i < fdc; i++)
1141 {
1142 int ch;
1143
1144 if (has_mbyte)
1145 ch = mb_ptr2char_adv(&p);
1146 else
1147 ch = *p++;
1148#ifdef FEAT_RIGHTLEFT
1149 if (wp->w_p_rl)
1150 idx = off + wp->w_width - i - 1 - col;
1151 else
1152#endif
1153 idx = off + col + i;
1154 if (enc_utf8)
1155 {
1156 if (ch >= 0x80)
1157 {
1158 ScreenLinesUC[idx] = ch;
1159 ScreenLinesC[0][idx] = 0;
1160 ScreenLines[idx] = 0x80;
1161 }
1162 else
1163 {
1164 ScreenLines[idx] = ch;
1165 ScreenLinesUC[idx] = 0;
1166 }
1167 }
1168 else
1169 ScreenLines[idx] = ch;
1170 }
1171
1172 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1173 col += fdc;
1174 }
1175
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001176 // Set all attributes of the 'number' or 'relativenumber' column and the
1177 // text
1178 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1179
1180#ifdef FEAT_SIGNS
1181 // If signs are being displayed, add two spaces.
1182 if (signcolumn_on(wp))
1183 {
1184 len = wp->w_width - col;
1185 if (len > 0)
1186 {
1187 if (len > 2)
1188 len = 2;
1189# ifdef FEAT_RIGHTLEFT
1190 if (wp->w_p_rl)
1191 // the line number isn't reversed
1192 copy_text_attr(off + wp->w_width - len - col,
1193 (char_u *)" ", len, HL_ATTR(HLF_FL));
1194 else
1195# endif
1196 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1197 col += len;
1198 }
1199 }
1200#endif
1201
1202 // 3. Add the 'number' or 'relativenumber' column
1203 if (wp->w_p_nu || wp->w_p_rnu)
1204 {
1205 len = wp->w_width - col;
1206 if (len > 0)
1207 {
1208 int w = number_width(wp);
1209 long num;
1210 char *fmt = "%*ld ";
1211
1212 if (len > w + 1)
1213 len = w + 1;
1214
1215 if (wp->w_p_nu && !wp->w_p_rnu)
1216 // 'number' + 'norelativenumber'
1217 num = (long)lnum;
1218 else
1219 {
1220 // 'relativenumber', don't use negative numbers
1221 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1222 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1223 {
1224 // 'number' + 'relativenumber': cursor line shows absolute
1225 // line number
1226 num = lnum;
1227 fmt = "%-*ld ";
1228 }
1229 }
1230
1231 sprintf((char *)buf, fmt, w, num);
1232#ifdef FEAT_RIGHTLEFT
1233 if (wp->w_p_rl)
1234 // the line number isn't reversed
1235 copy_text_attr(off + wp->w_width - len - col, buf, len,
1236 HL_ATTR(HLF_FL));
1237 else
1238#endif
1239 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1240 col += len;
1241 }
1242 }
1243
1244 // 4. Compose the folded-line string with 'foldtext', if set.
1245 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1246
1247 txtcol = col; // remember where text starts
1248
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001249 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1250 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001251 // Right-left text is put in columns 0 - number-col, normal text is put
1252 // in columns number-col - window-width.
1253 col = text_to_screenline(wp, text, col);
1254
1255 // Fill the rest of the line with the fold filler
1256#ifdef FEAT_RIGHTLEFT
1257 if (wp->w_p_rl)
1258 col -= txtcol;
1259#endif
1260 while (col < wp->w_width
1261#ifdef FEAT_RIGHTLEFT
1262 - (wp->w_p_rl ? txtcol : 0)
1263#endif
1264 )
1265 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001266 int c = wp->w_fill_chars.fold;
1267
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001268 if (enc_utf8)
1269 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001270 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001271 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001272 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001273 ScreenLinesC[0][off + col] = 0;
1274 ScreenLines[off + col] = 0x80; // avoid storing zero
1275 }
1276 else
1277 {
1278 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001279 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001280 }
1281 col++;
1282 }
1283 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001284 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001285 }
1286
1287 if (text != buf)
1288 vim_free(text);
1289
1290 // 6. set highlighting for the Visual area an other text.
1291 // If all folded lines are in the Visual area, highlight the line.
1292 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1293 {
1294 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1295 {
1296 // Visual is after curwin->w_cursor
1297 top = &curwin->w_cursor;
1298 bot = &VIsual;
1299 }
1300 else
1301 {
1302 // Visual is before curwin->w_cursor
1303 top = &VIsual;
1304 bot = &curwin->w_cursor;
1305 }
1306 if (lnum >= top->lnum
1307 && lnume <= bot->lnum
1308 && (VIsual_mode != 'v'
1309 || ((lnum > top->lnum
1310 || (lnum == top->lnum
1311 && top->col == 0))
1312 && (lnume < bot->lnum
1313 || (lnume == bot->lnum
1314 && (bot->col - (*p_sel == 'e'))
1315 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
1316 {
1317 if (VIsual_mode == Ctrl_V)
1318 {
1319 // Visual block mode: highlight the chars part of the block
1320 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1321 {
1322 if (wp->w_old_cursor_lcol != MAXCOL
1323 && wp->w_old_cursor_lcol + txtcol
1324 < (colnr_T)wp->w_width)
1325 len = wp->w_old_cursor_lcol;
1326 else
1327 len = wp->w_width - txtcol;
1328 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1329 len - (int)wp->w_old_cursor_fcol);
1330 }
1331 }
1332 else
1333 {
1334 // Set all attributes of the text
1335 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1336 }
1337 }
1338 }
1339
1340#ifdef FEAT_SYN_HL
1341 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1342 if (wp->w_p_cc_cols)
1343 {
1344 int i = 0;
1345 int j = wp->w_p_cc_cols[i];
1346 int old_txtcol = txtcol;
1347
1348 while (j > -1)
1349 {
1350 txtcol += j;
1351 if (wp->w_p_wrap)
1352 txtcol -= wp->w_skipcol;
1353 else
1354 txtcol -= wp->w_leftcol;
1355 if (txtcol >= 0 && txtcol < wp->w_width)
1356 ScreenAttrs[off + txtcol] = hl_combine_attr(
1357 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1358 txtcol = old_txtcol;
1359 j = wp->w_p_cc_cols[++i];
1360 }
1361 }
1362
1363 // Show 'cursorcolumn' in the fold line.
1364 if (wp->w_p_cuc)
1365 {
1366 txtcol += wp->w_virtcol;
1367 if (wp->w_p_wrap)
1368 txtcol -= wp->w_skipcol;
1369 else
1370 txtcol -= wp->w_leftcol;
1371 if (txtcol >= 0 && txtcol < wp->w_width)
1372 ScreenAttrs[off + txtcol] = hl_combine_attr(
1373 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1374 }
1375#endif
1376
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001377 screen_line(wp, row + W_WINROW(wp), wp->w_wincol,
1378 wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001379
1380 // Update w_cline_height and w_cline_folded if the cursor line was
1381 // updated (saves a call to plines() later).
1382 if (wp == curwin
1383 && lnum <= curwin->w_cursor.lnum
1384 && lnume >= curwin->w_cursor.lnum)
1385 {
1386 curwin->w_cline_row = row;
1387 curwin->w_cline_height = 1;
1388 curwin->w_cline_folded = TRUE;
1389 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1390 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001391
1392# ifdef FEAT_CONCEAL
1393 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001394 if (wp == curwin
1395 && wp->w_cursor.lnum >= lnum
1396 && wp->w_cursor.lnum <= lnume
1397 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001398 curs_columns(TRUE);
1399# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001400}
1401#endif
1402
1403/*
1404 * Update a single window.
1405 *
1406 * This may cause the windows below it also to be redrawn (when clearing the
1407 * screen or scrolling lines).
1408 *
1409 * How the window is redrawn depends on wp->w_redr_type. Each type also
1410 * implies the one below it.
1411 * NOT_VALID redraw the whole window
1412 * SOME_VALID redraw the whole window but do scroll when possible
1413 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1414 * INVERTED redraw the changed part of the Visual area
1415 * INVERTED_ALL redraw the whole Visual area
1416 * VALID 1. scroll up/down to adjust for a changed w_topline
1417 * 2. update lines at the top when scrolled down
1418 * 3. redraw changed text:
1419 * - if wp->w_buffer->b_mod_set set, update lines between
1420 * b_mod_top and b_mod_bot.
1421 * - if wp->w_redraw_top non-zero, redraw lines between
1422 * wp->w_redraw_top and wp->w_redr_bot.
1423 * - continue redrawing when syntax status is invalid.
1424 * 4. if scrolled up, update lines at the bottom.
1425 * This results in three areas that may need updating:
1426 * top: from first row to top_end (when scrolled down)
1427 * mid: from mid_start to mid_end (update inversion or changed text)
1428 * bot: from bot_start to last row (when scrolled up)
1429 */
1430 static void
1431win_update(win_T *wp)
1432{
1433 buf_T *buf = wp->w_buffer;
1434 int type;
1435 int top_end = 0; // Below last row of the top area that needs
1436 // updating. 0 when no top area updating.
1437 int mid_start = 999;// first row of the mid area that needs
1438 // updating. 999 when no mid area updating.
1439 int mid_end = 0; // Below last row of the mid area that needs
1440 // updating. 0 when no mid area updating.
1441 int bot_start = 999;// first row of the bot area that needs
1442 // updating. 999 when no bot area updating
1443 int scrolled_down = FALSE; // TRUE when scrolled down when
1444 // w_topline got smaller a bit
1445#ifdef FEAT_SEARCH_EXTRA
1446 int top_to_mod = FALSE; // redraw above mod_top
1447#endif
1448
1449 int row; // current window row to display
1450 linenr_T lnum; // current buffer lnum to display
1451 int idx; // current index in w_lines[]
1452 int srow; // starting row of the current line
1453
1454 int eof = FALSE; // if TRUE, we hit the end of the file
1455 int didline = FALSE; // if TRUE, we finished the last line
1456 int i;
1457 long j;
1458 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001459 linenr_T old_botline = wp->w_botline;
1460#ifdef FEAT_CONCEAL
1461 int old_wrow = wp->w_wrow;
1462 int old_wcol = wp->w_wcol;
1463#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001464#ifdef FEAT_FOLDING
1465 long fold_count;
1466#endif
1467#ifdef FEAT_SYN_HL
1468 // remember what happened to the previous line, to know if
1469 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001470# define DID_NONE 1 // didn't update a line
1471# define DID_LINE 2 // updated a normal line
1472# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001473 int did_update = DID_NONE;
1474 linenr_T syntax_last_parsed = 0; // last parsed text line
1475#endif
1476 linenr_T mod_top = 0;
1477 linenr_T mod_bot = 0;
1478#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1479 int save_got_int;
1480#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001481
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001482#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1483 // This needs to be done only for the first window when update_screen() is
1484 // called.
1485 if (!did_update_one_window)
1486 {
1487 did_update_one_window = TRUE;
1488# ifdef FEAT_SEARCH_EXTRA
1489 start_search_hl();
1490# endif
1491# ifdef FEAT_CLIPBOARD
1492 // When Visual area changed, may have to update selection.
1493 if (clip_star.available && clip_isautosel_star())
1494 clip_update_selection(&clip_star);
1495 if (clip_plus.available && clip_isautosel_plus())
1496 clip_update_selection(&clip_plus);
1497# endif
1498 }
1499#endif
1500
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001501 type = wp->w_redr_type;
1502
1503 if (type == NOT_VALID)
1504 {
1505 wp->w_redr_status = TRUE;
1506 wp->w_lines_valid = 0;
1507 }
1508
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001509 // Window frame is zero-height: nothing to draw.
1510 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1511 || (wp->w_frame->fr_height == wp->w_status_height
1512#if defined(FEAT_PROP_POPUP)
1513 && !popup_is_popup(wp)
1514#endif
1515 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001516 {
1517 wp->w_redr_type = 0;
1518 return;
1519 }
1520
1521 // Window is zero-width: Only need to draw the separator.
1522 if (wp->w_width == 0)
1523 {
1524 // draw the vertical separator right of this window
1525 draw_vsep_win(wp, 0);
1526 wp->w_redr_type = 0;
1527 return;
1528 }
1529
1530#ifdef FEAT_TERMINAL
1531 // If this window contains a terminal, redraw works completely differently.
1532 if (term_do_update_window(wp))
1533 {
1534 term_update_window(wp);
1535# ifdef FEAT_MENU
1536 // Draw the window toolbar, if there is one.
1537 if (winbar_height(wp) > 0)
1538 redraw_win_toolbar(wp);
1539# endif
1540 wp->w_redr_type = 0;
1541 return;
1542 }
1543#endif
1544
1545#ifdef FEAT_SEARCH_EXTRA
1546 init_search_hl(wp, &screen_search_hl);
1547#endif
1548
1549#ifdef FEAT_LINEBREAK
1550 // Force redraw when width of 'number' or 'relativenumber' column
1551 // changes.
1552 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1553 if (wp->w_nrwidth != i)
1554 {
1555 type = NOT_VALID;
1556 wp->w_nrwidth = i;
1557 }
1558 else
1559#endif
1560
1561 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1562 {
1563 // When there are both inserted/deleted lines and specific lines to be
1564 // redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1565 // everything (only happens when redrawing is off for while).
1566 type = NOT_VALID;
1567 }
1568 else
1569 {
1570 // Set mod_top to the first line that needs displaying because of
1571 // changes. Set mod_bot to the first line after the changes.
1572 mod_top = wp->w_redraw_top;
1573 if (wp->w_redraw_bot != 0)
1574 mod_bot = wp->w_redraw_bot + 1;
1575 else
1576 mod_bot = 0;
1577 if (buf->b_mod_set)
1578 {
1579 if (mod_top == 0 || mod_top > buf->b_mod_top)
1580 {
1581 mod_top = buf->b_mod_top;
1582#ifdef FEAT_SYN_HL
1583 // Need to redraw lines above the change that may be included
1584 // in a pattern match.
1585 if (syntax_present(wp))
1586 {
1587 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1588 if (mod_top < 1)
1589 mod_top = 1;
1590 }
1591#endif
1592 }
1593 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1594 mod_bot = buf->b_mod_bot;
1595
1596#ifdef FEAT_SEARCH_EXTRA
1597 // When 'hlsearch' is on and using a multi-line search pattern, a
1598 // change in one line may make the Search highlighting in a
1599 // previous line invalid. Simple solution: redraw all visible
1600 // lines above the change.
1601 // Same for a match pattern.
1602 if (screen_search_hl.rm.regprog != NULL
1603 && re_multiline(screen_search_hl.rm.regprog))
1604 top_to_mod = TRUE;
1605 else
1606 {
1607 matchitem_T *cur = wp->w_match_head;
1608
1609 while (cur != NULL)
1610 {
1611 if (cur->match.regprog != NULL
1612 && re_multiline(cur->match.regprog))
1613 {
1614 top_to_mod = TRUE;
1615 break;
1616 }
1617 cur = cur->next;
1618 }
1619 }
1620#endif
1621 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001622
1623#ifdef FEAT_SEARCH_EXTRA
1624 if (search_hl_has_cursor_lnum > 0)
1625 {
1626 // CurSearch was used last time, need to redraw the line with it to
1627 // avoid having two matches highlighted with CurSearch.
1628 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1629 mod_top = search_hl_has_cursor_lnum;
1630 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1631 mod_bot = search_hl_has_cursor_lnum + 1;
1632 }
1633#endif
1634
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001635#ifdef FEAT_FOLDING
1636 if (mod_top != 0 && hasAnyFolding(wp))
1637 {
1638 linenr_T lnumt, lnumb;
1639
1640 // A change in a line can cause lines above it to become folded or
1641 // unfolded. Find the top most buffer line that may be affected.
1642 // If the line was previously folded and displayed, get the first
1643 // line of that fold. If the line is folded now, get the first
1644 // folded line. Use the minimum of these two.
1645
1646 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1647 // the line below it. If there is no valid entry, use w_topline.
1648 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1649 // to this line. If there is no valid entry, use MAXLNUM.
1650 lnumt = wp->w_topline;
1651 lnumb = MAXLNUM;
1652 for (i = 0; i < wp->w_lines_valid; ++i)
1653 if (wp->w_lines[i].wl_valid)
1654 {
1655 if (wp->w_lines[i].wl_lastlnum < mod_top)
1656 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1657 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1658 {
1659 lnumb = wp->w_lines[i].wl_lnum;
1660 // When there is a fold column it might need updating
1661 // in the next line ("J" just above an open fold).
1662 if (compute_foldcolumn(wp, 0) > 0)
1663 ++lnumb;
1664 }
1665 }
1666
1667 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1668 if (mod_top > lnumt)
1669 mod_top = lnumt;
1670
1671 // Now do the same for the bottom line (one above mod_bot).
1672 --mod_bot;
1673 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1674 ++mod_bot;
1675 if (mod_bot < lnumb)
1676 mod_bot = lnumb;
1677 }
1678#endif
1679
1680 // When a change starts above w_topline and the end is below
1681 // w_topline, start redrawing at w_topline.
1682 // If the end of the change is above w_topline: do like no change was
1683 // made, but redraw the first line to find changes in syntax.
1684 if (mod_top != 0 && mod_top < wp->w_topline)
1685 {
1686 if (mod_bot > wp->w_topline)
1687 mod_top = wp->w_topline;
1688#ifdef FEAT_SYN_HL
1689 else if (syntax_present(wp))
1690 top_end = 1;
1691#endif
1692 }
1693
1694 // When line numbers are displayed need to redraw all lines below
1695 // inserted/deleted lines.
1696 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1697 mod_bot = MAXLNUM;
1698 }
1699 wp->w_redraw_top = 0; // reset for next time
1700 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001701#ifdef FEAT_SEARCH_EXTRA
1702 search_hl_has_cursor_lnum = 0;
1703#endif
1704
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001705
1706 // When only displaying the lines at the top, set top_end. Used when
1707 // window has scrolled down for msg_scrolled.
1708 if (type == REDRAW_TOP)
1709 {
1710 j = 0;
1711 for (i = 0; i < wp->w_lines_valid; ++i)
1712 {
1713 j += wp->w_lines[i].wl_size;
1714 if (j >= wp->w_upd_rows)
1715 {
1716 top_end = j;
1717 break;
1718 }
1719 }
1720 if (top_end == 0)
1721 // not found (cannot happen?): redraw everything
1722 type = NOT_VALID;
1723 else
1724 // top area defined, the rest is VALID
1725 type = VALID;
1726 }
1727
1728 // Trick: we want to avoid clearing the screen twice. screenclear() will
1729 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1730 // non-zero and thus not FALSE) will indicate that screenclear() was not
1731 // called.
1732 if (screen_cleared)
1733 screen_cleared = MAYBE;
1734
1735 // If there are no changes on the screen that require a complete redraw,
1736 // handle three cases:
1737 // 1: we are off the top of the screen by a few lines: scroll down
1738 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1739 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1740 // w_lines[] that needs updating.
1741 if ((type == VALID || type == SOME_VALID
1742 || type == INVERTED || type == INVERTED_ALL)
1743#ifdef FEAT_DIFF
1744 && !wp->w_botfill && !wp->w_old_botfill
1745#endif
1746 )
1747 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001748 if (mod_top != 0
1749 && wp->w_topline == mod_top
1750 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001751 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001752 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001753 // w_topline is the first changed line and window is not scrolled,
1754 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001755 }
1756 else if (wp->w_lines[0].wl_valid
1757 && (wp->w_topline < wp->w_lines[0].wl_lnum
1758#ifdef FEAT_DIFF
1759 || (wp->w_topline == wp->w_lines[0].wl_lnum
1760 && wp->w_topfill > wp->w_old_topfill)
1761#endif
1762 ))
1763 {
1764 // New topline is above old topline: May scroll down.
1765#ifdef FEAT_FOLDING
1766 if (hasAnyFolding(wp))
1767 {
1768 linenr_T ln;
1769
1770 // count the number of lines we are off, counting a sequence
1771 // of folded lines as one
1772 j = 0;
1773 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1774 {
1775 ++j;
1776 if (j >= wp->w_height - 2)
1777 break;
1778 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1779 }
1780 }
1781 else
1782#endif
1783 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1784 if (j < wp->w_height - 2) // not too far off
1785 {
1786 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1787#ifdef FEAT_DIFF
1788 // insert extra lines for previously invisible filler lines
1789 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1790 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1791 - wp->w_old_topfill;
1792#endif
1793 if (i < wp->w_height - 2) // less than a screen off
1794 {
1795 // Try to insert the correct number of lines.
1796 // If not the last window, delete the lines at the bottom.
1797 // win_ins_lines may fail when the terminal can't do it.
1798 if (i > 0)
1799 check_for_delay(FALSE);
1800 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1801 {
1802 if (wp->w_lines_valid != 0)
1803 {
1804 // Need to update rows that are new, stop at the
1805 // first one that scrolled down.
1806 top_end = i;
1807 scrolled_down = TRUE;
1808
1809 // Move the entries that were scrolled, disable
1810 // the entries for the lines to be redrawn.
1811 if ((wp->w_lines_valid += j) > wp->w_height)
1812 wp->w_lines_valid = wp->w_height;
1813 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1814 wp->w_lines[idx] = wp->w_lines[idx - j];
1815 while (idx >= 0)
1816 wp->w_lines[idx--].wl_valid = FALSE;
1817 }
1818 }
1819 else
1820 mid_start = 0; // redraw all lines
1821 }
1822 else
1823 mid_start = 0; // redraw all lines
1824 }
1825 else
1826 mid_start = 0; // redraw all lines
1827 }
1828 else
1829 {
1830 // New topline is at or below old topline: May scroll up.
1831 // When topline didn't change, find first entry in w_lines[] that
1832 // needs updating.
1833
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001834 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1835 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001836 j = -1;
1837 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001838 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001839 {
1840 if (wp->w_lines[i].wl_valid
1841 && wp->w_lines[i].wl_lnum == wp->w_topline)
1842 {
1843 j = i;
1844 break;
1845 }
1846 row += wp->w_lines[i].wl_size;
1847 }
1848 if (j == -1)
1849 {
1850 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1851 // lines
1852 mid_start = 0;
1853 }
1854 else
1855 {
1856 // Try to delete the correct number of lines.
1857 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1858#ifdef FEAT_DIFF
1859 // If the topline didn't change, delete old filler lines,
1860 // otherwise delete filler lines of the new topline...
1861 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1862 row += wp->w_old_topfill;
1863 else
1864 row += diff_check_fill(wp, wp->w_topline);
1865 // ... but don't delete new filler lines.
1866 row -= wp->w_topfill;
1867#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001868 if (row > Rows) // just in case
1869 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001870 if (row > 0)
1871 {
1872 check_for_delay(FALSE);
1873 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1874 == OK)
1875 bot_start = wp->w_height - row;
1876 else
1877 mid_start = 0; // redraw all lines
1878 }
1879 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1880 {
1881 // Skip the lines (below the deleted lines) that are still
1882 // valid and don't need redrawing. Copy their info
1883 // upwards, to compensate for the deleted lines. Set
1884 // bot_start to the first row that needs redrawing.
1885 bot_start = 0;
1886 idx = 0;
1887 for (;;)
1888 {
1889 wp->w_lines[idx] = wp->w_lines[j];
1890 // stop at line that didn't fit, unless it is still
1891 // valid (no lines deleted)
1892 if (row > 0 && bot_start + row
1893 + (int)wp->w_lines[j].wl_size > wp->w_height)
1894 {
1895 wp->w_lines_valid = idx + 1;
1896 break;
1897 }
1898 bot_start += wp->w_lines[idx++].wl_size;
1899
1900 // stop at the last valid entry in w_lines[].wl_size
1901 if (++j >= wp->w_lines_valid)
1902 {
1903 wp->w_lines_valid = idx;
1904 break;
1905 }
1906 }
1907#ifdef FEAT_DIFF
1908 // Correct the first entry for filler lines at the top
1909 // when it won't get updated below.
1910 if (wp->w_p_diff && bot_start > 0)
1911 wp->w_lines[0].wl_size =
1912 plines_win_nofill(wp, wp->w_topline, TRUE)
1913 + wp->w_topfill;
1914#endif
1915 }
1916 }
1917 }
1918
1919 // When starting redraw in the first line, redraw all lines. When
1920 // there is only one window it's probably faster to clear the screen
1921 // first.
1922 if (mid_start == 0)
1923 {
1924 mid_end = wp->w_height;
1925 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
1926 {
1927 // Clear the screen when it was not done by win_del_lines() or
1928 // win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1929 // then.
1930 if (screen_cleared != TRUE)
1931 screenclear();
1932 // The screen was cleared, redraw the tab pages line.
1933 if (redraw_tabline)
1934 draw_tabline();
1935 }
1936 }
1937
1938 // When win_del_lines() or win_ins_lines() caused the screen to be
1939 // cleared (only happens for the first window) or when screenclear()
1940 // was called directly above, "must_redraw" will have been set to
1941 // NOT_VALID, need to reset it here to avoid redrawing twice.
1942 if (screen_cleared == TRUE)
1943 must_redraw = 0;
1944 }
1945 else
1946 {
1947 // Not VALID or INVERTED: redraw all lines.
1948 mid_start = 0;
1949 mid_end = wp->w_height;
1950 }
1951
1952 if (type == SOME_VALID)
1953 {
1954 // SOME_VALID: redraw all lines.
1955 mid_start = 0;
1956 mid_end = wp->w_height;
1957 type = NOT_VALID;
1958 }
1959
1960 // check if we are updating or removing the inverted part
1961 if ((VIsual_active && buf == curwin->w_buffer)
1962 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1963 {
1964 linenr_T from, to;
1965
1966 if (VIsual_active)
1967 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001968 if (VIsual_mode != wp->w_old_visual_mode
1969 || type == INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001970 {
1971 // If the type of Visual selection changed, redraw the whole
1972 // selection. Also when the ownership of the X selection is
1973 // gained or lost.
1974 if (curwin->w_cursor.lnum < VIsual.lnum)
1975 {
1976 from = curwin->w_cursor.lnum;
1977 to = VIsual.lnum;
1978 }
1979 else
1980 {
1981 from = VIsual.lnum;
1982 to = curwin->w_cursor.lnum;
1983 }
1984 // redraw more when the cursor moved as well
1985 if (wp->w_old_cursor_lnum < from)
1986 from = wp->w_old_cursor_lnum;
1987 if (wp->w_old_cursor_lnum > to)
1988 to = wp->w_old_cursor_lnum;
1989 if (wp->w_old_visual_lnum < from)
1990 from = wp->w_old_visual_lnum;
1991 if (wp->w_old_visual_lnum > to)
1992 to = wp->w_old_visual_lnum;
1993 }
1994 else
1995 {
1996 // Find the line numbers that need to be updated: The lines
1997 // between the old cursor position and the current cursor
1998 // position. Also check if the Visual position changed.
1999 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
2000 {
2001 from = curwin->w_cursor.lnum;
2002 to = wp->w_old_cursor_lnum;
2003 }
2004 else
2005 {
2006 from = wp->w_old_cursor_lnum;
2007 to = curwin->w_cursor.lnum;
2008 if (from == 0) // Visual mode just started
2009 from = to;
2010 }
2011
2012 if (VIsual.lnum != wp->w_old_visual_lnum
2013 || VIsual.col != wp->w_old_visual_col)
2014 {
2015 if (wp->w_old_visual_lnum < from
2016 && wp->w_old_visual_lnum != 0)
2017 from = wp->w_old_visual_lnum;
2018 if (wp->w_old_visual_lnum > to)
2019 to = wp->w_old_visual_lnum;
2020 if (VIsual.lnum < from)
2021 from = VIsual.lnum;
2022 if (VIsual.lnum > to)
2023 to = VIsual.lnum;
2024 }
2025 }
2026
2027 // If in block mode and changed column or curwin->w_curswant:
2028 // update all lines.
2029 // First compute the actual start and end column.
2030 if (VIsual_mode == Ctrl_V)
2031 {
2032 colnr_T fromc, toc;
2033#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002034 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002035
2036 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002037 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002038#endif
2039 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002040 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002041#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002042 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002043#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002044 // Highlight to the end of the line, unless 'virtualedit' has
2045 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002046 if (curwin->w_curswant == MAXCOL)
2047 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002048 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002049 {
2050 pos_T pos;
2051 int cursor_above =
2052 curwin->w_cursor.lnum < VIsual.lnum;
2053
2054 // Need to find the longest line.
2055 toc = 0;
2056 pos.coladd = 0;
2057 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2058 ? pos.lnum <= VIsual.lnum
2059 : pos.lnum >= VIsual.lnum;
2060 pos.lnum += cursor_above ? 1 : -1)
2061 {
2062 colnr_T t;
2063
Bram Moolenaar6bcb1822021-07-09 15:54:00 +02002064 pos.col = (int)STRLEN(ml_get_buf(wp->w_buffer,
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002065 pos.lnum, FALSE));
2066 getvvcol(wp, &pos, NULL, NULL, &t);
2067 if (toc < t)
2068 toc = t;
2069 }
2070 ++toc;
2071 }
2072 else
2073 toc = MAXCOL;
2074 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002075
2076 if (fromc != wp->w_old_cursor_fcol
2077 || toc != wp->w_old_cursor_lcol)
2078 {
2079 if (from > VIsual.lnum)
2080 from = VIsual.lnum;
2081 if (to < VIsual.lnum)
2082 to = VIsual.lnum;
2083 }
2084 wp->w_old_cursor_fcol = fromc;
2085 wp->w_old_cursor_lcol = toc;
2086 }
2087 }
2088 else
2089 {
2090 // Use the line numbers of the old Visual area.
2091 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2092 {
2093 from = wp->w_old_cursor_lnum;
2094 to = wp->w_old_visual_lnum;
2095 }
2096 else
2097 {
2098 from = wp->w_old_visual_lnum;
2099 to = wp->w_old_cursor_lnum;
2100 }
2101 }
2102
2103 // There is no need to update lines above the top of the window.
2104 if (from < wp->w_topline)
2105 from = wp->w_topline;
2106
2107 // If we know the value of w_botline, use it to restrict the update to
2108 // the lines that are visible in the window.
2109 if (wp->w_valid & VALID_BOTLINE)
2110 {
2111 if (from >= wp->w_botline)
2112 from = wp->w_botline - 1;
2113 if (to >= wp->w_botline)
2114 to = wp->w_botline - 1;
2115 }
2116
2117 // Find the minimal part to be updated.
2118 // Watch out for scrolling that made entries in w_lines[] invalid.
2119 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2120 // top_end; need to redraw from top_end to the "to" line.
2121 // A middle mouse click with a Visual selection may change the text
2122 // above the Visual area and reset wl_valid, do count these for
2123 // mid_end (in srow).
2124 if (mid_start > 0)
2125 {
2126 lnum = wp->w_topline;
2127 idx = 0;
2128 srow = 0;
2129 if (scrolled_down)
2130 mid_start = top_end;
2131 else
2132 mid_start = 0;
2133 while (lnum < from && idx < wp->w_lines_valid) // find start
2134 {
2135 if (wp->w_lines[idx].wl_valid)
2136 mid_start += wp->w_lines[idx].wl_size;
2137 else if (!scrolled_down)
2138 srow += wp->w_lines[idx].wl_size;
2139 ++idx;
2140# ifdef FEAT_FOLDING
2141 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2142 lnum = wp->w_lines[idx].wl_lnum;
2143 else
2144# endif
2145 ++lnum;
2146 }
2147 srow += mid_start;
2148 mid_end = wp->w_height;
2149 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2150 {
2151 if (wp->w_lines[idx].wl_valid
2152 && wp->w_lines[idx].wl_lnum >= to + 1)
2153 {
2154 // Only update until first row of this line
2155 mid_end = srow;
2156 break;
2157 }
2158 srow += wp->w_lines[idx].wl_size;
2159 }
2160 }
2161 }
2162
2163 if (VIsual_active && buf == curwin->w_buffer)
2164 {
2165 wp->w_old_visual_mode = VIsual_mode;
2166 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2167 wp->w_old_visual_lnum = VIsual.lnum;
2168 wp->w_old_visual_col = VIsual.col;
2169 wp->w_old_curswant = curwin->w_curswant;
2170 }
2171 else
2172 {
2173 wp->w_old_visual_mode = 0;
2174 wp->w_old_cursor_lnum = 0;
2175 wp->w_old_visual_lnum = 0;
2176 wp->w_old_visual_col = 0;
2177 }
2178
2179#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2180 // reset got_int, otherwise regexp won't work
2181 save_got_int = got_int;
2182 got_int = 0;
2183#endif
2184#ifdef SYN_TIME_LIMIT
2185 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002186 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002187 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002188#endif
2189#ifdef FEAT_FOLDING
2190 win_foldinfo.fi_level = 0;
2191#endif
2192
2193#ifdef FEAT_MENU
2194 // Draw the window toolbar, if there is one.
2195 // TODO: only when needed.
2196 if (winbar_height(wp) > 0)
2197 redraw_win_toolbar(wp);
2198#endif
2199
2200 // Update all the window rows.
2201 idx = 0; // first entry in w_lines[].wl_size
2202 row = 0;
2203 srow = 0;
2204 lnum = wp->w_topline; // first line shown in window
2205 for (;;)
2206 {
2207 // stop updating when reached the end of the window (check for _past_
2208 // the end of the window is at the end of the loop)
2209 if (row == wp->w_height)
2210 {
2211 didline = TRUE;
2212 break;
2213 }
2214
2215 // stop updating when hit the end of the file
2216 if (lnum > buf->b_ml.ml_line_count)
2217 {
2218 eof = TRUE;
2219 break;
2220 }
2221
2222 // Remember the starting row of the line that is going to be dealt
2223 // with. It is used further down when the line doesn't fit.
2224 srow = row;
2225
2226 // Update a line when it is in an area that needs updating, when it
2227 // has changes or w_lines[idx] is invalid.
2228 // "bot_start" may be halfway a wrapped line after using
2229 // win_del_lines(), check if the current line includes it.
2230 // When syntax folding is being used, the saved syntax states will
2231 // already have been updated, we can't see where the syntax state is
2232 // the same again, just update until the end of the window.
2233 if (row < top_end
2234 || (row >= mid_start && row < mid_end)
2235#ifdef FEAT_SEARCH_EXTRA
2236 || top_to_mod
2237#endif
2238 || idx >= wp->w_lines_valid
2239 || (row + wp->w_lines[idx].wl_size > bot_start)
2240 || (mod_top != 0
2241 && (lnum == mod_top
2242 || (lnum >= mod_top
2243 && (lnum < mod_bot
2244#ifdef FEAT_SYN_HL
2245 || did_update == DID_FOLD
2246 || (did_update == DID_LINE
2247 && syntax_present(wp)
2248 && (
2249# ifdef FEAT_FOLDING
2250 (foldmethodIsSyntax(wp)
2251 && hasAnyFolding(wp)) ||
2252# endif
2253 syntax_check_changed(lnum)))
2254#endif
2255#ifdef FEAT_SEARCH_EXTRA
2256 // match in fixed position might need redraw
2257 // if lines were inserted or deleted
2258 || (wp->w_match_head != NULL
2259 && buf->b_mod_xlines != 0)
2260#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002261 ))))
2262#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002263 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2264 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002265#endif
2266 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002267 {
2268#ifdef FEAT_SEARCH_EXTRA
2269 if (lnum == mod_top)
2270 top_to_mod = FALSE;
2271#endif
2272
2273 // When at start of changed lines: May scroll following lines
2274 // up or down to minimize redrawing.
2275 // Don't do this when the change continues until the end.
2276 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002277 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002278 if (lnum == mod_top
2279 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002280 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2281 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002282 {
2283 int old_rows = 0;
2284 int new_rows = 0;
2285 int xtra_rows;
2286 linenr_T l;
2287
2288 // Count the old number of window rows, using w_lines[], which
2289 // should still contain the sizes for the lines as they are
2290 // currently displayed.
2291 for (i = idx; i < wp->w_lines_valid; ++i)
2292 {
2293 // Only valid lines have a meaningful wl_lnum. Invalid
2294 // lines are part of the changed area.
2295 if (wp->w_lines[i].wl_valid
2296 && wp->w_lines[i].wl_lnum == mod_bot)
2297 break;
2298 old_rows += wp->w_lines[i].wl_size;
2299#ifdef FEAT_FOLDING
2300 if (wp->w_lines[i].wl_valid
2301 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2302 {
2303 // Must have found the last valid entry above mod_bot.
2304 // Add following invalid entries.
2305 ++i;
2306 while (i < wp->w_lines_valid
2307 && !wp->w_lines[i].wl_valid)
2308 old_rows += wp->w_lines[i++].wl_size;
2309 break;
2310 }
2311#endif
2312 }
2313
2314 if (i >= wp->w_lines_valid)
2315 {
2316 // We can't find a valid line below the changed lines,
2317 // need to redraw until the end of the window.
2318 // Inserting/deleting lines has no use.
2319 bot_start = 0;
2320 }
2321 else
2322 {
2323 // Able to count old number of rows: Count new window
2324 // rows, and may insert/delete lines
2325 j = idx;
2326 for (l = lnum; l < mod_bot; ++l)
2327 {
2328#ifdef FEAT_FOLDING
2329 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2330 ++new_rows;
2331 else
2332#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002333 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002334#ifdef FEAT_DIFF
2335 if (l == wp->w_topline)
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002336 new_rows += plines_win_nofill(wp, l, TRUE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002337 + wp->w_topfill;
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002338 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002339#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002340 new_rows += plines_win(wp, l, TRUE);
2341 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002342 ++j;
2343 if (new_rows > wp->w_height - row - 2)
2344 {
2345 // it's getting too much, must redraw the rest
2346 new_rows = 9999;
2347 break;
2348 }
2349 }
2350 xtra_rows = new_rows - old_rows;
2351 if (xtra_rows < 0)
2352 {
2353 // May scroll text up. If there is not enough
2354 // remaining text or scrolling fails, must redraw the
2355 // rest. If scrolling works, must redraw the text
2356 // below the scrolled text.
2357 if (row - xtra_rows >= wp->w_height - 2)
2358 mod_bot = MAXLNUM;
2359 else
2360 {
2361 check_for_delay(FALSE);
2362 if (win_del_lines(wp, row,
2363 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2364 mod_bot = MAXLNUM;
2365 else
2366 bot_start = wp->w_height + xtra_rows;
2367 }
2368 }
2369 else if (xtra_rows > 0)
2370 {
2371 // May scroll text down. If there is not enough
2372 // remaining text of scrolling fails, must redraw the
2373 // rest.
2374 if (row + xtra_rows >= wp->w_height - 2)
2375 mod_bot = MAXLNUM;
2376 else
2377 {
2378 check_for_delay(FALSE);
2379 if (win_ins_lines(wp, row + old_rows,
2380 xtra_rows, FALSE, FALSE) == FAIL)
2381 mod_bot = MAXLNUM;
2382 else if (top_end > row + old_rows)
2383 // Scrolled the part at the top that requires
2384 // updating down.
2385 top_end += xtra_rows;
2386 }
2387 }
2388
2389 // When not updating the rest, may need to move w_lines[]
2390 // entries.
2391 if (mod_bot != MAXLNUM && i != j)
2392 {
2393 if (j < i)
2394 {
2395 int x = row + new_rows;
2396
2397 // move entries in w_lines[] upwards
2398 for (;;)
2399 {
2400 // stop at last valid entry in w_lines[]
2401 if (i >= wp->w_lines_valid)
2402 {
2403 wp->w_lines_valid = j;
2404 break;
2405 }
2406 wp->w_lines[j] = wp->w_lines[i];
2407 // stop at a line that won't fit
2408 if (x + (int)wp->w_lines[j].wl_size
2409 > wp->w_height)
2410 {
2411 wp->w_lines_valid = j + 1;
2412 break;
2413 }
2414 x += wp->w_lines[j++].wl_size;
2415 ++i;
2416 }
2417 if (bot_start > x)
2418 bot_start = x;
2419 }
2420 else // j > i
2421 {
2422 // move entries in w_lines[] downwards
2423 j -= i;
2424 wp->w_lines_valid += j;
2425 if (wp->w_lines_valid > wp->w_height)
2426 wp->w_lines_valid = wp->w_height;
2427 for (i = wp->w_lines_valid; i - j >= idx; --i)
2428 wp->w_lines[i] = wp->w_lines[i - j];
2429
2430 // The w_lines[] entries for inserted lines are
2431 // now invalid, but wl_size may be used above.
2432 // Reset to zero.
2433 while (i >= idx)
2434 {
2435 wp->w_lines[i].wl_size = 0;
2436 wp->w_lines[i--].wl_valid = FALSE;
2437 }
2438 }
2439 }
2440 }
2441 }
2442
2443#ifdef FEAT_FOLDING
2444 // When lines are folded, display one line for all of them.
2445 // Otherwise, display normally (can be several display lines when
2446 // 'wrap' is on).
2447 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2448 if (fold_count != 0)
2449 {
2450 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2451 ++row;
2452 --fold_count;
2453 wp->w_lines[idx].wl_folded = TRUE;
2454 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2455# ifdef FEAT_SYN_HL
2456 did_update = DID_FOLD;
2457# endif
2458 }
2459 else
2460#endif
2461 if (idx < wp->w_lines_valid
2462 && wp->w_lines[idx].wl_valid
2463 && wp->w_lines[idx].wl_lnum == lnum
2464 && lnum > wp->w_topline
2465 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2466 && !WIN_IS_POPUP(wp)
2467 && srow + wp->w_lines[idx].wl_size > wp->w_height
2468#ifdef FEAT_DIFF
2469 && diff_check_fill(wp, lnum) == 0
2470#endif
2471 )
2472 {
2473 // This line is not going to fit. Don't draw anything here,
2474 // will draw "@ " lines below.
2475 row = wp->w_height + 1;
2476 }
2477 else
2478 {
2479#ifdef FEAT_SEARCH_EXTRA
2480 prepare_search_hl(wp, &screen_search_hl, lnum);
2481#endif
2482#ifdef FEAT_SYN_HL
2483 // Let the syntax stuff know we skipped a few lines.
2484 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2485 && syntax_present(wp))
2486 syntax_end_parsing(syntax_last_parsed + 1);
2487#endif
2488
2489 // Display one line.
2490 row = win_line(wp, lnum, srow, wp->w_height,
2491 mod_top == 0, FALSE);
2492
2493#ifdef FEAT_FOLDING
2494 wp->w_lines[idx].wl_folded = FALSE;
2495 wp->w_lines[idx].wl_lastlnum = lnum;
2496#endif
2497#ifdef FEAT_SYN_HL
2498 did_update = DID_LINE;
2499 syntax_last_parsed = lnum;
2500#endif
2501 }
2502
2503 wp->w_lines[idx].wl_lnum = lnum;
2504 wp->w_lines[idx].wl_valid = TRUE;
2505
2506 // Past end of the window or end of the screen. Note that after
2507 // resizing wp->w_height may be end up too big. That's a problem
2508 // elsewhere, but prevent a crash here.
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002509 if (row > wp->w_height
2510 || row + wp->w_winrow >= (p_ch > 0 ? Rows : Rows + 1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002511 {
2512 // we may need the size of that too long line later on
2513 if (dollar_vcol == -1)
2514 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2515 ++idx;
2516 break;
2517 }
2518 if (dollar_vcol == -1)
2519 wp->w_lines[idx].wl_size = row - srow;
2520 ++idx;
2521#ifdef FEAT_FOLDING
2522 lnum += fold_count + 1;
2523#else
2524 ++lnum;
2525#endif
2526 }
2527 else
2528 {
Lewis Russell16246392022-03-29 11:38:17 +01002529 if (wp->w_p_rnu && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002530 {
2531#ifdef FEAT_FOLDING
Lewis Russell16246392022-03-29 11:38:17 +01002532 // 'relativenumber' set and the cursor moved vertically: The
2533 // text doesn't need to be drawn, but the number column does.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002534 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2535 if (fold_count != 0)
2536 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2537 else
2538#endif
2539 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
2540 }
2541
2542 // This line does not need to be drawn, advance to the next one.
2543 row += wp->w_lines[idx++].wl_size;
2544 if (row > wp->w_height) // past end of screen
2545 break;
2546#ifdef FEAT_FOLDING
2547 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2548#else
2549 ++lnum;
2550#endif
2551#ifdef FEAT_SYN_HL
2552 did_update = DID_NONE;
2553#endif
2554 }
2555
2556 if (lnum > buf->b_ml.ml_line_count)
2557 {
2558 eof = TRUE;
2559 break;
2560 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002561
2562 // Safety check: if any of the wl_size values is wrong we might go over
2563 // the end of w_lines[].
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002564 if (idx >= (p_ch > 0 ? Rows : Rows + 1))
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002565 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002566 }
2567
2568 // End of loop over all window lines.
2569
zeertzjqc20e46a2022-03-23 14:55:23 +00002570#ifdef FEAT_SYN_HL
2571 // Now that the window has been redrawn with the old and new cursor line,
2572 // update w_last_cursorline.
2573 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2574#endif
Lewis Russell16246392022-03-29 11:38:17 +01002575 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002576
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002577#ifdef FEAT_VTP
2578 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002579 // See the version that was fixed.
2580 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002581 {
K.Takata54119102022-02-03 13:33:03 +00002582 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002583
K.Takata54119102022-02-03 13:33:03 +00002584 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002585 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002586 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2587 LineOffset[k] + screen_Columns) > 1)
2588 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002589 else
K.Takata54119102022-02-03 13:33:03 +00002590 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002591 else
K.Takata54119102022-02-03 13:33:03 +00002592 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002593 }
2594#endif
2595
2596 if (idx > wp->w_lines_valid)
2597 wp->w_lines_valid = idx;
2598
2599#ifdef FEAT_SYN_HL
2600 // Let the syntax stuff know we stop parsing here.
2601 if (syntax_last_parsed != 0 && syntax_present(wp))
2602 syntax_end_parsing(syntax_last_parsed + 1);
2603#endif
2604
2605 // If we didn't hit the end of the file, and we didn't finish the last
2606 // line we were working on, then the line didn't fit.
2607 wp->w_empty_rows = 0;
2608#ifdef FEAT_DIFF
2609 wp->w_filler_rows = 0;
2610#endif
2611 if (!eof && !didline)
2612 {
2613 if (lnum == wp->w_topline)
2614 {
2615 // Single line that does not fit!
2616 // Don't overwrite it, it can be edited.
2617 wp->w_botline = lnum + 1;
2618 }
2619#ifdef FEAT_DIFF
2620 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2621 {
2622 // Window ends in filler lines.
2623 wp->w_botline = lnum;
2624 wp->w_filler_rows = wp->w_height - srow;
2625 }
2626#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002627#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002628 else if (WIN_IS_POPUP(wp))
2629 {
2630 // popup line that doesn't fit is left as-is
2631 wp->w_botline = lnum;
2632 }
2633#endif
2634 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2635 {
2636 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2637
2638 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002639 screen_puts_len((char_u *)"@@", wp->w_width > 2 ? 2 : wp->w_width,
2640 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002641 screen_fill(scr_row, scr_row + 1,
2642 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
2643 '@', ' ', HL_ATTR(HLF_AT));
2644 set_empty_rows(wp, srow);
2645 wp->w_botline = lnum;
2646 }
2647 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2648 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002649 int start_col = (int)W_ENDCOL(wp) - 3;
2650
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002651 // Last line isn't finished: Display "@@@" at the end.
2652 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2653 W_WINROW(wp) + wp->w_height,
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002654 start_col < wp->w_wincol ? wp->w_wincol : start_col,
2655 (int)W_ENDCOL(wp),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002656 '@', '@', HL_ATTR(HLF_AT));
2657 set_empty_rows(wp, srow);
2658 wp->w_botline = lnum;
2659 }
2660 else
2661 {
2662 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
2663 wp->w_botline = lnum;
2664 }
2665 }
2666 else
2667 {
2668 draw_vsep_win(wp, row);
2669 if (eof) // we hit the end of the file
2670 {
2671 wp->w_botline = buf->b_ml.ml_line_count + 1;
2672#ifdef FEAT_DIFF
2673 j = diff_check_fill(wp, wp->w_botline);
2674 if (j > 0 && !wp->w_botfill)
2675 {
2676 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002677 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002678 i = '-';
2679 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002680 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002681 if (row + j > wp->w_height)
2682 j = wp->w_height - row;
2683 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2684 row += j;
2685 }
2686#endif
2687 }
2688 else if (dollar_vcol == -1)
2689 wp->w_botline = lnum;
2690
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002691 // Make sure the rest of the screen is blank.
2692 // write the "eob" character from 'fillchars' to rows that aren't part
2693 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002694 if (WIN_IS_POPUP(wp))
2695 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2696 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002697 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2698 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002699 }
2700
2701#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002702 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002703 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002704#endif
2705
2706 // Reset the type of redrawing required, the window has been updated.
2707 wp->w_redr_type = 0;
2708#ifdef FEAT_DIFF
2709 wp->w_old_topfill = wp->w_topfill;
2710 wp->w_old_botfill = wp->w_botfill;
2711#endif
2712
2713 if (dollar_vcol == -1)
2714 {
2715 // There is a trick with w_botline. If we invalidate it on each
2716 // change that might modify it, this will cause a lot of expensive
2717 // calls to plines() in update_topline() each time. Therefore the
2718 // value of w_botline is often approximated, and this value is used to
2719 // compute the value of w_topline. If the value of w_botline was
2720 // wrong, check that the value of w_topline is correct (cursor is on
2721 // the visible part of the text). If it's not, we need to redraw
2722 // again. Mostly this just means scrolling up a few lines, so it
2723 // doesn't look too bad. Only do this for the current window (where
2724 // changes are relevant).
2725 wp->w_valid |= VALID_BOTLINE;
2726 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2727 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002728 win_T *wwp;
2729#if defined(FEAT_CONCEAL)
2730 linenr_T old_topline = wp->w_topline;
2731 int new_wcol = wp->w_wcol;
2732#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002733 recursive = TRUE;
2734 curwin->w_valid &= ~VALID_TOPLINE;
2735 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002736
2737#if defined(FEAT_CONCEAL)
2738 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2739 != (VALID_WCOL|VALID_WROW))
2740 {
2741 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002742 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002743 // update_topline() the cursor's row or column got invalidated.
2744 // If they are left invalid, setcursor() will recompute them
2745 // but there won't be any further win_line() call to re-fix the
2746 // column and the cursor will end up misplaced. So we call
2747 // cursor validation now and reapply the fix again (or call
2748 // win_line() to do it for us).
2749 validate_cursor();
2750 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2751 && old_topline == wp->w_topline)
2752 wp->w_wcol = new_wcol;
2753 else
2754 redrawWinline(wp, wp->w_cursor.lnum);
2755 }
2756#endif
2757 // New redraw either due to updated topline or due to wcol fix.
2758 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002759 {
2760 // Don't update for changes in buffer again.
2761 i = curbuf->b_mod_set;
2762 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002763 j = curbuf->b_mod_xlines;
2764 curbuf->b_mod_xlines = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002765 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002766 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002767 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002768 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002769 // Other windows might have w_redr_type raised in update_topline().
2770 must_redraw = 0;
2771 FOR_ALL_WINDOWS(wwp)
2772 if (wwp->w_redr_type > must_redraw)
2773 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002774 recursive = FALSE;
2775 }
2776 }
2777
2778#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2779 // restore got_int, unless CTRL-C was hit while redrawing
2780 if (!got_int)
2781 got_int = save_got_int;
2782#endif
2783}
2784
2785#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2786/*
2787 * Prepare for updating one or more windows.
2788 * Caller must check for "updating_screen" already set to avoid recursiveness.
2789 */
2790 static void
2791update_prepare(void)
2792{
2793 cursor_off();
2794 updating_screen = TRUE;
2795#ifdef FEAT_GUI
2796 // Remove the cursor before starting to do anything, because scrolling may
2797 // make it difficult to redraw the text under it.
2798 if (gui.in_use)
2799 gui_undraw_cursor();
2800#endif
2801#ifdef FEAT_SEARCH_EXTRA
2802 start_search_hl();
2803#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002804#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002805 // Update popup_mask if needed.
2806 may_update_popup_mask(must_redraw);
2807#endif
2808}
2809
2810/*
2811 * Finish updating one or more windows.
2812 */
2813 static void
2814update_finish(void)
2815{
2816 if (redraw_cmdline || redraw_mode)
2817 showmode();
2818
2819# ifdef FEAT_SEARCH_EXTRA
2820 end_search_hl();
2821# endif
2822
2823 after_updating_screen(TRUE);
2824
2825# ifdef FEAT_GUI
2826 // Redraw the cursor and update the scrollbars when all screen updating is
2827 // done.
2828 if (gui.in_use)
2829 {
2830 out_flush_cursor(FALSE, FALSE);
2831 gui_update_scrollbars(FALSE);
2832 }
2833# endif
2834}
2835#endif
2836
2837#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2838 void
2839update_debug_sign(buf_T *buf, linenr_T lnum)
2840{
2841 win_T *wp;
2842 int doit = FALSE;
2843
2844# ifdef FEAT_FOLDING
2845 win_foldinfo.fi_level = 0;
2846# endif
2847
2848 // update/delete a specific sign
2849 redraw_buf_line_later(buf, lnum);
2850
2851 // check if it resulted in the need to redraw a window
2852 FOR_ALL_WINDOWS(wp)
2853 if (wp->w_redr_type != 0)
2854 doit = TRUE;
2855
2856 // Return when there is nothing to do, screen updating is already
2857 // happening (recursive call), messages on the screen or still starting up.
2858 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002859 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002860 || msg_scrolled
2861#ifdef FEAT_GUI
2862 || gui.starting
2863#endif
2864 || starting)
2865 return;
2866
2867 // update all windows that need updating
2868 update_prepare();
2869
2870 FOR_ALL_WINDOWS(wp)
2871 {
2872 if (wp->w_redr_type != 0)
2873 win_update(wp);
2874 if (wp->w_redr_status)
2875 win_redr_status(wp, FALSE);
2876 }
2877
2878 update_finish();
2879}
2880#endif
2881
2882#if defined(FEAT_GUI) || defined(PROTO)
2883/*
2884 * Update a single window, its status line and maybe the command line msg.
2885 * Used for the GUI scrollbar.
2886 */
2887 void
2888updateWindow(win_T *wp)
2889{
2890 // return if already busy updating
2891 if (updating_screen)
2892 return;
2893
2894 update_prepare();
2895
2896#ifdef FEAT_CLIPBOARD
2897 // When Visual area changed, may have to update selection.
2898 if (clip_star.available && clip_isautosel_star())
2899 clip_update_selection(&clip_star);
2900 if (clip_plus.available && clip_isautosel_plus())
2901 clip_update_selection(&clip_plus);
2902#endif
2903
2904 win_update(wp);
2905
2906 // When the screen was cleared redraw the tab pages line.
2907 if (redraw_tabline)
2908 draw_tabline();
2909
2910 if (wp->w_redr_status
2911# ifdef FEAT_CMDL_INFO
2912 || p_ru
2913# endif
2914# ifdef FEAT_STL_OPT
2915 || *p_stl != NUL || *wp->w_p_stl != NUL
2916# endif
2917 )
2918 win_redr_status(wp, FALSE);
2919
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002920#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002921 // Display popup windows on top of everything.
2922 update_popups(win_update);
2923#endif
2924
2925 update_finish();
2926}
2927#endif
2928
2929#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2930/*
2931 * Redraw as soon as possible. When the command line is not scrolled redraw
2932 * right away and restore what was on the command line.
2933 * Return a code indicating what happened.
2934 */
2935 int
2936redraw_asap(int type)
2937{
2938 int rows;
2939 int cols = screen_Columns;
2940 int r;
2941 int ret = 0;
2942 schar_T *screenline; // copy from ScreenLines[]
2943 sattr_T *screenattr; // copy from ScreenAttrs[]
2944 int i;
2945 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2946 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2947 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2948
2949 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002950 if (msg_scrolled
2951 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
2952 || exiting
2953 || p_ch == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002954 return ret;
2955
2956 // Allocate space to save the text displayed in the command line area.
2957 rows = screen_Rows - cmdline_row;
2958 screenline = LALLOC_MULT(schar_T, rows * cols);
2959 screenattr = LALLOC_MULT(sattr_T, rows * cols);
2960 if (screenline == NULL || screenattr == NULL)
2961 ret = 2;
2962 if (enc_utf8)
2963 {
2964 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
2965 if (screenlineUC == NULL)
2966 ret = 2;
2967 for (i = 0; i < p_mco; ++i)
2968 {
2969 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
2970 if (screenlineC[i] == NULL)
2971 ret = 2;
2972 }
2973 }
2974 if (enc_dbcs == DBCS_JPNU)
2975 {
2976 screenline2 = LALLOC_MULT(schar_T, rows * cols);
2977 if (screenline2 == NULL)
2978 ret = 2;
2979 }
2980
2981 if (ret != 2)
2982 {
2983 // Save the text displayed in the command line area.
2984 for (r = 0; r < rows; ++r)
2985 {
2986 mch_memmove(screenline + r * cols,
2987 ScreenLines + LineOffset[cmdline_row + r],
2988 (size_t)cols * sizeof(schar_T));
2989 mch_memmove(screenattr + r * cols,
2990 ScreenAttrs + LineOffset[cmdline_row + r],
2991 (size_t)cols * sizeof(sattr_T));
2992 if (enc_utf8)
2993 {
2994 mch_memmove(screenlineUC + r * cols,
2995 ScreenLinesUC + LineOffset[cmdline_row + r],
2996 (size_t)cols * sizeof(u8char_T));
2997 for (i = 0; i < p_mco; ++i)
2998 mch_memmove(screenlineC[i] + r * cols,
2999 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3000 (size_t)cols * sizeof(u8char_T));
3001 }
3002 if (enc_dbcs == DBCS_JPNU)
3003 mch_memmove(screenline2 + r * cols,
3004 ScreenLines2 + LineOffset[cmdline_row + r],
3005 (size_t)cols * sizeof(schar_T));
3006 }
3007
3008 update_screen(0);
3009 ret = 3;
3010
3011 if (must_redraw == 0)
3012 {
3013 int off = (int)(current_ScreenLine - ScreenLines);
3014
3015 // Restore the text displayed in the command line area.
3016 for (r = 0; r < rows; ++r)
3017 {
3018 mch_memmove(current_ScreenLine,
3019 screenline + r * cols,
3020 (size_t)cols * sizeof(schar_T));
3021 mch_memmove(ScreenAttrs + off,
3022 screenattr + r * cols,
3023 (size_t)cols * sizeof(sattr_T));
3024 if (enc_utf8)
3025 {
3026 mch_memmove(ScreenLinesUC + off,
3027 screenlineUC + r * cols,
3028 (size_t)cols * sizeof(u8char_T));
3029 for (i = 0; i < p_mco; ++i)
3030 mch_memmove(ScreenLinesC[i] + off,
3031 screenlineC[i] + r * cols,
3032 (size_t)cols * sizeof(u8char_T));
3033 }
3034 if (enc_dbcs == DBCS_JPNU)
3035 mch_memmove(ScreenLines2 + off,
3036 screenline2 + r * cols,
3037 (size_t)cols * sizeof(schar_T));
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003038 screen_line(curwin, cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003039 }
3040 ret = 4;
3041 }
3042 }
3043
3044 vim_free(screenline);
3045 vim_free(screenattr);
3046 if (enc_utf8)
3047 {
3048 vim_free(screenlineUC);
3049 for (i = 0; i < p_mco; ++i)
3050 vim_free(screenlineC[i]);
3051 }
3052 if (enc_dbcs == DBCS_JPNU)
3053 vim_free(screenline2);
3054
3055 // Show the intro message when appropriate.
3056 maybe_intro_message();
3057
3058 setcursor();
3059
3060 return ret;
3061}
3062#endif
3063
3064/*
3065 * Invoked after an asynchronous callback is called.
3066 * If an echo command was used the cursor needs to be put back where
3067 * it belongs. If highlighting was changed a redraw is needed.
3068 * If "call_update_screen" is FALSE don't call update_screen() when at the
3069 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003070 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003071 */
3072 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003073redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003074{
3075 ++redrawing_for_callback;
3076
Bram Moolenaar24959102022-05-07 20:01:16 +01003077 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3078 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3079 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003080 {
3081 if (do_message)
3082 repeat_message();
3083 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003084 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003085 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003086#ifdef FEAT_WILDMENU
3087 if (pum_visible())
3088 cmdline_pum_display();
3089#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003090 // Don't redraw when in prompt_for_number().
3091 if (cmdline_row > 0)
3092 {
3093 // Redrawing only works when the screen didn't scroll. Don't clear
3094 // wildmenu entries.
3095 if (msg_scrolled == 0
3096#ifdef FEAT_WILDMENU
3097 && wild_menu_showing == 0
3098#endif
3099 && call_update_screen)
3100 update_screen(0);
3101
3102 // Redraw in the same position, so that the user can continue
3103 // editing the command.
3104 redrawcmdline_ex(FALSE);
3105 }
3106 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003107 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003108 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003109 update_topline();
3110 validate_cursor();
3111
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003112 // keep the command line if possible
3113 update_screen(VALID_NO_UPDATE);
3114 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003115
3116 if (msg_scrolled == 0)
3117 {
3118 // don't want a hit-enter prompt when something else is displayed
3119 msg_didany = FALSE;
3120 need_wait_return = FALSE;
3121 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003122 }
3123 cursor_on();
3124#ifdef FEAT_GUI
3125 if (gui.in_use && !gui_mch_is_blink_off())
3126 // Don't update the cursor when it is blinking and off to avoid
3127 // flicker.
3128 out_flush_cursor(FALSE, FALSE);
3129 else
3130#endif
3131 out_flush();
3132
3133 --redrawing_for_callback;
3134}
3135
3136/*
3137 * Redraw the current window later, with update_screen(type).
3138 * Set must_redraw only if not already set to a higher value.
3139 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
3140 */
3141 void
3142redraw_later(int type)
3143{
3144 redraw_win_later(curwin, type);
3145}
3146
3147 void
3148redraw_win_later(
3149 win_T *wp,
3150 int type)
3151{
3152 if (!exiting && wp->w_redr_type < type)
3153 {
3154 wp->w_redr_type = type;
3155 if (type >= NOT_VALID)
3156 wp->w_lines_valid = 0;
3157 if (must_redraw < type) // must_redraw is the maximum of all windows
3158 must_redraw = type;
3159 }
3160}
3161
3162/*
3163 * Force a complete redraw later. Also resets the highlighting. To be used
3164 * after executing a shell command that messes up the screen.
3165 */
3166 void
3167redraw_later_clear(void)
3168{
3169 redraw_all_later(CLEAR);
3170 reset_screen_attr();
3171}
3172
3173/*
3174 * Mark all windows to be redrawn later.
3175 */
3176 void
3177redraw_all_later(int type)
3178{
3179 win_T *wp;
3180
3181 FOR_ALL_WINDOWS(wp)
3182 redraw_win_later(wp, type);
3183 // This may be needed when switching tabs.
3184 if (must_redraw < type)
3185 must_redraw = type;
3186}
3187
3188/*
3189 * Mark all windows that are editing the current buffer to be updated later.
3190 */
3191 void
3192redraw_curbuf_later(int type)
3193{
3194 redraw_buf_later(curbuf, type);
3195}
3196
3197 void
3198redraw_buf_later(buf_T *buf, int type)
3199{
3200 win_T *wp;
3201
3202 FOR_ALL_WINDOWS(wp)
3203 {
3204 if (wp->w_buffer == buf)
3205 redraw_win_later(wp, type);
3206 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003207#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3208 // terminal in popup window is not in list of windows
3209 if (curwin->w_buffer == buf)
3210 redraw_win_later(curwin, type);
3211#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003212}
3213
3214#if defined(FEAT_SIGNS) || defined(PROTO)
3215 void
3216redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3217{
3218 win_T *wp;
3219
3220 FOR_ALL_WINDOWS(wp)
3221 if (wp->w_buffer == buf && lnum >= wp->w_topline
3222 && lnum < wp->w_botline)
3223 redrawWinline(wp, lnum);
3224}
3225#endif
3226
3227#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3228 void
3229redraw_buf_and_status_later(buf_T *buf, int type)
3230{
3231 win_T *wp;
3232
3233#ifdef FEAT_WILDMENU
3234 if (wild_menu_showing != 0)
3235 // Don't redraw while the command line completion is displayed, it
3236 // would disappear.
3237 return;
3238#endif
3239 FOR_ALL_WINDOWS(wp)
3240 {
3241 if (wp->w_buffer == buf)
3242 {
3243 redraw_win_later(wp, type);
3244 wp->w_redr_status = TRUE;
3245 }
3246 }
3247}
3248#endif
3249
3250/*
3251 * mark all status lines for redraw; used after first :cd
3252 */
3253 void
3254status_redraw_all(void)
3255{
3256 win_T *wp;
3257
3258 FOR_ALL_WINDOWS(wp)
3259 if (wp->w_status_height)
3260 {
3261 wp->w_redr_status = TRUE;
3262 redraw_later(VALID);
3263 }
3264}
3265
3266/*
3267 * mark all status lines of the current buffer for redraw
3268 */
3269 void
3270status_redraw_curbuf(void)
3271{
3272 win_T *wp;
3273
3274 FOR_ALL_WINDOWS(wp)
3275 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3276 {
3277 wp->w_redr_status = TRUE;
3278 redraw_later(VALID);
3279 }
3280}
3281
3282/*
3283 * Redraw all status lines that need to be redrawn.
3284 */
3285 void
3286redraw_statuslines(void)
3287{
3288 win_T *wp;
3289
3290 FOR_ALL_WINDOWS(wp)
3291 if (wp->w_redr_status)
3292 win_redr_status(wp, FALSE);
3293 if (redraw_tabline)
3294 draw_tabline();
3295}
3296
3297#if defined(FEAT_WILDMENU) || defined(PROTO)
3298/*
3299 * Redraw all status lines at the bottom of frame "frp".
3300 */
3301 void
3302win_redraw_last_status(frame_T *frp)
3303{
3304 if (frp->fr_layout == FR_LEAF)
3305 frp->fr_win->w_redr_status = TRUE;
3306 else if (frp->fr_layout == FR_ROW)
3307 {
3308 FOR_ALL_FRAMES(frp, frp->fr_child)
3309 win_redraw_last_status(frp);
3310 }
3311 else // frp->fr_layout == FR_COL
3312 {
3313 frp = frp->fr_child;
3314 while (frp->fr_next != NULL)
3315 frp = frp->fr_next;
3316 win_redraw_last_status(frp);
3317 }
3318}
3319#endif
3320
3321/*
3322 * Changed something in the current window, at buffer line "lnum", that
3323 * requires that line and possibly other lines to be redrawn.
3324 * Used when entering/leaving Insert mode with the cursor on a folded line.
3325 * Used to remove the "$" from a change command.
3326 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3327 * may become invalid and the whole window will have to be redrawn.
3328 */
3329 void
3330redrawWinline(
3331 win_T *wp,
3332 linenr_T lnum)
3333{
3334 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
3335 wp->w_redraw_top = lnum;
3336 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
3337 wp->w_redraw_bot = lnum;
3338 redraw_win_later(wp, VALID);
3339}