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