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