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