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