blob: fa5d6683df32370ce3b5b04a8f28233edc603932 [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
550#ifdef FEAT_CMDL_INFO
551 win_redr_ruler(wp, TRUE, ignore_pum);
552#endif
553 }
554
555 /*
556 * May need to draw the character below the vertical separator.
557 */
558 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
559 {
560 if (stl_connected(wp))
561 fillchar = fillchar_status(&attr, wp);
562 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +0100563 fillchar = fillchar_vsep(&attr, wp);
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200564 screen_putchar(fillchar, row, W_ENDCOL(wp), attr);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200565 }
566 busy = FALSE;
567}
568
569#ifdef FEAT_STL_OPT
570/*
571 * Redraw the status line according to 'statusline' and take care of any
572 * errors encountered.
573 */
574 static void
575redraw_custom_statusline(win_T *wp)
576{
577 static int entered = FALSE;
578 int saved_did_emsg = did_emsg;
579
580 // When called recursively return. This can happen when the statusline
581 // contains an expression that triggers a redraw.
582 if (entered)
583 return;
584 entered = TRUE;
585
586 did_emsg = FALSE;
587 win_redr_custom(wp, FALSE);
588 if (did_emsg)
589 {
590 // When there is an error disable the statusline, otherwise the
591 // display is messed up with errors and a redraw triggers the problem
592 // again and again.
593 set_string_option_direct((char_u *)"statusline", -1,
594 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
595 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
596 }
597 did_emsg |= saved_did_emsg;
598 entered = FALSE;
599}
600#endif
601
602/*
603 * Show current status info in ruler and various other places
604 * If always is FALSE, only show ruler if position has changed.
605 */
606 void
607showruler(int always)
608{
609 if (!always && !redrawing())
610 return;
611 if (pum_visible())
612 {
613 // Don't redraw right now, do it later.
614 curwin->w_redr_status = TRUE;
615 return;
616 }
617#if defined(FEAT_STL_OPT)
618 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
619 redraw_custom_statusline(curwin);
620 else
621#endif
622#ifdef FEAT_CMDL_INFO
623 win_redr_ruler(curwin, always, FALSE);
624#endif
625
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200626 if (need_maketitle
Bram Moolenaar651fca82021-11-29 20:39:38 +0000627#ifdef FEAT_STL_OPT
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200628 || (p_icon && (stl_syntax & STL_IN_ICON))
629 || (p_title && (stl_syntax & STL_IN_TITLE))
Bram Moolenaar651fca82021-11-29 20:39:38 +0000630#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200631 )
632 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +0000633
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200634 // Redraw the tab pages line if needed.
635 if (redraw_tabline)
636 draw_tabline();
637}
638
639#if defined(FEAT_CMDL_INFO) || defined(PROTO)
640 void
641win_redr_ruler(win_T *wp, int always, int ignore_pum)
642{
643#define RULER_BUF_LEN 70
644 char_u buffer[RULER_BUF_LEN];
645 int row;
646 int fillchar;
647 int attr;
648 int empty_line = FALSE;
649 colnr_T virtcol;
650 int i;
651 size_t len;
652 int o;
653 int this_ru_col;
654 int off = 0;
655 int width;
656
Bram Moolenaara2a89732022-08-31 14:46:18 +0100657 // If 'ruler' off don't do anything
658 if (!p_ru)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200659 return;
660
661 /*
662 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
663 * after deleting lines, before cursor.lnum is corrected.
664 */
665 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
666 return;
667
668 // Don't draw the ruler while doing insert-completion, it might overwrite
669 // the (long) mode message.
670 if (wp == lastwin && lastwin->w_status_height == 0)
671 if (edit_submode != NULL)
672 return;
673 // Don't draw the ruler when the popup menu is visible, it may overlap.
674 // Except when the popup menu will be redrawn anyway.
675 if (!ignore_pum && pum_visible())
676 return;
677
678#ifdef FEAT_STL_OPT
Bram Moolenaara2a89732022-08-31 14:46:18 +0100679 if (*p_ruf)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200680 {
Bram Moolenaar53989552019-12-23 22:59:18 +0100681 int called_emsg_before = called_emsg;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200682
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200683 win_redr_custom(wp, TRUE);
Bram Moolenaar53989552019-12-23 22:59:18 +0100684 if (called_emsg > called_emsg_before)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200685 set_string_option_direct((char_u *)"rulerformat", -1,
686 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200687 return;
688 }
689#endif
690
691 /*
692 * Check if not in Insert mode and the line is empty (will show "0-1").
693 */
Bram Moolenaar24959102022-05-07 20:01:16 +0100694 if ((State & MODE_INSERT) == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200695 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
696 empty_line = TRUE;
697
698 /*
699 * Only draw the ruler when something changed.
700 */
701 validate_virtcol_win(wp);
702 if ( redraw_cmdline
703 || always
704 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
705 || wp->w_cursor.col != wp->w_ru_cursor.col
706 || wp->w_virtcol != wp->w_ru_virtcol
707 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
708 || wp->w_topline != wp->w_ru_topline
709 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
710#ifdef FEAT_DIFF
711 || wp->w_topfill != wp->w_ru_topfill
712#endif
713 || empty_line != wp->w_ru_empty)
714 {
715 cursor_off();
716 if (wp->w_status_height)
717 {
Bram Moolenaarae0f1512021-03-30 22:12:12 +0200718 row = statusline_row(wp);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200719 fillchar = fillchar_status(&attr, wp);
720 off = wp->w_wincol;
721 width = wp->w_width;
722 }
723 else
724 {
725 row = Rows - 1;
726 fillchar = ' ';
727 attr = 0;
728 width = Columns;
729 off = 0;
730 }
731
732 // In list mode virtcol needs to be recomputed
733 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100734 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200735 {
736 wp->w_p_list = FALSE;
737 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
738 wp->w_p_list = TRUE;
739 }
740
741 /*
742 * Some sprintfs return the length, some return a pointer.
743 * To avoid portability problems we use strlen() here.
744 */
745 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
746 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
747 ? 0L
748 : (long)(wp->w_cursor.lnum));
749 len = STRLEN(buffer);
750 col_print(buffer + len, RULER_BUF_LEN - len,
751 empty_line ? 0 : (int)wp->w_cursor.col + 1,
752 (int)virtcol + 1);
753
754 /*
755 * Add a "50%" if there is room for it.
756 * On the last line, don't print in the last column (scrolls the
757 * screen up on some terminals).
758 */
759 i = (int)STRLEN(buffer);
760 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
761 o = i + vim_strsize(buffer + i + 1);
762 if (wp->w_status_height == 0) // can't use last char of screen
763 ++o;
764 this_ru_col = ru_col - (Columns - width);
765 if (this_ru_col < 0)
766 this_ru_col = 0;
767 // Never use more than half the window/screen width, leave the other
768 // half for the filename.
769 if (this_ru_col < (width + 1) / 2)
770 this_ru_col = (width + 1) / 2;
771 if (this_ru_col + o < width)
772 {
773 // need at least 3 chars left for get_rel_pos() + NUL
774 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
775 {
776 if (has_mbyte)
777 i += (*mb_char2bytes)(fillchar, buffer + i);
778 else
779 buffer[i++] = fillchar;
780 ++o;
781 }
782 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
783 }
784 // Truncate at window boundary.
785 if (has_mbyte)
786 {
787 o = 0;
788 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
789 {
790 o += (*mb_ptr2cells)(buffer + i);
791 if (this_ru_col + o > width)
792 {
793 buffer[i] = NUL;
794 break;
795 }
796 }
797 }
798 else if (this_ru_col + (int)STRLEN(buffer) > width)
799 buffer[width - this_ru_col] = NUL;
800
801 screen_puts(buffer, row, this_ru_col + off, attr);
802 i = redraw_cmdline;
803 screen_fill(row, row + 1,
804 this_ru_col + off + (int)STRLEN(buffer),
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000805 (off + width),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +0200806 fillchar, fillchar, attr);
807 // don't redraw the cmdline because of showing the ruler
808 redraw_cmdline = i;
809 wp->w_ru_cursor = wp->w_cursor;
810 wp->w_ru_virtcol = wp->w_virtcol;
811 wp->w_ru_empty = empty_line;
812 wp->w_ru_topline = wp->w_topline;
813 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
814#ifdef FEAT_DIFF
815 wp->w_ru_topfill = wp->w_topfill;
816#endif
817 }
818}
819#endif
820
821/*
822 * To be called when "updating_screen" was set before and now the postponed
823 * side effects may take place.
824 */
825 void
826after_updating_screen(int may_resize_shell UNUSED)
827{
828 updating_screen = FALSE;
829#ifdef FEAT_GUI
830 if (may_resize_shell)
831 gui_may_resize_shell();
832#endif
833#ifdef FEAT_TERMINAL
834 term_check_channel_closed_recently();
835#endif
836
837#ifdef HAVE_DROP_FILE
838 // If handle_drop() was called while updating_screen was TRUE need to
839 // handle the drop now.
840 handle_any_postponed_drop();
841#endif
842}
843
844/*
845 * Update all windows that are editing the current buffer.
846 */
847 void
848update_curbuf(int type)
849{
850 redraw_curbuf_later(type);
851 update_screen(type);
852}
853
854#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
855/*
856 * Copy "text" to ScreenLines using "attr".
857 * Returns the next screen column.
858 */
859 static int
860text_to_screenline(win_T *wp, char_u *text, int col)
861{
862 int off = (int)(current_ScreenLine - ScreenLines);
863
864 if (has_mbyte)
865 {
866 int cells;
867 int u8c, u8cc[MAX_MCO];
868 int i;
869 int idx;
870 int c_len;
871 char_u *p;
872# ifdef FEAT_ARABIC
873 int prev_c = 0; // previous Arabic character
874 int prev_c1 = 0; // first composing char for prev_c
875# endif
876
877# ifdef FEAT_RIGHTLEFT
878 if (wp->w_p_rl)
879 idx = off;
880 else
881# endif
882 idx = off + col;
883
884 // Store multibyte characters in ScreenLines[] et al. correctly.
885 for (p = text; *p != NUL; )
886 {
887 cells = (*mb_ptr2cells)(p);
888 c_len = (*mb_ptr2len)(p);
889 if (col + cells > wp->w_width
890# ifdef FEAT_RIGHTLEFT
891 - (wp->w_p_rl ? col : 0)
892# endif
893 )
894 break;
895 ScreenLines[idx] = *p;
896 if (enc_utf8)
897 {
898 u8c = utfc_ptr2char(p, u8cc);
899 if (*p < 0x80 && u8cc[0] == 0)
900 {
901 ScreenLinesUC[idx] = 0;
902#ifdef FEAT_ARABIC
903 prev_c = u8c;
904#endif
905 }
906 else
907 {
908#ifdef FEAT_ARABIC
909 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
910 {
911 // Do Arabic shaping.
912 int pc, pc1, nc;
913 int pcc[MAX_MCO];
914 int firstbyte = *p;
915
916 // The idea of what is the previous and next
917 // character depends on 'rightleft'.
918 if (wp->w_p_rl)
919 {
920 pc = prev_c;
921 pc1 = prev_c1;
922 nc = utf_ptr2char(p + c_len);
923 prev_c1 = u8cc[0];
924 }
925 else
926 {
927 pc = utfc_ptr2char(p + c_len, pcc);
928 nc = prev_c;
929 pc1 = pcc[0];
930 }
931 prev_c = u8c;
932
933 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
934 pc, pc1, nc);
935 ScreenLines[idx] = firstbyte;
936 }
937 else
938 prev_c = u8c;
939#endif
940 // Non-BMP character: display as ? or fullwidth ?.
941 ScreenLinesUC[idx] = u8c;
942 for (i = 0; i < Screen_mco; ++i)
943 {
944 ScreenLinesC[i][idx] = u8cc[i];
945 if (u8cc[i] == 0)
946 break;
947 }
948 }
949 if (cells > 1)
950 ScreenLines[idx + 1] = 0;
951 }
952 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
953 // double-byte single width character
954 ScreenLines2[idx] = p[1];
955 else if (cells > 1)
956 // double-width character
957 ScreenLines[idx + 1] = p[1];
958 col += cells;
959 idx += cells;
960 p += c_len;
961 }
962 }
963 else
964 {
965 int len = (int)STRLEN(text);
966
967 if (len > wp->w_width - col)
968 len = wp->w_width - col;
969 if (len > 0)
970 {
971#ifdef FEAT_RIGHTLEFT
972 if (wp->w_p_rl)
973 mch_memmove(current_ScreenLine, text, len);
974 else
975#endif
976 mch_memmove(current_ScreenLine + col, text, len);
977 col += len;
978 }
979 }
980 return col;
981}
982#endif
983
984#ifdef FEAT_MENU
985/*
986 * Draw the window toolbar.
987 */
988 static void
989redraw_win_toolbar(win_T *wp)
990{
991 vimmenu_T *menu;
992 int item_idx = 0;
993 int item_count = 0;
994 int col = 0;
995 int next_col;
996 int off = (int)(current_ScreenLine - ScreenLines);
997 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
998 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
999
1000 vim_free(wp->w_winbar_items);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001001 FOR_ALL_CHILD_MENUS(wp->w_winbar, menu)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001002 ++item_count;
1003 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
1004
1005 // TODO: use fewer spaces if there is not enough room
1006 for (menu = wp->w_winbar->children;
1007 menu != NULL && col < wp->w_width; menu = menu->next)
1008 {
1009 space_to_screenline(off + col, fill_attr);
1010 if (++col >= wp->w_width)
1011 break;
1012 if (col > 1)
1013 {
1014 space_to_screenline(off + col, fill_attr);
1015 if (++col >= wp->w_width)
1016 break;
1017 }
1018
1019 wp->w_winbar_items[item_idx].wb_startcol = col;
1020 space_to_screenline(off + col, button_attr);
1021 if (++col >= wp->w_width)
1022 break;
1023
1024 next_col = text_to_screenline(wp, menu->name, col);
1025 while (col < next_col)
1026 {
1027 ScreenAttrs[off + col] = button_attr;
1028 ++col;
1029 }
1030 wp->w_winbar_items[item_idx].wb_endcol = col;
1031 wp->w_winbar_items[item_idx].wb_menu = menu;
1032 ++item_idx;
1033
1034 if (col >= wp->w_width)
1035 break;
1036 space_to_screenline(off + col, button_attr);
1037 ++col;
1038 }
1039 while (col < wp->w_width)
1040 {
1041 space_to_screenline(off + col, fill_attr);
1042 ++col;
1043 }
1044 wp->w_winbar_items[item_idx].wb_menu = NULL; // end marker
1045
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001046 screen_line(wp, wp->w_winrow, wp->w_wincol, wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001047}
1048#endif
1049
1050#if defined(FEAT_FOLDING) || defined(PROTO)
1051/*
1052 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
1053 */
1054 static void
1055copy_text_attr(
1056 int off,
1057 char_u *buf,
1058 int len,
1059 int attr)
1060{
1061 int i;
1062
1063 mch_memmove(ScreenLines + off, buf, (size_t)len);
1064 if (enc_utf8)
1065 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
1066 for (i = 0; i < len; ++i)
1067 ScreenAttrs[off + i] = attr;
1068}
1069
1070/*
1071 * Display one folded line.
1072 */
1073 static void
1074fold_line(
1075 win_T *wp,
1076 long fold_count,
1077 foldinfo_T *foldinfo,
1078 linenr_T lnum,
1079 int row)
1080{
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001081 // Max value of 'foldcolumn' is 12 and maximum number of bytes in a
1082 // multi-byte character is MAX_MCO.
1083 char_u buf[MAX_MCO * 12 + 1];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001084 pos_T *top, *bot;
1085 linenr_T lnume = lnum + fold_count - 1;
1086 int len;
1087 char_u *text;
1088 int fdc;
1089 int col;
1090 int txtcol;
1091 int off = (int)(current_ScreenLine - ScreenLines);
1092 int ri;
1093
1094 // Build the fold line:
1095 // 1. Add the cmdwin_type for the command-line window
1096 // 2. Add the 'foldcolumn'
1097 // 3. Add the 'number' or 'relativenumber' column
1098 // 4. Compose the text
1099 // 5. Add the text
1100 // 6. set highlighting for the Visual area an other text
1101 col = 0;
1102
1103 // 1. Add the cmdwin_type for the command-line window
1104 // Ignores 'rightleft', this window is never right-left.
1105#ifdef FEAT_CMDWIN
1106 if (cmdwin_type != 0 && wp == curwin)
1107 {
1108 ScreenLines[off] = cmdwin_type;
1109 ScreenAttrs[off] = HL_ATTR(HLF_AT);
1110 if (enc_utf8)
1111 ScreenLinesUC[off] = 0;
1112 ++col;
1113 }
1114#endif
1115
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001116#ifdef FEAT_RIGHTLEFT
1117# define RL_MEMSET(p, v, l) \
1118 do { \
1119 if (wp->w_p_rl) \
kylo252ae6f1d82022-02-16 19:24:07 +00001120 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001121 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
1122 else \
kylo252ae6f1d82022-02-16 19:24:07 +00001123 for (ri = 0; ri < (l); ++ri) \
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001124 ScreenAttrs[off + (p) + ri] = v; \
1125 } while (0)
1126#else
1127# define RL_MEMSET(p, v, l) \
1128 do { \
1129 for (ri = 0; ri < l; ++ri) \
1130 ScreenAttrs[off + (p) + ri] = v; \
1131 } while (0)
1132#endif
1133
Bram Moolenaar4fa11752021-03-03 13:26:02 +01001134 // 2. Add the 'foldcolumn'
1135 // Reduce the width when there is not enough space.
1136 fdc = compute_foldcolumn(wp, col);
1137 if (fdc > 0)
1138 {
1139 char_u *p;
1140 int i;
1141 int idx;
1142
1143 fill_foldcolumn(buf, wp, TRUE, lnum);
1144 p = buf;
1145 for (i = 0; i < fdc; i++)
1146 {
1147 int ch;
1148
1149 if (has_mbyte)
1150 ch = mb_ptr2char_adv(&p);
1151 else
1152 ch = *p++;
1153#ifdef FEAT_RIGHTLEFT
1154 if (wp->w_p_rl)
1155 idx = off + wp->w_width - i - 1 - col;
1156 else
1157#endif
1158 idx = off + col + i;
1159 if (enc_utf8)
1160 {
1161 if (ch >= 0x80)
1162 {
1163 ScreenLinesUC[idx] = ch;
1164 ScreenLinesC[0][idx] = 0;
1165 ScreenLines[idx] = 0x80;
1166 }
1167 else
1168 {
1169 ScreenLines[idx] = ch;
1170 ScreenLinesUC[idx] = 0;
1171 }
1172 }
1173 else
1174 ScreenLines[idx] = ch;
1175 }
1176
1177 RL_MEMSET(col, HL_ATTR(HLF_FC), fdc);
1178 col += fdc;
1179 }
1180
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001181 // Set all attributes of the 'number' or 'relativenumber' column and the
1182 // text
1183 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
1184
1185#ifdef FEAT_SIGNS
1186 // If signs are being displayed, add two spaces.
1187 if (signcolumn_on(wp))
1188 {
1189 len = wp->w_width - col;
1190 if (len > 0)
1191 {
1192 if (len > 2)
1193 len = 2;
1194# ifdef FEAT_RIGHTLEFT
1195 if (wp->w_p_rl)
1196 // the line number isn't reversed
1197 copy_text_attr(off + wp->w_width - len - col,
1198 (char_u *)" ", len, HL_ATTR(HLF_FL));
1199 else
1200# endif
1201 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
1202 col += len;
1203 }
1204 }
1205#endif
1206
1207 // 3. Add the 'number' or 'relativenumber' column
1208 if (wp->w_p_nu || wp->w_p_rnu)
1209 {
1210 len = wp->w_width - col;
1211 if (len > 0)
1212 {
1213 int w = number_width(wp);
1214 long num;
1215 char *fmt = "%*ld ";
1216
1217 if (len > w + 1)
1218 len = w + 1;
1219
1220 if (wp->w_p_nu && !wp->w_p_rnu)
1221 // 'number' + 'norelativenumber'
1222 num = (long)lnum;
1223 else
1224 {
1225 // 'relativenumber', don't use negative numbers
1226 num = labs((long)get_cursor_rel_lnum(wp, lnum));
1227 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
1228 {
1229 // 'number' + 'relativenumber': cursor line shows absolute
1230 // line number
1231 num = lnum;
1232 fmt = "%-*ld ";
1233 }
1234 }
1235
1236 sprintf((char *)buf, fmt, w, num);
1237#ifdef FEAT_RIGHTLEFT
1238 if (wp->w_p_rl)
1239 // the line number isn't reversed
1240 copy_text_attr(off + wp->w_width - len - col, buf, len,
1241 HL_ATTR(HLF_FL));
1242 else
1243#endif
1244 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
1245 col += len;
1246 }
1247 }
1248
1249 // 4. Compose the folded-line string with 'foldtext', if set.
1250 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
1251
1252 txtcol = col; // remember where text starts
1253
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001254 // 5. move the text to current_ScreenLine. Fill up with "fold" from
1255 // 'fillchars'.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001256 // Right-left text is put in columns 0 - number-col, normal text is put
1257 // in columns number-col - window-width.
1258 col = text_to_screenline(wp, text, col);
1259
1260 // Fill the rest of the line with the fold filler
1261#ifdef FEAT_RIGHTLEFT
1262 if (wp->w_p_rl)
1263 col -= txtcol;
1264#endif
1265 while (col < wp->w_width
1266#ifdef FEAT_RIGHTLEFT
1267 - (wp->w_p_rl ? txtcol : 0)
1268#endif
1269 )
1270 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001271 int c = wp->w_fill_chars.fold;
1272
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001273 if (enc_utf8)
1274 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001275 if (c >= 0x80)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001276 {
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001277 ScreenLinesUC[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001278 ScreenLinesC[0][off + col] = 0;
1279 ScreenLines[off + col] = 0x80; // avoid storing zero
1280 }
1281 else
1282 {
1283 ScreenLinesUC[off + col] = 0;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001284 ScreenLines[off + col] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001285 }
1286 col++;
1287 }
1288 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001289 ScreenLines[off + col++] = c;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001290 }
1291
1292 if (text != buf)
1293 vim_free(text);
1294
1295 // 6. set highlighting for the Visual area an other text.
1296 // If all folded lines are in the Visual area, highlight the line.
1297 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
1298 {
1299 if (LTOREQ_POS(curwin->w_cursor, VIsual))
1300 {
1301 // Visual is after curwin->w_cursor
1302 top = &curwin->w_cursor;
1303 bot = &VIsual;
1304 }
1305 else
1306 {
1307 // Visual is before curwin->w_cursor
1308 top = &VIsual;
1309 bot = &curwin->w_cursor;
1310 }
1311 if (lnum >= top->lnum
1312 && lnume <= bot->lnum
1313 && (VIsual_mode != 'v'
1314 || ((lnum > top->lnum
1315 || (lnum == top->lnum
1316 && top->col == 0))
1317 && (lnume < bot->lnum
1318 || (lnume == bot->lnum
1319 && (bot->col - (*p_sel == 'e'))
1320 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
1321 {
1322 if (VIsual_mode == Ctrl_V)
1323 {
1324 // Visual block mode: highlight the chars part of the block
1325 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
1326 {
1327 if (wp->w_old_cursor_lcol != MAXCOL
1328 && wp->w_old_cursor_lcol + txtcol
1329 < (colnr_T)wp->w_width)
1330 len = wp->w_old_cursor_lcol;
1331 else
1332 len = wp->w_width - txtcol;
1333 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
1334 len - (int)wp->w_old_cursor_fcol);
1335 }
1336 }
1337 else
1338 {
1339 // Set all attributes of the text
1340 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
1341 }
1342 }
1343 }
1344
1345#ifdef FEAT_SYN_HL
1346 // Show colorcolumn in the fold line, but let cursorcolumn override it.
1347 if (wp->w_p_cc_cols)
1348 {
1349 int i = 0;
1350 int j = wp->w_p_cc_cols[i];
1351 int old_txtcol = txtcol;
1352
1353 while (j > -1)
1354 {
1355 txtcol += j;
1356 if (wp->w_p_wrap)
1357 txtcol -= wp->w_skipcol;
1358 else
1359 txtcol -= wp->w_leftcol;
1360 if (txtcol >= 0 && txtcol < wp->w_width)
1361 ScreenAttrs[off + txtcol] = hl_combine_attr(
1362 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
1363 txtcol = old_txtcol;
1364 j = wp->w_p_cc_cols[++i];
1365 }
1366 }
1367
1368 // Show 'cursorcolumn' in the fold line.
1369 if (wp->w_p_cuc)
1370 {
1371 txtcol += wp->w_virtcol;
1372 if (wp->w_p_wrap)
1373 txtcol -= wp->w_skipcol;
1374 else
1375 txtcol -= wp->w_leftcol;
1376 if (txtcol >= 0 && txtcol < wp->w_width)
1377 ScreenAttrs[off + txtcol] = hl_combine_attr(
1378 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
1379 }
1380#endif
1381
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001382 screen_line(wp, row + W_WINROW(wp), wp->w_wincol,
1383 wp->w_width, wp->w_width, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001384
1385 // Update w_cline_height and w_cline_folded if the cursor line was
1386 // updated (saves a call to plines() later).
1387 if (wp == curwin
1388 && lnum <= curwin->w_cursor.lnum
1389 && lnume >= curwin->w_cursor.lnum)
1390 {
1391 curwin->w_cline_row = row;
1392 curwin->w_cline_height = 1;
1393 curwin->w_cline_folded = TRUE;
1394 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
1395 }
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001396
1397# ifdef FEAT_CONCEAL
1398 // When the line was not folded w_wrow may have been set, recompute it.
Bram Moolenaar5cb09622021-07-05 22:03:04 +02001399 if (wp == curwin
1400 && wp->w_cursor.lnum >= lnum
1401 && wp->w_cursor.lnum <= lnume
1402 && conceal_cursor_line(wp))
Bram Moolenaar00aaa512021-07-03 18:04:11 +02001403 curs_columns(TRUE);
1404# endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001405}
1406#endif
1407
1408/*
1409 * Update a single window.
1410 *
1411 * This may cause the windows below it also to be redrawn (when clearing the
1412 * screen or scrolling lines).
1413 *
1414 * How the window is redrawn depends on wp->w_redr_type. Each type also
1415 * implies the one below it.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001416 * UPD_NOT_VALID redraw the whole window
1417 * UPD_SOME_VALID redraw the whole window but do scroll when possible
1418 * UPD_REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like
1419 * UPD_VALID
1420 * UPD_INVERTED redraw the changed part of the Visual area
1421 * UPD_INVERTED_ALL redraw the whole Visual area
1422 * UPD_VALID 1. scroll up/down to adjust for a changed w_topline
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001423 * 2. update lines at the top when scrolled down
1424 * 3. redraw changed text:
1425 * - if wp->w_buffer->b_mod_set set, update lines between
1426 * b_mod_top and b_mod_bot.
1427 * - if wp->w_redraw_top non-zero, redraw lines between
1428 * wp->w_redraw_top and wp->w_redr_bot.
1429 * - continue redrawing when syntax status is invalid.
1430 * 4. if scrolled up, update lines at the bottom.
1431 * This results in three areas that may need updating:
1432 * top: from first row to top_end (when scrolled down)
1433 * mid: from mid_start to mid_end (update inversion or changed text)
1434 * bot: from bot_start to last row (when scrolled up)
1435 */
1436 static void
1437win_update(win_T *wp)
1438{
1439 buf_T *buf = wp->w_buffer;
1440 int type;
1441 int top_end = 0; // Below last row of the top area that needs
1442 // updating. 0 when no top area updating.
1443 int mid_start = 999;// first row of the mid area that needs
1444 // updating. 999 when no mid area updating.
1445 int mid_end = 0; // Below last row of the mid area that needs
1446 // updating. 0 when no mid area updating.
1447 int bot_start = 999;// first row of the bot area that needs
1448 // updating. 999 when no bot area updating
1449 int scrolled_down = FALSE; // TRUE when scrolled down when
1450 // w_topline got smaller a bit
1451#ifdef FEAT_SEARCH_EXTRA
1452 int top_to_mod = FALSE; // redraw above mod_top
1453#endif
1454
1455 int row; // current window row to display
1456 linenr_T lnum; // current buffer lnum to display
1457 int idx; // current index in w_lines[]
1458 int srow; // starting row of the current line
1459
1460 int eof = FALSE; // if TRUE, we hit the end of the file
1461 int didline = FALSE; // if TRUE, we finished the last line
1462 int i;
1463 long j;
1464 static int recursive = FALSE; // being called recursively
Bram Moolenaarcbee6352019-11-12 20:49:15 +01001465 linenr_T old_botline = wp->w_botline;
1466#ifdef FEAT_CONCEAL
1467 int old_wrow = wp->w_wrow;
1468 int old_wcol = wp->w_wcol;
1469#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001470#ifdef FEAT_FOLDING
1471 long fold_count;
1472#endif
1473#ifdef FEAT_SYN_HL
1474 // remember what happened to the previous line, to know if
1475 // check_visual_highlight() can be used
Bram Moolenaare7a74d52022-03-19 11:10:15 +00001476# define DID_NONE 1 // didn't update a line
1477# define DID_LINE 2 // updated a normal line
1478# define DID_FOLD 3 // updated a folded line
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001479 int did_update = DID_NONE;
1480 linenr_T syntax_last_parsed = 0; // last parsed text line
1481#endif
1482 linenr_T mod_top = 0;
1483 linenr_T mod_bot = 0;
1484#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1485 int save_got_int;
1486#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001487
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001488#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
1489 // This needs to be done only for the first window when update_screen() is
1490 // called.
1491 if (!did_update_one_window)
1492 {
1493 did_update_one_window = TRUE;
1494# ifdef FEAT_SEARCH_EXTRA
1495 start_search_hl();
1496# endif
1497# ifdef FEAT_CLIPBOARD
1498 // When Visual area changed, may have to update selection.
1499 if (clip_star.available && clip_isautosel_star())
1500 clip_update_selection(&clip_star);
1501 if (clip_plus.available && clip_isautosel_plus())
1502 clip_update_selection(&clip_plus);
1503# endif
1504 }
1505#endif
1506
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001507 type = wp->w_redr_type;
1508
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001509 if (type == UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001510 {
1511 wp->w_redr_status = TRUE;
1512 wp->w_lines_valid = 0;
1513 }
1514
Bram Moolenaarae0f1512021-03-30 22:12:12 +02001515 // Window frame is zero-height: nothing to draw.
1516 if (wp->w_height + WINBAR_HEIGHT(wp) == 0
1517 || (wp->w_frame->fr_height == wp->w_status_height
1518#if defined(FEAT_PROP_POPUP)
1519 && !popup_is_popup(wp)
1520#endif
1521 ))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001522 {
1523 wp->w_redr_type = 0;
1524 return;
1525 }
1526
1527 // Window is zero-width: Only need to draw the separator.
1528 if (wp->w_width == 0)
1529 {
1530 // draw the vertical separator right of this window
1531 draw_vsep_win(wp, 0);
1532 wp->w_redr_type = 0;
1533 return;
1534 }
1535
1536#ifdef FEAT_TERMINAL
1537 // If this window contains a terminal, redraw works completely differently.
1538 if (term_do_update_window(wp))
1539 {
1540 term_update_window(wp);
1541# ifdef FEAT_MENU
1542 // Draw the window toolbar, if there is one.
1543 if (winbar_height(wp) > 0)
1544 redraw_win_toolbar(wp);
1545# endif
1546 wp->w_redr_type = 0;
1547 return;
1548 }
1549#endif
1550
1551#ifdef FEAT_SEARCH_EXTRA
1552 init_search_hl(wp, &screen_search_hl);
1553#endif
1554
Bram Moolenaarb6aab8f2022-10-03 20:01:16 +01001555 // Make sure skipcol is valid, it depends on various options and the window
1556 // width.
1557 if (wp->w_skipcol > 0)
1558 {
1559 int w = 0;
1560 int width1 = wp->w_width - win_col_off(wp);
1561 int width2 = width1 + win_col_off2(wp);
1562 int add = width1;
1563
1564 while (w < wp->w_skipcol)
1565 {
1566 if (w > 0)
1567 add = width2;
1568 w += add;
1569 }
1570 if (w != wp->w_skipcol)
1571 // always round down, the higher value may not be valid
1572 wp->w_skipcol = w - add;
1573 }
1574
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001575#ifdef FEAT_LINEBREAK
1576 // Force redraw when width of 'number' or 'relativenumber' column
1577 // changes.
1578 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
1579 if (wp->w_nrwidth != i)
1580 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001581 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001582 wp->w_nrwidth = i;
1583 }
1584 else
1585#endif
1586
1587 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1588 {
1589 // When there are both inserted/deleted lines and specific lines to be
1590 // redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1591 // everything (only happens when redrawing is off for while).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001592 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001593 }
1594 else
1595 {
1596 // Set mod_top to the first line that needs displaying because of
1597 // changes. Set mod_bot to the first line after the changes.
1598 mod_top = wp->w_redraw_top;
1599 if (wp->w_redraw_bot != 0)
1600 mod_bot = wp->w_redraw_bot + 1;
1601 else
1602 mod_bot = 0;
1603 if (buf->b_mod_set)
1604 {
1605 if (mod_top == 0 || mod_top > buf->b_mod_top)
1606 {
1607 mod_top = buf->b_mod_top;
1608#ifdef FEAT_SYN_HL
1609 // Need to redraw lines above the change that may be included
1610 // in a pattern match.
1611 if (syntax_present(wp))
1612 {
1613 mod_top -= buf->b_s.b_syn_sync_linebreaks;
1614 if (mod_top < 1)
1615 mod_top = 1;
1616 }
1617#endif
1618 }
1619 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1620 mod_bot = buf->b_mod_bot;
1621
1622#ifdef FEAT_SEARCH_EXTRA
1623 // When 'hlsearch' is on and using a multi-line search pattern, a
1624 // change in one line may make the Search highlighting in a
1625 // previous line invalid. Simple solution: redraw all visible
1626 // lines above the change.
1627 // Same for a match pattern.
1628 if (screen_search_hl.rm.regprog != NULL
1629 && re_multiline(screen_search_hl.rm.regprog))
1630 top_to_mod = TRUE;
1631 else
1632 {
1633 matchitem_T *cur = wp->w_match_head;
1634
1635 while (cur != NULL)
1636 {
Bram Moolenaar50faf022022-09-29 12:50:17 +01001637 if (cur->mit_match.regprog != NULL
1638 && re_multiline(cur->mit_match.regprog))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001639 {
1640 top_to_mod = TRUE;
1641 break;
1642 }
Bram Moolenaar50faf022022-09-29 12:50:17 +01001643 cur = cur->mit_next;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001644 }
1645 }
1646#endif
1647 }
Bram Moolenaar368137a2022-05-31 13:43:12 +01001648
1649#ifdef FEAT_SEARCH_EXTRA
1650 if (search_hl_has_cursor_lnum > 0)
1651 {
1652 // CurSearch was used last time, need to redraw the line with it to
1653 // avoid having two matches highlighted with CurSearch.
1654 if (mod_top == 0 || mod_top > search_hl_has_cursor_lnum)
1655 mod_top = search_hl_has_cursor_lnum;
1656 if (mod_bot == 0 || mod_bot < search_hl_has_cursor_lnum + 1)
1657 mod_bot = search_hl_has_cursor_lnum + 1;
1658 }
1659#endif
1660
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001661#ifdef FEAT_FOLDING
1662 if (mod_top != 0 && hasAnyFolding(wp))
1663 {
1664 linenr_T lnumt, lnumb;
1665
1666 // A change in a line can cause lines above it to become folded or
1667 // unfolded. Find the top most buffer line that may be affected.
1668 // If the line was previously folded and displayed, get the first
1669 // line of that fold. If the line is folded now, get the first
1670 // folded line. Use the minimum of these two.
1671
1672 // Find last valid w_lines[] entry above mod_top. Set lnumt to
1673 // the line below it. If there is no valid entry, use w_topline.
1674 // Find the first valid w_lines[] entry below mod_bot. Set lnumb
1675 // to this line. If there is no valid entry, use MAXLNUM.
1676 lnumt = wp->w_topline;
1677 lnumb = MAXLNUM;
1678 for (i = 0; i < wp->w_lines_valid; ++i)
1679 if (wp->w_lines[i].wl_valid)
1680 {
1681 if (wp->w_lines[i].wl_lastlnum < mod_top)
1682 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1683 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1684 {
1685 lnumb = wp->w_lines[i].wl_lnum;
1686 // When there is a fold column it might need updating
1687 // in the next line ("J" just above an open fold).
1688 if (compute_foldcolumn(wp, 0) > 0)
1689 ++lnumb;
1690 }
1691 }
1692
1693 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1694 if (mod_top > lnumt)
1695 mod_top = lnumt;
1696
1697 // Now do the same for the bottom line (one above mod_bot).
1698 --mod_bot;
1699 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1700 ++mod_bot;
1701 if (mod_bot < lnumb)
1702 mod_bot = lnumb;
1703 }
1704#endif
1705
1706 // When a change starts above w_topline and the end is below
1707 // w_topline, start redrawing at w_topline.
1708 // If the end of the change is above w_topline: do like no change was
1709 // made, but redraw the first line to find changes in syntax.
1710 if (mod_top != 0 && mod_top < wp->w_topline)
1711 {
1712 if (mod_bot > wp->w_topline)
1713 mod_top = wp->w_topline;
1714#ifdef FEAT_SYN_HL
1715 else if (syntax_present(wp))
1716 top_end = 1;
1717#endif
1718 }
1719
1720 // When line numbers are displayed need to redraw all lines below
1721 // inserted/deleted lines.
1722 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1723 mod_bot = MAXLNUM;
1724 }
1725 wp->w_redraw_top = 0; // reset for next time
1726 wp->w_redraw_bot = 0;
Bram Moolenaar368137a2022-05-31 13:43:12 +01001727#ifdef FEAT_SEARCH_EXTRA
1728 search_hl_has_cursor_lnum = 0;
1729#endif
1730
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001731
1732 // When only displaying the lines at the top, set top_end. Used when
1733 // window has scrolled down for msg_scrolled.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001734 if (type == UPD_REDRAW_TOP)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001735 {
1736 j = 0;
1737 for (i = 0; i < wp->w_lines_valid; ++i)
1738 {
1739 j += wp->w_lines[i].wl_size;
1740 if (j >= wp->w_upd_rows)
1741 {
1742 top_end = j;
1743 break;
1744 }
1745 }
1746 if (top_end == 0)
1747 // not found (cannot happen?): redraw everything
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001748 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001749 else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001750 // top area defined, the rest is UPD_VALID
1751 type = UPD_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001752 }
1753
1754 // Trick: we want to avoid clearing the screen twice. screenclear() will
1755 // set "screen_cleared" to TRUE. The special value MAYBE (which is still
1756 // non-zero and thus not FALSE) will indicate that screenclear() was not
1757 // called.
1758 if (screen_cleared)
1759 screen_cleared = MAYBE;
1760
1761 // If there are no changes on the screen that require a complete redraw,
1762 // handle three cases:
1763 // 1: we are off the top of the screen by a few lines: scroll down
1764 // 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1765 // 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1766 // w_lines[] that needs updating.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001767 if ((type == UPD_VALID || type == UPD_SOME_VALID
1768 || type == UPD_INVERTED || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001769#ifdef FEAT_DIFF
1770 && !wp->w_botfill && !wp->w_old_botfill
1771#endif
1772 )
1773 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001774 if (mod_top != 0
1775 && wp->w_topline == mod_top
1776 && (!wp->w_lines[0].wl_valid
Bram Moolenaarabb6fbd2022-03-25 15:42:27 +00001777 || wp->w_topline == wp->w_lines[0].wl_lnum))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001778 {
Bram Moolenaarf8992d42020-08-01 19:14:13 +02001779 // w_topline is the first changed line and window is not scrolled,
1780 // the scrolling from changed lines will be done further down.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001781 }
1782 else if (wp->w_lines[0].wl_valid
1783 && (wp->w_topline < wp->w_lines[0].wl_lnum
1784#ifdef FEAT_DIFF
1785 || (wp->w_topline == wp->w_lines[0].wl_lnum
1786 && wp->w_topfill > wp->w_old_topfill)
1787#endif
1788 ))
1789 {
1790 // New topline is above old topline: May scroll down.
1791#ifdef FEAT_FOLDING
1792 if (hasAnyFolding(wp))
1793 {
1794 linenr_T ln;
1795
1796 // count the number of lines we are off, counting a sequence
1797 // of folded lines as one
1798 j = 0;
1799 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1800 {
1801 ++j;
1802 if (j >= wp->w_height - 2)
1803 break;
1804 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1805 }
1806 }
1807 else
1808#endif
1809 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1810 if (j < wp->w_height - 2) // not too far off
1811 {
1812 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1813#ifdef FEAT_DIFF
1814 // insert extra lines for previously invisible filler lines
1815 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1816 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1817 - wp->w_old_topfill;
1818#endif
1819 if (i < wp->w_height - 2) // less than a screen off
1820 {
1821 // Try to insert the correct number of lines.
1822 // If not the last window, delete the lines at the bottom.
1823 // win_ins_lines may fail when the terminal can't do it.
1824 if (i > 0)
1825 check_for_delay(FALSE);
1826 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1827 {
1828 if (wp->w_lines_valid != 0)
1829 {
1830 // Need to update rows that are new, stop at the
1831 // first one that scrolled down.
1832 top_end = i;
1833 scrolled_down = TRUE;
1834
1835 // Move the entries that were scrolled, disable
1836 // the entries for the lines to be redrawn.
1837 if ((wp->w_lines_valid += j) > wp->w_height)
1838 wp->w_lines_valid = wp->w_height;
Bram Moolenaara2a89732022-08-31 14:46:18 +01001839 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001840 wp->w_lines[idx] = wp->w_lines[idx - j];
1841 while (idx >= 0)
1842 wp->w_lines[idx--].wl_valid = FALSE;
1843 }
1844 }
1845 else
1846 mid_start = 0; // redraw all lines
1847 }
1848 else
1849 mid_start = 0; // redraw all lines
1850 }
1851 else
1852 mid_start = 0; // redraw all lines
1853 }
1854 else
1855 {
1856 // New topline is at or below old topline: May scroll up.
1857 // When topline didn't change, find first entry in w_lines[] that
1858 // needs updating.
1859
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001860 // Try to find wp->w_topline in wp->w_lines[].wl_lnum. The check
1861 // for "Rows" is in case "wl_size" is incorrect somehow.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001862 j = -1;
1863 row = 0;
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001864 for (i = 0; i < wp->w_lines_valid && i < Rows; i++)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001865 {
1866 if (wp->w_lines[i].wl_valid
1867 && wp->w_lines[i].wl_lnum == wp->w_topline)
1868 {
1869 j = i;
1870 break;
1871 }
1872 row += wp->w_lines[i].wl_size;
1873 }
1874 if (j == -1)
1875 {
1876 // if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1877 // lines
1878 mid_start = 0;
1879 }
1880 else
1881 {
1882 // Try to delete the correct number of lines.
1883 // wp->w_topline is at wp->w_lines[i].wl_lnum.
1884#ifdef FEAT_DIFF
1885 // If the topline didn't change, delete old filler lines,
1886 // otherwise delete filler lines of the new topline...
1887 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1888 row += wp->w_old_topfill;
1889 else
1890 row += diff_check_fill(wp, wp->w_topline);
1891 // ... but don't delete new filler lines.
1892 row -= wp->w_topfill;
1893#endif
Bram Moolenaarf6ebc822022-01-16 13:58:33 +00001894 if (row > Rows) // just in case
1895 row = Rows;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001896 if (row > 0)
1897 {
1898 check_for_delay(FALSE);
1899 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1900 == OK)
1901 bot_start = wp->w_height - row;
1902 else
1903 mid_start = 0; // redraw all lines
1904 }
1905 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1906 {
1907 // Skip the lines (below the deleted lines) that are still
1908 // valid and don't need redrawing. Copy their info
1909 // upwards, to compensate for the deleted lines. Set
1910 // bot_start to the first row that needs redrawing.
1911 bot_start = 0;
1912 idx = 0;
1913 for (;;)
1914 {
1915 wp->w_lines[idx] = wp->w_lines[j];
1916 // stop at line that didn't fit, unless it is still
1917 // valid (no lines deleted)
1918 if (row > 0 && bot_start + row
1919 + (int)wp->w_lines[j].wl_size > wp->w_height)
1920 {
1921 wp->w_lines_valid = idx + 1;
1922 break;
1923 }
1924 bot_start += wp->w_lines[idx++].wl_size;
1925
1926 // stop at the last valid entry in w_lines[].wl_size
1927 if (++j >= wp->w_lines_valid)
1928 {
1929 wp->w_lines_valid = idx;
1930 break;
1931 }
1932 }
1933#ifdef FEAT_DIFF
1934 // Correct the first entry for filler lines at the top
1935 // when it won't get updated below.
1936 if (wp->w_p_diff && bot_start > 0)
1937 wp->w_lines[0].wl_size =
1938 plines_win_nofill(wp, wp->w_topline, TRUE)
1939 + wp->w_topfill;
1940#endif
1941 }
1942 }
1943 }
1944
Bram Moolenaar13608d82022-08-29 15:06:50 +01001945 // When starting redraw in the first line, redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001946 if (mid_start == 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001947 mid_end = wp->w_height;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001948
1949 // When win_del_lines() or win_ins_lines() caused the screen to be
1950 // cleared (only happens for the first window) or when screenclear()
1951 // was called directly above, "must_redraw" will have been set to
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001952 // UPD_NOT_VALID, need to reset it here to avoid redrawing twice.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001953 if (screen_cleared == TRUE)
1954 must_redraw = 0;
1955 }
1956 else
1957 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001958 // Not UPD_VALID or UPD_INVERTED: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001959 mid_start = 0;
1960 mid_end = wp->w_height;
1961 }
1962
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001963 if (type == UPD_SOME_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001964 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001965 // UPD_SOME_VALID: redraw all lines.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001966 mid_start = 0;
1967 mid_end = wp->w_height;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001968 type = UPD_NOT_VALID;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001969 }
1970
1971 // check if we are updating or removing the inverted part
1972 if ((VIsual_active && buf == curwin->w_buffer)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001973 || (wp->w_old_cursor_lnum != 0 && type != UPD_NOT_VALID))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001974 {
1975 linenr_T from, to;
1976
1977 if (VIsual_active)
1978 {
Bram Moolenaarfe154992022-03-22 20:42:12 +00001979 if (VIsual_mode != wp->w_old_visual_mode
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001980 || type == UPD_INVERTED_ALL)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02001981 {
1982 // If the type of Visual selection changed, redraw the whole
1983 // selection. Also when the ownership of the X selection is
1984 // gained or lost.
1985 if (curwin->w_cursor.lnum < VIsual.lnum)
1986 {
1987 from = curwin->w_cursor.lnum;
1988 to = VIsual.lnum;
1989 }
1990 else
1991 {
1992 from = VIsual.lnum;
1993 to = curwin->w_cursor.lnum;
1994 }
1995 // redraw more when the cursor moved as well
1996 if (wp->w_old_cursor_lnum < from)
1997 from = wp->w_old_cursor_lnum;
1998 if (wp->w_old_cursor_lnum > to)
1999 to = wp->w_old_cursor_lnum;
2000 if (wp->w_old_visual_lnum < from)
2001 from = wp->w_old_visual_lnum;
2002 if (wp->w_old_visual_lnum > to)
2003 to = wp->w_old_visual_lnum;
2004 }
2005 else
2006 {
2007 // Find the line numbers that need to be updated: The lines
2008 // between the old cursor position and the current cursor
2009 // position. Also check if the Visual position changed.
2010 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
2011 {
2012 from = curwin->w_cursor.lnum;
2013 to = wp->w_old_cursor_lnum;
2014 }
2015 else
2016 {
2017 from = wp->w_old_cursor_lnum;
2018 to = curwin->w_cursor.lnum;
2019 if (from == 0) // Visual mode just started
2020 from = to;
2021 }
2022
2023 if (VIsual.lnum != wp->w_old_visual_lnum
2024 || VIsual.col != wp->w_old_visual_col)
2025 {
2026 if (wp->w_old_visual_lnum < from
2027 && wp->w_old_visual_lnum != 0)
2028 from = wp->w_old_visual_lnum;
2029 if (wp->w_old_visual_lnum > to)
2030 to = wp->w_old_visual_lnum;
2031 if (VIsual.lnum < from)
2032 from = VIsual.lnum;
2033 if (VIsual.lnum > to)
2034 to = VIsual.lnum;
2035 }
2036 }
2037
2038 // If in block mode and changed column or curwin->w_curswant:
2039 // update all lines.
2040 // First compute the actual start and end column.
2041 if (VIsual_mode == Ctrl_V)
2042 {
2043 colnr_T fromc, toc;
2044#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002045 int save_ve_flags = curwin->w_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002046
2047 if (curwin->w_p_lbr)
Gary Johnson51ad8502021-08-03 18:33:08 +02002048 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002049#endif
2050 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002051 ++toc;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002052#if defined(FEAT_LINEBREAK)
Gary Johnson51ad8502021-08-03 18:33:08 +02002053 curwin->w_ve_flags = save_ve_flags;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002054#endif
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02002055 // Highlight to the end of the line, unless 'virtualedit' has
2056 // "block".
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002057 if (curwin->w_curswant == MAXCOL)
2058 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002059 if (get_ve_flags() & VE_BLOCK)
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002060 {
2061 pos_T pos;
2062 int cursor_above =
2063 curwin->w_cursor.lnum < VIsual.lnum;
2064
2065 // Need to find the longest line.
2066 toc = 0;
2067 pos.coladd = 0;
2068 for (pos.lnum = curwin->w_cursor.lnum; cursor_above
2069 ? pos.lnum <= VIsual.lnum
2070 : pos.lnum >= VIsual.lnum;
2071 pos.lnum += cursor_above ? 1 : -1)
2072 {
2073 colnr_T t;
2074
Bram Moolenaar6bcb1822021-07-09 15:54:00 +02002075 pos.col = (int)STRLEN(ml_get_buf(wp->w_buffer,
Bram Moolenaarb17ab862021-07-03 22:15:17 +02002076 pos.lnum, FALSE));
2077 getvvcol(wp, &pos, NULL, NULL, &t);
2078 if (toc < t)
2079 toc = t;
2080 }
2081 ++toc;
2082 }
2083 else
2084 toc = MAXCOL;
2085 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002086
2087 if (fromc != wp->w_old_cursor_fcol
2088 || toc != wp->w_old_cursor_lcol)
2089 {
2090 if (from > VIsual.lnum)
2091 from = VIsual.lnum;
2092 if (to < VIsual.lnum)
2093 to = VIsual.lnum;
2094 }
2095 wp->w_old_cursor_fcol = fromc;
2096 wp->w_old_cursor_lcol = toc;
2097 }
2098 }
2099 else
2100 {
2101 // Use the line numbers of the old Visual area.
2102 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2103 {
2104 from = wp->w_old_cursor_lnum;
2105 to = wp->w_old_visual_lnum;
2106 }
2107 else
2108 {
2109 from = wp->w_old_visual_lnum;
2110 to = wp->w_old_cursor_lnum;
2111 }
2112 }
2113
2114 // There is no need to update lines above the top of the window.
2115 if (from < wp->w_topline)
2116 from = wp->w_topline;
2117
2118 // If we know the value of w_botline, use it to restrict the update to
2119 // the lines that are visible in the window.
2120 if (wp->w_valid & VALID_BOTLINE)
2121 {
2122 if (from >= wp->w_botline)
2123 from = wp->w_botline - 1;
2124 if (to >= wp->w_botline)
2125 to = wp->w_botline - 1;
2126 }
2127
2128 // Find the minimal part to be updated.
2129 // Watch out for scrolling that made entries in w_lines[] invalid.
2130 // E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2131 // top_end; need to redraw from top_end to the "to" line.
2132 // A middle mouse click with a Visual selection may change the text
2133 // above the Visual area and reset wl_valid, do count these for
2134 // mid_end (in srow).
2135 if (mid_start > 0)
2136 {
2137 lnum = wp->w_topline;
2138 idx = 0;
2139 srow = 0;
2140 if (scrolled_down)
2141 mid_start = top_end;
2142 else
2143 mid_start = 0;
2144 while (lnum < from && idx < wp->w_lines_valid) // find start
2145 {
2146 if (wp->w_lines[idx].wl_valid)
2147 mid_start += wp->w_lines[idx].wl_size;
2148 else if (!scrolled_down)
2149 srow += wp->w_lines[idx].wl_size;
2150 ++idx;
2151# ifdef FEAT_FOLDING
2152 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2153 lnum = wp->w_lines[idx].wl_lnum;
2154 else
2155# endif
2156 ++lnum;
2157 }
2158 srow += mid_start;
2159 mid_end = wp->w_height;
2160 for ( ; idx < wp->w_lines_valid; ++idx) // find end
2161 {
2162 if (wp->w_lines[idx].wl_valid
2163 && wp->w_lines[idx].wl_lnum >= to + 1)
2164 {
2165 // Only update until first row of this line
2166 mid_end = srow;
2167 break;
2168 }
2169 srow += wp->w_lines[idx].wl_size;
2170 }
2171 }
2172 }
2173
2174 if (VIsual_active && buf == curwin->w_buffer)
2175 {
2176 wp->w_old_visual_mode = VIsual_mode;
2177 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2178 wp->w_old_visual_lnum = VIsual.lnum;
2179 wp->w_old_visual_col = VIsual.col;
2180 wp->w_old_curswant = curwin->w_curswant;
2181 }
2182 else
2183 {
2184 wp->w_old_visual_mode = 0;
2185 wp->w_old_cursor_lnum = 0;
2186 wp->w_old_visual_lnum = 0;
2187 wp->w_old_visual_col = 0;
2188 }
2189
2190#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2191 // reset got_int, otherwise regexp won't work
2192 save_got_int = got_int;
2193 got_int = 0;
2194#endif
2195#ifdef SYN_TIME_LIMIT
2196 // Set the time limit to 'redrawtime'.
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002197 redrawtime_limit_set = TRUE;
Paul Ollis65745772022-06-05 16:55:54 +01002198 init_regexp_timeout(p_rdt);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002199#endif
2200#ifdef FEAT_FOLDING
2201 win_foldinfo.fi_level = 0;
2202#endif
2203
2204#ifdef FEAT_MENU
2205 // Draw the window toolbar, if there is one.
2206 // TODO: only when needed.
2207 if (winbar_height(wp) > 0)
2208 redraw_win_toolbar(wp);
2209#endif
2210
2211 // Update all the window rows.
2212 idx = 0; // first entry in w_lines[].wl_size
2213 row = 0;
2214 srow = 0;
2215 lnum = wp->w_topline; // first line shown in window
2216 for (;;)
2217 {
2218 // stop updating when reached the end of the window (check for _past_
2219 // the end of the window is at the end of the loop)
2220 if (row == wp->w_height)
2221 {
2222 didline = TRUE;
2223 break;
2224 }
2225
2226 // stop updating when hit the end of the file
2227 if (lnum > buf->b_ml.ml_line_count)
2228 {
2229 eof = TRUE;
2230 break;
2231 }
2232
2233 // Remember the starting row of the line that is going to be dealt
2234 // with. It is used further down when the line doesn't fit.
2235 srow = row;
2236
2237 // Update a line when it is in an area that needs updating, when it
2238 // has changes or w_lines[idx] is invalid.
2239 // "bot_start" may be halfway a wrapped line after using
2240 // win_del_lines(), check if the current line includes it.
2241 // When syntax folding is being used, the saved syntax states will
2242 // already have been updated, we can't see where the syntax state is
2243 // the same again, just update until the end of the window.
2244 if (row < top_end
2245 || (row >= mid_start && row < mid_end)
2246#ifdef FEAT_SEARCH_EXTRA
2247 || top_to_mod
2248#endif
2249 || idx >= wp->w_lines_valid
2250 || (row + wp->w_lines[idx].wl_size > bot_start)
2251 || (mod_top != 0
2252 && (lnum == mod_top
2253 || (lnum >= mod_top
2254 && (lnum < mod_bot
2255#ifdef FEAT_SYN_HL
2256 || did_update == DID_FOLD
2257 || (did_update == DID_LINE
2258 && syntax_present(wp)
2259 && (
2260# ifdef FEAT_FOLDING
2261 (foldmethodIsSyntax(wp)
2262 && hasAnyFolding(wp)) ||
2263# endif
2264 syntax_check_changed(lnum)))
2265#endif
2266#ifdef FEAT_SEARCH_EXTRA
2267 // match in fixed position might need redraw
2268 // if lines were inserted or deleted
2269 || (wp->w_match_head != NULL
2270 && buf->b_mod_xlines != 0)
2271#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002272 ))))
2273#ifdef FEAT_SYN_HL
zeertzjqc20e46a2022-03-23 14:55:23 +00002274 || (wp->w_p_cul && lnum == wp->w_cursor.lnum)
2275 || lnum == wp->w_last_cursorline
Bram Moolenaar11a58af2019-10-24 22:32:31 +02002276#endif
2277 )
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002278 {
2279#ifdef FEAT_SEARCH_EXTRA
2280 if (lnum == mod_top)
2281 top_to_mod = FALSE;
2282#endif
2283
2284 // When at start of changed lines: May scroll following lines
2285 // up or down to minimize redrawing.
2286 // Don't do this when the change continues until the end.
2287 // Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002288 // Don't scroll when redrawing the top, scrolled already above.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002289 if (lnum == mod_top
2290 && mod_bot != MAXLNUM
Bram Moolenaarc9e7e342021-07-22 21:33:03 +02002291 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
2292 && row >= top_end)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002293 {
2294 int old_rows = 0;
2295 int new_rows = 0;
2296 int xtra_rows;
2297 linenr_T l;
2298
2299 // Count the old number of window rows, using w_lines[], which
2300 // should still contain the sizes for the lines as they are
2301 // currently displayed.
2302 for (i = idx; i < wp->w_lines_valid; ++i)
2303 {
2304 // Only valid lines have a meaningful wl_lnum. Invalid
2305 // lines are part of the changed area.
2306 if (wp->w_lines[i].wl_valid
2307 && wp->w_lines[i].wl_lnum == mod_bot)
2308 break;
2309 old_rows += wp->w_lines[i].wl_size;
2310#ifdef FEAT_FOLDING
2311 if (wp->w_lines[i].wl_valid
2312 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2313 {
2314 // Must have found the last valid entry above mod_bot.
2315 // Add following invalid entries.
2316 ++i;
2317 while (i < wp->w_lines_valid
2318 && !wp->w_lines[i].wl_valid)
2319 old_rows += wp->w_lines[i++].wl_size;
2320 break;
2321 }
2322#endif
2323 }
2324
2325 if (i >= wp->w_lines_valid)
2326 {
2327 // We can't find a valid line below the changed lines,
2328 // need to redraw until the end of the window.
2329 // Inserting/deleting lines has no use.
2330 bot_start = 0;
2331 }
2332 else
2333 {
2334 // Able to count old number of rows: Count new window
2335 // rows, and may insert/delete lines
2336 j = idx;
2337 for (l = lnum; l < mod_bot; ++l)
2338 {
2339#ifdef FEAT_FOLDING
2340 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2341 ++new_rows;
2342 else
2343#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002344 {
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002345#ifdef FEAT_DIFF
2346 if (l == wp->w_topline)
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002347 new_rows += plines_win_nofill(wp, l, TRUE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002348 + wp->w_topfill;
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002349 else
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002350#endif
Dominique Pelle9b0b8442021-10-18 20:56:39 +01002351 new_rows += plines_win(wp, l, TRUE);
2352 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002353 ++j;
2354 if (new_rows > wp->w_height - row - 2)
2355 {
2356 // it's getting too much, must redraw the rest
2357 new_rows = 9999;
2358 break;
2359 }
2360 }
2361 xtra_rows = new_rows - old_rows;
2362 if (xtra_rows < 0)
2363 {
2364 // May scroll text up. If there is not enough
2365 // remaining text or scrolling fails, must redraw the
2366 // rest. If scrolling works, must redraw the text
2367 // below the scrolled text.
2368 if (row - xtra_rows >= wp->w_height - 2)
2369 mod_bot = MAXLNUM;
2370 else
2371 {
2372 check_for_delay(FALSE);
2373 if (win_del_lines(wp, row,
2374 -xtra_rows, FALSE, FALSE, 0) == FAIL)
2375 mod_bot = MAXLNUM;
2376 else
2377 bot_start = wp->w_height + xtra_rows;
2378 }
2379 }
2380 else if (xtra_rows > 0)
2381 {
2382 // May scroll text down. If there is not enough
2383 // remaining text of scrolling fails, must redraw the
2384 // rest.
2385 if (row + xtra_rows >= wp->w_height - 2)
2386 mod_bot = MAXLNUM;
2387 else
2388 {
2389 check_for_delay(FALSE);
2390 if (win_ins_lines(wp, row + old_rows,
2391 xtra_rows, FALSE, FALSE) == FAIL)
2392 mod_bot = MAXLNUM;
2393 else if (top_end > row + old_rows)
2394 // Scrolled the part at the top that requires
2395 // updating down.
2396 top_end += xtra_rows;
2397 }
2398 }
2399
2400 // When not updating the rest, may need to move w_lines[]
2401 // entries.
2402 if (mod_bot != MAXLNUM && i != j)
2403 {
2404 if (j < i)
2405 {
2406 int x = row + new_rows;
2407
2408 // move entries in w_lines[] upwards
2409 for (;;)
2410 {
2411 // stop at last valid entry in w_lines[]
2412 if (i >= wp->w_lines_valid)
2413 {
2414 wp->w_lines_valid = j;
2415 break;
2416 }
2417 wp->w_lines[j] = wp->w_lines[i];
2418 // stop at a line that won't fit
2419 if (x + (int)wp->w_lines[j].wl_size
2420 > wp->w_height)
2421 {
2422 wp->w_lines_valid = j + 1;
2423 break;
2424 }
2425 x += wp->w_lines[j++].wl_size;
2426 ++i;
2427 }
2428 if (bot_start > x)
2429 bot_start = x;
2430 }
2431 else // j > i
2432 {
2433 // move entries in w_lines[] downwards
2434 j -= i;
2435 wp->w_lines_valid += j;
2436 if (wp->w_lines_valid > wp->w_height)
2437 wp->w_lines_valid = wp->w_height;
2438 for (i = wp->w_lines_valid; i - j >= idx; --i)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002439 wp->w_lines[i] = wp->w_lines[i - j];
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002440
2441 // The w_lines[] entries for inserted lines are
2442 // now invalid, but wl_size may be used above.
2443 // Reset to zero.
2444 while (i >= idx)
2445 {
2446 wp->w_lines[i].wl_size = 0;
2447 wp->w_lines[i--].wl_valid = FALSE;
2448 }
2449 }
2450 }
2451 }
2452 }
2453
2454#ifdef FEAT_FOLDING
2455 // When lines are folded, display one line for all of them.
2456 // Otherwise, display normally (can be several display lines when
2457 // 'wrap' is on).
2458 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2459 if (fold_count != 0)
2460 {
2461 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2462 ++row;
2463 --fold_count;
2464 wp->w_lines[idx].wl_folded = TRUE;
2465 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2466# ifdef FEAT_SYN_HL
2467 did_update = DID_FOLD;
2468# endif
2469 }
2470 else
2471#endif
2472 if (idx < wp->w_lines_valid
2473 && wp->w_lines[idx].wl_valid
2474 && wp->w_lines[idx].wl_lnum == lnum
2475 && lnum > wp->w_topline
2476 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
2477 && !WIN_IS_POPUP(wp)
2478 && srow + wp->w_lines[idx].wl_size > wp->w_height
2479#ifdef FEAT_DIFF
2480 && diff_check_fill(wp, lnum) == 0
2481#endif
2482 )
2483 {
2484 // This line is not going to fit. Don't draw anything here,
2485 // will draw "@ " lines below.
2486 row = wp->w_height + 1;
2487 }
2488 else
2489 {
2490#ifdef FEAT_SEARCH_EXTRA
2491 prepare_search_hl(wp, &screen_search_hl, lnum);
2492#endif
2493#ifdef FEAT_SYN_HL
2494 // Let the syntax stuff know we skipped a few lines.
2495 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
2496 && syntax_present(wp))
2497 syntax_end_parsing(syntax_last_parsed + 1);
2498#endif
2499
2500 // Display one line.
2501 row = win_line(wp, lnum, srow, wp->w_height,
2502 mod_top == 0, FALSE);
2503
2504#ifdef FEAT_FOLDING
2505 wp->w_lines[idx].wl_folded = FALSE;
2506 wp->w_lines[idx].wl_lastlnum = lnum;
2507#endif
2508#ifdef FEAT_SYN_HL
2509 did_update = DID_LINE;
2510 syntax_last_parsed = lnum;
2511#endif
2512 }
2513
2514 wp->w_lines[idx].wl_lnum = lnum;
2515 wp->w_lines[idx].wl_valid = TRUE;
2516
2517 // Past end of the window or end of the screen. Note that after
2518 // resizing wp->w_height may be end up too big. That's a problem
2519 // elsewhere, but prevent a crash here.
Bram Moolenaara2a89732022-08-31 14:46:18 +01002520 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002521 {
2522 // we may need the size of that too long line later on
2523 if (dollar_vcol == -1)
2524 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2525 ++idx;
2526 break;
2527 }
2528 if (dollar_vcol == -1)
2529 wp->w_lines[idx].wl_size = row - srow;
2530 ++idx;
2531#ifdef FEAT_FOLDING
2532 lnum += fold_count + 1;
2533#else
2534 ++lnum;
2535#endif
2536 }
2537 else
2538 {
Lewis Russell16246392022-03-29 11:38:17 +01002539 if (wp->w_p_rnu && wp->w_last_cursor_lnum_rnu != wp->w_cursor.lnum)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002540 {
2541#ifdef FEAT_FOLDING
Lewis Russell16246392022-03-29 11:38:17 +01002542 // 'relativenumber' set and the cursor moved vertically: The
2543 // text doesn't need to be drawn, but the number column does.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002544 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2545 if (fold_count != 0)
2546 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2547 else
2548#endif
2549 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
2550 }
2551
2552 // This line does not need to be drawn, advance to the next one.
2553 row += wp->w_lines[idx++].wl_size;
2554 if (row > wp->w_height) // past end of screen
2555 break;
2556#ifdef FEAT_FOLDING
2557 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2558#else
2559 ++lnum;
2560#endif
2561#ifdef FEAT_SYN_HL
2562 did_update = DID_NONE;
2563#endif
2564 }
2565
2566 if (lnum > buf->b_ml.ml_line_count)
2567 {
2568 eof = TRUE;
2569 break;
2570 }
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002571
2572 // Safety check: if any of the wl_size values is wrong we might go over
2573 // the end of w_lines[].
Bram Moolenaara2a89732022-08-31 14:46:18 +01002574 if (idx >= Rows)
Bram Moolenaarfa1a4572022-01-16 11:42:20 +00002575 break;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002576 }
2577
2578 // End of loop over all window lines.
2579
zeertzjqc20e46a2022-03-23 14:55:23 +00002580#ifdef FEAT_SYN_HL
2581 // Now that the window has been redrawn with the old and new cursor line,
2582 // update w_last_cursorline.
2583 wp->w_last_cursorline = wp->w_p_cul ? wp->w_cursor.lnum : 0;
2584#endif
Lewis Russell16246392022-03-29 11:38:17 +01002585 wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0;
zeertzjqc20e46a2022-03-23 14:55:23 +00002586
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002587#ifdef FEAT_VTP
2588 // Rewrite the character at the end of the screen line.
Bram Moolenaar7ed8f592020-04-28 20:44:42 +02002589 // See the version that was fixed.
2590 if (use_vtp() && get_conpty_fix_type() < 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002591 {
K.Takata54119102022-02-03 13:33:03 +00002592 int k;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002593
K.Takata54119102022-02-03 13:33:03 +00002594 for (k = 0; k < Rows; ++k)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002595 if (enc_utf8)
K.Takata54119102022-02-03 13:33:03 +00002596 if ((*mb_off2cells)(LineOffset[k] + Columns - 2,
2597 LineOffset[k] + screen_Columns) > 1)
2598 screen_draw_rectangle(k, Columns - 2, 1, 2, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002599 else
K.Takata54119102022-02-03 13:33:03 +00002600 screen_draw_rectangle(k, Columns - 1, 1, 1, FALSE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002601 else
K.Takata54119102022-02-03 13:33:03 +00002602 screen_char(LineOffset[k] + Columns - 1, k, Columns - 1);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002603 }
2604#endif
2605
2606 if (idx > wp->w_lines_valid)
2607 wp->w_lines_valid = idx;
2608
2609#ifdef FEAT_SYN_HL
2610 // Let the syntax stuff know we stop parsing here.
2611 if (syntax_last_parsed != 0 && syntax_present(wp))
2612 syntax_end_parsing(syntax_last_parsed + 1);
2613#endif
2614
2615 // If we didn't hit the end of the file, and we didn't finish the last
2616 // line we were working on, then the line didn't fit.
2617 wp->w_empty_rows = 0;
2618#ifdef FEAT_DIFF
2619 wp->w_filler_rows = 0;
2620#endif
2621 if (!eof && !didline)
2622 {
2623 if (lnum == wp->w_topline)
2624 {
2625 // Single line that does not fit!
2626 // Don't overwrite it, it can be edited.
2627 wp->w_botline = lnum + 1;
2628 }
2629#ifdef FEAT_DIFF
2630 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2631 {
2632 // Window ends in filler lines.
2633 wp->w_botline = lnum;
2634 wp->w_filler_rows = wp->w_height - srow;
2635 }
2636#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002637#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002638 else if (WIN_IS_POPUP(wp))
2639 {
2640 // popup line that doesn't fit is left as-is
2641 wp->w_botline = lnum;
2642 }
2643#endif
2644 else if (dy_flags & DY_TRUNCATE) // 'display' has "truncate"
2645 {
2646 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2647
2648 // Last line isn't finished: Display "@@@" in the last screen line.
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002649 screen_puts_len((char_u *)"@@", wp->w_width > 2 ? 2 : wp->w_width,
2650 scr_row, wp->w_wincol, HL_ATTR(HLF_AT));
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002651 screen_fill(scr_row, scr_row + 1,
2652 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
2653 '@', ' ', HL_ATTR(HLF_AT));
2654 set_empty_rows(wp, srow);
2655 wp->w_botline = lnum;
2656 }
2657 else if (dy_flags & DY_LASTLINE) // 'display' has "lastline"
2658 {
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002659 int start_col = (int)W_ENDCOL(wp) - 3;
2660
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002661 // Last line isn't finished: Display "@@@" at the end.
2662 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2663 W_WINROW(wp) + wp->w_height,
Bram Moolenaarcee9c842022-04-09 12:40:13 +01002664 start_col < wp->w_wincol ? wp->w_wincol : start_col,
2665 (int)W_ENDCOL(wp),
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002666 '@', '@', HL_ATTR(HLF_AT));
2667 set_empty_rows(wp, srow);
2668 wp->w_botline = lnum;
2669 }
2670 else
2671 {
2672 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
2673 wp->w_botline = lnum;
2674 }
2675 }
2676 else
2677 {
2678 draw_vsep_win(wp, row);
2679 if (eof) // we hit the end of the file
2680 {
2681 wp->w_botline = buf->b_ml.ml_line_count + 1;
2682#ifdef FEAT_DIFF
2683 j = diff_check_fill(wp, wp->w_botline);
2684 if (j > 0 && !wp->w_botfill)
2685 {
2686 // Display filler lines at the end of the file.
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002687 if (char2cells(wp->w_fill_chars.diff) > 1)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002688 i = '-';
2689 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002690 i = wp->w_fill_chars.diff;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002691 if (row + j > wp->w_height)
2692 j = wp->w_height - row;
2693 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
2694 row += j;
2695 }
2696#endif
2697 }
2698 else if (dollar_vcol == -1)
2699 wp->w_botline = lnum;
2700
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002701 // Make sure the rest of the screen is blank.
2702 // write the "eob" character from 'fillchars' to rows that aren't part
2703 // of the file.
Bram Moolenaar1666ac92019-11-10 17:22:31 +01002704 if (WIN_IS_POPUP(wp))
2705 win_draw_end(wp, ' ', ' ', FALSE, row, wp->w_height, HLF_AT);
2706 else
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002707 win_draw_end(wp, wp->w_fill_chars.eob, ' ', FALSE,
2708 row, wp->w_height, HLF_EOB);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002709 }
2710
2711#ifdef SYN_TIME_LIMIT
Paul Ollis65745772022-06-05 16:55:54 +01002712 disable_regexp_timeout();
Bram Moolenaar6f0cf622022-06-19 12:27:45 +01002713 redrawtime_limit_set = FALSE;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002714#endif
2715
2716 // Reset the type of redrawing required, the window has been updated.
2717 wp->w_redr_type = 0;
2718#ifdef FEAT_DIFF
2719 wp->w_old_topfill = wp->w_topfill;
2720 wp->w_old_botfill = wp->w_botfill;
2721#endif
2722
2723 if (dollar_vcol == -1)
2724 {
2725 // There is a trick with w_botline. If we invalidate it on each
2726 // change that might modify it, this will cause a lot of expensive
2727 // calls to plines() in update_topline() each time. Therefore the
2728 // value of w_botline is often approximated, and this value is used to
2729 // compute the value of w_topline. If the value of w_botline was
2730 // wrong, check that the value of w_topline is correct (cursor is on
2731 // the visible part of the text). If it's not, we need to redraw
2732 // again. Mostly this just means scrolling up a few lines, so it
2733 // doesn't look too bad. Only do this for the current window (where
2734 // changes are relevant).
2735 wp->w_valid |= VALID_BOTLINE;
2736 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2737 {
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002738 win_T *wwp;
2739#if defined(FEAT_CONCEAL)
2740 linenr_T old_topline = wp->w_topline;
2741 int new_wcol = wp->w_wcol;
2742#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002743 recursive = TRUE;
2744 curwin->w_valid &= ~VALID_TOPLINE;
2745 update_topline(); // may invalidate w_botline again
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002746
2747#if defined(FEAT_CONCEAL)
2748 if (old_wcol != new_wcol && (wp->w_valid & (VALID_WCOL|VALID_WROW))
2749 != (VALID_WCOL|VALID_WROW))
2750 {
2751 // A win_line() call applied a fix to screen cursor column to
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002752 // accommodate concealment of cursor line, but in this call to
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002753 // update_topline() the cursor's row or column got invalidated.
2754 // If they are left invalid, setcursor() will recompute them
2755 // but there won't be any further win_line() call to re-fix the
2756 // column and the cursor will end up misplaced. So we call
2757 // cursor validation now and reapply the fix again (or call
2758 // win_line() to do it for us).
2759 validate_cursor();
2760 if (wp->w_wcol == old_wcol && wp->w_wrow == old_wrow
2761 && old_topline == wp->w_topline)
2762 wp->w_wcol = new_wcol;
2763 else
2764 redrawWinline(wp, wp->w_cursor.lnum);
2765 }
2766#endif
2767 // New redraw either due to updated topline or due to wcol fix.
2768 if (wp->w_redr_type != 0)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002769 {
2770 // Don't update for changes in buffer again.
2771 i = curbuf->b_mod_set;
2772 curbuf->b_mod_set = FALSE;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002773 j = curbuf->b_mod_xlines;
2774 curbuf->b_mod_xlines = 0;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002775 win_update(curwin);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002776 curbuf->b_mod_set = i;
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002777 curbuf->b_mod_xlines = j;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002778 }
Bram Moolenaarcbee6352019-11-12 20:49:15 +01002779 // Other windows might have w_redr_type raised in update_topline().
2780 must_redraw = 0;
2781 FOR_ALL_WINDOWS(wwp)
2782 if (wwp->w_redr_type > must_redraw)
2783 must_redraw = wwp->w_redr_type;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002784 recursive = FALSE;
2785 }
2786 }
2787
2788#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2789 // restore got_int, unless CTRL-C was hit while redrawing
2790 if (!got_int)
2791 got_int = save_got_int;
2792#endif
2793}
2794
2795#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
2796/*
2797 * Prepare for updating one or more windows.
2798 * Caller must check for "updating_screen" already set to avoid recursiveness.
2799 */
2800 static void
2801update_prepare(void)
2802{
2803 cursor_off();
2804 updating_screen = TRUE;
2805#ifdef FEAT_GUI
2806 // Remove the cursor before starting to do anything, because scrolling may
2807 // make it difficult to redraw the text under it.
2808 if (gui.in_use)
2809 gui_undraw_cursor();
2810#endif
2811#ifdef FEAT_SEARCH_EXTRA
2812 start_search_hl();
2813#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002814#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002815 // Update popup_mask if needed.
2816 may_update_popup_mask(must_redraw);
2817#endif
2818}
2819
2820/*
2821 * Finish updating one or more windows.
2822 */
2823 static void
2824update_finish(void)
2825{
2826 if (redraw_cmdline || redraw_mode)
2827 showmode();
2828
2829# ifdef FEAT_SEARCH_EXTRA
2830 end_search_hl();
2831# endif
2832
2833 after_updating_screen(TRUE);
2834
2835# ifdef FEAT_GUI
2836 // Redraw the cursor and update the scrollbars when all screen updating is
2837 // done.
2838 if (gui.in_use)
2839 {
2840 out_flush_cursor(FALSE, FALSE);
2841 gui_update_scrollbars(FALSE);
2842 }
2843# endif
2844}
2845#endif
2846
2847#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
2848 void
2849update_debug_sign(buf_T *buf, linenr_T lnum)
2850{
2851 win_T *wp;
2852 int doit = FALSE;
2853
2854# ifdef FEAT_FOLDING
2855 win_foldinfo.fi_level = 0;
2856# endif
2857
2858 // update/delete a specific sign
2859 redraw_buf_line_later(buf, lnum);
2860
2861 // check if it resulted in the need to redraw a window
2862 FOR_ALL_WINDOWS(wp)
2863 if (wp->w_redr_type != 0)
2864 doit = TRUE;
2865
2866 // Return when there is nothing to do, screen updating is already
2867 // happening (recursive call), messages on the screen or still starting up.
2868 if (!doit || updating_screen
Bram Moolenaar24959102022-05-07 20:01:16 +01002869 || State == MODE_ASKMORE || State == MODE_HITRETURN
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002870 || msg_scrolled
2871#ifdef FEAT_GUI
2872 || gui.starting
2873#endif
2874 || starting)
2875 return;
2876
2877 // update all windows that need updating
2878 update_prepare();
2879
2880 FOR_ALL_WINDOWS(wp)
2881 {
2882 if (wp->w_redr_type != 0)
2883 win_update(wp);
2884 if (wp->w_redr_status)
2885 win_redr_status(wp, FALSE);
2886 }
2887
2888 update_finish();
2889}
2890#endif
2891
2892#if defined(FEAT_GUI) || defined(PROTO)
2893/*
2894 * Update a single window, its status line and maybe the command line msg.
2895 * Used for the GUI scrollbar.
2896 */
2897 void
2898updateWindow(win_T *wp)
2899{
2900 // return if already busy updating
2901 if (updating_screen)
2902 return;
2903
2904 update_prepare();
2905
2906#ifdef FEAT_CLIPBOARD
2907 // When Visual area changed, may have to update selection.
2908 if (clip_star.available && clip_isautosel_star())
2909 clip_update_selection(&clip_star);
2910 if (clip_plus.available && clip_isautosel_plus())
2911 clip_update_selection(&clip_plus);
2912#endif
2913
2914 win_update(wp);
2915
2916 // When the screen was cleared redraw the tab pages line.
2917 if (redraw_tabline)
2918 draw_tabline();
2919
2920 if (wp->w_redr_status
2921# ifdef FEAT_CMDL_INFO
2922 || p_ru
2923# endif
2924# ifdef FEAT_STL_OPT
2925 || *p_stl != NUL || *wp->w_p_stl != NUL
2926# endif
2927 )
2928 win_redr_status(wp, FALSE);
2929
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002930#ifdef FEAT_PROP_POPUP
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002931 // Display popup windows on top of everything.
2932 update_popups(win_update);
2933#endif
2934
2935 update_finish();
2936}
2937#endif
2938
2939#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2940/*
2941 * Redraw as soon as possible. When the command line is not scrolled redraw
2942 * right away and restore what was on the command line.
2943 * Return a code indicating what happened.
2944 */
2945 int
2946redraw_asap(int type)
2947{
2948 int rows;
2949 int cols = screen_Columns;
2950 int r;
2951 int ret = 0;
2952 schar_T *screenline; // copy from ScreenLines[]
2953 sattr_T *screenattr; // copy from ScreenAttrs[]
2954 int i;
2955 u8char_T *screenlineUC = NULL; // copy from ScreenLinesUC[]
2956 u8char_T *screenlineC[MAX_MCO]; // copy from ScreenLinesC[][]
2957 schar_T *screenline2 = NULL; // copy from ScreenLines2[]
2958
2959 redraw_later(type);
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01002960 if (msg_scrolled
2961 || (State != MODE_NORMAL && State != MODE_NORMAL_BUSY)
Bram Moolenaara2a89732022-08-31 14:46:18 +01002962 || exiting)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02002963 return ret;
2964
2965 // Allocate space to save the text displayed in the command line area.
2966 rows = screen_Rows - cmdline_row;
2967 screenline = LALLOC_MULT(schar_T, rows * cols);
2968 screenattr = LALLOC_MULT(sattr_T, rows * cols);
2969 if (screenline == NULL || screenattr == NULL)
2970 ret = 2;
2971 if (enc_utf8)
2972 {
2973 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
2974 if (screenlineUC == NULL)
2975 ret = 2;
2976 for (i = 0; i < p_mco; ++i)
2977 {
2978 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
2979 if (screenlineC[i] == NULL)
2980 ret = 2;
2981 }
2982 }
2983 if (enc_dbcs == DBCS_JPNU)
2984 {
2985 screenline2 = LALLOC_MULT(schar_T, rows * cols);
2986 if (screenline2 == NULL)
2987 ret = 2;
2988 }
2989
2990 if (ret != 2)
2991 {
2992 // Save the text displayed in the command line area.
2993 for (r = 0; r < rows; ++r)
2994 {
2995 mch_memmove(screenline + r * cols,
2996 ScreenLines + LineOffset[cmdline_row + r],
2997 (size_t)cols * sizeof(schar_T));
2998 mch_memmove(screenattr + r * cols,
2999 ScreenAttrs + LineOffset[cmdline_row + r],
3000 (size_t)cols * sizeof(sattr_T));
3001 if (enc_utf8)
3002 {
3003 mch_memmove(screenlineUC + r * cols,
3004 ScreenLinesUC + LineOffset[cmdline_row + r],
3005 (size_t)cols * sizeof(u8char_T));
3006 for (i = 0; i < p_mco; ++i)
3007 mch_memmove(screenlineC[i] + r * cols,
3008 ScreenLinesC[i] + LineOffset[cmdline_row + r],
3009 (size_t)cols * sizeof(u8char_T));
3010 }
3011 if (enc_dbcs == DBCS_JPNU)
3012 mch_memmove(screenline2 + r * cols,
3013 ScreenLines2 + LineOffset[cmdline_row + r],
3014 (size_t)cols * sizeof(schar_T));
3015 }
3016
3017 update_screen(0);
3018 ret = 3;
3019
3020 if (must_redraw == 0)
3021 {
3022 int off = (int)(current_ScreenLine - ScreenLines);
3023
3024 // Restore the text displayed in the command line area.
3025 for (r = 0; r < rows; ++r)
3026 {
3027 mch_memmove(current_ScreenLine,
3028 screenline + r * cols,
3029 (size_t)cols * sizeof(schar_T));
3030 mch_memmove(ScreenAttrs + off,
3031 screenattr + r * cols,
3032 (size_t)cols * sizeof(sattr_T));
3033 if (enc_utf8)
3034 {
3035 mch_memmove(ScreenLinesUC + off,
3036 screenlineUC + r * cols,
3037 (size_t)cols * sizeof(u8char_T));
3038 for (i = 0; i < p_mco; ++i)
3039 mch_memmove(ScreenLinesC[i] + off,
3040 screenlineC[i] + r * cols,
3041 (size_t)cols * sizeof(u8char_T));
3042 }
3043 if (enc_dbcs == DBCS_JPNU)
3044 mch_memmove(ScreenLines2 + off,
3045 screenline2 + r * cols,
3046 (size_t)cols * sizeof(schar_T));
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01003047 screen_line(curwin, cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003048 }
3049 ret = 4;
3050 }
3051 }
3052
3053 vim_free(screenline);
3054 vim_free(screenattr);
3055 if (enc_utf8)
3056 {
3057 vim_free(screenlineUC);
3058 for (i = 0; i < p_mco; ++i)
3059 vim_free(screenlineC[i]);
3060 }
3061 if (enc_dbcs == DBCS_JPNU)
3062 vim_free(screenline2);
3063
3064 // Show the intro message when appropriate.
3065 maybe_intro_message();
3066
3067 setcursor();
3068
3069 return ret;
3070}
3071#endif
3072
3073/*
3074 * Invoked after an asynchronous callback is called.
3075 * If an echo command was used the cursor needs to be put back where
3076 * it belongs. If highlighting was changed a redraw is needed.
3077 * If "call_update_screen" is FALSE don't call update_screen() when at the
3078 * command line.
Bram Moolenaare5050712021-12-09 10:51:05 +00003079 * If "redraw_message" is TRUE.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003080 */
3081 void
Bram Moolenaare5050712021-12-09 10:51:05 +00003082redraw_after_callback(int call_update_screen, int do_message)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003083{
3084 ++redrawing_for_callback;
3085
Bram Moolenaar24959102022-05-07 20:01:16 +01003086 if (State == MODE_HITRETURN || State == MODE_ASKMORE
3087 || State == MODE_SETWSIZE || State == MODE_EXTERNCMD
3088 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaare5050712021-12-09 10:51:05 +00003089 {
3090 if (do_message)
3091 repeat_message();
3092 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003093 else if (State & MODE_CMDLINE)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003094 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003095 if (pum_visible())
3096 cmdline_pum_display();
Bram Moolenaar54162322022-08-26 16:58:51 +01003097
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003098 // Don't redraw when in prompt_for_number().
3099 if (cmdline_row > 0)
3100 {
3101 // Redrawing only works when the screen didn't scroll. Don't clear
3102 // wildmenu entries.
3103 if (msg_scrolled == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003104 && wild_menu_showing == 0
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003105 && call_update_screen)
3106 update_screen(0);
3107
3108 // Redraw in the same position, so that the user can continue
3109 // editing the command.
3110 redrawcmdline_ex(FALSE);
3111 }
3112 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003113 else if (State & (MODE_NORMAL | MODE_INSERT | MODE_TERMINAL))
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003114 {
zeertzjq3e559cd2022-03-27 19:26:55 +01003115 update_topline();
3116 validate_cursor();
3117
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003118 // keep the command line if possible
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003119 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003120 setcursor();
Bram Moolenaar9f284162021-04-22 21:39:30 +02003121
3122 if (msg_scrolled == 0)
3123 {
3124 // don't want a hit-enter prompt when something else is displayed
3125 msg_didany = FALSE;
3126 need_wait_return = FALSE;
3127 }
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003128 }
3129 cursor_on();
3130#ifdef FEAT_GUI
3131 if (gui.in_use && !gui_mch_is_blink_off())
3132 // Don't update the cursor when it is blinking and off to avoid
3133 // flicker.
3134 out_flush_cursor(FALSE, FALSE);
3135 else
3136#endif
3137 out_flush();
3138
3139 --redrawing_for_callback;
3140}
3141
3142/*
3143 * Redraw the current window later, with update_screen(type).
3144 * Set must_redraw only if not already set to a higher value.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003145 * E.g. if must_redraw is UPD_CLEAR, type UPD_NOT_VALID will do nothing.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003146 */
3147 void
3148redraw_later(int type)
3149{
3150 redraw_win_later(curwin, type);
3151}
3152
3153 void
3154redraw_win_later(
3155 win_T *wp,
3156 int type)
3157{
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003158 if (!exiting && !redraw_not_allowed && wp->w_redr_type < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003159 {
3160 wp->w_redr_type = type;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003161 if (type >= UPD_NOT_VALID)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003162 wp->w_lines_valid = 0;
3163 if (must_redraw < type) // must_redraw is the maximum of all windows
3164 must_redraw = type;
3165 }
3166}
3167
3168/*
3169 * Force a complete redraw later. Also resets the highlighting. To be used
3170 * after executing a shell command that messes up the screen.
3171 */
3172 void
3173redraw_later_clear(void)
3174{
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003175 redraw_all_later(UPD_CLEAR);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003176 reset_screen_attr();
3177}
3178
3179/*
Bram Moolenaar13608d82022-08-29 15:06:50 +01003180 * Mark all windows to be redrawn later. Except popup windows.
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003181 */
3182 void
3183redraw_all_later(int type)
3184{
3185 win_T *wp;
3186
3187 FOR_ALL_WINDOWS(wp)
3188 redraw_win_later(wp, type);
3189 // This may be needed when switching tabs.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003190 set_must_redraw(type);
3191}
3192
Bram Moolenaar13608d82022-08-29 15:06:50 +01003193#if 0 // not actually used yet, it probably should
3194/*
3195 * Mark all windows, including popup windows, to be redrawn.
3196 */
3197 void
3198redraw_all_windows_later(int type)
3199{
3200 redraw_all_later(type);
3201#ifdef FEAT_PROP_POPUP
3202 popup_redraw_all(); // redraw all popup windows
3203#endif
3204}
3205#endif
3206
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01003207/*
3208 * Set "must_redraw" to "type" unless it already has a higher value
3209 * or it is currently not allowed.
3210 */
3211 void
3212set_must_redraw(int type)
3213{
3214 if (!redraw_not_allowed && must_redraw < type)
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003215 must_redraw = type;
3216}
3217
3218/*
3219 * Mark all windows that are editing the current buffer to be updated later.
3220 */
3221 void
3222redraw_curbuf_later(int type)
3223{
3224 redraw_buf_later(curbuf, type);
3225}
3226
3227 void
3228redraw_buf_later(buf_T *buf, int type)
3229{
3230 win_T *wp;
3231
3232 FOR_ALL_WINDOWS(wp)
3233 {
3234 if (wp->w_buffer == buf)
3235 redraw_win_later(wp, type);
3236 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003237#if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
3238 // terminal in popup window is not in list of windows
3239 if (curwin->w_buffer == buf)
3240 redraw_win_later(curwin, type);
3241#endif
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003242}
3243
3244#if defined(FEAT_SIGNS) || defined(PROTO)
3245 void
3246redraw_buf_line_later(buf_T *buf, linenr_T lnum)
3247{
3248 win_T *wp;
3249
3250 FOR_ALL_WINDOWS(wp)
3251 if (wp->w_buffer == buf && lnum >= wp->w_topline
3252 && lnum < wp->w_botline)
3253 redrawWinline(wp, lnum);
3254}
3255#endif
3256
3257#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3258 void
3259redraw_buf_and_status_later(buf_T *buf, int type)
3260{
3261 win_T *wp;
3262
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003263 if (wild_menu_showing != 0)
3264 // Don't redraw while the command line completion is displayed, it
3265 // would disappear.
3266 return;
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003267 FOR_ALL_WINDOWS(wp)
3268 {
3269 if (wp->w_buffer == buf)
3270 {
3271 redraw_win_later(wp, type);
3272 wp->w_redr_status = TRUE;
3273 }
3274 }
3275}
3276#endif
3277
3278/*
3279 * mark all status lines for redraw; used after first :cd
3280 */
3281 void
3282status_redraw_all(void)
3283{
3284 win_T *wp;
3285
3286 FOR_ALL_WINDOWS(wp)
3287 if (wp->w_status_height)
3288 {
3289 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003290 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003291 }
3292}
3293
3294/*
3295 * mark all status lines of the current buffer for redraw
3296 */
3297 void
3298status_redraw_curbuf(void)
3299{
3300 win_T *wp;
3301
3302 FOR_ALL_WINDOWS(wp)
3303 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
3304 {
3305 wp->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003306 redraw_later(UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003307 }
3308}
3309
3310/*
3311 * Redraw all status lines that need to be redrawn.
3312 */
3313 void
3314redraw_statuslines(void)
3315{
3316 win_T *wp;
3317
3318 FOR_ALL_WINDOWS(wp)
3319 if (wp->w_redr_status)
3320 win_redr_status(wp, FALSE);
3321 if (redraw_tabline)
3322 draw_tabline();
3323}
3324
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003325/*
3326 * Redraw all status lines at the bottom of frame "frp".
3327 */
3328 void
3329win_redraw_last_status(frame_T *frp)
3330{
3331 if (frp->fr_layout == FR_LEAF)
3332 frp->fr_win->w_redr_status = TRUE;
3333 else if (frp->fr_layout == FR_ROW)
3334 {
3335 FOR_ALL_FRAMES(frp, frp->fr_child)
3336 win_redraw_last_status(frp);
3337 }
3338 else // frp->fr_layout == FR_COL
3339 {
3340 frp = frp->fr_child;
3341 while (frp->fr_next != NULL)
3342 frp = frp->fr_next;
3343 win_redraw_last_status(frp);
3344 }
3345}
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003346
3347/*
3348 * Changed something in the current window, at buffer line "lnum", that
3349 * requires that line and possibly other lines to be redrawn.
3350 * Used when entering/leaving Insert mode with the cursor on a folded line.
3351 * Used to remove the "$" from a change command.
3352 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
3353 * may become invalid and the whole window will have to be redrawn.
3354 */
3355 void
3356redrawWinline(
3357 win_T *wp,
3358 linenr_T lnum)
3359{
3360 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
3361 wp->w_redraw_top = lnum;
3362 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
3363 wp->w_redraw_bot = lnum;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003364 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar7528d1f2019-09-19 23:06:20 +02003365}