1 // This is an open source non-commercial project. Dear PVS-Studio, please check
2 // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3
4 // screen.c: code for displaying on the screen
5 //
6 // Output to the screen (console, terminal emulator or GUI window) is minimized
7 // by remembering what is already on the screen, and only updating the parts
8 // that changed.
9 //
10 // The grid_*() functions write to the screen and handle updating grid->lines[].
11 //
12 // update_screen() is the function that updates all windows and status lines.
13 // It is called from the main loop when must_redraw is non-zero. It may be
14 // called from other places when an immediate screen update is needed.
15 //
16 // The part of the buffer that is displayed in a window is set with:
17 // - w_topline (first buffer line in window)
18 // - w_topfill (filler lines above the first line)
19 // - w_leftcol (leftmost window cell in window),
20 // - w_skipcol (skipped window cells of first line)
21 //
22 // Commands that only move the cursor around in a window, do not need to take
23 // action to update the display. The main loop will check if w_topline is
24 // valid and update it (scroll the window) when needed.
25 //
26 // Commands that scroll a window change w_topline and must call
27 // check_cursor() to move the cursor into the visible part of the window, and
28 // call redraw_later(wp, VALID) to have the window displayed by update_screen()
29 // later.
30 //
31 // Commands that change text in the buffer must call changed_bytes() or
32 // changed_lines() to mark the area that changed and will require updating
33 // later. The main loop will call update_screen(), which will update each
34 // window that shows the changed buffer. This assumes text above the change
35 // can remain displayed as it is. Text after the change may need updating for
36 // scrolling, folding and syntax highlighting.
37 //
38 // Commands that change how a window is displayed (e.g., setting 'list') or
39 // invalidate the contents of a window in another way (e.g., change fold
40 // settings), must call redraw_later(wp, NOT_VALID) to have the whole window
41 // redisplayed by update_screen() later.
42 //
43 // Commands that change how a buffer is displayed (e.g., setting 'tabstop')
44 // must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
45 // buffer redisplayed by update_screen() later.
46 //
47 // Commands that change highlighting and possibly cause a scroll too must call
48 // redraw_later(wp, SOME_VALID) to update the whole window but still use
49 // scrolling to avoid redrawing everything. But the length of displayed lines
50 // must not change, use NOT_VALID then.
51 //
52 // Commands that move the window position must call redraw_later(wp, NOT_VALID).
53 // TODO(neovim): should minimize redrawing by scrolling when possible.
54 //
55 // Commands that change everything (e.g., resizing the screen) must call
56 // redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
57 //
58 // Things that are handled indirectly:
59 // - When messages scroll the screen up, msg_scrolled will be set and
60 // update_screen() called to redraw.
61 ///
62
63 #include <assert.h>
64 #include <inttypes.h>
65 #include <stdbool.h>
66 #include <string.h>
67
68 #include "nvim/api/extmark.h"
69 #include "nvim/api/private/helpers.h"
70 #include "nvim/api/vim.h"
71 #include "nvim/arabic.h"
72 #include "nvim/ascii.h"
73 #include "nvim/buffer.h"
74 #include "nvim/charset.h"
75 #include "nvim/cursor.h"
76 #include "nvim/cursor_shape.h"
77 #include "nvim/decoration.h"
78 #include "nvim/diff.h"
79 #include "nvim/edit.h"
80 #include "nvim/eval.h"
81 #include "nvim/ex_cmds.h"
82 #include "nvim/ex_cmds2.h"
83 #include "nvim/ex_getln.h"
84 #include "nvim/extmark.h"
85 #include "nvim/fileio.h"
86 #include "nvim/fold.h"
87 #include "nvim/garray.h"
88 #include "nvim/getchar.h"
89 #include "nvim/highlight.h"
90 #include "nvim/indent.h"
91 #include "nvim/lib/kvec.h"
92 #include "nvim/log.h"
93 #include "nvim/lua/executor.h"
94 #include "nvim/main.h"
95 #include "nvim/mark.h"
96 #include "nvim/mbyte.h"
97 #include "nvim/memline.h"
98 #include "nvim/memory.h"
99 #include "nvim/menu.h"
100 #include "nvim/message.h"
101 #include "nvim/misc1.h"
102 #include "nvim/move.h"
103 #include "nvim/normal.h"
104 #include "nvim/option.h"
105 #include "nvim/os/time.h"
106 #include "nvim/os_unix.h"
107 #include "nvim/path.h"
108 #include "nvim/plines.h"
109 #include "nvim/popupmnu.h"
110 #include "nvim/quickfix.h"
111 #include "nvim/regexp.h"
112 #include "nvim/screen.h"
113 #include "nvim/search.h"
114 #include "nvim/sign.h"
115 #include "nvim/spell.h"
116 #include "nvim/state.h"
117 #include "nvim/strings.h"
118 #include "nvim/syntax.h"
119 #include "nvim/terminal.h"
120 #include "nvim/ui.h"
121 #include "nvim/ui_compositor.h"
122 #include "nvim/undo.h"
123 #include "nvim/version.h"
124 #include "nvim/vim.h"
125 #include "nvim/window.h"
126
127 #define MB_FILLER_CHAR '<' /* character used when a double-width character
128 * doesn't fit. */
129
130 typedef kvec_withinit_t(DecorProvider *, 4) Providers;
131
132 // temporary buffer for rendering a single screenline, so it can be
133 // compared with previous contents to calculate smallest delta.
134 // Per-cell attributes
135 static size_t linebuf_size = 0;
136 static schar_T *linebuf_char = NULL;
137 static sattr_T *linebuf_attr = NULL;
138
139 static match_T search_hl; // used for 'hlsearch' highlight matching
140
141 StlClickDefinition *tab_page_click_defs = NULL;
142
143 long tab_page_click_defs_size = 0;
144
145 // for line_putchar. Contains the state that needs to be remembered from
146 // putting one character to the next.
147 typedef struct {
148 const char *p;
149 int prev_c; // previous Arabic character
150 int prev_c1; // first composing char for prev_c
151 } LineState;
152 #define LINE_STATE(p) { p, 0, 0 }
153
154 /// Whether to call "ui_call_grid_resize" in win_grid_alloc
155 static bool send_grid_resize = false;
156
157 static bool conceal_cursor_used = false;
158
159 static bool redraw_popupmenu = false;
160 static bool msg_grid_invalid = false;
161
162 static bool resizing = false;
163
164
165 #ifdef INCLUDE_GENERATED_DECLARATIONS
166 # include "screen.c.generated.h"
167 #endif
168 #define SEARCH_HL_PRIORITY 0
169
170 static char *provider_err = NULL;
171
provider_invoke(NS ns_id,const char * name,LuaRef ref,Array args,bool default_true)172 static bool provider_invoke(NS ns_id, const char *name, LuaRef ref, Array args, bool default_true)
173 {
174 Error err = ERROR_INIT;
175
176 textlock++;
177 provider_active = true;
178 Object ret = nlua_call_ref(ref, name, args, true, &err);
179 provider_active = false;
180 textlock--;
181
182 if (!ERROR_SET(&err)
183 && api_object_to_bool(ret, "provider %s retval", default_true, &err)) {
184 return true;
185 }
186
187 if (ERROR_SET(&err)) {
188 const char *ns_name = describe_ns(ns_id);
189 ELOG("error in provider %s:%s: %s", ns_name, name, err.msg);
190 bool verbose_errs = true; // TODO(bfredl):
191 if (verbose_errs && provider_err == NULL) {
192 static char errbuf[IOSIZE];
193 snprintf(errbuf, sizeof errbuf, "%s: %s", ns_name, err.msg);
194 provider_err = xstrdup(errbuf);
195 }
196 }
197
198 api_free_object(ret);
199 return false;
200 }
201
202 /// Redraw a window later, with update_screen(type).
203 ///
204 /// Set must_redraw only if not already set to a higher value.
205 /// e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
redraw_later(win_T * wp,int type)206 void redraw_later(win_T *wp, int type)
207 FUNC_ATTR_NONNULL_ALL
208 {
209 if (!exiting && wp->w_redr_type < type) {
210 wp->w_redr_type = type;
211 if (type >= NOT_VALID) {
212 wp->w_lines_valid = 0;
213 }
214 if (must_redraw < type) { // must_redraw is the maximum of all windows
215 must_redraw = type;
216 }
217 }
218 }
219
220 /*
221 * Mark all windows to be redrawn later.
222 */
redraw_all_later(int type)223 void redraw_all_later(int type)
224 {
225 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
226 redraw_later(wp, type);
227 }
228 // This may be needed when switching tabs.
229 if (must_redraw < type) {
230 must_redraw = type;
231 }
232 }
233
screen_invalidate_highlights(void)234 void screen_invalidate_highlights(void)
235 {
236 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
237 redraw_later(wp, NOT_VALID);
238 wp->w_grid_alloc.valid = false;
239 }
240 }
241
242 /*
243 * Mark all windows that are editing the current buffer to be updated later.
244 */
redraw_curbuf_later(int type)245 void redraw_curbuf_later(int type)
246 {
247 redraw_buf_later(curbuf, type);
248 }
249
redraw_buf_later(buf_T * buf,int type)250 void redraw_buf_later(buf_T *buf, int type)
251 {
252 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
253 if (wp->w_buffer == buf) {
254 redraw_later(wp, type);
255 }
256 }
257 }
258
redraw_buf_line_later(buf_T * buf,linenr_T line)259 void redraw_buf_line_later(buf_T *buf, linenr_T line)
260 {
261 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
262 if (wp->w_buffer == buf
263 && line >= wp->w_topline && line < wp->w_botline) {
264 redrawWinline(wp, line);
265 }
266 }
267 }
268
redraw_buf_range_later(buf_T * buf,linenr_T firstline,linenr_T lastline)269 void redraw_buf_range_later(buf_T *buf, linenr_T firstline, linenr_T lastline)
270 {
271 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
272 if (wp->w_buffer == buf
273 && lastline >= wp->w_topline && firstline < wp->w_botline) {
274 if (wp->w_redraw_top == 0 || wp->w_redraw_top > firstline) {
275 wp->w_redraw_top = firstline;
276 }
277 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lastline) {
278 wp->w_redraw_bot = lastline;
279 }
280 redraw_later(wp, VALID);
281 }
282 }
283 }
284
285 /*
286 * Changed something in the current window, at buffer line "lnum", that
287 * requires that line and possibly other lines to be redrawn.
288 * Used when entering/leaving Insert mode with the cursor on a folded line.
289 * Used to remove the "$" from a change command.
290 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
291 * may become invalid and the whole window will have to be redrawn.
292 */
redrawWinline(win_T * wp,linenr_T lnum)293 void redrawWinline(win_T *wp, linenr_T lnum)
294 FUNC_ATTR_NONNULL_ALL
295 {
296 if (lnum >= wp->w_topline
297 && lnum < wp->w_botline) {
298 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum) {
299 wp->w_redraw_top = lnum;
300 }
301 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum) {
302 wp->w_redraw_bot = lnum;
303 }
304 redraw_later(wp, VALID);
305 }
306 }
307
308 /*
309 * update all windows that are editing the current buffer
310 */
update_curbuf(int type)311 void update_curbuf(int type)
312 {
313 redraw_curbuf_later(type);
314 update_screen(type);
315 }
316
317 /// Redraw the parts of the screen that is marked for redraw.
318 ///
319 /// Most code shouldn't call this directly, rather use redraw_later() and
320 /// and redraw_all_later() to mark parts of the screen as needing a redraw.
321 ///
322 /// @param type set to a NOT_VALID to force redraw of entire screen
update_screen(int type)323 int update_screen(int type)
324 {
325 static bool did_intro = false;
326
327 // Don't do anything if the screen structures are (not yet) valid.
328 // A VimResized autocmd can invoke redrawing in the middle of a resize,
329 // which would bypass the checks in screen_resize for popupmenu etc.
330 if (!default_grid.chars || resizing) {
331 return FAIL;
332 }
333
334 // May have postponed updating diffs.
335 if (need_diff_redraw) {
336 diff_redraw(true);
337 }
338
339 if (must_redraw) {
340 if (type < must_redraw) { // use maximal type
341 type = must_redraw;
342 }
343
344 /* must_redraw is reset here, so that when we run into some weird
345 * reason to redraw while busy redrawing (e.g., asynchronous
346 * scrolling), or update_topline() in win_update() will cause a
347 * scroll, the screen will be redrawn later or in win_update(). */
348 must_redraw = 0;
349 }
350
351 // Need to update w_lines[].
352 if (curwin->w_lines_valid == 0 && type < NOT_VALID) {
353 type = NOT_VALID;
354 }
355
356 /* Postpone the redrawing when it's not needed and when being called
357 * recursively. */
358 if (!redrawing() || updating_screen) {
359 must_redraw = type;
360 if (type > INVERTED_ALL) {
361 curwin->w_lines_valid = 0; // don't use w_lines[].wl_size now
362 }
363 return FAIL;
364 }
365 updating_screen = 1;
366
367 display_tick++; // let syntax code know we're in a next round of
368 // display updating
369
370 // Tricky: vim code can reset msg_scrolled behind our back, so need
371 // separate bookkeeping for now.
372 if (msg_did_scroll) {
373 msg_did_scroll = false;
374 msg_scrolled_at_flush = 0;
375 }
376
377 if (type >= CLEAR || !default_grid.valid) {
378 ui_comp_set_screen_valid(false);
379 }
380
381 // if the screen was scrolled up when displaying a message, scroll it down
382 if (msg_scrolled || msg_grid_invalid) {
383 clear_cmdline = true;
384 int valid = MAX(Rows - msg_scrollsize(), 0);
385 if (msg_grid.chars) {
386 // non-displayed part of msg_grid is considered invalid.
387 for (int i = 0; i < MIN(msg_scrollsize(), msg_grid.Rows); i++) {
388 grid_clear_line(&msg_grid, msg_grid.line_offset[i],
389 msg_grid.Columns, false);
390 }
391 }
392 if (msg_use_msgsep()) {
393 msg_grid.throttled = false;
394 // CLEAR is already handled
395 if (type == NOT_VALID && !ui_has(kUIMultigrid) && msg_scrolled) {
396 ui_comp_set_screen_valid(false);
397 for (int i = valid; i < Rows-p_ch; i++) {
398 grid_clear_line(&default_grid, default_grid.line_offset[i],
399 Columns, false);
400 }
401 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
402 if (wp->w_floating) {
403 continue;
404 }
405 if (W_ENDROW(wp) > valid) {
406 wp->w_redr_type = MAX(wp->w_redr_type, NOT_VALID);
407 }
408 if (W_ENDROW(wp) + wp->w_status_height > valid) {
409 wp->w_redr_status = true;
410 }
411 }
412 }
413 msg_grid_set_pos(Rows-p_ch, false);
414 msg_grid_invalid = false;
415 } else if (msg_scrolled > Rows - 5) { // clearing is faster
416 type = CLEAR;
417 } else if (type != CLEAR) {
418 check_for_delay(false);
419 grid_ins_lines(&default_grid, 0, msg_scrolled, Rows, 0, Columns);
420 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
421 if (wp->w_floating) {
422 continue;
423 }
424 if (wp->w_winrow < msg_scrolled) {
425 if (W_ENDROW(wp) > msg_scrolled
426 && wp->w_redr_type < REDRAW_TOP
427 && wp->w_lines_valid > 0
428 && wp->w_topline == wp->w_lines[0].wl_lnum) {
429 wp->w_upd_rows = msg_scrolled - wp->w_winrow;
430 wp->w_redr_type = REDRAW_TOP;
431 } else {
432 wp->w_redr_type = NOT_VALID;
433 if (W_ENDROW(wp) + wp->w_status_height
434 <= msg_scrolled) {
435 wp->w_redr_status = TRUE;
436 }
437 }
438 }
439 }
440 redraw_cmdline = true;
441 redraw_tabline = true;
442 }
443 msg_scrolled = 0;
444 msg_scrolled_at_flush = 0;
445 need_wait_return = false;
446 }
447
448 win_ui_flush();
449 msg_ext_check_clear();
450
451 // reset cmdline_row now (may have been changed temporarily)
452 compute_cmdrow();
453
454 // Check for changed highlighting
455 if (need_highlight_changed) {
456 highlight_changed();
457 }
458
459 if (type == CLEAR) { // first clear screen
460 screenclear(); // will reset clear_cmdline
461 cmdline_screen_cleared(); // clear external cmdline state
462 type = NOT_VALID;
463 // must_redraw may be set indirectly, avoid another redraw later
464 must_redraw = 0;
465 } else if (!default_grid.valid) {
466 grid_invalidate(&default_grid);
467 default_grid.valid = true;
468 }
469
470 // After disabling msgsep the grid might not have been deallocated yet,
471 // hence we also need to check msg_grid.chars
472 if (type == NOT_VALID && (msg_use_grid() || msg_grid.chars)) {
473 grid_fill(&default_grid, Rows-p_ch, Rows, 0, Columns, ' ', ' ', 0);
474 }
475
476 ui_comp_set_screen_valid(true);
477
478 Providers providers;
479 kvi_init(providers);
480 for (size_t i = 0; i < kv_size(decor_providers); i++) {
481 DecorProvider *p = &kv_A(decor_providers, i);
482 if (!p->active) {
483 continue;
484 }
485
486 bool active;
487 if (p->redraw_start != LUA_NOREF) {
488 FIXED_TEMP_ARRAY(args, 2);
489 args.items[0] = INTEGER_OBJ(display_tick);
490 args.items[1] = INTEGER_OBJ(type);
491 active = provider_invoke(p->ns_id, "start", p->redraw_start, args, true);
492 } else {
493 active = true;
494 }
495
496 if (active) {
497 kvi_push(providers, p);
498 }
499 }
500
501 // "start" callback could have changed highlights for global elements
502 if (win_check_ns_hl(NULL)) {
503 redraw_cmdline = true;
504 redraw_tabline = true;
505 }
506
507 if (clear_cmdline) { // going to clear cmdline (done below)
508 check_for_delay(false);
509 }
510
511 /* Force redraw when width of 'number' or 'relativenumber' column
512 * changes. */
513 if (curwin->w_redr_type < NOT_VALID
514 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
515 ? number_width(curwin) : 0)) {
516 curwin->w_redr_type = NOT_VALID;
517 }
518
519 /*
520 * Only start redrawing if there is really something to do.
521 */
522 if (type == INVERTED) {
523 update_curswant();
524 }
525 if (curwin->w_redr_type < type
526 && !((type == VALID
527 && curwin->w_lines[0].wl_valid
528 && curwin->w_topfill == curwin->w_old_topfill
529 && curwin->w_botfill == curwin->w_old_botfill
530 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
531 || (type == INVERTED
532 && VIsual_active
533 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
534 && curwin->w_old_visual_mode == VIsual_mode
535 && (curwin->w_valid & VALID_VIRTCOL)
536 && curwin->w_old_curswant == curwin->w_curswant)
537 )) {
538 curwin->w_redr_type = type;
539 }
540
541 // Redraw the tab pages line if needed.
542 if (redraw_tabline || type >= NOT_VALID) {
543 update_window_hl(curwin, type >= NOT_VALID);
544 FOR_ALL_TABS(tp) {
545 if (tp != curtab) {
546 update_window_hl(tp->tp_curwin, type >= NOT_VALID);
547 }
548 }
549 draw_tabline();
550 }
551
552 /*
553 * Correct stored syntax highlighting info for changes in each displayed
554 * buffer. Each buffer must only be done once.
555 */
556 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
557 update_window_hl(wp, type >= NOT_VALID);
558
559 buf_T *buf = wp->w_buffer;
560 if (buf->b_mod_set) {
561 if (buf->b_mod_tick_syn < display_tick
562 && syntax_present(wp)) {
563 syn_stack_apply_changes(buf);
564 buf->b_mod_tick_syn = display_tick;
565 }
566
567 if (buf->b_mod_tick_decor < display_tick) {
568 for (size_t i = 0; i < kv_size(providers); i++) {
569 DecorProvider *p = kv_A(providers, i);
570 if (p && p->redraw_buf != LUA_NOREF) {
571 FIXED_TEMP_ARRAY(args, 1);
572 args.items[0] = BUFFER_OBJ(buf->handle);
573 provider_invoke(p->ns_id, "buf", p->redraw_buf, args, true);
574 }
575 }
576 buf->b_mod_tick_decor = display_tick;
577 }
578 }
579 }
580
581 /*
582 * Go from top to bottom through the windows, redrawing the ones that need
583 * it.
584 */
585 bool did_one = false;
586 search_hl.rm.regprog = NULL;
587
588
589 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
590 if (wp->w_redr_type == CLEAR && wp->w_floating && wp->w_grid_alloc.chars) {
591 grid_invalidate(&wp->w_grid_alloc);
592 wp->w_redr_type = NOT_VALID;
593 }
594
595 // reallocate grid if needed.
596 win_grid_alloc(wp);
597
598 if (wp->w_redr_border || wp->w_redr_type >= NOT_VALID) {
599 win_redr_border(wp);
600 }
601
602 if (wp->w_redr_type != 0) {
603 if (!did_one) {
604 did_one = true;
605 start_search_hl();
606 }
607 win_update(wp, &providers);
608 }
609
610 // redraw status line after the window to minimize cursor movement
611 if (wp->w_redr_status) {
612 win_redr_status(wp);
613 }
614 }
615
616 end_search_hl();
617
618 // May need to redraw the popup menu.
619 if (pum_drawn() && must_redraw_pum) {
620 pum_redraw();
621 }
622
623 send_grid_resize = false;
624
625 /* Reset b_mod_set flags. Going through all windows is probably faster
626 * than going through all buffers (there could be many buffers). */
627 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
628 wp->w_buffer->b_mod_set = false;
629 }
630
631 updating_screen = 0;
632
633 /* Clear or redraw the command line. Done last, because scrolling may
634 * mess up the command line. */
635 if (clear_cmdline || redraw_cmdline) {
636 showmode();
637 }
638
639 // May put up an introductory message when not editing a file
640 if (!did_intro) {
641 maybe_intro_message();
642 }
643 did_intro = true;
644
645 for (size_t i = 0; i < kv_size(providers); i++) {
646 DecorProvider *p = kv_A(providers, i);
647 if (!p->active) {
648 continue;
649 }
650
651 if (p->redraw_end != LUA_NOREF) {
652 FIXED_TEMP_ARRAY(args, 1);
653 args.items[0] = INTEGER_OBJ(display_tick);
654 provider_invoke(p->ns_id, "end", p->redraw_end, args, true);
655 }
656 }
657 kvi_destroy(providers);
658
659
660 // either cmdline is cleared, not drawn or mode is last drawn
661 cmdline_was_last_drawn = false;
662 return OK;
663 }
664
665 // Return true if the cursor line in window "wp" may be concealed, according
666 // to the 'concealcursor' option.
conceal_cursor_line(const win_T * wp)667 bool conceal_cursor_line(const win_T *wp)
668 FUNC_ATTR_NONNULL_ALL
669 {
670 int c;
671
672 if (*wp->w_p_cocu == NUL) {
673 return false;
674 }
675 if (get_real_state() & VISUAL) {
676 c = 'v';
677 } else if (State & INSERT) {
678 c = 'i';
679 } else if (State & NORMAL) {
680 c = 'n';
681 } else if (State & CMDLINE) {
682 c = 'c';
683 } else {
684 return false;
685 }
686 return vim_strchr(wp->w_p_cocu, c) != NULL;
687 }
688
689 // Check if the cursor line needs to be redrawn because of 'concealcursor'.
690 //
691 // When cursor is moved at the same time, both lines will be redrawn regardless.
conceal_check_cursor_line(void)692 void conceal_check_cursor_line(void)
693 {
694 bool should_conceal = conceal_cursor_line(curwin);
695 if (curwin->w_p_cole > 0 && (conceal_cursor_used != should_conceal)) {
696 redrawWinline(curwin, curwin->w_cursor.lnum);
697 // Need to recompute cursor column, e.g., when starting Visual mode
698 // without concealing.
699 curs_columns(curwin, true);
700 }
701 }
702
703 /// Whether cursorline is drawn in a special way
704 ///
705 /// If true, both old and new cursorline will need
706 /// to be redrawn when moving cursor within windows.
707 /// TODO(bfredl): VIsual_active shouldn't be needed, but is used to fix a glitch
708 /// caused by scrolling.
win_cursorline_standout(const win_T * wp)709 bool win_cursorline_standout(const win_T *wp)
710 FUNC_ATTR_NONNULL_ALL
711 {
712 return wp->w_p_cul
713 || (wp->w_p_cole > 0 && (VIsual_active || !conceal_cursor_line(wp)));
714 }
715
716 /*
717 * Update a single window.
718 *
719 * This may cause the windows below it also to be redrawn (when clearing the
720 * screen or scrolling lines).
721 *
722 * How the window is redrawn depends on wp->w_redr_type. Each type also
723 * implies the one below it.
724 * NOT_VALID redraw the whole window
725 * SOME_VALID redraw the whole window but do scroll when possible
726 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
727 * INVERTED redraw the changed part of the Visual area
728 * INVERTED_ALL redraw the whole Visual area
729 * VALID 1. scroll up/down to adjust for a changed w_topline
730 * 2. update lines at the top when scrolled down
731 * 3. redraw changed text:
732 * - if wp->w_buffer->b_mod_set set, update lines between
733 * b_mod_top and b_mod_bot.
734 * - if wp->w_redraw_top non-zero, redraw lines between
735 * wp->w_redraw_top and wp->w_redr_bot.
736 * - continue redrawing when syntax status is invalid.
737 * 4. if scrolled up, update lines at the bottom.
738 * This results in three areas that may need updating:
739 * top: from first row to top_end (when scrolled down)
740 * mid: from mid_start to mid_end (update inversion or changed text)
741 * bot: from bot_start to last row (when scrolled up)
742 */
win_update(win_T * wp,Providers * providers)743 static void win_update(win_T *wp, Providers *providers)
744 {
745 buf_T *buf = wp->w_buffer;
746 int type;
747 int top_end = 0; /* Below last row of the top area that needs
748 updating. 0 when no top area updating. */
749 int mid_start = 999; /* first row of the mid area that needs
750 updating. 999 when no mid area updating. */
751 int mid_end = 0; /* Below last row of the mid area that needs
752 updating. 0 when no mid area updating. */
753 int bot_start = 999; /* first row of the bot area that needs
754 updating. 999 when no bot area updating */
755 bool scrolled_down = false; // true when scrolled down when w_topline got smaller a bit
756 bool top_to_mod = false; // redraw above mod_top
757
758 int row; // current window row to display
759 linenr_T lnum; // current buffer lnum to display
760 int idx; // current index in w_lines[]
761 int srow; // starting row of the current line
762
763 bool eof = false; // if true, we hit the end of the file
764 bool didline = false; // if true, we finished the last line
765 int i;
766 long j;
767 static bool recursive = false; // being called recursively
768 const linenr_T old_botline = wp->w_botline;
769 // Remember what happened to the previous line.
770 #define DID_NONE 1 // didn't update a line
771 #define DID_LINE 2 // updated a normal line
772 #define DID_FOLD 3 // updated a folded line
773 int did_update = DID_NONE;
774 linenr_T syntax_last_parsed = 0; // last parsed text line
775 linenr_T mod_top = 0;
776 linenr_T mod_bot = 0;
777 int save_got_int;
778
779
780 // If we can compute a change in the automatic sizing of the sign column
781 // under 'signcolumn=auto:X' and signs currently placed in the buffer, better
782 // figuring it out here so we can redraw the entire screen for it.
783 buf_signcols(buf);
784
785 type = wp->w_redr_type;
786
787 if (type >= NOT_VALID) {
788 wp->w_redr_status = true;
789 wp->w_lines_valid = 0;
790 }
791
792 // Window is zero-height: nothing to draw.
793 if (wp->w_grid.Rows == 0) {
794 wp->w_redr_type = 0;
795 return;
796 }
797
798 // Window is zero-width: Only need to draw the separator.
799 if (wp->w_grid.Columns == 0) {
800 // draw the vertical separator right of this window
801 draw_vsep_win(wp, 0);
802 wp->w_redr_type = 0;
803 return;
804 }
805
806 init_search_hl(wp);
807
808 /* Force redraw when width of 'number' or 'relativenumber' column
809 * changes. */
810 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
811 if (wp->w_nrwidth != i) {
812 type = NOT_VALID;
813 wp->w_nrwidth = i;
814
815 if (buf->terminal) {
816 terminal_check_size(buf->terminal);
817 }
818 } else if (buf->b_mod_set
819 && buf->b_mod_xlines != 0
820 && wp->w_redraw_top != 0) {
821 // When there are both inserted/deleted lines and specific lines to be
822 // redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
823 // everything (only happens when redrawing is off for while).
824 type = NOT_VALID;
825 } else {
826 /*
827 * Set mod_top to the first line that needs displaying because of
828 * changes. Set mod_bot to the first line after the changes.
829 */
830 mod_top = wp->w_redraw_top;
831 if (wp->w_redraw_bot != 0) {
832 mod_bot = wp->w_redraw_bot + 1;
833 } else {
834 mod_bot = 0;
835 }
836 if (buf->b_mod_set) {
837 if (mod_top == 0 || mod_top > buf->b_mod_top) {
838 mod_top = buf->b_mod_top;
839 /* Need to redraw lines above the change that may be included
840 * in a pattern match. */
841 if (syntax_present(wp)) {
842 mod_top -= buf->b_s.b_syn_sync_linebreaks;
843 if (mod_top < 1) {
844 mod_top = 1;
845 }
846 }
847 }
848 if (mod_bot == 0 || mod_bot < buf->b_mod_bot) {
849 mod_bot = buf->b_mod_bot;
850 }
851
852 // When 'hlsearch' is on and using a multi-line search pattern, a
853 // change in one line may make the Search highlighting in a
854 // previous line invalid. Simple solution: redraw all visible
855 // lines above the change.
856 // Same for a match pattern.
857 if (search_hl.rm.regprog != NULL
858 && re_multiline(search_hl.rm.regprog)) {
859 top_to_mod = true;
860 } else {
861 const matchitem_T *cur = wp->w_match_head;
862 while (cur != NULL) {
863 if (cur->match.regprog != NULL
864 && re_multiline(cur->match.regprog)) {
865 top_to_mod = true;
866 break;
867 }
868 cur = cur->next;
869 }
870 }
871 }
872 if (mod_top != 0 && hasAnyFolding(wp)) {
873 linenr_T lnumt, lnumb;
874
875 /*
876 * A change in a line can cause lines above it to become folded or
877 * unfolded. Find the top most buffer line that may be affected.
878 * If the line was previously folded and displayed, get the first
879 * line of that fold. If the line is folded now, get the first
880 * folded line. Use the minimum of these two.
881 */
882
883 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
884 * the line below it. If there is no valid entry, use w_topline.
885 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
886 * to this line. If there is no valid entry, use MAXLNUM. */
887 lnumt = wp->w_topline;
888 lnumb = MAXLNUM;
889 for (i = 0; i < wp->w_lines_valid; ++i) {
890 if (wp->w_lines[i].wl_valid) {
891 if (wp->w_lines[i].wl_lastlnum < mod_top) {
892 lnumt = wp->w_lines[i].wl_lastlnum + 1;
893 }
894 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot) {
895 lnumb = wp->w_lines[i].wl_lnum;
896 // When there is a fold column it might need updating
897 // in the next line ("J" just above an open fold).
898 if (compute_foldcolumn(wp, 0) > 0) {
899 lnumb++;
900 }
901 }
902 }
903 }
904
905 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, true, NULL);
906 if (mod_top > lnumt) {
907 mod_top = lnumt;
908 }
909
910 // Now do the same for the bottom line (one above mod_bot).
911 mod_bot--;
912 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, true, NULL);
913 mod_bot++;
914 if (mod_bot < lnumb) {
915 mod_bot = lnumb;
916 }
917 }
918
919 /* When a change starts above w_topline and the end is below
920 * w_topline, start redrawing at w_topline.
921 * If the end of the change is above w_topline: do like no change was
922 * made, but redraw the first line to find changes in syntax. */
923 if (mod_top != 0 && mod_top < wp->w_topline) {
924 if (mod_bot > wp->w_topline) {
925 mod_top = wp->w_topline;
926 } else if (syntax_present(wp)) {
927 top_end = 1;
928 }
929 }
930
931 /* When line numbers are displayed need to redraw all lines below
932 * inserted/deleted lines. */
933 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu) {
934 mod_bot = MAXLNUM;
935 }
936 }
937 wp->w_redraw_top = 0; // reset for next time
938 wp->w_redraw_bot = 0;
939
940 /*
941 * When only displaying the lines at the top, set top_end. Used when
942 * window has scrolled down for msg_scrolled.
943 */
944 if (type == REDRAW_TOP) {
945 j = 0;
946 for (i = 0; i < wp->w_lines_valid; ++i) {
947 j += wp->w_lines[i].wl_size;
948 if (j >= wp->w_upd_rows) {
949 top_end = j;
950 break;
951 }
952 }
953 if (top_end == 0) {
954 // not found (cannot happen?): redraw everything
955 type = NOT_VALID;
956 } else {
957 // top area defined, the rest is VALID
958 type = VALID;
959 }
960 }
961
962 /*
963 * If there are no changes on the screen that require a complete redraw,
964 * handle three cases:
965 * 1: we are off the top of the screen by a few lines: scroll down
966 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
967 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
968 * w_lines[] that needs updating.
969 */
970 if ((type == VALID || type == SOME_VALID
971 || type == INVERTED || type == INVERTED_ALL)
972 && !wp->w_botfill && !wp->w_old_botfill) {
973 if (mod_top != 0
974 && wp->w_topline == mod_top
975 && (!wp->w_lines[0].wl_valid
976 || wp->w_topline <= wp->w_lines[0].wl_lnum)) {
977 // w_topline is the first changed line and window is not scrolled,
978 // the scrolling from changed lines will be done further down.
979 } else if (wp->w_lines[0].wl_valid
980 && (wp->w_topline < wp->w_lines[0].wl_lnum
981 || (wp->w_topline == wp->w_lines[0].wl_lnum
982 && wp->w_topfill > wp->w_old_topfill)
983 )) {
984 /*
985 * New topline is above old topline: May scroll down.
986 */
987 if (hasAnyFolding(wp)) {
988 linenr_T ln;
989
990 /* count the number of lines we are off, counting a sequence
991 * of folded lines as one */
992 j = 0;
993 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ln++) {
994 j++;
995 if (j >= wp->w_grid.Rows - 2) {
996 break;
997 }
998 (void)hasFoldingWin(wp, ln, NULL, &ln, true, NULL);
999 }
1000 } else {
1001 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1002 }
1003 if (j < wp->w_grid.Rows - 2) { // not too far off
1004 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1005 // insert extra lines for previously invisible filler lines
1006 if (wp->w_lines[0].wl_lnum != wp->w_topline) {
1007 i += win_get_fill(wp, wp->w_lines[0].wl_lnum) - wp->w_old_topfill;
1008 }
1009 if (i != 0 && i < wp->w_grid.Rows - 2) { // less than a screen off
1010 // Try to insert the correct number of lines.
1011 // If not the last window, delete the lines at the bottom.
1012 // win_ins_lines may fail when the terminal can't do it.
1013 win_scroll_lines(wp, 0, i);
1014 if (wp->w_lines_valid != 0) {
1015 // Need to update rows that are new, stop at the
1016 // first one that scrolled down.
1017 top_end = i;
1018 scrolled_down = true;
1019
1020 // Move the entries that were scrolled, disable
1021 // the entries for the lines to be redrawn.
1022 if ((wp->w_lines_valid += j) > wp->w_grid.Rows) {
1023 wp->w_lines_valid = wp->w_grid.Rows;
1024 }
1025 for (idx = wp->w_lines_valid; idx - j >= 0; idx--) {
1026 wp->w_lines[idx] = wp->w_lines[idx - j];
1027 }
1028 while (idx >= 0) {
1029 wp->w_lines[idx--].wl_valid = false;
1030 }
1031 }
1032 } else {
1033 mid_start = 0; // redraw all lines
1034 }
1035 } else {
1036 mid_start = 0; // redraw all lines
1037 }
1038 } else {
1039 /*
1040 * New topline is at or below old topline: May scroll up.
1041 * When topline didn't change, find first entry in w_lines[] that
1042 * needs updating.
1043 */
1044
1045 // try to find wp->w_topline in wp->w_lines[].wl_lnum
1046 j = -1;
1047 row = 0;
1048 for (i = 0; i < wp->w_lines_valid; i++) {
1049 if (wp->w_lines[i].wl_valid
1050 && wp->w_lines[i].wl_lnum == wp->w_topline) {
1051 j = i;
1052 break;
1053 }
1054 row += wp->w_lines[i].wl_size;
1055 }
1056 if (j == -1) {
1057 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1058 * lines */
1059 mid_start = 0;
1060 } else {
1061 /*
1062 * Try to delete the correct number of lines.
1063 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1064 */
1065 /* If the topline didn't change, delete old filler lines,
1066 * otherwise delete filler lines of the new topline... */
1067 if (wp->w_lines[0].wl_lnum == wp->w_topline) {
1068 row += wp->w_old_topfill;
1069 } else {
1070 row += win_get_fill(wp, wp->w_topline);
1071 }
1072 // ... but don't delete new filler lines.
1073 row -= wp->w_topfill;
1074 if (row > 0) {
1075 win_scroll_lines(wp, 0, -row);
1076 bot_start = wp->w_grid.Rows - row;
1077 }
1078 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0) {
1079 /*
1080 * Skip the lines (below the deleted lines) that are still
1081 * valid and don't need redrawing. Copy their info
1082 * upwards, to compensate for the deleted lines. Set
1083 * bot_start to the first row that needs redrawing.
1084 */
1085 bot_start = 0;
1086 idx = 0;
1087 for (;;) {
1088 wp->w_lines[idx] = wp->w_lines[j];
1089 /* stop at line that didn't fit, unless it is still
1090 * valid (no lines deleted) */
1091 if (row > 0 && bot_start + row
1092 + (int)wp->w_lines[j].wl_size > wp->w_grid.Rows) {
1093 wp->w_lines_valid = idx + 1;
1094 break;
1095 }
1096 bot_start += wp->w_lines[idx++].wl_size;
1097
1098 // stop at the last valid entry in w_lines[].wl_size
1099 if (++j >= wp->w_lines_valid) {
1100 wp->w_lines_valid = idx;
1101 break;
1102 }
1103 }
1104
1105 // Correct the first entry for filler lines at the top
1106 // when it won't get updated below.
1107 if (win_may_fill(wp) && bot_start > 0) {
1108 wp->w_lines[0].wl_size = (plines_win_nofill(wp, wp->w_topline, true)
1109 + wp->w_topfill);
1110 }
1111 }
1112 }
1113 }
1114
1115 // When starting redraw in the first line, redraw all lines.
1116 if (mid_start == 0) {
1117 mid_end = wp->w_grid.Rows;
1118 }
1119 } else {
1120 // Not VALID or INVERTED: redraw all lines.
1121 mid_start = 0;
1122 mid_end = wp->w_grid.Rows;
1123 }
1124
1125 if (type == SOME_VALID) {
1126 // SOME_VALID: redraw all lines.
1127 mid_start = 0;
1128 mid_end = wp->w_grid.Rows;
1129 type = NOT_VALID;
1130 }
1131
1132 // check if we are updating or removing the inverted part
1133 if ((VIsual_active && buf == curwin->w_buffer)
1134 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID)) {
1135 linenr_T from, to;
1136
1137 if (VIsual_active) {
1138 if (VIsual_mode != wp->w_old_visual_mode || type == INVERTED_ALL) {
1139 // If the type of Visual selection changed, redraw the whole
1140 // selection. Also when the ownership of the X selection is
1141 // gained or lost.
1142 if (curwin->w_cursor.lnum < VIsual.lnum) {
1143 from = curwin->w_cursor.lnum;
1144 to = VIsual.lnum;
1145 } else {
1146 from = VIsual.lnum;
1147 to = curwin->w_cursor.lnum;
1148 }
1149 // redraw more when the cursor moved as well
1150 if (wp->w_old_cursor_lnum < from) {
1151 from = wp->w_old_cursor_lnum;
1152 }
1153 if (wp->w_old_cursor_lnum > to) {
1154 to = wp->w_old_cursor_lnum;
1155 }
1156 if (wp->w_old_visual_lnum < from) {
1157 from = wp->w_old_visual_lnum;
1158 }
1159 if (wp->w_old_visual_lnum > to) {
1160 to = wp->w_old_visual_lnum;
1161 }
1162 } else {
1163 /*
1164 * Find the line numbers that need to be updated: The lines
1165 * between the old cursor position and the current cursor
1166 * position. Also check if the Visual position changed.
1167 */
1168 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum) {
1169 from = curwin->w_cursor.lnum;
1170 to = wp->w_old_cursor_lnum;
1171 } else {
1172 from = wp->w_old_cursor_lnum;
1173 to = curwin->w_cursor.lnum;
1174 if (from == 0) { // Visual mode just started
1175 from = to;
1176 }
1177 }
1178
1179 if (VIsual.lnum != wp->w_old_visual_lnum
1180 || VIsual.col != wp->w_old_visual_col) {
1181 if (wp->w_old_visual_lnum < from
1182 && wp->w_old_visual_lnum != 0) {
1183 from = wp->w_old_visual_lnum;
1184 }
1185 if (wp->w_old_visual_lnum > to) {
1186 to = wp->w_old_visual_lnum;
1187 }
1188 if (VIsual.lnum < from) {
1189 from = VIsual.lnum;
1190 }
1191 if (VIsual.lnum > to) {
1192 to = VIsual.lnum;
1193 }
1194 }
1195 }
1196
1197 /*
1198 * If in block mode and changed column or curwin->w_curswant:
1199 * update all lines.
1200 * First compute the actual start and end column.
1201 */
1202 if (VIsual_mode == Ctrl_V) {
1203 colnr_T fromc, toc;
1204 int save_ve_flags = ve_flags;
1205
1206 if (curwin->w_p_lbr) {
1207 ve_flags = VE_ALL;
1208 }
1209
1210 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1211 ve_flags = save_ve_flags;
1212 toc++;
1213 // Highlight to the end of the line, unless 'virtualedit' has
1214 // "block".
1215 if (curwin->w_curswant == MAXCOL && !(ve_flags & VE_BLOCK)) {
1216 toc = MAXCOL;
1217 }
1218
1219 if (fromc != wp->w_old_cursor_fcol
1220 || toc != wp->w_old_cursor_lcol) {
1221 if (from > VIsual.lnum) {
1222 from = VIsual.lnum;
1223 }
1224 if (to < VIsual.lnum) {
1225 to = VIsual.lnum;
1226 }
1227 }
1228 wp->w_old_cursor_fcol = fromc;
1229 wp->w_old_cursor_lcol = toc;
1230 }
1231 } else {
1232 // Use the line numbers of the old Visual area.
1233 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum) {
1234 from = wp->w_old_cursor_lnum;
1235 to = wp->w_old_visual_lnum;
1236 } else {
1237 from = wp->w_old_visual_lnum;
1238 to = wp->w_old_cursor_lnum;
1239 }
1240 }
1241
1242 /*
1243 * There is no need to update lines above the top of the window.
1244 */
1245 if (from < wp->w_topline) {
1246 from = wp->w_topline;
1247 }
1248
1249 /*
1250 * If we know the value of w_botline, use it to restrict the update to
1251 * the lines that are visible in the window.
1252 */
1253 if (wp->w_valid & VALID_BOTLINE) {
1254 if (from >= wp->w_botline) {
1255 from = wp->w_botline - 1;
1256 }
1257 if (to >= wp->w_botline) {
1258 to = wp->w_botline - 1;
1259 }
1260 }
1261
1262 /*
1263 * Find the minimal part to be updated.
1264 * Watch out for scrolling that made entries in w_lines[] invalid.
1265 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1266 * top_end; need to redraw from top_end to the "to" line.
1267 * A middle mouse click with a Visual selection may change the text
1268 * above the Visual area and reset wl_valid, do count these for
1269 * mid_end (in srow).
1270 */
1271 if (mid_start > 0) {
1272 lnum = wp->w_topline;
1273 idx = 0;
1274 srow = 0;
1275 if (scrolled_down) {
1276 mid_start = top_end;
1277 } else {
1278 mid_start = 0;
1279 }
1280 while (lnum < from && idx < wp->w_lines_valid) { // find start
1281 if (wp->w_lines[idx].wl_valid) {
1282 mid_start += wp->w_lines[idx].wl_size;
1283 } else if (!scrolled_down) {
1284 srow += wp->w_lines[idx].wl_size;
1285 }
1286 ++idx;
1287 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid) {
1288 lnum = wp->w_lines[idx].wl_lnum;
1289 } else {
1290 ++lnum;
1291 }
1292 }
1293 srow += mid_start;
1294 mid_end = wp->w_grid.Rows;
1295 for (; idx < wp->w_lines_valid; idx++) { // find end
1296 if (wp->w_lines[idx].wl_valid
1297 && wp->w_lines[idx].wl_lnum >= to + 1) {
1298 // Only update until first row of this line
1299 mid_end = srow;
1300 break;
1301 }
1302 srow += wp->w_lines[idx].wl_size;
1303 }
1304 }
1305 }
1306
1307 if (VIsual_active && buf == curwin->w_buffer) {
1308 wp->w_old_visual_mode = VIsual_mode;
1309 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1310 wp->w_old_visual_lnum = VIsual.lnum;
1311 wp->w_old_visual_col = VIsual.col;
1312 wp->w_old_curswant = curwin->w_curswant;
1313 } else {
1314 wp->w_old_visual_mode = 0;
1315 wp->w_old_cursor_lnum = 0;
1316 wp->w_old_visual_lnum = 0;
1317 wp->w_old_visual_col = 0;
1318 }
1319
1320 // reset got_int, otherwise regexp won't work
1321 save_got_int = got_int;
1322 got_int = 0;
1323 // Set the time limit to 'redrawtime'.
1324 proftime_T syntax_tm = profile_setlimit(p_rdt);
1325 syn_set_timeout(&syntax_tm);
1326
1327 /*
1328 * Update all the window rows.
1329 */
1330 idx = 0; // first entry in w_lines[].wl_size
1331 row = 0;
1332 srow = 0;
1333 lnum = wp->w_topline; // first line shown in window
1334
1335 decor_redraw_reset(buf, &decor_state);
1336
1337 Providers line_providers;
1338 kvi_init(line_providers);
1339
1340 linenr_T knownmax = ((wp->w_valid & VALID_BOTLINE)
1341 ? wp->w_botline
1342 : (wp->w_topline + wp->w_height_inner));
1343
1344 for (size_t k = 0; k < kv_size(*providers); k++) {
1345 DecorProvider *p = kv_A(*providers, k);
1346 if (p && p->redraw_win != LUA_NOREF) {
1347 FIXED_TEMP_ARRAY(args, 4);
1348 args.items[0] = WINDOW_OBJ(wp->handle);
1349 args.items[1] = BUFFER_OBJ(buf->handle);
1350 // TODO(bfredl): we are not using this, but should be first drawn line?
1351 args.items[2] = INTEGER_OBJ(wp->w_topline-1);
1352 args.items[3] = INTEGER_OBJ(knownmax);
1353 if (provider_invoke(p->ns_id, "win", p->redraw_win, args, true)) {
1354 kvi_push(line_providers, p);
1355 }
1356 }
1357 }
1358
1359 win_check_ns_hl(wp);
1360
1361
1362 for (;;) {
1363 /* stop updating when reached the end of the window (check for _past_
1364 * the end of the window is at the end of the loop) */
1365 if (row == wp->w_grid.Rows) {
1366 didline = true;
1367 break;
1368 }
1369
1370 // stop updating when hit the end of the file
1371 if (lnum > buf->b_ml.ml_line_count) {
1372 eof = true;
1373 break;
1374 }
1375
1376 /* Remember the starting row of the line that is going to be dealt
1377 * with. It is used further down when the line doesn't fit. */
1378 srow = row;
1379
1380 // Update a line when it is in an area that needs updating, when it
1381 // has changes or w_lines[idx] is invalid.
1382 // "bot_start" may be halfway a wrapped line after using
1383 // win_scroll_lines(), check if the current line includes it.
1384 // When syntax folding is being used, the saved syntax states will
1385 // already have been updated, we can't see where the syntax state is
1386 // the same again, just update until the end of the window.
1387 if (row < top_end
1388 || (row >= mid_start && row < mid_end)
1389 || top_to_mod
1390 || idx >= wp->w_lines_valid
1391 || (row + wp->w_lines[idx].wl_size > bot_start)
1392 || (mod_top != 0
1393 && (lnum == mod_top
1394 || (lnum >= mod_top
1395 && (lnum < mod_bot
1396 || did_update == DID_FOLD
1397 || (did_update == DID_LINE
1398 && syntax_present(wp)
1399 && ((foldmethodIsSyntax(wp)
1400 && hasAnyFolding(wp))
1401 || syntax_check_changed(lnum)))
1402 // match in fixed position might need redraw
1403 // if lines were inserted or deleted
1404 || (wp->w_match_head != NULL
1405 && buf->b_mod_xlines != 0)))))
1406 || (wp->w_p_cul && (lnum == wp->w_cursor.lnum
1407 || lnum == wp->w_last_cursorline))) {
1408 if (lnum == mod_top) {
1409 top_to_mod = false;
1410 }
1411
1412 /*
1413 * When at start of changed lines: May scroll following lines
1414 * up or down to minimize redrawing.
1415 * Don't do this when the change continues until the end.
1416 * Don't scroll when dollar_vcol >= 0, keep the "$".
1417 * Don't scroll when redrawing the top, scrolled already above.
1418 */
1419 if (lnum == mod_top
1420 && mod_bot != MAXLNUM
1421 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1)
1422 && row >= top_end) {
1423 int old_rows = 0;
1424 int new_rows = 0;
1425 int xtra_rows;
1426 linenr_T l;
1427
1428 /* Count the old number of window rows, using w_lines[], which
1429 * should still contain the sizes for the lines as they are
1430 * currently displayed. */
1431 for (i = idx; i < wp->w_lines_valid; ++i) {
1432 /* Only valid lines have a meaningful wl_lnum. Invalid
1433 * lines are part of the changed area. */
1434 if (wp->w_lines[i].wl_valid
1435 && wp->w_lines[i].wl_lnum == mod_bot) {
1436 break;
1437 }
1438 old_rows += wp->w_lines[i].wl_size;
1439 if (wp->w_lines[i].wl_valid
1440 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot) {
1441 /* Must have found the last valid entry above mod_bot.
1442 * Add following invalid entries. */
1443 ++i;
1444 while (i < wp->w_lines_valid
1445 && !wp->w_lines[i].wl_valid) {
1446 old_rows += wp->w_lines[i++].wl_size;
1447 }
1448 break;
1449 }
1450 }
1451
1452 if (i >= wp->w_lines_valid) {
1453 /* We can't find a valid line below the changed lines,
1454 * need to redraw until the end of the window.
1455 * Inserting/deleting lines has no use. */
1456 bot_start = 0;
1457 } else {
1458 /* Able to count old number of rows: Count new window
1459 * rows, and may insert/delete lines */
1460 j = idx;
1461 for (l = lnum; l < mod_bot; l++) {
1462 if (hasFoldingWin(wp, l, NULL, &l, true, NULL)) {
1463 new_rows++;
1464 } else if (l == wp->w_topline) {
1465 new_rows += plines_win_nofill(wp, l, true) + wp->w_topfill;
1466 } else {
1467 new_rows += plines_win(wp, l, true);
1468 }
1469 j++;
1470 if (new_rows > wp->w_grid.Rows - row - 2) {
1471 // it's getting too much, must redraw the rest
1472 new_rows = 9999;
1473 break;
1474 }
1475 }
1476 xtra_rows = new_rows - old_rows;
1477 if (xtra_rows < 0) {
1478 /* May scroll text up. If there is not enough
1479 * remaining text or scrolling fails, must redraw the
1480 * rest. If scrolling works, must redraw the text
1481 * below the scrolled text. */
1482 if (row - xtra_rows >= wp->w_grid.Rows - 2) {
1483 mod_bot = MAXLNUM;
1484 } else {
1485 win_scroll_lines(wp, row, xtra_rows);
1486 bot_start = wp->w_grid.Rows + xtra_rows;
1487 }
1488 } else if (xtra_rows > 0) {
1489 /* May scroll text down. If there is not enough
1490 * remaining text of scrolling fails, must redraw the
1491 * rest. */
1492 if (row + xtra_rows >= wp->w_grid.Rows - 2) {
1493 mod_bot = MAXLNUM;
1494 } else {
1495 win_scroll_lines(wp, row + old_rows, xtra_rows);
1496 if (top_end > row + old_rows) {
1497 // Scrolled the part at the top that requires
1498 // updating down.
1499 top_end += xtra_rows;
1500 }
1501 }
1502 }
1503
1504 /* When not updating the rest, may need to move w_lines[]
1505 * entries. */
1506 if (mod_bot != MAXLNUM && i != j) {
1507 if (j < i) {
1508 int x = row + new_rows;
1509
1510 // move entries in w_lines[] upwards
1511 for (;;) {
1512 // stop at last valid entry in w_lines[]
1513 if (i >= wp->w_lines_valid) {
1514 wp->w_lines_valid = j;
1515 break;
1516 }
1517 wp->w_lines[j] = wp->w_lines[i];
1518 // stop at a line that won't fit
1519 if (x + (int)wp->w_lines[j].wl_size
1520 > wp->w_grid.Rows) {
1521 wp->w_lines_valid = j + 1;
1522 break;
1523 }
1524 x += wp->w_lines[j++].wl_size;
1525 ++i;
1526 }
1527 if (bot_start > x) {
1528 bot_start = x;
1529 }
1530 } else { // j > i
1531 // move entries in w_lines[] downwards
1532 j -= i;
1533 wp->w_lines_valid += j;
1534 if (wp->w_lines_valid > wp->w_grid.Rows) {
1535 wp->w_lines_valid = wp->w_grid.Rows;
1536 }
1537 for (i = wp->w_lines_valid; i - j >= idx; i--) {
1538 wp->w_lines[i] = wp->w_lines[i - j];
1539 }
1540
1541 /* The w_lines[] entries for inserted lines are
1542 * now invalid, but wl_size may be used above.
1543 * Reset to zero. */
1544 while (i >= idx) {
1545 wp->w_lines[i].wl_size = 0;
1546 wp->w_lines[i--].wl_valid = FALSE;
1547 }
1548 }
1549 }
1550 }
1551 }
1552
1553 /*
1554 * When lines are folded, display one line for all of them.
1555 * Otherwise, display normally (can be several display lines when
1556 * 'wrap' is on).
1557 */
1558 foldinfo_T foldinfo = fold_info(wp, lnum);
1559
1560 if (foldinfo.fi_lines == 0
1561 && idx < wp->w_lines_valid
1562 && wp->w_lines[idx].wl_valid
1563 && wp->w_lines[idx].wl_lnum == lnum
1564 && lnum > wp->w_topline
1565 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
1566 && srow + wp->w_lines[idx].wl_size > wp->w_grid.Rows
1567 && win_get_fill(wp, lnum) == 0) {
1568 // This line is not going to fit. Don't draw anything here,
1569 // will draw "@ " lines below.
1570 row = wp->w_grid.Rows + 1;
1571 } else {
1572 prepare_search_hl(wp, lnum);
1573 // Let the syntax stuff know we skipped a few lines.
1574 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1575 && syntax_present(wp)) {
1576 syntax_end_parsing(syntax_last_parsed + 1);
1577 }
1578
1579 // Display one line
1580 row = win_line(wp, lnum, srow,
1581 foldinfo.fi_lines ? srow : wp->w_grid.Rows,
1582 mod_top == 0, false, foldinfo, &line_providers);
1583
1584 wp->w_lines[idx].wl_folded = foldinfo.fi_lines != 0;
1585 wp->w_lines[idx].wl_lastlnum = lnum;
1586 did_update = DID_LINE;
1587
1588 if (foldinfo.fi_lines > 0) {
1589 did_update = DID_FOLD;
1590 foldinfo.fi_lines--;
1591 wp->w_lines[idx].wl_lastlnum = lnum + foldinfo.fi_lines;
1592 }
1593
1594 syntax_last_parsed = lnum;
1595 }
1596
1597 wp->w_lines[idx].wl_lnum = lnum;
1598 wp->w_lines[idx].wl_valid = true;
1599
1600 if (row > wp->w_grid.Rows) { // past end of grid
1601 // we may need the size of that too long line later on
1602 if (dollar_vcol == -1) {
1603 wp->w_lines[idx].wl_size = plines_win(wp, lnum, true);
1604 }
1605 idx++;
1606 break;
1607 }
1608 if (dollar_vcol == -1) {
1609 wp->w_lines[idx].wl_size = row - srow;
1610 }
1611 idx++;
1612 lnum += foldinfo.fi_lines + 1;
1613 } else {
1614 if (wp->w_p_rnu) {
1615 // 'relativenumber' set: The text doesn't need to be drawn, but
1616 // the number column nearly always does.
1617 foldinfo_T info = fold_info(wp, lnum);
1618 (void)win_line(wp, lnum, srow, wp->w_grid.Rows, true, true,
1619 info, &line_providers);
1620 }
1621
1622 // This line does not need to be drawn, advance to the next one.
1623 row += wp->w_lines[idx++].wl_size;
1624 if (row > wp->w_grid.Rows) { // past end of screen
1625 break;
1626 }
1627 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1628 did_update = DID_NONE;
1629 }
1630
1631 if (lnum > buf->b_ml.ml_line_count) {
1632 eof = true;
1633 break;
1634 }
1635 }
1636 /*
1637 * End of loop over all window lines.
1638 */
1639
1640
1641 if (idx > wp->w_lines_valid) {
1642 wp->w_lines_valid = idx;
1643 }
1644
1645 /*
1646 * Let the syntax stuff know we stop parsing here.
1647 */
1648 if (syntax_last_parsed != 0 && syntax_present(wp)) {
1649 syntax_end_parsing(syntax_last_parsed + 1);
1650 }
1651
1652 /*
1653 * If we didn't hit the end of the file, and we didn't finish the last
1654 * line we were working on, then the line didn't fit.
1655 */
1656 wp->w_empty_rows = 0;
1657 wp->w_filler_rows = 0;
1658 if (!eof && !didline) {
1659 int at_attr = hl_combine_attr(wp->w_hl_attr_normal,
1660 win_hl_attr(wp, HLF_AT));
1661 if (lnum == wp->w_topline) {
1662 /*
1663 * Single line that does not fit!
1664 * Don't overwrite it, it can be edited.
1665 */
1666 wp->w_botline = lnum + 1;
1667 } else if (win_get_fill(wp, lnum) >= wp->w_grid.Rows - srow) {
1668 // Window ends in filler lines.
1669 wp->w_botline = lnum;
1670 wp->w_filler_rows = wp->w_grid.Rows - srow;
1671 } else if (dy_flags & DY_TRUNCATE) { // 'display' has "truncate"
1672 int scr_row = wp->w_grid.Rows - 1;
1673
1674 // Last line isn't finished: Display "@@@" in the last screen line.
1675 grid_puts_len(&wp->w_grid, (char_u *)"@@", 2, scr_row, 0, at_attr);
1676
1677 grid_fill(&wp->w_grid, scr_row, scr_row + 1, 2, wp->w_grid.Columns,
1678 '@', ' ', at_attr);
1679 set_empty_rows(wp, srow);
1680 wp->w_botline = lnum;
1681 } else if (dy_flags & DY_LASTLINE) { // 'display' has "lastline"
1682 // Last line isn't finished: Display "@@@" at the end.
1683 grid_fill(&wp->w_grid, wp->w_grid.Rows - 1, wp->w_grid.Rows,
1684 wp->w_grid.Columns - 3, wp->w_grid.Columns, '@', '@', at_attr);
1685 set_empty_rows(wp, srow);
1686 wp->w_botline = lnum;
1687 } else {
1688 win_draw_end(wp, '@', ' ', true, srow, wp->w_grid.Rows, HLF_AT);
1689 wp->w_botline = lnum;
1690 }
1691 } else {
1692 if (eof) { // we hit the end of the file
1693 wp->w_botline = buf->b_ml.ml_line_count + 1;
1694 j = win_get_fill(wp, wp->w_botline);
1695 if (j > 0 && !wp->w_botfill) {
1696 // Display filler text below last line. win_line() will check
1697 // for ml_line_count+1 and only draw filler lines
1698 foldinfo_T info = FOLDINFO_INIT;
1699 row = win_line(wp, wp->w_botline, row, wp->w_grid.Rows,
1700 false, false, info, &line_providers);
1701 }
1702 } else if (dollar_vcol == -1) {
1703 wp->w_botline = lnum;
1704 }
1705
1706 // make sure the rest of the screen is blank
1707 // write the 'eob' character to rows that aren't part of the file.
1708 win_draw_end(wp, wp->w_p_fcs_chars.eob, ' ', false, row, wp->w_grid.Rows,
1709 HLF_EOB);
1710 }
1711
1712 kvi_destroy(line_providers);
1713
1714 if (wp->w_redr_type >= REDRAW_TOP) {
1715 draw_vsep_win(wp, 0);
1716 }
1717 syn_set_timeout(NULL);
1718
1719 // Reset the type of redrawing required, the window has been updated.
1720 wp->w_redr_type = 0;
1721 wp->w_old_topfill = wp->w_topfill;
1722 wp->w_old_botfill = wp->w_botfill;
1723
1724 if (dollar_vcol == -1) {
1725 /*
1726 * There is a trick with w_botline. If we invalidate it on each
1727 * change that might modify it, this will cause a lot of expensive
1728 * calls to plines_win() in update_topline() each time. Therefore the
1729 * value of w_botline is often approximated, and this value is used to
1730 * compute the value of w_topline. If the value of w_botline was
1731 * wrong, check that the value of w_topline is correct (cursor is on
1732 * the visible part of the text). If it's not, we need to redraw
1733 * again. Mostly this just means scrolling up a few lines, so it
1734 * doesn't look too bad. Only do this for the current window (where
1735 * changes are relevant).
1736 */
1737 wp->w_valid |= VALID_BOTLINE;
1738 wp->w_viewport_invalid = true;
1739 if (wp == curwin && wp->w_botline != old_botline && !recursive) {
1740 recursive = true;
1741 curwin->w_valid &= ~VALID_TOPLINE;
1742 update_topline(curwin); // may invalidate w_botline again
1743 if (must_redraw != 0) {
1744 // Don't update for changes in buffer again.
1745 i = curbuf->b_mod_set;
1746 curbuf->b_mod_set = false;
1747 win_update(curwin, providers);
1748 must_redraw = 0;
1749 curbuf->b_mod_set = i;
1750 }
1751 recursive = false;
1752 }
1753 }
1754
1755
1756 // restore got_int, unless CTRL-C was hit while redrawing
1757 if (!got_int) {
1758 got_int = save_got_int;
1759 }
1760 } // NOLINT(readability/fn_size)
1761
1762 /// Returns width of the signcolumn that should be used for the whole window
1763 ///
1764 /// @param wp window we want signcolumn width from
1765 /// @return max width of signcolumn (cell unit)
1766 ///
1767 /// @note Returns a constant for now but hopefully we can improve neovim so that
1768 /// the returned value width adapts to the maximum number of marks to draw
1769 /// for the window
1770 /// TODO(teto)
win_signcol_width(win_T * wp)1771 int win_signcol_width(win_T *wp)
1772 {
1773 // 2 is vim default value
1774 return 2;
1775 }
1776
1777 /// Call grid_fill() with columns adjusted for 'rightleft' if needed.
1778 /// Return the new offset.
win_fill_end(win_T * wp,int c1,int c2,int off,int width,int row,int endrow,int attr)1779 static int win_fill_end(win_T *wp, int c1, int c2, int off, int width, int row, int endrow,
1780 int attr)
1781 {
1782 int nn = off + width;
1783
1784 if (nn > wp->w_grid.Columns) {
1785 nn = wp->w_grid.Columns;
1786 }
1787
1788 if (wp->w_p_rl) {
1789 grid_fill(&wp->w_grid, row, endrow, W_ENDCOL(wp) - nn, W_ENDCOL(wp) - off,
1790 c1, c2, attr);
1791 } else {
1792 grid_fill(&wp->w_grid, row, endrow, off, nn, c1, c2, attr);
1793 }
1794
1795 return nn;
1796 }
1797
1798 /// Clear lines near the end of the window and mark the unused lines with "c1".
1799 /// Use "c2" as filler character.
1800 /// When "draw_margin" is true, then draw the sign/fold/number columns.
win_draw_end(win_T * wp,int c1,int c2,bool draw_margin,int row,int endrow,hlf_T hl)1801 static void win_draw_end(win_T *wp, int c1, int c2, bool draw_margin, int row, int endrow, hlf_T hl)
1802 {
1803 assert(hl >= 0 && hl < HLF_COUNT);
1804 int n = 0;
1805
1806 if (draw_margin) {
1807 // draw the fold column
1808 int fdc = compute_foldcolumn(wp, 0);
1809 if (fdc > 0) {
1810 n = win_fill_end(wp, ' ', ' ', n, fdc, row, endrow,
1811 win_hl_attr(wp, HLF_FC));
1812 }
1813 // draw the sign column
1814 int count = win_signcol_count(wp);
1815 if (count > 0) {
1816 n = win_fill_end(wp, ' ', ' ', n, win_signcol_width(wp) * count, row,
1817 endrow, win_hl_attr(wp, HLF_SC));
1818 }
1819 // draw the number column
1820 if ((wp->w_p_nu || wp->w_p_rnu) && vim_strchr(p_cpo, CPO_NUMCOL) == NULL) {
1821 n = win_fill_end(wp, ' ', ' ', n, number_width(wp) + 1, row, endrow,
1822 win_hl_attr(wp, HLF_N));
1823 }
1824 }
1825
1826 int attr = hl_combine_attr(wp->w_hl_attr_normal, win_hl_attr(wp, hl));
1827
1828 if (wp->w_p_rl) {
1829 grid_fill(&wp->w_grid, row, endrow, wp->w_wincol, W_ENDCOL(wp) - 1 - n,
1830 c2, c2, attr);
1831 grid_fill(&wp->w_grid, row, endrow, W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
1832 c1, c2, attr);
1833 } else {
1834 grid_fill(&wp->w_grid, row, endrow, n, wp->w_grid.Columns, c1, c2, attr);
1835 }
1836
1837 set_empty_rows(wp, row);
1838 }
1839
1840
1841 /// Advance **color_cols
1842 ///
1843 /// @return true when there are columns to draw.
advance_color_col(int vcol,int ** color_cols)1844 static bool advance_color_col(int vcol, int **color_cols)
1845 {
1846 while (**color_cols >= 0 && vcol > **color_cols) {
1847 ++*color_cols;
1848 }
1849 return **color_cols >= 0;
1850 }
1851
1852 // Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
1853 // space is available for window "wp", minus "col".
compute_foldcolumn(win_T * wp,int col)1854 static int compute_foldcolumn(win_T *wp, int col)
1855 {
1856 int fdc = win_fdccol_count(wp);
1857 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
1858 int wwidth = wp->w_grid.Columns;
1859
1860 if (fdc > wwidth - (col + wmw)) {
1861 fdc = wwidth - (col + wmw);
1862 }
1863 return fdc;
1864 }
1865
1866 /// Put a single char from an UTF-8 buffer into a line buffer.
1867 ///
1868 /// Handles composing chars and arabic shaping state.
line_putchar(buf_T * buf,LineState * s,schar_T * dest,int maxcells,bool rl,int vcol)1869 static int line_putchar(buf_T *buf, LineState *s, schar_T *dest, int maxcells, bool rl, int vcol)
1870 {
1871 const char_u *p = (char_u *)s->p;
1872 int cells = utf_ptr2cells(p);
1873 int c_len = utfc_ptr2len(p);
1874 int u8c, u8cc[MAX_MCO];
1875 if (cells > maxcells) {
1876 return -1;
1877 }
1878 u8c = utfc_ptr2char(p, u8cc);
1879 if (*p == TAB) {
1880 cells = MIN(tabstop_padding(vcol, buf->b_p_ts, buf->b_p_vts_array), maxcells);
1881 for (int c = 0; c < cells; c++) {
1882 schar_from_ascii(dest[c], ' ');
1883 }
1884 goto done;
1885 } else if (*p < 0x80 && u8cc[0] == 0) {
1886 schar_from_ascii(dest[0], *p);
1887 s->prev_c = u8c;
1888 } else {
1889 if (p_arshape && !p_tbidi && arabic_char(u8c)) {
1890 // Do Arabic shaping.
1891 int pc, pc1, nc;
1892 int pcc[MAX_MCO];
1893 int firstbyte = *p;
1894
1895 // The idea of what is the previous and next
1896 // character depends on 'rightleft'.
1897 if (rl) {
1898 pc = s->prev_c;
1899 pc1 = s->prev_c1;
1900 nc = utf_ptr2char(p + c_len);
1901 s->prev_c1 = u8cc[0];
1902 } else {
1903 pc = utfc_ptr2char(p + c_len, pcc);
1904 nc = s->prev_c;
1905 pc1 = pcc[0];
1906 }
1907 s->prev_c = u8c;
1908
1909 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0], pc, pc1, nc);
1910 } else {
1911 s->prev_c = u8c;
1912 }
1913 schar_from_cc(dest[0], u8c, u8cc);
1914 }
1915 if (cells > 1) {
1916 dest[1][0] = 0;
1917 }
1918 done:
1919 s->p += c_len;
1920 return cells;
1921 }
1922
1923
1924 /// Fills the foldcolumn at "p" for window "wp".
1925 /// Only to be called when 'foldcolumn' > 0.
1926 ///
1927 /// @param[out] p Char array to write into
1928 /// @param lnum Absolute current line number
1929 /// @param closed Whether it is in 'foldcolumn' mode
1930 ///
1931 /// Assume monocell characters
1932 /// @return number of chars added to \param p
fill_foldcolumn(char_u * p,win_T * wp,foldinfo_T foldinfo,linenr_T lnum)1933 static size_t fill_foldcolumn(char_u *p, win_T *wp, foldinfo_T foldinfo, linenr_T lnum)
1934 {
1935 int i = 0;
1936 int level;
1937 int first_level;
1938 int fdc = compute_foldcolumn(wp, 0); // available cell width
1939 size_t char_counter = 0;
1940 int symbol = 0;
1941 int len = 0;
1942 bool closed = foldinfo.fi_lines > 0;
1943 // Init to all spaces.
1944 memset(p, ' ', MAX_MCO * fdc + 1);
1945
1946 level = foldinfo.fi_level;
1947
1948 // If the column is too narrow, we start at the lowest level that
1949 // fits and use numbers to indicated the depth.
1950 first_level = level - fdc - closed + 1;
1951 if (first_level < 1) {
1952 first_level = 1;
1953 }
1954
1955 for (i = 0; i < MIN(fdc, level); i++) {
1956 if (foldinfo.fi_lnum == lnum
1957 && first_level + i >= foldinfo.fi_low_level) {
1958 symbol = wp->w_p_fcs_chars.foldopen;
1959 } else if (first_level == 1) {
1960 symbol = wp->w_p_fcs_chars.foldsep;
1961 } else if (first_level + i <= 9) {
1962 symbol = '0' + first_level + i;
1963 } else {
1964 symbol = '>';
1965 }
1966
1967 len = utf_char2bytes(symbol, &p[char_counter]);
1968 char_counter += len;
1969 if (first_level + i >= level) {
1970 i++;
1971 break;
1972 }
1973 }
1974
1975 if (closed) {
1976 if (symbol != 0) {
1977 // rollback previous write
1978 char_counter -= len;
1979 memset(&p[char_counter], ' ', len);
1980 }
1981 len = utf_char2bytes(wp->w_p_fcs_chars.foldclosed, &p[char_counter]);
1982 char_counter += len;
1983 }
1984
1985 return MAX(char_counter + (fdc-i), (size_t)fdc);
1986 }
1987
1988 /// Display line "lnum" of window 'wp' on the screen.
1989 /// wp->w_virtcol needs to be valid.
1990 ///
1991 /// @param lnum line to display
1992 /// @param startrow first row relative to window grid
1993 /// @param endrow last grid row to be redrawn
1994 /// @param nochange not updating for changed text
1995 /// @param number_only only update the number column
1996 /// @param foldinfo fold info for this line
1997 /// @param[in, out] providers decoration providers active this line
1998 /// items will be disables if they cause errors
1999 /// or explicitly return `false`.
2000 ///
2001 /// @return the number of last row the line occupies.
win_line(win_T * wp,linenr_T lnum,int startrow,int endrow,bool nochange,bool number_only,foldinfo_T foldinfo,Providers * providers)2002 static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool nochange,
2003 bool number_only, foldinfo_T foldinfo, Providers *providers)
2004 {
2005 int c = 0; // init for GCC
2006 long vcol = 0; // virtual column (for tabs)
2007 long vcol_sbr = -1; // virtual column after showbreak
2008 long vcol_prev = -1; // "vcol" of previous character
2009 char_u *line; // current line
2010 char_u *ptr; // current position in "line"
2011 int row; // row in the window, excl w_winrow
2012 ScreenGrid *grid = &wp->w_grid; // grid specific to the window
2013
2014 char_u extra[57]; // sign, line number and 'fdc' must
2015 // fit in here
2016 int n_extra = 0; // number of extra chars
2017 char_u *p_extra = NULL; // string of extra chars, plus NUL
2018 char_u *p_extra_free = NULL; // p_extra needs to be freed
2019 int c_extra = NUL; // extra chars, all the same
2020 int c_final = NUL; // final char, mandatory if set
2021 int extra_attr = 0; // attributes when n_extra != 0
2022 static char_u *at_end_str = (char_u *)""; // used for p_extra when displaying
2023 // curwin->w_p_lcs_chars.eol at
2024 // end-of-line
2025 int lcs_eol_one = wp->w_p_lcs_chars.eol; // 'eol' until it's been used
2026 int lcs_prec_todo = wp->w_p_lcs_chars.prec; // 'prec' until it's been used
2027 bool has_fold = foldinfo.fi_level != 0 && foldinfo.fi_lines > 0;
2028
2029 // saved "extra" items for when draw_state becomes WL_LINE (again)
2030 int saved_n_extra = 0;
2031 char_u *saved_p_extra = NULL;
2032 int saved_c_extra = 0;
2033 int saved_c_final = 0;
2034 int saved_char_attr = 0;
2035
2036 int n_attr = 0; // chars with special attr
2037 int saved_attr2 = 0; // char_attr saved for n_attr
2038 int n_attr3 = 0; // chars with overruling special attr
2039 int saved_attr3 = 0; // char_attr saved for n_attr3
2040
2041 int n_skip = 0; // nr of chars to skip for 'nowrap'
2042
2043 int fromcol = -10; // start of inverting
2044 int tocol = MAXCOL; // end of inverting
2045 int fromcol_prev = -2; // start of inverting after cursor
2046 bool noinvcur = false; // don't invert the cursor
2047 bool lnum_in_visual_area = false;
2048 pos_T pos;
2049 long v;
2050
2051 int char_attr = 0; // attributes for next character
2052 bool attr_pri = false; // char_attr has priority
2053 bool area_highlighting = false; // Visual or incsearch highlighting in this line
2054 int attr = 0; // attributes for area highlighting
2055 int area_attr = 0; // attributes desired by highlighting
2056 int search_attr = 0; // attributes desired by 'hlsearch'
2057 int vcol_save_attr = 0; // saved attr for 'cursorcolumn'
2058 int syntax_attr = 0; // attributes desired by syntax
2059 int has_syntax = FALSE; // this buffer has syntax highl.
2060 int save_did_emsg;
2061 int eol_hl_off = 0; // 1 if highlighted char after EOL
2062 bool draw_color_col = false; // highlight colorcolumn
2063 int *color_cols = NULL; // pointer to according columns array
2064 bool has_spell = false; // this buffer has spell checking
2065 #define SPWORDLEN 150
2066 char_u nextline[SPWORDLEN * 2]; // text with start of the next line
2067 int nextlinecol = 0; // column where nextline[] starts
2068 int nextline_idx = 0; /* index in nextline[] where next line
2069 starts */
2070 int spell_attr = 0; // attributes desired by spelling
2071 int word_end = 0; // last byte with same spell_attr
2072 static linenr_T checked_lnum = 0; // line number for "checked_col"
2073 static int checked_col = 0; /* column in "checked_lnum" up to which
2074 * there are no spell errors */
2075 static int cap_col = -1; // column to check for Cap word
2076 static linenr_T capcol_lnum = 0; // line number where "cap_col"
2077 int cur_checked_col = 0; // checked column for current line
2078 int extra_check = 0; // has syntax or linebreak
2079 int multi_attr = 0; // attributes desired by multibyte
2080 int mb_l = 1; // multi-byte byte length
2081 int mb_c = 0; // decoded multi-byte character
2082 bool mb_utf8 = false; // screen char is UTF-8 char
2083 int u8cc[MAX_MCO]; // composing UTF-8 chars
2084 int filler_lines; // nr of filler lines to be drawn
2085 int filler_todo; // nr of filler lines still to do + 1
2086 hlf_T diff_hlf = (hlf_T)0; // type of diff highlighting
2087 int change_start = MAXCOL; // first col of changed area
2088 int change_end = -1; // last col of changed area
2089 colnr_T trailcol = MAXCOL; // start of trailing spaces
2090 colnr_T leadcol = 0; // start of leading spaces
2091 bool in_multispace = false; // in multiple consecutive spaces
2092 int multispace_pos = 0; // position in lcs-multispace string
2093 bool need_showbreak = false; // overlong line, skip first x chars
2094 sign_attrs_T sattrs[SIGN_SHOW_MAX]; // attributes for signs
2095 int num_signs; // number of signs for line
2096 int line_attr = 0; // attribute for the whole line
2097 int line_attr_save;
2098 int line_attr_lowprio = 0; // low-priority attribute for the line
2099 int line_attr_lowprio_save;
2100 matchitem_T *cur; // points to the match list
2101 match_T *shl; // points to search_hl or a match
2102 bool shl_flag; // flag to indicate whether search_hl
2103 // has been processed or not
2104 bool prevcol_hl_flag; // flag to indicate whether prevcol
2105 // equals startcol of search_hl or one
2106 // of the matches
2107 int prev_c = 0; // previous Arabic character
2108 int prev_c1 = 0; // first composing char for prev_c
2109
2110 bool search_attr_from_match = false; // if search_attr is from :match
2111 bool has_decor = false; // this buffer has decoration
2112 int win_col_offset = 0; // offset for window columns
2113
2114 char_u buf_fold[FOLD_TEXT_LEN + 1]; // Hold value returned by get_foldtext
2115
2116 bool area_active = false;
2117
2118 int cul_attr = 0; // set when 'cursorline' active
2119 // 'cursorlineopt' has "screenline" and cursor is in this line
2120 bool cul_screenline = false;
2121 // margin columns for the screen line, needed for when 'cursorlineopt'
2122 // contains "screenline"
2123 int left_curline_col = 0;
2124 int right_curline_col = 0;
2125
2126 // draw_state: items that are drawn in sequence:
2127 #define WL_START 0 // nothing done yet
2128 #define WL_CMDLINE WL_START + 1 // cmdline window column
2129 #define WL_FOLD WL_CMDLINE + 1 // 'foldcolumn'
2130 #define WL_SIGN WL_FOLD + 1 // column for signs
2131 #define WL_NR WL_SIGN + 1 // line number
2132 #define WL_BRI WL_NR + 1 // 'breakindent'
2133 #define WL_SBR WL_BRI + 1 // 'showbreak' or 'diff'
2134 #define WL_LINE WL_SBR + 1 // text in the line
2135 int draw_state = WL_START; // what to draw next
2136
2137 int syntax_flags = 0;
2138 int syntax_seqnr = 0;
2139 int prev_syntax_id = 0;
2140 int conceal_attr = win_hl_attr(wp, HLF_CONCEAL);
2141 bool is_concealing = false;
2142 int boguscols = 0; ///< nonexistent columns added to
2143 ///< force wrapping
2144 int vcol_off = 0; ///< offset for concealed characters
2145 int did_wcol = false;
2146 int match_conc = 0; ///< cchar for match functions
2147 int old_boguscols = 0;
2148 #define VCOL_HLC (vcol - vcol_off)
2149 #define FIX_FOR_BOGUSCOLS \
2150 { \
2151 n_extra += vcol_off; \
2152 vcol -= vcol_off; \
2153 vcol_off = 0; \
2154 col -= boguscols; \
2155 old_boguscols = boguscols; \
2156 boguscols = 0; \
2157 }
2158
2159 if (startrow > endrow) { // past the end already!
2160 return startrow;
2161 }
2162
2163 row = startrow;
2164
2165 buf_T *buf = wp->w_buffer;
2166 bool end_fill = (lnum == buf->b_ml.ml_line_count+1);
2167
2168 if (!number_only) {
2169 // To speed up the loop below, set extra_check when there is linebreak,
2170 // trailing white space and/or syntax processing to be done.
2171 extra_check = wp->w_p_lbr;
2172 if (syntax_present(wp) && !wp->w_s->b_syn_error && !wp->w_s->b_syn_slow) {
2173 // Prepare for syntax highlighting in this line. When there is an
2174 // error, stop syntax highlighting.
2175 save_did_emsg = did_emsg;
2176 did_emsg = false;
2177 syntax_start(wp, lnum);
2178 if (did_emsg) {
2179 wp->w_s->b_syn_error = true;
2180 } else {
2181 did_emsg = save_did_emsg;
2182 if (!wp->w_s->b_syn_slow) {
2183 has_syntax = true;
2184 extra_check = true;
2185 }
2186 }
2187 }
2188
2189 has_decor = decor_redraw_line(wp->w_buffer, lnum-1,
2190 &decor_state);
2191
2192 for (size_t k = 0; k < kv_size(*providers); k++) {
2193 DecorProvider *p = kv_A(*providers, k);
2194 if (p && p->redraw_line != LUA_NOREF) {
2195 FIXED_TEMP_ARRAY(args, 3);
2196 args.items[0] = WINDOW_OBJ(wp->handle);
2197 args.items[1] = BUFFER_OBJ(buf->handle);
2198 args.items[2] = INTEGER_OBJ(lnum-1);
2199 if (provider_invoke(p->ns_id, "line", p->redraw_line, args, true)) {
2200 has_decor = true;
2201 } else {
2202 // return 'false' or error: skip rest of this window
2203 kv_A(*providers, k) = NULL;
2204 }
2205
2206 win_check_ns_hl(wp);
2207 }
2208 }
2209
2210 if (provider_err) {
2211 Decoration err_decor = DECORATION_INIT;
2212 int hl_err = syn_check_group(S_LEN("ErrorMsg"));
2213 kv_push(err_decor.virt_text,
2214 ((VirtTextChunk){ .text = provider_err,
2215 .hl_id = hl_err }));
2216 err_decor.virt_text_width = mb_string2cells((char_u *)provider_err);
2217 decor_add_ephemeral(lnum-1, 0, lnum-1, 0, &err_decor);
2218 provider_err = NULL;
2219 has_decor = true;
2220 }
2221
2222 if (has_decor) {
2223 extra_check = true;
2224 }
2225
2226 // Check for columns to display for 'colorcolumn'.
2227 color_cols = wp->w_buffer->terminal ? NULL : wp->w_p_cc_cols;
2228 if (color_cols != NULL) {
2229 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
2230 }
2231
2232 if (wp->w_p_spell
2233 && !has_fold
2234 && !end_fill
2235 && *wp->w_s->b_p_spl != NUL
2236 && !GA_EMPTY(&wp->w_s->b_langp)
2237 && *(char **)(wp->w_s->b_langp.ga_data) != NULL) {
2238 // Prepare for spell checking.
2239 has_spell = true;
2240 extra_check = true;
2241
2242 // Get the start of the next line, so that words that wrap to the next
2243 // line are found too: "et<line-break>al.".
2244 // Trick: skip a few chars for C/shell/Vim comments
2245 nextline[SPWORDLEN] = NUL;
2246 if (lnum < wp->w_buffer->b_ml.ml_line_count) {
2247 line = ml_get_buf(wp->w_buffer, lnum + 1, false);
2248 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2249 }
2250
2251 // When a word wrapped from the previous line the start of the current
2252 // line is valid.
2253 if (lnum == checked_lnum) {
2254 cur_checked_col = checked_col;
2255 }
2256 checked_lnum = 0;
2257
2258 // When there was a sentence end in the previous line may require a
2259 // word starting with capital in this line. In line 1 always check
2260 // the first word.
2261 if (lnum != capcol_lnum) {
2262 cap_col = -1;
2263 }
2264 if (lnum == 1) {
2265 cap_col = 0;
2266 }
2267 capcol_lnum = 0;
2268 }
2269
2270 // handle Visual active in this window
2271 if (VIsual_active && wp->w_buffer == curwin->w_buffer) {
2272 pos_T *top, *bot;
2273
2274 if (ltoreq(curwin->w_cursor, VIsual)) {
2275 // Visual is after curwin->w_cursor
2276 top = &curwin->w_cursor;
2277 bot = &VIsual;
2278 } else {
2279 // Visual is before curwin->w_cursor
2280 top = &VIsual;
2281 bot = &curwin->w_cursor;
2282 }
2283 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
2284 if (VIsual_mode == Ctrl_V) {
2285 // block mode
2286 if (lnum_in_visual_area) {
2287 fromcol = wp->w_old_cursor_fcol;
2288 tocol = wp->w_old_cursor_lcol;
2289 }
2290 } else {
2291 // non-block mode
2292 if (lnum > top->lnum && lnum <= bot->lnum) {
2293 fromcol = 0;
2294 } else if (lnum == top->lnum) {
2295 if (VIsual_mode == 'V') { // linewise
2296 fromcol = 0;
2297 } else {
2298 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2299 if (gchar_pos(top) == NUL) {
2300 tocol = fromcol + 1;
2301 }
2302 }
2303 }
2304 if (VIsual_mode != 'V' && lnum == bot->lnum) {
2305 if (*p_sel == 'e' && bot->col == 0
2306 && bot->coladd == 0) {
2307 fromcol = -10;
2308 tocol = MAXCOL;
2309 } else if (bot->col == MAXCOL) {
2310 tocol = MAXCOL;
2311 } else {
2312 pos = *bot;
2313 if (*p_sel == 'e') {
2314 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2315 } else {
2316 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2317 tocol++;
2318 }
2319 }
2320 }
2321 }
2322
2323 // Check if the char under the cursor should be inverted (highlighted).
2324 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2325 && cursor_is_block_during_visual(*p_sel == 'e')) {
2326 noinvcur = true;
2327 }
2328
2329 // if inverting in this line set area_highlighting
2330 if (fromcol >= 0) {
2331 area_highlighting = true;
2332 attr = win_hl_attr(wp, HLF_V);
2333 }
2334 // handle 'incsearch' and ":s///c" highlighting
2335 } else if (highlight_match
2336 && wp == curwin
2337 && !has_fold
2338 && lnum >= curwin->w_cursor.lnum
2339 && lnum <= curwin->w_cursor.lnum + search_match_lines) {
2340 if (lnum == curwin->w_cursor.lnum) {
2341 getvcol(curwin, &(curwin->w_cursor),
2342 (colnr_T *)&fromcol, NULL, NULL);
2343 } else {
2344 fromcol = 0;
2345 }
2346 if (lnum == curwin->w_cursor.lnum + search_match_lines) {
2347 pos.lnum = lnum;
2348 pos.col = search_match_endcol;
2349 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2350 }
2351 // do at least one character; happens when past end of line
2352 if (fromcol == tocol && search_match_endcol) {
2353 tocol = fromcol + 1;
2354 }
2355 area_highlighting = true;
2356 attr = win_hl_attr(wp, HLF_I);
2357 }
2358 }
2359
2360 filler_lines = diff_check(wp, lnum);
2361 if (filler_lines < 0) {
2362 if (filler_lines == -1) {
2363 if (diff_find_change(wp, lnum, &change_start, &change_end)) {
2364 diff_hlf = HLF_ADD; // added line
2365 } else if (change_start == 0) {
2366 diff_hlf = HLF_TXD; // changed text
2367 } else {
2368 diff_hlf = HLF_CHD; // changed line
2369 }
2370 } else {
2371 diff_hlf = HLF_ADD; // added line
2372 }
2373 filler_lines = 0;
2374 area_highlighting = true;
2375 }
2376 VirtLines virt_lines = KV_INITIAL_VALUE;
2377 int n_virt_lines = decor_virt_lines(wp, lnum, &virt_lines);
2378 filler_lines += n_virt_lines;
2379 if (lnum == wp->w_topline) {
2380 filler_lines = wp->w_topfill;
2381 n_virt_lines = MIN(n_virt_lines, filler_lines);
2382 }
2383 filler_todo = filler_lines;
2384
2385 // Cursor line highlighting for 'cursorline' in the current window.
2386 if (lnum == wp->w_cursor.lnum) {
2387 // Do not show the cursor line in the text when Visual mode is active,
2388 // because it's not clear what is selected then.
2389 if (wp->w_p_cul && !(wp == curwin && VIsual_active)
2390 && wp->w_p_culopt_flags != CULOPT_NBR) {
2391 cul_screenline = (wp->w_p_wrap
2392 && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
2393 if (!cul_screenline) {
2394 cul_attr = win_hl_attr(wp, HLF_CUL);
2395 HlAttrs ae = syn_attr2entry(cul_attr);
2396 // We make a compromise here (#7383):
2397 // * low-priority CursorLine if fg is not set
2398 // * high-priority ("same as Vim" priority) CursorLine if fg is set
2399 if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) {
2400 line_attr_lowprio = cul_attr;
2401 } else {
2402 if (!(State & INSERT) && bt_quickfix(wp->w_buffer)
2403 && qf_current_entry(wp) == lnum) {
2404 line_attr = hl_combine_attr(cul_attr, line_attr);
2405 } else {
2406 line_attr = cul_attr;
2407 }
2408 }
2409 } else {
2410 margin_columns_win(wp, &left_curline_col, &right_curline_col);
2411 }
2412 area_highlighting = true;
2413 }
2414 // Update w_last_cursorline even if Visual mode is active.
2415 wp->w_last_cursorline = wp->w_cursor.lnum;
2416 }
2417
2418 memset(sattrs, 0, sizeof(sattrs));
2419 num_signs = buf_get_signattrs(wp->w_buffer, lnum, sattrs);
2420
2421 // If this line has a sign with line highlighting set line_attr.
2422 // TODO(bfredl, vigoux): this should not take priority over decoration!
2423 sign_attrs_T *sattr = sign_get_attr(SIGN_LINEHL, sattrs, 0, 1);
2424 if (sattr != NULL) {
2425 line_attr = sattr->sat_linehl;
2426 }
2427
2428 // Highlight the current line in the quickfix window.
2429 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum) {
2430 line_attr = win_hl_attr(wp, HLF_QFL);
2431 }
2432
2433 if (line_attr_lowprio || line_attr) {
2434 area_highlighting = true;
2435 }
2436
2437 if (cul_screenline) {
2438 line_attr_save = line_attr;
2439 line_attr_lowprio_save = line_attr_lowprio;
2440 }
2441
2442 line = end_fill ? (char_u *)"" : ml_get_buf(wp->w_buffer, lnum, false);
2443 ptr = line;
2444
2445 if (has_spell && !number_only) {
2446 // For checking first word with a capital skip white space.
2447 if (cap_col == 0) {
2448 cap_col = (int)getwhitecols(line);
2449 }
2450
2451 /* To be able to spell-check over line boundaries copy the end of the
2452 * current line into nextline[]. Above the start of the next line was
2453 * copied to nextline[SPWORDLEN]. */
2454 if (nextline[SPWORDLEN] == NUL) {
2455 // No next line or it is empty.
2456 nextlinecol = MAXCOL;
2457 nextline_idx = 0;
2458 } else {
2459 v = (long)STRLEN(line);
2460 if (v < SPWORDLEN) {
2461 /* Short line, use it completely and append the start of the
2462 * next line. */
2463 nextlinecol = 0;
2464 memmove(nextline, line, (size_t)v);
2465 STRMOVE(nextline + v, nextline + SPWORDLEN);
2466 nextline_idx = v + 1;
2467 } else {
2468 // Long line, use only the last SPWORDLEN bytes.
2469 nextlinecol = v - SPWORDLEN;
2470 memmove(nextline, line + nextlinecol, SPWORDLEN); // -V512
2471 nextline_idx = SPWORDLEN + 1;
2472 }
2473 }
2474 }
2475
2476 if (wp->w_p_list && !has_fold && !end_fill) {
2477 if (wp->w_p_lcs_chars.space
2478 || wp->w_p_lcs_chars.multispace != NULL
2479 || wp->w_p_lcs_chars.trail
2480 || wp->w_p_lcs_chars.lead
2481 || wp->w_p_lcs_chars.nbsp) {
2482 extra_check = true;
2483 }
2484 // find start of trailing whitespace
2485 if (wp->w_p_lcs_chars.trail) {
2486 trailcol = (colnr_T)STRLEN(ptr);
2487 while (trailcol > (colnr_T)0 && ascii_iswhite(ptr[trailcol - 1])) {
2488 trailcol--;
2489 }
2490 trailcol += (colnr_T)(ptr - line);
2491 }
2492 // find end of leading whitespace
2493 if (wp->w_p_lcs_chars.lead) {
2494 leadcol = 0;
2495 while (ascii_iswhite(ptr[leadcol])) {
2496 leadcol++;
2497 }
2498 if (ptr[leadcol] == NUL) {
2499 // in a line full of spaces all of them are treated as trailing
2500 leadcol = (colnr_T)0;
2501 } else {
2502 // keep track of the first column not filled with spaces
2503 leadcol += (colnr_T)(ptr - line) + 1;
2504 }
2505 }
2506 }
2507
2508 /*
2509 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2510 * first character to be displayed.
2511 */
2512 if (wp->w_p_wrap) {
2513 v = wp->w_skipcol;
2514 } else {
2515 v = wp->w_leftcol;
2516 }
2517 if (v > 0 && !number_only) {
2518 char_u *prev_ptr = ptr;
2519 while (vcol < v && *ptr != NUL) {
2520 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
2521 vcol += c;
2522 prev_ptr = ptr;
2523 MB_PTR_ADV(ptr);
2524 }
2525
2526 // When:
2527 // - 'cuc' is set, or
2528 // - 'colorcolumn' is set, or
2529 // - 'virtualedit' is set, or
2530 // - the visual mode is active,
2531 // the end of the line may be before the start of the displayed part.
2532 if (vcol < v && (wp->w_p_cuc
2533 || draw_color_col
2534 || virtual_active()
2535 || (VIsual_active && wp->w_buffer == curwin->w_buffer))) {
2536 vcol = v;
2537 }
2538
2539 /* Handle a character that's not completely on the screen: Put ptr at
2540 * that character but skip the first few screen characters. */
2541 if (vcol > v) {
2542 vcol -= c;
2543 ptr = prev_ptr;
2544 // If the character fits on the screen, don't need to skip it.
2545 // Except for a TAB.
2546 if (utf_ptr2cells(ptr) >= c || *ptr == TAB) {
2547 n_skip = v - vcol;
2548 }
2549 }
2550
2551 /*
2552 * Adjust for when the inverted text is before the screen,
2553 * and when the start of the inverted text is before the screen.
2554 */
2555 if (tocol <= vcol) {
2556 fromcol = 0;
2557 } else if (fromcol >= 0 && fromcol < vcol) {
2558 fromcol = vcol;
2559 }
2560
2561 // When w_skipcol is non-zero, first line needs 'showbreak'
2562 if (wp->w_p_wrap) {
2563 need_showbreak = true;
2564 }
2565 // When spell checking a word we need to figure out the start of the
2566 // word and if it's badly spelled or not.
2567 if (has_spell) {
2568 size_t len;
2569 colnr_T linecol = (colnr_T)(ptr - line);
2570 hlf_T spell_hlf = HLF_COUNT;
2571
2572 pos = wp->w_cursor;
2573 wp->w_cursor.lnum = lnum;
2574 wp->w_cursor.col = linecol;
2575 len = spell_move_to(wp, FORWARD, true, true, &spell_hlf);
2576
2577 // spell_move_to() may call ml_get() and make "line" invalid
2578 line = ml_get_buf(wp->w_buffer, lnum, false);
2579 ptr = line + linecol;
2580
2581 if (len == 0 || (int)wp->w_cursor.col > ptr - line) {
2582 /* no bad word found at line start, don't check until end of a
2583 * word */
2584 spell_hlf = HLF_COUNT;
2585 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
2586 } else {
2587 // bad word found, use attributes until end of word
2588 assert(len <= INT_MAX);
2589 word_end = wp->w_cursor.col + (int)len + 1;
2590
2591 // Turn index into actual attributes.
2592 if (spell_hlf != HLF_COUNT) {
2593 spell_attr = highlight_attr[spell_hlf];
2594 }
2595 }
2596 wp->w_cursor = pos;
2597
2598 // Need to restart syntax highlighting for this line.
2599 if (has_syntax) {
2600 syntax_start(wp, lnum);
2601 }
2602 }
2603 }
2604
2605 /*
2606 * Correct highlighting for cursor that can't be disabled.
2607 * Avoids having to check this for each character.
2608 */
2609 if (fromcol >= 0) {
2610 if (noinvcur) {
2611 if ((colnr_T)fromcol == wp->w_virtcol) {
2612 /* highlighting starts at cursor, let it start just after the
2613 * cursor */
2614 fromcol_prev = fromcol;
2615 fromcol = -1;
2616 } else if ((colnr_T)fromcol < wp->w_virtcol) {
2617 // restart highlighting after the cursor
2618 fromcol_prev = wp->w_virtcol;
2619 }
2620 }
2621 if (fromcol >= tocol) {
2622 fromcol = -1;
2623 }
2624 }
2625
2626 /*
2627 * Handle highlighting the last used search pattern and matches.
2628 * Do this for both search_hl and the match list.
2629 */
2630 cur = wp->w_match_head;
2631 shl_flag = false;
2632 while ((cur != NULL || !shl_flag) && !number_only
2633 && !has_fold && !end_fill) {
2634 if (!shl_flag) {
2635 shl = &search_hl;
2636 shl_flag = true;
2637 } else {
2638 shl = &cur->hl; // -V595
2639 }
2640 shl->startcol = MAXCOL;
2641 shl->endcol = MAXCOL;
2642 shl->attr_cur = 0;
2643 shl->is_addpos = false;
2644 v = (ptr - line);
2645 if (cur != NULL) {
2646 cur->pos.cur = 0;
2647 }
2648 next_search_hl(wp, shl, lnum, (colnr_T)v,
2649 shl == &search_hl ? NULL : cur);
2650 if (wp->w_s->b_syn_slow) {
2651 has_syntax = false;
2652 }
2653
2654 // Need to get the line again, a multi-line regexp may have made it
2655 // invalid.
2656 line = ml_get_buf(wp->w_buffer, lnum, false);
2657 ptr = line + v;
2658
2659 if (shl->lnum != 0 && shl->lnum <= lnum) {
2660 if (shl->lnum == lnum) {
2661 shl->startcol = shl->rm.startpos[0].col;
2662 } else {
2663 shl->startcol = 0;
2664 }
2665 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
2666 - shl->rm.startpos[0].lnum) {
2667 shl->endcol = shl->rm.endpos[0].col;
2668 } else {
2669 shl->endcol = MAXCOL;
2670 }
2671 // Highlight one character for an empty match.
2672 if (shl->startcol == shl->endcol) {
2673 if (line[shl->endcol] != NUL) {
2674 shl->endcol += utfc_ptr2len(line + shl->endcol);
2675 } else {
2676 ++shl->endcol;
2677 }
2678 }
2679 if ((long)shl->startcol < v) { // match at leftcol
2680 shl->attr_cur = shl->attr;
2681 search_attr = shl->attr;
2682 search_attr_from_match = shl != &search_hl;
2683 }
2684 area_highlighting = true;
2685 }
2686 if (shl != &search_hl && cur != NULL) {
2687 cur = cur->next;
2688 }
2689 }
2690
2691 unsigned off = 0; // Offset relative start of line
2692 int col = 0; // Visual column on screen.
2693 if (wp->w_p_rl) {
2694 // Rightleft window: process the text in the normal direction, but put
2695 // it in linebuf_char[off] from right to left. Start at the
2696 // rightmost column of the window.
2697 col = grid->Columns - 1;
2698 off += col;
2699 }
2700
2701 // won't highlight after TERM_ATTRS_MAX columns
2702 int term_attrs[TERM_ATTRS_MAX] = { 0 };
2703 if (wp->w_buffer->terminal) {
2704 terminal_get_line_attributes(wp->w_buffer->terminal, wp, lnum, term_attrs);
2705 extra_check = true;
2706 }
2707
2708 int sign_idx = 0;
2709 // Repeat for the whole displayed line.
2710 for (;;) {
2711 int has_match_conc = 0; ///< match wants to conceal
2712 bool did_decrement_ptr = false;
2713
2714 // Skip this quickly when working on the text.
2715 if (draw_state != WL_LINE) {
2716 if (cul_screenline) {
2717 cul_attr = 0;
2718 line_attr = line_attr_save;
2719 line_attr_lowprio = line_attr_lowprio_save;
2720 }
2721
2722 if (draw_state == WL_CMDLINE - 1 && n_extra == 0) {
2723 draw_state = WL_CMDLINE;
2724 if (cmdwin_type != 0 && wp == curwin) {
2725 // Draw the cmdline character.
2726 n_extra = 1;
2727 c_extra = cmdwin_type;
2728 c_final = NUL;
2729 char_attr = win_hl_attr(wp, HLF_AT);
2730 }
2731 }
2732
2733 if (draw_state == WL_FOLD - 1 && n_extra == 0) {
2734 int fdc = compute_foldcolumn(wp, 0);
2735
2736 draw_state = WL_FOLD;
2737 if (fdc > 0) {
2738 // Draw the 'foldcolumn'. Allocate a buffer, "extra" may
2739 // already be in use.
2740 xfree(p_extra_free);
2741 p_extra_free = xmalloc(MAX_MCO * fdc + 1);
2742 n_extra = (int)fill_foldcolumn(p_extra_free, wp, foldinfo, lnum);
2743 p_extra_free[n_extra] = NUL;
2744 p_extra = p_extra_free;
2745 c_extra = NUL;
2746 c_final = NUL;
2747 char_attr = win_hl_attr(wp, HLF_FC);
2748 }
2749 }
2750
2751 // sign column, this is hit until sign_idx reaches count
2752 if (draw_state == WL_SIGN - 1 && n_extra == 0) {
2753 draw_state = WL_SIGN;
2754 /* Show the sign column when there are any signs in this
2755 * buffer or when using Netbeans. */
2756 int count = win_signcol_count(wp);
2757 if (count > 0) {
2758 get_sign_display_info(false, wp, sattrs, row,
2759 startrow, filler_lines, filler_todo, count,
2760 &c_extra, &c_final, extra, sizeof(extra),
2761 &p_extra, &n_extra,
2762 &char_attr, &draw_state, &sign_idx);
2763 }
2764 }
2765
2766 if (draw_state == WL_NR - 1 && n_extra == 0) {
2767 draw_state = WL_NR;
2768 /* Display the absolute or relative line number. After the
2769 * first fill with blanks when the 'n' flag isn't in 'cpo' */
2770 if ((wp->w_p_nu || wp->w_p_rnu)
2771 && (row == startrow + filler_lines
2772 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL)) {
2773 // If 'signcolumn' is set to 'number' and a sign is present
2774 // in 'lnum', then display the sign instead of the line
2775 // number.
2776 if (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'
2777 && num_signs > 0) {
2778 int count = win_signcol_count(wp);
2779 get_sign_display_info(true, wp, sattrs, row,
2780 startrow, filler_lines, filler_todo, count,
2781 &c_extra, &c_final, extra, sizeof(extra),
2782 &p_extra, &n_extra,
2783 &char_attr, &draw_state, &sign_idx);
2784 } else {
2785 if (row == startrow + filler_lines) {
2786 // Draw the line number (empty space after wrapping). */
2787 long num;
2788 char *fmt = "%*ld ";
2789
2790 if (wp->w_p_nu && !wp->w_p_rnu) {
2791 // 'number' + 'norelativenumber'
2792 num = (long)lnum;
2793 } else {
2794 // 'relativenumber', don't use negative numbers
2795 num = labs((long)get_cursor_rel_lnum(wp, lnum));
2796 if (num == 0 && wp->w_p_nu && wp->w_p_rnu) {
2797 // 'number' + 'relativenumber'
2798 num = lnum;
2799 fmt = "%-*ld ";
2800 }
2801 }
2802
2803 snprintf((char *)extra, sizeof(extra),
2804 fmt, number_width(wp), num);
2805 if (wp->w_skipcol > 0) {
2806 for (p_extra = extra; *p_extra == ' '; p_extra++) {
2807 *p_extra = '-';
2808 }
2809 }
2810 if (wp->w_p_rl) { // reverse line numbers
2811 // like rl_mirror(), but keep the space at the end
2812 char_u *p2 = skipwhite(extra);
2813 p2 = skiptowhite(p2) - 1;
2814 for (char_u *p1 = skipwhite(extra); p1 < p2; p1++, p2--) {
2815 const int t = *p1;
2816 *p1 = *p2;
2817 *p2 = t;
2818 }
2819 }
2820 p_extra = extra;
2821 c_extra = NUL;
2822 c_final = NUL;
2823 } else {
2824 c_extra = ' ';
2825 c_final = NUL;
2826 }
2827 n_extra = number_width(wp) + 1;
2828 char_attr = win_hl_attr(wp, HLF_N);
2829
2830 if (wp->w_p_rnu && lnum < wp->w_cursor.lnum) {
2831 // Use LineNrAbove
2832 char_attr = win_hl_attr(wp, HLF_LNA);
2833 }
2834 if (wp->w_p_rnu && lnum > wp->w_cursor.lnum) {
2835 // Use LineNrBelow
2836 char_attr = win_hl_attr(wp, HLF_LNB);
2837 }
2838
2839 sign_attrs_T *num_sattr = sign_get_attr(SIGN_NUMHL, sattrs, 0, 1);
2840 if (num_sattr != NULL) {
2841 // :sign defined with "numhl" highlight.
2842 char_attr = num_sattr->sat_numhl;
2843 } else if (wp->w_p_cul
2844 && lnum == wp->w_cursor.lnum
2845 && (wp->w_p_culopt_flags & CULOPT_NBR)
2846 && (row == startrow
2847 || wp->w_p_culopt_flags & CULOPT_LINE)
2848 && filler_todo == 0) {
2849 // When 'cursorline' is set highlight the line number of
2850 // the current line differently.
2851 // When 'cursorlineopt' has "screenline" only highlight
2852 // the line number itself.
2853 // TODO(vim): Can we use CursorLine instead of CursorLineNr
2854 // when CursorLineNr isn't set?
2855 char_attr = win_hl_attr(wp, HLF_CLN);
2856 }
2857 }
2858 }
2859 }
2860
2861 if (draw_state == WL_NR && n_extra == 0) {
2862 win_col_offset = off;
2863 }
2864
2865 if (wp->w_briopt_sbr && draw_state == WL_BRI - 1
2866 && n_extra == 0 && *get_showbreak_value(wp) != NUL) {
2867 // draw indent after showbreak value
2868 draw_state = WL_BRI;
2869 } else if (wp->w_briopt_sbr && draw_state == WL_SBR && n_extra == 0) {
2870 // after the showbreak, draw the breakindent
2871 draw_state = WL_BRI - 1;
2872 }
2873
2874 // draw 'breakindent': indent wrapped text accordingly
2875 if (draw_state == WL_BRI - 1 && n_extra == 0) {
2876 draw_state = WL_BRI;
2877 // if need_showbreak is set, breakindent also applies
2878 if (wp->w_p_bri && (row != startrow || need_showbreak)
2879 && filler_lines == 0) {
2880 char_attr = 0;
2881
2882 if (diff_hlf != (hlf_T)0) {
2883 char_attr = win_hl_attr(wp, diff_hlf);
2884 }
2885 p_extra = NULL;
2886 c_extra = ' ';
2887 c_final = NUL;
2888 n_extra =
2889 get_breakindent_win(wp, ml_get_buf(wp->w_buffer, lnum, false));
2890 if (row == startrow) {
2891 n_extra -= win_col_off2(wp);
2892 if (n_extra < 0) {
2893 n_extra = 0;
2894 }
2895 }
2896 if (wp->w_skipcol > 0 && wp->w_p_wrap && wp->w_briopt_sbr) {
2897 need_showbreak = false;
2898 }
2899 // Correct end of highlighted area for 'breakindent',
2900 // required wen 'linebreak' is also set.
2901 if (tocol == vcol) {
2902 tocol += n_extra;
2903 }
2904 }
2905 }
2906
2907 if (draw_state == WL_SBR - 1 && n_extra == 0) {
2908 draw_state = WL_SBR;
2909 if (filler_todo > filler_lines - n_virt_lines) {
2910 // TODO(bfredl): check this doesn't inhibit TUI-style
2911 // clear-to-end-of-line.
2912 c_extra = ' ';
2913 c_final = NUL;
2914 if (wp->w_p_rl) {
2915 n_extra = col + 1;
2916 } else {
2917 n_extra = grid->Columns - col;
2918 }
2919 char_attr = 0;
2920 } else if (filler_todo > 0) {
2921 // draw "deleted" diff line(s)
2922 if (char2cells(wp->w_p_fcs_chars.diff) > 1) {
2923 c_extra = '-';
2924 c_final = NUL;
2925 } else {
2926 c_extra = wp->w_p_fcs_chars.diff;
2927 c_final = NUL;
2928 }
2929 if (wp->w_p_rl) {
2930 n_extra = col + 1;
2931 } else {
2932 n_extra = grid->Columns - col;
2933 }
2934 char_attr = win_hl_attr(wp, HLF_DED);
2935 }
2936 char_u *const sbr = get_showbreak_value(wp);
2937 if (*sbr != NUL && need_showbreak) {
2938 // Draw 'showbreak' at the start of each broken line.
2939 p_extra = sbr;
2940 c_extra = NUL;
2941 c_final = NUL;
2942 n_extra = (int)STRLEN(sbr);
2943 char_attr = win_hl_attr(wp, HLF_AT);
2944 if (wp->w_skipcol == 0 || !wp->w_p_wrap) {
2945 need_showbreak = false;
2946 }
2947 vcol_sbr = vcol + mb_charlen(sbr);
2948 // Correct end of highlighted area for 'showbreak',
2949 // required when 'linebreak' is also set.
2950 if (tocol == vcol) {
2951 tocol += n_extra;
2952 }
2953 // Combine 'showbreak' with 'cursorline', prioritizing 'showbreak'.
2954 if (cul_attr) {
2955 char_attr = hl_combine_attr(cul_attr, char_attr);
2956 }
2957 }
2958 }
2959
2960 if (draw_state == WL_LINE - 1 && n_extra == 0) {
2961 sign_idx = 0;
2962 draw_state = WL_LINE;
2963
2964 if (has_decor && row == startrow + filler_lines) {
2965 // hide virt_text on text hidden by 'nowrap'
2966 decor_redraw_col(wp->w_buffer, vcol, off, true, &decor_state);
2967 }
2968
2969 if (saved_n_extra) {
2970 // Continue item from end of wrapped line.
2971 n_extra = saved_n_extra;
2972 c_extra = saved_c_extra;
2973 c_final = saved_c_final;
2974 p_extra = saved_p_extra;
2975 char_attr = saved_char_attr;
2976 } else {
2977 char_attr = 0;
2978 }
2979 }
2980 }
2981
2982 if (cul_screenline && draw_state == WL_LINE
2983 && vcol >= left_curline_col
2984 && vcol < right_curline_col) {
2985 cul_attr = win_hl_attr(wp, HLF_CUL);
2986 HlAttrs ae = syn_attr2entry(cul_attr);
2987 if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) {
2988 line_attr_lowprio = cul_attr;
2989 } else {
2990 if (!(State & INSERT) && bt_quickfix(wp->w_buffer)
2991 && qf_current_entry(wp) == lnum) {
2992 line_attr = hl_combine_attr(cul_attr, line_attr);
2993 } else {
2994 line_attr = cul_attr;
2995 }
2996 }
2997 }
2998
2999 // When still displaying '$' of change command, stop at cursor
3000 if (((dollar_vcol >= 0
3001 && wp == curwin
3002 && lnum == wp->w_cursor.lnum
3003 && vcol >= (long)wp->w_virtcol)
3004 || (number_only && draw_state > WL_NR))
3005 && filler_todo <= 0) {
3006 draw_virt_text(buf, win_col_offset, &col, grid->Columns);
3007 grid_put_linebuf(grid, row, 0, col, -grid->Columns, wp->w_p_rl, wp,
3008 wp->w_hl_attr_normal, false);
3009 // Pretend we have finished updating the window. Except when
3010 // 'cursorcolumn' is set.
3011 if (wp->w_p_cuc) {
3012 row = wp->w_cline_row + wp->w_cline_height;
3013 } else {
3014 row = grid->Rows;
3015 }
3016 break;
3017 }
3018
3019 if (draw_state == WL_LINE
3020 && has_fold
3021 && vcol == 0
3022 && n_extra == 0
3023 && row == startrow) {
3024 char_attr = win_hl_attr(wp, HLF_FL);
3025
3026 linenr_T lnume = lnum + foldinfo.fi_lines - 1;
3027 memset(buf_fold, ' ', FOLD_TEXT_LEN);
3028 p_extra = get_foldtext(wp, lnum, lnume, foldinfo, buf_fold);
3029 n_extra = STRLEN(p_extra);
3030
3031 if (p_extra != buf_fold) {
3032 xfree(p_extra_free);
3033 p_extra_free = p_extra;
3034 }
3035 c_extra = NUL;
3036 c_final = NUL;
3037 p_extra[n_extra] = NUL;
3038 }
3039
3040 if (draw_state == WL_LINE
3041 && has_fold
3042 && col < grid->Columns
3043 && n_extra == 0
3044 && row == startrow) {
3045 // fill rest of line with 'fold'
3046 c_extra = wp->w_p_fcs_chars.fold;
3047 c_final = NUL;
3048
3049 n_extra = wp->w_p_rl ? (col + 1) : (grid->Columns - col);
3050 }
3051
3052 if (draw_state == WL_LINE
3053 && has_fold
3054 && col >= grid->Columns
3055 && n_extra != 0
3056 && row == startrow) {
3057 // Truncate the folding.
3058 n_extra = 0;
3059 }
3060
3061 if (draw_state == WL_LINE && (area_highlighting || has_spell)) {
3062 // handle Visual or match highlighting in this line
3063 if (vcol == fromcol
3064 || (vcol + 1 == fromcol && n_extra == 0
3065 && utf_ptr2cells(ptr) > 1)
3066 || ((int)vcol_prev == fromcol_prev
3067 && vcol_prev < vcol // not at margin
3068 && vcol < tocol)) {
3069 area_attr = attr; // start highlighting
3070 if (area_highlighting) {
3071 area_active = true;
3072 }
3073 } else if (area_attr != 0 && (vcol == tocol
3074 || (noinvcur
3075 && (colnr_T)vcol == wp->w_virtcol))) {
3076 area_attr = 0; // stop highlighting
3077 area_active = false;
3078 }
3079
3080 if (!n_extra) {
3081 /*
3082 * Check for start/end of search pattern match.
3083 * After end, check for start/end of next match.
3084 * When another match, have to check for start again.
3085 * Watch out for matching an empty string!
3086 * Do this for 'search_hl' and the match list (ordered by
3087 * priority).
3088 */
3089 v = (ptr - line);
3090 cur = wp->w_match_head;
3091 shl_flag = false;
3092 while (cur != NULL || !shl_flag) {
3093 if (!shl_flag
3094 && (cur == NULL || cur->priority > SEARCH_HL_PRIORITY)) {
3095 shl = &search_hl;
3096 shl_flag = true;
3097 } else {
3098 shl = &cur->hl;
3099 }
3100 if (cur != NULL) {
3101 cur->pos.cur = 0;
3102 }
3103 bool pos_inprogress = true; // mark that a position match search is
3104 // in progress
3105 while (shl->rm.regprog != NULL
3106 || (cur != NULL && pos_inprogress)) {
3107 if (shl->startcol != MAXCOL
3108 && v >= (long)shl->startcol
3109 && v < (long)shl->endcol) {
3110 int tmp_col = v + utfc_ptr2len(ptr);
3111
3112 if (shl->endcol < tmp_col) {
3113 shl->endcol = tmp_col;
3114 }
3115 shl->attr_cur = shl->attr;
3116 // Match with the "Conceal" group results in hiding
3117 // the match.
3118 if (cur != NULL
3119 && shl != &search_hl
3120 && syn_name2id("Conceal") == cur->hlg_id) {
3121 has_match_conc = v == (long)shl->startcol ? 2 : 1;
3122 match_conc = cur->conceal_char;
3123 } else {
3124 has_match_conc = 0;
3125 }
3126 } else if (v == (long)shl->endcol) {
3127 shl->attr_cur = 0;
3128
3129 next_search_hl(wp, shl, lnum, (colnr_T)v,
3130 shl == &search_hl ? NULL : cur);
3131 pos_inprogress = !(cur == NULL || cur->pos.cur == 0);
3132
3133 // Need to get the line again, a multi-line regexp
3134 // may have made it invalid.
3135 line = ml_get_buf(wp->w_buffer, lnum, false);
3136 ptr = line + v;
3137
3138 if (shl->lnum == lnum) {
3139 shl->startcol = shl->rm.startpos[0].col;
3140 if (shl->rm.endpos[0].lnum == 0) {
3141 shl->endcol = shl->rm.endpos[0].col;
3142 } else {
3143 shl->endcol = MAXCOL;
3144 }
3145
3146 if (shl->startcol == shl->endcol) {
3147 // highlight empty match, try again after it
3148 shl->endcol += utfc_ptr2len(line + shl->endcol);
3149 }
3150
3151 // Loop to check if the match starts at the
3152 // current position
3153 continue;
3154 }
3155 }
3156 break;
3157 }
3158 if (shl != &search_hl && cur != NULL) {
3159 cur = cur->next;
3160 }
3161 }
3162
3163 /* Use attributes from match with highest priority among
3164 * 'search_hl' and the match list. */
3165 search_attr_from_match = false;
3166 search_attr = search_hl.attr_cur;
3167 cur = wp->w_match_head;
3168 shl_flag = false;
3169 while (cur != NULL || !shl_flag) {
3170 if (!shl_flag
3171 && (cur == NULL || cur->priority > SEARCH_HL_PRIORITY)) {
3172 shl = &search_hl;
3173 shl_flag = true;
3174 } else {
3175 shl = &cur->hl;
3176 }
3177 if (shl->attr_cur != 0) {
3178 search_attr = shl->attr_cur;
3179 search_attr_from_match = shl != &search_hl;
3180 }
3181 if (shl != &search_hl && cur != NULL) {
3182 cur = cur->next;
3183 }
3184 }
3185 // Only highlight one character after the last column.
3186 if (*ptr == NUL
3187 && (wp->w_p_list && lcs_eol_one == -1)) {
3188 search_attr = 0;
3189 }
3190
3191 // Do not allow a conceal over EOL otherwise EOL will be missed
3192 // and bad things happen.
3193 if (*ptr == NUL) {
3194 has_match_conc = 0;
3195 }
3196 }
3197
3198 if (diff_hlf != (hlf_T)0) {
3199 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3200 && n_extra == 0) {
3201 diff_hlf = HLF_TXD; // changed text
3202 }
3203 if (diff_hlf == HLF_TXD && ptr - line > change_end
3204 && n_extra == 0) {
3205 diff_hlf = HLF_CHD; // changed line
3206 }
3207 line_attr = win_hl_attr(wp, diff_hlf);
3208 // Overlay CursorLine onto diff-mode highlight.
3209 if (cul_attr) {
3210 line_attr = 0 != line_attr_lowprio // Low-priority CursorLine
3211 ? hl_combine_attr(hl_combine_attr(cul_attr, line_attr),
3212 hl_get_underline())
3213 : hl_combine_attr(line_attr, cul_attr);
3214 }
3215 }
3216
3217 // Decide which of the highlight attributes to use.
3218 attr_pri = true;
3219
3220 if (area_attr != 0) {
3221 char_attr = hl_combine_attr(line_attr, area_attr);
3222 } else if (search_attr != 0) {
3223 char_attr = hl_combine_attr(line_attr, search_attr);
3224 }
3225 // Use line_attr when not in the Visual or 'incsearch' area
3226 // (area_attr may be 0 when "noinvcur" is set).
3227 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3228 || vcol < fromcol || vcol_prev < fromcol_prev
3229 || vcol >= tocol)) {
3230 char_attr = line_attr;
3231 } else {
3232 attr_pri = false;
3233 if (has_syntax) {
3234 char_attr = syntax_attr;
3235 } else {
3236 char_attr = 0;
3237 }
3238 }
3239 }
3240
3241 // Get the next character to put on the screen.
3242 //
3243 // The "p_extra" points to the extra stuff that is inserted to
3244 // represent special characters (non-printable stuff) and other
3245 // things. When all characters are the same, c_extra is used.
3246 // If c_final is set, it will compulsorily be used at the end.
3247 // "p_extra" must end in a NUL to avoid utfc_ptr2len() reads past
3248 // "p_extra[n_extra]".
3249 // For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3250 if (n_extra > 0) {
3251 if (c_extra != NUL || (n_extra == 1 && c_final != NUL)) {
3252 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
3253 mb_c = c; // doesn't handle non-utf-8 multi-byte!
3254 if (utf_char2len(c) > 1) {
3255 mb_utf8 = true;
3256 u8cc[0] = 0;
3257 c = 0xc0;
3258 } else {
3259 mb_utf8 = false;
3260 }
3261 } else {
3262 assert(p_extra != NULL);
3263 c = *p_extra;
3264 mb_c = c;
3265 // If the UTF-8 character is more than one byte:
3266 // Decode it into "mb_c".
3267 mb_l = utfc_ptr2len(p_extra);
3268 mb_utf8 = false;
3269 if (mb_l > n_extra) {
3270 mb_l = 1;
3271 } else if (mb_l > 1) {
3272 mb_c = utfc_ptr2char(p_extra, u8cc);
3273 mb_utf8 = true;
3274 c = 0xc0;
3275 }
3276 if (mb_l == 0) { // at the NUL at end-of-line
3277 mb_l = 1;
3278 }
3279
3280 // If a double-width char doesn't fit display a '>' in the last column.
3281 if ((wp->w_p_rl ? (col <= 0) : (col >= grid->Columns - 1))
3282 && utf_char2cells(mb_c) == 2) {
3283 c = '>';
3284 mb_c = c;
3285 mb_l = 1;
3286 (void)mb_l;
3287 multi_attr = win_hl_attr(wp, HLF_AT);
3288
3289 if (cul_attr) {
3290 multi_attr = 0 != line_attr_lowprio
3291 ? hl_combine_attr(cul_attr, multi_attr)
3292 : hl_combine_attr(multi_attr, cul_attr);
3293 }
3294
3295 // put the pointer back to output the double-width
3296 // character at the start of the next line.
3297 n_extra++;
3298 p_extra--;
3299 } else {
3300 n_extra -= mb_l - 1;
3301 p_extra += mb_l - 1;
3302 }
3303 p_extra++;
3304 }
3305 n_extra--;
3306 } else if (foldinfo.fi_lines > 0) {
3307 // skip writing the buffer line itself
3308 c = NUL;
3309 XFREE_CLEAR(p_extra_free);
3310 } else {
3311 int c0;
3312
3313 XFREE_CLEAR(p_extra_free);
3314
3315 // Get a character from the line itself.
3316 c0 = c = *ptr;
3317 mb_c = c;
3318 // If the UTF-8 character is more than one byte: Decode it
3319 // into "mb_c".
3320 mb_l = utfc_ptr2len(ptr);
3321 mb_utf8 = false;
3322 if (mb_l > 1) {
3323 mb_c = utfc_ptr2char(ptr, u8cc);
3324 // Overlong encoded ASCII or ASCII with composing char
3325 // is displayed normally, except a NUL.
3326 if (mb_c < 0x80) {
3327 c0 = c = mb_c;
3328 }
3329 mb_utf8 = true;
3330
3331 // At start of the line we can have a composing char.
3332 // Draw it as a space with a composing char.
3333 if (utf_iscomposing(mb_c)) {
3334 int i;
3335
3336 for (i = MAX_MCO - 1; i > 0; i--) {
3337 u8cc[i] = u8cc[i - 1];
3338 }
3339 u8cc[0] = mb_c;
3340 mb_c = ' ';
3341 }
3342 }
3343
3344 if ((mb_l == 1 && c >= 0x80)
3345 || (mb_l >= 1 && mb_c == 0)
3346 || (mb_l > 1 && (!vim_isprintc(mb_c)))) {
3347 // Illegal UTF-8 byte: display as <xx>.
3348 // Non-BMP character : display as ? or fullwidth ?.
3349 transchar_hex((char *)extra, mb_c);
3350 if (wp->w_p_rl) { // reverse
3351 rl_mirror(extra);
3352 }
3353
3354 p_extra = extra;
3355 c = *p_extra;
3356 mb_c = mb_ptr2char_adv((const char_u **)&p_extra);
3357 mb_utf8 = (c >= 0x80);
3358 n_extra = (int)STRLEN(p_extra);
3359 c_extra = NUL;
3360 c_final = NUL;
3361 if (area_attr == 0 && search_attr == 0) {
3362 n_attr = n_extra + 1;
3363 extra_attr = win_hl_attr(wp, HLF_8);
3364 saved_attr2 = char_attr; // save current attr
3365 }
3366 } else if (mb_l == 0) { // at the NUL at end-of-line
3367 mb_l = 1;
3368 } else if (p_arshape && !p_tbidi && arabic_char(mb_c)) {
3369 // Do Arabic shaping.
3370 int pc, pc1, nc;
3371 int pcc[MAX_MCO];
3372
3373 // The idea of what is the previous and next
3374 // character depends on 'rightleft'.
3375 if (wp->w_p_rl) {
3376 pc = prev_c;
3377 pc1 = prev_c1;
3378 nc = utf_ptr2char(ptr + mb_l);
3379 prev_c1 = u8cc[0];
3380 } else {
3381 pc = utfc_ptr2char(ptr + mb_l, pcc);
3382 nc = prev_c;
3383 pc1 = pcc[0];
3384 }
3385 prev_c = mb_c;
3386
3387 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
3388 } else {
3389 prev_c = mb_c;
3390 }
3391 // If a double-width char doesn't fit display a '>' in the
3392 // last column; the character is displayed at the start of the
3393 // next line.
3394 if ((wp->w_p_rl ? (col <= 0) :
3395 (col >= grid->Columns - 1))
3396 && utf_char2cells(mb_c) == 2) {
3397 c = '>';
3398 mb_c = c;
3399 mb_utf8 = false;
3400 mb_l = 1;
3401 multi_attr = win_hl_attr(wp, HLF_AT);
3402 // Put pointer back so that the character will be
3403 // displayed at the start of the next line.
3404 ptr--;
3405 did_decrement_ptr = true;
3406 } else if (*ptr != NUL) {
3407 ptr += mb_l - 1;
3408 }
3409
3410 // If a double-width char doesn't fit at the left side display a '<' in
3411 // the first column. Don't do this for unprintable characters.
3412 if (n_skip > 0 && mb_l > 1 && n_extra == 0) {
3413 n_extra = 1;
3414 c_extra = MB_FILLER_CHAR;
3415 c_final = NUL;
3416 c = ' ';
3417 if (area_attr == 0 && search_attr == 0) {
3418 n_attr = n_extra + 1;
3419 extra_attr = win_hl_attr(wp, HLF_AT);
3420 saved_attr2 = char_attr; // save current attr
3421 }
3422 mb_c = c;
3423 mb_utf8 = false;
3424 mb_l = 1;
3425 }
3426 ptr++;
3427
3428 if (extra_check) {
3429 bool can_spell = true;
3430
3431 /* Get syntax attribute, unless still at the start of the line
3432 * (double-wide char that doesn't fit). */
3433 v = (ptr - line);
3434 if (has_syntax && v > 0) {
3435 /* Get the syntax attribute for the character. If there
3436 * is an error, disable syntax highlighting. */
3437 save_did_emsg = did_emsg;
3438 did_emsg = FALSE;
3439
3440 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3441 has_spell ? &can_spell : NULL, false);
3442
3443 if (did_emsg) {
3444 wp->w_s->b_syn_error = TRUE;
3445 has_syntax = FALSE;
3446 } else {
3447 did_emsg = save_did_emsg;
3448 }
3449
3450 // Need to get the line again, a multi-line regexp may
3451 // have made it invalid.
3452 line = ml_get_buf(wp->w_buffer, lnum, false);
3453 ptr = line + v;
3454
3455 if (!attr_pri) {
3456 if (cul_attr) {
3457 char_attr = 0 != line_attr_lowprio
3458 ? hl_combine_attr(cul_attr, syntax_attr)
3459 : hl_combine_attr(syntax_attr, cul_attr);
3460 } else {
3461 char_attr = syntax_attr;
3462 }
3463 } else {
3464 char_attr = hl_combine_attr(syntax_attr, char_attr);
3465 }
3466 // no concealing past the end of the line, it interferes
3467 // with line highlighting.
3468 if (c == NUL) {
3469 syntax_flags = 0;
3470 } else {
3471 syntax_flags = get_syntax_info(&syntax_seqnr);
3472 }
3473 } else if (!attr_pri) {
3474 char_attr = 0;
3475 }
3476
3477 /* Check spelling (unless at the end of the line).
3478 * Only do this when there is no syntax highlighting, the
3479 * @Spell cluster is not used or the current syntax item
3480 * contains the @Spell cluster. */
3481 v = (ptr - line);
3482 if (has_spell && v >= word_end && v > cur_checked_col) {
3483 spell_attr = 0;
3484 if (!attr_pri) {
3485 char_attr = syntax_attr;
3486 }
3487 if (c != 0 && (!has_syntax || can_spell)) {
3488 char_u *prev_ptr;
3489 char_u *p;
3490 int len;
3491 hlf_T spell_hlf = HLF_COUNT;
3492 prev_ptr = ptr - mb_l;
3493 v -= mb_l - 1;
3494
3495 /* Use nextline[] if possible, it has the start of the
3496 * next line concatenated. */
3497 if ((prev_ptr - line) - nextlinecol >= 0) {
3498 p = nextline + ((prev_ptr - line) - nextlinecol);
3499 } else {
3500 p = prev_ptr;
3501 }
3502 cap_col -= (int)(prev_ptr - line);
3503 size_t tmplen = spell_check(wp, p, &spell_hlf, &cap_col, nochange);
3504 assert(tmplen <= INT_MAX);
3505 len = (int)tmplen;
3506 word_end = v + len;
3507
3508 /* In Insert mode only highlight a word that
3509 * doesn't touch the cursor. */
3510 if (spell_hlf != HLF_COUNT
3511 && (State & INSERT) != 0
3512 && wp->w_cursor.lnum == lnum
3513 && wp->w_cursor.col >=
3514 (colnr_T)(prev_ptr - line)
3515 && wp->w_cursor.col < (colnr_T)word_end) {
3516 spell_hlf = HLF_COUNT;
3517 spell_redraw_lnum = lnum;
3518 }
3519
3520 if (spell_hlf == HLF_COUNT && p != prev_ptr
3521 && (p - nextline) + len > nextline_idx) {
3522 /* Remember that the good word continues at the
3523 * start of the next line. */
3524 checked_lnum = lnum + 1;
3525 checked_col = (int)((p - nextline) + len - nextline_idx);
3526 }
3527
3528 // Turn index into actual attributes.
3529 if (spell_hlf != HLF_COUNT) {
3530 spell_attr = highlight_attr[spell_hlf];
3531 }
3532
3533 if (cap_col > 0) {
3534 if (p != prev_ptr
3535 && (p - nextline) + cap_col >= nextline_idx) {
3536 /* Remember that the word in the next line
3537 * must start with a capital. */
3538 capcol_lnum = lnum + 1;
3539 cap_col = (int)((p - nextline) + cap_col
3540 - nextline_idx);
3541 } else {
3542 // Compute the actual column.
3543 cap_col += (int)(prev_ptr - line);
3544 }
3545 }
3546 }
3547 }
3548 if (spell_attr != 0) {
3549 if (!attr_pri) {
3550 char_attr = hl_combine_attr(char_attr, spell_attr);
3551 } else {
3552 char_attr = hl_combine_attr(spell_attr, char_attr);
3553 }
3554 }
3555
3556 if (wp->w_buffer->terminal) {
3557 char_attr = hl_combine_attr(term_attrs[vcol], char_attr);
3558 }
3559
3560 if (has_decor && v > 0) {
3561 bool selected = (area_active || (area_highlighting && noinvcur
3562 && (colnr_T)vcol == wp->w_virtcol));
3563 int extmark_attr = decor_redraw_col(wp->w_buffer, (colnr_T)v-1, off,
3564 selected, &decor_state);
3565 if (extmark_attr != 0) {
3566 if (!attr_pri) {
3567 char_attr = hl_combine_attr(char_attr, extmark_attr);
3568 } else {
3569 char_attr = hl_combine_attr(extmark_attr, char_attr);
3570 }
3571 }
3572 }
3573
3574 // Found last space before word: check for line break.
3575 if (wp->w_p_lbr && c0 == c && vim_isbreak(c)
3576 && !vim_isbreak((int)(*ptr))) {
3577 int mb_off = utf_head_off(line, ptr - 1);
3578 char_u *p = ptr - (mb_off + 1);
3579 // TODO: is passing p for start of the line OK?
3580 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol, NULL) - 1;
3581
3582 // We have just drawn the showbreak value, no need to add
3583 // space for it again.
3584 if (vcol == vcol_sbr) {
3585 n_extra -= mb_charlen(get_showbreak_value(wp));
3586 if (n_extra < 0) {
3587 n_extra = 0;
3588 }
3589 }
3590
3591 if (c == TAB && n_extra + col > grid->Columns) {
3592 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
3593 wp->w_buffer->b_p_vts_array) - 1;
3594 }
3595 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
3596 c_final = NUL;
3597 if (ascii_iswhite(c)) {
3598 if (c == TAB) {
3599 // See "Tab alignment" below.
3600 FIX_FOR_BOGUSCOLS;
3601 }
3602 if (!wp->w_p_list) {
3603 c = ' ';
3604 }
3605 }
3606 }
3607
3608 in_multispace = c == ' ' && ((ptr > line + 1 && ptr[-2] == ' ') || *ptr == ' ');
3609 if (!in_multispace) {
3610 multispace_pos = 0;
3611 }
3612
3613 // 'list': Change char 160 to 'nbsp' and space to 'space'.
3614 // But not when the character is followed by a composing
3615 // character (use mb_l to check that).
3616 if (wp->w_p_list
3617 && ((((c == 160 && mb_l == 1)
3618 || (mb_utf8
3619 && ((mb_c == 160 && mb_l == 2)
3620 || (mb_c == 0x202f && mb_l == 3))))
3621 && wp->w_p_lcs_chars.nbsp)
3622 || (c == ' '
3623 && mb_l == 1
3624 && (wp->w_p_lcs_chars.space
3625 || (in_multispace && wp->w_p_lcs_chars.multispace != NULL))
3626 && ptr - line >= leadcol
3627 && ptr - line <= trailcol))) {
3628 if (in_multispace && wp->w_p_lcs_chars.multispace != NULL) {
3629 c = wp->w_p_lcs_chars.multispace[multispace_pos++];
3630 if (wp->w_p_lcs_chars.multispace[multispace_pos] == NUL) {
3631 multispace_pos = 0;
3632 }
3633 } else {
3634 c = (c == ' ') ? wp->w_p_lcs_chars.space : wp->w_p_lcs_chars.nbsp;
3635 }
3636 n_attr = 1;
3637 extra_attr = win_hl_attr(wp, HLF_0);
3638 saved_attr2 = char_attr; // save current attr
3639 mb_c = c;
3640 if (utf_char2len(c) > 1) {
3641 mb_utf8 = true;
3642 u8cc[0] = 0;
3643 c = 0xc0;
3644 } else {
3645 mb_utf8 = false;
3646 }
3647 }
3648
3649 if ((trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3650 || (leadcol != 0 && ptr < line + leadcol && c == ' ')) {
3651 c = (ptr > line + trailcol) ? wp->w_p_lcs_chars.trail
3652 : wp->w_p_lcs_chars.lead;
3653 n_attr = 1;
3654 extra_attr = win_hl_attr(wp, HLF_0);
3655 saved_attr2 = char_attr; // save current attr
3656 mb_c = c;
3657 if (utf_char2len(c) > 1) {
3658 mb_utf8 = true;
3659 u8cc[0] = 0;
3660 c = 0xc0;
3661 } else {
3662 mb_utf8 = false;
3663 }
3664 }
3665 }
3666
3667 /*
3668 * Handling of non-printable characters.
3669 */
3670 if (!vim_isprintc(c)) {
3671 // when getting a character from the file, we may have to
3672 // turn it into something else on the way to putting it on the screen.
3673 if (c == TAB && (!wp->w_p_list || wp->w_p_lcs_chars.tab1)) {
3674 int tab_len = 0;
3675 long vcol_adjusted = vcol; // removed showbreak length
3676 char_u *const sbr = get_showbreak_value(wp);
3677
3678 // Only adjust the tab_len, when at the first column after the
3679 // showbreak value was drawn.
3680 if (*sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap) {
3681 vcol_adjusted = vcol - mb_charlen(sbr);
3682 }
3683 // tab amount depends on current column
3684 tab_len = tabstop_padding(vcol_adjusted,
3685 wp->w_buffer->b_p_ts,
3686 wp->w_buffer->b_p_vts_array) - 1;
3687
3688 if (!wp->w_p_lbr || !wp->w_p_list) {
3689 n_extra = tab_len;
3690 } else {
3691 char_u *p;
3692 int i;
3693 int saved_nextra = n_extra;
3694
3695 if (vcol_off > 0) {
3696 // there are characters to conceal
3697 tab_len += vcol_off;
3698 }
3699 // boguscols before FIX_FOR_BOGUSCOLS macro from above.
3700 if (wp->w_p_lcs_chars.tab1 && old_boguscols > 0
3701 && n_extra > tab_len) {
3702 tab_len += n_extra - tab_len;
3703 }
3704
3705 // if n_extra > 0, it gives the number of chars
3706 // to use for a tab, else we need to calculate the width
3707 // for a tab
3708 int len = (tab_len * utf_char2len(wp->w_p_lcs_chars.tab2));
3709 if (n_extra > 0) {
3710 len += n_extra - tab_len;
3711 }
3712 c = wp->w_p_lcs_chars.tab1;
3713 p = xmalloc(len + 1);
3714 memset(p, ' ', len);
3715 p[len] = NUL;
3716 xfree(p_extra_free);
3717 p_extra_free = p;
3718 for (i = 0; i < tab_len; i++) {
3719 if (*p == NUL) {
3720 tab_len = i;
3721 break;
3722 }
3723 int lcs = wp->w_p_lcs_chars.tab2;
3724
3725 // if tab3 is given, need to change the char
3726 // for tab
3727 if (wp->w_p_lcs_chars.tab3 && i == tab_len - 1) {
3728 lcs = wp->w_p_lcs_chars.tab3;
3729 }
3730 utf_char2bytes(lcs, p);
3731 p += utf_char2len(lcs);
3732 n_extra += utf_char2len(lcs) - (saved_nextra > 0 ? 1 : 0);
3733 }
3734 p_extra = p_extra_free;
3735
3736 // n_extra will be increased by FIX_FOX_BOGUSCOLS
3737 // macro below, so need to adjust for that here
3738 if (vcol_off > 0) {
3739 n_extra -= vcol_off;
3740 }
3741 }
3742
3743 {
3744 int vc_saved = vcol_off;
3745
3746 // Tab alignment should be identical regardless of
3747 // 'conceallevel' value. So tab compensates of all
3748 // previous concealed characters, and thus resets
3749 // vcol_off and boguscols accumulated so far in the
3750 // line. Note that the tab can be longer than
3751 // 'tabstop' when there are concealed characters.
3752 FIX_FOR_BOGUSCOLS;
3753
3754 // Make sure, the highlighting for the tab char will be
3755 // correctly set further below (effectively reverts the
3756 // FIX_FOR_BOGSUCOLS macro.
3757 if (n_extra == tab_len + vc_saved && wp->w_p_list
3758 && wp->w_p_lcs_chars.tab1) {
3759 tab_len += vc_saved;
3760 }
3761 }
3762
3763 mb_utf8 = false; // don't draw as UTF-8
3764 if (wp->w_p_list) {
3765 c = (n_extra == 0 && wp->w_p_lcs_chars.tab3)
3766 ? wp->w_p_lcs_chars.tab3
3767 : wp->w_p_lcs_chars.tab1;
3768 if (wp->w_p_lbr) {
3769 c_extra = NUL; // using p_extra from above
3770 } else {
3771 c_extra = wp->w_p_lcs_chars.tab2;
3772 }
3773 c_final = wp->w_p_lcs_chars.tab3;
3774 n_attr = tab_len + 1;
3775 extra_attr = win_hl_attr(wp, HLF_0);
3776 saved_attr2 = char_attr; // save current attr
3777 mb_c = c;
3778 if (utf_char2len(c) > 1) {
3779 mb_utf8 = true;
3780 u8cc[0] = 0;
3781 c = 0xc0;
3782 }
3783 } else {
3784 c_final = NUL;
3785 c_extra = ' ';
3786 c = ' ';
3787 }
3788 } else if (c == NUL
3789 && (wp->w_p_list
3790 || ((fromcol >= 0 || fromcol_prev >= 0)
3791 && tocol > vcol
3792 && VIsual_mode != Ctrl_V
3793 && (wp->w_p_rl ? (col >= 0) : (col < grid->Columns))
3794 && !(noinvcur
3795 && lnum == wp->w_cursor.lnum
3796 && (colnr_T)vcol == wp->w_virtcol)))
3797 && lcs_eol_one > 0) {
3798 // Display a '$' after the line or highlight an extra
3799 // character if the line break is included.
3800 // For a diff line the highlighting continues after the "$".
3801 if (diff_hlf == (hlf_T)0
3802 && line_attr == 0
3803 && line_attr_lowprio == 0) {
3804 // In virtualedit, visual selections may extend beyond end of line
3805 if (area_highlighting && virtual_active()
3806 && tocol != MAXCOL && vcol < tocol) {
3807 n_extra = 0;
3808 } else {
3809 p_extra = at_end_str;
3810 n_extra = 1;
3811 c_extra = NUL;
3812 c_final = NUL;
3813 }
3814 }
3815 if (wp->w_p_list && wp->w_p_lcs_chars.eol > 0) {
3816 c = wp->w_p_lcs_chars.eol;
3817 } else {
3818 c = ' ';
3819 }
3820 lcs_eol_one = -1;
3821 ptr--; // put it back at the NUL
3822 extra_attr = win_hl_attr(wp, HLF_AT);
3823 n_attr = 1;
3824 mb_c = c;
3825 if (utf_char2len(c) > 1) {
3826 mb_utf8 = true;
3827 u8cc[0] = 0;
3828 c = 0xc0;
3829 } else {
3830 mb_utf8 = false; // don't draw as UTF-8
3831 }
3832 } else if (c != NUL) {
3833 p_extra = transchar_buf(wp->w_buffer, c);
3834 if (n_extra == 0) {
3835 n_extra = byte2cells(c) - 1;
3836 }
3837 if ((dy_flags & DY_UHEX) && wp->w_p_rl) {
3838 rl_mirror(p_extra); // reverse "<12>"
3839 }
3840 c_extra = NUL;
3841 c_final = NUL;
3842 if (wp->w_p_lbr) {
3843 char_u *p;
3844
3845 c = *p_extra;
3846 p = xmalloc(n_extra + 1);
3847 memset(p, ' ', n_extra);
3848 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
3849 p[n_extra] = NUL;
3850 xfree(p_extra_free);
3851 p_extra_free = p_extra = p;
3852 } else {
3853 n_extra = byte2cells(c) - 1;
3854 c = *p_extra++;
3855 }
3856 n_attr = n_extra + 1;
3857 extra_attr = win_hl_attr(wp, HLF_8);
3858 saved_attr2 = char_attr; // save current attr
3859 mb_utf8 = false; // don't draw as UTF-8
3860 } else if (VIsual_active
3861 && (VIsual_mode == Ctrl_V || VIsual_mode == 'v')
3862 && virtual_active()
3863 && tocol != MAXCOL
3864 && vcol < tocol
3865 && (wp->w_p_rl ? (col >= 0) : (col < grid->Columns))) {
3866 c = ' ';
3867 ptr--; // put it back at the NUL
3868 }
3869 }
3870
3871 if (wp->w_p_cole > 0
3872 && (wp != curwin || lnum != wp->w_cursor.lnum
3873 || conceal_cursor_line(wp))
3874 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
3875 && !(lnum_in_visual_area
3876 && vim_strchr(wp->w_p_cocu, 'v') == NULL)) {
3877 char_attr = conceal_attr;
3878 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
3879 && (syn_get_sub_char() != NUL
3880 || (has_match_conc && match_conc)
3881 || wp->w_p_cole == 1)
3882 && wp->w_p_cole != 3) {
3883 // First time at this concealed item: display one
3884 // character.
3885 if (has_match_conc && match_conc) {
3886 c = match_conc;
3887 } else if (syn_get_sub_char() != NUL) {
3888 c = syn_get_sub_char();
3889 } else if (wp->w_p_lcs_chars.conceal != NUL) {
3890 c = wp->w_p_lcs_chars.conceal;
3891 } else {
3892 c = ' ';
3893 }
3894
3895 prev_syntax_id = syntax_seqnr;
3896
3897 if (n_extra > 0) {
3898 vcol_off += n_extra;
3899 }
3900 vcol += n_extra;
3901 if (wp->w_p_wrap && n_extra > 0) {
3902 if (wp->w_p_rl) {
3903 col -= n_extra;
3904 boguscols -= n_extra;
3905 } else {
3906 boguscols += n_extra;
3907 col += n_extra;
3908 }
3909 }
3910 n_extra = 0;
3911 n_attr = 0;
3912 } else if (n_skip == 0) {
3913 is_concealing = true;
3914 n_skip = 1;
3915 }
3916 mb_c = c;
3917 if (utf_char2len(c) > 1) {
3918 mb_utf8 = true;
3919 u8cc[0] = 0;
3920 c = 0xc0;
3921 } else {
3922 mb_utf8 = false; // don't draw as UTF-8
3923 }
3924 } else {
3925 prev_syntax_id = 0;
3926 is_concealing = false;
3927 }
3928
3929 if (n_skip > 0 && did_decrement_ptr) {
3930 // not showing the '>', put pointer back to avoid getting stuck
3931 ptr++;
3932 }
3933 } // end of printing from buffer content
3934
3935 /* In the cursor line and we may be concealing characters: correct
3936 * the cursor column when we reach its position. */
3937 if (!did_wcol && draw_state == WL_LINE
3938 && wp == curwin && lnum == wp->w_cursor.lnum
3939 && conceal_cursor_line(wp)
3940 && (int)wp->w_virtcol <= vcol + n_skip) {
3941 if (wp->w_p_rl) {
3942 wp->w_wcol = grid->Columns - col + boguscols - 1;
3943 } else {
3944 wp->w_wcol = col - boguscols;
3945 }
3946 wp->w_wrow = row;
3947 did_wcol = true;
3948 wp->w_valid |= VALID_WCOL|VALID_WROW|VALID_VIRTCOL;
3949 }
3950
3951 // Don't override visual selection highlighting.
3952 if (n_attr > 0 && draw_state == WL_LINE && !search_attr_from_match) {
3953 char_attr = hl_combine_attr(char_attr, extra_attr);
3954 }
3955
3956 // Handle the case where we are in column 0 but not on the first
3957 // character of the line and the user wants us to show us a
3958 // special character (via 'listchars' option "precedes:<char>".
3959 if (lcs_prec_todo != NUL
3960 && wp->w_p_list
3961 && (wp->w_p_wrap ? (wp->w_skipcol > 0 && row == 0) : wp->w_leftcol > 0)
3962 && filler_todo <= 0
3963 && draw_state > WL_NR
3964 && c != NUL) {
3965 c = wp->w_p_lcs_chars.prec;
3966 lcs_prec_todo = NUL;
3967 if (utf_char2cells(mb_c) > 1) {
3968 // Double-width character being overwritten by the "precedes"
3969 // character, need to fill up half the character.
3970 c_extra = MB_FILLER_CHAR;
3971 c_final = NUL;
3972 n_extra = 1;
3973 n_attr = 2;
3974 extra_attr = win_hl_attr(wp, HLF_AT);
3975 }
3976 mb_c = c;
3977 if (utf_char2len(c) > 1) {
3978 mb_utf8 = true;
3979 u8cc[0] = 0;
3980 c = 0xc0;
3981 } else {
3982 mb_utf8 = false; // don't draw as UTF-8
3983 }
3984 saved_attr3 = char_attr; // save current attr
3985 char_attr = win_hl_attr(wp, HLF_AT); // overwriting char_attr
3986 n_attr3 = 1;
3987 }
3988
3989 // At end of the text line or just after the last character.
3990 if (c == NUL && eol_hl_off == 0) {
3991 long prevcol = (ptr - line) - 1;
3992
3993 // we're not really at that column when skipping some text
3994 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol) {
3995 prevcol++;
3996 }
3997
3998 // Invert at least one char, used for Visual and empty line or
3999 // highlight match at end of line. If it's beyond the last
4000 // char on the screen, just overwrite that one (tricky!) Not
4001 // needed when a '$' was displayed for 'list'.
4002 prevcol_hl_flag = false;
4003 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol) {
4004 prevcol_hl_flag = true;
4005 } else {
4006 cur = wp->w_match_head;
4007 while (cur != NULL) {
4008 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol) {
4009 prevcol_hl_flag = true;
4010 break;
4011 }
4012 cur = cur->next;
4013 }
4014 }
4015 if (wp->w_p_lcs_chars.eol == lcs_eol_one
4016 && ((area_attr != 0 && vcol == fromcol
4017 && (VIsual_mode != Ctrl_V
4018 || lnum == VIsual.lnum
4019 || lnum == curwin->w_cursor.lnum))
4020 // highlight 'hlsearch' match at end of line
4021 || prevcol_hl_flag)) {
4022 int n = 0;
4023
4024 if (wp->w_p_rl) {
4025 if (col < 0) {
4026 n = 1;
4027 }
4028 } else {
4029 if (col >= grid->Columns) {
4030 n = -1;
4031 }
4032 }
4033 if (n != 0) {
4034 /* At the window boundary, highlight the last character
4035 * instead (better than nothing). */
4036 off += n;
4037 col += n;
4038 } else {
4039 // Add a blank character to highlight.
4040 schar_from_ascii(linebuf_char[off], ' ');
4041 }
4042 if (area_attr == 0 && !has_fold) {
4043 // Use attributes from match with highest priority among
4044 // 'search_hl' and the match list.
4045 char_attr = search_hl.attr;
4046 cur = wp->w_match_head;
4047 shl_flag = false;
4048 while (cur != NULL || !shl_flag) {
4049 if (!shl_flag
4050 && (cur == NULL || cur->priority > SEARCH_HL_PRIORITY)) {
4051 shl = &search_hl;
4052 shl_flag = true;
4053 } else {
4054 shl = &cur->hl;
4055 }
4056 if ((ptr - line) - 1 == (long)shl->startcol
4057 && (shl == &search_hl || !shl->is_addpos)) {
4058 char_attr = shl->attr;
4059 }
4060 if (shl != &search_hl && cur != NULL) {
4061 cur = cur->next;
4062 }
4063 }
4064 }
4065
4066 int eol_attr = char_attr;
4067 if (cul_attr) {
4068 eol_attr = hl_combine_attr(cul_attr, eol_attr);
4069 }
4070 linebuf_attr[off] = eol_attr;
4071 if (wp->w_p_rl) {
4072 --col;
4073 --off;
4074 } else {
4075 ++col;
4076 ++off;
4077 }
4078 ++vcol;
4079 eol_hl_off = 1;
4080 }
4081 // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
4082 if (wp->w_p_wrap) {
4083 v = wp->w_skipcol;
4084 } else {
4085 v = wp->w_leftcol;
4086 }
4087
4088 // check if line ends before left margin
4089 if (vcol < v + col - win_col_off(wp)) {
4090 vcol = v + col - win_col_off(wp);
4091 }
4092 // Get rid of the boguscols now, we want to draw until the right
4093 // edge for 'cursorcolumn'.
4094 col -= boguscols;
4095 // boguscols = 0; // Disabled because value never read after this
4096
4097 if (draw_color_col) {
4098 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
4099 }
4100
4101 bool has_virttext = false;
4102 // Make sure alignment is the same regardless
4103 // if listchars=eol:X is used or not.
4104 int eol_skip = (wp->w_p_lcs_chars.eol == lcs_eol_one && eol_hl_off == 0
4105 ? 1 : 0);
4106
4107 if (has_decor) {
4108 has_virttext = decor_redraw_eol(wp->w_buffer, &decor_state, &line_attr,
4109 col + eol_skip);
4110 }
4111
4112 if (((wp->w_p_cuc
4113 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4114 && (int)wp->w_virtcol <
4115 grid->Columns * (row - startrow + 1) + v
4116 && lnum != wp->w_cursor.lnum)
4117 || draw_color_col || line_attr_lowprio || line_attr
4118 || diff_hlf != (hlf_T)0 || has_virttext)) {
4119 int rightmost_vcol = 0;
4120 int i;
4121
4122 if (wp->w_p_cuc) {
4123 rightmost_vcol = wp->w_virtcol;
4124 }
4125
4126 if (draw_color_col) {
4127 // determine rightmost colorcolumn to possibly draw
4128 for (i = 0; color_cols[i] >= 0; i++) {
4129 if (rightmost_vcol < color_cols[i]) {
4130 rightmost_vcol = color_cols[i];
4131 }
4132 }
4133 }
4134
4135 int cuc_attr = win_hl_attr(wp, HLF_CUC);
4136 int mc_attr = win_hl_attr(wp, HLF_MC);
4137
4138 int diff_attr = 0;
4139 if (diff_hlf == HLF_TXD) {
4140 diff_hlf = HLF_CHD;
4141 }
4142 if (diff_hlf != 0) {
4143 diff_attr = win_hl_attr(wp, diff_hlf);
4144 }
4145
4146 int base_attr = hl_combine_attr(line_attr_lowprio, diff_attr);
4147 if (base_attr || line_attr || has_virttext) {
4148 rightmost_vcol = INT_MAX;
4149 }
4150
4151 int col_stride = wp->w_p_rl ? -1 : 1;
4152
4153 while (wp->w_p_rl ? col >= 0 : col < grid->Columns) {
4154 schar_from_ascii(linebuf_char[off], ' ');
4155 col += col_stride;
4156 if (draw_color_col) {
4157 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
4158 }
4159
4160 int col_attr = base_attr;
4161
4162 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol) {
4163 col_attr = cuc_attr;
4164 } else if (draw_color_col && VCOL_HLC == *color_cols) {
4165 col_attr = mc_attr;
4166 }
4167
4168 col_attr = hl_combine_attr(col_attr, line_attr);
4169
4170 linebuf_attr[off] = col_attr;
4171 off += col_stride;
4172
4173 if (VCOL_HLC >= rightmost_vcol) {
4174 break;
4175 }
4176
4177 vcol += 1;
4178 }
4179 }
4180
4181 // TODO(bfredl): integrate with the common beyond-the-end-loop
4182 if (wp->w_buffer->terminal) {
4183 // terminal buffers may need to highlight beyond the end of the
4184 // logical line
4185 int n = wp->w_p_rl ? -1 : 1;
4186 while (col >= 0 && col < grid->Columns) {
4187 schar_from_ascii(linebuf_char[off], ' ');
4188 linebuf_attr[off] = vcol >= TERM_ATTRS_MAX ? 0 : term_attrs[vcol];
4189 off += n;
4190 vcol += n;
4191 col += n;
4192 }
4193 }
4194
4195 draw_virt_text(buf, win_col_offset, &col, grid->Columns);
4196 grid_put_linebuf(grid, row, 0, col, grid->Columns, wp->w_p_rl, wp,
4197 wp->w_hl_attr_normal, false);
4198 row++;
4199
4200 /*
4201 * Update w_cline_height and w_cline_folded if the cursor line was
4202 * updated (saves a call to plines_win() later).
4203 */
4204 if (wp == curwin && lnum == curwin->w_cursor.lnum) {
4205 curwin->w_cline_row = startrow;
4206 curwin->w_cline_height = row - startrow;
4207 curwin->w_cline_folded = foldinfo.fi_lines > 0;
4208 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4209 conceal_cursor_used = conceal_cursor_line(curwin);
4210 }
4211 break;
4212 }
4213
4214 // Show "extends" character from 'listchars' if beyond the line end and
4215 // 'list' is set.
4216 if (wp->w_p_lcs_chars.ext != NUL
4217 && wp->w_p_list
4218 && !wp->w_p_wrap
4219 && filler_todo <= 0
4220 && (wp->w_p_rl ? col == 0 : col == grid->Columns - 1)
4221 && !has_fold
4222 && (*ptr != NUL
4223 || lcs_eol_one > 0
4224 || (n_extra && (c_extra != NUL || *p_extra != NUL)))) {
4225 c = wp->w_p_lcs_chars.ext;
4226 char_attr = win_hl_attr(wp, HLF_AT);
4227 mb_c = c;
4228 if (utf_char2len(c) > 1) {
4229 mb_utf8 = true;
4230 u8cc[0] = 0;
4231 c = 0xc0;
4232 } else {
4233 mb_utf8 = false;
4234 }
4235 }
4236
4237 // advance to the next 'colorcolumn'
4238 if (draw_color_col) {
4239 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
4240 }
4241
4242 // Highlight the cursor column if 'cursorcolumn' is set. But don't
4243 // highlight the cursor position itself.
4244 // Also highlight the 'colorcolumn' if it is different than
4245 // 'cursorcolumn'
4246 // Also highlight the 'colorcolumn' if 'breakindent' and/or 'showbreak'
4247 // options are set
4248 vcol_save_attr = -1;
4249 if ((draw_state == WL_LINE
4250 || draw_state == WL_BRI
4251 || draw_state == WL_SBR)
4252 && !lnum_in_visual_area
4253 && search_attr == 0
4254 && area_attr == 0
4255 && filler_todo <= 0) {
4256 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
4257 && lnum != wp->w_cursor.lnum) {
4258 vcol_save_attr = char_attr;
4259 char_attr = hl_combine_attr(win_hl_attr(wp, HLF_CUC), char_attr);
4260 } else if (draw_color_col && VCOL_HLC == *color_cols) {
4261 vcol_save_attr = char_attr;
4262 char_attr = hl_combine_attr(win_hl_attr(wp, HLF_MC), char_attr);
4263 }
4264 }
4265
4266 // Apply lowest-priority line attr now, so everything can override it.
4267 if (draw_state == WL_LINE) {
4268 char_attr = hl_combine_attr(line_attr_lowprio, char_attr);
4269 }
4270
4271 // Store character to be displayed.
4272 // Skip characters that are left of the screen for 'nowrap'.
4273 vcol_prev = vcol;
4274 if (draw_state < WL_LINE || n_skip <= 0) {
4275 //
4276 // Store the character.
4277 //
4278 if (wp->w_p_rl && utf_char2cells(mb_c) > 1) {
4279 // A double-wide character is: put first halve in left cell.
4280 off--;
4281 col--;
4282 }
4283 if (mb_utf8) {
4284 schar_from_cc(linebuf_char[off], mb_c, u8cc);
4285 } else {
4286 schar_from_ascii(linebuf_char[off], c);
4287 }
4288 if (multi_attr) {
4289 linebuf_attr[off] = multi_attr;
4290 multi_attr = 0;
4291 } else {
4292 linebuf_attr[off] = char_attr;
4293 }
4294
4295 if (utf_char2cells(mb_c) > 1) {
4296 // Need to fill two screen columns.
4297 off++;
4298 col++;
4299 // UTF-8: Put a 0 in the second screen char.
4300 linebuf_char[off][0] = 0;
4301 if (draw_state > WL_NR && filler_todo <= 0) {
4302 vcol++;
4303 }
4304 // When "tocol" is halfway through a character, set it to the end of
4305 // the character, otherwise highlighting won't stop.
4306 if (tocol == vcol) {
4307 tocol++;
4308 }
4309 if (wp->w_p_rl) {
4310 // now it's time to backup one cell
4311 --off;
4312 --col;
4313 }
4314 }
4315 if (wp->w_p_rl) {
4316 --off;
4317 --col;
4318 } else {
4319 ++off;
4320 ++col;
4321 }
4322 } else if (wp->w_p_cole > 0 && is_concealing) {
4323 --n_skip;
4324 ++vcol_off;
4325 if (n_extra > 0) {
4326 vcol_off += n_extra;
4327 }
4328 if (wp->w_p_wrap) {
4329 /*
4330 * Special voodoo required if 'wrap' is on.
4331 *
4332 * Advance the column indicator to force the line
4333 * drawing to wrap early. This will make the line
4334 * take up the same screen space when parts are concealed,
4335 * so that cursor line computations aren't messed up.
4336 *
4337 * To avoid the fictitious advance of 'col' causing
4338 * trailing junk to be written out of the screen line
4339 * we are building, 'boguscols' keeps track of the number
4340 * of bad columns we have advanced.
4341 */
4342 if (n_extra > 0) {
4343 vcol += n_extra;
4344 if (wp->w_p_rl) {
4345 col -= n_extra;
4346 boguscols -= n_extra;
4347 } else {
4348 col += n_extra;
4349 boguscols += n_extra;
4350 }
4351 n_extra = 0;
4352 n_attr = 0;
4353 }
4354
4355
4356 if (utf_char2cells(mb_c) > 1) {
4357 // Need to fill two screen columns.
4358 if (wp->w_p_rl) {
4359 --boguscols;
4360 --col;
4361 } else {
4362 ++boguscols;
4363 ++col;
4364 }
4365 }
4366
4367 if (wp->w_p_rl) {
4368 --boguscols;
4369 --col;
4370 } else {
4371 ++boguscols;
4372 ++col;
4373 }
4374 } else {
4375 if (n_extra > 0) {
4376 vcol += n_extra;
4377 n_extra = 0;
4378 n_attr = 0;
4379 }
4380 }
4381 } else {
4382 --n_skip;
4383 }
4384
4385 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
4386 * column. */
4387 if (draw_state > WL_NR
4388 && filler_todo <= 0) {
4389 ++vcol;
4390 }
4391
4392 if (vcol_save_attr >= 0) {
4393 char_attr = vcol_save_attr;
4394 }
4395
4396 // restore attributes after "predeces" in 'listchars'
4397 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0) {
4398 char_attr = saved_attr3;
4399 }
4400
4401 // restore attributes after last 'listchars' or 'number' char
4402 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0) {
4403 char_attr = saved_attr2;
4404 }
4405
4406 /*
4407 * At end of screen line and there is more to come: Display the line
4408 * so far. If there is no more to display it is caught above.
4409 */
4410 if ((wp->w_p_rl ? (col < 0) : (col >= grid->Columns))
4411 && foldinfo.fi_lines == 0
4412 && (*ptr != NUL
4413 || filler_todo > 0
4414 || (wp->w_p_list && wp->w_p_lcs_chars.eol != NUL
4415 && p_extra != at_end_str)
4416 || (n_extra != 0
4417 && (c_extra != NUL || *p_extra != NUL)))) {
4418 bool wrap = wp->w_p_wrap // Wrapping enabled.
4419 && filler_todo <= 0 // Not drawing diff filler lines.
4420 && lcs_eol_one != -1 // Haven't printed the lcs_eol character.
4421 && row != endrow - 1 // Not the last line being displayed.
4422 && (grid->Columns == Columns // Window spans the width of the screen,
4423 || ui_has(kUIMultigrid)) // or has dedicated grid.
4424 && !wp->w_p_rl; // Not right-to-left.
4425
4426 int draw_col = col - boguscols;
4427 if (filler_todo > 0) {
4428 int index = filler_todo - (filler_lines - n_virt_lines);
4429 if (index > 0) {
4430 int i = kv_size(virt_lines) - index;
4431 assert(i >= 0);
4432 int offset = kv_A(virt_lines, i).left_col ? 0 : win_col_offset;
4433 draw_virt_text_item(buf, offset, kv_A(virt_lines, i).line,
4434 kHlModeReplace, grid->Columns, offset);
4435 }
4436 } else {
4437 draw_virt_text(buf, win_col_offset, &draw_col, grid->Columns);
4438 }
4439
4440 grid_put_linebuf(grid, row, 0, draw_col, grid->Columns, wp->w_p_rl,
4441 wp, wp->w_hl_attr_normal, wrap);
4442 if (wrap) {
4443 ScreenGrid *current_grid = grid;
4444 int current_row = row, dummy_col = 0; // dummy_col unused
4445 screen_adjust_grid(¤t_grid, ¤t_row, &dummy_col);
4446
4447 // Force a redraw of the first column of the next line.
4448 current_grid->attrs[current_grid->line_offset[current_row+1]] = -1;
4449
4450 // Remember that the line wraps, used for modeless copy.
4451 current_grid->line_wraps[current_row] = true;
4452 }
4453
4454 boguscols = 0;
4455 row++;
4456
4457 /* When not wrapping and finished diff lines, or when displayed
4458 * '$' and highlighting until last column, break here. */
4459 if ((!wp->w_p_wrap
4460 && filler_todo <= 0
4461 ) || lcs_eol_one == -1) {
4462 break;
4463 }
4464
4465 // When the window is too narrow draw all "@" lines.
4466 if (draw_state != WL_LINE && filler_todo <= 0) {
4467 win_draw_end(wp, '@', ' ', true, row, wp->w_grid.Rows, HLF_AT);
4468 row = endrow;
4469 }
4470
4471 // When line got too long for screen break here.
4472 if (row == endrow) {
4473 ++row;
4474 break;
4475 }
4476
4477 col = 0;
4478 off = 0;
4479 if (wp->w_p_rl) {
4480 col = grid->Columns - 1; // col is not used if breaking!
4481 off += col;
4482 }
4483
4484 // reset the drawing state for the start of a wrapped line
4485 draw_state = WL_START;
4486 saved_n_extra = n_extra;
4487 saved_p_extra = p_extra;
4488 saved_c_extra = c_extra;
4489 saved_c_final = c_final;
4490 saved_char_attr = char_attr;
4491 n_extra = 0;
4492 lcs_prec_todo = wp->w_p_lcs_chars.prec;
4493 if (filler_todo <= 0) {
4494 need_showbreak = true;
4495 }
4496 filler_todo--;
4497 // When the filler lines are actually below the last line of the
4498 // file, don't draw the line itself, break here.
4499 if (filler_todo == 0 && (wp->w_botfill || end_fill)) {
4500 break;
4501 }
4502 }
4503 } // for every character in the line
4504
4505 // After an empty line check first word for capital.
4506 if (*skipwhite(line) == NUL) {
4507 capcol_lnum = lnum + 1;
4508 cap_col = 0;
4509 }
4510
4511 kv_destroy(virt_lines);
4512 xfree(p_extra_free);
4513 return row;
4514 }
4515
draw_virt_text(buf_T * buf,int col_off,int * end_col,int max_col)4516 void draw_virt_text(buf_T *buf, int col_off, int *end_col, int max_col)
4517 {
4518 DecorState *state = &decor_state;
4519 int right_pos = max_col;
4520 bool do_eol = state->eol_col > -1;
4521 for (size_t i = 0; i < kv_size(state->active); i++) {
4522 DecorRange *item = &kv_A(state->active, i);
4523 if (!(item->start_row == state->row && kv_size(item->decor.virt_text))) {
4524 continue;
4525 }
4526 if (item->win_col == -1) {
4527 if (item->decor.virt_text_pos == kVTRightAlign) {
4528 right_pos -= item->decor.virt_text_width;
4529 item->win_col = right_pos;
4530 } else if (item->decor.virt_text_pos == kVTEndOfLine && do_eol) {
4531 item->win_col = state->eol_col;
4532 } else if (item->decor.virt_text_pos == kVTWinCol) {
4533 item->win_col = MAX(item->decor.col+col_off, 0);
4534 }
4535 }
4536 if (item->win_col < 0) {
4537 continue;
4538 }
4539
4540 int col = draw_virt_text_item(buf, item->win_col, item->decor.virt_text,
4541 item->decor.hl_mode, max_col, item->win_col-col_off);
4542 item->win_col = -2; // deactivate
4543 if (item->decor.virt_text_pos == kVTEndOfLine && do_eol) {
4544 state->eol_col = col+1;
4545 }
4546
4547 *end_col = MAX(*end_col, col);
4548 }
4549 }
4550
draw_virt_text_item(buf_T * buf,int col,VirtText vt,HlMode hl_mode,int max_col,int vcol)4551 static int draw_virt_text_item(buf_T *buf, int col, VirtText vt, HlMode hl_mode, int max_col,
4552 int vcol)
4553 {
4554 LineState s = LINE_STATE("");
4555 int virt_attr = 0;
4556 size_t virt_pos = 0;
4557
4558 while (col < max_col) {
4559 if (!*s.p) {
4560 if (virt_pos >= kv_size(vt)) {
4561 break;
4562 }
4563 virt_attr = 0;
4564 do {
4565 s.p = kv_A(vt, virt_pos).text;
4566 int hl_id = kv_A(vt, virt_pos).hl_id;
4567 virt_attr = hl_combine_attr(virt_attr,
4568 hl_id > 0 ? syn_id2attr(hl_id) : 0);
4569 virt_pos++;
4570 } while (!s.p && virt_pos < kv_size(vt));
4571 if (!s.p) {
4572 break;
4573 }
4574 }
4575 int attr;
4576 bool through = false;
4577 if (hl_mode == kHlModeCombine) {
4578 attr = hl_combine_attr(linebuf_attr[col], virt_attr);
4579 } else if (hl_mode == kHlModeBlend) {
4580 through = (*s.p == ' ');
4581 attr = hl_blend_attrs(linebuf_attr[col], virt_attr, &through);
4582 } else {
4583 attr = virt_attr;
4584 }
4585 schar_T dummy[2];
4586 int cells = line_putchar(buf, &s, through ? dummy : &linebuf_char[col],
4587 max_col-col, false, vcol);
4588 // if we failed to emit a char, we still need to advance
4589 cells = MAX(cells, 1);
4590
4591 for (int c = 0; c < cells; c++) {
4592 linebuf_attr[col++] = attr;
4593 }
4594 vcol += cells;
4595 }
4596 return col;
4597 }
4598
4599 /// Determine if dedicated window grid should be used or the default_grid
4600 ///
4601 /// If UI did not request multigrid support, draw all windows on the
4602 /// default_grid.
4603 ///
4604 /// NB: this function can only been used with window grids in a context where
4605 /// win_grid_alloc already has been called!
4606 ///
4607 /// If the default_grid is used, adjust window relative positions to global
4608 /// screen positions.
screen_adjust_grid(ScreenGrid ** grid,int * row_off,int * col_off)4609 void screen_adjust_grid(ScreenGrid **grid, int *row_off, int *col_off)
4610 {
4611 if ((*grid)->target) {
4612 *row_off += (*grid)->row_offset;
4613 *col_off += (*grid)->col_offset;
4614 *grid = (*grid)->target;
4615 }
4616 }
4617
4618 // Get information needed to display the sign in line 'lnum' in window 'wp'.
4619 // If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
4620 // Otherwise the sign is going to be displayed in the sign column.
4621 //
4622 // @param count max number of signs
4623 // @param[out] n_extrap number of characters from pp_extra to display
4624 // @param[in, out] sign_idxp Index of the displayed sign
get_sign_display_info(bool nrcol,win_T * wp,sign_attrs_T sattrs[],int row,int startrow,int filler_lines,int filler_todo,int count,int * c_extrap,int * c_finalp,char_u * extra,size_t extra_size,char_u ** pp_extra,int * n_extrap,int * char_attrp,int * draw_statep,int * sign_idxp)4625 static void get_sign_display_info(bool nrcol, win_T *wp, sign_attrs_T sattrs[], int row,
4626 int startrow, int filler_lines, int filler_todo, int count,
4627 int *c_extrap, int *c_finalp, char_u *extra, size_t extra_size,
4628 char_u **pp_extra, int *n_extrap, int *char_attrp,
4629 int *draw_statep, int *sign_idxp)
4630 {
4631 // Draw cells with the sign value or blank.
4632 *c_extrap = ' ';
4633 *c_finalp = NUL;
4634 if (nrcol) {
4635 *n_extrap = number_width(wp) + 1;
4636 } else {
4637 *char_attrp = win_hl_attr(wp, HLF_SC);
4638 *n_extrap = win_signcol_width(wp);
4639 }
4640
4641 if (row == startrow + filler_lines && filler_todo <= 0) {
4642 sign_attrs_T *sattr = sign_get_attr(SIGN_TEXT, sattrs, *sign_idxp, count);
4643 if (sattr != NULL) {
4644 *pp_extra = sattr->sat_text;
4645 if (*pp_extra != NULL) {
4646 *c_extrap = NUL;
4647 *c_finalp = NUL;
4648
4649 if (nrcol) {
4650 int n, width = number_width(wp) - 2;
4651 for (n = 0; n < width; n++) {
4652 extra[n] = ' ';
4653 }
4654 extra[n] = NUL;
4655 STRCAT(extra, *pp_extra);
4656 STRCAT(extra, " ");
4657 *pp_extra = extra;
4658 *n_extrap = (int)STRLEN(*pp_extra);
4659 } else {
4660 int symbol_blen = (int)STRLEN(*pp_extra);
4661
4662 // TODO(oni-link): Is sign text already extended to
4663 // full cell width?
4664 assert((size_t)win_signcol_width(wp) >= mb_string2cells(*pp_extra));
4665 // symbol(s) bytes + (filling spaces) (one byte each)
4666 *n_extrap = symbol_blen +
4667 (win_signcol_width(wp) - mb_string2cells(*pp_extra));
4668
4669 assert(extra_size > (size_t)symbol_blen);
4670 memset(extra, ' ', extra_size);
4671 memcpy(extra, *pp_extra, symbol_blen);
4672
4673 *pp_extra = extra;
4674 (*pp_extra)[*n_extrap] = NUL;
4675 }
4676 }
4677 *char_attrp = sattr->sat_texthl;
4678 }
4679 }
4680
4681 (*sign_idxp)++;
4682 if (*sign_idxp < count) {
4683 *draw_statep = WL_SIGN - 1;
4684 } else {
4685 *sign_idxp = 0;
4686 }
4687 }
4688
4689
4690 /*
4691 * Check whether the given character needs redrawing:
4692 * - the (first byte of the) character is different
4693 * - the attributes are different
4694 * - the character is multi-byte and the next byte is different
4695 * - the character is two cells wide and the second cell differs.
4696 */
grid_char_needs_redraw(ScreenGrid * grid,int off_from,int off_to,int cols)4697 static int grid_char_needs_redraw(ScreenGrid *grid, int off_from, int off_to, int cols)
4698 {
4699 return (cols > 0
4700 && ((schar_cmp(linebuf_char[off_from], grid->chars[off_to])
4701 || linebuf_attr[off_from] != grid->attrs[off_to]
4702 || (line_off2cells(linebuf_char, off_from, off_from + cols) > 1
4703 && schar_cmp(linebuf_char[off_from + 1],
4704 grid->chars[off_to + 1])))
4705 || rdb_flags & RDB_NODELTA));
4706 }
4707
4708 /// Move one buffered line to the window grid, but only the characters that
4709 /// have actually changed. Handle insert/delete character.
4710 /// "coloff" gives the first column on the grid for this line.
4711 /// "endcol" gives the columns where valid characters are.
4712 /// "clear_width" is the width of the window. It's > 0 if the rest of the line
4713 /// needs to be cleared, negative otherwise.
4714 /// "rlflag" is TRUE in a rightleft window:
4715 /// When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4716 /// When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4717 /// If "wrap" is true, then hint to the UI that "row" contains a line
4718 /// which has wrapped into the next row.
grid_put_linebuf(ScreenGrid * grid,int row,int coloff,int endcol,int clear_width,int rlflag,win_T * wp,int bg_attr,bool wrap)4719 static void grid_put_linebuf(ScreenGrid *grid, int row, int coloff, int endcol, int clear_width,
4720 int rlflag, win_T *wp, int bg_attr, bool wrap)
4721 {
4722 unsigned off_from;
4723 unsigned off_to;
4724 unsigned max_off_from;
4725 unsigned max_off_to;
4726 int col = 0;
4727 bool redraw_this; // Does character need redraw?
4728 bool redraw_next; // redraw_this for next character
4729 bool clear_next = false;
4730 int char_cells; // 1: normal char
4731 // 2: occupies two display cells
4732 int start_dirty = -1, end_dirty = 0;
4733
4734 // TODO(bfredl): check all callsites and eliminate
4735 // Check for illegal row and col, just in case
4736 if (row >= grid->Rows) {
4737 row = grid->Rows - 1;
4738 }
4739 if (endcol > grid->Columns) {
4740 endcol = grid->Columns;
4741 }
4742
4743 screen_adjust_grid(&grid, &row, &coloff);
4744
4745 // Safety check. Avoids clang warnings down the call stack.
4746 if (grid->chars == NULL || row >= grid->Rows || coloff >= grid->Columns) {
4747 DLOG("invalid state, skipped");
4748 return;
4749 }
4750
4751 off_from = 0;
4752 off_to = grid->line_offset[row] + coloff;
4753 max_off_from = linebuf_size;
4754 max_off_to = grid->line_offset[row] + grid->Columns;
4755
4756 if (rlflag) {
4757 // Clear rest first, because it's left of the text.
4758 if (clear_width > 0) {
4759 while (col <= endcol && grid->chars[off_to][0] == ' '
4760 && grid->chars[off_to][1] == NUL
4761 && grid->attrs[off_to] == bg_attr) {
4762 ++off_to;
4763 ++col;
4764 }
4765 if (col <= endcol) {
4766 grid_fill(grid, row, row + 1, col + coloff, endcol + coloff + 1,
4767 ' ', ' ', bg_attr);
4768 }
4769 }
4770 col = endcol + 1;
4771 off_to = grid->line_offset[row] + col + coloff;
4772 off_from += col;
4773 endcol = (clear_width > 0 ? clear_width : -clear_width);
4774 }
4775
4776 if (bg_attr) {
4777 for (int c = col; c < endcol; c++) {
4778 linebuf_attr[off_from+c] =
4779 hl_combine_attr(bg_attr, linebuf_attr[off_from+c]);
4780 }
4781 }
4782
4783 redraw_next = grid_char_needs_redraw(grid, off_from, off_to, endcol - col);
4784
4785 while (col < endcol) {
4786 char_cells = 1;
4787 if (col + 1 < endcol) {
4788 char_cells = line_off2cells(linebuf_char, off_from, max_off_from);
4789 }
4790 redraw_this = redraw_next;
4791 redraw_next = grid_char_needs_redraw(grid, off_from + char_cells,
4792 off_to + char_cells,
4793 endcol - col - char_cells);
4794
4795 if (redraw_this) {
4796 if (start_dirty == -1) {
4797 start_dirty = col;
4798 }
4799 end_dirty = col + char_cells;
4800 // When writing a single-width character over a double-width
4801 // character and at the end of the redrawn text, need to clear out
4802 // the right halve of the old character.
4803 // Also required when writing the right halve of a double-width
4804 // char over the left halve of an existing one
4805 if (col + char_cells == endcol
4806 && ((char_cells == 1
4807 && grid_off2cells(grid, off_to, max_off_to) > 1)
4808 || (char_cells == 2
4809 && grid_off2cells(grid, off_to, max_off_to) == 1
4810 && grid_off2cells(grid, off_to + 1, max_off_to) > 1))) {
4811 clear_next = true;
4812 }
4813
4814 schar_copy(grid->chars[off_to], linebuf_char[off_from]);
4815 if (char_cells == 2) {
4816 schar_copy(grid->chars[off_to+1], linebuf_char[off_from+1]);
4817 }
4818
4819 grid->attrs[off_to] = linebuf_attr[off_from];
4820 // For simplicity set the attributes of second half of a
4821 // double-wide character equal to the first half.
4822 if (char_cells == 2) {
4823 grid->attrs[off_to + 1] = linebuf_attr[off_from];
4824 }
4825 }
4826
4827 off_to += char_cells;
4828 off_from += char_cells;
4829 col += char_cells;
4830 }
4831
4832 if (clear_next) {
4833 // Clear the second half of a double-wide character of which the left
4834 // half was overwritten with a single-wide character.
4835 schar_from_ascii(grid->chars[off_to], ' ');
4836 end_dirty++;
4837 }
4838
4839 int clear_end = -1;
4840 if (clear_width > 0 && !rlflag) {
4841 // blank out the rest of the line
4842 // TODO(bfredl): we could cache winline widths
4843 while (col < clear_width) {
4844 if (grid->chars[off_to][0] != ' '
4845 || grid->chars[off_to][1] != NUL
4846 || grid->attrs[off_to] != bg_attr) {
4847 grid->chars[off_to][0] = ' ';
4848 grid->chars[off_to][1] = NUL;
4849 grid->attrs[off_to] = bg_attr;
4850 if (start_dirty == -1) {
4851 start_dirty = col;
4852 end_dirty = col;
4853 } else if (clear_end == -1) {
4854 end_dirty = endcol;
4855 }
4856 clear_end = col+1;
4857 }
4858 col++;
4859 off_to++;
4860 }
4861 }
4862
4863 if (clear_width > 0 || wp->w_width != grid->Columns) {
4864 // If we cleared after the end of the line, it did not wrap.
4865 // For vsplit, line wrapping is not possible.
4866 grid->line_wraps[row] = false;
4867 }
4868
4869 if (clear_end < end_dirty) {
4870 clear_end = end_dirty;
4871 }
4872 if (start_dirty == -1) {
4873 start_dirty = end_dirty;
4874 }
4875 if (clear_end > start_dirty) {
4876 ui_line(grid, row, coloff+start_dirty, coloff+end_dirty, coloff+clear_end,
4877 bg_attr, wrap);
4878 }
4879 }
4880
4881 /*
4882 * Mirror text "str" for right-left displaying.
4883 * Only works for single-byte characters (e.g., numbers).
4884 */
rl_mirror(char_u * str)4885 void rl_mirror(char_u *str)
4886 {
4887 char_u *p1, *p2;
4888 int t;
4889
4890 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2) {
4891 t = *p1;
4892 *p1 = *p2;
4893 *p2 = t;
4894 }
4895 }
4896
4897 /*
4898 * mark all status lines for redraw; used after first :cd
4899 */
status_redraw_all(void)4900 void status_redraw_all(void)
4901 {
4902 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
4903 if (wp->w_status_height) {
4904 wp->w_redr_status = true;
4905 redraw_later(wp, VALID);
4906 }
4907 }
4908 }
4909
4910 /// Marks all status lines of the current buffer for redraw.
status_redraw_curbuf(void)4911 void status_redraw_curbuf(void)
4912 {
4913 status_redraw_buf(curbuf);
4914 }
4915
4916 /// Marks all status lines of the specified buffer for redraw.
status_redraw_buf(buf_T * buf)4917 void status_redraw_buf(buf_T *buf)
4918 {
4919 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
4920 if (wp->w_status_height != 0 && wp->w_buffer == buf) {
4921 wp->w_redr_status = true;
4922 redraw_later(wp, VALID);
4923 }
4924 }
4925 }
4926
4927 /*
4928 * Redraw all status lines that need to be redrawn.
4929 */
redraw_statuslines(void)4930 void redraw_statuslines(void)
4931 {
4932 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
4933 if (wp->w_redr_status) {
4934 win_redr_status(wp);
4935 }
4936 }
4937 if (redraw_tabline) {
4938 draw_tabline();
4939 }
4940 }
4941
4942 /*
4943 * Redraw all status lines at the bottom of frame "frp".
4944 */
win_redraw_last_status(const frame_T * frp)4945 void win_redraw_last_status(const frame_T *frp)
4946 FUNC_ATTR_NONNULL_ARG(1)
4947 {
4948 if (frp->fr_layout == FR_LEAF) {
4949 frp->fr_win->w_redr_status = true;
4950 } else if (frp->fr_layout == FR_ROW) {
4951 FOR_ALL_FRAMES(frp, frp->fr_child) {
4952 win_redraw_last_status(frp);
4953 }
4954 } else {
4955 assert(frp->fr_layout == FR_COL);
4956 frp = frp->fr_child;
4957 while (frp->fr_next != NULL) {
4958 frp = frp->fr_next;
4959 }
4960 win_redraw_last_status(frp);
4961 }
4962 }
4963
4964 /*
4965 * Draw the verticap separator right of window "wp" starting with line "row".
4966 */
draw_vsep_win(win_T * wp,int row)4967 static void draw_vsep_win(win_T *wp, int row)
4968 {
4969 int hl;
4970 int c;
4971
4972 if (wp->w_vsep_width) {
4973 // draw the vertical separator right of this window
4974 c = fillchar_vsep(wp, &hl);
4975 grid_fill(&default_grid, wp->w_winrow + row, W_ENDROW(wp),
4976 W_ENDCOL(wp), W_ENDCOL(wp) + 1, c, ' ', hl);
4977 }
4978 }
4979
4980
4981 /*
4982 * Get the length of an item as it will be shown in the status line.
4983 */
status_match_len(expand_T * xp,char_u * s)4984 static int status_match_len(expand_T *xp, char_u *s)
4985 {
4986 int len = 0;
4987
4988 int emenu = (xp->xp_context == EXPAND_MENUS
4989 || xp->xp_context == EXPAND_MENUNAMES);
4990
4991 // Check for menu separators - replace with '|'.
4992 if (emenu && menu_is_separator(s)) {
4993 return 1;
4994 }
4995
4996 while (*s != NUL) {
4997 s += skip_status_match_char(xp, s);
4998 len += ptr2cells(s);
4999 MB_PTR_ADV(s);
5000 }
5001
5002 return len;
5003 }
5004
5005 /*
5006 * Return the number of characters that should be skipped in a status match.
5007 * These are backslashes used for escaping. Do show backslashes in help tags.
5008 */
skip_status_match_char(expand_T * xp,char_u * s)5009 static int skip_status_match_char(expand_T *xp, char_u *s)
5010 {
5011 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5012 || ((xp->xp_context == EXPAND_MENUS
5013 || xp->xp_context == EXPAND_MENUNAMES)
5014 && (s[0] == '\t'
5015 || (s[0] == '\\' && s[1] != NUL)))) {
5016 #ifndef BACKSLASH_IN_FILENAME
5017 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!') {
5018 return 2;
5019 }
5020 #endif
5021 return 1;
5022 }
5023 return 0;
5024 }
5025
5026 /// Show wildchar matches in the status line.
5027 /// Show at least the "match" item.
5028 /// We start at item 'first_match' in the list and show all matches that fit.
5029 ///
5030 /// If inversion is possible we use it. Else '=' characters are used.
5031 ///
5032 /// @param matches list of matches
win_redr_status_matches(expand_T * xp,int num_matches,char_u ** matches,int match,int showtail)5033 void win_redr_status_matches(expand_T *xp, int num_matches, char_u **matches, int match,
5034 int showtail)
5035 {
5036 #define L_MATCH(m) (showtail ? sm_gettail(matches[m], false) : matches[m])
5037 int row;
5038 char_u *buf;
5039 int len;
5040 int clen; // length in screen cells
5041 int fillchar;
5042 int attr;
5043 int i;
5044 bool highlight = true;
5045 char_u *selstart = NULL;
5046 int selstart_col = 0;
5047 char_u *selend = NULL;
5048 static int first_match = 0;
5049 bool add_left = false;
5050 char_u *s;
5051 int emenu;
5052 int l;
5053
5054 if (matches == NULL) { // interrupted completion?
5055 return;
5056 }
5057
5058 buf = xmalloc(Columns * MB_MAXBYTES + 1);
5059
5060 if (match == -1) { // don't show match but original text
5061 match = 0;
5062 highlight = false;
5063 }
5064 // count 1 for the ending ">"
5065 clen = status_match_len(xp, L_MATCH(match)) + 3;
5066 if (match == 0) {
5067 first_match = 0;
5068 } else if (match < first_match) {
5069 // jumping left, as far as we can go
5070 first_match = match;
5071 add_left = true;
5072 } else {
5073 // check if match fits on the screen
5074 for (i = first_match; i < match; ++i) {
5075 clen += status_match_len(xp, L_MATCH(i)) + 2;
5076 }
5077 if (first_match > 0) {
5078 clen += 2;
5079 }
5080 // jumping right, put match at the left
5081 if ((long)clen > Columns) {
5082 first_match = match;
5083 // if showing the last match, we can add some on the left
5084 clen = 2;
5085 for (i = match; i < num_matches; ++i) {
5086 clen += status_match_len(xp, L_MATCH(i)) + 2;
5087 if ((long)clen >= Columns) {
5088 break;
5089 }
5090 }
5091 if (i == num_matches) {
5092 add_left = true;
5093 }
5094 }
5095 }
5096 if (add_left) {
5097 while (first_match > 0) {
5098 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5099 if ((long)clen >= Columns) {
5100 break;
5101 }
5102 first_match--;
5103 }
5104 }
5105
5106 fillchar = fillchar_status(&attr, curwin);
5107
5108 if (first_match == 0) {
5109 *buf = NUL;
5110 len = 0;
5111 } else {
5112 STRCPY(buf, "< ");
5113 len = 2;
5114 }
5115 clen = len;
5116
5117 i = first_match;
5118 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns) {
5119 if (i == match) {
5120 selstart = buf + len;
5121 selstart_col = clen;
5122 }
5123
5124 s = L_MATCH(i);
5125 // Check for menu separators - replace with '|'
5126 emenu = (xp->xp_context == EXPAND_MENUS
5127 || xp->xp_context == EXPAND_MENUNAMES);
5128 if (emenu && menu_is_separator(s)) {
5129 STRCPY(buf + len, transchar('|'));
5130 l = (int)STRLEN(buf + len);
5131 len += l;
5132 clen += l;
5133 } else {
5134 for (; *s != NUL; ++s) {
5135 s += skip_status_match_char(xp, s);
5136 clen += ptr2cells(s);
5137 if ((l = utfc_ptr2len(s)) > 1) {
5138 STRNCPY(buf + len, s, l); // NOLINT(runtime/printf)
5139 s += l - 1;
5140 len += l;
5141 } else {
5142 STRCPY(buf + len, transchar_byte(*s));
5143 len += (int)STRLEN(buf + len);
5144 }
5145 }
5146 }
5147 if (i == match) {
5148 selend = buf + len;
5149 }
5150
5151 *(buf + len++) = ' ';
5152 *(buf + len++) = ' ';
5153 clen += 2;
5154 if (++i == num_matches) {
5155 break;
5156 }
5157 }
5158
5159 if (i != num_matches) {
5160 *(buf + len++) = '>';
5161 ++clen;
5162 }
5163
5164 buf[len] = NUL;
5165
5166 row = cmdline_row - 1;
5167 if (row >= 0) {
5168 if (wild_menu_showing == 0 || wild_menu_showing == WM_LIST) {
5169 if (msg_scrolled > 0) {
5170 // Put the wildmenu just above the command line. If there is
5171 // no room, scroll the screen one line up.
5172 if (cmdline_row == Rows - 1) {
5173 msg_scroll_up(false);
5174 msg_scrolled++;
5175 } else {
5176 cmdline_row++;
5177 row++;
5178 }
5179 wild_menu_showing = WM_SCROLLED;
5180 } else {
5181 // Create status line if needed by setting 'laststatus' to 2.
5182 // Set 'winminheight' to zero to avoid that the window is
5183 // resized.
5184 if (lastwin->w_status_height == 0) {
5185 save_p_ls = p_ls;
5186 save_p_wmh = p_wmh;
5187 p_ls = 2;
5188 p_wmh = 0;
5189 last_status(false);
5190 }
5191 wild_menu_showing = WM_SHOWN;
5192 }
5193 }
5194
5195 // Tricky: wildmenu can be drawn either over a status line, or at empty
5196 // scrolled space in the message output
5197 ScreenGrid *grid = (wild_menu_showing == WM_SCROLLED)
5198 ? &msg_grid_adj : &default_grid;
5199
5200 grid_puts(grid, buf, row, 0, attr);
5201 if (selstart != NULL && highlight) {
5202 *selend = NUL;
5203 grid_puts(grid, selstart, row, selstart_col, HL_ATTR(HLF_WM));
5204 }
5205
5206 grid_fill(grid, row, row + 1, clen, Columns,
5207 fillchar, fillchar, attr);
5208 }
5209
5210 win_redraw_last_status(topframe);
5211 xfree(buf);
5212 }
5213
5214 /// Redraw the status line of window `wp`.
5215 ///
5216 /// If inversion is possible we use it. Else '=' characters are used.
win_redr_status(win_T * wp)5217 static void win_redr_status(win_T *wp)
5218 {
5219 int row;
5220 char_u *p;
5221 int len;
5222 int fillchar;
5223 int attr;
5224 int this_ru_col;
5225 static int busy = FALSE;
5226
5227 // May get here recursively when 'statusline' (indirectly)
5228 // invokes ":redrawstatus". Simply ignore the call then.
5229 if (busy
5230 // Also ignore if wildmenu is showing.
5231 || (wild_menu_showing != 0 && !ui_has(kUIWildmenu))) {
5232 return;
5233 }
5234 busy = true;
5235
5236 wp->w_redr_status = FALSE;
5237 if (wp->w_status_height == 0) {
5238 // no status line, can only be last window
5239 redraw_cmdline = true;
5240 } else if (!redrawing()) {
5241 // Don't redraw right now, do it later. Don't update status line when
5242 // popup menu is visible and may be drawn over it
5243 wp->w_redr_status = true;
5244 } else if (*p_stl != NUL || *wp->w_p_stl != NUL) {
5245 // redraw custom status line
5246 redraw_custom_statusline(wp);
5247 } else {
5248 fillchar = fillchar_status(&attr, wp);
5249
5250 get_trans_bufname(wp->w_buffer);
5251 p = NameBuff;
5252 len = (int)STRLEN(p);
5253
5254 if (bt_help(wp->w_buffer)
5255 || wp->w_p_pvw
5256 || bufIsChanged(wp->w_buffer)
5257 || wp->w_buffer->b_p_ro) {
5258 *(p + len++) = ' ';
5259 }
5260 if (bt_help(wp->w_buffer)) {
5261 STRCPY(p + len, _("[Help]"));
5262 len += (int)STRLEN(p + len);
5263 }
5264 if (wp->w_p_pvw) {
5265 STRCPY(p + len, _("[Preview]"));
5266 len += (int)STRLEN(p + len);
5267 }
5268 if (bufIsChanged(wp->w_buffer)) {
5269 STRCPY(p + len, "[+]");
5270 len += 3;
5271 }
5272 if (wp->w_buffer->b_p_ro) {
5273 STRCPY(p + len, _("[RO]"));
5274 // len += (int)STRLEN(p + len); // dead assignment
5275 }
5276
5277 this_ru_col = ru_col - (Columns - wp->w_width);
5278 if (this_ru_col < (wp->w_width + 1) / 2) {
5279 this_ru_col = (wp->w_width + 1) / 2;
5280 }
5281 if (this_ru_col <= 1) {
5282 p = (char_u *)"<"; // No room for file name!
5283 len = 1;
5284 } else {
5285 int clen = 0, i;
5286
5287 // Count total number of display cells.
5288 clen = (int)mb_string2cells(p);
5289
5290 // Find first character that will fit.
5291 // Going from start to end is much faster for DBCS.
5292 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5293 i += utfc_ptr2len(p + i)) {
5294 clen -= utf_ptr2cells(p + i);
5295 }
5296 len = clen;
5297 if (i > 0) {
5298 p = p + i - 1;
5299 *p = '<';
5300 ++len;
5301 }
5302 }
5303
5304 row = W_ENDROW(wp);
5305 grid_puts(&default_grid, p, row, wp->w_wincol, attr);
5306 grid_fill(&default_grid, row, row + 1, len + wp->w_wincol,
5307 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
5308
5309 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
5310 && this_ru_col - len > (int)(STRLEN(NameBuff) + 1)) {
5311 grid_puts(&default_grid, NameBuff, row,
5312 (int)(this_ru_col - STRLEN(NameBuff) - 1), attr);
5313 }
5314
5315 win_redr_ruler(wp, true);
5316 }
5317
5318 /*
5319 * May need to draw the character below the vertical separator.
5320 */
5321 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing()) {
5322 if (stl_connected(wp)) {
5323 fillchar = fillchar_status(&attr, wp);
5324 } else {
5325 fillchar = fillchar_vsep(wp, &attr);
5326 }
5327 grid_putchar(&default_grid, fillchar, W_ENDROW(wp), W_ENDCOL(wp), attr);
5328 }
5329 busy = FALSE;
5330 }
5331
5332 /*
5333 * Redraw the status line according to 'statusline' and take care of any
5334 * errors encountered.
5335 */
redraw_custom_statusline(win_T * wp)5336 static void redraw_custom_statusline(win_T *wp)
5337 {
5338 static bool entered = false;
5339 int saved_did_emsg = did_emsg;
5340
5341 /* When called recursively return. This can happen when the statusline
5342 * contains an expression that triggers a redraw. */
5343 if (entered) {
5344 return;
5345 }
5346 entered = true;
5347
5348 did_emsg = false;
5349 win_redr_custom(wp, false);
5350 if (did_emsg) {
5351 // When there is an error disable the statusline, otherwise the
5352 // display is messed up with errors and a redraw triggers the problem
5353 // again and again.
5354 set_string_option_direct("statusline", -1, (char_u *)"",
5355 OPT_FREE | (*wp->w_p_stl != NUL
5356 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
5357 }
5358 did_emsg |= saved_did_emsg;
5359 entered = false;
5360 }
5361
5362 /// Only call if (wp->w_vsep_width != 0).
5363 ///
5364 /// @return true if the status line of window "wp" is connected to the status
5365 /// line of the window right of it. If not, then it's a vertical separator.
stl_connected(win_T * wp)5366 bool stl_connected(win_T *wp)
5367 {
5368 frame_T *fr;
5369
5370 fr = wp->w_frame;
5371 while (fr->fr_parent != NULL) {
5372 if (fr->fr_parent->fr_layout == FR_COL) {
5373 if (fr->fr_next != NULL) {
5374 break;
5375 }
5376 } else {
5377 if (fr->fr_next != NULL) {
5378 return true;
5379 }
5380 }
5381 fr = fr->fr_parent;
5382 }
5383 return false;
5384 }
5385
5386
5387 /// Get the value to show for the language mappings, active 'keymap'.
5388 ///
5389 /// @param fmt format string containing one %s item
5390 /// @param buf buffer for the result
5391 /// @param len length of buffer
get_keymap_str(win_T * wp,char_u * fmt,char_u * buf,int len)5392 bool get_keymap_str(win_T *wp, char_u *fmt, char_u *buf, int len)
5393 {
5394 char_u *p;
5395
5396 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP) {
5397 return false;
5398 }
5399
5400 {
5401 buf_T *old_curbuf = curbuf;
5402 win_T *old_curwin = curwin;
5403 char_u *s;
5404
5405 curbuf = wp->w_buffer;
5406 curwin = wp;
5407 STRCPY(buf, "b:keymap_name"); // must be writable
5408 emsg_skip++;
5409 s = p = eval_to_string(buf, NULL, false);
5410 emsg_skip--;
5411 curbuf = old_curbuf;
5412 curwin = old_curwin;
5413 if (p == NULL || *p == NUL) {
5414 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED) {
5415 p = wp->w_buffer->b_p_keymap;
5416 } else {
5417 p = (char_u *)"lang";
5418 }
5419 }
5420 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1) {
5421 buf[0] = NUL;
5422 }
5423 xfree(s);
5424 }
5425 return buf[0] != NUL;
5426 }
5427
5428 /*
5429 * Redraw the status line or ruler of window "wp".
5430 * When "wp" is NULL redraw the tab pages line from 'tabline'.
5431 */
win_redr_custom(win_T * wp,bool draw_ruler)5432 static void win_redr_custom(win_T *wp, bool draw_ruler)
5433 {
5434 static bool entered = false;
5435 int attr;
5436 int curattr;
5437 int row;
5438 int col = 0;
5439 int maxwidth;
5440 int width;
5441 int n;
5442 int len;
5443 int fillchar;
5444 char_u buf[MAXPATHL];
5445 char_u *stl;
5446 char_u *p;
5447 stl_hlrec_t *hltab;
5448 StlClickRecord *tabtab;
5449 int use_sandbox = false;
5450 win_T *ewp;
5451 int p_crb_save;
5452
5453 ScreenGrid *grid = &default_grid;
5454
5455 /* There is a tiny chance that this gets called recursively: When
5456 * redrawing a status line triggers redrawing the ruler or tabline.
5457 * Avoid trouble by not allowing recursion. */
5458 if (entered) {
5459 return;
5460 }
5461 entered = true;
5462
5463 // setup environment for the task at hand
5464 if (wp == NULL) {
5465 // Use 'tabline'. Always at the first line of the screen.
5466 stl = p_tal;
5467 row = 0;
5468 fillchar = ' ';
5469 attr = HL_ATTR(HLF_TPF);
5470 maxwidth = Columns;
5471 use_sandbox = was_set_insecurely(wp, "tabline", 0);
5472 } else {
5473 row = W_ENDROW(wp);
5474 fillchar = fillchar_status(&attr, wp);
5475 maxwidth = wp->w_width;
5476
5477 if (draw_ruler) {
5478 stl = p_ruf;
5479 // advance past any leading group spec - implicit in ru_col
5480 if (*stl == '%') {
5481 if (*++stl == '-') {
5482 stl++;
5483 }
5484 if (atoi((char *)stl)) {
5485 while (ascii_isdigit(*stl)) {
5486 stl++;
5487 }
5488 }
5489 if (*stl++ != '(') {
5490 stl = p_ruf;
5491 }
5492 }
5493 col = ru_col - (Columns - wp->w_width);
5494 if (col < (wp->w_width + 1) / 2) {
5495 col = (wp->w_width + 1) / 2;
5496 }
5497 maxwidth = wp->w_width - col;
5498 if (!wp->w_status_height) {
5499 grid = &msg_grid_adj;
5500 row = Rows - 1;
5501 maxwidth--; // writing in last column may cause scrolling
5502 fillchar = ' ';
5503 attr = HL_ATTR(HLF_MSG);
5504 }
5505
5506 use_sandbox = was_set_insecurely(wp, "rulerformat", 0);
5507 } else {
5508 if (*wp->w_p_stl != NUL) {
5509 stl = wp->w_p_stl;
5510 } else {
5511 stl = p_stl;
5512 }
5513 use_sandbox = was_set_insecurely(wp, "statusline", *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
5514 }
5515
5516 col += wp->w_wincol;
5517 }
5518
5519 if (maxwidth <= 0) {
5520 goto theend;
5521 }
5522
5523 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
5524 * the cursor away and back. */
5525 ewp = wp == NULL ? curwin : wp;
5526 p_crb_save = ewp->w_p_crb;
5527 ewp->w_p_crb = FALSE;
5528
5529 /* Make a copy, because the statusline may include a function call that
5530 * might change the option value and free the memory. */
5531 stl = vim_strsave(stl);
5532 width =
5533 build_stl_str_hl(ewp, buf, sizeof(buf), stl, use_sandbox,
5534 fillchar, maxwidth, &hltab, &tabtab);
5535 xfree(stl);
5536 ewp->w_p_crb = p_crb_save;
5537
5538 // Make all characters printable.
5539 p = (char_u *)transstr((const char *)buf, true);
5540 len = STRLCPY(buf, p, sizeof(buf));
5541 len = (size_t)len < sizeof(buf) ? len : (int)sizeof(buf) - 1;
5542 xfree(p);
5543
5544 // fill up with "fillchar"
5545 while (width < maxwidth && len < (int)sizeof(buf) - 1) {
5546 len += utf_char2bytes(fillchar, buf + len);
5547 width++;
5548 }
5549 buf[len] = NUL;
5550
5551 /*
5552 * Draw each snippet with the specified highlighting.
5553 */
5554 grid_puts_line_start(grid, row);
5555
5556 curattr = attr;
5557 p = buf;
5558 for (n = 0; hltab[n].start != NULL; n++) {
5559 int textlen = (int)(hltab[n].start - p);
5560 grid_puts_len(grid, p, textlen, row, col, curattr);
5561 col += vim_strnsize(p, textlen);
5562 p = hltab[n].start;
5563
5564 if (hltab[n].userhl == 0) {
5565 curattr = attr;
5566 } else if (hltab[n].userhl < 0) {
5567 curattr = syn_id2attr(-hltab[n].userhl);
5568 } else if (wp != NULL && wp != curwin && wp->w_status_height != 0) {
5569 curattr = highlight_stlnc[hltab[n].userhl - 1];
5570 } else {
5571 curattr = highlight_user[hltab[n].userhl - 1];
5572 }
5573 }
5574 // Make sure to use an empty string instead of p, if p is beyond buf + len.
5575 grid_puts(grid, p >= buf + len ? (char_u *)"" : p, row, col,
5576 curattr);
5577
5578 grid_puts_line_flush(false);
5579
5580 if (wp == NULL) {
5581 // Fill the tab_page_click_defs array for clicking in the tab pages line.
5582 col = 0;
5583 len = 0;
5584 p = buf;
5585 StlClickDefinition cur_click_def = {
5586 .type = kStlClickDisabled,
5587 };
5588 for (n = 0; tabtab[n].start != NULL; n++) {
5589 len += vim_strnsize(p, (int)(tabtab[n].start - (char *)p));
5590 while (col < len) {
5591 tab_page_click_defs[col++] = cur_click_def;
5592 }
5593 p = (char_u *)tabtab[n].start;
5594 cur_click_def = tabtab[n].def;
5595 }
5596 while (col < Columns) {
5597 tab_page_click_defs[col++] = cur_click_def;
5598 }
5599 }
5600
5601 theend:
5602 entered = false;
5603 }
5604
win_redr_border(win_T * wp)5605 static void win_redr_border(win_T *wp)
5606 {
5607 wp->w_redr_border = false;
5608 if (!(wp->w_floating && wp->w_float_config.border)) {
5609 return;
5610 }
5611
5612 ScreenGrid *grid = &wp->w_grid_alloc;
5613
5614 schar_T *chars = wp->w_float_config.border_chars;
5615 int *attrs = wp->w_float_config.border_attr;
5616
5617
5618 int *adj = wp->w_border_adj;
5619 int irow = wp->w_height_inner, icol = wp->w_width_inner;
5620
5621 if (adj[0]) {
5622 grid_puts_line_start(grid, 0);
5623 if (adj[3]) {
5624 grid_put_schar(grid, 0, 0, chars[0], attrs[0]);
5625 }
5626 for (int i = 0; i < icol; i++) {
5627 grid_put_schar(grid, 0, i+adj[3], chars[1], attrs[1]);
5628 }
5629 if (adj[1]) {
5630 grid_put_schar(grid, 0, icol+adj[3], chars[2], attrs[2]);
5631 }
5632 grid_puts_line_flush(false);
5633 }
5634
5635 for (int i = 0; i < irow; i++) {
5636 if (adj[3]) {
5637 grid_puts_line_start(grid, i+adj[0]);
5638 grid_put_schar(grid, i+adj[0], 0, chars[7], attrs[7]);
5639 grid_puts_line_flush(false);
5640 }
5641 if (adj[1]) {
5642 int ic = (i == 0 && !adj[0] && chars[2][0]) ? 2 : 3;
5643 grid_puts_line_start(grid, i+adj[0]);
5644 grid_put_schar(grid, i+adj[0], icol+adj[3], chars[ic], attrs[ic]);
5645 grid_puts_line_flush(false);
5646 }
5647 }
5648
5649 if (adj[2]) {
5650 grid_puts_line_start(grid, irow+adj[0]);
5651 if (adj[3]) {
5652 grid_put_schar(grid, irow+adj[0], 0, chars[6], attrs[6]);
5653 }
5654 for (int i = 0; i < icol; i++) {
5655 int ic = (i == 0 && !adj[3] && chars[6][0]) ? 6 : 5;
5656 grid_put_schar(grid, irow+adj[0], i+adj[3], chars[ic], attrs[ic]);
5657 }
5658 if (adj[1]) {
5659 grid_put_schar(grid, irow+adj[0], icol+adj[3], chars[4], attrs[4]);
5660 }
5661 grid_puts_line_flush(false);
5662 }
5663 }
5664
5665 // Low-level functions to manipulate individual character cells on the
5666 // screen grid.
5667
5668 /// Put a ASCII character in a screen cell.
schar_from_ascii(char_u * p,const char c)5669 static void schar_from_ascii(char_u *p, const char c)
5670 {
5671 p[0] = c;
5672 p[1] = 0;
5673 }
5674
5675 /// Put a unicode character in a screen cell.
schar_from_char(char_u * p,int c)5676 static int schar_from_char(char_u *p, int c)
5677 {
5678 int len = utf_char2bytes(c, p);
5679 p[len] = NUL;
5680 return len;
5681 }
5682
5683 /// Put a unicode char, and up to MAX_MCO composing chars, in a screen cell.
schar_from_cc(char_u * p,int c,int u8cc[MAX_MCO])5684 static int schar_from_cc(char_u *p, int c, int u8cc[MAX_MCO])
5685 {
5686 int len = utf_char2bytes(c, p);
5687 for (int i = 0; i < MAX_MCO; i++) {
5688 if (u8cc[i] == 0) {
5689 break;
5690 }
5691 len += utf_char2bytes(u8cc[i], p + len);
5692 }
5693 p[len] = 0;
5694 return len;
5695 }
5696
5697 /// compare the contents of two screen cells.
schar_cmp(char_u * sc1,char_u * sc2)5698 static int schar_cmp(char_u *sc1, char_u *sc2)
5699 {
5700 return STRNCMP(sc1, sc2, sizeof(schar_T));
5701 }
5702
5703 /// copy the contents of screen cell `sc2` into cell `sc1`
schar_copy(char_u * sc1,char_u * sc2)5704 static void schar_copy(char_u *sc1, char_u *sc2)
5705 {
5706 STRLCPY(sc1, sc2, sizeof(schar_T));
5707 }
5708
line_off2cells(schar_T * line,size_t off,size_t max_off)5709 static int line_off2cells(schar_T *line, size_t off, size_t max_off)
5710 {
5711 return (off + 1 < max_off && line[off + 1][0] == 0) ? 2 : 1;
5712 }
5713
5714 /// Return number of display cells for char at grid->chars[off].
5715 /// We make sure that the offset used is less than "max_off".
grid_off2cells(ScreenGrid * grid,size_t off,size_t max_off)5716 static int grid_off2cells(ScreenGrid *grid, size_t off, size_t max_off)
5717 {
5718 return line_off2cells(grid->chars, off, max_off);
5719 }
5720
5721 /// Return true if the character at "row"/"col" on the screen is the left side
5722 /// of a double-width character.
5723 ///
5724 /// Caller must make sure "row" and "col" are not invalid!
grid_lefthalve(ScreenGrid * grid,int row,int col)5725 bool grid_lefthalve(ScreenGrid *grid, int row, int col)
5726 {
5727 screen_adjust_grid(&grid, &row, &col);
5728
5729 return grid_off2cells(grid, grid->line_offset[row] + col,
5730 grid->line_offset[row] + grid->Columns) > 1;
5731 }
5732
5733 /// Correct a position on the screen, if it's the right half of a double-wide
5734 /// char move it to the left half. Returns the corrected column.
grid_fix_col(ScreenGrid * grid,int col,int row)5735 int grid_fix_col(ScreenGrid *grid, int col, int row)
5736 {
5737 int coloff = 0;
5738 screen_adjust_grid(&grid, &row, &coloff);
5739
5740 col += coloff;
5741 if (grid->chars != NULL && col > 0
5742 && grid->chars[grid->line_offset[row] + col][0] == 0) {
5743 return col - 1 - coloff;
5744 }
5745 return col - coloff;
5746 }
5747
5748 /// output a single character directly to the grid
grid_putchar(ScreenGrid * grid,int c,int row,int col,int attr)5749 void grid_putchar(ScreenGrid *grid, int c, int row, int col, int attr)
5750 {
5751 char_u buf[MB_MAXBYTES + 1];
5752
5753 buf[utf_char2bytes(c, buf)] = NUL;
5754 grid_puts(grid, buf, row, col, attr);
5755 }
5756
5757 /// get a single character directly from grid.chars into "bytes[]".
5758 /// Also return its attribute in *attrp;
grid_getbytes(ScreenGrid * grid,int row,int col,char_u * bytes,int * attrp)5759 void grid_getbytes(ScreenGrid *grid, int row, int col, char_u *bytes, int *attrp)
5760 {
5761 unsigned off;
5762
5763 screen_adjust_grid(&grid, &row, &col);
5764
5765 // safety check
5766 if (grid->chars != NULL && row < grid->Rows && col < grid->Columns) {
5767 off = grid->line_offset[row] + col;
5768 *attrp = grid->attrs[off];
5769 schar_copy(bytes, grid->chars[off]);
5770 }
5771 }
5772
5773
5774 /// put string '*text' on the window grid at position 'row' and 'col', with
5775 /// attributes 'attr', and update chars[] and attrs[].
5776 /// Note: only outputs within one row, message is truncated at grid boundary!
5777 /// Note: if grid, row and/or col is invalid, nothing is done.
grid_puts(ScreenGrid * grid,char_u * text,int row,int col,int attr)5778 void grid_puts(ScreenGrid *grid, char_u *text, int row, int col, int attr)
5779 {
5780 grid_puts_len(grid, text, -1, row, col, attr);
5781 }
5782
5783 static ScreenGrid *put_dirty_grid = NULL;
5784 static int put_dirty_row = -1;
5785 static int put_dirty_first = INT_MAX;
5786 static int put_dirty_last = 0;
5787
5788 /// Start a group of grid_puts_len calls that builds a single grid line.
5789 ///
5790 /// Must be matched with a grid_puts_line_flush call before moving to
5791 /// another line.
grid_puts_line_start(ScreenGrid * grid,int row)5792 void grid_puts_line_start(ScreenGrid *grid, int row)
5793 {
5794 int col = 0; // unused
5795 screen_adjust_grid(&grid, &row, &col);
5796 assert(put_dirty_row == -1);
5797 put_dirty_row = row;
5798 put_dirty_grid = grid;
5799 }
5800
grid_put_schar(ScreenGrid * grid,int row,int col,char_u * schar,int attr)5801 void grid_put_schar(ScreenGrid *grid, int row, int col, char_u *schar, int attr)
5802 {
5803 assert(put_dirty_row == row);
5804 unsigned int off = grid->line_offset[row] + col;
5805 if (grid->attrs[off] != attr || schar_cmp(grid->chars[off], schar)) {
5806 schar_copy(grid->chars[off], schar);
5807 grid->attrs[off] = attr;
5808
5809 put_dirty_first = MIN(put_dirty_first, col);
5810 // TODO(bfredl): Y U NO DOUBLEWIDTH?
5811 put_dirty_last = MAX(put_dirty_last, col+1);
5812 }
5813 }
5814
5815 /// like grid_puts(), but output "text[len]". When "len" is -1 output up to
5816 /// a NUL.
grid_puts_len(ScreenGrid * grid,char_u * text,int textlen,int row,int col,int attr)5817 void grid_puts_len(ScreenGrid *grid, char_u *text, int textlen, int row, int col, int attr)
5818 {
5819 unsigned off;
5820 char_u *ptr = text;
5821 int len = textlen;
5822 int c;
5823 unsigned max_off;
5824 int mbyte_blen = 1;
5825 int mbyte_cells = 1;
5826 int u8c = 0;
5827 int u8cc[MAX_MCO];
5828 int clear_next_cell = FALSE;
5829 int prev_c = 0; // previous Arabic character
5830 int pc, nc, nc1;
5831 int pcc[MAX_MCO];
5832 int need_redraw;
5833 bool do_flush = false;
5834
5835 screen_adjust_grid(&grid, &row, &col);
5836
5837 // Safety check. The check for negative row and column is to fix issue
5838 // vim/vim#4102. TODO(neovim): find out why row/col could be negative.
5839 if (grid->chars == NULL
5840 || row >= grid->Rows || row < 0
5841 || col >= grid->Columns || col < 0) {
5842 return;
5843 }
5844
5845 if (put_dirty_row == -1) {
5846 grid_puts_line_start(grid, row);
5847 do_flush = true;
5848 } else {
5849 if (grid != put_dirty_grid || row != put_dirty_row) {
5850 abort();
5851 }
5852 }
5853 off = grid->line_offset[row] + col;
5854
5855 /* When drawing over the right halve of a double-wide char clear out the
5856 * left halve. Only needed in a terminal. */
5857 if (grid != &default_grid && col == 0 && grid_invalid_row(grid, row)) {
5858 // redraw the previous cell, make it empty
5859 put_dirty_first = -1;
5860 put_dirty_last = MAX(put_dirty_last, 1);
5861 }
5862
5863 max_off = grid->line_offset[row] + grid->Columns;
5864 while (col < grid->Columns
5865 && (len < 0 || (int)(ptr - text) < len)
5866 && *ptr != NUL) {
5867 c = *ptr;
5868 // check if this is the first byte of a multibyte
5869 if (len > 0) {
5870 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
5871 } else {
5872 mbyte_blen = utfc_ptr2len(ptr);
5873 }
5874 if (len >= 0) {
5875 u8c = utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr));
5876 } else {
5877 u8c = utfc_ptr2char(ptr, u8cc);
5878 }
5879 mbyte_cells = utf_char2cells(u8c);
5880 if (p_arshape && !p_tbidi && arabic_char(u8c)) {
5881 // Do Arabic shaping.
5882 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len) {
5883 // Past end of string to be displayed.
5884 nc = NUL;
5885 nc1 = NUL;
5886 } else {
5887 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
5888 (int)((text + len) - ptr - mbyte_blen));
5889 nc1 = pcc[0];
5890 }
5891 pc = prev_c;
5892 prev_c = u8c;
5893 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
5894 } else {
5895 prev_c = u8c;
5896 }
5897 if (col + mbyte_cells > grid->Columns) {
5898 // Only 1 cell left, but character requires 2 cells:
5899 // display a '>' in the last column to avoid wrapping. */
5900 c = '>';
5901 mbyte_cells = 1;
5902 }
5903
5904 schar_T buf;
5905 schar_from_cc(buf, u8c, u8cc);
5906
5907
5908 need_redraw = schar_cmp(grid->chars[off], buf)
5909 || (mbyte_cells == 2 && grid->chars[off + 1][0] != 0)
5910 || grid->attrs[off] != attr
5911 || exmode_active;
5912
5913 if (need_redraw) {
5914 // When at the end of the text and overwriting a two-cell
5915 // character with a one-cell character, need to clear the next
5916 // cell. Also when overwriting the left halve of a two-cell char
5917 // with the right halve of a two-cell char. Do this only once
5918 // (utf8_off2cells() may return 2 on the right halve).
5919 if (clear_next_cell) {
5920 clear_next_cell = false;
5921 } else if ((len < 0 ? ptr[mbyte_blen] == NUL
5922 : ptr + mbyte_blen >= text + len)
5923 && ((mbyte_cells == 1
5924 && grid_off2cells(grid, off, max_off) > 1)
5925 || (mbyte_cells == 2
5926 && grid_off2cells(grid, off, max_off) == 1
5927 && grid_off2cells(grid, off + 1, max_off) > 1))) {
5928 clear_next_cell = true;
5929 }
5930
5931 schar_copy(grid->chars[off], buf);
5932 grid->attrs[off] = attr;
5933 if (mbyte_cells == 2) {
5934 grid->chars[off + 1][0] = 0;
5935 grid->attrs[off + 1] = attr;
5936 }
5937 put_dirty_first = MIN(put_dirty_first, col);
5938 put_dirty_last = MAX(put_dirty_last, col+mbyte_cells);
5939 }
5940
5941 off += mbyte_cells;
5942 col += mbyte_cells;
5943 ptr += mbyte_blen;
5944 if (clear_next_cell) {
5945 // This only happens at the end, display one space next.
5946 ptr = (char_u *)" ";
5947 len = -1;
5948 }
5949 }
5950
5951 if (do_flush) {
5952 grid_puts_line_flush(true);
5953 }
5954 }
5955
5956 /// End a group of grid_puts_len calls and send the screen buffer to the UI
5957 /// layer.
5958 ///
5959 /// @param set_cursor Move the visible cursor to the end of the changed region.
5960 /// This is a workaround for not yet refactored code paths
5961 /// and shouldn't be used in new code.
grid_puts_line_flush(bool set_cursor)5962 void grid_puts_line_flush(bool set_cursor)
5963 {
5964 assert(put_dirty_row != -1);
5965 if (put_dirty_first < put_dirty_last) {
5966 if (set_cursor) {
5967 ui_grid_cursor_goto(put_dirty_grid->handle, put_dirty_row,
5968 MIN(put_dirty_last, put_dirty_grid->Columns-1));
5969 }
5970 if (!put_dirty_grid->throttled) {
5971 ui_line(put_dirty_grid, put_dirty_row, put_dirty_first, put_dirty_last,
5972 put_dirty_last, 0, false);
5973 } else if (put_dirty_grid->dirty_col) {
5974 if (put_dirty_last > put_dirty_grid->dirty_col[put_dirty_row]) {
5975 put_dirty_grid->dirty_col[put_dirty_row] = put_dirty_last;
5976 }
5977 }
5978 put_dirty_first = INT_MAX;
5979 put_dirty_last = 0;
5980 }
5981 put_dirty_row = -1;
5982 put_dirty_grid = NULL;
5983 }
5984
5985 /*
5986 * Prepare for 'hlsearch' highlighting.
5987 */
start_search_hl(void)5988 static void start_search_hl(void)
5989 {
5990 if (p_hls && !no_hlsearch) {
5991 end_search_hl(); // just in case it wasn't called before
5992 last_pat_prog(&search_hl.rm);
5993 // Set the time limit to 'redrawtime'.
5994 search_hl.tm = profile_setlimit(p_rdt);
5995 }
5996 }
5997
5998 /*
5999 * Clean up for 'hlsearch' highlighting.
6000 */
end_search_hl(void)6001 static void end_search_hl(void)
6002 {
6003 if (search_hl.rm.regprog != NULL) {
6004 vim_regfree(search_hl.rm.regprog);
6005 search_hl.rm.regprog = NULL;
6006 }
6007 }
6008
6009
6010 /*
6011 * Init for calling prepare_search_hl().
6012 */
init_search_hl(win_T * wp)6013 static void init_search_hl(win_T *wp)
6014 FUNC_ATTR_NONNULL_ALL
6015 {
6016 // Setup for match and 'hlsearch' highlighting. Disable any previous
6017 // match
6018 matchitem_T *cur = wp->w_match_head;
6019 while (cur != NULL) {
6020 cur->hl.rm = cur->match;
6021 if (cur->hlg_id == 0) {
6022 cur->hl.attr = 0;
6023 } else {
6024 cur->hl.attr = syn_id2attr(cur->hlg_id);
6025 }
6026 cur->hl.buf = wp->w_buffer;
6027 cur->hl.lnum = 0;
6028 cur->hl.first_lnum = 0;
6029 // Set the time limit to 'redrawtime'.
6030 cur->hl.tm = profile_setlimit(p_rdt);
6031 cur = cur->next;
6032 }
6033 search_hl.buf = wp->w_buffer;
6034 search_hl.lnum = 0;
6035 search_hl.first_lnum = 0;
6036 search_hl.attr = win_hl_attr(wp, HLF_L);
6037
6038 // time limit is set at the toplevel, for all windows
6039 }
6040
6041 /*
6042 * Advance to the match in window "wp" line "lnum" or past it.
6043 */
prepare_search_hl(win_T * wp,linenr_T lnum)6044 static void prepare_search_hl(win_T *wp, linenr_T lnum)
6045 FUNC_ATTR_NONNULL_ALL
6046 {
6047 matchitem_T *cur; // points to the match list
6048 match_T *shl; // points to search_hl or a match
6049 bool shl_flag; // flag to indicate whether search_hl
6050 // has been processed or not
6051
6052 // When using a multi-line pattern, start searching at the top
6053 // of the window or just after a closed fold.
6054 // Do this both for search_hl and the match list.
6055 cur = wp->w_match_head;
6056 shl_flag = false;
6057 while (cur != NULL || shl_flag == false) {
6058 if (shl_flag == false) {
6059 shl = &search_hl;
6060 shl_flag = true;
6061 } else {
6062 shl = &cur->hl; // -V595
6063 }
6064 if (shl->rm.regprog != NULL
6065 && shl->lnum == 0
6066 && re_multiline(shl->rm.regprog)) {
6067 if (shl->first_lnum == 0) {
6068 for (shl->first_lnum = lnum;
6069 shl->first_lnum > wp->w_topline;
6070 shl->first_lnum--) {
6071 if (hasFoldingWin(wp, shl->first_lnum - 1, NULL, NULL, true, NULL)) {
6072 break;
6073 }
6074 }
6075 }
6076 if (cur != NULL) {
6077 cur->pos.cur = 0;
6078 }
6079 bool pos_inprogress = true; // mark that a position match search is
6080 // in progress
6081 int n = 0;
6082 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
6083 || (cur != NULL && pos_inprogress))) {
6084 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
6085 shl == &search_hl ? NULL : cur);
6086 pos_inprogress = !(cur == NULL || cur->pos.cur == 0);
6087 if (shl->lnum != 0) {
6088 shl->first_lnum = shl->lnum
6089 + shl->rm.endpos[0].lnum
6090 - shl->rm.startpos[0].lnum;
6091 n = shl->rm.endpos[0].col;
6092 } else {
6093 ++shl->first_lnum;
6094 n = 0;
6095 }
6096 }
6097 }
6098 if (shl != &search_hl && cur != NULL) {
6099 cur = cur->next;
6100 }
6101 }
6102 }
6103
6104 /// Search for a next 'hlsearch' or match.
6105 /// Uses shl->buf.
6106 /// Sets shl->lnum and shl->rm contents.
6107 /// Note: Assumes a previous match is always before "lnum", unless
6108 /// shl->lnum is zero.
6109 /// Careful: Any pointers for buffer lines will become invalid.
6110 ///
6111 /// @param shl points to search_hl or a match
6112 /// @param mincol minimal column for a match
6113 /// @param cur to retrieve match positions if any
next_search_hl(win_T * win,match_T * shl,linenr_T lnum,colnr_T mincol,matchitem_T * cur)6114 static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol,
6115 matchitem_T *cur)
6116 FUNC_ATTR_NONNULL_ARG(2)
6117 {
6118 linenr_T l;
6119 colnr_T matchcol;
6120 long nmatched = 0;
6121 int save_called_emsg = called_emsg;
6122
6123 // for :{range}s/pat only highlight inside the range
6124 if (lnum < search_first_line || lnum > search_last_line) {
6125 shl->lnum = 0;
6126 return;
6127 }
6128
6129 if (shl->lnum != 0) {
6130 // Check for three situations:
6131 // 1. If the "lnum" is below a previous match, start a new search.
6132 // 2. If the previous match includes "mincol", use it.
6133 // 3. Continue after the previous match.
6134 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6135 if (lnum > l) {
6136 shl->lnum = 0;
6137 } else if (lnum < l || shl->rm.endpos[0].col > mincol) {
6138 return;
6139 }
6140 }
6141
6142 /*
6143 * Repeat searching for a match until one is found that includes "mincol"
6144 * or none is found in this line.
6145 */
6146 called_emsg = FALSE;
6147 for (;;) {
6148 // Stop searching after passing the time limit.
6149 if (profile_passed_limit(shl->tm)) {
6150 shl->lnum = 0; // no match found in time
6151 break;
6152 }
6153 // Three situations:
6154 // 1. No useful previous match: search from start of line.
6155 // 2. Not Vi compatible or empty match: continue at next character.
6156 // Break the loop if this is beyond the end of the line.
6157 // 3. Vi compatible searching: continue at end of previous match.
6158 if (shl->lnum == 0) {
6159 matchcol = 0;
6160 } else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6161 || (shl->rm.endpos[0].lnum == 0
6162 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col)) {
6163 char_u *ml;
6164
6165 matchcol = shl->rm.startpos[0].col;
6166 ml = ml_get_buf(shl->buf, lnum, false) + matchcol;
6167 if (*ml == NUL) {
6168 ++matchcol;
6169 shl->lnum = 0;
6170 break;
6171 }
6172 matchcol += utfc_ptr2len(ml);
6173 } else {
6174 matchcol = shl->rm.endpos[0].col;
6175 }
6176
6177 shl->lnum = lnum;
6178 if (shl->rm.regprog != NULL) {
6179 // Remember whether shl->rm is using a copy of the regprog in
6180 // cur->match.
6181 bool regprog_is_copy = (shl != &search_hl
6182 && cur != NULL
6183 && shl == &cur->hl
6184 && cur->match.regprog == cur->hl.rm.regprog);
6185 int timed_out = false;
6186
6187 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6188 &(shl->tm), &timed_out);
6189 // Copy the regprog, in case it got freed and recompiled.
6190 if (regprog_is_copy) {
6191 cur->match.regprog = cur->hl.rm.regprog;
6192 }
6193 if (called_emsg || got_int || timed_out) {
6194 // Error while handling regexp: stop using this regexp.
6195 if (shl == &search_hl) {
6196 // don't free regprog in the match list, it's a copy
6197 vim_regfree(shl->rm.regprog);
6198 set_no_hlsearch(true);
6199 }
6200 shl->rm.regprog = NULL;
6201 shl->lnum = 0;
6202 got_int = FALSE; // avoid the "Type :quit to exit Vim" message
6203 break;
6204 }
6205 } else if (cur != NULL) {
6206 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
6207 }
6208 if (nmatched == 0) {
6209 shl->lnum = 0; // no match found
6210 break;
6211 }
6212 if (shl->rm.startpos[0].lnum > 0
6213 || shl->rm.startpos[0].col >= mincol
6214 || nmatched > 1
6215 || shl->rm.endpos[0].col > mincol) {
6216 shl->lnum += shl->rm.startpos[0].lnum;
6217 break; // useful match found
6218 }
6219
6220 // Restore called_emsg for assert_fails().
6221 called_emsg = save_called_emsg;
6222 }
6223 }
6224
6225 /// @param shl points to a match. Fill on match.
6226 /// @param posmatch match positions
6227 /// @param mincol minimal column for a match
6228 ///
6229 /// @return one on match, otherwise return zero.
next_search_hl_pos(match_T * shl,linenr_T lnum,posmatch_T * posmatch,colnr_T mincol)6230 static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *posmatch, colnr_T mincol)
6231 FUNC_ATTR_NONNULL_ALL
6232 {
6233 int i;
6234 int found = -1;
6235
6236 shl->lnum = 0;
6237 for (i = posmatch->cur; i < MAXPOSMATCH; i++) {
6238 llpos_T *pos = &posmatch->pos[i];
6239
6240 if (pos->lnum == 0) {
6241 break;
6242 }
6243 if (pos->len == 0 && pos->col < mincol) {
6244 continue;
6245 }
6246 if (pos->lnum == lnum) {
6247 if (found >= 0) {
6248 // if this match comes before the one at "found" then swap
6249 // them
6250 if (pos->col < posmatch->pos[found].col) {
6251 llpos_T tmp = *pos;
6252
6253 *pos = posmatch->pos[found];
6254 posmatch->pos[found] = tmp;
6255 }
6256 } else {
6257 found = i;
6258 }
6259 }
6260 }
6261 posmatch->cur = 0;
6262 if (found >= 0) {
6263 colnr_T start = posmatch->pos[found].col == 0
6264 ? 0: posmatch->pos[found].col - 1;
6265 colnr_T end = posmatch->pos[found].col == 0
6266 ? MAXCOL : start + posmatch->pos[found].len;
6267
6268 shl->lnum = lnum;
6269 shl->rm.startpos[0].lnum = 0;
6270 shl->rm.startpos[0].col = start;
6271 shl->rm.endpos[0].lnum = 0;
6272 shl->rm.endpos[0].col = end;
6273 shl->is_addpos = true;
6274 posmatch->cur = found + 1;
6275 return 1;
6276 }
6277 return 0;
6278 }
6279
6280
6281 /// Fill the grid from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6282 /// with character 'c1' in first column followed by 'c2' in the other columns.
6283 /// Use attributes 'attr'.
grid_fill(ScreenGrid * grid,int start_row,int end_row,int start_col,int end_col,int c1,int c2,int attr)6284 void grid_fill(ScreenGrid *grid, int start_row, int end_row, int start_col, int end_col, int c1,
6285 int c2, int attr)
6286 {
6287 schar_T sc;
6288
6289 int row_off = 0, col_off = 0;
6290 screen_adjust_grid(&grid, &row_off, &col_off);
6291 start_row += row_off;
6292 end_row += row_off;
6293 start_col += col_off;
6294 end_col += col_off;
6295
6296 // safety check
6297 if (end_row > grid->Rows) {
6298 end_row = grid->Rows;
6299 }
6300 if (end_col > grid->Columns) {
6301 end_col = grid->Columns;
6302 }
6303
6304 // nothing to do
6305 if (start_row >= end_row || start_col >= end_col) {
6306 return;
6307 }
6308
6309 for (int row = start_row; row < end_row; row++) {
6310 // When drawing over the right halve of a double-wide char clear
6311 // out the left halve. When drawing over the left halve of a
6312 // double wide-char clear out the right halve. Only needed in a
6313 // terminal.
6314 if (start_col > 0 && grid_fix_col(grid, start_col, row) != start_col) {
6315 grid_puts_len(grid, (char_u *)" ", 1, row, start_col - 1, 0);
6316 }
6317 if (end_col < grid->Columns
6318 && grid_fix_col(grid, end_col, row) != end_col) {
6319 grid_puts_len(grid, (char_u *)" ", 1, row, end_col, 0);
6320 }
6321
6322 // if grid was resized (in ext_multigrid mode), the UI has no redraw updates
6323 // for the newly resized grid. It is better mark everything as dirty and
6324 // send all the updates.
6325 int dirty_first = INT_MAX;
6326 int dirty_last = 0;
6327
6328 int col = start_col;
6329 schar_from_char(sc, c1);
6330 int lineoff = grid->line_offset[row];
6331 for (col = start_col; col < end_col; col++) {
6332 int off = lineoff + col;
6333 if (schar_cmp(grid->chars[off], sc)
6334 || grid->attrs[off] != attr) {
6335 schar_copy(grid->chars[off], sc);
6336 grid->attrs[off] = attr;
6337 if (dirty_first == INT_MAX) {
6338 dirty_first = col;
6339 }
6340 dirty_last = col+1;
6341 }
6342 if (col == start_col) {
6343 schar_from_char(sc, c2);
6344 }
6345 }
6346 if (dirty_last > dirty_first) {
6347 // TODO(bfredl): support a cleared suffix even with a batched line?
6348 if (put_dirty_row == row) {
6349 put_dirty_first = MIN(put_dirty_first, dirty_first);
6350 put_dirty_last = MAX(put_dirty_last, dirty_last);
6351 } else if (grid->throttled) {
6352 // Note: assumes msg_grid is the only throttled grid
6353 assert(grid == &msg_grid);
6354 int dirty = 0;
6355 if (attr != HL_ATTR(HLF_MSG) || c2 != ' ') {
6356 dirty = dirty_last;
6357 } else if (c1 != ' ') {
6358 dirty = dirty_first + 1;
6359 }
6360 if (grid->dirty_col && dirty > grid->dirty_col[row]) {
6361 grid->dirty_col[row] = dirty;
6362 }
6363 } else {
6364 int last = c2 != ' ' ? dirty_last : dirty_first + (c1 != ' ');
6365 ui_line(grid, row, dirty_first, last, dirty_last, attr, false);
6366 }
6367 }
6368
6369 if (end_col == grid->Columns) {
6370 grid->line_wraps[row] = false;
6371 }
6372 }
6373 }
6374
6375 /// Check if there should be a delay. Used before clearing or redrawing the
6376 /// screen or the command line.
check_for_delay(bool check_msg_scroll)6377 void check_for_delay(bool check_msg_scroll)
6378 {
6379 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
6380 && !did_wait_return
6381 && emsg_silent == 0) {
6382 ui_flush();
6383 os_delay(1006L, true);
6384 emsg_on_display = false;
6385 if (check_msg_scroll) {
6386 msg_scroll = false;
6387 }
6388 }
6389 }
6390
6391 /// (Re)allocates a window grid if size changed while in ext_multigrid mode.
6392 /// Updates size, offsets and handle for the grid regardless.
6393 ///
6394 /// If "doclear" is true, don't try to copy from the old grid rather clear the
6395 /// resized grid.
win_grid_alloc(win_T * wp)6396 void win_grid_alloc(win_T *wp)
6397 {
6398 ScreenGrid *grid = &wp->w_grid;
6399 ScreenGrid *grid_allocated = &wp->w_grid_alloc;
6400
6401 int rows = wp->w_height_inner;
6402 int cols = wp->w_width_inner;
6403 int total_rows = wp->w_height_outer;
6404 int total_cols = wp->w_width_outer;
6405
6406 bool want_allocation = ui_has(kUIMultigrid) || wp->w_floating;
6407 bool has_allocation = (grid_allocated->chars != NULL);
6408
6409 if (grid->Rows != rows) {
6410 wp->w_lines_valid = 0;
6411 xfree(wp->w_lines);
6412 wp->w_lines = xcalloc(rows+1, sizeof(wline_T));
6413 }
6414
6415 int was_resized = false;
6416 if (want_allocation && (!has_allocation
6417 || grid_allocated->Rows != total_rows
6418 || grid_allocated->Columns != total_cols)) {
6419 grid_alloc(grid_allocated, total_rows, total_cols,
6420 wp->w_grid_alloc.valid, false);
6421 grid_allocated->valid = true;
6422 if (wp->w_floating && wp->w_float_config.border) {
6423 wp->w_redr_border = true;
6424 }
6425 was_resized = true;
6426 } else if (!want_allocation && has_allocation) {
6427 // Single grid mode, all rendering will be redirected to default_grid.
6428 // Only keep track of the size and offset of the window.
6429 grid_free(grid_allocated);
6430 grid_allocated->valid = false;
6431 was_resized = true;
6432 } else if (want_allocation && has_allocation && !wp->w_grid_alloc.valid) {
6433 grid_invalidate(grid_allocated);
6434 grid_allocated->valid = true;
6435 }
6436
6437 grid->Rows = rows;
6438 grid->Columns = cols;
6439
6440 if (want_allocation) {
6441 grid->target = grid_allocated;
6442 grid->row_offset = wp->w_border_adj[0];
6443 grid->col_offset = wp->w_border_adj[3];
6444 } else {
6445 grid->target = &default_grid;
6446 grid->row_offset = wp->w_winrow;
6447 grid->col_offset = wp->w_wincol;
6448 }
6449
6450 // send grid resize event if:
6451 // - a grid was just resized
6452 // - screen_resize was called and all grid sizes must be sent
6453 // - the UI wants multigrid event (necessary)
6454 if ((send_grid_resize || was_resized) && want_allocation) {
6455 ui_call_grid_resize(grid_allocated->handle,
6456 grid_allocated->Columns, grid_allocated->Rows);
6457 }
6458 }
6459
6460 /// assign a handle to the grid. The grid need not be allocated.
grid_assign_handle(ScreenGrid * grid)6461 void grid_assign_handle(ScreenGrid *grid)
6462 {
6463 static int last_grid_handle = DEFAULT_GRID_HANDLE;
6464
6465 // only assign a grid handle if not already
6466 if (grid->handle == 0) {
6467 grid->handle = ++last_grid_handle;
6468 }
6469 }
6470
6471 /// Resize the screen to Rows and Columns.
6472 ///
6473 /// Allocate default_grid.chars[] and other grid arrays.
6474 ///
6475 /// There may be some time between setting Rows and Columns and (re)allocating
6476 /// default_grid arrays. This happens when starting up and when
6477 /// (manually) changing the shell size. Always use default_grid.Rows and
6478 /// default_grid.Columns to access items in default_grid.chars[]. Use Rows
6479 /// and Columns for positioning text etc. where the final size of the shell is
6480 /// needed.
screenalloc(void)6481 void screenalloc(void)
6482 {
6483 // It's possible that we produce an out-of-memory message below, which
6484 // will cause this function to be called again. To break the loop, just
6485 // return here.
6486 if (resizing) {
6487 return;
6488 }
6489 resizing = true;
6490
6491 int retry_count = 0;
6492
6493 retry:
6494 // Allocation of the screen buffers is done only when the size changes and
6495 // when Rows and Columns have been set and we have started doing full
6496 // screen stuff.
6497 if ((default_grid.chars != NULL
6498 && Rows == default_grid.Rows
6499 && Columns == default_grid.Columns
6500 )
6501 || Rows == 0
6502 || Columns == 0
6503 || (!full_screen && default_grid.chars == NULL)) {
6504 resizing = false;
6505 return;
6506 }
6507
6508 /*
6509 * Note that the window sizes are updated before reallocating the arrays,
6510 * thus we must not redraw here!
6511 */
6512 ++RedrawingDisabled;
6513
6514 // win_new_shellsize will recompute floats position, but tell the
6515 // compositor to not redraw them yet
6516 ui_comp_set_screen_valid(false);
6517 if (msg_grid.chars) {
6518 msg_grid_invalid = true;
6519 }
6520
6521 win_new_shellsize(); // fit the windows in the new sized shell
6522
6523 comp_col(); // recompute columns for shown command and ruler
6524
6525 // We're changing the size of the screen.
6526 // - Allocate new arrays for default_grid
6527 // - Move lines from the old arrays into the new arrays, clear extra
6528 // lines (unless the screen is going to be cleared).
6529 // - Free the old arrays.
6530 //
6531 // If anything fails, make grid arrays NULL, so we don't do anything!
6532 // Continuing with the old arrays may result in a crash, because the
6533 // size is wrong.
6534
6535 grid_alloc(&default_grid, Rows, Columns, true, true);
6536 StlClickDefinition *new_tab_page_click_defs =
6537 xcalloc((size_t)Columns, sizeof(*new_tab_page_click_defs));
6538
6539 clear_tab_page_click_defs(tab_page_click_defs, tab_page_click_defs_size);
6540 xfree(tab_page_click_defs);
6541
6542 tab_page_click_defs = new_tab_page_click_defs;
6543 tab_page_click_defs_size = Columns;
6544
6545 default_grid.comp_height = Rows;
6546 default_grid.comp_width = Columns;
6547
6548 default_grid.row_offset = 0;
6549 default_grid.col_offset = 0;
6550 default_grid.handle = DEFAULT_GRID_HANDLE;
6551
6552 must_redraw = CLEAR; // need to clear the screen later
6553
6554 RedrawingDisabled--;
6555
6556 /*
6557 * Do not apply autocommands more than 3 times to avoid an endless loop
6558 * in case applying autocommands always changes Rows or Columns.
6559 */
6560 if (starting == 0 && ++retry_count <= 3) {
6561 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, false, curbuf);
6562 // In rare cases, autocommands may have altered Rows or Columns,
6563 // jump back to check if we need to allocate the screen again.
6564 goto retry;
6565 }
6566
6567 resizing = false;
6568 }
6569
grid_alloc(ScreenGrid * grid,int rows,int columns,bool copy,bool valid)6570 void grid_alloc(ScreenGrid *grid, int rows, int columns, bool copy, bool valid)
6571 {
6572 int new_row;
6573 ScreenGrid new = *grid;
6574 assert(rows >= 0 && columns >= 0);
6575 size_t ncells = (size_t)rows * columns;
6576 new.chars = xmalloc(ncells * sizeof(schar_T));
6577 new.attrs = xmalloc(ncells * sizeof(sattr_T));
6578 new.line_offset = xmalloc((size_t)(rows * sizeof(unsigned)));
6579 new.line_wraps = xmalloc((size_t)(rows * sizeof(char_u)));
6580
6581 new.Rows = rows;
6582 new.Columns = columns;
6583
6584 for (new_row = 0; new_row < new.Rows; new_row++) {
6585 new.line_offset[new_row] = new_row * new.Columns;
6586 new.line_wraps[new_row] = false;
6587
6588 grid_clear_line(&new, new.line_offset[new_row], columns, valid);
6589
6590 if (copy) {
6591 // If the screen is not going to be cleared, copy as much as
6592 // possible from the old screen to the new one and clear the rest
6593 // (used when resizing the window at the "--more--" prompt or when
6594 // executing an external command, for the GUI).
6595 if (new_row < grid->Rows && grid->chars != NULL) {
6596 int len = MIN(grid->Columns, new.Columns);
6597 memmove(new.chars + new.line_offset[new_row],
6598 grid->chars + grid->line_offset[new_row],
6599 (size_t)len * sizeof(schar_T));
6600 memmove(new.attrs + new.line_offset[new_row],
6601 grid->attrs + grid->line_offset[new_row],
6602 (size_t)len * sizeof(sattr_T));
6603 }
6604 }
6605 }
6606 grid_free(grid);
6607 *grid = new;
6608
6609 // Share a single scratch buffer for all grids, by
6610 // ensuring it is as wide as the widest grid.
6611 if (linebuf_size < (size_t)columns) {
6612 xfree(linebuf_char);
6613 xfree(linebuf_attr);
6614 linebuf_char = xmalloc(columns * sizeof(schar_T));
6615 linebuf_attr = xmalloc(columns * sizeof(sattr_T));
6616 linebuf_size = columns;
6617 }
6618 }
6619
grid_free(ScreenGrid * grid)6620 void grid_free(ScreenGrid *grid)
6621 {
6622 xfree(grid->chars);
6623 xfree(grid->attrs);
6624 xfree(grid->line_offset);
6625 xfree(grid->line_wraps);
6626
6627 grid->chars = NULL;
6628 grid->attrs = NULL;
6629 grid->line_offset = NULL;
6630 grid->line_wraps = NULL;
6631 }
6632
6633 /// Doesn't allow reinit, so must only be called by free_all_mem!
screen_free_all_mem(void)6634 void screen_free_all_mem(void)
6635 {
6636 grid_free(&default_grid);
6637 xfree(linebuf_char);
6638 xfree(linebuf_attr);
6639 }
6640
6641 /// Clear tab_page_click_defs table
6642 ///
6643 /// @param[out] tpcd Table to clear.
6644 /// @param[in] tpcd_size Size of the table.
clear_tab_page_click_defs(StlClickDefinition * const tpcd,const long tpcd_size)6645 void clear_tab_page_click_defs(StlClickDefinition *const tpcd, const long tpcd_size)
6646 {
6647 if (tpcd != NULL) {
6648 for (long i = 0; i < tpcd_size; i++) {
6649 if (i == 0 || tpcd[i].func != tpcd[i - 1].func) {
6650 xfree(tpcd[i].func);
6651 }
6652 }
6653 memset(tpcd, 0, (size_t)tpcd_size * sizeof(tpcd[0]));
6654 }
6655 }
6656
screenclear(void)6657 void screenclear(void)
6658 {
6659 check_for_delay(false);
6660 screenalloc(); // allocate screen buffers if size changed
6661
6662 int i;
6663
6664 if (starting == NO_SCREEN || default_grid.chars == NULL) {
6665 return;
6666 }
6667
6668 // blank out the default grid
6669 for (i = 0; i < default_grid.Rows; i++) {
6670 grid_clear_line(&default_grid, default_grid.line_offset[i],
6671 default_grid.Columns, true);
6672 default_grid.line_wraps[i] = false;
6673 }
6674
6675 ui_call_grid_clear(1); // clear the display
6676 ui_comp_set_screen_valid(true);
6677
6678 clear_cmdline = false;
6679 mode_displayed = false;
6680
6681 redraw_all_later(NOT_VALID);
6682 redraw_cmdline = true;
6683 redraw_tabline = true;
6684 redraw_popupmenu = true;
6685 pum_invalidate();
6686 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
6687 if (wp->w_floating) {
6688 wp->w_redr_type = CLEAR;
6689 }
6690 }
6691 if (must_redraw == CLEAR) {
6692 must_redraw = NOT_VALID; // no need to clear again
6693 }
6694 compute_cmdrow();
6695 msg_row = cmdline_row; // put cursor on last line for messages
6696 msg_col = 0;
6697 msg_scrolled = 0; // can't scroll back
6698 msg_didany = false;
6699 msg_didout = false;
6700 if (HL_ATTR(HLF_MSG) > 0 && msg_use_grid() && msg_grid.chars) {
6701 grid_invalidate(&msg_grid);
6702 msg_grid_validate();
6703 msg_grid_invalid = false;
6704 clear_cmdline = true;
6705 }
6706 }
6707
6708 /// clear a line in the grid starting at "off" until "width" characters
6709 /// are cleared.
grid_clear_line(ScreenGrid * grid,unsigned off,int width,bool valid)6710 void grid_clear_line(ScreenGrid *grid, unsigned off, int width, bool valid)
6711 {
6712 for (int col = 0; col < width; col++) {
6713 schar_from_ascii(grid->chars[off + col], ' ');
6714 }
6715 int fill = valid ? 0 : -1;
6716 (void)memset(grid->attrs + off, fill, (size_t)width * sizeof(sattr_T));
6717 }
6718
grid_invalidate(ScreenGrid * grid)6719 void grid_invalidate(ScreenGrid *grid)
6720 {
6721 (void)memset(grid->attrs, -1, grid->Rows * grid->Columns * sizeof(sattr_T));
6722 }
6723
grid_invalid_row(ScreenGrid * grid,int row)6724 bool grid_invalid_row(ScreenGrid *grid, int row)
6725 {
6726 return grid->attrs[grid->line_offset[row]] < 0;
6727 }
6728
6729
6730 /// Copy part of a grid line for vertically split window.
linecopy(ScreenGrid * grid,int to,int from,int col,int width)6731 static void linecopy(ScreenGrid *grid, int to, int from, int col, int width)
6732 {
6733 unsigned off_to = grid->line_offset[to] + col;
6734 unsigned off_from = grid->line_offset[from] + col;
6735
6736 memmove(grid->chars + off_to, grid->chars + off_from,
6737 width * sizeof(schar_T));
6738 memmove(grid->attrs + off_to, grid->attrs + off_from,
6739 width * sizeof(sattr_T));
6740 }
6741
6742 /*
6743 * Set cursor to its position in the current window.
6744 */
setcursor(void)6745 void setcursor(void)
6746 {
6747 if (redrawing()) {
6748 validate_cursor();
6749
6750 ScreenGrid *grid = &curwin->w_grid;
6751 int row = curwin->w_wrow;
6752 int col = curwin->w_wcol;
6753 if (curwin->w_p_rl) {
6754 // With 'rightleft' set and the cursor on a double-wide character,
6755 // position it on the leftmost column.
6756 col = curwin->w_width_inner - curwin->w_wcol
6757 - ((utf_ptr2cells(get_cursor_pos_ptr()) == 2
6758 && vim_isprintc(gchar_cursor())) ? 2 : 1);
6759 }
6760
6761 screen_adjust_grid(&grid, &row, &col);
6762 ui_grid_cursor_goto(grid->handle, row, col);
6763 }
6764 }
6765
6766 /// Scroll 'line_count' lines at 'row' in window 'wp'.
6767 ///
6768 /// Positive `line_count' means scrolling down, so that more space is available
6769 /// at 'row'. Negative `line_count` implies deleting lines at `row`.
win_scroll_lines(win_T * wp,int row,int line_count)6770 void win_scroll_lines(win_T *wp, int row, int line_count)
6771 {
6772 if (!redrawing() || line_count == 0) {
6773 return;
6774 }
6775
6776 // No lines are being moved, just draw over the entire area
6777 if (row + abs(line_count) >= wp->w_grid.Rows) {
6778 return;
6779 }
6780
6781 if (line_count < 0) {
6782 grid_del_lines(&wp->w_grid, row, -line_count,
6783 wp->w_grid.Rows, 0, wp->w_grid.Columns);
6784 } else {
6785 grid_ins_lines(&wp->w_grid, row, line_count,
6786 wp->w_grid.Rows, 0, wp->w_grid.Columns);
6787 }
6788 }
6789
6790 /*
6791 * The rest of the routines in this file perform screen manipulations. The
6792 * given operation is performed physically on the screen. The corresponding
6793 * change is also made to the internal screen image. In this way, the editor
6794 * anticipates the effect of editing changes on the appearance of the screen.
6795 * That way, when we call screenupdate a complete redraw isn't usually
6796 * necessary. Another advantage is that we can keep adding code to anticipate
6797 * screen changes, and in the meantime, everything still works.
6798 */
6799
6800
6801 /// insert lines on the screen and move the existing lines down
6802 /// 'line_count' is the number of lines to be inserted.
6803 /// 'end' is the line after the scrolled part. Normally it is Rows.
6804 /// 'col' is the column from with we start inserting.
6805 //
6806 /// 'row', 'col' and 'end' are relative to the start of the region.
grid_ins_lines(ScreenGrid * grid,int row,int line_count,int end,int col,int width)6807 void grid_ins_lines(ScreenGrid *grid, int row, int line_count, int end, int col, int width)
6808 {
6809 int i;
6810 int j;
6811 unsigned temp;
6812
6813 int row_off = 0;
6814 screen_adjust_grid(&grid, &row_off, &col);
6815 row += row_off;
6816 end += row_off;
6817
6818 if (line_count <= 0) {
6819 return;
6820 }
6821
6822 // Shift line_offset[] line_count down to reflect the inserted lines.
6823 // Clear the inserted lines.
6824 for (i = 0; i < line_count; i++) {
6825 if (width != grid->Columns) {
6826 // need to copy part of a line
6827 j = end - 1 - i;
6828 while ((j -= line_count) >= row) {
6829 linecopy(grid, j + line_count, j, col, width);
6830 }
6831 j += line_count;
6832 grid_clear_line(grid, grid->line_offset[j] + col, width, false);
6833 grid->line_wraps[j] = false;
6834 } else {
6835 j = end - 1 - i;
6836 temp = grid->line_offset[j];
6837 while ((j -= line_count) >= row) {
6838 grid->line_offset[j + line_count] = grid->line_offset[j];
6839 grid->line_wraps[j + line_count] = grid->line_wraps[j];
6840 }
6841 grid->line_offset[j + line_count] = temp;
6842 grid->line_wraps[j + line_count] = false;
6843 grid_clear_line(grid, temp, grid->Columns, false);
6844 }
6845 }
6846
6847 if (!grid->throttled) {
6848 ui_call_grid_scroll(grid->handle, row, end, col, col+width, -line_count, 0);
6849 }
6850
6851 return;
6852 }
6853
6854 /// delete lines on the screen and move lines up.
6855 /// 'end' is the line after the scrolled part. Normally it is Rows.
6856 /// When scrolling region used 'off' is the offset from the top for the region.
6857 /// 'row' and 'end' are relative to the start of the region.
grid_del_lines(ScreenGrid * grid,int row,int line_count,int end,int col,int width)6858 void grid_del_lines(ScreenGrid *grid, int row, int line_count, int end, int col, int width)
6859 {
6860 int j;
6861 int i;
6862 unsigned temp;
6863
6864 int row_off = 0;
6865 screen_adjust_grid(&grid, &row_off, &col);
6866 row += row_off;
6867 end += row_off;
6868
6869 if (line_count <= 0) {
6870 return;
6871 }
6872
6873 // Now shift line_offset[] line_count up to reflect the deleted lines.
6874 // Clear the inserted lines.
6875 for (i = 0; i < line_count; i++) {
6876 if (width != grid->Columns) {
6877 // need to copy part of a line
6878 j = row + i;
6879 while ((j += line_count) <= end - 1) {
6880 linecopy(grid, j - line_count, j, col, width);
6881 }
6882 j -= line_count;
6883 grid_clear_line(grid, grid->line_offset[j] + col, width, false);
6884 grid->line_wraps[j] = false;
6885 } else {
6886 // whole width, moving the line pointers is faster
6887 j = row + i;
6888 temp = grid->line_offset[j];
6889 while ((j += line_count) <= end - 1) {
6890 grid->line_offset[j - line_count] = grid->line_offset[j];
6891 grid->line_wraps[j - line_count] = grid->line_wraps[j];
6892 }
6893 grid->line_offset[j - line_count] = temp;
6894 grid->line_wraps[j - line_count] = false;
6895 grid_clear_line(grid, temp, grid->Columns, false);
6896 }
6897 }
6898
6899 if (!grid->throttled) {
6900 ui_call_grid_scroll(grid->handle, row, end, col, col+width, line_count, 0);
6901 }
6902
6903 return;
6904 }
6905
6906
6907 // Show the current mode and ruler.
6908 //
6909 // If clear_cmdline is true, clear the rest of the cmdline.
6910 // If clear_cmdline is false there may be a message there that needs to be
6911 // cleared only if a mode is shown.
6912 // Return the length of the message (0 if no message).
showmode(void)6913 int showmode(void)
6914 {
6915 int need_clear;
6916 int length = 0;
6917 int do_mode;
6918 int attr;
6919 int sub_attr;
6920
6921 if (ui_has(kUIMessages) && clear_cmdline) {
6922 msg_ext_clear(true);
6923 }
6924
6925 // don't make non-flushed message part of the showmode
6926 msg_ext_ui_flush();
6927
6928 msg_grid_validate();
6929
6930 do_mode = ((p_smd && msg_silent == 0)
6931 && ((State & TERM_FOCUS)
6932 || (State & INSERT)
6933 || restart_edit
6934 || VIsual_active));
6935 if (do_mode || reg_recording != 0) {
6936 // Don't show mode right now, when not redrawing or inside a mapping.
6937 // Call char_avail() only when we are going to show something, because
6938 // it takes a bit of time.
6939 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0) {
6940 redraw_cmdline = true; // show mode later
6941 return 0;
6942 }
6943
6944 bool nwr_save = need_wait_return;
6945
6946 // wait a bit before overwriting an important message
6947 check_for_delay(false);
6948
6949 // if the cmdline is more than one line high, erase top lines
6950 need_clear = clear_cmdline;
6951 if (clear_cmdline && cmdline_row < Rows - 1) {
6952 msg_clr_cmdline(); // will reset clear_cmdline
6953 }
6954
6955 // Position on the last line in the window, column 0
6956 msg_pos_mode();
6957 attr = HL_ATTR(HLF_CM); // Highlight mode
6958
6959 // When the screen is too narrow to show the entire mode message,
6960 // avoid scrolling and truncate instead.
6961 msg_no_more = true;
6962 int save_lines_left = lines_left;
6963 lines_left = 0;
6964
6965 if (do_mode) {
6966 msg_puts_attr("--", attr);
6967 // CTRL-X in Insert mode
6968 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU)) {
6969 // These messages can get long, avoid a wrap in a narrow window.
6970 // Prefer showing edit_submode_extra. With external messages there
6971 // is no imposed limit.
6972 if (ui_has(kUIMessages)) {
6973 length = INT_MAX;
6974 } else {
6975 length = (Rows - msg_row) * Columns - 3;
6976 }
6977 if (edit_submode_extra != NULL) {
6978 length -= vim_strsize(edit_submode_extra);
6979 }
6980 if (length > 0) {
6981 if (edit_submode_pre != NULL) {
6982 length -= vim_strsize(edit_submode_pre);
6983 }
6984 if (length - vim_strsize(edit_submode) > 0) {
6985 if (edit_submode_pre != NULL) {
6986 msg_puts_attr((const char *)edit_submode_pre, attr);
6987 }
6988 msg_puts_attr((const char *)edit_submode, attr);
6989 }
6990 if (edit_submode_extra != NULL) {
6991 msg_puts_attr(" ", attr); // Add a space in between.
6992 if ((int)edit_submode_highl < HLF_COUNT) {
6993 sub_attr = win_hl_attr(curwin, edit_submode_highl);
6994 } else {
6995 sub_attr = attr;
6996 }
6997 msg_puts_attr((const char *)edit_submode_extra, sub_attr);
6998 }
6999 }
7000 } else {
7001 if (State & TERM_FOCUS) {
7002 msg_puts_attr(_(" TERMINAL"), attr);
7003 } else if (State & VREPLACE_FLAG) {
7004 msg_puts_attr(_(" VREPLACE"), attr);
7005 } else if (State & REPLACE_FLAG) {
7006 msg_puts_attr(_(" REPLACE"), attr);
7007 } else if (State & INSERT) {
7008 if (p_ri) {
7009 msg_puts_attr(_(" REVERSE"), attr);
7010 }
7011 msg_puts_attr(_(" INSERT"), attr);
7012 } else if (restart_edit == 'I' || restart_edit == 'i'
7013 || restart_edit == 'a') {
7014 msg_puts_attr(_(" (insert)"), attr);
7015 } else if (restart_edit == 'R') {
7016 msg_puts_attr(_(" (replace)"), attr);
7017 } else if (restart_edit == 'V') {
7018 msg_puts_attr(_(" (vreplace)"), attr);
7019 }
7020 if (p_hkmap) {
7021 msg_puts_attr(_(" Hebrew"), attr);
7022 }
7023 if (State & LANGMAP) {
7024 if (curwin->w_p_arab) {
7025 msg_puts_attr(_(" Arabic"), attr);
7026 } else if (get_keymap_str(curwin, (char_u *)" (%s)",
7027 NameBuff, MAXPATHL)) {
7028 msg_puts_attr((char *)NameBuff, attr);
7029 }
7030 }
7031 if ((State & INSERT) && p_paste) {
7032 msg_puts_attr(_(" (paste)"), attr);
7033 }
7034
7035 if (VIsual_active) {
7036 char *p;
7037
7038 // Don't concatenate separate words to avoid translation
7039 // problems.
7040 switch ((VIsual_select ? 4 : 0)
7041 + (VIsual_mode == Ctrl_V) * 2
7042 + (VIsual_mode == 'V')) {
7043 case 0:
7044 p = N_(" VISUAL"); break;
7045 case 1:
7046 p = N_(" VISUAL LINE"); break;
7047 case 2:
7048 p = N_(" VISUAL BLOCK"); break;
7049 case 4:
7050 p = N_(" SELECT"); break;
7051 case 5:
7052 p = N_(" SELECT LINE"); break;
7053 default:
7054 p = N_(" SELECT BLOCK"); break;
7055 }
7056 msg_puts_attr(_(p), attr);
7057 }
7058 msg_puts_attr(" --", attr);
7059 }
7060
7061 need_clear = TRUE;
7062 }
7063 if (reg_recording != 0
7064 && edit_submode == NULL // otherwise it gets too long
7065 ) {
7066 recording_mode(attr);
7067 need_clear = true;
7068 }
7069
7070 mode_displayed = true;
7071 if (need_clear || clear_cmdline) {
7072 msg_clr_eos();
7073 }
7074 msg_didout = false; // overwrite this message
7075 length = msg_col;
7076 msg_col = 0;
7077 msg_no_more = false;
7078 lines_left = save_lines_left;
7079 need_wait_return = nwr_save; // never ask for hit-return for this
7080 } else if (clear_cmdline && msg_silent == 0) {
7081 // Clear the whole command line. Will reset "clear_cmdline".
7082 msg_clr_cmdline();
7083 }
7084
7085 // NB: also handles clearing the showmode if it was empty or disabled
7086 msg_ext_flush_showmode();
7087
7088 // In Visual mode the size of the selected area must be redrawn.
7089 if (VIsual_active) {
7090 clear_showcmd();
7091 }
7092
7093 // If the last window has no status line, the ruler is after the mode
7094 // message and must be redrawn
7095 win_T *last = lastwin_nofloating();
7096 if (redrawing() && last->w_status_height == 0) {
7097 win_redr_ruler(last, true);
7098 }
7099 redraw_cmdline = false;
7100 clear_cmdline = false;
7101
7102 return length;
7103 }
7104
7105 /*
7106 * Position for a mode message.
7107 */
msg_pos_mode(void)7108 static void msg_pos_mode(void)
7109 {
7110 msg_col = 0;
7111 msg_row = Rows - 1;
7112 }
7113
7114 /// Delete mode message. Used when ESC is typed which is expected to end
7115 /// Insert mode (but Insert mode didn't end yet!).
7116 /// Caller should check "mode_displayed".
unshowmode(bool force)7117 void unshowmode(bool force)
7118 {
7119 // Don't delete it right now, when not redrawing or inside a mapping.
7120 if (!redrawing() || (!force && char_avail() && !KeyTyped)) {
7121 redraw_cmdline = true; // delete mode later
7122 } else {
7123 clearmode();
7124 }
7125 }
7126
7127 // Clear the mode message.
clearmode(void)7128 void clearmode(void)
7129 {
7130 const int save_msg_row = msg_row;
7131 const int save_msg_col = msg_col;
7132
7133 msg_ext_ui_flush();
7134 msg_pos_mode();
7135 if (reg_recording != 0) {
7136 recording_mode(HL_ATTR(HLF_CM));
7137 }
7138 msg_clr_eos();
7139 msg_ext_flush_showmode();
7140
7141 msg_col = save_msg_col;
7142 msg_row = save_msg_row;
7143 }
7144
recording_mode(int attr)7145 static void recording_mode(int attr)
7146 {
7147 msg_puts_attr(_("recording"), attr);
7148 if (!shortmess(SHM_RECORDING)) {
7149 char s[4];
7150 snprintf(s, ARRAY_SIZE(s), " @%c", reg_recording);
7151 msg_puts_attr(s, attr);
7152 }
7153 }
7154
7155 /*
7156 * Draw the tab pages line at the top of the Vim window.
7157 */
draw_tabline(void)7158 void draw_tabline(void)
7159 {
7160 int tabcount = 0;
7161 int tabwidth = 0;
7162 int col = 0;
7163 int scol = 0;
7164 int attr;
7165 win_T *wp;
7166 win_T *cwp;
7167 int wincount;
7168 int modified;
7169 int c;
7170 int len;
7171 int attr_nosel = HL_ATTR(HLF_TP);
7172 int attr_fill = HL_ATTR(HLF_TPF);
7173 char_u *p;
7174 int room;
7175 int use_sep_chars = (t_colors < 8
7176 );
7177
7178 if (default_grid.chars == NULL) {
7179 return;
7180 }
7181 redraw_tabline = false;
7182
7183 if (ui_has(kUITabline)) {
7184 ui_ext_tabline_update();
7185 return;
7186 }
7187
7188 if (tabline_height() < 1) {
7189 return;
7190 }
7191
7192
7193 // Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
7194 assert(Columns == tab_page_click_defs_size);
7195 clear_tab_page_click_defs(tab_page_click_defs, tab_page_click_defs_size);
7196
7197 // Use the 'tabline' option if it's set.
7198 if (*p_tal != NUL) {
7199 int saved_did_emsg = did_emsg;
7200
7201 // Check for an error. If there is one we would loop in redrawing the
7202 // screen. Avoid that by making 'tabline' empty.
7203 did_emsg = false;
7204 win_redr_custom(NULL, false);
7205 if (did_emsg) {
7206 set_string_option_direct("tabline", -1,
7207 (char_u *)"", OPT_FREE, SID_ERROR);
7208 }
7209 did_emsg |= saved_did_emsg;
7210 } else {
7211 FOR_ALL_TABS(tp) {
7212 ++tabcount;
7213 }
7214
7215 if (tabcount > 0) {
7216 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
7217 }
7218
7219 if (tabwidth < 6) {
7220 tabwidth = 6;
7221 }
7222
7223 attr = attr_nosel;
7224 tabcount = 0;
7225
7226 FOR_ALL_TABS(tp) {
7227 if (col >= Columns - 4) {
7228 break;
7229 }
7230
7231 scol = col;
7232
7233 if (tp == curtab) {
7234 cwp = curwin;
7235 wp = firstwin;
7236 } else {
7237 cwp = tp->tp_curwin;
7238 wp = tp->tp_firstwin;
7239 }
7240
7241
7242 if (tp->tp_topframe == topframe) {
7243 attr = win_hl_attr(cwp, HLF_TPS);
7244 }
7245 if (use_sep_chars && col > 0) {
7246 grid_putchar(&default_grid, '|', 0, col++, attr);
7247 }
7248
7249 if (tp->tp_topframe != topframe) {
7250 attr = win_hl_attr(cwp, HLF_TP);
7251 }
7252
7253 grid_putchar(&default_grid, ' ', 0, col++, attr);
7254
7255 modified = false;
7256
7257 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount) {
7258 if (bufIsChanged(wp->w_buffer)) {
7259 modified = true;
7260 }
7261 }
7262
7263
7264 if (modified || wincount > 1) {
7265 if (wincount > 1) {
7266 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
7267 len = (int)STRLEN(NameBuff);
7268 if (col + len >= Columns - 3) {
7269 break;
7270 }
7271 grid_puts_len(&default_grid, NameBuff, len, 0, col,
7272 hl_combine_attr(attr, win_hl_attr(cwp, HLF_T)));
7273 col += len;
7274 }
7275 if (modified) {
7276 grid_puts_len(&default_grid, (char_u *)"+", 1, 0, col++, attr);
7277 }
7278 grid_putchar(&default_grid, ' ', 0, col++, attr);
7279 }
7280
7281 room = scol - col + tabwidth - 1;
7282 if (room > 0) {
7283 // Get buffer name in NameBuff[]
7284 get_trans_bufname(cwp->w_buffer);
7285 (void)shorten_dir(NameBuff);
7286 len = vim_strsize(NameBuff);
7287 p = NameBuff;
7288 while (len > room) {
7289 len -= ptr2cells(p);
7290 MB_PTR_ADV(p);
7291 }
7292 if (len > Columns - col - 1) {
7293 len = Columns - col - 1;
7294 }
7295
7296 grid_puts_len(&default_grid, p, (int)STRLEN(p), 0, col, attr);
7297 col += len;
7298 }
7299 grid_putchar(&default_grid, ' ', 0, col++, attr);
7300
7301 // Store the tab page number in tab_page_click_defs[], so that
7302 // jump_to_mouse() knows where each one is.
7303 tabcount++;
7304 while (scol < col) {
7305 tab_page_click_defs[scol++] = (StlClickDefinition) {
7306 .type = kStlClickTabSwitch,
7307 .tabnr = tabcount,
7308 .func = NULL,
7309 };
7310 }
7311 }
7312
7313 if (use_sep_chars) {
7314 c = '_';
7315 } else {
7316 c = ' ';
7317 }
7318 grid_fill(&default_grid, 0, 1, col, Columns, c, c, attr_fill);
7319
7320 // Put an "X" for closing the current tab if there are several.
7321 if (first_tabpage->tp_next != NULL) {
7322 grid_putchar(&default_grid, 'X', 0, Columns - 1, attr_nosel);
7323 tab_page_click_defs[Columns - 1] = (StlClickDefinition) {
7324 .type = kStlClickTabClose,
7325 .tabnr = 999,
7326 .func = NULL,
7327 };
7328 }
7329 }
7330
7331 // Reset the flag here again, in case evaluating 'tabline' causes it to be
7332 // set.
7333 redraw_tabline = false;
7334 }
7335
ui_ext_tabline_update(void)7336 void ui_ext_tabline_update(void)
7337 {
7338 Array tabs = ARRAY_DICT_INIT;
7339 FOR_ALL_TABS(tp) {
7340 Dictionary tab_info = ARRAY_DICT_INIT;
7341 PUT(tab_info, "tab", TABPAGE_OBJ(tp->handle));
7342
7343 win_T *cwp = (tp == curtab) ? curwin : tp->tp_curwin;
7344 get_trans_bufname(cwp->w_buffer);
7345 PUT(tab_info, "name", STRING_OBJ(cstr_to_string((char *)NameBuff)));
7346
7347 ADD(tabs, DICTIONARY_OBJ(tab_info));
7348 }
7349
7350 Array buffers = ARRAY_DICT_INIT;
7351 FOR_ALL_BUFFERS(buf) {
7352 // Do not include unlisted buffers
7353 if (!buf->b_p_bl) {
7354 continue;
7355 }
7356
7357 Dictionary buffer_info = ARRAY_DICT_INIT;
7358 PUT(buffer_info, "buffer", BUFFER_OBJ(buf->handle));
7359
7360 get_trans_bufname(buf);
7361 PUT(buffer_info, "name", STRING_OBJ(cstr_to_string((char *)NameBuff)));
7362
7363 ADD(buffers, DICTIONARY_OBJ(buffer_info));
7364 }
7365
7366 ui_call_tabline_update(curtab->handle, tabs, curbuf->handle, buffers);
7367 }
7368
7369 /*
7370 * Get buffer name for "buf" into NameBuff[].
7371 * Takes care of special buffer names and translates special characters.
7372 */
get_trans_bufname(buf_T * buf)7373 void get_trans_bufname(buf_T *buf)
7374 {
7375 if (buf_spname(buf) != NULL) {
7376 STRLCPY(NameBuff, buf_spname(buf), MAXPATHL);
7377 } else {
7378 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
7379 }
7380 trans_characters(NameBuff, MAXPATHL);
7381 }
7382
7383 /*
7384 * Get the character to use in a status line. Get its attributes in "*attr".
7385 */
fillchar_status(int * attr,win_T * wp)7386 int fillchar_status(int *attr, win_T *wp)
7387 {
7388 int fill;
7389 bool is_curwin = (wp == curwin);
7390 if (is_curwin) {
7391 *attr = win_hl_attr(wp, HLF_S);
7392 fill = wp->w_p_fcs_chars.stl;
7393 } else {
7394 *attr = win_hl_attr(wp, HLF_SNC);
7395 fill = wp->w_p_fcs_chars.stlnc;
7396 }
7397 // Use fill when there is highlighting, and highlighting of current
7398 // window differs, or the fillchars differ, or this is not the
7399 // current window
7400 if (*attr != 0 && ((win_hl_attr(wp, HLF_S) != win_hl_attr(wp, HLF_SNC)
7401 || !is_curwin || ONE_WINDOW)
7402 || (wp->w_p_fcs_chars.stl != wp->w_p_fcs_chars.stlnc))) {
7403 return fill;
7404 }
7405 if (is_curwin) {
7406 return '^';
7407 }
7408 return '=';
7409 }
7410
7411 /*
7412 * Get the character to use in a separator between vertically split windows.
7413 * Get its attributes in "*attr".
7414 */
fillchar_vsep(win_T * wp,int * attr)7415 static int fillchar_vsep(win_T *wp, int *attr)
7416 {
7417 *attr = win_hl_attr(wp, HLF_C);
7418 return wp->w_p_fcs_chars.vert;
7419 }
7420
7421 /*
7422 * Return TRUE if redrawing should currently be done.
7423 */
redrawing(void)7424 int redrawing(void)
7425 {
7426 return !RedrawingDisabled
7427 && !(p_lz && char_avail() && !KeyTyped && !do_redraw);
7428 }
7429
7430 /*
7431 * Return TRUE if printing messages should currently be done.
7432 */
messaging(void)7433 int messaging(void)
7434 {
7435 return !(p_lz && char_avail() && !KeyTyped);
7436 }
7437
7438 /// Show current status info in ruler and various other places
7439 ///
7440 /// @param always if false, only show ruler if position has changed.
showruler(bool always)7441 void showruler(bool always)
7442 {
7443 if (!always && !redrawing()) {
7444 return;
7445 }
7446 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height) {
7447 redraw_custom_statusline(curwin);
7448 } else {
7449 win_redr_ruler(curwin, always);
7450 }
7451
7452 if (need_maketitle
7453 || (p_icon && (stl_syntax & STL_IN_ICON))
7454 || (p_title && (stl_syntax & STL_IN_TITLE))) {
7455 maketitle();
7456 }
7457 // Redraw the tab pages line if needed.
7458 if (redraw_tabline) {
7459 draw_tabline();
7460 }
7461 }
7462
win_redr_ruler(win_T * wp,bool always)7463 static void win_redr_ruler(win_T *wp, bool always)
7464 {
7465 static bool did_show_ext_ruler = false;
7466
7467 // If 'ruler' off or redrawing disabled, don't do anything
7468 if (!p_ru) {
7469 return;
7470 }
7471
7472 /*
7473 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
7474 * after deleting lines, before cursor.lnum is corrected.
7475 */
7476 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count) {
7477 return;
7478 }
7479
7480 // Don't draw the ruler while doing insert-completion, it might overwrite
7481 // the (long) mode message.
7482 if (wp == lastwin && lastwin->w_status_height == 0) {
7483 if (edit_submode != NULL) {
7484 return;
7485 }
7486 }
7487
7488 if (*p_ruf) {
7489 int save_called_emsg = called_emsg;
7490
7491 called_emsg = false;
7492 win_redr_custom(wp, true);
7493 if (called_emsg) {
7494 set_string_option_direct("rulerformat", -1, (char_u *)"",
7495 OPT_FREE, SID_ERROR);
7496 }
7497 called_emsg |= save_called_emsg;
7498 return;
7499 }
7500
7501 /*
7502 * Check if not in Insert mode and the line is empty (will show "0-1").
7503 */
7504 int empty_line = FALSE;
7505 if (!(State & INSERT)
7506 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL) {
7507 empty_line = TRUE;
7508 }
7509
7510 /*
7511 * Only draw the ruler when something changed.
7512 */
7513 validate_virtcol_win(wp);
7514 if (redraw_cmdline
7515 || always
7516 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
7517 || wp->w_cursor.col != wp->w_ru_cursor.col
7518 || wp->w_virtcol != wp->w_ru_virtcol
7519 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
7520 || wp->w_topline != wp->w_ru_topline
7521 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
7522 || wp->w_topfill != wp->w_ru_topfill
7523 || empty_line != wp->w_ru_empty) {
7524 int width;
7525 int row;
7526 int fillchar;
7527 int attr;
7528 int off;
7529 bool part_of_status = false;
7530
7531 if (wp->w_status_height) {
7532 row = W_ENDROW(wp);
7533 fillchar = fillchar_status(&attr, wp);
7534 off = wp->w_wincol;
7535 width = wp->w_width;
7536 part_of_status = true;
7537 } else {
7538 row = Rows - 1;
7539 fillchar = ' ';
7540 attr = HL_ATTR(HLF_MSG);
7541 width = Columns;
7542 off = 0;
7543 }
7544
7545 // In list mode virtcol needs to be recomputed
7546 colnr_T virtcol = wp->w_virtcol;
7547 if (wp->w_p_list && wp->w_p_lcs_chars.tab1 == NUL) {
7548 wp->w_p_list = false;
7549 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
7550 wp->w_p_list = true;
7551 }
7552
7553 #define RULER_BUF_LEN 70
7554 char_u buffer[RULER_BUF_LEN];
7555
7556 /*
7557 * Some sprintfs return the length, some return a pointer.
7558 * To avoid portability problems we use strlen() here.
7559 */
7560 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%" PRId64 ",",
7561 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) ? (int64_t)0L
7562 : (int64_t)wp->w_cursor.lnum);
7563 size_t len = STRLEN(buffer);
7564 col_print(buffer + len, RULER_BUF_LEN - len,
7565 empty_line ? 0 : (int)wp->w_cursor.col + 1,
7566 (int)virtcol + 1);
7567
7568 /*
7569 * Add a "50%" if there is room for it.
7570 * On the last line, don't print in the last column (scrolls the
7571 * screen up on some terminals).
7572 */
7573 int i = (int)STRLEN(buffer);
7574 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
7575 int o = i + vim_strsize(buffer + i + 1);
7576 if (wp->w_status_height == 0) { // can't use last char of screen
7577 o++;
7578 }
7579 int this_ru_col = ru_col - (Columns - width);
7580 if (this_ru_col < 0) {
7581 this_ru_col = 0;
7582 }
7583 // Never use more than half the window/screen width, leave the other half
7584 // for the filename.
7585 if (this_ru_col < (width + 1) / 2) {
7586 this_ru_col = (width + 1) / 2;
7587 }
7588 if (this_ru_col + o < width) {
7589 // Need at least 3 chars left for get_rel_pos() + NUL.
7590 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4) {
7591 i += utf_char2bytes(fillchar, buffer + i);
7592 o++;
7593 }
7594 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
7595 }
7596
7597 if (ui_has(kUIMessages) && !part_of_status) {
7598 Array content = ARRAY_DICT_INIT;
7599 Array chunk = ARRAY_DICT_INIT;
7600 ADD(chunk, INTEGER_OBJ(attr));
7601 ADD(chunk, STRING_OBJ(cstr_to_string((char *)buffer)));
7602 ADD(content, ARRAY_OBJ(chunk));
7603 ui_call_msg_ruler(content);
7604 did_show_ext_ruler = true;
7605 } else {
7606 if (did_show_ext_ruler) {
7607 ui_call_msg_ruler((Array)ARRAY_DICT_INIT);
7608 did_show_ext_ruler = false;
7609 }
7610 // Truncate at window boundary.
7611 o = 0;
7612 for (i = 0; buffer[i] != NUL; i += utfc_ptr2len(buffer + i)) {
7613 o += utf_ptr2cells(buffer + i);
7614 if (this_ru_col + o > width) {
7615 buffer[i] = NUL;
7616 break;
7617 }
7618 }
7619
7620 ScreenGrid *grid = part_of_status ? &default_grid : &msg_grid_adj;
7621 grid_puts(grid, buffer, row, this_ru_col + off, attr);
7622 grid_fill(grid, row, row + 1,
7623 this_ru_col + off + (int)STRLEN(buffer), off + width, fillchar,
7624 fillchar, attr);
7625 }
7626
7627 wp->w_ru_cursor = wp->w_cursor;
7628 wp->w_ru_virtcol = wp->w_virtcol;
7629 wp->w_ru_empty = empty_line;
7630 wp->w_ru_topline = wp->w_topline;
7631 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
7632 wp->w_ru_topfill = wp->w_topfill;
7633 }
7634 }
7635
7636 /*
7637 * Return the width of the 'number' and 'relativenumber' column.
7638 * Caller may need to check if 'number' or 'relativenumber' is set.
7639 * Otherwise it depends on 'numberwidth' and the line count.
7640 */
number_width(win_T * wp)7641 int number_width(win_T *wp)
7642 {
7643 int n;
7644 linenr_T lnum;
7645
7646 if (wp->w_p_rnu && !wp->w_p_nu) {
7647 // cursor line shows "0"
7648 lnum = wp->w_height_inner;
7649 } else {
7650 // cursor line shows absolute line number
7651 lnum = wp->w_buffer->b_ml.ml_line_count;
7652 }
7653
7654 if (lnum == wp->w_nrwidth_line_count) {
7655 return wp->w_nrwidth_width;
7656 }
7657 wp->w_nrwidth_line_count = lnum;
7658
7659 n = 0;
7660 do {
7661 lnum /= 10;
7662 ++n;
7663 } while (lnum > 0);
7664
7665 // 'numberwidth' gives the minimal width plus one
7666 if (n < wp->w_p_nuw - 1) {
7667 n = wp->w_p_nuw - 1;
7668 }
7669
7670 // If 'signcolumn' is set to 'number' and there is a sign to display, then
7671 // the minimal width for the number column is 2.
7672 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
7673 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')) {
7674 n = 2;
7675 }
7676
7677 wp->w_nrwidth_width = n;
7678 return n;
7679 }
7680
7681 /// Used when 'cursorlineopt' contains "screenline": compute the margins between
7682 /// which the highlighting is used.
margin_columns_win(win_T * wp,int * left_col,int * right_col)7683 static void margin_columns_win(win_T *wp, int *left_col, int *right_col)
7684 {
7685 // cache previous calculations depending on w_virtcol
7686 static int saved_w_virtcol;
7687 static win_T *prev_wp;
7688 static int prev_left_col;
7689 static int prev_right_col;
7690 static int prev_col_off;
7691
7692 int cur_col_off = win_col_off(wp);
7693 int width1;
7694 int width2;
7695
7696 if (saved_w_virtcol == wp->w_virtcol && prev_wp == wp
7697 && prev_col_off == cur_col_off) {
7698 *right_col = prev_right_col;
7699 *left_col = prev_left_col;
7700 return;
7701 }
7702
7703 width1 = wp->w_width - cur_col_off;
7704 width2 = width1 + win_col_off2(wp);
7705
7706 *left_col = 0;
7707 *right_col = width1;
7708
7709 if (wp->w_virtcol >= (colnr_T)width1) {
7710 *right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
7711 }
7712 if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0) {
7713 *left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
7714 }
7715
7716 // cache values
7717 prev_left_col = *left_col;
7718 prev_right_col = *right_col;
7719 prev_wp = wp;
7720 saved_w_virtcol = wp->w_virtcol;
7721 prev_col_off = cur_col_off;
7722 }
7723
7724 /// Set dimensions of the Nvim application "shell".
screen_resize(int width,int height)7725 void screen_resize(int width, int height)
7726 {
7727 static bool recursive = false;
7728
7729 // Avoid recursiveness, can happen when setting the window size causes
7730 // another window-changed signal.
7731 if (updating_screen || recursive) {
7732 return;
7733 }
7734
7735 if (width < 0 || height < 0) { // just checking...
7736 return;
7737 }
7738
7739 if (State == HITRETURN || State == SETWSIZE) {
7740 // postpone the resizing
7741 State = SETWSIZE;
7742 return;
7743 }
7744
7745 /* curwin->w_buffer can be NULL when we are closing a window and the
7746 * buffer has already been closed and removing a scrollbar causes a resize
7747 * event. Don't resize then, it will happen after entering another buffer.
7748 */
7749 if (curwin->w_buffer == NULL) {
7750 return;
7751 }
7752
7753 recursive = true;
7754
7755 Rows = height;
7756 Columns = width;
7757 check_shellsize();
7758 int max_p_ch = Rows - min_rows() + 1;
7759 if (!ui_has(kUIMessages) && p_ch > max_p_ch) {
7760 p_ch = max_p_ch ? max_p_ch : 1;
7761 }
7762 height = Rows;
7763 width = Columns;
7764 p_lines = Rows;
7765 p_columns = Columns;
7766 ui_call_grid_resize(1, width, height);
7767
7768 send_grid_resize = true;
7769
7770 /* The window layout used to be adjusted here, but it now happens in
7771 * screenalloc() (also invoked from screenclear()). That is because the
7772 * "recursive" check above may skip this, but not screenalloc(). */
7773
7774 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM) {
7775 screenclear();
7776 }
7777
7778 if (starting != NO_SCREEN) {
7779 maketitle();
7780 changed_line_abv_curs();
7781 invalidate_botline();
7782
7783 /*
7784 * We only redraw when it's needed:
7785 * - While at the more prompt or executing an external command, don't
7786 * redraw, but position the cursor.
7787 * - While editing the command line, only redraw that.
7788 * - in Ex mode, don't redraw anything.
7789 * - Otherwise, redraw right now, and position the cursor.
7790 * Always need to call update_screen() or screenalloc(), to make
7791 * sure Rows/Columns and the size of the screen is correct!
7792 */
7793 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
7794 || exmode_active) {
7795 screenalloc();
7796 if (msg_grid.chars) {
7797 msg_grid_validate();
7798 }
7799 // TODO(bfredl): sometimes messes up the output. Implement clear+redraw
7800 // also for the pager? (or: what if the pager was just a modal window?)
7801 ui_comp_set_screen_valid(true);
7802 repeat_message();
7803 } else {
7804 if (curwin->w_p_scb) {
7805 do_check_scrollbind(true);
7806 }
7807 if (State & CMDLINE) {
7808 redraw_popupmenu = false;
7809 update_screen(NOT_VALID);
7810 redrawcmdline();
7811 if (pum_drawn()) {
7812 cmdline_pum_display(false);
7813 }
7814 } else {
7815 update_topline(curwin);
7816 if (pum_drawn()) {
7817 // TODO(bfredl): ins_compl_show_pum wants to redraw the screen first.
7818 // For now make sure the nested update_screen(0) won't redraw the
7819 // pum at the old position. Try to untangle this later.
7820 redraw_popupmenu = false;
7821 ins_compl_show_pum();
7822 }
7823 update_screen(NOT_VALID);
7824 if (redrawing()) {
7825 setcursor();
7826 }
7827 }
7828 }
7829 ui_flush();
7830 }
7831 recursive = false;
7832 }
7833
7834 /// Check if the new Nvim application "shell" dimensions are valid.
7835 /// Correct it if it's too small or way too big.
check_shellsize(void)7836 void check_shellsize(void)
7837 {
7838 if (Rows < min_rows()) {
7839 // need room for one window and command line
7840 Rows = min_rows();
7841 }
7842 limit_screen_size();
7843 }
7844
7845 // Limit Rows and Columns to avoid an overflow in Rows * Columns.
limit_screen_size(void)7846 void limit_screen_size(void)
7847 {
7848 if (Columns < MIN_COLUMNS) {
7849 Columns = MIN_COLUMNS;
7850 } else if (Columns > 10000) {
7851 Columns = 10000;
7852 }
7853
7854 if (Rows > 1000) {
7855 Rows = 1000;
7856 }
7857 }
7858
win_new_shellsize(void)7859 void win_new_shellsize(void)
7860 {
7861 static long old_Rows = 0;
7862 static long old_Columns = 0;
7863
7864 if (old_Rows != Rows) {
7865 // If 'window' uses the whole screen, keep it using that.
7866 // Don't change it when set with "-w size" on the command line.
7867 if (p_window == old_Rows - 1 || (old_Rows == 0 && p_window == 0)) {
7868 p_window = Rows - 1;
7869 }
7870 old_Rows = Rows;
7871 shell_new_rows(); // update window sizes
7872 }
7873 if (old_Columns != Columns) {
7874 old_Columns = Columns;
7875 shell_new_columns(); // update window sizes
7876 }
7877 }
7878
get_win_by_grid_handle(handle_T handle)7879 win_T *get_win_by_grid_handle(handle_T handle)
7880 {
7881 FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
7882 if (wp->w_grid_alloc.handle == handle) {
7883 return wp;
7884 }
7885 }
7886 return NULL;
7887 }
7888
7889