blob: 77828089c2442f176935e1a2c05bdd032a76ac16 [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
Bram Moolenaara4d158b2022-08-14 14:17:45 +010031 * call redraw_later(UPD_VALID) to have the window displayed by update_screen()
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020032 * 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
Bram Moolenaara4d158b2022-08-14 14:17:45 +010043 * settings), must call redraw_later(UPD_NOT_VALID) to have the whole window
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020044 * redisplayed by update_screen() later.
45 *
46 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
Bram Moolenaara4d158b2022-08-14 14:17:45 +010047 * must call redraw_curbuf_later(UPD_NOT_VALID) to have all the windows for the
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020048 * buffer redisplayed by update_screen() later.
49 *
50 * Commands that change highlighting and possibly cause a scroll too must call
Bram Moolenaara4d158b2022-08-14 14:17:45 +010051 * redraw_later(UPD_SOME_VALID) to update the whole window but still use
52 * scrolling to avoid redrawing everything. But the length of displayed lines
53 * must not change, use UPD_NOT_VALID then.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020054 *
Bram Moolenaara4d158b2022-08-14 14:17:45 +010055 * Commands that move the window position must call redraw_later(UPD_NOT_VALID).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020056 * TODO: should minimize redrawing by scrolling when possible.
57 *
58 * Commands that change everything (e.g., resizing the screen) must call
Bram Moolenaara4d158b2022-08-14 14:17:45 +010059 * redraw_all_later(UPD_NOT_VALID) or redraw_all_later(UPD_CLEAR).
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020060 *
61 * Things that are handled indirectly:
62 * - When messages scroll the screen up, msg_scrolled will be set and
63 * update_screen() called to redraw.
64 */
65
66#include "vim.h"
67
68static void win_update(win_T *wp);
69#ifdef FEAT_STL_OPT
70static void redraw_custom_statusline(win_T *wp);
71#endif
Bram Moolenaare52e0c82020-02-28 22:20:10 +010072#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
73static int did_update_one_window;
74#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020075
Bram Moolenaarbdff0122020-04-05 18:56:05 +020076static void win_redr_status(win_T *wp, int ignore_pum);
77
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020078/*
79 * Based on the current value of curwin->w_topline, transfer a screenfull
80 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
81 * Return OK when the screen was updated, FAIL if it was not done.
82 */
83 int
84update_screen(int type_arg)
85{
86 int type = type_arg;
87 win_T *wp;
88 static int did_intro = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020089#ifdef FEAT_GUI
Bram Moolenaare52e0c82020-02-28 22:20:10 +010090 int did_one = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020091 int did_undraw = FALSE;
92 int gui_cursor_col = 0;
93 int gui_cursor_row = 0;
94#endif
95 int no_update = FALSE;
Bram Moolenaare0c03c82021-04-23 21:01:34 +020096 int save_pum_will_redraw = pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +020097
98 // Don't do anything if the screen structures are (not yet) valid.
99 if (!screen_valid(TRUE))
100 return FAIL;
101
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100102 if (type == UPD_VALID_NO_UPDATE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200103 {
104 no_update = TRUE;
105 type = 0;
106 }
107
108#ifdef FEAT_EVAL
109 {
110 buf_T *buf;
111
112 // Before updating the screen, notify any listeners of changed text.
113 FOR_ALL_BUFFERS(buf)
114 invoke_listeners(buf);
115 }
116#endif
117
118#ifdef FEAT_DIFF
119 // May have postponed updating diffs.
120 if (need_diff_redraw)
121 diff_redraw(TRUE);
122#endif
123
124 if (must_redraw)
125 {
126 if (type < must_redraw) // use maximal type
127 type = must_redraw;
128
129 // must_redraw is reset here, so that when we run into some weird
130 // reason to redraw while busy redrawing (e.g., asynchronous
131 // scrolling), or update_topline() in win_update() will cause a
132 // scroll, the screen will be redrawn later or in win_update().
133 must_redraw = 0;
134 }
135
136 // May need to update w_lines[].
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100137 if (curwin->w_lines_valid == 0 && type < UPD_NOT_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200138#ifdef FEAT_TERMINAL
139 && !term_do_update_window(curwin)
140#endif
141 )
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100142 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200143
144 // Postpone the redrawing when it's not needed and when being called
145 // recursively.
146 if (!redrawing() || updating_screen)
147 {
148 redraw_later(type); // remember type for next time
149 must_redraw = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100150 if (type > UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200151 curwin->w_lines_valid = 0; // don't use w_lines[].wl_size now
152 return FAIL;
153 }
154 updating_screen = TRUE;
155
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100156#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200157 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
158 // in some windows.
159 may_update_popup_mask(type);
160#endif
161
162#ifdef FEAT_SYN_HL
163 ++display_tick; // let syntax code know we're in a next round of
164 // display updating
165#endif
166 if (no_update)
167 ++no_win_do_lines_ins;
168
169 // if the screen was scrolled up when displaying a message, scroll it down
170 if (msg_scrolled)
171 {
172 clear_cmdline = TRUE;
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100173 if (type != UPD_CLEAR)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200174 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100175 if (msg_scrolled > Rows - 5) // redrawing is faster
Bram Moolenaarb13d3402022-08-29 13:44:28 +0100176 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100177 type = UPD_NOT_VALID;
Bram Moolenaarb13d3402022-08-29 13:44:28 +0100178 redraw_as_cleared();
179 }
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100180 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200181 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100182 check_for_delay(FALSE);
183 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
184 == FAIL)
Bram Moolenaarb13d3402022-08-29 13:44:28 +0100185 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100186 type = UPD_NOT_VALID;
Bram Moolenaarb13d3402022-08-29 13:44:28 +0100187 redraw_as_cleared();
188 }
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100189 FOR_ALL_WINDOWS(wp)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200190 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100191 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200192 {
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100193 if (W_WINROW(wp) + wp->w_height > msg_scrolled
194 && wp->w_redr_type < UPD_REDRAW_TOP
195 && wp->w_lines_valid > 0
196 && wp->w_topline == wp->w_lines[0].wl_lnum)
197 {
198 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
199 wp->w_redr_type = UPD_REDRAW_TOP;
200 }
201 else
202 {
203 wp->w_redr_type = UPD_NOT_VALID;
204 if (W_WINROW(wp) + wp->w_height
205 + wp->w_status_height <= msg_scrolled)
206 wp->w_redr_status = TRUE;
207 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200208 }
209 }
Bram Moolenaarf73e5ba2022-08-29 12:41:06 +0100210 if (!no_update)
211 redraw_cmdline = TRUE;
212 redraw_tabline = TRUE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200213 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200214 }
215 msg_scrolled = 0;
216 need_wait_return = FALSE;
217 }
218
219 // reset cmdline_row now (may have been changed temporarily)
220 compute_cmdrow();
221
222 // Check for changed highlighting
223 if (need_highlight_changed)
224 highlight_changed();
225
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100226 if (type == UPD_CLEAR) // first clear screen
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200227 {
228 screenclear(); // will reset clear_cmdline
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100229 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200230 // must_redraw may be set indirectly, avoid another redraw later
231 must_redraw = 0;
232 }
233
234 if (clear_cmdline) // going to clear cmdline (done below)
235 check_for_delay(FALSE);
236
237#ifdef FEAT_LINEBREAK
238 // Force redraw when width of 'number' or 'relativenumber' column
239 // changes.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100240 if (curwin->w_redr_type < UPD_NOT_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200241 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
242 ? number_width(curwin) : 0))
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100243 curwin->w_redr_type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200244#endif
245
246 // Only start redrawing if there is really something to do.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100247 if (type == UPD_INVERTED)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200248 update_curswant();
249 if (curwin->w_redr_type < type
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100250 && !((type == UPD_VALID
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200251 && curwin->w_lines[0].wl_valid
252#ifdef FEAT_DIFF
253 && curwin->w_topfill == curwin->w_old_topfill
254 && curwin->w_botfill == curwin->w_old_botfill
255#endif
256 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100257 || (type == UPD_INVERTED
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200258 && VIsual_active
259 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
260 && curwin->w_old_visual_mode == VIsual_mode
261 && (curwin->w_valid & VALID_VIRTCOL)
262 && curwin->w_old_curswant == curwin->w_curswant)
263 ))
264 curwin->w_redr_type = type;
265
266 // Redraw the tab pages line if needed.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100267 if (redraw_tabline || type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200268 draw_tabline();
269
270#ifdef FEAT_SYN_HL
271 // Correct stored syntax highlighting info for changes in each displayed
272 // buffer. Each buffer must only be done once.
273 FOR_ALL_WINDOWS(wp)
274 {
275 if (wp->w_buffer->b_mod_set)
276 {
277 win_T *wwp;
278
279 // Check if we already did this buffer.
280 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
281 if (wwp->w_buffer == wp->w_buffer)
282 break;
283 if (wwp == wp && syntax_present(wp))
284 syn_stack_apply_changes(wp->w_buffer);
285 }
286 }
287#endif
288
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200289 if (pum_redraw_in_same_position())
290 // Avoid flicker if the popup menu is going to be redrawn in the same
291 // position.
292 pum_will_redraw = TRUE;
293
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200294 // Go from top to bottom through the windows, redrawing the ones that need
295 // it.
296#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100297 did_update_one_window = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200298#endif
299#ifdef FEAT_SEARCH_EXTRA
300 screen_search_hl.rm.regprog = NULL;
301#endif
302 FOR_ALL_WINDOWS(wp)
303 {
304 if (wp->w_redr_type != 0)
305 {
306 cursor_off();
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100307#ifdef FEAT_GUI
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200308 if (!did_one)
309 {
310 did_one = TRUE;
Bram Moolenaare52e0c82020-02-28 22:20:10 +0100311
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200312 // Remove the cursor before starting to do anything, because
313 // scrolling may make it difficult to redraw the text under
314 // it.
Bram Moolenaar09f067f2021-04-11 13:29:18 +0200315 // Also remove the cursor if it needs to be hidden due to an
316 // ongoing cursor-less sleep.
317 if (gui.in_use && (wp == curwin || cursor_is_sleeping()))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200318 {
319 gui_cursor_col = gui.cursor_col;
320 gui_cursor_row = gui.cursor_row;
321 gui_undraw_cursor();
322 did_undraw = TRUE;
323 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200324 }
325#endif
326 win_update(wp);
327 }
328
329 // redraw status line after the window to minimize cursor movement
330 if (wp->w_redr_status)
331 {
332 cursor_off();
333 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
334 }
335 }
336#if defined(FEAT_SEARCH_EXTRA)
337 end_search_hl();
338#endif
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200339
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200340 // May need to redraw the popup menu.
Bram Moolenaare0c03c82021-04-23 21:01:34 +0200341 pum_will_redraw = save_pum_will_redraw;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200342 pum_may_redraw();
343
344 // Reset b_mod_set flags. Going through all windows is probably faster
345 // than going through all buffers (there could be many buffers).
346 FOR_ALL_WINDOWS(wp)
347 wp->w_buffer->b_mod_set = FALSE;
348
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100349#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200350 // Display popup windows on top of the windows and command line.
351 update_popups(win_update);
352#endif
353
Bram Moolenaar3194e5b2021-12-13 21:59:09 +0000354#ifdef FEAT_TERMINAL
355 FOR_ALL_WINDOWS(wp)
356 // If this window contains a terminal, after redrawing all windows, the
357 // dirty row range can be reset.
358 term_did_update_window(wp);
359#endif
360
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200361 after_updating_screen(TRUE);
362
363 // Clear or redraw the command line. Done last, because scrolling may
364 // mess up the command line.
365 if (clear_cmdline || redraw_cmdline || redraw_mode)
366 showmode();
367
368 if (no_update)
369 --no_win_do_lines_ins;
370
371 // May put up an introductory message when not editing a file
372 if (!did_intro)
373 maybe_intro_message();
374 did_intro = TRUE;
375
376#ifdef FEAT_GUI
377 // Redraw the cursor and update the scrollbars when all screen updating is
378 // done.
379 if (gui.in_use)
380 {
381 if (did_undraw && !gui_mch_is_blink_off())
382 {
383 mch_disable_flush();
384 out_flush(); // required before updating the cursor
385 mch_enable_flush();
386
387 // Put the GUI position where the cursor was, gui_update_cursor()
388 // uses that.
389 gui.col = gui_cursor_col;
390 gui.row = gui_cursor_row;
391 gui.col = mb_fix_col(gui.col, gui.row);
392 gui_update_cursor(FALSE, FALSE);
393 gui_may_flush();
394 screen_cur_col = gui.col;
395 screen_cur_row = gui.row;
396 }
397 else
398 out_flush();
399 gui_update_scrollbars(FALSE);
400 }
401#endif
402 return OK;
403}
404
405/*
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200406 * Return the row for drawing the statusline and the ruler of window "wp".
407 */
Bram Moolenaar49c51b82021-04-01 16:16:18 +0200408 int
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200409statusline_row(win_T *wp)
410{
411#if defined(FEAT_PROP_POPUP)
412 // If the window is really zero height the winbar isn't displayed.
413 if (wp->w_frame->fr_height == wp->w_status_height && !popup_is_popup(wp))
414 return wp->w_winrow;
415#endif
416 return W_WINROW(wp) + wp->w_height;
417}
418
419/*
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200420 * Redraw the status line of window wp.
421 *
422 * If inversion is possible we use it. Else '=' characters are used.
423 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
424 * displayed.
425 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200426 static void
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200427win_redr_status(win_T *wp, int ignore_pum UNUSED)
428{
429 int row;
430 char_u *p;
431 int len;
432 int fillchar;
433 int attr;
434 int this_ru_col;
435 static int busy = FALSE;
436
437 // It's possible to get here recursively when 'statusline' (indirectly)
438 // invokes ":redrawstatus". Simply ignore the call then.
439 if (busy)
440 return;
441 busy = TRUE;
442
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200443 row = statusline_row(wp);
444
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200445 wp->w_redr_status = FALSE;
446 if (wp->w_status_height == 0)
447 {
448 // no status line, can only be last window
449 redraw_cmdline = TRUE;
450 }
451 else if (!redrawing()
452 // don't update status line when popup menu is visible and may be
453 // drawn over it, unless it will be redrawn later
454 || (!ignore_pum && pum_visible()))
455 {
456 // Don't redraw right now, do it later.
457 wp->w_redr_status = TRUE;
458 }
459#ifdef FEAT_STL_OPT
460 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
461 {
462 // redraw custom status line
463 redraw_custom_statusline(wp);
464 }
465#endif
466 else
467 {
468 fillchar = fillchar_status(&attr, wp);
469
470 get_trans_bufname(wp->w_buffer);
471 p = NameBuff;
472 len = (int)STRLEN(p);
473
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000474 if ((bt_help(wp->w_buffer)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200475#ifdef FEAT_QUICKFIX
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000476 || wp->w_p_pvw
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200477#endif
Bram Moolenaarde05bb22022-01-13 13:08:14 +0000478 || bufIsChanged(wp->w_buffer)
479 || wp->w_buffer->b_p_ro)
480 && len < MAXPATHL - 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200481 *(p + len++) = ' ';
482 if (bt_help(wp->w_buffer))
483 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100484 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Help]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200485 len += (int)STRLEN(p + len);
486 }
487#ifdef FEAT_QUICKFIX
488 if (wp->w_p_pvw)
489 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100490 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[Preview]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200491 len += (int)STRLEN(p + len);
492 }
493#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100494 if (bufIsChanged(wp->w_buffer) && !bt_terminal(wp->w_buffer))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200495 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100496 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", "[+]");
497 len += (int)STRLEN(p + len);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200498 }
499 if (wp->w_buffer->b_p_ro)
500 {
Bram Moolenaar826bfe42021-10-08 18:39:28 +0100501 vim_snprintf((char *)p + len, MAXPATHL - len, "%s", _("[RO]"));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200502 len += (int)STRLEN(p + len);
503 }
504
505 this_ru_col = ru_col - (Columns - wp->w_width);
506 if (this_ru_col < (wp->w_width + 1) / 2)
507 this_ru_col = (wp->w_width + 1) / 2;
508 if (this_ru_col <= 1)
509 {
510 p = (char_u *)"<"; // No room for file name!
511 len = 1;
512 }
513 else if (has_mbyte)
514 {
515 int clen = 0, i;
516
517 // Count total number of display cells.
518 clen = mb_string2cells(p, -1);
519
520 // Find first character that will fit.
521 // Going from start to end is much faster for DBCS.
522 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
523 i += (*mb_ptr2len)(p + i))
524 clen -= (*mb_ptr2cells)(p + i);
525 len = clen;
526 if (i > 0)
527 {
528 p = p + i - 1;
529 *p = '<';
530 ++len;
531 }
532
533 }
534 else if (len > this_ru_col - 1)
535 {
536 p += len - (this_ru_col - 1);
537 *p = '<';
538 len = this_ru_col - 1;
539 }
540
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200541 screen_puts(p, row, wp->w_wincol, attr);
542 screen_fill(row, row + 1, len + wp->w_wincol,
543 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
544
545 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000546 && (this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200547 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
548 - 1 + wp->w_wincol), attr);
549
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200550 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200551 }
552
553 /*
554 * May need to draw the character below the vertical separator.
555 */
556 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
557 {
558 if (stl_connected(wp))
559 fillchar = fillchar_status(&attr, wp);
560 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100561 fillchar = fillchar_vsep(&attr, wp);
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200562 screen_putchar(fillchar, row, W_ENDCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200563 }
564 busy = FALSE;
565}
566
567#ifdef FEAT_STL_OPT
568/*
569 * Redraw the status line according to 'statusline' and take care of any
570 * errors encountered.
571 */
572 static void
573redraw_custom_statusline(win_T *wp)
574{
575 static int entered = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200576
577 // When called recursively return. This can happen when the statusline
578 // contains an expression that triggers a redraw.
579 if (entered)
580 return;
581 entered = TRUE;
582
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200583 win_redr_custom(wp, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200584 entered = FALSE;
585}
586#endif
587
588/*
589 * Show current status info in ruler and various other places
590 * If always is FALSE, only show ruler if position has changed.
591 */
592 void
593showruler(int always)
594{
595 if (!always && !redrawing())
596 return;
597 if (pum_visible())
598 {
599 // Don't redraw right now, do it later.
600 curwin->w_redr_status = TRUE;
601 return;
602 }
603#if defined(FEAT_STL_OPT)
604 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
605 redraw_custom_statusline(curwin);
606 else
607#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200608 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200609
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200610 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000611#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200612 || (p_icon && (stl_syntax & STL_IN_ICON))
613 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000614#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200615 )
616 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000617
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200618 // Redraw the tab pages line if needed.
619 if (redraw_tabline)
620 draw_tabline();
621}
622
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200623 void
624win_redr_ruler(win_T *wp, int always, int ignore_pum)
625{
626#define RULER_BUF_LEN 70
627 char_u buffer[RULER_BUF_LEN];
628 int row;
629 int fillchar;
630 int attr;
631 int empty_line = FALSE;
632 colnr_T virtcol;
633 int i;
634 size_t len;
635 int o;
636 int this_ru_col;
637 int off = 0;
638 int width;
639
Bram Moolenaara2a89732022-08-31 14:46:18 +0100640 // If 'ruler' off don't do anything
641 if (!p_ru)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200642 return;
643
644 /*
645 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
646 * after deleting lines, before cursor.lnum is corrected.
647 */
648 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
649 return;
650
651 // Don't draw the ruler while doing insert-completion, it might overwrite
652 // the (long) mode message.
653 if (wp == lastwin && lastwin->w_status_height == 0)
654 if (edit_submode != NULL)
655 return;
656 // Don't draw the ruler when the popup menu is visible, it may overlap.
657 // Except when the popup menu will be redrawn anyway.
658 if (!ignore_pum && pum_visible())
659 return;
660
661#ifdef FEAT_STL_OPT
Bram Moolenaara2a89732022-08-31 14:46:18 +0100662 if (*p_ruf)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200663 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200664 win_redr_custom(wp, TRUE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200665 return;
666 }
667#endif
668
669 /*
670 * Check if not in Insert mode and the line is empty (will show "0-1").
671 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100672 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200673 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
674 empty_line = TRUE;
675
676 /*
677 * Only draw the ruler when something changed.
678 */
679 validate_virtcol_win(wp);
680 if ( redraw_cmdline
681 || always
682 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
683 || wp->w_cursor.col != wp->w_ru_cursor.col
684 || wp->w_virtcol != wp->w_ru_virtcol
685 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
686 || wp->w_topline != wp->w_ru_topline
687 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
688#ifdef FEAT_DIFF
689 || wp->w_topfill != wp->w_ru_topfill
690#endif
691 || empty_line != wp->w_ru_empty)
692 {
693 cursor_off();
694 if (wp->w_status_height)
695 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200696 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200697 fillchar = fillchar_status(&attr, wp);
698 off = wp->w_wincol;
699 width = wp->w_width;
700 }
701 else
702 {
703 row = Rows - 1;
704 fillchar = ' ';
705 attr = 0;
706 width = Columns;
707 off = 0;
708 }
709
710 // In list mode virtcol needs to be recomputed
711 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100712 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200713 {
714 wp->w_p_list = FALSE;
715 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
716 wp->w_p_list = TRUE;
717 }
718
719 /*
720 * Some sprintfs return the length, some return a pointer.
721 * To avoid portability problems we use strlen() here.
722 */
723 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
724 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
725 ? 0L
726 : (long)(wp->w_cursor.lnum));
727 len = STRLEN(buffer);
728 col_print(buffer + len, RULER_BUF_LEN - len,
729 empty_line ? 0 : (int)wp->w_cursor.col + 1,
730 (int)virtcol + 1);
731
732 /*
733 * Add a "50%" if there is room for it.
734 * On the last line, don't print in the last column (scrolls the
735 * screen up on some terminals).
736 */
737 i = (int)STRLEN(buffer);
738 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
739 o = i + vim_strsize(buffer + i + 1);
740 if (wp->w_status_height == 0) // can't use last char of screen
741 ++o;
742 this_ru_col = ru_col - (Columns - width);
743 if (this_ru_col < 0)
744 this_ru_col = 0;
745 // Never use more than half the window/screen width, leave the other
746 // half for the filename.
747 if (this_ru_col < (width + 1) / 2)
748 this_ru_col = (width + 1) / 2;
749 if (this_ru_col + o < width)
750 {
751 // need at least 3 chars left for get_rel_pos() + NUL
752 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
753 {
754 if (has_mbyte)
755 i += (*mb_char2bytes)(fillchar, buffer + i);
756 else
757 buffer[i++] = fillchar;
758 ++o;
759 }
760 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
761 }
762 // Truncate at window boundary.
763 if (has_mbyte)
764 {
765 o = 0;
766 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
767 {
768 o += (*mb_ptr2cells)(buffer + i);
769 if (this_ru_col + o > width)
770 {
771 buffer[i] = NUL;
772 break;
773 }
774 }
775 }
776 else if (this_ru_col + (int)STRLEN(buffer) > width)
777 buffer[width - this_ru_col] = NUL;
778
779 screen_puts(buffer, row, this_ru_col + off, attr);
780 i = redraw_cmdline;
781 screen_fill(row, row + 1,
782 this_ru_col + off + (int)STRLEN(buffer),
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000783 (off + width),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200784 fillchar, fillchar, attr);
785 // don't redraw the cmdline because of showing the ruler
786 redraw_cmdline = i;
787 wp->w_ru_cursor = wp->w_cursor;
788 wp->w_ru_virtcol = wp->w_virtcol;
789 wp->w_ru_empty = empty_line;
790 wp->w_ru_topline = wp->w_topline;
791 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
792#ifdef FEAT_DIFF
793 wp->w_ru_topfill = wp->w_topfill;
794#endif
795 }
796}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200797
798/*
799 * To be called when "updating_screen" was set before and now the postponed
800 * side effects may take place.
801 */
802 void
803after_updating_screen(int may_resize_shell UNUSED)
804{
805 updating_screen = FALSE;
806#ifdef FEAT_GUI
807 if (may_resize_shell)
808 gui_may_resize_shell();
809#endif
810#ifdef FEAT_TERMINAL
811 term_check_channel_closed_recently();
812#endif
813
814#ifdef HAVE_DROP_FILE
815 // If handle_drop() was called while updating_screen was TRUE need to
816 // handle the drop now.
817 handle_any_postponed_drop();
818#endif
819}
820
821/*
822 * Update all windows that are editing the current buffer.
823 */
824 void
825update_curbuf(int type)
826{
827 redraw_curbuf_later(type);
828 update_screen(type);
829}
830
831#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
832/*
833 * Copy "text" to ScreenLines using "attr".
834 * Returns the next screen column.
835 */
836 static int
837text_to_screenline(win_T *wp, char_u *text, int col)
838{
839 int off = (int)(current_ScreenLine - ScreenLines);
840
841 if (has_mbyte)
842 {
843 int cells;
844 int u8c, u8cc[MAX_MCO];
845 int i;
846 int idx;
847 int c_len;
848 char_u *p;
849# ifdef FEAT_ARABIC
850 int prev_c = 0; // previous Arabic character
851 int prev_c1 = 0; // first composing char for prev_c
852# endif
853
854# ifdef FEAT_RIGHTLEFT
855 if (wp->w_p_rl)
856 idx = off;
857 else
858# endif
859 idx = off + col;
860
861 // Store multibyte characters in ScreenLines[] et al. correctly.
862 for (p = text; *p != NUL; )
863 {
864 cells = (*mb_ptr2cells)(p);
865 c_len = (*mb_ptr2len)(p);
866 if (col + cells > wp->w_width
867# ifdef FEAT_RIGHTLEFT
868 - (wp->w_p_rl ? col : 0)
869# endif
870 )
871 break;
872 ScreenLines[idx] = *p;
873 if (enc_utf8)
874 {
875 u8c = utfc_ptr2char(p, u8cc);
876 if (*p < 0x80 && u8cc[0] == 0)
877 {
878 ScreenLinesUC[idx] = 0;
879#ifdef FEAT_ARABIC
880 prev_c = u8c;
881#endif
882 }
883 else
884 {
885#ifdef FEAT_ARABIC
886 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
887 {
888 // Do Arabic shaping.
889 int pc, pc1, nc;
890 int pcc[MAX_MCO];
891 int firstbyte = *p;
892
893 // The idea of what is the previous and next
894 // character depends on 'rightleft'.
895 if (wp->w_p_rl)
896 {
897 pc = prev_c;
898 pc1 = prev_c1;
899 nc = utf_ptr2char(p + c_len);
900 prev_c1 = u8cc[0];
901 }
902 else
903 {
904 pc = utfc_ptr2char(p + c_len, pcc);
905 nc = prev_c;
906 pc1 = pcc[0];
907 }
908 prev_c = u8c;
909
910 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
911 pc, pc1, nc);
912 ScreenLines[idx] = firstbyte;
913 }
914 else
915 prev_c = u8c;
916#endif
917 // Non-BMP character: display as ? or fullwidth ?.
918 ScreenLinesUC[idx] = u8c;
919 for (i = 0; i < Screen_mco; ++i)
920 {
921 ScreenLinesC[i][idx] = u8cc[i];
922 if (u8cc[i] == 0)
923 break;
924 }
925 }
926 if (cells > 1)
927 ScreenLines[idx + 1] = 0;
928 }
929 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
930 // double-byte single width character
931 ScreenLines2[idx] = p[1];
932 else if (cells > 1)
933 // double-width character
934 ScreenLines[idx + 1] = p[1];
935 col += cells;
936 idx += cells;
937 p += c_len;
938 }
939 }
940 else
941 {
942 int len = (int)STRLEN(text);
943
944 if (len > wp->w_width - col)
945 len = wp->w_width - col;
946 if (len > 0)
947 {
948#ifdef FEAT_RIGHTLEFT
949 if (wp->w_p_rl)
950 mch_memmove(current_ScreenLine, text, len);
951 else
952#endif
953 mch_memmove(current_ScreenLine + col, text, len);
954 col += len;
955 }
956 }
957 return col;
958}
959#endif
960
961#ifdef FEAT_MENU
962/*
963 * Draw the window toolbar.
964 */
965 static void
966redraw_win_toolbar(win_T *wp)
967{
968 vimmenu_T *menu;
969 int item_idx = 0;
970 int item_count = 0;
971 int col = 0;
972 int next_col;
973 int off = (int)(current_ScreenLine - ScreenLines);
974 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
975 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
976
977 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200978 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200979 ++item_count;
980 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
981
982 // TODO: use fewer spaces if there is not enough room
983 for (menu = wp->w_winbar->children;
984 menu != NULL && col < wp->w_width; menu = menu->next)
985 {
986 space_to_screenline(off + col, fill_attr);
987 if (++col >= wp->w_width)
988 break;
989 if (col > 1)
990 {
991 space_to_screenline(off + col, fill_attr);
992 if (++col >= wp->w_width)
993 break;
994 }
995
996 wp->w_winbar_items[item_idx].wb_startcol = col;
997 space_to_screenline(off + col, button_attr);
998 if (++col >= wp->w_width)
999 break;
1000
1001 next_col = text_to_screenline(wp, menu->name, col);
1002 while (col < next_col)
1003 {
1004 ScreenAttrs[off + col] = button_attr;
1005 ++col;
1006 }
1007 wp->w_winbar_items[item_idx].wb_endcol = col;
1008 wp->w_winbar_items[item_idx].wb_menu = menu;
1009 ++item_idx;
1010
1011 if (col >= wp->w_width)
1012 break;
1013 space_to_screenline(off + col, button_attr);
1014 ++col;
1015 }
1016 while (col < wp->w_width)
1017 {
1018 space_to_screenline(off + col, fill_attr);
1019 ++col;
1020 }
1021 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1022
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001023 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001024}
1025#endif
1026
1027#if defined(FEAT_FOLDING) || defined(PROTO)
1028/*
1029 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1030 */
1031 static void
1032copy_text_attr(
1033 int off,
1034 char_u *buf,
1035 int len,
1036 int attr)
1037{
1038 int i;
1039
1040 mch_memmove(ScreenLines + off, buf, (size_t)len);
1041 if (enc_utf8)
1042 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1043 for (i = 0; i < len; ++i)
1044 ScreenAttrs[off + i] = attr;
1045}
1046
1047/*
1048 * Display one folded line.
1049 */
1050 static void
1051fold_line(
1052 win_T *wp,
1053 long fold_count,
1054 foldinfo_T *foldinfo,
1055 linenr_T lnum,
1056 int row)
1057{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001058 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1059 // multi-byte character is MAX_MCO.
1060 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001061 pos_T *top, *bot;
1062 linenr_T lnume = lnum + fold_count - 1;
1063 int len;
1064 char_u *text;
1065 int fdc;
1066 int col;
1067 int txtcol;
1068 int off = (int)(current_ScreenLine - ScreenLines);
1069 int ri;
1070
1071 // Build the fold line:
1072 // 1. Add the cmdwin_type for the command-line window
1073 // 2. Add the 'foldcolumn'
1074 // 3. Add the 'number' or 'relativenumber' column
1075 // 4. Compose the text
1076 // 5. Add the text
1077 // 6. set highlighting for the Visual area an other text
1078 col = 0;
1079
1080 // 1. Add the cmdwin_type for the command-line window
1081 // Ignores 'rightleft', this window is never right-left.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001082 if (cmdwin_type != 0 && wp == curwin)
1083 {
1084 ScreenLines[off] = cmdwin_type;
1085 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1086 if (enc_utf8)
1087 ScreenLinesUC[off] = 0;
1088 ++col;
1089 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001090
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001091#ifdef FEAT_RIGHTLEFT
1092# define RL_MEMSET(p, v, l) \
1093 do { \
1094 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001095 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001096 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1097 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001098 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001099 ScreenAttrs[off + (p) + ri] = v; \
1100 } while (0)
1101#else
1102# define RL_MEMSET(p, v, l) \
1103 do { \
1104 for (ri = 0; ri < l; ++ri) \
1105 ScreenAttrs[off + (p) + ri] = v; \
1106 } while (0)
1107#endif
1108
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001109 // 2. Add the 'foldcolumn'
1110 // Reduce the width when there is not enough space.
1111 fdc = compute_foldcolumn(wp, col);
1112 if (fdc > 0)
1113 {
1114 char_u *p;
1115 int i;
1116 int idx;
1117
1118 fill_foldcolumn(buf, wp, TRUE, lnum);
1119 p = buf;
1120 for (i = 0; i < fdc; i++)
1121 {
1122 int ch;
1123
1124 if (has_mbyte)
1125 ch = mb_ptr2char_adv(&p);
1126 else
1127 ch = *p++;
1128#ifdef FEAT_RIGHTLEFT
1129 if (wp->w_p_rl)
1130 idx = off + wp->w_width - i - 1 - col;
1131 else
1132#endif
1133 idx = off + col + i;
1134 if (enc_utf8)
1135 {
1136 if (ch >= 0x80)
1137 {
1138 ScreenLinesUC[idx] = ch;
1139 ScreenLinesC[0][idx] = 0;
1140 ScreenLines[idx] = 0x80;
1141 }
1142 else
1143 {
1144 ScreenLines[idx] = ch;
1145 ScreenLinesUC[idx] = 0;
1146 }
1147 }
1148 else
1149 ScreenLines[idx] = ch;
1150 }
1151
1152 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1153 col += fdc;
1154 }
1155
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001156 // Set all attributes of the 'number' or 'relativenumber' column and the
1157 // text
1158 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1159
1160#ifdef FEAT_SIGNS
1161 // If signs are being displayed, add two spaces.
1162 if (signcolumn_on(wp))
1163 {
1164 len = wp->w_width - col;
1165 if (len > 0)
1166 {
1167 if (len > 2)
1168 len = 2;
1169# ifdef FEAT_RIGHTLEFT
1170 if (wp->w_p_rl)
1171 // the line number isn't reversed
1172 copy_text_attr(off + wp->w_width - len - col,
1173 (char_u *)" ", len, HL_ATTR(HLF_FL));
1174 else
1175# endif
1176 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1177 col += len;
1178 }
1179 }
1180#endif
1181
1182 // 3. Add the 'number' or 'relativenumber' column
1183 if (wp->w_p_nu || wp->w_p_rnu)
1184 {
1185 len = wp->w_width - col;
1186 if (len > 0)
1187 {
1188 int w = number_width(wp);
1189 long num;
1190 char *fmt = "%*ld ";
1191
1192 if (len > w + 1)
1193 len = w + 1;
1194
1195 if (wp->w_p_nu && !wp->w_p_rnu)
1196 // 'number' + 'norelativenumber'
1197 num = (long)lnum;
1198 else
1199 {
1200 // 'relativenumber', don't use negative numbers
1201 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1202 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1203 {
1204 // 'number' + 'relativenumber': cursor line shows absolute
1205 // line number
1206 num = lnum;
1207 fmt = "%-*ld ";
1208 }
1209 }
1210
1211 sprintf((char *)buf, fmt, w, num);
1212#ifdef FEAT_RIGHTLEFT
1213 if (wp->w_p_rl)
1214 // the line number isn't reversed
1215 copy_text_attr(off + wp->w_width - len - col, buf, len,
1216 HL_ATTR(HLF_FL));
1217 else
1218#endif
1219 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1220 col += len;
1221 }
1222 }
1223
1224 // 4. Compose the folded-line string with 'foldtext', if set.
1225 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1226
1227 txtcol = col; // remember where text starts
1228
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001229 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1230 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001231 // Right-left text is put in columns 0 - number-col, normal text is put
1232 // in columns number-col - window-width.
1233 col = text_to_screenline(wp, text, col);
1234
1235 // Fill the rest of the line with the fold filler
1236#ifdef FEAT_RIGHTLEFT
1237 if (wp->w_p_rl)
1238 col -= txtcol;
1239#endif
1240 while (col < wp->w_width
1241#ifdef FEAT_RIGHTLEFT
1242 - (wp->w_p_rl ? txtcol : 0)
1243#endif
1244 )
1245 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001246 int c = wp->w_fill_chars.fold;
1247
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001248 if (enc_utf8)
1249 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001250 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001251 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001252 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001253 ScreenLinesC[0][off + col] = 0;
1254 ScreenLines[off + col] = 0x80; // avoid storing zero
1255 }
1256 else
1257 {
1258 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001259 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001260 }
1261 col++;
1262 }
1263 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001264 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001265 }
1266
1267 if (text != buf)
1268 vim_free(text);
1269
1270 // 6. set highlighting for the Visual area an other text.
1271 // If all folded lines are in the Visual area, highlight the line.
1272 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1273 {
1274 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1275 {
1276 // Visual is after curwin->w_cursor
1277 top = &curwin->w_cursor;
1278 bot = &VIsual;
1279 }
1280 else
1281 {
1282 // Visual is before curwin->w_cursor
1283 top = &VIsual;
1284 bot = &curwin->w_cursor;
1285 }
1286 if (lnum >= top->lnum
1287 && lnume <= bot->lnum
1288 && (VIsual_mode != 'v'
1289 || ((lnum > top->lnum
1290 || (lnum == top->lnum
1291 && top->col == 0))
1292 && (lnume < bot->lnum
1293 || (lnume == bot->lnum
1294 && (bot->col - (*p_sel == 'e'))
1295 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
1296 {
1297 if (VIsual_mode == Ctrl_V)
1298 {
1299 // Visual block mode: highlight the chars part of the block
1300 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1301 {
1302 if (wp->w_old_cursor_lcol != MAXCOL
1303 && wp->w_old_cursor_lcol + txtcol
1304 < (colnr_T)wp->w_width)
1305 len = wp->w_old_cursor_lcol;
1306 else
1307 len = wp->w_width - txtcol;
1308 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1309 len - (int)wp->w_old_cursor_fcol);
1310 }
1311 }
1312 else
1313 {
1314 // Set all attributes of the text
1315 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1316 }
1317 }
1318 }
1319
1320#ifdef FEAT_SYN_HL
1321 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1322 if (wp->w_p_cc_cols)
1323 {
1324 int i = 0;
1325 int j = wp->w_p_cc_cols[i];
1326 int old_txtcol = txtcol;
1327
1328 while (j > -1)
1329 {
1330 txtcol += j;
1331 if (wp->w_p_wrap)
1332 txtcol -= wp->w_skipcol;
1333 else
1334 txtcol -= wp->w_leftcol;
1335 if (txtcol >= 0 && txtcol < wp->w_width)
1336 ScreenAttrs[off + txtcol] = hl_combine_attr(
1337 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1338 txtcol = old_txtcol;
1339 j = wp->w_p_cc_cols[++i];
1340 }
1341 }
1342
1343 // Show 'cursorcolumn' in the fold line.
1344 if (wp->w_p_cuc)
1345 {
1346 txtcol += wp->w_virtcol;
1347 if (wp->w_p_wrap)
1348 txtcol -= wp->w_skipcol;
1349 else
1350 txtcol -= wp->w_leftcol;
1351 if (txtcol >= 0 && txtcol < wp->w_width)
1352 ScreenAttrs[off + txtcol] = hl_combine_attr(
1353 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1354 }
1355#endif
1356
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001357 screen_line(wp, row + W_WINROW(wp), wp->w_wincol,
1358 wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001359
1360 // Update w_cline_height and w_cline_folded if the cursor line was
1361 // updated (saves a call to plines() later).
1362 if (wp == curwin
1363 && lnum <= curwin->w_cursor.lnum
1364 && lnume >= curwin->w_cursor.lnum)
1365 {
1366 curwin->w_cline_row = row;
1367 curwin->w_cline_height = 1;
1368 curwin->w_cline_folded = TRUE;
1369 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1370 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001371
1372# ifdef FEAT_CONCEAL
1373 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001374 if (wp == curwin
1375 && wp->w_cursor.lnum >= lnum
1376 && wp->w_cursor.lnum <= lnume
1377 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001378 curs_columns(TRUE);
1379# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001380}
1381#endif
1382
1383/*
1384 * Update a single window.
1385 *
1386 * This may cause the windows below it also to be redrawn (when clearing the
1387 * screen or scrolling lines).
1388 *
1389 * How the window is redrawn depends on wp->w_redr_type. Each type also
1390 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001391 * UPD_NOT_VALID redraw the whole window
1392 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1393 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1394 * UPD_VALID
1395 * UPD_INVERTED redraw the changed part of the Visual area
1396 * UPD_INVERTED_ALL redraw the whole Visual area
1397 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001398 * 2. update lines at the top when scrolled down
1399 * 3. redraw changed text:
1400 * - if wp->w_buffer->b_mod_set set, update lines between
1401 * b_mod_top and b_mod_bot.
1402 * - if wp->w_redraw_top non-zero, redraw lines between
1403 * wp->w_redraw_top and wp->w_redr_bot.
1404 * - continue redrawing when syntax status is invalid.
1405 * 4. if scrolled up, update lines at the bottom.
1406 * This results in three areas that may need updating:
1407 * top: from first row to top_end (when scrolled down)
1408 * mid: from mid_start to mid_end (update inversion or changed text)
1409 * bot: from bot_start to last row (when scrolled up)
1410 */
1411 static void
1412win_update(win_T *wp)
1413{
1414 buf_T *buf = wp->w_buffer;
1415 int type;
1416 int top_end = 0; // Below last row of the top area that needs
1417 // updating. 0 when no top area updating.
1418 int mid_start = 999;// first row of the mid area that needs
1419 // updating. 999 when no mid area updating.
1420 int mid_end = 0; // Below last row of the mid area that needs
1421 // updating. 0 when no mid area updating.
1422 int bot_start = 999;// first row of the bot area that needs
1423 // updating. 999 when no bot area updating
1424 int scrolled_down = FALSE; // TRUE when scrolled down when
1425 // w_topline got smaller a bit
1426#ifdef FEAT_SEARCH_EXTRA
1427 int top_to_mod = FALSE; // redraw above mod_top
1428#endif
1429
1430 int row; // current window row to display
1431 linenr_T lnum; // current buffer lnum to display
1432 int idx; // current index in w_lines[]
1433 int srow; // starting row of the current line
1434
1435 int eof = FALSE; // if TRUE, we hit the end of the file
1436 int didline = FALSE; // if TRUE, we finished the last line
1437 int i;
1438 long j;
1439 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001440 linenr_T old_botline = wp->w_botline;
1441#ifdef FEAT_CONCEAL
1442 int old_wrow = wp->w_wrow;
1443 int old_wcol = wp->w_wcol;
1444#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001445#ifdef FEAT_FOLDING
1446 long fold_count;
1447#endif
1448#ifdef FEAT_SYN_HL
1449 // remember what happened to the previous line, to know if
1450 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001451# define DID_NONE 1 // didn't update a line
1452# define DID_LINE 2 // updated a normal line
1453# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001454 int did_update = DID_NONE;
1455 linenr_T syntax_last_parsed = 0; // last parsed text line
1456#endif
1457 linenr_T mod_top = 0;
1458 linenr_T mod_bot = 0;
1459#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1460 int save_got_int;
1461#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001462
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001463#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1464 // This needs to be done only for the first window when update_screen() is
1465 // called.
1466 if (!did_update_one_window)
1467 {
1468 did_update_one_window = TRUE;
1469# ifdef FEAT_SEARCH_EXTRA
1470 start_search_hl();
1471# endif
1472# ifdef FEAT_CLIPBOARD
1473 // When Visual area changed, may have to update selection.
1474 if (clip_star.available && clip_isautosel_star())
1475 clip_update_selection(&clip_star);
1476 if (clip_plus.available && clip_isautosel_plus())
1477 clip_update_selection(&clip_plus);
1478# endif
1479 }
1480#endif
1481
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001482 type = wp->w_redr_type;
1483
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001484 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001485 {
1486 wp->w_redr_status = TRUE;
1487 wp->w_lines_valid = 0;
1488 }
1489
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001490 // Window frame is zero-height: nothing to draw.
1491 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1492 || (wp->w_frame->fr_height == wp->w_status_height
1493#if defined(FEAT_PROP_POPUP)
1494 && !popup_is_popup(wp)
1495#endif
1496 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001497 {
1498 wp->w_redr_type = 0;
1499 return;
1500 }
1501
1502 // Window is zero-width: Only need to draw the separator.
1503 if (wp->w_width == 0)
1504 {
1505 // draw the vertical separator right of this window
1506 draw_vsep_win(wp, 0);
1507 wp->w_redr_type = 0;
1508 return;
1509 }
1510
1511#ifdef FEAT_TERMINAL
1512 // If this window contains a terminal, redraw works completely differently.
1513 if (term_do_update_window(wp))
1514 {
1515 term_update_window(wp);
1516# ifdef FEAT_MENU
1517 // Draw the window toolbar, if there is one.
1518 if (winbar_height(wp) > 0)
1519 redraw_win_toolbar(wp);
1520# endif
1521 wp->w_redr_type = 0;
1522 return;
1523 }
1524#endif
1525
1526#ifdef FEAT_SEARCH_EXTRA
1527 init_search_hl(wp, &screen_search_hl);
1528#endif
1529
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001530 // Make sure skipcol is valid, it depends on various options and the window
1531 // width.
1532 if (wp->w_skipcol > 0)
1533 {
1534 int w = 0;
1535 int width1 = wp->w_width - win_col_off(wp);
1536 int width2 = width1 + win_col_off2(wp);
1537 int add = width1;
1538
1539 while (w < wp->w_skipcol)
1540 {
1541 if (w > 0)
1542 add = width2;
1543 w += add;
1544 }
1545 if (w != wp->w_skipcol)
1546 // always round down, the higher value may not be valid
1547 wp->w_skipcol = w - add;
1548 }
1549
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001550#ifdef FEAT_LINEBREAK
1551 // Force redraw when width of 'number' or 'relativenumber' column
1552 // changes.
1553 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1554 if (wp->w_nrwidth != i)
1555 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001556 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001557 wp->w_nrwidth = i;
1558 }
1559 else
1560#endif
1561
1562 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1563 {
1564 // When there are both inserted/deleted lines and specific lines to be
1565 // redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1566 // everything (only happens when redrawing is off for while).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001567 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001568 }
1569 else
1570 {
1571 // Set mod_top to the first line that needs displaying because of
1572 // changes. Set mod_bot to the first line after the changes.
1573 mod_top = wp->w_redraw_top;
1574 if (wp->w_redraw_bot != 0)
1575 mod_bot = wp->w_redraw_bot + 1;
1576 else
1577 mod_bot = 0;
1578 if (buf->b_mod_set)
1579 {
1580 if (mod_top == 0 || mod_top > buf->b_mod_top)
1581 {
1582 mod_top = buf->b_mod_top;
1583#ifdef FEAT_SYN_HL
1584 // Need to redraw lines above the change that may be included
1585 // in a pattern match.
1586 if (syntax_present(wp))
1587 {
1588 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1589 if (mod_top < 1)
1590 mod_top = 1;
1591 }
1592#endif
1593 }
1594 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1595 mod_bot = buf->b_mod_bot;
1596
1597#ifdef FEAT_SEARCH_EXTRA
1598 // When 'hlsearch' is on and using a multi-line search pattern, a
1599 // change in one line may make the Search highlighting in a
1600 // previous line invalid. Simple solution: redraw all visible
1601 // lines above the change.
1602 // Same for a match pattern.
1603 if (screen_search_hl.rm.regprog != NULL
1604 && re_multiline(screen_search_hl.rm.regprog))
1605 top_to_mod = TRUE;
1606 else
1607 {
1608 matchitem_T *cur = wp->w_match_head;
1609
1610 while (cur != NULL)
1611 {
Bram Moolenaar50faf022022-09-29 12:50:17 +01001612 if (cur->mit_match.regprog != NULL
1613 && re_multiline(cur->mit_match.regprog))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001614 {
1615 top_to_mod = TRUE;
1616 break;
1617 }
Bram Moolenaar50faf022022-09-29 12:50:17 +01001618 cur = cur->mit_next;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001619 }
1620 }
1621#endif
1622 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001623
1624#ifdef FEAT_SEARCH_EXTRA
1625 if (search_hl_has_cursor_lnum > 0)
1626 {
1627 // CurSearch was used last time, need to redraw the line with it to
1628 // avoid having two matches highlighted with CurSearch.
1629 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1630 mod_top = search_hl_has_cursor_lnum;
1631 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1632 mod_bot = search_hl_has_cursor_lnum + 1;
1633 }
1634#endif
1635
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001636#ifdef FEAT_FOLDING
1637 if (mod_top != 0 && hasAnyFolding(wp))
1638 {
1639 linenr_T lnumt, lnumb;
1640
1641 // A change in a line can cause lines above it to become folded or
1642 // unfolded. Find the top most buffer line that may be affected.
1643 // If the line was previously folded and displayed, get the first
1644 // line of that fold. If the line is folded now, get the first
1645 // folded line. Use the minimum of these two.
1646
1647 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1648 // the line below it. If there is no valid entry, use w_topline.
1649 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1650 // to this line. If there is no valid entry, use MAXLNUM.
1651 lnumt = wp->w_topline;
1652 lnumb = MAXLNUM;
1653 for (i = 0; i < wp->w_lines_valid; ++i)
1654 if (wp->w_lines[i].wl_valid)
1655 {
1656 if (wp->w_lines[i].wl_lastlnum < mod_top)
1657 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1658 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1659 {
1660 lnumb = wp->w_lines[i].wl_lnum;
1661 // When there is a fold column it might need updating
1662 // in the next line ("J" just above an open fold).
1663 if (compute_foldcolumn(wp, 0) > 0)
1664 ++lnumb;
1665 }
1666 }
1667
1668 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1669 if (mod_top > lnumt)
1670 mod_top = lnumt;
1671
1672 // Now do the same for the bottom line (one above mod_bot).
1673 --mod_bot;
1674 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1675 ++mod_bot;
1676 if (mod_bot < lnumb)
1677 mod_bot = lnumb;
1678 }
1679#endif
1680
1681 // When a change starts above w_topline and the end is below
1682 // w_topline, start redrawing at w_topline.
1683 // If the end of the change is above w_topline: do like no change was
1684 // made, but redraw the first line to find changes in syntax.
1685 if (mod_top != 0 && mod_top < wp->w_topline)
1686 {
1687 if (mod_bot > wp->w_topline)
1688 mod_top = wp->w_topline;
1689#ifdef FEAT_SYN_HL
1690 else if (syntax_present(wp))
1691 top_end = 1;
1692#endif
1693 }
1694
1695 // When line numbers are displayed need to redraw all lines below
1696 // inserted/deleted lines.
1697 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1698 mod_bot = MAXLNUM;
1699 }
1700 wp->w_redraw_top = 0; // reset for next time
1701 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001702#ifdef FEAT_SEARCH_EXTRA
1703 search_hl_has_cursor_lnum = 0;
1704#endif
1705
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001706
1707 // When only displaying the lines at the top, set top_end. Used when
1708 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001709 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001710 {
1711 j = 0;
1712 for (i = 0; i < wp->w_lines_valid; ++i)
1713 {
1714 j += wp->w_lines[i].wl_size;
1715 if (j >= wp->w_upd_rows)
1716 {
1717 top_end = j;
1718 break;
1719 }
1720 }
1721 if (top_end == 0)
1722 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001723 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001724 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001725 // top area defined, the rest is UPD_VALID
1726 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001727 }
1728
1729 // Trick: we want to avoid clearing the screen twice. screenclear() will
1730 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1731 // non-zero and thus not FALSE) will indicate that screenclear() was not
1732 // called.
1733 if (screen_cleared)
1734 screen_cleared = MAYBE;
1735
1736 // If there are no changes on the screen that require a complete redraw,
1737 // handle three cases:
1738 // 1: we are off the top of the screen by a few lines: scroll down
1739 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1740 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1741 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001742 if ((type == UPD_VALID || type == UPD_SOME_VALID
1743 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001744#ifdef FEAT_DIFF
1745 && !wp->w_botfill && !wp->w_old_botfill
1746#endif
1747 )
1748 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001749 if (mod_top != 0
1750 && wp->w_topline == mod_top
1751 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001752 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001753 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001754 // w_topline is the first changed line and window is not scrolled,
1755 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001756 }
1757 else if (wp->w_lines[0].wl_valid
1758 && (wp->w_topline < wp->w_lines[0].wl_lnum
1759#ifdef FEAT_DIFF
1760 || (wp->w_topline == wp->w_lines[0].wl_lnum
1761 && wp->w_topfill > wp->w_old_topfill)
1762#endif
1763 ))
1764 {
1765 // New topline is above old topline: May scroll down.
1766#ifdef FEAT_FOLDING
1767 if (hasAnyFolding(wp))
1768 {
1769 linenr_T ln;
1770
1771 // count the number of lines we are off, counting a sequence
1772 // of folded lines as one
1773 j = 0;
1774 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1775 {
1776 ++j;
1777 if (j >= wp->w_height - 2)
1778 break;
1779 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1780 }
1781 }
1782 else
1783#endif
1784 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1785 if (j < wp->w_height - 2) // not too far off
1786 {
1787 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1788#ifdef FEAT_DIFF
1789 // insert extra lines for previously invisible filler lines
1790 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1791 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1792 - wp->w_old_topfill;
1793#endif
1794 if (i < wp->w_height - 2) // less than a screen off
1795 {
1796 // Try to insert the correct number of lines.
1797 // If not the last window, delete the lines at the bottom.
1798 // win_ins_lines may fail when the terminal can't do it.
1799 if (i > 0)
1800 check_for_delay(FALSE);
1801 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1802 {
1803 if (wp->w_lines_valid != 0)
1804 {
1805 // Need to update rows that are new, stop at the
1806 // first one that scrolled down.
1807 top_end = i;
1808 scrolled_down = TRUE;
1809
1810 // Move the entries that were scrolled, disable
1811 // the entries for the lines to be redrawn.
1812 if ((wp->w_lines_valid += j) > wp->w_height)
1813 wp->w_lines_valid = wp->w_height;
Bram Moolenaara2a89732022-08-31 14:46:18 +01001814 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001815 wp->w_lines[idx] = wp->w_lines[idx - j];
1816 while (idx >= 0)
1817 wp->w_lines[idx--].wl_valid = FALSE;
1818 }
1819 }
1820 else
1821 mid_start = 0; // redraw all lines
1822 }
1823 else
1824 mid_start = 0; // redraw all lines
1825 }
1826 else
1827 mid_start = 0; // redraw all lines
1828 }
1829 else
1830 {
1831 // New topline is at or below old topline: May scroll up.
1832 // When topline didn't change, find first entry in w_lines[] that
1833 // needs updating.
1834
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001835 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1836 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001837 j = -1;
1838 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001839 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001840 {
1841 if (wp->w_lines[i].wl_valid
1842 && wp->w_lines[i].wl_lnum == wp->w_topline)
1843 {
1844 j = i;
1845 break;
1846 }
1847 row += wp->w_lines[i].wl_size;
1848 }
1849 if (j == -1)
1850 {
1851 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1852 // lines
1853 mid_start = 0;
1854 }
1855 else
1856 {
1857 // Try to delete the correct number of lines.
1858 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1859#ifdef FEAT_DIFF
1860 // If the topline didn't change, delete old filler lines,
1861 // otherwise delete filler lines of the new topline...
1862 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1863 row += wp->w_old_topfill;
1864 else
1865 row += diff_check_fill(wp, wp->w_topline);
1866 // ... but don't delete new filler lines.
1867 row -= wp->w_topfill;
1868#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001869 if (row > Rows) // just in case
1870 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001871 if (row > 0)
1872 {
1873 check_for_delay(FALSE);
1874 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1875 == OK)
1876 bot_start = wp->w_height - row;
1877 else
1878 mid_start = 0; // redraw all lines
1879 }
1880 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1881 {
1882 // Skip the lines (below the deleted lines) that are still
1883 // valid and don't need redrawing. Copy their info
1884 // upwards, to compensate for the deleted lines. Set
1885 // bot_start to the first row that needs redrawing.
1886 bot_start = 0;
1887 idx = 0;
1888 for (;;)
1889 {
1890 wp->w_lines[idx] = wp->w_lines[j];
1891 // stop at line that didn't fit, unless it is still
1892 // valid (no lines deleted)
1893 if (row > 0 && bot_start + row
1894 + (int)wp->w_lines[j].wl_size > wp->w_height)
1895 {
1896 wp->w_lines_valid = idx + 1;
1897 break;
1898 }
1899 bot_start += wp->w_lines[idx++].wl_size;
1900
1901 // stop at the last valid entry in w_lines[].wl_size
1902 if (++j >= wp->w_lines_valid)
1903 {
1904 wp->w_lines_valid = idx;
1905 break;
1906 }
1907 }
1908#ifdef FEAT_DIFF
1909 // Correct the first entry for filler lines at the top
1910 // when it won't get updated below.
1911 if (wp->w_p_diff && bot_start > 0)
1912 wp->w_lines[0].wl_size =
1913 plines_win_nofill(wp, wp->w_topline, TRUE)
1914 + wp->w_topfill;
1915#endif
1916 }
1917 }
1918 }
1919
Bram Moolenaar13608d82022-08-29 15:06:50 +01001920 // When starting redraw in the first line, redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001921 if (mid_start == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001922 mid_end = wp->w_height;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001923
1924 // When win_del_lines() or win_ins_lines() caused the screen to be
1925 // cleared (only happens for the first window) or when screenclear()
1926 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001927 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001928 if (screen_cleared == TRUE)
1929 must_redraw = 0;
1930 }
1931 else
1932 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001933 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001934 mid_start = 0;
1935 mid_end = wp->w_height;
1936 }
1937
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001938 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001939 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001940 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001941 mid_start = 0;
1942 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001943 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001944 }
1945
1946 // check if we are updating or removing the inverted part
1947 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001948 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001949 {
1950 linenr_T from, to;
1951
1952 if (VIsual_active)
1953 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001954 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001955 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001956 {
1957 // If the type of Visual selection changed, redraw the whole
1958 // selection. Also when the ownership of the X selection is
1959 // gained or lost.
1960 if (curwin->w_cursor.lnum < VIsual.lnum)
1961 {
1962 from = curwin->w_cursor.lnum;
1963 to = VIsual.lnum;
1964 }
1965 else
1966 {
1967 from = VIsual.lnum;
1968 to = curwin->w_cursor.lnum;
1969 }
1970 // redraw more when the cursor moved as well
1971 if (wp->w_old_cursor_lnum < from)
1972 from = wp->w_old_cursor_lnum;
1973 if (wp->w_old_cursor_lnum > to)
1974 to = wp->w_old_cursor_lnum;
1975 if (wp->w_old_visual_lnum < from)
1976 from = wp->w_old_visual_lnum;
1977 if (wp->w_old_visual_lnum > to)
1978 to = wp->w_old_visual_lnum;
1979 }
1980 else
1981 {
1982 // Find the line numbers that need to be updated: The lines
1983 // between the old cursor position and the current cursor
1984 // position. Also check if the Visual position changed.
1985 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1986 {
1987 from = curwin->w_cursor.lnum;
1988 to = wp->w_old_cursor_lnum;
1989 }
1990 else
1991 {
1992 from = wp->w_old_cursor_lnum;
1993 to = curwin->w_cursor.lnum;
1994 if (from == 0) // Visual mode just started
1995 from = to;
1996 }
1997
1998 if (VIsual.lnum != wp->w_old_visual_lnum
1999 || VIsual.col != wp->w_old_visual_col)
2000 {
2001 if (wp->w_old_visual_lnum < from
2002 && wp->w_old_visual_lnum != 0)
2003 from = wp->w_old_visual_lnum;
2004 if (wp->w_old_visual_lnum > to)
2005 to = wp->w_old_visual_lnum;
2006 if (VIsual.lnum < from)
2007 from = VIsual.lnum;
2008 if (VIsual.lnum > to)
2009 to = VIsual.lnum;
2010 }
2011 }
2012
2013 // If in block mode and changed column or curwin->w_curswant:
2014 // update all lines.
2015 // First compute the actual start and end column.
2016 if (VIsual_mode == Ctrl_V)
2017 {
2018 colnr_T fromc, toc;
2019#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002020 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002021
2022 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002023 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002024#endif
2025 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002026 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002027#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002028 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002029#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002030 // Highlight to the end of the line, unless 'virtualedit' has
2031 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002032 if (curwin->w_curswant == MAXCOL)
2033 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002034 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002035 {
2036 pos_T pos;
2037 int cursor_above =
2038 curwin->w_cursor.lnum < VIsual.lnum;
2039
2040 // Need to find the longest line.
2041 toc = 0;
2042 pos.coladd = 0;
2043 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2044 ? pos.lnum <= VIsual.lnum
2045 : pos.lnum >= VIsual.lnum;
2046 pos.lnum += cursor_above ? 1 : -1)
2047 {
2048 colnr_T t;
2049
Bram Moolenaar6bcb1822021-07-09 15:54:00 +02002050 pos.col = (int)STRLEN(ml_get_buf(wp->w_buffer,
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002051 pos.lnum, FALSE));
2052 getvvcol(wp, &pos, NULL, NULL, &t);
2053 if (toc < t)
2054 toc = t;
2055 }
2056 ++toc;
2057 }
2058 else
2059 toc = MAXCOL;
2060 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002061
2062 if (fromc != wp->w_old_cursor_fcol
2063 || toc != wp->w_old_cursor_lcol)
2064 {
2065 if (from > VIsual.lnum)
2066 from = VIsual.lnum;
2067 if (to < VIsual.lnum)
2068 to = VIsual.lnum;
2069 }
2070 wp->w_old_cursor_fcol = fromc;
2071 wp->w_old_cursor_lcol = toc;
2072 }
2073 }
2074 else
2075 {
2076 // Use the line numbers of the old Visual area.
2077 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2078 {
2079 from = wp->w_old_cursor_lnum;
2080 to = wp->w_old_visual_lnum;
2081 }
2082 else
2083 {
2084 from = wp->w_old_visual_lnum;
2085 to = wp->w_old_cursor_lnum;
2086 }
2087 }
2088
2089 // There is no need to update lines above the top of the window.
2090 if (from < wp->w_topline)
2091 from = wp->w_topline;
2092
2093 // If we know the value of w_botline, use it to restrict the update to
2094 // the lines that are visible in the window.
2095 if (wp->w_valid & VALID_BOTLINE)
2096 {
2097 if (from >= wp->w_botline)
2098 from = wp->w_botline - 1;
2099 if (to >= wp->w_botline)
2100 to = wp->w_botline - 1;
2101 }
2102
2103 // Find the minimal part to be updated.
2104 // Watch out for scrolling that made entries in w_lines[] invalid.
2105 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2106 // top_end; need to redraw from top_end to the "to" line.
2107 // A middle mouse click with a Visual selection may change the text
2108 // above the Visual area and reset wl_valid, do count these for
2109 // mid_end (in srow).
2110 if (mid_start > 0)
2111 {
2112 lnum = wp->w_topline;
2113 idx = 0;
2114 srow = 0;
2115 if (scrolled_down)
2116 mid_start = top_end;
2117 else
2118 mid_start = 0;
2119 while (lnum < from && idx < wp->w_lines_valid) // find start
2120 {
2121 if (wp->w_lines[idx].wl_valid)
2122 mid_start += wp->w_lines[idx].wl_size;
2123 else if (!scrolled_down)
2124 srow += wp->w_lines[idx].wl_size;
2125 ++idx;
2126# ifdef FEAT_FOLDING
2127 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2128 lnum = wp->w_lines[idx].wl_lnum;
2129 else
2130# endif
2131 ++lnum;
2132 }
2133 srow += mid_start;
2134 mid_end = wp->w_height;
2135 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2136 {
2137 if (wp->w_lines[idx].wl_valid
2138 && wp->w_lines[idx].wl_lnum >= to + 1)
2139 {
2140 // Only update until first row of this line
2141 mid_end = srow;
2142 break;
2143 }
2144 srow += wp->w_lines[idx].wl_size;
2145 }
2146 }
2147 }
2148
2149 if (VIsual_active && buf == curwin->w_buffer)
2150 {
2151 wp->w_old_visual_mode = VIsual_mode;
2152 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2153 wp->w_old_visual_lnum = VIsual.lnum;
2154 wp->w_old_visual_col = VIsual.col;
2155 wp->w_old_curswant = curwin->w_curswant;
2156 }
2157 else
2158 {
2159 wp->w_old_visual_mode = 0;
2160 wp->w_old_cursor_lnum = 0;
2161 wp->w_old_visual_lnum = 0;
2162 wp->w_old_visual_col = 0;
2163 }
2164
2165#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2166 // reset got_int, otherwise regexp won't work
2167 save_got_int = got_int;
2168 got_int = 0;
2169#endif
2170#ifdef SYN_TIME_LIMIT
2171 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002172 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002173 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002174#endif
2175#ifdef FEAT_FOLDING
2176 win_foldinfo.fi_level = 0;
2177#endif
2178
2179#ifdef FEAT_MENU
2180 // Draw the window toolbar, if there is one.
2181 // TODO: only when needed.
2182 if (winbar_height(wp) > 0)
2183 redraw_win_toolbar(wp);
2184#endif
2185
2186 // Update all the window rows.
2187 idx = 0; // first entry in w_lines[].wl_size
2188 row = 0;
2189 srow = 0;
2190 lnum = wp->w_topline; // first line shown in window
2191 for (;;)
2192 {
2193 // stop updating when reached the end of the window (check for _past_
2194 // the end of the window is at the end of the loop)
2195 if (row == wp->w_height)
2196 {
2197 didline = TRUE;
2198 break;
2199 }
2200
2201 // stop updating when hit the end of the file
2202 if (lnum > buf->b_ml.ml_line_count)
2203 {
2204 eof = TRUE;
2205 break;
2206 }
2207
2208 // Remember the starting row of the line that is going to be dealt
2209 // with. It is used further down when the line doesn't fit.
2210 srow = row;
2211
2212 // Update a line when it is in an area that needs updating, when it
2213 // has changes or w_lines[idx] is invalid.
2214 // "bot_start" may be halfway a wrapped line after using
2215 // win_del_lines(), check if the current line includes it.
2216 // When syntax folding is being used, the saved syntax states will
2217 // already have been updated, we can't see where the syntax state is
2218 // the same again, just update until the end of the window.
2219 if (row < top_end
2220 || (row >= mid_start && row < mid_end)
2221#ifdef FEAT_SEARCH_EXTRA
2222 || top_to_mod
2223#endif
2224 || idx >= wp->w_lines_valid
2225 || (row + wp->w_lines[idx].wl_size > bot_start)
2226 || (mod_top != 0
2227 && (lnum == mod_top
2228 || (lnum >= mod_top
2229 && (lnum < mod_bot
2230#ifdef FEAT_SYN_HL
2231 || did_update == DID_FOLD
2232 || (did_update == DID_LINE
2233 && syntax_present(wp)
2234 && (
2235# ifdef FEAT_FOLDING
2236 (foldmethodIsSyntax(wp)
2237 && hasAnyFolding(wp)) ||
2238# endif
2239 syntax_check_changed(lnum)))
2240#endif
2241#ifdef FEAT_SEARCH_EXTRA
2242 // match in fixed position might need redraw
2243 // if lines were inserted or deleted
2244 || (wp->w_match_head != NULL
2245 && buf->b_mod_xlines != 0)
2246#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002247 ))))
2248#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002249 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2250 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002251#endif
2252 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002253 {
2254#ifdef FEAT_SEARCH_EXTRA
2255 if (lnum == mod_top)
2256 top_to_mod = FALSE;
2257#endif
2258
2259 // When at start of changed lines: May scroll following lines
2260 // up or down to minimize redrawing.
2261 // Don't do this when the change continues until the end.
2262 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002263 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002264 if (lnum == mod_top
2265 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002266 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2267 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002268 {
2269 int old_rows = 0;
2270 int new_rows = 0;
2271 int xtra_rows;
2272 linenr_T l;
2273
2274 // Count the old number of window rows, using w_lines[], which
2275 // should still contain the sizes for the lines as they are
2276 // currently displayed.
2277 for (i = idx; i < wp->w_lines_valid; ++i)
2278 {
2279 // Only valid lines have a meaningful wl_lnum. Invalid
2280 // lines are part of the changed area.
2281 if (wp->w_lines[i].wl_valid
2282 && wp->w_lines[i].wl_lnum == mod_bot)
2283 break;
2284 old_rows += wp->w_lines[i].wl_size;
2285#ifdef FEAT_FOLDING
2286 if (wp->w_lines[i].wl_valid
2287 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2288 {
2289 // Must have found the last valid entry above mod_bot.
2290 // Add following invalid entries.
2291 ++i;
2292 while (i < wp->w_lines_valid
2293 && !wp->w_lines[i].wl_valid)
2294 old_rows += wp->w_lines[i++].wl_size;
2295 break;
2296 }
2297#endif
2298 }
2299
2300 if (i >= wp->w_lines_valid)
2301 {
2302 // We can't find a valid line below the changed lines,
2303 // need to redraw until the end of the window.
2304 // Inserting/deleting lines has no use.
2305 bot_start = 0;
2306 }
2307 else
2308 {
2309 // Able to count old number of rows: Count new window
2310 // rows, and may insert/delete lines
2311 j = idx;
2312 for (l = lnum; l < mod_bot; ++l)
2313 {
2314#ifdef FEAT_FOLDING
2315 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2316 ++new_rows;
2317 else
2318#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002319 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002320#ifdef FEAT_DIFF
2321 if (l == wp->w_topline)
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002322 new_rows += plines_win_nofill(wp, l, TRUE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002323 + wp->w_topfill;
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002324 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002325#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002326 new_rows += plines_win(wp, l, TRUE);
2327 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002328 ++j;
2329 if (new_rows > wp->w_height - row - 2)
2330 {
2331 // it's getting too much, must redraw the rest
2332 new_rows = 9999;
2333 break;
2334 }
2335 }
2336 xtra_rows = new_rows - old_rows;
2337 if (xtra_rows < 0)
2338 {
2339 // May scroll text up. If there is not enough
2340 // remaining text or scrolling fails, must redraw the
2341 // rest. If scrolling works, must redraw the text
2342 // below the scrolled text.
2343 if (row - xtra_rows >= wp->w_height - 2)
2344 mod_bot = MAXLNUM;
2345 else
2346 {
2347 check_for_delay(FALSE);
2348 if (win_del_lines(wp, row,
2349 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2350 mod_bot = MAXLNUM;
2351 else
2352 bot_start = wp->w_height + xtra_rows;
2353 }
2354 }
2355 else if (xtra_rows > 0)
2356 {
2357 // May scroll text down. If there is not enough
2358 // remaining text of scrolling fails, must redraw the
2359 // rest.
2360 if (row + xtra_rows >= wp->w_height - 2)
2361 mod_bot = MAXLNUM;
2362 else
2363 {
2364 check_for_delay(FALSE);
2365 if (win_ins_lines(wp, row + old_rows,
2366 xtra_rows, FALSE, FALSE) == FAIL)
2367 mod_bot = MAXLNUM;
2368 else if (top_end > row + old_rows)
2369 // Scrolled the part at the top that requires
2370 // updating down.
2371 top_end += xtra_rows;
2372 }
2373 }
2374
2375 // When not updating the rest, may need to move w_lines[]
2376 // entries.
2377 if (mod_bot != MAXLNUM && i != j)
2378 {
2379 if (j < i)
2380 {
2381 int x = row + new_rows;
2382
2383 // move entries in w_lines[] upwards
2384 for (;;)
2385 {
2386 // stop at last valid entry in w_lines[]
2387 if (i >= wp->w_lines_valid)
2388 {
2389 wp->w_lines_valid = j;
2390 break;
2391 }
2392 wp->w_lines[j] = wp->w_lines[i];
2393 // stop at a line that won't fit
2394 if (x + (int)wp->w_lines[j].wl_size
2395 > wp->w_height)
2396 {
2397 wp->w_lines_valid = j + 1;
2398 break;
2399 }
2400 x += wp->w_lines[j++].wl_size;
2401 ++i;
2402 }
2403 if (bot_start > x)
2404 bot_start = x;
2405 }
2406 else // j > i
2407 {
2408 // move entries in w_lines[] downwards
2409 j -= i;
2410 wp->w_lines_valid += j;
2411 if (wp->w_lines_valid > wp->w_height)
2412 wp->w_lines_valid = wp->w_height;
2413 for (i = wp->w_lines_valid; i - j >= idx; --i)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002414 wp->w_lines[i] = wp->w_lines[i - j];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002415
2416 // The w_lines[] entries for inserted lines are
2417 // now invalid, but wl_size may be used above.
2418 // Reset to zero.
2419 while (i >= idx)
2420 {
2421 wp->w_lines[i].wl_size = 0;
2422 wp->w_lines[i--].wl_valid = FALSE;
2423 }
2424 }
2425 }
2426 }
2427 }
2428
2429#ifdef FEAT_FOLDING
2430 // When lines are folded, display one line for all of them.
2431 // Otherwise, display normally (can be several display lines when
2432 // 'wrap' is on).
2433 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2434 if (fold_count != 0)
2435 {
2436 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2437 ++row;
2438 --fold_count;
2439 wp->w_lines[idx].wl_folded = TRUE;
2440 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2441# ifdef FEAT_SYN_HL
2442 did_update = DID_FOLD;
2443# endif
2444 }
2445 else
2446#endif
2447 if (idx < wp->w_lines_valid
2448 && wp->w_lines[idx].wl_valid
2449 && wp->w_lines[idx].wl_lnum == lnum
2450 && lnum > wp->w_topline
2451 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2452 && !WIN_IS_POPUP(wp)
2453 && srow + wp->w_lines[idx].wl_size > wp->w_height
2454#ifdef FEAT_DIFF
2455 && diff_check_fill(wp, lnum) == 0
2456#endif
2457 )
2458 {
2459 // This line is not going to fit. Don't draw anything here,
2460 // will draw "@ " lines below.
2461 row = wp->w_height + 1;
2462 }
2463 else
2464 {
2465#ifdef FEAT_SEARCH_EXTRA
2466 prepare_search_hl(wp, &screen_search_hl, lnum);
2467#endif
2468#ifdef FEAT_SYN_HL
2469 // Let the syntax stuff know we skipped a few lines.
2470 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2471 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002472 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002473#endif
2474
2475 // Display one line.
2476 row = win_line(wp, lnum, srow, wp->w_height,
2477 mod_top == 0, FALSE);
2478
2479#ifdef FEAT_FOLDING
2480 wp->w_lines[idx].wl_folded = FALSE;
2481 wp->w_lines[idx].wl_lastlnum = lnum;
2482#endif
2483#ifdef FEAT_SYN_HL
2484 did_update = DID_LINE;
2485 syntax_last_parsed = lnum;
2486#endif
2487 }
2488
2489 wp->w_lines[idx].wl_lnum = lnum;
2490 wp->w_lines[idx].wl_valid = TRUE;
2491
2492 // Past end of the window or end of the screen. Note that after
2493 // resizing wp->w_height may be end up too big. That's a problem
2494 // elsewhere, but prevent a crash here.
Bram Moolenaara2a89732022-08-31 14:46:18 +01002495 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002496 {
2497 // we may need the size of that too long line later on
2498 if (dollar_vcol == -1)
2499 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2500 ++idx;
2501 break;
2502 }
2503 if (dollar_vcol == -1)
2504 wp->w_lines[idx].wl_size = row - srow;
2505 ++idx;
2506#ifdef FEAT_FOLDING
2507 lnum += fold_count + 1;
2508#else
2509 ++lnum;
2510#endif
2511 }
2512 else
2513 {
Lewis Russell16246392022-03-29 11:38:17 +01002514 if (wp->w_p_rnu && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002515 {
2516#ifdef FEAT_FOLDING
Lewis Russell16246392022-03-29 11:38:17 +01002517 // 'relativenumber' set and the cursor moved vertically: The
2518 // text doesn't need to be drawn, but the number column does.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002519 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2520 if (fold_count != 0)
2521 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2522 else
2523#endif
2524 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
2525 }
2526
2527 // This line does not need to be drawn, advance to the next one.
2528 row += wp->w_lines[idx++].wl_size;
2529 if (row > wp->w_height) // past end of screen
2530 break;
2531#ifdef FEAT_FOLDING
2532 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2533#else
2534 ++lnum;
2535#endif
2536#ifdef FEAT_SYN_HL
2537 did_update = DID_NONE;
2538#endif
2539 }
2540
2541 if (lnum > buf->b_ml.ml_line_count)
2542 {
2543 eof = TRUE;
2544 break;
2545 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002546
2547 // Safety check: if any of the wl_size values is wrong we might go over
2548 // the end of w_lines[].
Bram Moolenaara2a89732022-08-31 14:46:18 +01002549 if (idx >= Rows)
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002550 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002551 }
2552
2553 // End of loop over all window lines.
2554
zeertzjqc20e46a2022-03-23 14:55:23 +00002555#ifdef FEAT_SYN_HL
2556 // Now that the window has been redrawn with the old and new cursor line,
2557 // update w_last_cursorline.
2558 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2559#endif
Lewis Russell16246392022-03-29 11:38:17 +01002560 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002561
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002562#ifdef FEAT_VTP
2563 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002564 // See the version that was fixed.
2565 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002566 {
K.Takata54119102022-02-03 13:33:03 +00002567 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002568
K.Takata54119102022-02-03 13:33:03 +00002569 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002570 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002571 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2572 LineOffset[k] + screen_Columns) > 1)
2573 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002574 else
K.Takata54119102022-02-03 13:33:03 +00002575 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002576 else
K.Takata54119102022-02-03 13:33:03 +00002577 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002578 }
2579#endif
2580
2581 if (idx > wp->w_lines_valid)
2582 wp->w_lines_valid = idx;
2583
2584#ifdef FEAT_SYN_HL
2585 // Let the syntax stuff know we stop parsing here.
2586 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar0abd6cf2022-10-14 17:04:09 +01002587 syntax_end_parsing(wp, syntax_last_parsed + 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002588#endif
2589
2590 // If we didn't hit the end of the file, and we didn't finish the last
2591 // line we were working on, then the line didn't fit.
2592 wp->w_empty_rows = 0;
2593#ifdef FEAT_DIFF
2594 wp->w_filler_rows = 0;
2595#endif
2596 if (!eof && !didline)
2597 {
2598 if (lnum == wp->w_topline)
2599 {
2600 // Single line that does not fit!
2601 // Don't overwrite it, it can be edited.
2602 wp->w_botline = lnum + 1;
2603 }
2604#ifdef FEAT_DIFF
2605 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2606 {
2607 // Window ends in filler lines.
2608 wp->w_botline = lnum;
2609 wp->w_filler_rows = wp->w_height - srow;
2610 }
2611#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002612#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002613 else if (WIN_IS_POPUP(wp))
2614 {
2615 // popup line that doesn't fit is left as-is
2616 wp->w_botline = lnum;
2617 }
2618#endif
2619 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2620 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002621 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2622 int symbol = wp->w_fill_chars.lastline;
zeertzjq18b35002022-10-04 20:35:37 +01002623 int charlen;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002624 char_u fillbuf[12]; // 2 characters of 6 bytes
2625
zeertzjq18b35002022-10-04 20:35:37 +01002626 charlen = mb_char2bytes(symbol, &fillbuf[0]);
2627 mb_char2bytes(symbol, &fillbuf[charlen]);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002628
2629 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002630 screen_puts_len(fillbuf,
zeertzjq18b35002022-10-04 20:35:37 +01002631 (wp->w_width > 2 ? 2 : wp->w_width) * charlen,
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002632 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002633 screen_fill(scr_row, scr_row + 1,
2634 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002635 symbol, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002636 set_empty_rows(wp, srow);
2637 wp->w_botline = lnum;
2638 }
2639 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2640 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002641 int start_col = (int)W_ENDCOL(wp) - 3;
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002642 int symbol = wp->w_fill_chars.lastline;
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002643
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002644 // Last line isn't finished: Display "@@@" at the end.
2645 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2646 W_WINROW(wp) + wp->w_height,
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002647 start_col < wp->w_wincol ? wp->w_wincol : start_col,
2648 (int)W_ENDCOL(wp),
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002649 symbol, symbol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002650 set_empty_rows(wp, srow);
2651 wp->w_botline = lnum;
2652 }
2653 else
2654 {
Bram Moolenaar4ba5f1d2022-10-04 14:36:29 +01002655 win_draw_end(wp, wp->w_fill_chars.lastline, ' ', TRUE,
2656 srow, wp->w_height, HLF_AT);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002657 wp->w_botline = lnum;
2658 }
2659 }
2660 else
2661 {
2662 draw_vsep_win(wp, row);
2663 if (eof) // we hit the end of the file
2664 {
2665 wp->w_botline = buf->b_ml.ml_line_count + 1;
2666#ifdef FEAT_DIFF
2667 j = diff_check_fill(wp, wp->w_botline);
2668 if (j > 0 && !wp->w_botfill)
2669 {
2670 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002671 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002672 i = '-';
2673 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002674 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002675 if (row + j > wp->w_height)
2676 j = wp->w_height - row;
2677 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2678 row += j;
2679 }
2680#endif
2681 }
2682 else if (dollar_vcol == -1)
2683 wp->w_botline = lnum;
2684
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002685 // Make sure the rest of the screen is blank.
2686 // write the "eob" character from 'fillchars' to rows that aren't part
2687 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002688 if (WIN_IS_POPUP(wp))
2689 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2690 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002691 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2692 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002693 }
2694
2695#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002696 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002697 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002698#endif
2699
2700 // Reset the type of redrawing required, the window has been updated.
2701 wp->w_redr_type = 0;
2702#ifdef FEAT_DIFF
2703 wp->w_old_topfill = wp->w_topfill;
2704 wp->w_old_botfill = wp->w_botfill;
2705#endif
2706
2707 if (dollar_vcol == -1)
2708 {
2709 // There is a trick with w_botline. If we invalidate it on each
2710 // change that might modify it, this will cause a lot of expensive
2711 // calls to plines() in update_topline() each time. Therefore the
2712 // value of w_botline is often approximated, and this value is used to
2713 // compute the value of w_topline. If the value of w_botline was
2714 // wrong, check that the value of w_topline is correct (cursor is on
2715 // the visible part of the text). If it's not, we need to redraw
2716 // again. Mostly this just means scrolling up a few lines, so it
2717 // doesn't look too bad. Only do this for the current window (where
2718 // changes are relevant).
2719 wp->w_valid |= VALID_BOTLINE;
2720 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2721 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002722 win_T *wwp;
2723#if defined(FEAT_CONCEAL)
2724 linenr_T old_topline = wp->w_topline;
2725 int new_wcol = wp->w_wcol;
2726#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002727 recursive = TRUE;
2728 curwin->w_valid &= ~VALID_TOPLINE;
2729 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002730
2731#if defined(FEAT_CONCEAL)
2732 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2733 != (VALID_WCOL|VALID_WROW))
2734 {
2735 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002736 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002737 // update_topline() the cursor's row or column got invalidated.
2738 // If they are left invalid, setcursor() will recompute them
2739 // but there won't be any further win_line() call to re-fix the
2740 // column and the cursor will end up misplaced. So we call
2741 // cursor validation now and reapply the fix again (or call
2742 // win_line() to do it for us).
2743 validate_cursor();
2744 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2745 && old_topline == wp->w_topline)
2746 wp->w_wcol = new_wcol;
2747 else
2748 redrawWinline(wp, wp->w_cursor.lnum);
2749 }
2750#endif
2751 // New redraw either due to updated topline or due to wcol fix.
2752 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002753 {
2754 // Don't update for changes in buffer again.
2755 i = curbuf->b_mod_set;
2756 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002757 j = curbuf->b_mod_xlines;
2758 curbuf->b_mod_xlines = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002759 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002760 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002761 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002762 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002763 // Other windows might have w_redr_type raised in update_topline().
2764 must_redraw = 0;
2765 FOR_ALL_WINDOWS(wwp)
2766 if (wwp->w_redr_type > must_redraw)
2767 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002768 recursive = FALSE;
2769 }
2770 }
2771
2772#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2773 // restore got_int, unless CTRL-C was hit while redrawing
2774 if (!got_int)
2775 got_int = save_got_int;
2776#endif
2777}
2778
2779#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2780/*
2781 * Prepare for updating one or more windows.
2782 * Caller must check for "updating_screen" already set to avoid recursiveness.
2783 */
2784 static void
2785update_prepare(void)
2786{
2787 cursor_off();
2788 updating_screen = TRUE;
2789#ifdef FEAT_GUI
2790 // Remove the cursor before starting to do anything, because scrolling may
2791 // make it difficult to redraw the text under it.
2792 if (gui.in_use)
2793 gui_undraw_cursor();
2794#endif
2795#ifdef FEAT_SEARCH_EXTRA
2796 start_search_hl();
2797#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002798#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002799 // Update popup_mask if needed.
2800 may_update_popup_mask(must_redraw);
2801#endif
2802}
2803
2804/*
2805 * Finish updating one or more windows.
2806 */
2807 static void
2808update_finish(void)
2809{
2810 if (redraw_cmdline || redraw_mode)
2811 showmode();
2812
2813# ifdef FEAT_SEARCH_EXTRA
2814 end_search_hl();
2815# endif
2816
2817 after_updating_screen(TRUE);
2818
2819# ifdef FEAT_GUI
2820 // Redraw the cursor and update the scrollbars when all screen updating is
2821 // done.
2822 if (gui.in_use)
2823 {
2824 out_flush_cursor(FALSE, FALSE);
2825 gui_update_scrollbars(FALSE);
2826 }
2827# endif
2828}
2829#endif
2830
2831#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2832 void
2833update_debug_sign(buf_T *buf, linenr_T lnum)
2834{
2835 win_T *wp;
2836 int doit = FALSE;
2837
2838# ifdef FEAT_FOLDING
2839 win_foldinfo.fi_level = 0;
2840# endif
2841
2842 // update/delete a specific sign
2843 redraw_buf_line_later(buf, lnum);
2844
2845 // check if it resulted in the need to redraw a window
2846 FOR_ALL_WINDOWS(wp)
2847 if (wp->w_redr_type != 0)
2848 doit = TRUE;
2849
2850 // Return when there is nothing to do, screen updating is already
2851 // happening (recursive call), messages on the screen or still starting up.
2852 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002853 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002854 || msg_scrolled
2855#ifdef FEAT_GUI
2856 || gui.starting
2857#endif
2858 || starting)
2859 return;
2860
2861 // update all windows that need updating
2862 update_prepare();
2863
2864 FOR_ALL_WINDOWS(wp)
2865 {
2866 if (wp->w_redr_type != 0)
2867 win_update(wp);
2868 if (wp->w_redr_status)
2869 win_redr_status(wp, FALSE);
2870 }
2871
2872 update_finish();
2873}
2874#endif
2875
2876#if defined(FEAT_GUI) || defined(PROTO)
2877/*
2878 * Update a single window, its status line and maybe the command line msg.
2879 * Used for the GUI scrollbar.
2880 */
2881 void
2882updateWindow(win_T *wp)
2883{
2884 // return if already busy updating
2885 if (updating_screen)
2886 return;
2887
2888 update_prepare();
2889
2890#ifdef FEAT_CLIPBOARD
2891 // When Visual area changed, may have to update selection.
2892 if (clip_star.available && clip_isautosel_star())
2893 clip_update_selection(&clip_star);
2894 if (clip_plus.available && clip_isautosel_plus())
2895 clip_update_selection(&clip_plus);
2896#endif
2897
2898 win_update(wp);
2899
2900 // When the screen was cleared redraw the tab pages line.
2901 if (redraw_tabline)
2902 draw_tabline();
2903
Martin Tournoijba43e762022-10-13 22:12:15 +01002904 if (wp->w_redr_status || p_ru
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002905# ifdef FEAT_STL_OPT
2906 || *p_stl != NUL || *wp->w_p_stl != NUL
2907# endif
2908 )
2909 win_redr_status(wp, FALSE);
2910
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002911#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002912 // Display popup windows on top of everything.
2913 update_popups(win_update);
2914#endif
2915
2916 update_finish();
2917}
2918#endif
2919
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002920/*
2921 * Redraw as soon as possible. When the command line is not scrolled redraw
2922 * right away and restore what was on the command line.
2923 * Return a code indicating what happened.
2924 */
2925 int
2926redraw_asap(int type)
2927{
2928 int rows;
2929 int cols = screen_Columns;
2930 int r;
2931 int ret = 0;
2932 schar_T *screenline; // copy from ScreenLines[]
2933 sattr_T *screenattr; // copy from ScreenAttrs[]
2934 int i;
2935 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2936 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2937 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2938
2939 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002940 if (msg_scrolled
2941 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002942 || exiting)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002943 return ret;
2944
2945 // Allocate space to save the text displayed in the command line area.
2946 rows = screen_Rows - cmdline_row;
2947 screenline = LALLOC_MULT(schar_T, rows * cols);
2948 screenattr = LALLOC_MULT(sattr_T, rows * cols);
2949 if (screenline == NULL || screenattr == NULL)
2950 ret = 2;
2951 if (enc_utf8)
2952 {
2953 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
2954 if (screenlineUC == NULL)
2955 ret = 2;
2956 for (i = 0; i < p_mco; ++i)
2957 {
2958 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
2959 if (screenlineC[i] == NULL)
2960 ret = 2;
2961 }
2962 }
2963 if (enc_dbcs == DBCS_JPNU)
2964 {
2965 screenline2 = LALLOC_MULT(schar_T, rows * cols);
2966 if (screenline2 == NULL)
2967 ret = 2;
2968 }
2969
2970 if (ret != 2)
2971 {
2972 // Save the text displayed in the command line area.
2973 for (r = 0; r < rows; ++r)
2974 {
2975 mch_memmove(screenline + r * cols,
2976 ScreenLines + LineOffset[cmdline_row + r],
2977 (size_t)cols * sizeof(schar_T));
2978 mch_memmove(screenattr + r * cols,
2979 ScreenAttrs + LineOffset[cmdline_row + r],
2980 (size_t)cols * sizeof(sattr_T));
2981 if (enc_utf8)
2982 {
2983 mch_memmove(screenlineUC + r * cols,
2984 ScreenLinesUC + LineOffset[cmdline_row + r],
2985 (size_t)cols * sizeof(u8char_T));
2986 for (i = 0; i < p_mco; ++i)
2987 mch_memmove(screenlineC[i] + r * cols,
2988 ScreenLinesC[i] + LineOffset[cmdline_row + r],
2989 (size_t)cols * sizeof(u8char_T));
2990 }
2991 if (enc_dbcs == DBCS_JPNU)
2992 mch_memmove(screenline2 + r * cols,
2993 ScreenLines2 + LineOffset[cmdline_row + r],
2994 (size_t)cols * sizeof(schar_T));
2995 }
2996
2997 update_screen(0);
2998 ret = 3;
2999
3000 if (must_redraw == 0)
3001 {
3002 int off = (int)(current_ScreenLine - ScreenLines);
3003
3004 // Restore the text displayed in the command line area.
3005 for (r = 0; r < rows; ++r)
3006 {
3007 mch_memmove(current_ScreenLine,
3008 screenline + r * cols,
3009 (size_t)cols * sizeof(schar_T));
3010 mch_memmove(ScreenAttrs + off,
3011 screenattr + r * cols,
3012 (size_t)cols * sizeof(sattr_T));
3013 if (enc_utf8)
3014 {
3015 mch_memmove(ScreenLinesUC + off,
3016 screenlineUC + r * cols,
3017 (size_t)cols * sizeof(u8char_T));
3018 for (i = 0; i < p_mco; ++i)
3019 mch_memmove(ScreenLinesC[i] + off,
3020 screenlineC[i] + r * cols,
3021 (size_t)cols * sizeof(u8char_T));
3022 }
3023 if (enc_dbcs == DBCS_JPNU)
3024 mch_memmove(ScreenLines2 + off,
3025 screenline2 + r * cols,
3026 (size_t)cols * sizeof(schar_T));
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003027 screen_line(curwin, cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003028 }
3029 ret = 4;
3030 }
3031 }
3032
3033 vim_free(screenline);
3034 vim_free(screenattr);
3035 if (enc_utf8)
3036 {
3037 vim_free(screenlineUC);
3038 for (i = 0; i < p_mco; ++i)
3039 vim_free(screenlineC[i]);
3040 }
3041 if (enc_dbcs == DBCS_JPNU)
3042 vim_free(screenline2);
3043
3044 // Show the intro message when appropriate.
3045 maybe_intro_message();
3046
3047 setcursor();
3048
3049 return ret;
3050}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003051
3052/*
3053 * Invoked after an asynchronous callback is called.
3054 * If an echo command was used the cursor needs to be put back where
3055 * it belongs. If highlighting was changed a redraw is needed.
3056 * If "call_update_screen" is FALSE don't call update_screen() when at the
3057 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003058 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003059 */
3060 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003061redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003062{
3063 ++redrawing_for_callback;
3064
Bram Moolenaar24959102022-05-07 20:01:16 +01003065 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3066 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3067 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003068 {
3069 if (do_message)
3070 repeat_message();
3071 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003072 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003073 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003074 if (pum_visible())
3075 cmdline_pum_display();
Bram Moolenaar54162322022-08-26 16:58:51 +01003076
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003077 // Don't redraw when in prompt_for_number().
3078 if (cmdline_row > 0)
3079 {
3080 // Redrawing only works when the screen didn't scroll. Don't clear
3081 // wildmenu entries.
3082 if (msg_scrolled == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003083 && wild_menu_showing == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003084 && call_update_screen)
3085 update_screen(0);
3086
3087 // Redraw in the same position, so that the user can continue
3088 // editing the command.
3089 redrawcmdline_ex(FALSE);
3090 }
3091 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003092 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003093 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003094 update_topline();
3095 validate_cursor();
3096
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003097 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003098 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003099 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003100
3101 if (msg_scrolled == 0)
3102 {
3103 // don't want a hit-enter prompt when something else is displayed
3104 msg_didany = FALSE;
3105 need_wait_return = FALSE;
3106 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003107 }
3108 cursor_on();
3109#ifdef FEAT_GUI
3110 if (gui.in_use && !gui_mch_is_blink_off())
3111 // Don't update the cursor when it is blinking and off to avoid
3112 // flicker.
3113 out_flush_cursor(FALSE, FALSE);
3114 else
3115#endif
3116 out_flush();
3117
3118 --redrawing_for_callback;
3119}
3120
3121/*
3122 * Redraw the current window later, with update_screen(type).
3123 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003124 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003125 */
3126 void
3127redraw_later(int type)
3128{
3129 redraw_win_later(curwin, type);
3130}
3131
3132 void
3133redraw_win_later(
3134 win_T *wp,
3135 int type)
3136{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003137 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003138 {
3139 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003140 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003141 wp->w_lines_valid = 0;
3142 if (must_redraw < type) // must_redraw is the maximum of all windows
3143 must_redraw = type;
3144 }
3145}
3146
3147/*
3148 * Force a complete redraw later. Also resets the highlighting. To be used
3149 * after executing a shell command that messes up the screen.
3150 */
3151 void
3152redraw_later_clear(void)
3153{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003154 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003155 reset_screen_attr();
3156}
3157
3158/*
Bram Moolenaar13608d82022-08-29 15:06:50 +01003159 * Mark all windows to be redrawn later. Except popup windows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003160 */
3161 void
3162redraw_all_later(int type)
3163{
3164 win_T *wp;
3165
3166 FOR_ALL_WINDOWS(wp)
3167 redraw_win_later(wp, type);
3168 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003169 set_must_redraw(type);
3170}
3171
Bram Moolenaar13608d82022-08-29 15:06:50 +01003172#if 0 // not actually used yet, it probably should
3173/*
3174 * Mark all windows, including popup windows, to be redrawn.
3175 */
3176 void
3177redraw_all_windows_later(int type)
3178{
3179 redraw_all_later(type);
3180#ifdef FEAT_PROP_POPUP
3181 popup_redraw_all(); // redraw all popup windows
3182#endif
3183}
3184#endif
3185
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003186/*
3187 * Set "must_redraw" to "type" unless it already has a higher value
3188 * or it is currently not allowed.
3189 */
3190 void
3191set_must_redraw(int type)
3192{
3193 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003194 must_redraw = type;
3195}
3196
3197/*
3198 * Mark all windows that are editing the current buffer to be updated later.
3199 */
3200 void
3201redraw_curbuf_later(int type)
3202{
3203 redraw_buf_later(curbuf, type);
3204}
3205
3206 void
3207redraw_buf_later(buf_T *buf, int type)
3208{
3209 win_T *wp;
3210
3211 FOR_ALL_WINDOWS(wp)
3212 {
3213 if (wp->w_buffer == buf)
3214 redraw_win_later(wp, type);
3215 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003216#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3217 // terminal in popup window is not in list of windows
3218 if (curwin->w_buffer == buf)
3219 redraw_win_later(curwin, type);
3220#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003221}
3222
3223#if defined(FEAT_SIGNS) || defined(PROTO)
3224 void
3225redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3226{
3227 win_T *wp;
3228
3229 FOR_ALL_WINDOWS(wp)
3230 if (wp->w_buffer == buf && lnum >= wp->w_topline
3231 && lnum < wp->w_botline)
3232 redrawWinline(wp, lnum);
3233}
3234#endif
3235
3236#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3237 void
3238redraw_buf_and_status_later(buf_T *buf, int type)
3239{
3240 win_T *wp;
3241
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003242 if (wild_menu_showing != 0)
3243 // Don't redraw while the command line completion is displayed, it
3244 // would disappear.
3245 return;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003246 FOR_ALL_WINDOWS(wp)
3247 {
3248 if (wp->w_buffer == buf)
3249 {
3250 redraw_win_later(wp, type);
3251 wp->w_redr_status = TRUE;
3252 }
3253 }
3254}
3255#endif
3256
3257/*
3258 * mark all status lines for redraw; used after first :cd
3259 */
3260 void
3261status_redraw_all(void)
3262{
3263 win_T *wp;
3264
3265 FOR_ALL_WINDOWS(wp)
3266 if (wp->w_status_height)
3267 {
3268 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003269 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003270 }
3271}
3272
3273/*
3274 * mark all status lines of the current buffer for redraw
3275 */
3276 void
3277status_redraw_curbuf(void)
3278{
3279 win_T *wp;
3280
3281 FOR_ALL_WINDOWS(wp)
3282 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3283 {
3284 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003285 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003286 }
3287}
3288
3289/*
3290 * Redraw all status lines that need to be redrawn.
3291 */
3292 void
3293redraw_statuslines(void)
3294{
3295 win_T *wp;
3296
3297 FOR_ALL_WINDOWS(wp)
3298 if (wp->w_redr_status)
3299 win_redr_status(wp, FALSE);
3300 if (redraw_tabline)
3301 draw_tabline();
3302}
3303
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003304/*
3305 * Redraw all status lines at the bottom of frame "frp".
3306 */
3307 void
3308win_redraw_last_status(frame_T *frp)
3309{
3310 if (frp->fr_layout == FR_LEAF)
3311 frp->fr_win->w_redr_status = TRUE;
3312 else if (frp->fr_layout == FR_ROW)
3313 {
3314 FOR_ALL_FRAMES(frp, frp->fr_child)
3315 win_redraw_last_status(frp);
3316 }
3317 else // frp->fr_layout == FR_COL
3318 {
3319 frp = frp->fr_child;
3320 while (frp->fr_next != NULL)
3321 frp = frp->fr_next;
3322 win_redraw_last_status(frp);
3323 }
3324}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003325
3326/*
3327 * Changed something in the current window, at buffer line "lnum", that
3328 * requires that line and possibly other lines to be redrawn.
3329 * Used when entering/leaving Insert mode with the cursor on a folded line.
3330 * Used to remove the "$" from a change command.
3331 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3332 * may become invalid and the whole window will have to be redrawn.
3333 */
3334 void
3335redrawWinline(
3336 win_T *wp,
3337 linenr_T lnum)
3338{
3339 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
3340 wp->w_redraw_top = lnum;
3341 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
3342 wp->w_redraw_bot = lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003343 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003344}