diff options
Diffstat (limited to 'src/nvim/screen.c')
-rw-r--r-- | src/nvim/screen.c | 4953 |
1 files changed, 2393 insertions, 2560 deletions
diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 34eef83164..2467cf192f 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -1,88 +1,71 @@ -/* - * screen.c: code for displaying on the screen - * - * Output to the screen (console, terminal emulator or GUI window) is minimized - * by remembering what is already on the screen, and only updating the parts - * that changed. - * - * ScreenLines[off] Contains a copy of the whole screen, as it is currently - * displayed (excluding text written by external commands). - * ScreenAttrs[off] Contains the associated attributes. - * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[] - * for each line. - * LineWraps[row] Flag for each line whether it wraps to the next line. - * - * For double-byte characters, two consecutive bytes in ScreenLines[] can form - * one character which occupies two display cells. - * For UTF-8 a multi-byte character is converted to Unicode and stored in - * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII - * character without composing chars ScreenLinesUC[] will be 0 and - * ScreenLinesC[][] is not used. When the character occupies two display - * cells the next byte in ScreenLines[] is 0. - * ScreenLinesC[][] contain up to 'maxcombine' composing characters - * (drawn on top of the first character). There is 0 after the last one used. - * ScreenLines2[] is only used for euc-jp to store the second byte if the - * first byte is 0x8e (single-width character). - * - * The screen_*() functions write to the screen and handle updating - * ScreenLines[]. - * - * update_screen() is the function that updates all windows and status lines. - * It is called form the main loop when must_redraw is non-zero. It may be - * called from other places when an immediate screen update is needed. - * - * The part of the buffer that is displayed in a window is set with: - * - w_topline (first buffer line in window) - * - w_topfill (filler lines above the first line) - * - w_leftcol (leftmost window cell in window), - * - w_skipcol (skipped window cells of first line) - * - * Commands that only move the cursor around in a window, do not need to take - * action to update the display. The main loop will check if w_topline is - * valid and update it (scroll the window) when needed. - * - * Commands that scroll a window change w_topline and must call - * check_cursor() to move the cursor into the visible part of the window, and - * call redraw_later(VALID) to have the window displayed by update_screen() - * later. - * - * Commands that change text in the buffer must call changed_bytes() or - * changed_lines() to mark the area that changed and will require updating - * later. The main loop will call update_screen(), which will update each - * window that shows the changed buffer. This assumes text above the change - * can remain displayed as it is. Text after the change may need updating for - * scrolling, folding and syntax highlighting. - * - * Commands that change how a window is displayed (e.g., setting 'list') or - * invalidate the contents of a window in another way (e.g., change fold - * settings), must call redraw_later(NOT_VALID) to have the whole window - * redisplayed by update_screen() later. - * - * Commands that change how a buffer is displayed (e.g., setting 'tabstop') - * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the - * buffer redisplayed by update_screen() later. - * - * Commands that change highlighting and possibly cause a scroll too must call - * redraw_later(SOME_VALID) to update the whole window but still use scrolling - * to avoid redrawing everything. But the length of displayed lines must not - * change, use NOT_VALID then. - * - * Commands that move the window position must call redraw_later(NOT_VALID). - * TODO: should minimize redrawing by scrolling when possible. - * - * Commands that change everything (e.g., resizing the screen) must call - * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR). - * - * Things that are handled indirectly: - * - When messages scroll the screen up, msg_scrolled will be set and - * update_screen() called to redraw. - */ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +// screen.c: code for displaying on the screen +// +// Output to the screen (console, terminal emulator or GUI window) is minimized +// by remembering what is already on the screen, and only updating the parts +// that changed. +// +// The grid_*() functions write to the screen and handle updating grid->lines[]. +// +// update_screen() is the function that updates all windows and status lines. +// It is called from the main loop when must_redraw is non-zero. It may be +// called from other places when an immediate screen update is needed. +// +// The part of the buffer that is displayed in a window is set with: +// - w_topline (first buffer line in window) +// - w_topfill (filler lines above the first line) +// - w_leftcol (leftmost window cell in window), +// - w_skipcol (skipped window cells of first line) +// +// Commands that only move the cursor around in a window, do not need to take +// action to update the display. The main loop will check if w_topline is +// valid and update it (scroll the window) when needed. +// +// Commands that scroll a window change w_topline and must call +// check_cursor() to move the cursor into the visible part of the window, and +// call redraw_later(VALID) to have the window displayed by update_screen() +// later. +// +// Commands that change text in the buffer must call changed_bytes() or +// changed_lines() to mark the area that changed and will require updating +// later. The main loop will call update_screen(), which will update each +// window that shows the changed buffer. This assumes text above the change +// can remain displayed as it is. Text after the change may need updating for +// scrolling, folding and syntax highlighting. +// +// Commands that change how a window is displayed (e.g., setting 'list') or +// invalidate the contents of a window in another way (e.g., change fold +// settings), must call redraw_later(NOT_VALID) to have the whole window +// redisplayed by update_screen() later. +// +// Commands that change how a buffer is displayed (e.g., setting 'tabstop') +// must call redraw_curbuf_later(NOT_VALID) to have all the windows for the +// buffer redisplayed by update_screen() later. +// +// Commands that change highlighting and possibly cause a scroll too must call +// redraw_later(SOME_VALID) to update the whole window but still use scrolling +// to avoid redrawing everything. But the length of displayed lines must not +// change, use NOT_VALID then. +// +// Commands that move the window position must call redraw_later(NOT_VALID). +// TODO(neovim): should minimize redrawing by scrolling when possible. +// +// Commands that change everything (e.g., resizing the screen) must call +// redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR). +// +// Things that are handled indirectly: +// - When messages scroll the screen up, msg_scrolled will be set and +// update_screen() called to redraw. +/// #include <assert.h> #include <inttypes.h> #include <stdbool.h> #include <string.h> +#include "nvim/log.h" #include "nvim/vim.h" #include "nvim/ascii.h" #include "nvim/arabic.h" @@ -90,6 +73,7 @@ #include "nvim/buffer.h" #include "nvim/charset.h" #include "nvim/cursor.h" +#include "nvim/cursor_shape.h" #include "nvim/diff.h" #include "nvim/eval.h" #include "nvim/ex_cmds.h" @@ -101,14 +85,15 @@ #include "nvim/fold.h" #include "nvim/indent.h" #include "nvim/getchar.h" +#include "nvim/highlight.h" #include "nvim/main.h" +#include "nvim/mark.h" #include "nvim/mbyte.h" #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/menu.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/misc2.h" #include "nvim/garray.h" #include "nvim/move.h" #include "nvim/normal.h" @@ -120,6 +105,7 @@ #include "nvim/regexp.h" #include "nvim/search.h" #include "nvim/spell.h" +#include "nvim/state.h" #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/terminal.h" @@ -128,29 +114,45 @@ #include "nvim/version.h" #include "nvim/window.h" #include "nvim/os/time.h" +#include "nvim/api/private/helpers.h" #define MB_FILLER_CHAR '<' /* character used when a double-width character * doesn't fit. */ #define W_ENDCOL(wp) (wp->w_wincol + wp->w_width) +#define W_ENDROW(wp) (wp->w_winrow + wp->w_height) -/* - * The attributes that are actually active for writing to the screen. - */ -static int screen_attr = 0; + +// temporary buffer for rendering a single screenline, so it can be +// comparared with previous contents to calulate smallest delta. +static size_t linebuf_size = 0; +static schar_T *linebuf_char = NULL; +static sattr_T *linebuf_attr = NULL; static match_T search_hl; /* used for 'hlsearch' highlight matching */ static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */ -/* - * Buffer for one screen line (characters and attributes). - */ -static schar_T *current_ScreenLine; - StlClickDefinition *tab_page_click_defs = NULL; + long tab_page_click_defs_size = 0; -# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl)) +// for line_putchar. Contains the state that needs to be remembered from +// putting one character to the next. +typedef struct { + const char_u *p; + int prev_c; // previous Arabic character + int prev_c1; // first composing char for prev_c +} LineState; +#define LINE_STATE(p) { p, 0, 0 } + +/// Whether to call "ui_call_grid_resize" in win_grid_alloc +static bool send_grid_resize = false; + +/// Highlight ids are no longer valid. Force retransmission +static bool highlights_invalid = false; + +static bool conceal_cursor_used = false; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "screen.c.generated.h" #endif @@ -168,7 +170,7 @@ void redraw_later(int type) void redraw_win_later(win_T *wp, int type) { - if (wp->w_redr_type < type) { + if (!exiting && wp->w_redr_type < type) { wp->w_redr_type = type; if (type >= NOT_VALID) wp->w_lines_valid = 0; @@ -178,17 +180,6 @@ void redraw_win_later(win_T *wp, int type) } /* - * Force a complete redraw later. Also resets the highlighting. To be used - * after executing a shell command that messes up the screen. - */ -void redraw_later_clear(void) -{ - redraw_all_later(CLEAR); - /* Use attributes that is very unlikely to appear in text. */ - screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE; -} - -/* * Mark all windows to be redrawn later. */ void redraw_all_later(int type) @@ -196,6 +187,16 @@ void redraw_all_later(int type) FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { redraw_win_later(wp, type); } + // This may be needed when switching tabs. + if (must_redraw < type) { + must_redraw = type; + } +} + +void screen_invalidate_highlights(void) +{ + redraw_all_later(NOT_VALID); + highlights_invalid = true; } /* @@ -215,6 +216,15 @@ void redraw_buf_later(buf_T *buf, int type) } } +void redraw_buf_line_later(buf_T *buf, linenr_T line) +{ + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + if (wp->w_buffer == buf) { + redrawWinline(wp, line); + } + } +} + /* * Changed something in the current window, at buffer line "lnum", that * requires that line and possibly other lines to be redrawn. @@ -224,24 +234,20 @@ void redraw_buf_later(buf_T *buf, int type) * may become invalid and the whole window will have to be redrawn. */ void -redrawWinline ( - linenr_T lnum, - int invalid /* window line height is invalid now */ +redrawWinline( + win_T *wp, + linenr_T lnum ) { - int i; - - if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum) - curwin->w_redraw_top = lnum; - if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum) - curwin->w_redraw_bot = lnum; - redraw_later(VALID); - - if (invalid) { - /* A w_lines[] entry for this lnum has become invalid. */ - i = find_wl_entry(curwin, lnum); - if (i >= 0) - curwin->w_lines[i].wl_valid = FALSE; + if (lnum >= wp->w_topline + && lnum < wp->w_botline) { + if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum) { + wp->w_redraw_top = lnum; + } + if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum) { + wp->w_redraw_bot = lnum; + } + redraw_win_later(wp, VALID); } } @@ -254,12 +260,12 @@ void update_curbuf(int type) update_screen(type); } -/* - * update_screen() - * - * Based on the current value of curwin->w_topline, transfer a screenfull - * of stuff from Filemem to ScreenLines[], and update curwin->w_botline. - */ +/// Redraw the parts of the screen that is marked for redraw. +/// +/// Most code shouldn't call this directly, rather use redraw_later() and +/// and redraw_all_later() to mark parts of the screen as needing a redraw. +/// +/// @param type set to a NOT_VALID to force redraw of entire screen void update_screen(int type) { static int did_intro = FALSE; @@ -302,16 +308,33 @@ void update_screen(int type) * if the screen was scrolled up when displaying a message, scroll it down */ if (msg_scrolled) { - clear_cmdline = TRUE; - if (msg_scrolled > Rows - 5) /* clearing is faster */ + ui_call_win_scroll_over_reset(); + clear_cmdline = true; + if (dy_flags & DY_MSGSEP) { + int valid = MAX(Rows - msg_scrollsize(), 0); + if (valid == 0) { + redraw_tabline = true; + } + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + if (W_ENDROW(wp) > valid) { + wp->w_redr_type = NOT_VALID; + wp->w_lines_valid = 0; + } + if (W_ENDROW(wp) + wp->w_status_height > valid) { + wp->w_redr_status = true; + } + } + } else if (msg_scrolled > default_grid.Rows - 5) { // clearing is faster type = CLEAR; - else if (type != CLEAR) { - check_for_delay(FALSE); - if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL) + } else if (type != CLEAR) { + check_for_delay(false); + if (grid_ins_lines(&default_grid, 0, msg_scrolled, (int)Rows, + 0, (int)Columns) == FAIL) { type = CLEAR; + } FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { if (wp->w_winrow < msg_scrolled) { - if (wp->w_winrow + wp->w_height > msg_scrolled + if (W_ENDROW(wp) > msg_scrolled && wp->w_redr_type < REDRAW_TOP && wp->w_lines_valid > 0 && wp->w_topline == wp->w_lines[0].wl_lnum) { @@ -319,7 +342,7 @@ void update_screen(int type) wp->w_redr_type = REDRAW_TOP; } else { wp->w_redr_type = NOT_VALID; - if (wp->w_winrow + wp->w_height + wp->w_status_height + if (W_ENDROW(wp) + wp->w_status_height <= msg_scrolled) { wp->w_redr_status = TRUE; } @@ -340,9 +363,14 @@ void update_screen(int type) if (need_highlight_changed) highlight_changed(); - if (type == CLEAR) { /* first clear screen */ - screenclear(); /* will reset clear_cmdline */ + if (type == CLEAR) { // first clear screen + screenclear(); // will reset clear_cmdline + cmdline_screen_cleared(); // clear external cmdline state type = NOT_VALID; + // must_redraw may be set indirectly, avoid another redraw later + must_redraw = 0; + } else if (highlights_invalid) { + grid_invalidate(&default_grid); } if (clear_cmdline) /* going to clear cmdline (done below) */ @@ -375,15 +403,24 @@ void update_screen(int type) )) curwin->w_redr_type = type; - /* Redraw the tab pages line if needed. */ - if (redraw_tabline || type >= NOT_VALID) + // Redraw the tab pages line if needed. + if (redraw_tabline || type >= NOT_VALID) { + update_window_hl(curwin, type >= NOT_VALID); + FOR_ALL_TABS(tp) { + if (tp != curtab) { + update_window_hl(tp->tp_curwin, type >= NOT_VALID); + } + } draw_tabline(); + } /* * Correct stored syntax highlighting info for changes in each displayed * buffer. Each buffer must only be done once. */ FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + update_window_hl(wp, type >= NOT_VALID); + if (wp->w_buffer->b_mod_set) { win_T *wwp; @@ -416,13 +453,16 @@ void update_screen(int type) /* redraw status line after the window to minimize cursor movement */ if (wp->w_redr_status) { - win_redr_status(wp); + win_redr_status(wp, true); // any popup menu will be redrawn below } } + send_grid_resize = false; + highlights_invalid = false; end_search_hl(); - /* May need to redraw the popup menu. */ - if (pum_visible()) + // May need to redraw the popup menu. + if (pum_drawn()) { pum_redraw(); + } /* Reset b_mod_set flags. Going through all windows is probably faster * than going through all buffers (there could be many buffers). */ @@ -467,122 +507,29 @@ int conceal_cursor_line(win_T *wp) return vim_strchr(wp->w_p_cocu, c) != NULL; } -/* - * Check if the cursor line needs to be redrawn because of 'concealcursor'. - */ -void conceal_check_cursur_line(void) +// Check if the cursor line needs to be redrawn because of 'concealcursor'. +// +// When cursor is moved at the same time, both lines will be redrawn regardless. +void conceal_check_cursor_line(void) { - if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin)) { - need_cursor_line_redraw = TRUE; - /* Need to recompute cursor column, e.g., when starting Visual mode - * without concealing. */ - curs_columns(TRUE); + bool should_conceal = conceal_cursor_line(curwin); + if (curwin->w_p_cole > 0 && (conceal_cursor_used != should_conceal)) { + redrawWinline(curwin, curwin->w_cursor.lnum); + // Need to recompute cursor column, e.g., when starting Visual mode + // without concealing. */ + curs_columns(true); } } -void update_single_line(win_T *wp, linenr_T lnum) -{ - int row; - int j; - - if (lnum >= wp->w_topline && lnum < wp->w_botline - && foldedCount(wp, lnum, &win_foldinfo) == 0) { - row = 0; - for (j = 0; j < wp->w_lines_valid; ++j) { - if (lnum == wp->w_lines[j].wl_lnum) { - init_search_hl(wp); - start_search_hl(); - prepare_search_hl(wp, lnum); - win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, false); - end_search_hl(); - break; - } - row += wp->w_lines[j].wl_size; - } - } - need_cursor_line_redraw = FALSE; -} - - -/* - * Prepare for updating one or more windows. - * Caller must check for "updating_screen" already set to avoid recursiveness. - */ -static void update_prepare(void) -{ - updating_screen = TRUE; - start_search_hl(); -} - -/* - * Finish updating one or more windows. - */ -static void update_finish(void) -{ - if (redraw_cmdline) { - showmode(); - } - - end_search_hl(); - updating_screen = FALSE; -} - -void update_debug_sign(buf_T *buf, linenr_T lnum) -{ - int doit = FALSE; - win_foldinfo.fi_level = 0; - - /* update/delete a specific mark */ - FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { - if (buf != NULL && lnum > 0) { - if (wp->w_buffer == buf && lnum >= wp->w_topline - && lnum < wp->w_botline) { - if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum) { - wp->w_redraw_top = lnum; - } - if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum) { - wp->w_redraw_bot = lnum; - } - redraw_win_later(wp, VALID); - } - } else { - redraw_win_later(wp, VALID); - } - if (wp->w_redr_type != 0) { - doit = TRUE; - } - } - - /* Return when there is nothing to do, screen updating is already - * happening (recursive call) or still starting up. */ - if (!doit || updating_screen || starting) { - return; - } - - /* update all windows that need updating */ - update_prepare(); - - FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { - if (wp->w_redr_type != 0) { - win_update(wp); - } - if (wp->w_redr_status) { - win_redr_status(wp); - } - } - - update_finish(); -} - -/* - * Return TRUE when window "wp" has a column to draw signs in. - */ -static int draw_signcolumn(win_T *wp) +/// Whether cursorline is drawn in a special way +/// +/// If true, both old and new cursorline will need +/// need to be redrawn when moving cursor within windows. +bool win_cursorline_standout(win_T *wp) { - return (wp->w_buffer->b_signlist != NULL); + return wp->w_p_cul || (wp->w_p_cole > 0 && !conceal_cursor_line(wp)); } - /* * Update a single window. * @@ -639,11 +586,10 @@ static void win_update(win_T *wp) static int recursive = FALSE; /* being called recursively */ int old_botline = wp->w_botline; long fold_count; - /* remember what happened to the previous line, to know if - * check_visual_highlight() can be used */ -#define DID_NONE 1 /* didn't update a line */ -#define DID_LINE 2 /* updated a normal line */ -#define DID_FOLD 3 /* updated a folded line */ + // Remember what happened to the previous line. +#define DID_NONE 1 // didn't update a line +#define DID_LINE 2 // updated a normal line +#define DID_FOLD 3 // updated a folded line int did_update = DID_NONE; linenr_T syntax_last_parsed = 0; /* last parsed text line */ linenr_T mod_top = 0; @@ -652,20 +598,22 @@ static void win_update(win_T *wp) type = wp->w_redr_type; - if (type == NOT_VALID) { - wp->w_redr_status = TRUE; + win_grid_alloc(wp); + + if (type >= NOT_VALID) { + wp->w_redr_status = true; wp->w_lines_valid = 0; } - /* Window is zero-height: nothing to draw. */ - if (wp->w_height == 0) { + // Window is zero-height: nothing to draw. + if (wp->w_grid.Rows == 0) { wp->w_redr_type = 0; return; } - /* Window is zero-width: Only need to draw the separator. */ - if (wp->w_width == 0) { - /* draw the vertical separator right of this window */ + // Window is zero-width: Only need to draw the separator. + if (wp->w_grid.Columns == 0) { + // draw the vertical separator right of this window draw_vsep_win(wp, 0); wp->w_redr_type = 0; return; @@ -679,12 +627,18 @@ static void win_update(win_T *wp) if (wp->w_nrwidth != i) { type = NOT_VALID; wp->w_nrwidth = i; - } else if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0) { - /* - * When there are both inserted/deleted lines and specific lines to be - * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw - * everything (only happens when redrawing is off for while). - */ + + if (buf->terminal) { + terminal_resize(buf->terminal, + (uint16_t)(MAX(0, wp->w_grid.Columns - win_col_off(wp))), + (uint16_t)wp->w_grid.Rows); + } + } else if (buf->b_mod_set + && buf->b_mod_xlines != 0 + && wp->w_redraw_top != 0) { + // When there are both inserted/deleted lines and specific lines to be + // redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw + // everything (only happens when redrawing is off for while). type = NOT_VALID; } else { /* @@ -764,16 +718,18 @@ static void win_update(win_T *wp) } } - (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL); - if (mod_top > lnumt) + (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, true, NULL); + if (mod_top > lnumt) { mod_top = lnumt; + } - /* Now do the same for the bottom line (one above mod_bot). */ - --mod_bot; - (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL); - ++mod_bot; - if (mod_bot < lnumb) + // Now do the same for the bottom line (one above mod_bot). + mod_bot--; + (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, true, NULL); + mod_bot++; + if (mod_bot < lnumb) { mod_bot = lnumb; + } } /* When a change starts above w_topline and the end is below @@ -814,13 +770,6 @@ static void win_update(win_T *wp) type = VALID; } - /* Trick: we want to avoid clearing the screen twice. screenclear() will - * set "screen_cleared" to TRUE. The special value MAYBE (which is still - * non-zero and thus not FALSE) will indicate that screenclear() was not - * called. */ - if (screen_cleared) - screen_cleared = MAYBE; - /* * If there are no changes on the screen that require a complete redraw, * handle three cases: @@ -852,29 +801,26 @@ static void win_update(win_T *wp) /* count the number of lines we are off, counting a sequence * of folded lines as one */ j = 0; - for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln) { - ++j; - if (j >= wp->w_height - 2) + for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ln++) { + j++; + if (j >= wp->w_grid.Rows - 2) { break; - (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL); + } + (void)hasFoldingWin(wp, ln, NULL, &ln, true, NULL); } } else j = wp->w_lines[0].wl_lnum - wp->w_topline; - if (j < wp->w_height - 2) { /* not too far off */ + if (j < wp->w_grid.Rows - 2) { // not too far off i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1); /* insert extra lines for previously invisible filler lines */ if (wp->w_lines[0].wl_lnum != wp->w_topline) i += diff_check_fill(wp, wp->w_lines[0].wl_lnum) - wp->w_old_topfill; - if (i < wp->w_height - 2) { /* less than a screen off */ - /* - * Try to insert the correct number of lines. - * If not the last window, delete the lines at the bottom. - * win_ins_lines may fail when the terminal can't do it. - */ - if (i > 0) - check_for_delay(FALSE); - if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK) { + if (i < wp->w_grid.Rows - 2) { // less than a screen off + // Try to insert the correct number of lines. + // If not the last window, delete the lines at the bottom. + // win_ins_lines may fail when the terminal can't do it. + if (win_ins_lines(wp, 0, i) == OK) { if (wp->w_lines_valid != 0) { /* Need to update rows that are new, stop at the * first one that scrolled down. */ @@ -883,12 +829,15 @@ static void win_update(win_T *wp) /* Move the entries that were scrolled, disable * the entries for the lines to be redrawn. */ - if ((wp->w_lines_valid += j) > wp->w_height) - wp->w_lines_valid = wp->w_height; - for (idx = wp->w_lines_valid; idx - j >= 0; idx--) + if ((wp->w_lines_valid += j) > wp->w_grid.Rows) { + wp->w_lines_valid = wp->w_grid.Rows; + } + for (idx = wp->w_lines_valid; idx - j >= 0; idx--) { wp->w_lines[idx] = wp->w_lines[idx - j]; - while (idx >= 0) - wp->w_lines[idx--].wl_valid = FALSE; + } + while (idx >= 0) { + wp->w_lines[idx--].wl_valid = false; + } } } else mid_start = 0; /* redraw all lines */ @@ -932,11 +881,11 @@ static void win_update(win_T *wp) /* ... but don't delete new filler lines. */ row -= wp->w_topfill; if (row > 0) { - check_for_delay(FALSE); - if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK) - bot_start = wp->w_height - row; - else - mid_start = 0; /* redraw all lines */ + if (win_del_lines(wp, 0, row) == OK) { + bot_start = wp->w_grid.Rows - row; + } else { + mid_start = 0; // redraw all lines + } } if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0) { /* @@ -952,7 +901,7 @@ static void win_update(win_T *wp) /* stop at line that didn't fit, unless it is still * valid (no lines deleted) */ if (row > 0 && bot_start + row - + (int)wp->w_lines[j].wl_size > wp->w_height) { + + (int)wp->w_lines[j].wl_size > wp->w_grid.Rows) { wp->w_lines_valid = idx + 1; break; } @@ -968,45 +917,26 @@ static void win_update(win_T *wp) * when it won't get updated below. */ if (wp->w_p_diff && bot_start > 0) wp->w_lines[0].wl_size = - plines_win_nofill(wp, wp->w_topline, TRUE) + plines_win_nofill(wp, wp->w_topline, true) + wp->w_topfill; } } } - /* When starting redraw in the first line, redraw all lines. When - * there is only one window it's probably faster to clear the screen - * first. */ + // When starting redraw in the first line, redraw all lines. if (mid_start == 0) { - mid_end = wp->w_height; - if (lastwin == firstwin) { - /* Clear the screen when it was not done by win_del_lines() or - * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE - * then. */ - if (screen_cleared != TRUE) - screenclear(); - /* The screen was cleared, redraw the tab pages line. */ - if (redraw_tabline) - draw_tabline(); - } - } - - /* When win_del_lines() or win_ins_lines() caused the screen to be - * cleared (only happens for the first window) or when screenclear() - * was called directly above, "must_redraw" will have been set to - * NOT_VALID, need to reset it here to avoid redrawing twice. */ - if (screen_cleared == TRUE) - must_redraw = 0; + mid_end = wp->w_grid.Rows; + } } else { /* Not VALID or INVERTED: redraw all lines. */ mid_start = 0; - mid_end = wp->w_height; + mid_end = wp->w_grid.Rows; } if (type == SOME_VALID) { /* SOME_VALID: redraw all lines. */ mid_start = 0; - mid_end = wp->w_height; + mid_end = wp->w_grid.Rows; type = NOT_VALID; } @@ -1016,14 +946,10 @@ static void win_update(win_T *wp) linenr_T from, to; if (VIsual_active) { - if (VIsual_active - && (VIsual_mode != wp->w_old_visual_mode - || type == INVERTED_ALL)) { - /* - * If the type of Visual selection changed, redraw the whole - * selection. Also when the ownership of the X selection is - * gained or lost. - */ + if (VIsual_mode != wp->w_old_visual_mode || type == INVERTED_ALL) { + // If the type of Visual selection changed, redraw the whole + // selection. Also when the ownership of the X selection is + // gained or lost. if (curwin->w_cursor.lnum < VIsual.lnum) { from = curwin->w_cursor.lnum; to = VIsual.lnum; @@ -1155,8 +1081,8 @@ static void win_update(win_T *wp) ++lnum; } srow += mid_start; - mid_end = wp->w_height; - for (; idx < wp->w_lines_valid; ++idx) { /* find end */ + mid_end = wp->w_grid.Rows; + for (; idx < wp->w_lines_valid; idx++) { // find end if (wp->w_lines[idx].wl_valid && wp->w_lines[idx].wl_lnum >= to + 1) { /* Only update until first row of this line */ @@ -1196,8 +1122,8 @@ static void win_update(win_T *wp) for (;; ) { /* stop updating when reached the end of the window (check for _past_ * the end of the window is at the end of the loop) */ - if (row == wp->w_height) { - didline = TRUE; + if (row == wp->w_grid.Rows) { + didline = true; break; } @@ -1211,15 +1137,13 @@ static void win_update(win_T *wp) * with. It is used further down when the line doesn't fit. */ srow = row; - /* - * Update a line when it is in an area that needs updating, when it - * has changes or w_lines[idx] is invalid. - * bot_start may be halfway through a wrapped line after using - * win_del_lines(), check if the current line includes it. - * When syntax folding is being used, the saved syntax states will - * already have been updated, we can't see where the syntax state is - * the same again, just update until the end of the window. - */ + // Update a line when it is in an area that needs updating, when it + // has changes or w_lines[idx] is invalid. + // "bot_start" may be halfway a wrapped line after using + // win_del_lines(), check if the current line includes it. + // When syntax folding is being used, the saved syntax states will + // already have been updated, we can't see where the syntax state is + // the same again, just update until the end of the window. if (row < top_end || (row >= mid_start && row < mid_end) || top_to_mod @@ -1288,17 +1212,17 @@ static void win_update(win_T *wp) /* Able to count old number of rows: Count new window * rows, and may insert/delete lines */ j = idx; - for (l = lnum; l < mod_bot; ++l) { - if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL)) - ++new_rows; - else if (l == wp->w_topline) - new_rows += plines_win_nofill(wp, l, TRUE) - + wp->w_topfill; - else - new_rows += plines_win(wp, l, TRUE); - ++j; - if (new_rows > wp->w_height - row - 2) { - /* it's getting too much, must redraw the rest */ + for (l = lnum; l < mod_bot; l++) { + if (hasFoldingWin(wp, l, NULL, &l, true, NULL)) { + new_rows++; + } else if (l == wp->w_topline) { + new_rows += plines_win_nofill(wp, l, true) + wp->w_topfill; + } else { + new_rows += plines_win(wp, l, true); + } + j++; + if (new_rows > wp->w_grid.Rows - row - 2) { + // it's getting too much, must redraw the rest new_rows = 9999; break; } @@ -1309,31 +1233,29 @@ static void win_update(win_T *wp) * remaining text or scrolling fails, must redraw the * rest. If scrolling works, must redraw the text * below the scrolled text. */ - if (row - xtra_rows >= wp->w_height - 2) + if (row - xtra_rows >= wp->w_grid.Rows - 2) { mod_bot = MAXLNUM; - else { - check_for_delay(FALSE); - if (win_del_lines(wp, row, - -xtra_rows, FALSE, FALSE) == FAIL) + } else { + if (win_del_lines(wp, row, -xtra_rows) == FAIL) { mod_bot = MAXLNUM; - else - bot_start = wp->w_height + xtra_rows; + } else { + bot_start = wp->w_grid.Rows + xtra_rows; + } } } else if (xtra_rows > 0) { /* May scroll text down. If there is not enough * remaining text of scrolling fails, must redraw the * rest. */ - if (row + xtra_rows >= wp->w_height - 2) + if (row + xtra_rows >= wp->w_grid.Rows - 2) { mod_bot = MAXLNUM; - else { - check_for_delay(FALSE); - if (win_ins_lines(wp, row + old_rows, - xtra_rows, FALSE, FALSE) == FAIL) + } else { + if (win_ins_lines(wp, row + old_rows, xtra_rows) == FAIL) { mod_bot = MAXLNUM; - else if (top_end > row + old_rows) - /* Scrolled the part at the top that requires - * updating down. */ + } else if (top_end > row + old_rows) { + // Scrolled the part at the top that requires + // updating down. top_end += xtra_rows; + } } } @@ -1353,7 +1275,7 @@ static void win_update(win_T *wp) wp->w_lines[j] = wp->w_lines[i]; /* stop at a line that won't fit */ if (x + (int)wp->w_lines[j].wl_size - > wp->w_height) { + > wp->w_grid.Rows) { wp->w_lines_valid = j + 1; break; } @@ -1366,10 +1288,12 @@ static void win_update(win_T *wp) /* move entries in w_lines[] downwards */ j -= i; wp->w_lines_valid += j; - if (wp->w_lines_valid > wp->w_height) - wp->w_lines_valid = wp->w_height; - for (i = wp->w_lines_valid; i - j >= idx; --i) + if (wp->w_lines_valid > wp->w_grid.Rows) { + wp->w_lines_valid = wp->w_grid.Rows; + } + for (i = wp->w_lines_valid; i - j >= idx; i--) { wp->w_lines[i] = wp->w_lines[i - j]; + } /* The w_lines[] entries for inserted lines are * now invalid, but wl_size may be used above. @@ -1400,13 +1324,13 @@ static void win_update(win_T *wp) && wp->w_lines[idx].wl_valid && wp->w_lines[idx].wl_lnum == lnum && lnum > wp->w_topline - && !(dy_flags & DY_LASTLINE) - && srow + wp->w_lines[idx].wl_size > wp->w_height + && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE)) + && srow + wp->w_lines[idx].wl_size > wp->w_grid.Rows && diff_check_fill(wp, lnum) == 0 ) { /* This line is not going to fit. Don't draw anything here, * will draw "@ " lines below. */ - row = wp->w_height + 1; + row = wp->w_grid.Rows + 1; } else { prepare_search_hl(wp, lnum); /* Let the syntax stuff know we skipped a few lines. */ @@ -1417,7 +1341,7 @@ static void win_update(win_T *wp) /* * Display one line. */ - row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0); + row = win_line(wp, lnum, srow, wp->w_grid.Rows, mod_top == 0, false); wp->w_lines[idx].wl_folded = FALSE; wp->w_lines[idx].wl_lastlnum = lnum; @@ -1426,12 +1350,14 @@ static void win_update(win_T *wp) } wp->w_lines[idx].wl_lnum = lnum; - wp->w_lines[idx].wl_valid = TRUE; - if (row > wp->w_height) { /* past end of screen */ - /* we may need the size of that too long line later on */ - if (dollar_vcol == -1) - wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE); - ++idx; + wp->w_lines[idx].wl_valid = true; + + if (row > wp->w_grid.Rows) { // past end of grid + // we may need the size of that too long line later on + if (dollar_vcol == -1) { + wp->w_lines[idx].wl_size = plines_win(wp, lnum, true); + } + idx++; break; } if (dollar_vcol == -1) @@ -1439,10 +1365,22 @@ static void win_update(win_T *wp) ++idx; lnum += fold_count + 1; } else { - /* This line does not need updating, advance to the next one */ + if (wp->w_p_rnu) { + // 'relativenumber' set: The text doesn't need to be drawn, but + // the number column nearly always does. + fold_count = foldedCount(wp, lnum, &win_foldinfo); + if (fold_count != 0) { + fold_line(wp, fold_count, &win_foldinfo, lnum, row); + } else { + (void)win_line(wp, lnum, srow, wp->w_grid.Rows, true, true); + } + } + + // This line does not need to be drawn, advance to the next one. row += wp->w_lines[idx++].wl_size; - if (row > wp->w_height) /* past end of screen */ + if (row > wp->w_grid.Rows) { // past end of screen break; + } lnum = wp->w_lines[idx - 1].wl_lastlnum + 1; did_update = DID_NONE; } @@ -1473,33 +1411,40 @@ static void win_update(win_T *wp) wp->w_empty_rows = 0; wp->w_filler_rows = 0; if (!eof && !didline) { + int at_attr = hl_combine_attr(wp->w_hl_attr_normal, + win_hl_attr(wp, HLF_AT)); if (lnum == wp->w_topline) { /* * Single line that does not fit! * Don't overwrite it, it can be edited. */ wp->w_botline = lnum + 1; - } else if (diff_check_fill(wp, lnum) >= wp->w_height - srow) { - /* Window ends in filler lines. */ + } else if (diff_check_fill(wp, lnum) >= wp->w_grid.Rows - srow) { + // Window ends in filler lines. wp->w_botline = lnum; - wp->w_filler_rows = wp->w_height - srow; - } else if (dy_flags & DY_LASTLINE) { /* 'display' has "lastline" */ - /* - * Last line isn't finished: Display "@@@" at the end. - */ - screen_fill(wp->w_winrow + wp->w_height - 1, - wp->w_winrow + wp->w_height, - W_ENDCOL(wp) - 3, W_ENDCOL(wp), - '@', '@', hl_attr(HLF_AT)); + wp->w_filler_rows = wp->w_grid.Rows - srow; + } else if (dy_flags & DY_TRUNCATE) { // 'display' has "truncate" + int scr_row = wp->w_grid.Rows - 1; + + // Last line isn't finished: Display "@@@" in the last screen line. + grid_puts_len(&wp->w_grid, (char_u *)"@@", 2, scr_row, 0, at_attr); + + grid_fill(&wp->w_grid, scr_row, scr_row + 1, 2, (int)wp->w_grid.Columns, + '@', ' ', at_attr); + set_empty_rows(wp, srow); + wp->w_botline = lnum; + } else if (dy_flags & DY_LASTLINE) { // 'display' has "lastline" + // Last line isn't finished: Display "@@@" at the end. + grid_fill(&wp->w_grid, wp->w_grid.Rows - 1, wp->w_grid.Rows, + wp->w_grid.Columns - 3, wp->w_grid.Columns, '@', '@', at_attr); set_empty_rows(wp, srow); wp->w_botline = lnum; } else { - win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT); + win_draw_end(wp, '@', ' ', srow, wp->w_grid.Rows, HLF_AT); wp->w_botline = lnum; } } else { - draw_vsep_win(wp, row); - if (eof) { /* we hit the end of the file */ + if (eof) { // we hit the end of the file wp->w_botline = buf->b_ml.ml_line_count + 1; j = diff_check_fill(wp, wp->w_botline); if (j > 0 && !wp->w_botfill) { @@ -1510,17 +1455,22 @@ static void win_update(win_T *wp) i = '-'; else i = fill_diff; - if (row + j > wp->w_height) - j = wp->w_height - row; + if (row + j > wp->w_grid.Rows) { + j = wp->w_grid.Rows - row; + } win_draw_end(wp, i, i, row, row + (int)j, HLF_DED); row += j; } } else if (dollar_vcol == -1) wp->w_botline = lnum; - /* make sure the rest of the screen is blank */ - /* put '~'s on rows that aren't part of the file. */ - win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB); + // make sure the rest of the screen is blank + // write the 'fill_eob' character to rows that aren't part of the file. + win_draw_end(wp, fill_eob, ' ', row, wp->w_grid.Rows, HLF_EOB); + } + + if (wp->w_redr_type >= REDRAW_TOP) { + draw_vsep_win(wp, 0); } /* Reset the type of redrawing required, the window has been updated. */ @@ -1563,6 +1513,20 @@ static void win_update(win_T *wp) got_int = save_got_int; } +/// Returns width of the signcolumn that should be used for the whole window +/// +/// @param wp window we want signcolumn width from +/// @return max width of signcolumn (cell unit) +/// +/// @note Returns a constant for now but hopefully we can improve neovim so that +/// the returned value width adapts to the maximum number of marks to draw +/// for the window +/// TODO(teto) +int win_signcol_width(win_T *wp) +{ + // 2 is vim default value + return 2; +} /* * Clear the rest of the window and mark the unused lines with "c1". use "c2" @@ -1574,77 +1538,74 @@ static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T h # define FDC_OFF n int fdc = compute_foldcolumn(wp, 0); + int attr = hl_combine_attr(wp->w_hl_attr_normal, win_hl_attr(wp, hl)); + if (wp->w_p_rl) { // No check for cmdline window: should never be right-left. n = fdc; if (n > 0) { - /* draw the fold column at the right */ - if (n > wp->w_width) - n = wp->w_width; - screen_fill(wp->w_winrow + row, wp->w_winrow + endrow, - W_ENDCOL(wp) - n, W_ENDCOL(wp), - ' ', ' ', hl_attr(HLF_FC)); + // draw the fold column at the right + if (n > wp->w_grid.Columns) { + n = wp->w_grid.Columns; + } + grid_fill(&wp->w_grid, row, endrow, wp->w_grid.Columns - n, + wp->w_grid.Columns, ' ', ' ', win_hl_attr(wp, HLF_FC)); } - if (draw_signcolumn(wp)) { - int nn = n + 2; + if (signcolumn_on(wp)) { + int nn = n + win_signcol_width(wp); - /* draw the sign column left of the fold column */ - if (nn > wp->w_width) { - nn = wp->w_width; + // draw the sign column left of the fold column + if (nn > wp->w_grid.Columns) { + nn = wp->w_grid.Columns; } - screen_fill(wp->w_winrow + row, wp->w_winrow + endrow, - W_ENDCOL(wp) - nn, W_ENDCOL(wp) - n, - ' ', ' ', hl_attr(HLF_SC)); + grid_fill(&wp->w_grid, row, endrow, wp->w_grid.Columns - nn, + wp->w_grid.Columns - n, ' ', ' ', win_hl_attr(wp, HLF_SC)); n = nn; } - screen_fill(wp->w_winrow + row, wp->w_winrow + endrow, - wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF, - c2, c2, hl_attr(hl)); - screen_fill(wp->w_winrow + row, wp->w_winrow + endrow, - W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF, - c1, c2, hl_attr(hl)); + grid_fill(&wp->w_grid, row, endrow, 0, wp->w_grid.Columns - 1 - FDC_OFF, + c2, c2, attr); + grid_fill(&wp->w_grid, row, endrow, + wp->w_grid.Columns - 1 - FDC_OFF, wp->w_grid.Columns - FDC_OFF, + c1, c2, attr); } else { if (cmdwin_type != 0 && wp == curwin) { /* draw the cmdline character in the leftmost column */ n = 1; - if (n > wp->w_width) - n = wp->w_width; - screen_fill(wp->w_winrow + row, wp->w_winrow + endrow, - wp->w_wincol, wp->w_wincol + n, - cmdwin_type, ' ', hl_attr(HLF_AT)); + if (n > wp->w_grid.Columns) { + n = wp->w_grid.Columns; + } + grid_fill(&wp->w_grid, row, endrow, 0, n, cmdwin_type, ' ', + win_hl_attr(wp, HLF_AT)); } if (fdc > 0) { int nn = n + fdc; - /* draw the fold column at the left */ - if (nn > wp->w_width) - nn = wp->w_width; - screen_fill(wp->w_winrow + row, wp->w_winrow + endrow, - wp->w_wincol + n, wp->w_wincol + nn, - ' ', ' ', hl_attr(HLF_FC)); + // draw the fold column at the left + if (nn > wp->w_grid.Columns) { + nn = wp->w_grid.Columns; + } + grid_fill(&wp->w_grid, row, endrow, n, nn, ' ', ' ', + win_hl_attr(wp, HLF_FC)); n = nn; } - if (draw_signcolumn(wp)) - { - int nn = n + 2; + if (signcolumn_on(wp)) { + int nn = n + win_signcol_width(wp); - /* draw the sign column after the fold column */ - if (nn > wp->w_width) { - nn = wp->w_width; + // draw the sign column after the fold column + if (nn > wp->w_grid.Columns) { + nn = wp->w_grid.Columns; } - screen_fill(wp->w_winrow + row, wp->w_winrow + endrow, - wp->w_wincol + n, wp->w_wincol + nn, - ' ', ' ', hl_attr(HLF_SC)); + grid_fill(&wp->w_grid, row, endrow, n, nn, ' ', ' ', + win_hl_attr(wp, HLF_SC)); n = nn; } - screen_fill(wp->w_winrow + row, wp->w_winrow + endrow, - wp->w_wincol + FDC_OFF, W_ENDCOL(wp), - c1, c2, hl_attr(hl)); + grid_fill(&wp->w_grid, row, endrow, FDC_OFF, wp->w_grid.Columns, c1, c2, + attr); } set_empty_rows(wp, row); } @@ -1666,7 +1627,7 @@ static int compute_foldcolumn(win_T *wp, int col) { int fdc = wp->w_p_fdc; int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw; - int wwidth = wp->w_width; + int wwidth = wp->w_grid.Columns; if (fdc > wwidth - (col + wmw)) { fdc = wwidth - (col + wmw); @@ -1674,12 +1635,62 @@ static int compute_foldcolumn(win_T *wp, int col) return fdc; } +/// Put a single char from an UTF-8 buffer into a line buffer. +/// +/// Handles composing chars and arabic shaping state. +static int line_putchar(LineState *s, schar_T *dest, int maxcells, bool rl) +{ + const char_u *p = s->p; + int cells = utf_ptr2cells(p); + int c_len = utfc_ptr2len(p); + int u8c, u8cc[MAX_MCO]; + if (cells > maxcells) { + return -1; + } + u8c = utfc_ptr2char(p, u8cc); + if (*p < 0x80 && u8cc[0] == 0) { + schar_from_ascii(dest[0], *p); + s->prev_c = u8c; + } else { + if (p_arshape && !p_tbidi && arabic_char(u8c)) { + // Do Arabic shaping. + int pc, pc1, nc; + int pcc[MAX_MCO]; + int firstbyte = *p; + + // The idea of what is the previous and next + // character depends on 'rightleft'. + if (rl) { + pc = s->prev_c; + pc1 = s->prev_c1; + nc = utf_ptr2char(p + c_len); + s->prev_c1 = u8cc[0]; + } else { + pc = utfc_ptr2char(p + c_len, pcc); + nc = s->prev_c; + pc1 = pcc[0]; + } + s->prev_c = u8c; + + u8c = arabic_shape(u8c, &firstbyte, &u8cc[0], pc, pc1, nc); + } else { + s->prev_c = u8c; + } + schar_from_cc(dest[0], u8c, u8cc); + } + if (cells > 1) { + dest[1][0] = 0; + } + s->p += c_len; + return cells; +} + /* * Display one folded line. */ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row) { - char_u buf[51]; + char_u buf[FOLD_TEXT_LEN]; pos_T *top, *bot; linenr_T lnume = lnum + fold_count - 1; int len; @@ -1687,7 +1698,7 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T int fdc; int col; int txtcol; - int off = (int)(current_ScreenLine - ScreenLines); + int off; int ri; /* Build the fold line: @@ -1699,17 +1710,16 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T * 6. set highlighting for the Visual area an other text */ col = 0; + off = 0; /* * 1. Add the cmdwin_type for the command-line window * Ignores 'rightleft', this window is never right-left. */ if (cmdwin_type != 0 && wp == curwin) { - ScreenLines[off] = cmdwin_type; - ScreenAttrs[off] = hl_attr(HLF_AT); - if (enc_utf8) - ScreenLinesUC[off] = 0; - ++col; + schar_from_ascii(linebuf_char[off], cmdwin_type); + linebuf_attr[off] = win_hl_attr(wp, HLF_AT); + col++; } // 2. Add the 'foldcolumn' @@ -1720,35 +1730,43 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T if (wp->w_p_rl) { int i; - copy_text_attr(off + wp->w_width - fdc - col, buf, fdc, - hl_attr(HLF_FC)); - /* reverse the fold column */ - for (i = 0; i < fdc; ++i) - ScreenLines[off + wp->w_width - i - 1 - col] = buf[i]; - } else - copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC)); + copy_text_attr(off + wp->w_grid.Columns - fdc - col, buf, fdc, + win_hl_attr(wp, HLF_FC)); + // reverse the fold column + for (i = 0; i < fdc; i++) { + schar_from_ascii(linebuf_char[off + wp->w_grid.Columns - i - 1 - col], + buf[i]); + } + } else { + copy_text_attr(off + col, buf, fdc, win_hl_attr(wp, HLF_FC)); + } col += fdc; } -# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \ - for (ri = 0; ri < l; ++ri) \ - ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \ - else \ - for (ri = 0; ri < l; ++ri) \ - ScreenAttrs[off + (p) + ri] = v +# define RL_MEMSET(p, v, l) if (wp->w_p_rl) { \ + for (ri = 0; ri < l; ri++) { \ + linebuf_attr[off + (wp->w_grid.Columns - (p) - (l)) + ri] = v; \ + } \ + } else { \ + for (ri = 0; ri < l; ri++) { \ + linebuf_attr[off + (p) + ri] = v; \ + } \ + } /* Set all attributes of the 'number' or 'relativenumber' column and the * text */ - RL_MEMSET(col, hl_attr(HLF_FL), wp->w_width - col); + RL_MEMSET(col, win_hl_attr(wp, HLF_FL), wp->w_grid.Columns - col); - /* If signs are being displayed, add two spaces. */ - if (draw_signcolumn(wp)) { - len = wp->w_width - col; + // If signs are being displayed, add spaces. + if (signcolumn_on(wp)) { + len = wp->w_grid.Columns - col; if (len > 0) { - if (len > 2) { - len = 2; + int len_max = win_signcol_width(wp); + if (len > len_max) { + len = len_max; } - copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL)); + copy_text_attr(off + col, (char_u *)" ", len, + win_hl_attr(wp, HLF_FL)); col += len; } } @@ -1757,7 +1775,7 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T * 3. Add the 'number' or 'relativenumber' column */ if (wp->w_p_nu || wp->w_p_rnu) { - len = wp->w_width - col; + len = wp->w_grid.Columns - col; if (len > 0) { int w = number_width(wp); long num; @@ -1780,13 +1798,14 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T } } - sprintf((char *)buf, fmt, w, num); - if (wp->w_p_rl) - /* the line number isn't reversed */ - copy_text_attr(off + wp->w_width - len - col, buf, len, - hl_attr(HLF_FL)); - else - copy_text_attr(off + col, buf, len, hl_attr(HLF_FL)); + snprintf((char *)buf, FOLD_TEXT_LEN, fmt, w, num); + if (wp->w_p_rl) { + // the line number isn't reversed + copy_text_attr(off + wp->w_grid.Columns - len - col, buf, len, + win_hl_attr(wp, HLF_FL)); + } else { + copy_text_attr(off + col, buf, len, win_hl_attr(wp, HLF_FL)); + } col += len; } } @@ -1798,113 +1817,43 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T txtcol = col; /* remember where text starts */ - /* - * 5. move the text to current_ScreenLine. Fill up with "fill_fold". - * Right-left text is put in columns 0 - number-col, normal text is put - * in columns number-col - window-width. - */ - if (has_mbyte) { - int cells; - int u8c, u8cc[MAX_MCO]; - int i; - int idx; - int c_len; - char_u *p; - int prev_c = 0; /* previous Arabic character */ - int prev_c1 = 0; /* first composing char for prev_c */ - - if (wp->w_p_rl) - idx = off; - else - idx = off + col; - - /* Store multibyte characters in ScreenLines[] et al. correctly. */ - for (p = text; *p != NUL; ) { - cells = (*mb_ptr2cells)(p); - c_len = (*mb_ptr2len)(p); - if (col + cells > wp->w_width - - (wp->w_p_rl ? col : 0) - ) - break; - ScreenLines[idx] = *p; - if (enc_utf8) { - u8c = utfc_ptr2char(p, u8cc); - if (*p < 0x80 && u8cc[0] == 0) { - ScreenLinesUC[idx] = 0; - prev_c = u8c; - } else { - if (p_arshape && !p_tbidi && arabic_char(u8c)) { - /* Do Arabic shaping. */ - int pc, pc1, nc; - int pcc[MAX_MCO]; - int firstbyte = *p; - - /* The idea of what is the previous and next - * character depends on 'rightleft'. */ - if (wp->w_p_rl) { - pc = prev_c; - pc1 = prev_c1; - nc = utf_ptr2char(p + c_len); - prev_c1 = u8cc[0]; - } else { - pc = utfc_ptr2char(p + c_len, pcc); - nc = prev_c; - pc1 = pcc[0]; - } - prev_c = u8c; + // 5. move the text to linebuf_char[off]. Fill up with "fill_fold". + // Right-left text is put in columns 0 - number-col, normal text is put + // in columns number-col - window-width. + int idx; - u8c = arabic_shape(u8c, &firstbyte, &u8cc[0], - pc, pc1, nc); - ScreenLines[idx] = firstbyte; - } else - prev_c = u8c; - /* Non-BMP character: display as ? or fullwidth ?. */ - ScreenLinesUC[idx] = u8c; - for (i = 0; i < Screen_mco; ++i) { - ScreenLinesC[i][idx] = u8cc[i]; - if (u8cc[i] == 0) - break; - } - } - if (cells > 1) - ScreenLines[idx + 1] = 0; - } else if (enc_dbcs == DBCS_JPNU && *p == 0x8e) - /* double-byte single width character */ - ScreenLines2[idx] = p[1]; - else if (cells > 1) - /* double-width character */ - ScreenLines[idx + 1] = p[1]; - col += cells; - idx += cells; - p += c_len; - } + if (wp->w_p_rl) { + idx = off; } else { - len = (int)STRLEN(text); - if (len > wp->w_width - col) - len = wp->w_width - col; - if (len > 0) { - if (wp->w_p_rl) - STRNCPY(current_ScreenLine, text, len); - else - STRNCPY(current_ScreenLine + col, text, len); - col += len; + idx = off + col; + } + + LineState s = LINE_STATE(text); + + while (*s.p != NUL) { + // TODO(bfredl): cargo-culted from the old Vim code: + // if(col + cells > wp->w_width - (wp->w_p_rl ? col : 0)) { break; } + // This is obvious wrong. If Vim ever fixes this, solve for "cells" again + // in the correct condition. + int maxcells = wp->w_grid.Columns - col - (wp->w_p_rl ? col : 0); + int cells = line_putchar(&s, &linebuf_char[idx], maxcells, wp->w_p_rl); + if (cells == -1) { + break; } + col += cells; + idx += cells; } /* Fill the rest of the line with the fold filler */ if (wp->w_p_rl) col -= txtcol; - while (col < wp->w_width + + schar_T sc; + schar_from_char(sc, fill_fold); + while (col < wp->w_grid.Columns - (wp->w_p_rl ? txtcol : 0) ) { - if (enc_utf8) { - if (fill_fold >= 0x80) { - ScreenLinesUC[off + col] = fill_fold; - ScreenLinesC[0][off + col] = 0; - } else - ScreenLinesUC[off + col] = 0; - } - ScreenLines[off + col++] = fill_fold; + schar_copy(linebuf_char[off+col++], sc); } if (text != buf) @@ -1936,20 +1885,21 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE))))))) { if (VIsual_mode == Ctrl_V) { - /* Visual block mode: highlight the chars part of the block */ - if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width) { + // Visual block mode: highlight the chars part of the block + if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_grid.Columns) { if (wp->w_old_cursor_lcol != MAXCOL && wp->w_old_cursor_lcol + txtcol - < (colnr_T)wp->w_width) + < (colnr_T)wp->w_grid.Columns) { len = wp->w_old_cursor_lcol; - else - len = wp->w_width - txtcol; - RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V), - len - (int)wp->w_old_cursor_fcol); + } else { + len = wp->w_grid.Columns - txtcol; + } + RL_MEMSET(wp->w_old_cursor_fcol + txtcol, win_hl_attr(wp, HLF_V), + len - (int)wp->w_old_cursor_fcol); } } else { - /* Set all attributes of the text */ - RL_MEMSET(txtcol, hl_attr(HLF_V), wp->w_width - txtcol); + // Set all attributes of the text + RL_MEMSET(txtcol, win_hl_attr(wp, HLF_V), wp->w_grid.Columns - txtcol); } } } @@ -1967,9 +1917,9 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T } else { txtcol -= wp->w_leftcol; } - if (txtcol >= 0 && txtcol < wp->w_width) { - ScreenAttrs[off + txtcol] = - hl_combine_attr(ScreenAttrs[off + txtcol], hl_attr(HLF_MC)); + if (txtcol >= 0 && txtcol < wp->w_grid.Columns) { + linebuf_attr[off + txtcol] = + hl_combine_attr(linebuf_attr[off + txtcol], win_hl_attr(wp, HLF_MC)); } txtcol = old_txtcol; j = wp->w_p_cc_cols[++i]; @@ -1983,13 +1933,14 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T txtcol -= wp->w_skipcol; else txtcol -= wp->w_leftcol; - if (txtcol >= 0 && txtcol < wp->w_width) - ScreenAttrs[off + txtcol] = hl_combine_attr( - ScreenAttrs[off + txtcol], hl_attr(HLF_CUC)); + if (txtcol >= 0 && txtcol < wp->w_grid.Columns) { + linebuf_attr[off + txtcol] = hl_combine_attr( + linebuf_attr[off + txtcol], win_hl_attr(wp, HLF_CUC)); + } } - SCREEN_LINE(row + wp->w_winrow, wp->w_wincol, wp->w_width, - wp->w_width, FALSE); + grid_put_linebuf(&wp->w_grid, row, 0, wp->w_grid.Columns, wp->w_grid.Columns, + false, wp, wp->w_hl_attr_normal, false); /* * Update w_cline_height and w_cline_folded if the cursor line was @@ -2002,21 +1953,22 @@ static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T curwin->w_cline_height = 1; curwin->w_cline_folded = true; curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW); + conceal_cursor_used = conceal_cursor_line(curwin); } } -/* - * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr". - */ + +/// Copy "buf[len]" to linebuf_char["off"] and set attributes to "attr". +/// +/// Only works for ASCII text! static void copy_text_attr(int off, char_u *buf, int len, int attr) { int i; - memmove(ScreenLines + off, buf, (size_t)len); - if (enc_utf8) - memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len); - for (i = 0; i < len; ++i) - ScreenAttrs[off + i] = attr; + for (i = 0; i < len; i++) { + schar_from_ascii(linebuf_char[off + i], buf[i]); + linebuf_attr[off + i] = attr; + } } /* @@ -2086,19 +2038,18 @@ win_line ( linenr_T lnum, int startrow, int endrow, - bool nochange /* not updating for changed text */ + bool nochange, // not updating for changed text + bool number_only // only update the number column ) { - int col; /* visual column on screen */ - unsigned off; /* offset in ScreenLines/ScreenAttrs */ - int c = 0; /* init for GCC */ - long vcol = 0; /* virtual column (for tabs) */ + int c = 0; // init for GCC + long vcol = 0; // virtual column (for tabs) long vcol_sbr = -1; // virtual column after showbreak - long vcol_prev = -1; /* "vcol" of previous character */ - char_u *line; /* current line */ - char_u *ptr; /* current position in "line" */ - int row; /* row in the window, excl w_winrow */ - int screen_row; /* row on the screen, incl w_winrow */ + long vcol_prev = -1; // "vcol" of previous character + char_u *line; // current line + char_u *ptr; // current position in "line" + int row; // row in the window, excl w_winrow + ScreenGrid *grid = &wp->w_grid; // grid specfic to the window char_u extra[18]; /* line number and 'fdc' must fit in here */ int n_extra = 0; /* number of extra chars */ @@ -2124,11 +2075,11 @@ win_line ( int n_skip = 0; /* nr of chars to skip for 'nowrap' */ - int fromcol, tocol; /* start/end of inverting */ - int fromcol_prev = -2; /* start of inverting after cursor */ - int noinvcur = FALSE; /* don't invert the cursor */ - pos_T *top, *bot; - int lnum_in_visual_area = FALSE; + int fromcol = 0, tocol = 0; // start/end of inverting + int fromcol_prev = -2; // start of inverting after cursor + int noinvcur = false; // don't invert the cursor + pos_T *top, *bot; + int lnum_in_visual_area = false; pos_T pos; long v; @@ -2143,10 +2094,10 @@ win_line ( int syntax_attr = 0; /* attributes desired by syntax */ int has_syntax = FALSE; /* this buffer has syntax highl. */ int save_did_emsg; - int eol_hl_off = 0; /* 1 if highlighted char after EOL */ - int draw_color_col = FALSE; /* highlight colorcolumn */ - int *color_cols = NULL; /* pointer to according columns array */ - bool has_spell = false; /* this buffer has spell checking */ + int eol_hl_off = 0; // 1 if highlighted char after EOL + int draw_color_col = false; // highlight colorcolumn + int *color_cols = NULL; // pointer to according columns array + bool has_spell = false; // this buffer has spell checking # define SPWORDLEN 150 char_u nextline[SPWORDLEN * 2]; /* text with start of the next line */ int nextlinecol = 0; /* column where nextline[] starts */ @@ -2157,37 +2108,38 @@ win_line ( static linenr_T checked_lnum = 0; /* line number for "checked_col" */ static int checked_col = 0; /* column in "checked_lnum" up to which * there are no spell errors */ - static int cap_col = -1; /* column to check for Cap word */ - static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */ - int cur_checked_col = 0; /* checked column for current line */ - int extra_check; /* has syntax or linebreak */ - int multi_attr = 0; /* attributes desired by multibyte */ - int mb_l = 1; /* multi-byte byte length */ - int mb_c = 0; /* decoded multi-byte character */ - int mb_utf8 = FALSE; /* screen char is UTF-8 char */ - int u8cc[MAX_MCO]; /* composing UTF-8 chars */ - int filler_lines; /* nr of filler lines to be drawn */ - int filler_todo; /* nr of filler lines still to do + 1 */ - hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */ - int change_start = MAXCOL; /* first col of changed area */ - int change_end = -1; /* last col of changed area */ - colnr_T trailcol = MAXCOL; /* start of trailing spaces */ - int need_showbreak = FALSE; - int line_attr = 0; /* attribute for the whole line */ - matchitem_T *cur; /* points to the match list */ - match_T *shl; /* points to search_hl or a match */ - int shl_flag; /* flag to indicate whether search_hl - has been processed or not */ - int prevcol_hl_flag; /* flag to indicate whether prevcol - equals startcol of search_hl or one - of the matches */ - int prev_c = 0; /* previous Arabic character */ - int prev_c1 = 0; /* first composing char for prev_c */ - int did_line_attr = 0; - - bool has_bufhl = false; // this buffer has highlight matches - int bufhl_attr = 0; // attributes desired by bufhl - bufhl_lineinfo_T bufhl_info; // bufhl data for this line + static int cap_col = -1; // column to check for Cap word + static linenr_T capcol_lnum = 0; // line number where "cap_col" + int cur_checked_col = 0; // checked column for current line + int extra_check = 0; // has syntax or linebreak + int multi_attr = 0; // attributes desired by multibyte + int mb_l = 1; // multi-byte byte length + int mb_c = 0; // decoded multi-byte character + bool mb_utf8 = false; // screen char is UTF-8 char + int u8cc[MAX_MCO]; // composing UTF-8 chars + int filler_lines; // nr of filler lines to be drawn + int filler_todo; // nr of filler lines still to do + 1 + hlf_T diff_hlf = (hlf_T)0; // type of diff highlighting + int change_start = MAXCOL; // first col of changed area + int change_end = -1; // last col of changed area + colnr_T trailcol = MAXCOL; // start of trailing spaces + int need_showbreak = false; // overlong line, skip first x chars + int line_attr = 0; // attribute for the whole line + int line_attr_lowprio = 0; // low-priority attribute for the line + matchitem_T *cur; // points to the match list + match_T *shl; // points to search_hl or a match + int shl_flag; // flag to indicate whether search_hl + // has been processed or not + bool prevcol_hl_flag; // flag to indicate whether prevcol + // equals startcol of search_hl or one + // of the matches + int prev_c = 0; // previous Arabic character + int prev_c1 = 0; // first composing char for prev_c + + bool search_attr_from_match = false; // if search_attr is from :match + BufhlLineInfo bufhl_info; // bufhl data for this line + bool has_bufhl = false; // this buffer has highlight matches + bool do_virttext = false; // draw virtual text for this line /* draw_state: items that are drawn in sequence: */ #define WL_START 0 /* nothing done yet */ @@ -2203,14 +2155,14 @@ win_line ( int syntax_flags = 0; int syntax_seqnr = 0; int prev_syntax_id = 0; - int conceal_attr = hl_attr(HLF_CONCEAL); + int conceal_attr = win_hl_attr(wp, HLF_CONCEAL); int is_concealing = false; int boguscols = 0; ///< nonexistent columns added to ///< force wrapping int vcol_off = 0; ///< offset for concealed characters int did_wcol = false; - int match_conc = false; ///< cchar for match functions - int has_match_conc = false; ///< match wants to conceal + int match_conc = 0; ///< cchar for match functions + int has_match_conc = 0; ///< match wants to conceal int old_boguscols = 0; # define VCOL_HLC (vcol - vcol_off) # define FIX_FOR_BOGUSCOLS \ @@ -2227,158 +2179,167 @@ win_line ( return startrow; row = startrow; - screen_row = row + wp->w_winrow; - /* - * To speed up the loop below, set extra_check when there is linebreak, - * trailing white space and/or syntax processing to be done. - */ - extra_check = wp->w_p_lbr; - if (syntax_present(wp) && !wp->w_s->b_syn_error) { - /* Prepare for syntax highlighting in this line. When there is an - * error, stop syntax highlighting. */ - save_did_emsg = did_emsg; - did_emsg = FALSE; - syntax_start(wp, lnum); - if (did_emsg) - wp->w_s->b_syn_error = TRUE; - else { - did_emsg = save_did_emsg; - has_syntax = TRUE; - extra_check = TRUE; - } - } - - if (bufhl_start_line(wp->w_buffer, lnum, &bufhl_info)) { - has_bufhl = true; - extra_check = true; - } + if (!number_only) { + // To speed up the loop below, set extra_check when there is linebreak, + // trailing white space and/or syntax processing to be done. + extra_check = wp->w_p_lbr; + if (syntax_present(wp) && !wp->w_s->b_syn_error) { + // Prepare for syntax highlighting in this line. When there is an + // error, stop syntax highlighting. + save_did_emsg = did_emsg; + did_emsg = false; + syntax_start(wp, lnum); + if (did_emsg) { + wp->w_s->b_syn_error = true; + } else { + did_emsg = save_did_emsg; + has_syntax = true; + extra_check = true; + } + } - /* Check for columns to display for 'colorcolumn'. */ - color_cols = wp->w_buffer->terminal ? NULL : wp->w_p_cc_cols; - if (color_cols != NULL) - draw_color_col = advance_color_col(VCOL_HLC, &color_cols); - - if (wp->w_p_spell - && *wp->w_s->b_p_spl != NUL - && !GA_EMPTY(&wp->w_s->b_langp) - && *(char **)(wp->w_s->b_langp.ga_data) != NULL) { - /* Prepare for spell checking. */ - has_spell = true; - extra_check = TRUE; - - /* Get the start of the next line, so that words that wrap to the next - * line are found too: "et<line-break>al.". - * Trick: skip a few chars for C/shell/Vim comments */ - nextline[SPWORDLEN] = NUL; - if (lnum < wp->w_buffer->b_ml.ml_line_count) { - line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE); - spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN); - } - - /* When a word wrapped from the previous line the start of the current - * line is valid. */ - if (lnum == checked_lnum) - cur_checked_col = checked_col; - checked_lnum = 0; - - /* When there was a sentence end in the previous line may require a - * word starting with capital in this line. In line 1 always check - * the first word. */ - if (lnum != capcol_lnum) - cap_col = -1; - if (lnum == 1) - cap_col = 0; - capcol_lnum = 0; - } + if (bufhl_start_line(wp->w_buffer, lnum, &bufhl_info)) { + if (kv_size(bufhl_info.line->items)) { + has_bufhl = true; + extra_check = true; + } + if (kv_size(bufhl_info.line->virt_text)) { + do_virttext = true; + } + } - /* - * handle visual active in this window - */ - fromcol = -10; - tocol = MAXCOL; - if (VIsual_active && wp->w_buffer == curwin->w_buffer) { - /* Visual is after curwin->w_cursor */ - if (ltoreq(curwin->w_cursor, VIsual)) { - top = &curwin->w_cursor; - bot = &VIsual; - } else { /* Visual is before curwin->w_cursor */ - top = &VIsual; - bot = &curwin->w_cursor; + // Check for columns to display for 'colorcolumn'. + color_cols = wp->w_buffer->terminal ? NULL : wp->w_p_cc_cols; + if (color_cols != NULL) { + draw_color_col = advance_color_col(VCOL_HLC, &color_cols); } - lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum); - if (VIsual_mode == Ctrl_V) { /* block mode */ - if (lnum_in_visual_area) { - fromcol = wp->w_old_cursor_fcol; - tocol = wp->w_old_cursor_lcol; + + if (wp->w_p_spell + && *wp->w_s->b_p_spl != NUL + && !GA_EMPTY(&wp->w_s->b_langp) + && *(char **)(wp->w_s->b_langp.ga_data) != NULL) { + // Prepare for spell checking. + has_spell = true; + extra_check = true; + + // Get the start of the next line, so that words that wrap to the next + // line are found too: "et<line-break>al.". + // Trick: skip a few chars for C/shell/Vim comments + nextline[SPWORDLEN] = NUL; + if (lnum < wp->w_buffer->b_ml.ml_line_count) { + line = ml_get_buf(wp->w_buffer, lnum + 1, false); + spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN); } - } else { /* non-block mode */ - if (lnum > top->lnum && lnum <= bot->lnum) - fromcol = 0; - else if (lnum == top->lnum) { - if (VIsual_mode == 'V') /* linewise */ + + // When a word wrapped from the previous line the start of the current + // line is valid. + if (lnum == checked_lnum) { + cur_checked_col = checked_col; + } + checked_lnum = 0; + + // When there was a sentence end in the previous line may require a + // word starting with capital in this line. In line 1 always check + // the first word. + if (lnum != capcol_lnum) { + cap_col = -1; + } + if (lnum == 1) { + cap_col = 0; + } + capcol_lnum = 0; + } + + // + // handle visual active in this window + // + fromcol = -10; + tocol = MAXCOL; + if (VIsual_active && wp->w_buffer == curwin->w_buffer) { + // Visual is after curwin->w_cursor + if (ltoreq(curwin->w_cursor, VIsual)) { + top = &curwin->w_cursor; + bot = &VIsual; + } else { // Visual is before curwin->w_cursor + top = &VIsual; + bot = &curwin->w_cursor; + } + lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum); + if (VIsual_mode == Ctrl_V) { // block mode + if (lnum_in_visual_area) { + fromcol = wp->w_old_cursor_fcol; + tocol = wp->w_old_cursor_lcol; + } + } else { // non-block mode + if (lnum > top->lnum && lnum <= bot->lnum) { fromcol = 0; - else { - getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL); - if (gchar_pos(top) == NUL) - tocol = fromcol + 1; + } else if (lnum == top->lnum) { + if (VIsual_mode == 'V') { // linewise + fromcol = 0; + } else { + getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL); + if (gchar_pos(top) == NUL) { + tocol = fromcol + 1; + } + } } - } - if (VIsual_mode != 'V' && lnum == bot->lnum) { - if (*p_sel == 'e' && bot->col == 0 - && bot->coladd == 0 - ) { - fromcol = -10; - tocol = MAXCOL; - } else if (bot->col == MAXCOL) - tocol = MAXCOL; - else { - pos = *bot; - if (*p_sel == 'e') - getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL); - else { - getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol); - ++tocol; + if (VIsual_mode != 'V' && lnum == bot->lnum) { + if (*p_sel == 'e' && bot->col == 0 + && bot->coladd == 0) { + fromcol = -10; + tocol = MAXCOL; + } else if (bot->col == MAXCOL) { + tocol = MAXCOL; + } else { + pos = *bot; + if (*p_sel == 'e') { + getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL); + } else { + getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol); + tocol++; + } } } } - } - /* Check if the character under the cursor should not be inverted */ - if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin - ) - noinvcur = TRUE; + // Check if the char under the cursor should be inverted (highlighted). + if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin + && cursor_is_block_during_visual(*p_sel == 'e')) { + noinvcur = true; + } - /* if inverting in this line set area_highlighting */ - if (fromcol >= 0) { - area_highlighting = TRUE; - attr = hl_attr(HLF_V); + // if inverting in this line set area_highlighting + if (fromcol >= 0) { + area_highlighting = true; + attr = win_hl_attr(wp, HLF_V); + } + // handle 'incsearch' and ":s///c" highlighting + } else if (highlight_match + && wp == curwin + && lnum >= curwin->w_cursor.lnum + && lnum <= curwin->w_cursor.lnum + search_match_lines) { + if (lnum == curwin->w_cursor.lnum) { + getvcol(curwin, &(curwin->w_cursor), + (colnr_T *)&fromcol, NULL, NULL); + } else { + fromcol = 0; + } + if (lnum == curwin->w_cursor.lnum + search_match_lines) { + pos.lnum = lnum; + pos.col = search_match_endcol; + getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL); + } else { + tocol = MAXCOL; + } + // do at least one character; happens when past end of line + if (fromcol == tocol) { + tocol = fromcol + 1; + } + area_highlighting = true; + attr = win_hl_attr(wp, HLF_I); } } - /* - * handle 'incsearch' and ":s///c" highlighting - */ - else if (highlight_match - && wp == curwin - && lnum >= curwin->w_cursor.lnum - && lnum <= curwin->w_cursor.lnum + search_match_lines) { - if (lnum == curwin->w_cursor.lnum) - getvcol(curwin, &(curwin->w_cursor), - (colnr_T *)&fromcol, NULL, NULL); - else - fromcol = 0; - if (lnum == curwin->w_cursor.lnum + search_match_lines) { - pos.lnum = lnum; - pos.col = search_match_endcol; - getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL); - } else - tocol = MAXCOL; - /* do at least one character; happens when past end of line */ - if (fromcol == tocol) - tocol = fromcol + 1; - area_highlighting = TRUE; - attr = hl_attr(HLF_I); - } filler_lines = diff_check(wp, lnum); if (filler_lines < 0) { @@ -2398,24 +2359,52 @@ win_line ( filler_lines = wp->w_topfill; filler_todo = filler_lines; - /* If this line has a sign with line highlighting set line_attr. */ + // Cursor line highlighting for 'cursorline' in the current window. Not + // when Visual mode is active, because it's not clear what is selected + // then. + if (wp->w_p_cul && lnum == wp->w_cursor.lnum + && !(wp == curwin && VIsual_active)) { + int cul_attr = win_hl_attr(wp, HLF_CUL); + HlAttrs ae = syn_attr2entry(cul_attr); + + // We make a compromise here (#7383): + // * low-priority CursorLine if fg is not set + // * high-priority ("same as Vim" priority) CursorLine if fg is set + if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) { + line_attr_lowprio = cul_attr; + } else { + if (!(State & INSERT) && bt_quickfix(wp->w_buffer) + && qf_current_entry(wp) == lnum) { + line_attr = hl_combine_attr(cul_attr, line_attr); + } else { + line_attr = cul_attr; + } + } + } + + // If this line has a sign with line highlighting set line_attr. v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL); - if (v != 0) - line_attr = sign_get_attr((int)v, TRUE); + if (v != 0) { + line_attr = sign_get_attr((int)v, SIGN_LINEHL); + } - /* Highlight the current line in the quickfix window. */ - if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum) - line_attr = hl_attr(HLF_L); - if (line_attr != 0) - area_highlighting = TRUE; + // Highlight the current line in the quickfix window. + if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum) { + line_attr = win_hl_attr(wp, HLF_QFL); + } + + if (line_attr_lowprio || line_attr) { + area_highlighting = true; + } line = ml_get_buf(wp->w_buffer, lnum, FALSE); ptr = line; - if (has_spell) { - /* For checking first word with a capital skip white space. */ - if (cap_col == 0) - cap_col = (int)(skipwhite(line) - line); + if (has_spell && !number_only) { + // For checking first word with a capital skip white space. + if (cap_col == 0) { + cap_col = (int)getwhitecols(line); + } /* To be able to spell-check over line boundaries copy the end of the * current line into nextline[]. Above the start of the next line was @@ -2436,7 +2425,7 @@ win_line ( } else { /* Long line, use only the last SPWORDLEN bytes. */ nextlinecol = v - SPWORDLEN; - memmove(nextline, line + nextlinecol, SPWORDLEN); + memmove(nextline, line + nextlinecol, SPWORDLEN); // -V512 nextline_idx = SPWORDLEN + 1; } } @@ -2464,13 +2453,13 @@ win_line ( v = wp->w_skipcol; else v = wp->w_leftcol; - if (v > 0) { + if (v > 0 && !number_only) { char_u *prev_ptr = ptr; while (vcol < v && *ptr != NUL) { c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL); vcol += c; prev_ptr = ptr; - mb_ptr_adv(ptr); + MB_PTR_ADV(ptr); } // When: @@ -2491,7 +2480,11 @@ win_line ( if (vcol > v) { vcol -= c; ptr = prev_ptr; - n_skip = v - vcol; + // If the character fits on the screen, don't need to skip it. + // Except for a TAB. + if (utf_ptr2cells(ptr) >= c || *ptr == TAB) { + n_skip = v - vcol; + } } /* @@ -2568,21 +2561,24 @@ win_line ( * Do this for both search_hl and the match list. */ cur = wp->w_match_head; - shl_flag = FALSE; - while (cur != NULL || shl_flag == FALSE) { - if (shl_flag == FALSE) { + shl_flag = false; + while ((cur != NULL || !shl_flag) && !number_only) { + if (!shl_flag) { shl = &search_hl; - shl_flag = TRUE; - } else - shl = &cur->hl; + shl_flag = true; + } else { + shl = &cur->hl; // -V595 + } shl->startcol = MAXCOL; shl->endcol = MAXCOL; shl->attr_cur = 0; + shl->is_addpos = false; v = (long)(ptr - line); if (cur != NULL) { cur->pos.cur = 0; } - next_search_hl(wp, shl, lnum, (colnr_T)v, cur); + next_search_hl(wp, shl, lnum, (colnr_T)v, + shl == &search_hl ? NULL : cur); // Need to get the line again, a multi-line regexp may have made it // invalid. @@ -2612,6 +2608,7 @@ win_line ( if ((long)shl->startcol < v) { // match at leftcol shl->attr_cur = shl->attr; search_attr = shl->attr; + search_attr_from_match = shl != &search_hl; } area_highlighting = true; } @@ -2619,22 +2616,13 @@ win_line ( cur = cur->next; } - /* Cursor line highlighting for 'cursorline' in the current window. Not - * when Visual mode is active, because it's not clear what is selected - * then. */ - if (wp->w_p_cul && lnum == wp->w_cursor.lnum - && !(wp == curwin && VIsual_active)) { - line_attr = hl_attr(HLF_CUL); - area_highlighting = true; - } - - off = (unsigned)(current_ScreenLine - ScreenLines); - col = 0; + unsigned off = 0; // Offset relative start of line + int col = 0; // Visual column on screen. if (wp->w_p_rl) { - /* Rightleft window: process the text in the normal direction, but put - * it in current_ScreenLine[] from right to left. Start at the - * rightmost column of the window. */ - col = wp->w_width - 1; + // Rightleft window: process the text in the normal direction, but put + // it in linebuf_char[off] from right to left. Start at the + // rightmost column of the window. + col = grid->Columns - 1; off += col; } @@ -2647,7 +2635,7 @@ win_line ( // Repeat for the whole displayed line. for (;; ) { - has_match_conc = false; + has_match_conc = 0; // Skip this quickly when working on the text. if (draw_state != WL_LINE) { if (draw_state == WL_CMDLINE - 1 && n_extra == 0) { @@ -2656,7 +2644,7 @@ win_line ( /* Draw the cmdline character. */ n_extra = 1; c_extra = cmdwin_type; - char_attr = hl_attr(HLF_AT); + char_attr = win_hl_attr(wp, HLF_AT); } } @@ -2665,13 +2653,16 @@ win_line ( draw_state = WL_FOLD; if (fdc > 0) { - // Draw the 'foldcolumn'. - fill_foldcolumn(extra, wp, false, lnum); + // Draw the 'foldcolumn'. Allocate a buffer, "extra" may + // already be in use. + xfree(p_extra_free); + p_extra_free = xmalloc(12 + 1); + fill_foldcolumn(p_extra_free, wp, false, lnum); n_extra = fdc; - p_extra = extra; - p_extra[n_extra] = NUL; + p_extra_free[n_extra] = NUL; + p_extra = p_extra_free; c_extra = NUL; - char_attr = hl_attr(HLF_FC); + char_attr = win_hl_attr(wp, HLF_FC); } } @@ -2680,22 +2671,29 @@ win_line ( draw_state = WL_SIGN; /* Show the sign column when there are any signs in this * buffer or when using Netbeans. */ - if (draw_signcolumn(wp)) { + if (signcolumn_on(wp)) { int text_sign; - /* Draw two cells with the sign value or blank. */ + // Draw cells with the sign value or blank. c_extra = ' '; - char_attr = hl_attr(HLF_SC); - n_extra = 2; + char_attr = win_hl_attr(wp, HLF_SC); + n_extra = win_signcol_width(wp); if (row == startrow + filler_lines && filler_todo <= 0) { text_sign = buf_getsigntype(wp->w_buffer, lnum, SIGN_TEXT); if (text_sign != 0) { p_extra = sign_get_text(text_sign); + int symbol_blen = (int)STRLEN(p_extra); if (p_extra != NULL) { c_extra = NUL; - n_extra = (int)STRLEN(p_extra); + // symbol(s) bytes + (filling spaces) (one byte each) + n_extra = symbol_blen + + (win_signcol_width(wp) - mb_string2cells(p_extra)); + memset(extra, ' ', sizeof(extra)); + STRNCPY(extra, p_extra, STRLEN(p_extra)); + p_extra = extra; + p_extra[n_extra] = NUL; } - char_attr = sign_get_attr(text_sign, FALSE); + char_attr = sign_get_attr(text_sign, SIGN_TEXT); } } } @@ -2741,14 +2739,20 @@ win_line ( } else c_extra = ' '; n_extra = number_width(wp) + 1; - char_attr = hl_attr(HLF_N); - /* When 'cursorline' is set highlight the line number of - * the current line differently. - * TODO: Can we use CursorLine instead of CursorLineNr - * when CursorLineNr isn't set? */ - if ((wp->w_p_cul || wp->w_p_rnu) - && lnum == wp->w_cursor.lnum) - char_attr = hl_attr(HLF_CLN); + char_attr = win_hl_attr(wp, HLF_N); + + int num_sign = buf_getsigntype(wp->w_buffer, lnum, SIGN_NUMHL); + if (num_sign != 0) { + // :sign defined with "numhl" highlight. + char_attr = sign_get_attr(num_sign, SIGN_NUMHL); + } else if ((wp->w_p_cul || wp->w_p_rnu) + && lnum == wp->w_cursor.lnum) { + // When 'cursorline' is set highlight the line number of + // the current line differently. + // TODO(vim): Can we use CursorLine instead of CursorLineNr + // when CursorLineNr isn't set? + char_attr = win_hl_attr(wp, HLF_CLN); + } } } @@ -2761,16 +2765,18 @@ win_line ( draw_state = WL_BRI - 1; } - // draw 'breakindent': indent wrapped text accodringly + // draw 'breakindent': indent wrapped text accordingly if (draw_state == WL_BRI - 1 && n_extra == 0) { draw_state = WL_BRI; - if (wp->w_p_bri && n_extra == 0 && row != startrow && filler_lines == 0) { - char_attr = 0; // was: hl_attr(HLF_AT); + // if need_showbreak is set, breakindent also applies + if (wp->w_p_bri && (row != startrow || need_showbreak) + && filler_lines == 0) { + char_attr = 0; if (diff_hlf != (hlf_T)0) { - char_attr = hl_attr(diff_hlf); + char_attr = win_hl_attr(wp, diff_hlf); if (wp->w_p_cul && lnum == wp->w_cursor.lnum) { - char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUL)); + char_attr = hl_combine_attr(char_attr, win_hl_attr(wp, HLF_CUL)); } } p_extra = NULL; @@ -2791,19 +2797,20 @@ win_line ( c_extra = '-'; else c_extra = fill_diff; - if (wp->w_p_rl) + if (wp->w_p_rl) { n_extra = col + 1; - else - n_extra = wp->w_width - col; - char_attr = hl_attr(HLF_DED); + } else { + n_extra = grid->Columns - col; + } + char_attr = win_hl_attr(wp, HLF_DED); } if (*p_sbr != NUL && need_showbreak) { /* Draw 'showbreak' at the start of each broken line. */ p_extra = p_sbr; c_extra = NUL; n_extra = (int)STRLEN(p_sbr); - char_attr = hl_attr(HLF_AT); - need_showbreak = FALSE; + char_attr = win_hl_attr(wp, HLF_AT); + need_showbreak = false; vcol_sbr = vcol + MB_CHARLEN(p_sbr); /* Correct end of highlighted area for 'showbreak', * required when 'linebreak' is also set. */ @@ -2811,7 +2818,7 @@ win_line ( tocol += n_extra; /* combine 'showbreak' with 'cursorline' */ if (wp->w_p_cul && lnum == wp->w_cursor.lnum) { - char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUL)); + char_attr = hl_combine_attr(char_attr, win_hl_attr(wp, HLF_CUL)); } } } @@ -2824,39 +2831,43 @@ win_line ( c_extra = saved_c_extra; p_extra = saved_p_extra; char_attr = saved_char_attr; - } else + } else { char_attr = 0; + } } } - /* When still displaying '$' of change command, stop at cursor */ - if (dollar_vcol >= 0 && wp == curwin - && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol - && filler_todo <= 0 - ) { - SCREEN_LINE(screen_row, wp->w_wincol, col, -wp->w_width, wp->w_p_rl); - /* Pretend we have finished updating the window. Except when - * 'cursorcolumn' is set. */ - if (wp->w_p_cuc) + // When still displaying '$' of change command, stop at cursor + if ((dollar_vcol >= 0 && wp == curwin + && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol + && filler_todo <= 0) + || (number_only && draw_state > WL_NR)) { + grid_put_linebuf(grid, row, 0, col, -grid->Columns, wp->w_p_rl, wp, + wp->w_hl_attr_normal, false); + // Pretend we have finished updating the window. Except when + // 'cursorcolumn' is set. + if (wp->w_p_cuc) { row = wp->w_cline_row + wp->w_cline_height; - else - row = wp->w_height; + } else { + row = grid->Rows; + } break; } - if (draw_state == WL_LINE && area_highlighting) { - /* handle Visual or match highlighting in this line */ + if (draw_state == WL_LINE && (area_highlighting || has_spell)) { + // handle Visual or match highlighting in this line if (vcol == fromcol - || (has_mbyte && vcol + 1 == fromcol && n_extra == 0 - && (*mb_ptr2cells)(ptr) > 1) + || (vcol + 1 == fromcol && n_extra == 0 + && utf_ptr2cells(ptr) > 1) || ((int)vcol_prev == fromcol_prev - && vcol_prev < vcol /* not at margin */ - && vcol < tocol)) - area_attr = attr; /* start highlighting */ - else if (area_attr != 0 - && (vcol == tocol - || (noinvcur && (colnr_T)vcol == wp->w_virtcol))) - area_attr = 0; /* stop highlighting */ + && vcol_prev < vcol // not at margin + && vcol < tocol)) { + area_attr = attr; // start highlighting + } else if (area_attr != 0 && (vcol == tocol + || (noinvcur + && (colnr_T)vcol == wp->w_virtcol))) { + area_attr = 0; // stop highlighting + } if (!n_extra) { /* @@ -2897,16 +2908,16 @@ win_line ( shl->attr_cur = shl->attr; if (cur != NULL && syn_name2id((char_u *)"Conceal") == cur->hlg_id) { - has_match_conc = true; + has_match_conc = v == (long)shl->startcol ? 2 : 1; match_conc = cur->conceal_char; } else { - has_match_conc = match_conc = false; + has_match_conc = match_conc = 0; } } else if (v == (long)shl->endcol) { shl->attr_cur = 0; - prev_syntax_id = 0; - next_search_hl(wp, shl, lnum, (colnr_T)v, cur); + next_search_hl(wp, shl, lnum, (colnr_T)v, + shl == &search_hl ? NULL : cur); pos_inprogress = !(cur == NULL || cur->pos.cur == 0); /* Need to get the line again, a multi-line regexp @@ -2944,6 +2955,7 @@ win_line ( /* Use attributes from match with highest priority among * 'search_hl' and the match list. */ + search_attr_from_match = false; search_attr = search_hl.attr_cur; cur = wp->w_match_head; shl_flag = FALSE; @@ -2956,23 +2968,37 @@ win_line ( shl_flag = TRUE; } else shl = &cur->hl; - if (shl->attr_cur != 0) + if (shl->attr_cur != 0) { search_attr = shl->attr_cur; + search_attr_from_match = shl != &search_hl; + } if (shl != &search_hl && cur != NULL) cur = cur->next; } + // Only highlight one character after the last column. + if (*ptr == NUL + && (wp->w_p_list && lcs_eol_one == -1)) { + search_attr = 0; + } } if (diff_hlf != (hlf_T)0) { if (diff_hlf == HLF_CHD && ptr - line >= change_start - && n_extra == 0) - diff_hlf = HLF_TXD; /* changed text */ + && n_extra == 0) { + diff_hlf = HLF_TXD; // changed text + } if (diff_hlf == HLF_TXD && ptr - line > change_end - && n_extra == 0) - diff_hlf = HLF_CHD; /* changed line */ - line_attr = hl_attr(diff_hlf); + && n_extra == 0) { + diff_hlf = HLF_CHD; // changed line + } + line_attr = win_hl_attr(wp, diff_hlf); + // Overlay CursorLine onto diff-mode highlight. if (wp->w_p_cul && lnum == wp->w_cursor.lnum) { - line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL)); + line_attr = 0 != line_attr_lowprio // Low-priority CursorLine + ? hl_combine_attr(hl_combine_attr(win_hl_attr(wp, HLF_CUL), + line_attr), + hl_get_underline()) + : hl_combine_attr(line_attr, win_hl_attr(wp, HLF_CUL)); } } @@ -2988,14 +3014,15 @@ win_line ( // (area_attr may be 0 when "noinvcur" is set). else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL) || vcol < fromcol || vcol_prev < fromcol_prev - || vcol >= tocol)) + || vcol >= tocol)) { char_attr = line_attr; - else { - attr_pri = FALSE; - if (has_syntax) + } else { + attr_pri = false; + if (has_syntax) { char_attr = syntax_attr; - else + } else { char_attr = 0; + } } } @@ -3013,55 +3040,49 @@ win_line ( if (n_extra > 0) { if (c_extra != NUL) { c = c_extra; - mb_c = c; /* doesn't handle non-utf-8 multi-byte! */ - if (enc_utf8 && (*mb_char2len)(c) > 1) { - mb_utf8 = TRUE; + mb_c = c; // doesn't handle non-utf-8 multi-byte! + if (enc_utf8 && utf_char2len(c) > 1) { + mb_utf8 = true; u8cc[0] = 0; c = 0xc0; - } else - mb_utf8 = FALSE; + } else { + mb_utf8 = false; + } } else { c = *p_extra; if (has_mbyte) { mb_c = c; if (enc_utf8) { - /* If the UTF-8 character is more than one byte: - * Decode it into "mb_c". */ - mb_l = (*mb_ptr2len)(p_extra); - mb_utf8 = FALSE; - if (mb_l > n_extra) + // If the UTF-8 character is more than one byte: + // Decode it into "mb_c". + mb_l = utfc_ptr2len(p_extra); + mb_utf8 = false; + if (mb_l > n_extra) { mb_l = 1; - else if (mb_l > 1) { + } else if (mb_l > 1) { mb_c = utfc_ptr2char(p_extra, u8cc); - mb_utf8 = TRUE; + mb_utf8 = true; c = 0xc0; } - } else { - /* if this is a DBCS character, put it in "mb_c" */ - mb_l = MB_BYTE2LEN(c); - if (mb_l >= n_extra) - mb_l = 1; - else if (mb_l > 1) - mb_c = (c << 8) + p_extra[1]; } if (mb_l == 0) /* at the NUL at end-of-line */ mb_l = 1; /* If a double-width char doesn't fit display a '>' in the * last column. */ - if (( - wp->w_p_rl ? (col <= 0) : - (col >= wp->w_width - 1)) + if ((wp->w_p_rl ? (col <= 0) : + (col >= grid->Columns - 1)) && (*mb_char2cells)(mb_c) == 2) { c = '>'; mb_c = c; mb_l = 1; - mb_utf8 = FALSE; - multi_attr = hl_attr(HLF_AT); - /* put the pointer back to output the double-width - * character at the start of the next line. */ - ++n_extra; - --p_extra; + mb_utf8 = false; + multi_attr = win_hl_attr(wp, HLF_AT); + + // put the pointer back to output the double-width + // character at the start of the next line. + n_extra++; + p_extra--; } else { n_extra -= mb_l - 1; p_extra += mb_l - 1; @@ -3071,36 +3092,39 @@ win_line ( } --n_extra; } else { + int c0; + if (p_extra_free != NULL) { xfree(p_extra_free); p_extra_free = NULL; } - /* - * Get a character from the line itself. - */ - c = *ptr; + + // Get a character from the line itself. + c0 = c = *ptr; if (has_mbyte) { mb_c = c; if (enc_utf8) { - /* If the UTF-8 character is more than one byte: Decode it - * into "mb_c". */ - mb_l = (*mb_ptr2len)(ptr); - mb_utf8 = FALSE; + // If the UTF-8 character is more than one byte: Decode it + // into "mb_c". + mb_l = utfc_ptr2len(ptr); + mb_utf8 = false; if (mb_l > 1) { mb_c = utfc_ptr2char(ptr, u8cc); - /* Overlong encoded ASCII or ASCII with composing char - * is displayed normally, except a NUL. */ - if (mb_c < 0x80) - c = mb_c; - mb_utf8 = TRUE; + // Overlong encoded ASCII or ASCII with composing char + // is displayed normally, except a NUL. + if (mb_c < 0x80) { + c0 = c = mb_c; + } + mb_utf8 = true; /* At start of the line we can have a composing char. * Draw it as a space with a composing char. */ if (utf_iscomposing(mb_c)) { int i; - for (i = Screen_mco - 1; i > 0; --i) + for (i = MAX_MCO - 1; i > 0; i--) { u8cc[i] = u8cc[i - 1]; + } u8cc[0] = mb_c; mb_c = ' '; } @@ -3111,21 +3135,21 @@ win_line ( || (mb_l > 1 && (!vim_isprintc(mb_c)))) { // Illegal UTF-8 byte: display as <xx>. // Non-BMP character : display as ? or fullwidth ?. - transchar_hex(extra, mb_c); - if (wp->w_p_rl) { // reverse - rl_mirror(extra); + transchar_hex((char *)extra, mb_c); + if (wp->w_p_rl) { // reverse + rl_mirror(extra); } p_extra = extra; c = *p_extra; - mb_c = mb_ptr2char_adv(&p_extra); + mb_c = mb_ptr2char_adv((const char_u **)&p_extra); mb_utf8 = (c >= 0x80); n_extra = (int)STRLEN(p_extra); c_extra = NUL; if (area_attr == 0 && search_attr == 0) { n_attr = n_extra + 1; - extra_attr = hl_attr(HLF_8); - saved_attr2 = char_attr; /* save current attr */ + extra_attr = win_hl_attr(wp, HLF_8); + saved_attr2 = char_attr; // save current attr } } else if (mb_l == 0) /* at the NUL at end-of-line */ mb_l = 1; @@ -3177,8 +3201,8 @@ win_line ( c = *p_extra++; if (area_attr == 0 && search_attr == 0) { n_attr = n_extra + 1; - extra_attr = hl_attr(HLF_8); - saved_attr2 = char_attr; /* save current attr */ + extra_attr = win_hl_attr(wp, HLF_8); + saved_attr2 = char_attr; // save current attr } mb_c = c; } @@ -3187,20 +3211,20 @@ win_line ( /* If a double-width char doesn't fit display a '>' in the * last column; the character is displayed at the start of the * next line. */ - if (( - wp->w_p_rl ? (col <= 0) : - (col >= wp->w_width - 1)) + if ((wp->w_p_rl ? (col <= 0) : + (col >= grid->Columns - 1)) && (*mb_char2cells)(mb_c) == 2) { c = '>'; mb_c = c; - mb_utf8 = FALSE; + mb_utf8 = false; mb_l = 1; - multi_attr = hl_attr(HLF_AT); - /* Put pointer back so that the character will be - * displayed at the start of the next line. */ - --ptr; - } else if (*ptr != NUL) + multi_attr = win_hl_attr(wp, HLF_AT); + // Put pointer back so that the character will be + // displayed at the start of the next line. + ptr--; + } else if (*ptr != NUL) { ptr += mb_l - 1; + } /* If a double-width char doesn't fit at the left side display * a '<' in the first column. Don't do this for unprintable @@ -3211,11 +3235,11 @@ win_line ( c = ' '; if (area_attr == 0 && search_attr == 0) { n_attr = n_extra + 1; - extra_attr = hl_attr(HLF_AT); - saved_attr2 = char_attr; /* save current attr */ + extra_attr = win_hl_attr(wp, HLF_AT); + saved_attr2 = char_attr; // save current attr } mb_c = c; - mb_utf8 = FALSE; + mb_utf8 = false; mb_l = 1; } @@ -3235,8 +3259,7 @@ win_line ( did_emsg = FALSE; syntax_attr = get_syntax_attr((colnr_T)v - 1, - has_spell ? &can_spell : - NULL, FALSE); + has_spell ? &can_spell : NULL, false); if (did_emsg) { wp->w_s->b_syn_error = TRUE; @@ -3285,10 +3308,11 @@ win_line ( /* Use nextline[] if possible, it has the start of the * next line concatenated. */ - if ((prev_ptr - line) - nextlinecol >= 0) - p = nextline + (prev_ptr - line) - nextlinecol; - else + if ((prev_ptr - line) - nextlinecol >= 0) { + p = nextline + ((prev_ptr - line) - nextlinecol); + } else { p = prev_ptr; + } cap_col -= (int)(prev_ptr - line); size_t tmplen = spell_check(wp, p, &spell_hlf, &cap_col, nochange); assert(tmplen <= INT_MAX); @@ -3341,7 +3365,7 @@ win_line ( } if (has_bufhl && v > 0) { - bufhl_attr = bufhl_get_attr(&bufhl_info, (colnr_T)v); + int bufhl_attr = bufhl_get_attr(&bufhl_info, (colnr_T)v); if (bufhl_attr != 0) { if (!attr_pri) { char_attr = hl_combine_attr(char_attr, bufhl_attr); @@ -3355,15 +3379,14 @@ win_line ( char_attr = hl_combine_attr(char_attr, term_attrs[vcol]); } - /* - * Found last space before word: check for line break. - */ - if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)) { - int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0; + // Found last space before word: check for line break. + if (wp->w_p_lbr && c0 == c && vim_isbreak(c) + && !vim_isbreak((int)(*ptr))) { + int mb_off = utf_head_off(line, ptr - 1); char_u *p = ptr - (mb_off + 1); // TODO: is passing p for start of the line OK? n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol, NULL) - 1; - if (c == TAB && n_extra + col > wp->w_width) { + if (c == TAB && n_extra + col > grid->Columns) { n_extra = (int)wp->w_buffer->b_p_ts - vcol % (int)wp->w_buffer->b_p_ts - 1; } @@ -3386,10 +3409,10 @@ win_line ( || (c == ' ' && lcs_space && ptr - line <= trailcol))) { c = (c == ' ') ? lcs_space : lcs_nbsp; n_attr = 1; - extra_attr = hl_attr(HLF_8); + extra_attr = win_hl_attr(wp, HLF_0); saved_attr2 = char_attr; // save current attr mb_c = c; - if (enc_utf8 && (*mb_char2len)(c) > 1) { + if (enc_utf8 && utf_char2len(c) > 1) { mb_utf8 = true; u8cc[0] = 0; c = 0xc0; @@ -3401,27 +3424,25 @@ win_line ( if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ') { c = lcs_trail; n_attr = 1; - extra_attr = hl_attr(HLF_8); + extra_attr = win_hl_attr(wp, HLF_0); saved_attr2 = char_attr; // save current attr mb_c = c; - if (enc_utf8 && (*mb_char2len)(c) > 1) { - mb_utf8 = TRUE; + if (enc_utf8 && utf_char2len(c) > 1) { + mb_utf8 = true; u8cc[0] = 0; c = 0xc0; - } else - mb_utf8 = FALSE; + } else { + mb_utf8 = false; + } } } /* * Handling of non-printable characters. */ - if (!(chartab[c & 0xff] & CT_PRINT_CHAR)) { - /* - * when getting a character from the file, we may have to - * turn it into something else on the way to putting it - * into "ScreenLines". - */ + if (!vim_isprintc(c)) { + // when getting a character from the file, we may have to + // turn it into something else on the way to putting it on the screen. if (c == TAB && (!wp->w_p_list || lcs_tab1)) { int tab_len = 0; long vcol_adjusted = vcol; // removed showbreak length @@ -3446,8 +3467,7 @@ win_line ( tab_len += vcol_off; } // boguscols before FIX_FOR_BOGUSCOLS macro from above. - if (wp->w_p_list && lcs_tab1 && old_boguscols > 0 - && n_extra > tab_len) { + if (lcs_tab1 && old_boguscols > 0 && n_extra > tab_len) { tab_len += n_extra - tab_len; } @@ -3461,9 +3481,10 @@ win_line ( p = xmalloc(len + 1); memset(p, ' ', len); p[len] = NUL; + xfree(p_extra_free); p_extra_free = p; for (i = 0; i < tab_len; i++) { - mb_char2bytes(lcs_tab2, p); + utf_char2bytes(lcs_tab2, p); p += mb_char2len(lcs_tab2); n_extra += mb_char2len(lcs_tab2) - (saved_nextra > 0 ? 1: 0); } @@ -3495,7 +3516,7 @@ win_line ( } } - mb_utf8 = (int)false; // don't draw as UTF-8 + mb_utf8 = false; // don't draw as UTF-8 if (wp->w_p_list) { c = lcs_tab1; if (wp->w_p_lbr) { @@ -3504,11 +3525,11 @@ win_line ( c_extra = lcs_tab2; } n_attr = tab_len + 1; - extra_attr = hl_attr(HLF_8); - saved_attr2 = char_attr; /* save current attr */ + extra_attr = win_hl_attr(wp, HLF_0); + saved_attr2 = char_attr; // save current attr mb_c = c; - if (enc_utf8 && (*mb_char2len)(c) > 1) { - mb_utf8 = TRUE; + if (enc_utf8 && utf_char2len(c) > 1) { + mb_utf8 = true; u8cc[0] = 0; c = 0xc0; } @@ -3521,24 +3542,22 @@ win_line ( || ((fromcol >= 0 || fromcol_prev >= 0) && tocol > vcol && VIsual_mode != Ctrl_V - && ( - wp->w_p_rl ? (col >= 0) : - (col < wp->w_width)) + && (wp->w_p_rl ? (col >= 0) : (col < grid->Columns)) && !(noinvcur && lnum == wp->w_cursor.lnum && (colnr_T)vcol == wp->w_virtcol))) && lcs_eol_one > 0) { // Display a '$' after the line or highlight an extra // character if the line break is included. - // For a diff line the highlighting continues after the - // "$". - if (diff_hlf == (hlf_T)0 && line_attr == 0) { - /* In virtualedit, visual selections may extend - * beyond end of line. */ + // For a diff line the highlighting continues after the "$". + if (diff_hlf == (hlf_T)0 + && line_attr == 0 + && line_attr_lowprio == 0) { + // In virtualedit, visual selections may extend beyond end of line if (area_highlighting && virtual_active() - && tocol != MAXCOL && vcol < tocol) + && tocol != MAXCOL && vcol < tocol) { n_extra = 0; - else { + } else { p_extra = at_end_str; n_extra = 1; c_extra = NUL; @@ -3551,15 +3570,16 @@ win_line ( } lcs_eol_one = -1; ptr--; // put it back at the NUL - extra_attr = hl_attr(HLF_AT); + extra_attr = win_hl_attr(wp, HLF_AT); n_attr = 1; mb_c = c; - if (enc_utf8 && (*mb_char2len)(c) > 1) { - mb_utf8 = TRUE; + if (enc_utf8 && utf_char2len(c) > 1) { + mb_utf8 = true; u8cc[0] = 0; c = 0xc0; - } else - mb_utf8 = FALSE; /* don't draw as UTF-8 */ + } else { + mb_utf8 = false; // don't draw as UTF-8 + } } else if (c != NUL) { p_extra = transchar(c); if (n_extra == 0) { @@ -3576,61 +3596,35 @@ win_line ( memset(p, ' ', n_extra); STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1); p[n_extra] = NUL; + xfree(p_extra_free); p_extra_free = p_extra = p; } else { n_extra = byte2cells(c) - 1; c = *p_extra++; } n_attr = n_extra + 1; - extra_attr = hl_attr(HLF_8); + extra_attr = win_hl_attr(wp, HLF_8); saved_attr2 = char_attr; // save current attr mb_utf8 = false; // don't draw as UTF-8 } else if (VIsual_active - && (VIsual_mode == Ctrl_V - || VIsual_mode == 'v') + && (VIsual_mode == Ctrl_V || VIsual_mode == 'v') && virtual_active() && tocol != MAXCOL && vcol < tocol - && ( - wp->w_p_rl ? (col >= 0) : - (col < wp->w_width))) { + && (wp->w_p_rl ? (col >= 0) : (col < grid->Columns))) { c = ' '; ptr--; // put it back at the NUL - } else if ((diff_hlf != (hlf_T)0 || line_attr != 0) - && (wp->w_p_rl - ? (col >= 0) - : (col - boguscols < wp->w_width))) { - // Highlight until the right side of the window - c = ' '; - ptr--; // put it back at the NUL - - // Remember we do the char for line highlighting. - did_line_attr++; - - // don't do search HL for the rest of the line - if (line_attr != 0 && char_attr == search_attr && col > 0) { - char_attr = line_attr; - } - if (diff_hlf == HLF_TXD) { - diff_hlf = HLF_CHD; - if (attr == 0 || char_attr != attr) { - char_attr = hl_attr(diff_hlf); - if (wp->w_p_cul && lnum == wp->w_cursor.lnum) { - char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUL)); - } - } - } } } if (wp->w_p_cole > 0 && (wp != curwin || lnum != wp->w_cursor.lnum || conceal_cursor_line(wp)) - && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc) + && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0) && !(lnum_in_visual_area && vim_strchr(wp->w_p_cocu, 'v') == NULL)) { char_attr = conceal_attr; - if (prev_syntax_id != syntax_seqnr + if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1) && (syn_get_sub_char() != NUL || match_conc || wp->w_p_cole == 1) && wp->w_p_cole != 3) { @@ -3667,12 +3661,13 @@ win_line ( n_skip = 1; } mb_c = c; - if (enc_utf8 && (*mb_char2len)(c) > 1) { - mb_utf8 = TRUE; + if (enc_utf8 && utf_char2len(c) > 1) { + mb_utf8 = true; u8cc[0] = 0; c = 0xc0; - } else - mb_utf8 = FALSE; /* don't draw as UTF-8 */ + } else { + mb_utf8 = false; // don't draw as UTF-8 + } } else { prev_syntax_id = 0; is_concealing = FALSE; @@ -3686,7 +3681,7 @@ win_line ( && conceal_cursor_line(wp) && (int)wp->w_virtcol <= vcol + n_skip) { if (wp->w_p_rl) { - wp->w_wcol = wp->w_width - col + boguscols - 1; + wp->w_wcol = grid->Columns - col + boguscols - 1; } else { wp->w_wcol = col - boguscols; } @@ -3695,7 +3690,7 @@ win_line ( } // Don't override visual selection highlighting. - if (n_attr > 0 && draw_state == WL_LINE) { + if (n_attr > 0 && draw_state == WL_LINE && !search_attr_from_match) { char_attr = hl_combine_attr(char_attr, extra_attr); } @@ -3718,43 +3713,43 @@ win_line ( c_extra = MB_FILLER_CHAR; n_extra = 1; n_attr = 2; - extra_attr = hl_attr(HLF_AT); + extra_attr = win_hl_attr(wp, HLF_AT); } mb_c = c; - if (enc_utf8 && (*mb_char2len)(c) > 1) { - mb_utf8 = TRUE; + if (enc_utf8 && utf_char2len(c) > 1) { + mb_utf8 = true; u8cc[0] = 0; c = 0xc0; } else { mb_utf8 = false; // don't draw as UTF-8 } saved_attr3 = char_attr; // save current attr - char_attr = hl_attr(HLF_AT); // later copied to char_attr + char_attr = win_hl_attr(wp, HLF_AT); // overwriting char_attr n_attr3 = 1; } /* * At end of the text line or just after the last character. */ - if (c == NUL || did_line_attr == 1) { + if (c == NUL) { long prevcol = (long)(ptr - line) - (c == NUL); /* we're not really at that column when skipping some text */ if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol) ++prevcol; - /* Invert at least one char, used for Visual and empty line or - * highlight match at end of line. If it's beyond the last - * char on the screen, just overwrite that one (tricky!) Not - * needed when a '$' was displayed for 'list'. */ - prevcol_hl_flag = FALSE; - if (prevcol == (long)search_hl.startcol) - prevcol_hl_flag = TRUE; - else { + // Invert at least one char, used for Visual and empty line or + // highlight match at end of line. If it's beyond the last + // char on the screen, just overwrite that one (tricky!) Not + // needed when a '$' was displayed for 'list'. + prevcol_hl_flag = false; + if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol) { + prevcol_hl_flag = true; + } else { cur = wp->w_match_head; while (cur != NULL) { - if (prevcol == (long)cur->hl.startcol) { - prevcol_hl_flag = TRUE; + if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol) { + prevcol_hl_flag = true; break; } cur = cur->next; @@ -3766,17 +3761,17 @@ win_line ( || lnum == VIsual.lnum || lnum == curwin->w_cursor.lnum) && c == NUL) - /* highlight 'hlsearch' match at end of line */ - || (prevcol_hl_flag == TRUE && did_line_attr <= 1) - )) { + // highlight 'hlsearch' match at end of line + || prevcol_hl_flag)) { int n = 0; if (wp->w_p_rl) { if (col < 0) n = 1; } else { - if (col >= wp->w_width) + if (col >= grid->Columns) { n = -1; + } } if (n != 0) { /* At the window boundary, highlight the last character @@ -3784,10 +3779,8 @@ win_line ( off += n; col += n; } else { - /* Add a blank character to highlight. */ - ScreenLines[off] = ' '; - if (enc_utf8) - ScreenLinesUC[off] = 0; + // Add a blank character to highlight. + schar_from_ascii(linebuf_char[off], ' '); } if (area_attr == 0) { /* Use attributes from match with highest priority among @@ -3804,13 +3797,21 @@ win_line ( shl_flag = TRUE; } else shl = &cur->hl; - if ((ptr - line) - 1 == (long)shl->startcol) + if ((ptr - line) - 1 == (long)shl->startcol + && (shl == &search_hl || !shl->is_addpos)) { char_attr = shl->attr; - if (shl != &search_hl && cur != NULL) + } + if (shl != &search_hl && cur != NULL) { cur = cur->next; + } } } - ScreenAttrs[off] = char_attr; + + int eol_attr = char_attr; + if (wp->w_p_cul && lnum == wp->w_cursor.lnum) { + eol_attr = hl_combine_attr(win_hl_attr(wp, HLF_CUL), eol_attr); + } + linebuf_attr[off] = eol_attr; if (wp->w_p_rl) { --col; --off; @@ -3821,25 +3822,12 @@ win_line ( ++vcol; eol_hl_off = 1; } - } - - /* - * At end of the text line. - */ - if (c == NUL) { - if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol - && lnum == wp->w_cursor.lnum) { - /* highlight last char after line */ - --col; - --off; - --vcol; - } - - /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */ - if (wp->w_p_wrap) + // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. + if (wp->w_p_wrap) { v = wp->w_skipcol; - else + } else { v = wp->w_leftcol; + } /* check if line ends before left margin */ if (vcol < v + col - win_col_off(wp)) @@ -3855,58 +3843,124 @@ win_line ( if (((wp->w_p_cuc && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off && (int)wp->w_virtcol < - wp->w_width * (row - startrow + 1) + v + grid->Columns * (row - startrow + 1) + v && lnum != wp->w_cursor.lnum) - || draw_color_col) - && !wp->w_p_rl - ) { + || draw_color_col || line_attr_lowprio || line_attr + || diff_hlf != (hlf_T)0 || do_virttext)) { int rightmost_vcol = 0; int i; - if (wp->w_p_cuc) + VirtText virt_text = do_virttext ? bufhl_info.line->virt_text + : (VirtText)KV_INITIAL_VALUE; + size_t virt_pos = 0; + LineState s = LINE_STATE((char_u *)""); + int virt_attr = 0; + + // Make sure alignment is the same regardless + // if listchars=eol:X is used or not. + bool delay_virttext = lcs_eol == lcs_eol_one && eol_hl_off == 0; + + if (wp->w_p_cuc) { rightmost_vcol = wp->w_virtcol; - if (draw_color_col) - /* determine rightmost colorcolumn to possibly draw */ - for (i = 0; color_cols[i] >= 0; ++i) - if (rightmost_vcol < color_cols[i]) + } + + if (draw_color_col) { + // determine rightmost colorcolumn to possibly draw + for (i = 0; color_cols[i] >= 0; i++) { + if (rightmost_vcol < color_cols[i]) { rightmost_vcol = color_cols[i]; + } + } + } - while (col < wp->w_width) { - ScreenLines[off] = ' '; - if (enc_utf8) - ScreenLinesUC[off] = 0; - ++col; - if (draw_color_col) - draw_color_col = advance_color_col(VCOL_HLC, - &color_cols); - - if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol) - ScreenAttrs[off++] = hl_attr(HLF_CUC); - else if (draw_color_col && VCOL_HLC == *color_cols) - ScreenAttrs[off++] = hl_attr(HLF_MC); - else - ScreenAttrs[off++] = 0; + int cuc_attr = win_hl_attr(wp, HLF_CUC); + int mc_attr = win_hl_attr(wp, HLF_MC); + + int diff_attr = 0; + if (diff_hlf == HLF_TXD) { + diff_hlf = HLF_CHD; + } + if (diff_hlf != 0) { + diff_attr = win_hl_attr(wp, diff_hlf); + } + + int base_attr = hl_combine_attr(line_attr_lowprio, diff_attr); + if (base_attr || line_attr) { + rightmost_vcol = INT_MAX; + } + + int col_stride = wp->w_p_rl ? -1 : 1; + + while (wp->w_p_rl ? col >= 0 : col < grid->Columns) { + int cells = -1; + if (do_virttext && !delay_virttext) { + if (*s.p == NUL) { + if (virt_pos < virt_text.size) { + s.p = (char_u *)kv_A(virt_text, virt_pos).text; + int hl_id = kv_A(virt_text, virt_pos).hl_id; + virt_attr = hl_id > 0 ? syn_id2attr(hl_id) : 0; + virt_pos++; + } else { + do_virttext = false; + } + } + if (*s.p != NUL) { + cells = line_putchar(&s, &linebuf_char[off], grid->Columns - col, + false); + } + } + delay_virttext = false; + + if (cells == -1) { + schar_from_ascii(linebuf_char[off], ' '); + cells = 1; + } + col += cells * col_stride; + if (draw_color_col) { + draw_color_col = advance_color_col(VCOL_HLC, &color_cols); + } - if (VCOL_HLC >= rightmost_vcol) + int col_attr = base_attr; + + if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol) { + col_attr = cuc_attr; + } else if (draw_color_col && VCOL_HLC == *color_cols) { + col_attr = mc_attr; + } + + if (do_virttext) { + col_attr = hl_combine_attr(col_attr, virt_attr); + } + + col_attr = hl_combine_attr(col_attr, line_attr); + + linebuf_attr[off] = col_attr; + if (cells == 2) { + linebuf_attr[off+1] = col_attr; + } + off += cells * col_stride; + + if (VCOL_HLC >= rightmost_vcol && *s.p == NUL + && virt_pos >= virt_text.size) { break; + } ++vcol; } } + // TODO(bfredl): integrate with the common beyond-the-end-loop if (wp->w_buffer->terminal) { // terminal buffers may need to highlight beyond the end of the // logical line - while (col < wp->w_width) { - ScreenLines[off] = ' '; - if (enc_utf8) { - ScreenLinesUC[off] = 0; - } - ScreenAttrs[off++] = term_attrs[vcol++]; + while (col < grid->Columns) { + schar_from_ascii(linebuf_char[off], ' '); + linebuf_attr[off++] = term_attrs[vcol++]; col++; } } - SCREEN_LINE(screen_row, wp->w_wincol, col, wp->w_width, wp->w_p_rl); + grid_put_linebuf(grid, row, 0, col, grid->Columns, wp->w_p_rl, wp, + wp->w_hl_attr_normal, false); row++; /* @@ -3918,6 +3972,7 @@ win_line ( curwin->w_cline_height = row - startrow; curwin->w_cline_folded = false; curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW); + conceal_cursor_used = conceal_cursor_line(curwin); } break; @@ -3927,21 +3982,20 @@ win_line ( if (lcs_ext && !wp->w_p_wrap && filler_todo <= 0 - && ( - wp->w_p_rl ? col == 0 : - col == wp->w_width - 1) + && (wp->w_p_rl ? col == 0 : col == grid->Columns - 1) && (*ptr != NUL || (wp->w_p_list && lcs_eol_one > 0) || (n_extra && (c_extra != NUL || *p_extra != NUL)))) { c = lcs_ext; - char_attr = hl_attr(HLF_AT); + char_attr = win_hl_attr(wp, HLF_AT); mb_c = c; - if (enc_utf8 && (*mb_char2len)(c) > 1) { - mb_utf8 = TRUE; + if (enc_utf8 && utf_char2len(c) > 1) { + mb_utf8 = true; u8cc[0] = 0; c = 0xc0; - } else - mb_utf8 = FALSE; + } else { + mb_utf8 = false; + } } /* advance to the next 'colorcolumn' */ @@ -3953,17 +4007,23 @@ win_line ( * Also highlight the 'colorcolumn' if it is different than * 'cursorcolumn' */ vcol_save_attr = -1; - if (draw_state == WL_LINE && !lnum_in_visual_area) { + if (draw_state == WL_LINE && !lnum_in_visual_area + && search_attr == 0 && area_attr == 0) { if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol && lnum != wp->w_cursor.lnum) { vcol_save_attr = char_attr; - char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC)); + char_attr = hl_combine_attr(win_hl_attr(wp, HLF_CUC), char_attr); } else if (draw_color_col && VCOL_HLC == *color_cols) { vcol_save_attr = char_attr; - char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC)); + char_attr = hl_combine_attr(win_hl_attr(wp, HLF_MC), char_attr); } } + // Apply lowest-priority line attr now, so everything can override it. + if (draw_state == WL_LINE) { + char_attr = hl_combine_attr(line_attr_lowprio, char_attr); + } + /* * Store character to be displayed. * Skip characters that are left of the screen for 'nowrap'. @@ -3978,43 +4038,24 @@ win_line ( --off; --col; } - ScreenLines[off] = c; - if (enc_dbcs == DBCS_JPNU) { - if ((mb_c & 0xff00) == 0x8e00) - ScreenLines[off] = 0x8e; - ScreenLines2[off] = mb_c & 0xff; - } else if (enc_utf8) { - if (mb_utf8) { - int i; - - ScreenLinesUC[off] = mb_c; - if ((c & 0xff) == 0) - ScreenLines[off] = 0x80; /* avoid storing zero */ - for (i = 0; i < Screen_mco; ++i) { - ScreenLinesC[i][off] = u8cc[i]; - if (u8cc[i] == 0) - break; - } - } else - ScreenLinesUC[off] = 0; + if (mb_utf8) { + schar_from_cc(linebuf_char[off], mb_c, u8cc); + } else { + schar_from_ascii(linebuf_char[off], c); } if (multi_attr) { - ScreenAttrs[off] = multi_attr; + linebuf_attr[off] = multi_attr; multi_attr = 0; - } else - ScreenAttrs[off] = char_attr; + } else { + linebuf_attr[off] = char_attr; + } if (has_mbyte && (*mb_char2cells)(mb_c) > 1) { // Need to fill two screen columns. off++; col++; - if (enc_utf8) { - // UTF-8: Put a 0 in the second screen char. - ScreenLines[off] = 0; - } else { - // DBCS: Put second byte in the second screen char. - ScreenLines[off] = mb_c & 0xff; - } + // UTF-8: Put a 0 in the second screen char. + linebuf_char[off][0] = 0; if (draw_state > WL_NR && filler_todo <= 0) { vcol++; } @@ -4120,19 +4161,35 @@ win_line ( * At end of screen line and there is more to come: Display the line * so far. If there is no more to display it is caught above. */ - if (( - wp->w_p_rl ? (col < 0) : - (col >= wp->w_width)) + if ((wp->w_p_rl ? (col < 0) : (col >= grid->Columns)) && (*ptr != NUL || filler_todo > 0 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str) || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL))) ) { - SCREEN_LINE(screen_row, wp->w_wincol, col - boguscols, - wp->w_width, wp->w_p_rl); + bool wrap = wp->w_p_wrap // Wrapping enabled. + && filler_todo <= 0 // Not drawing diff filler lines. + && lcs_eol_one != -1 // Haven't printed the lcs_eol character. + && row != endrow - 1 // Not the last line being displayed. + && (grid->Columns == Columns // Window spans the width of the screen, + || ui_is_external(kUIMultigrid)) // or has dedicated grid. + && !wp->w_p_rl; // Not right-to-left. + grid_put_linebuf(grid, row, 0, col - boguscols, grid->Columns, wp->w_p_rl, + wp, wp->w_hl_attr_normal, wrap); + if (wrap) { + ScreenGrid *current_grid = grid; + int current_row = row, dummy_col = 0; // dummy_col unused + screen_adjust_grid(¤t_grid, ¤t_row, &dummy_col); + + // Force a redraw of the first column of the next line. + current_grid->attrs[current_grid->line_offset[current_row+1]] = -1; + + // Remember that the line wraps, used for modeless copy. + current_grid->line_wraps[current_row] = true; + } + boguscols = 0; - ++row; - ++screen_row; + row++; /* When not wrapping and finished diff lines, or when displayed * '$' and highlighting until last column, break here. */ @@ -4145,8 +4202,7 @@ win_line ( if (draw_state != WL_LINE && filler_todo <= 0 ) { - win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT); - draw_vsep_win(wp, row); + win_draw_end(wp, '@', ' ', row, wp->w_grid.Rows, HLF_AT); row = endrow; } @@ -4156,59 +4212,10 @@ win_line ( break; } - if (ui_current_row() == screen_row - 1 - && filler_todo <= 0 - && wp->w_width == Columns) { - /* Remember that the line wraps, used for modeless copy. */ - LineWraps[screen_row - 1] = TRUE; - - /* - * Special trick to make copy/paste of wrapped lines work with - * xterm/screen: write an extra character beyond the end of - * the line. This will work with all terminal types - * (regardless of the xn,am settings). - * Only do this if the cursor is on the current line - * (something has been written in it). - * Don't do this for the GUI. - * Don't do this for double-width characters. - * Don't do this for a window not at the right screen border. - */ - if (!(has_mbyte - && ((*mb_off2cells)(LineOffset[screen_row], - LineOffset[screen_row] + screen_Columns) - == 2 - || (*mb_off2cells)(LineOffset[screen_row - 1] - + (int)Columns - 2, - LineOffset[screen_row] + screen_Columns) - == 2)) - ) { - /* First make sure we are at the end of the screen line, - * then output the same character again to let the - * terminal know about the wrap. If the terminal doesn't - * auto-wrap, we overwrite the character. */ - if (ui_current_col() != wp->w_width) - screen_char(LineOffset[screen_row - 1] - + (unsigned)Columns - 1, - screen_row - 1, (int)(Columns - 1)); - - /* When there is a multi-byte character, just output a - * space to keep it simple. */ - if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[ - screen_row - - 1] + (Columns - 1)]) > 1) { - ui_putc(' '); - } else { - ui_putc(ScreenLines[LineOffset[screen_row - 1] + (Columns - 1)]); - } - /* force a redraw of the first char on the next line */ - ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1; - } - } - col = 0; - off = (unsigned)(current_ScreenLine - ScreenLines); + off = 0; if (wp->w_p_rl) { - col = wp->w_width - 1; /* col is not used if breaking! */ + col = grid->Columns - 1; // col is not used if breaking! off += col; } @@ -4237,27 +4244,27 @@ win_line ( cap_col = 0; } + xfree(p_extra_free); return row; } - -/* - * Return if the composing characters at "off_from" and "off_to" differ. - * Only to be used when ScreenLinesUC[off_from] != 0. - */ -static int comp_char_differs(int off_from, int off_to) +/// Determine if dedicated window grid should be used or the default_grid +/// +/// If UI did not request multigrid support, draw all windows on the +/// default_grid. +/// +/// If the default_grid is used, adjust window relative positions to global +/// screen positions. +static void screen_adjust_grid(ScreenGrid **grid, int *row_off, int *col_off) { - int i; - - for (i = 0; i < Screen_mco; ++i) { - if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to]) - return TRUE; - if (ScreenLinesC[i][off_from] == 0) - break; + if (!ui_is_external(kUIMultigrid) && *grid != &default_grid) { + *row_off += (*grid)->row_offset; + *col_off += (*grid)->col_offset; + *grid = &default_grid; } - return FALSE; } + /* * Check whether the given character needs redrawing: * - the (first byte of the) character is different @@ -4265,222 +4272,184 @@ static int comp_char_differs(int off_from, int off_to) * - the character is multi-byte and the next byte is different * - the character is two cells wide and the second cell differs. */ -static int char_needs_redraw(int off_from, int off_to, int cols) +static int grid_char_needs_redraw(ScreenGrid *grid, int off_from, int off_to, + int cols) { return (cols > 0 - && ((ScreenLines[off_from] != ScreenLines[off_to] - || ScreenAttrs[off_from] != ScreenAttrs[off_to]) - - || (enc_dbcs != 0 - && MB_BYTE2LEN(ScreenLines[off_from]) > 1 - && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e - ? ScreenLines2[off_from] != ScreenLines2[off_to] - : (cols > 1 && ScreenLines[off_from + 1] - != ScreenLines[off_to + 1]))) - || (enc_utf8 - && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to] - || (ScreenLinesUC[off_from] != 0 - && comp_char_differs(off_from, off_to)) - || ((*mb_off2cells)(off_from, off_from + cols) > 1 - && ScreenLines[off_from + 1] - != ScreenLines[off_to + 1]))))); + && ((schar_cmp(linebuf_char[off_from], grid->chars[off_to]) + || linebuf_attr[off_from] != grid->attrs[off_to] + || (line_off2cells(linebuf_char, off_from, off_from + cols) > 1 + && schar_cmp(linebuf_char[off_from + 1], + grid->chars[off_to + 1]))) + || p_wd < 0)); } -/* - * Move one "cooked" screen line to the screen, but only the characters that - * have actually changed. Handle insert/delete character. - * "coloff" gives the first column on the screen for this line. - * "endcol" gives the columns where valid characters are. - * "clear_width" is the width of the window. It's > 0 if the rest of the line - * needs to be cleared, negative otherwise. - * "rlflag" is TRUE in a rightleft window: - * When TRUE and "clear_width" > 0, clear columns 0 to "endcol" - * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width" - */ -static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag) +/// Move one buffered line to the window grid, but only the characters that +/// have actually changed. Handle insert/delete character. +/// "coloff" gives the first column on the grid for this line. +/// "endcol" gives the columns where valid characters are. +/// "clear_width" is the width of the window. It's > 0 if the rest of the line +/// needs to be cleared, negative otherwise. +/// "rlflag" is TRUE in a rightleft window: +/// When TRUE and "clear_width" > 0, clear columns 0 to "endcol" +/// When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width" +/// If "wrap" is true, then hint to the UI that "row" contains a line +/// which has wrapped into the next row. +static void grid_put_linebuf(ScreenGrid *grid, int row, int coloff, int endcol, + int clear_width, int rlflag, win_T *wp, + int bg_attr, bool wrap) { unsigned off_from; unsigned off_to; unsigned max_off_from; unsigned max_off_to; int col = 0; - int hl; - int force = FALSE; /* force update rest of the line */ - int redraw_this /* bool: does character need redraw? */ - ; - int redraw_next; /* redraw_this for next character */ - int clear_next = FALSE; - int char_cells; /* 1: normal char */ - /* 2: occupies two display cells */ -# define CHAR_CELLS char_cells - - /* Check for illegal row and col, just in case. */ - if (row >= Rows) - row = Rows - 1; - if (endcol > Columns) - endcol = Columns; - - - off_from = (unsigned)(current_ScreenLine - ScreenLines); - off_to = LineOffset[row] + coloff; - max_off_from = off_from + screen_Columns; - max_off_to = LineOffset[row] + screen_Columns; + bool redraw_this; // Does character need redraw? + bool redraw_next; // redraw_this for next character + bool clear_next = false; + int char_cells; // 1: normal char + // 2: occupies two display cells + int start_dirty = -1, end_dirty = 0; + + // TODO(bfredl): check all callsites and eliminate + // Check for illegal row and col, just in case + if (row >= grid->Rows) { + row = grid->Rows - 1; + } + if (endcol > grid->Columns) { + endcol = grid->Columns; + } + + screen_adjust_grid(&grid, &row, &coloff); + + off_from = 0; + off_to = grid->line_offset[row] + coloff; + max_off_from = linebuf_size; + max_off_to = grid->line_offset[row] + grid->Columns; if (rlflag) { /* Clear rest first, because it's left of the text. */ if (clear_width > 0) { - while (col <= endcol && ScreenLines[off_to] == ' ' - && ScreenAttrs[off_to] == 0 - && (!enc_utf8 || ScreenLinesUC[off_to] == 0) + while (col <= endcol && grid->chars[off_to][0] == ' ' + && grid->chars[off_to][1] == NUL + && grid->attrs[off_to] == bg_attr ) { ++off_to; ++col; } - if (col <= endcol) - screen_fill(row, row + 1, col + coloff, - endcol + coloff + 1, ' ', ' ', 0); + if (col <= endcol) { + grid_fill(grid, row, row + 1, col + coloff, endcol + coloff + 1, + ' ', ' ', bg_attr); + } } col = endcol + 1; - off_to = LineOffset[row] + col + coloff; + off_to = grid->line_offset[row] + col + coloff; off_from += col; endcol = (clear_width > 0 ? clear_width : -clear_width); } - redraw_next = char_needs_redraw(off_from, off_to, endcol - col); + if (bg_attr) { + for (int c = col; c < endcol; c++) { + linebuf_attr[off_from+c] = + hl_combine_attr(bg_attr, linebuf_attr[off_from+c]); + } + } - while (col < endcol) { - if (has_mbyte && (col + 1 < endcol)) - char_cells = (*mb_off2cells)(off_from, max_off_from); - else - char_cells = 1; + redraw_next = grid_char_needs_redraw(grid, off_from, off_to, endcol - col); + while (col < endcol) { + char_cells = 1; + if (col + 1 < endcol) { + char_cells = line_off2cells(linebuf_char, off_from, max_off_from); + } redraw_this = redraw_next; - redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS, - off_to + CHAR_CELLS, endcol - col - CHAR_CELLS); - + redraw_next = grid_char_needs_redraw(grid, off_from + char_cells, + off_to + char_cells, + endcol - col - char_cells); if (redraw_this) { - if (enc_dbcs != 0) { - /* Check if overwriting a double-byte with a single-byte or - * the other way around requires another character to be - * redrawn. For UTF-8 this isn't needed, because comparing - * ScreenLinesUC[] is sufficient. */ - if (char_cells == 1 - && col + 1 < endcol - && (*mb_off2cells)(off_to, max_off_to) > 1) { - /* Writing a single-cell character over a double-cell - * character: need to redraw the next cell. */ - ScreenLines[off_to + 1] = 0; - redraw_next = TRUE; - } else if (char_cells == 2 - && col + 2 < endcol - && (*mb_off2cells)(off_to, max_off_to) == 1 - && (*mb_off2cells)(off_to + 1, max_off_to) > 1) { - /* Writing the second half of a double-cell character over - * a double-cell character: need to redraw the second - * cell. */ - ScreenLines[off_to + 2] = 0; - redraw_next = TRUE; - } - - if (enc_dbcs == DBCS_JPNU) - ScreenLines2[off_to] = ScreenLines2[off_from]; + if (start_dirty == -1) { + start_dirty = col; } - /* When writing a single-width character over a double-width - * character and at the end of the redrawn text, need to clear out - * the right halve of the old character. - * Also required when writing the right halve of a double-width - * char over the left halve of an existing one. */ - if (has_mbyte && col + char_cells == endcol + end_dirty = col + char_cells; + // When writing a single-width character over a double-width + // character and at the end of the redrawn text, need to clear out + // the right halve of the old character. + // Also required when writing the right halve of a double-width + // char over the left halve of an existing one + if (col + char_cells == endcol && ((char_cells == 1 - && (*mb_off2cells)(off_to, max_off_to) > 1) + && grid_off2cells(grid, off_to, max_off_to) > 1) || (char_cells == 2 - && (*mb_off2cells)(off_to, max_off_to) == 1 - && (*mb_off2cells)(off_to + 1, max_off_to) > 1))) - clear_next = TRUE; - - ScreenLines[off_to] = ScreenLines[off_from]; - if (enc_utf8) { - ScreenLinesUC[off_to] = ScreenLinesUC[off_from]; - if (ScreenLinesUC[off_from] != 0) { - int i; - - for (i = 0; i < Screen_mco; ++i) - ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from]; - } + && grid_off2cells(grid, off_to, max_off_to) == 1 + && grid_off2cells(grid, off_to + 1, max_off_to) > 1))) { + clear_next = true; } - if (char_cells == 2) - ScreenLines[off_to + 1] = ScreenLines[off_from + 1]; - ScreenAttrs[off_to] = ScreenAttrs[off_from]; - /* For simplicity set the attributes of second half of a - * double-wide character equal to the first half. */ - if (char_cells == 2) - ScreenAttrs[off_to + 1] = ScreenAttrs[off_from]; + schar_copy(grid->chars[off_to], linebuf_char[off_from]); + if (char_cells == 2) { + schar_copy(grid->chars[off_to+1], linebuf_char[off_from+1]); + } - if (enc_dbcs != 0 && char_cells == 2) - screen_char_2(off_to, row, col + coloff); - else - screen_char(off_to, row, col + coloff); + grid->attrs[off_to] = linebuf_attr[off_from]; + // For simplicity set the attributes of second half of a + // double-wide character equal to the first half. + if (char_cells == 2) { + grid->attrs[off_to + 1] = linebuf_attr[off_from]; + } } - off_to += CHAR_CELLS; - off_from += CHAR_CELLS; - col += CHAR_CELLS; + off_to += char_cells; + off_from += char_cells; + col += char_cells; } if (clear_next) { /* Clear the second half of a double-wide character of which the left * half was overwritten with a single-wide character. */ - ScreenLines[off_to] = ' '; - if (enc_utf8) - ScreenLinesUC[off_to] = 0; - screen_char(off_to, row, col + coloff); + schar_from_ascii(grid->chars[off_to], ' '); + end_dirty++; + } + + int clear_end = -1; + if (clear_width > 0 && !rlflag) { + // blank out the rest of the line + // TODO(bfredl): we could cache winline widths + while (col < clear_width) { + if (grid->chars[off_to][0] != ' ' + || grid->chars[off_to][1] != NUL + || grid->attrs[off_to] != bg_attr) { + grid->chars[off_to][0] = ' '; + grid->chars[off_to][1] = NUL; + grid->attrs[off_to] = bg_attr; + if (start_dirty == -1) { + start_dirty = col; + end_dirty = col; + } else if (clear_end == -1) { + end_dirty = endcol; + } + clear_end = col+1; + } + col++; + off_to++; + } } - if (clear_width > 0 - && !rlflag - ) { + if (clear_width > 0 || wp->w_width != grid->Columns) { + // If we cleared after the end of the line, it did not wrap. + // For vsplit, line wrapping is not possible. + grid->line_wraps[row] = false; + } - /* blank out the rest of the line */ - while (col < clear_width && ScreenLines[off_to] == ' ' - && ScreenAttrs[off_to] == 0 - && (!enc_utf8 || ScreenLinesUC[off_to] == 0) - ) { - ++off_to; - ++col; - } - if (col < clear_width) { - screen_fill(row, row + 1, col + coloff, clear_width + coloff, - ' ', ' ', 0); - off_to += clear_width - col; - col = clear_width; - } - } - - if (clear_width > 0) { - /* For a window that's left of another, draw the separator char. */ - if (col + coloff < Columns) { - int c; - - c = fillchar_vsep(&hl); - if (ScreenLines[off_to] != (schar_T)c - || (enc_utf8 && (int)ScreenLinesUC[off_to] - != (c >= 0x80 ? c : 0)) - || ScreenAttrs[off_to] != hl) { - ScreenLines[off_to] = c; - ScreenAttrs[off_to] = hl; - if (enc_utf8) { - if (c >= 0x80) { - ScreenLinesUC[off_to] = c; - ScreenLinesC[0][off_to] = 0; - } else - ScreenLinesUC[off_to] = 0; - } - screen_char(off_to, row, col + coloff); - } - } else - LineWraps[row] = FALSE; + if (clear_end < end_dirty) { + clear_end = end_dirty; + } + if (start_dirty == -1) { + start_dirty = end_dirty; + } + if (clear_end > start_dirty) { + ui_line(grid, row, coloff+start_dirty, coloff+end_dirty, coloff+clear_end, + bg_attr, wrap); } } @@ -4514,14 +4483,18 @@ void status_redraw_all(void) } } -/* - * mark all status lines of the current buffer for redraw - */ +/// Marks all status lines of the current buffer for redraw. void status_redraw_curbuf(void) { + status_redraw_buf(curbuf); +} + +/// Marks all status lines of the specified buffer for redraw. +void status_redraw_buf(buf_T *buf) +{ FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { - if (wp->w_status_height != 0 && wp->w_buffer == curbuf) { - wp->w_redr_status = TRUE; + if (wp->w_status_height != 0 && wp->w_buffer == buf) { + wp->w_redr_status = true; redraw_later(VALID); } } @@ -4534,7 +4507,7 @@ void redraw_statuslines(void) { FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { if (wp->w_redr_status) { - win_redr_status(wp); + win_redr_status(wp, false); } } if (redraw_tabline) @@ -4568,11 +4541,10 @@ static void draw_vsep_win(win_T *wp, int row) int c; if (wp->w_vsep_width) { - /* draw the vertical separator right of this window */ - c = fillchar_vsep(&hl); - screen_fill(wp->w_winrow + row, wp->w_winrow + wp->w_height, - W_ENDCOL(wp), W_ENDCOL(wp) + 1, - c, ' ', hl); + // draw the vertical separator right of this window + c = fillchar_vsep(wp, &hl); + grid_fill(&default_grid, wp->w_winrow + row, W_ENDROW(wp), + W_ENDCOL(wp), W_ENDCOL(wp) + 1, c, ' ', hl); } } @@ -4594,7 +4566,7 @@ static int status_match_len(expand_T *xp, char_u *s) while (*s != NUL) { s += skip_status_match_char(xp, s); len += ptr2cells(s); - mb_ptr_adv(s); + MB_PTR_ADV(s); } return len; @@ -4657,7 +4629,8 @@ win_redr_status_matches ( if (matches == NULL) /* interrupted completion? */ return; - buf = xmalloc(has_mbyte ? Columns * MB_MAXBYTES + 1 : Columns + 1); + buf = xmalloc(has_mbyte ? default_grid.Columns * MB_MAXBYTES + 1 + : default_grid.Columns + 1); if (match == -1) { /* don't show match but original text */ match = 0; @@ -4677,15 +4650,16 @@ win_redr_status_matches ( clen += status_match_len(xp, L_MATCH(i)) + 2; if (first_match > 0) clen += 2; - /* jumping right, put match at the left */ - if ((long)clen > Columns) { + // jumping right, put match at the left + if ((long)clen > default_grid.Columns) { first_match = match; /* if showing the last match, we can add some on the left */ clen = 2; for (i = match; i < num_matches; ++i) { clen += status_match_len(xp, L_MATCH(i)) + 2; - if ((long)clen >= Columns) + if ((long)clen >= default_grid.Columns) { break; + } } if (i == num_matches) add_left = TRUE; @@ -4694,12 +4668,13 @@ win_redr_status_matches ( if (add_left) while (first_match > 0) { clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2; - if ((long)clen >= Columns) + if ((long)clen >= default_grid.Columns) { break; - --first_match; + } + first_match--; } - fillchar = fillchar_status(&attr, TRUE); + fillchar = fillchar_status(&attr, curwin); if (first_match == 0) { *buf = NUL; @@ -4711,7 +4686,8 @@ win_redr_status_matches ( clen = len; i = first_match; - while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns) { + while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) + < default_grid.Columns) { if (i == match) { selstart = buf + len; selstart_col = clen; @@ -4762,12 +4738,12 @@ win_redr_status_matches ( if (msg_scrolled > 0) { /* Put the wildmenu just above the command line. If there is * no room, scroll the screen one line up. */ - if (cmdline_row == Rows - 1) { - screen_del_lines(0, 0, 1, (int)Rows, NULL); - ++msg_scrolled; + if (cmdline_row == default_grid.Rows - 1) { + grid_del_lines(&default_grid, 0, 1, (int)Rows, 0, (int)Columns); + msg_scrolled++; } else { - ++cmdline_row; - ++row; + cmdline_row++; + row++; } wild_menu_showing = WM_SCROLLED; } else { @@ -4785,25 +4761,26 @@ win_redr_status_matches ( } } - screen_puts(buf, row, 0, attr); + grid_puts(&default_grid, buf, row, 0, attr); if (selstart != NULL && highlight) { *selend = NUL; - screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM)); + grid_puts(&default_grid, selstart, row, selstart_col, HL_ATTR(HLF_WM)); } - screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr); + grid_fill(&default_grid, row, row + 1, clen, (int)default_grid.Columns, + fillchar, fillchar, attr); } win_redraw_last_status(topframe); xfree(buf); } -/* - * Redraw the status line of window wp. - * - * If inversion is possible we use it. Else '=' characters are used. - */ -void win_redr_status(win_T *wp) +/// Redraw the status line of window `wp`. +/// +/// If inversion is possible we use it. Else '=' characters are used. +/// If "ignore_pum" is true, also redraw statusline when the popup menu is +/// displayed. +static void win_redr_status(win_T *wp, int ignore_pum) { int row; char_u *p; @@ -4813,28 +4790,28 @@ void win_redr_status(win_T *wp) int this_ru_col; static int busy = FALSE; - /* It's possible to get here recursively when 'statusline' (indirectly) - * invokes ":redrawstatus". Simply ignore the call then. */ - if (busy) + // May get here recursively when 'statusline' (indirectly) + // invokes ":redrawstatus". Simply ignore the call then. + if (busy + // Also ignore if wildmenu is showing. + || (wild_menu_showing != 0 && !ui_is_external(kUIWildmenu))) { return; - busy = TRUE; + } + busy = true; wp->w_redr_status = FALSE; if (wp->w_status_height == 0) { - /* no status line, can only be last window */ - redraw_cmdline = TRUE; - } else if (!redrawing() - /* don't update status line when popup menu is visible and may be - * drawn over it */ - || pum_visible() - ) { - /* Don't redraw right now, do it later. */ - wp->w_redr_status = TRUE; + // no status line, can only be last window + redraw_cmdline = true; + } else if (!redrawing() || (!ignore_pum && pum_drawn())) { + // Don't redraw right now, do it later. Don't update status line when + // popup menu is visible and may be drawn over it + wp->w_redr_status = true; } else if (*p_stl != NUL || *wp->w_p_stl != NUL) { /* redraw custom status line */ redraw_custom_statusline(wp); } else { - fillchar = fillchar_status(&attr, wp == curwin); + fillchar = fillchar_status(&attr, wp); get_trans_bufname(wp->w_buffer); p = NameBuff; @@ -4843,8 +4820,9 @@ void win_redr_status(win_T *wp) if (wp->w_buffer->b_help || wp->w_p_pvw || bufIsChanged(wp->w_buffer) - || wp->w_buffer->b_p_ro) + || wp->w_buffer->b_p_ro) { *(p + len++) = ' '; + } if (wp->w_buffer->b_help) { STRCPY(p + len, _("[Help]")); len += (int)STRLEN(p + len); @@ -4859,48 +4837,45 @@ void win_redr_status(win_T *wp) } if (wp->w_buffer->b_p_ro) { STRCPY(p + len, _("[RO]")); - len += 4; + // len += (int)STRLEN(p + len); // dead assignment } this_ru_col = ru_col - (Columns - wp->w_width); - if (this_ru_col < (wp->w_width + 1) / 2) + if (this_ru_col < (wp->w_width + 1) / 2) { this_ru_col = (wp->w_width + 1) / 2; + } if (this_ru_col <= 1) { - p = (char_u *)"<"; /* No room for file name! */ + p = (char_u *)"<"; // No room for file name! len = 1; - } else if (has_mbyte) { + } else { int clen = 0, i; - /* Count total number of display cells. */ - clen = (int) mb_string2cells(p); + // Count total number of display cells. + clen = (int)mb_string2cells(p); - /* Find first character that will fit. - * Going from start to end is much faster for DBCS. */ + // Find first character that will fit. + // Going from start to end is much faster for DBCS. for (i = 0; p[i] != NUL && clen >= this_ru_col - 1; - i += (*mb_ptr2len)(p + i)) - clen -= (*mb_ptr2cells)(p + i); + i += utfc_ptr2len(p + i)) { + clen -= utf_ptr2cells(p + i); + } len = clen; if (i > 0) { p = p + i - 1; *p = '<'; ++len; } - - } else if (len > this_ru_col - 1) { - p += len - (this_ru_col - 1); - *p = '<'; - len = this_ru_col - 1; } - row = wp->w_winrow + wp->w_height; - screen_puts(p, row, wp->w_wincol, attr); - screen_fill(row, row + 1, len + wp->w_wincol, - this_ru_col + wp->w_wincol, fillchar, fillchar, attr); + row = W_ENDROW(wp); + grid_puts(&default_grid, p, row, wp->w_wincol, attr); + grid_fill(&default_grid, row, row + 1, len + wp->w_wincol, + this_ru_col + wp->w_wincol, fillchar, fillchar, attr); - if (get_keymap_str(wp, NameBuff, MAXPATHL) + if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL) && this_ru_col - len > (int)(STRLEN(NameBuff) + 1)) - screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff) - - 1 + wp->w_wincol), attr); + grid_puts(&default_grid, NameBuff, row, + (int)(this_ru_col - STRLEN(NameBuff) - 1), attr); win_redr_ruler(wp, TRUE); } @@ -4909,12 +4884,12 @@ void win_redr_status(win_T *wp) * May need to draw the character below the vertical separator. */ if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing()) { - if (stl_connected(wp)) - fillchar = fillchar_status(&attr, wp == curwin); - else - fillchar = fillchar_vsep(&attr); - screen_putchar(fillchar, wp->w_winrow + wp->w_height, - W_ENDCOL(wp), attr); + if (stl_connected(wp)) { + fillchar = fillchar_status(&attr, wp); + } else { + fillchar = fillchar_vsep(wp, &attr); + } + grid_putchar(&default_grid, fillchar, W_ENDROW(wp), W_ENDCOL(wp), attr); } busy = FALSE; } @@ -4925,8 +4900,8 @@ void win_redr_status(win_T *wp) */ static void redraw_custom_statusline(win_T *wp) { - static int entered = FALSE; - int save_called_emsg = called_emsg; + static int entered = false; + int saved_did_emsg = did_emsg; /* When called recursively return. This can happen when the statusline * contains an expression that triggers a redraw. */ @@ -4934,18 +4909,18 @@ static void redraw_custom_statusline(win_T *wp) return; entered = TRUE; - called_emsg = FALSE; - win_redr_custom(wp, FALSE); - if (called_emsg) { - /* When there is an error disable the statusline, otherwise the - * display is messed up with errors and a redraw triggers the problem - * again and again. */ + did_emsg = false; + win_redr_custom(wp, false); + if (did_emsg) { + // When there is an error disable the statusline, otherwise the + // display is messed up with errors and a redraw triggers the problem + // again and again. set_string_option_direct((char_u *)"statusline", -1, (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR); } - called_emsg |= save_called_emsg; - entered = FALSE; + did_emsg |= saved_did_emsg; + entered = false; } /* @@ -4978,8 +4953,9 @@ int stl_connected(win_T *wp) int get_keymap_str ( win_T *wp, - char_u *buf, /* buffer for the result */ - int len /* length of buffer */ + char_u *fmt, // format string containing one %s item + char_u *buf, // buffer for the result + int len // length of buffer ) { char_u *p; @@ -5006,10 +4982,9 @@ get_keymap_str ( else p = (char_u *)"lang"; } - if ((int)(STRLEN(p) + 3) < len) - sprintf((char *)buf, "<%s>", p); - else + if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1) { buf[0] = NUL; + } xfree(s); } return buf[0] != NUL; @@ -5057,12 +5032,12 @@ win_redr_custom ( stl = p_tal; row = 0; fillchar = ' '; - attr = hl_attr(HLF_TPF); - maxwidth = Columns; + attr = HL_ATTR(HLF_TPF); + maxwidth = default_grid.Columns; use_sandbox = was_set_insecurely((char_u *)"tabline", 0); } else { - row = wp->w_winrow + wp->w_height; - fillchar = fillchar_status(&attr, wp == curwin); + row = W_ENDROW(wp); + fillchar = fillchar_status(&attr, wp); maxwidth = wp->w_width; if (draw_ruler) { @@ -5077,13 +5052,14 @@ win_redr_custom ( if (*stl++ != '(') stl = p_ruf; } - col = ru_col - (Columns - wp->w_width); - if (col < (wp->w_width + 1) / 2) + col = ru_col - (default_grid.Columns - wp->w_width); + if (col < (wp->w_width + 1) / 2) { col = (wp->w_width + 1) / 2; + } maxwidth = wp->w_width - col; if (!wp->w_status_height) { - row = Rows - 1; - --maxwidth; /* writing in last column may cause scrolling */ + row = default_grid.Rows - 1; + maxwidth--; // writing in last column may cause scrolling fillchar = ' '; attr = 0; } @@ -5119,28 +5095,30 @@ win_redr_custom ( xfree(stl); ewp->w_p_crb = p_crb_save; - /* Make all characters printable. */ - p = transstr(buf); + // Make all characters printable. + p = (char_u *)transstr((const char *)buf); len = STRLCPY(buf, p, sizeof(buf)); len = (size_t)len < sizeof(buf) ? len : (int)sizeof(buf) - 1; xfree(p); /* fill up with "fillchar" */ while (width < maxwidth && len < (int)sizeof(buf) - 1) { - len += (*mb_char2bytes)(fillchar, buf + len); - ++width; + len += utf_char2bytes(fillchar, buf + len); + width++; } buf[len] = NUL; /* * Draw each snippet with the specified highlighting. */ + screen_puts_line_start(row); + curattr = attr; p = buf; for (n = 0; hltab[n].start != NULL; n++) { - int len = (int)(hltab[n].start - p); - screen_puts_len(p, len, row, col, curattr); - col += vim_strnsize(p, len); + int textlen = (int)(hltab[n].start - p); + grid_puts_len(&default_grid, p, textlen, row, col, curattr); + col += vim_strnsize(p, textlen); p = hltab[n].start; if (hltab[n].userhl == 0) @@ -5153,7 +5131,10 @@ win_redr_custom ( curattr = highlight_user[hltab[n].userhl - 1]; } // Make sure to use an empty string instead of p, if p is beyond buf + len. - screen_puts(p >= buf + len ? (char_u *)"" : p, row, col, curattr); + grid_puts(&default_grid, p >= buf + len ? (char_u *)"" : p, row, col, + curattr); + + grid_puts_line_flush(&default_grid, false); if (wp == NULL) { // Fill the tab_page_click_defs array for clicking in the tab pages line. @@ -5171,7 +5152,7 @@ win_redr_custom ( p = (char_u *) tabtab[n].start; cur_click_def = tabtab[n].def; } - while (col < Columns) { + while (col < default_grid.Columns) { tab_page_click_defs[col++] = cur_click_def; } } @@ -5180,86 +5161,144 @@ theend: entered = FALSE; } +// Low-level functions to manipulate invidual character cells on the +// screen grid. -/* - * Output a single character directly to the screen and update ScreenLines. - */ -void screen_putchar(int c, int row, int col, int attr) +/// Put a ASCII character in a screen cell. +static void schar_from_ascii(char_u *p, const char c) { - char_u buf[MB_MAXBYTES + 1]; + p[0] = c; + p[1] = 0; +} + +/// Put a unicode character in a screen cell. +static int schar_from_char(char_u *p, int c) +{ + int len = utf_char2bytes(c, p); + p[len] = NUL; + return len; +} - if (has_mbyte) - buf[(*mb_char2bytes)(c, buf)] = NUL; - else { - buf[0] = c; - buf[1] = NUL; +/// Put a unicode char, and up to MAX_MCO composing chars, in a screen cell. +static int schar_from_cc(char_u *p, int c, int u8cc[MAX_MCO]) +{ + int len = utf_char2bytes(c, p); + for (int i = 0; i < MAX_MCO; i++) { + if (u8cc[i] == 0) { + break; + } + len += utf_char2bytes(u8cc[i], p + len); } - screen_puts(buf, row, col, attr); + p[len] = 0; + return len; } -/* - * Get a single character directly from ScreenLines into "bytes[]". - * Also return its attribute in *attrp; - */ -void screen_getbytes(int row, int col, char_u *bytes, int *attrp) +/// compare the contents of two screen cells. +static int schar_cmp(char_u *sc1, char_u *sc2) { - unsigned off; + return STRNCMP(sc1, sc2, sizeof(schar_T)); +} - /* safety check */ - if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns) { - off = LineOffset[row] + col; - *attrp = ScreenAttrs[off]; - bytes[0] = ScreenLines[off]; - bytes[1] = NUL; +/// copy the contents of screen cell `sc2` into cell `sc1` +static void schar_copy(char_u *sc1, char_u *sc2) +{ + STRLCPY(sc1, sc2, sizeof(schar_T)); +} - if (enc_utf8 && ScreenLinesUC[off] != 0) - bytes[utfc_char2bytes(off, bytes)] = NUL; - else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e) { - bytes[0] = ScreenLines[off]; - bytes[1] = ScreenLines2[off]; - bytes[2] = NUL; - } else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1) { - bytes[1] = ScreenLines[off + 1]; - bytes[2] = NUL; - } +static int line_off2cells(schar_T *line, size_t off, size_t max_off) +{ + return (off + 1 < max_off && line[off + 1][0] == 0) ? 2 : 1; +} + +/// Return number of display cells for char at grid->chars[off]. +/// We make sure that the offset used is less than "max_off". +static int grid_off2cells(ScreenGrid *grid, size_t off, size_t max_off) +{ + return line_off2cells(grid->chars, off, max_off); +} + +/// Return true if the character at "row"/"col" on the screen is the left side +/// of a double-width character. +/// +/// Caller must make sure "row" and "col" are not invalid! +bool grid_lefthalve(ScreenGrid *grid, int row, int col) +{ + screen_adjust_grid(&grid, &row, &col); + + return grid_off2cells(grid, grid->line_offset[row] + col, + grid->line_offset[row] + grid->Columns) > 1; +} + +/// Correct a position on the screen, if it's the right half of a double-wide +/// char move it to the left half. Returns the corrected column. +int grid_fix_col(ScreenGrid *grid, int col, int row) +{ + int coloff = 0; + screen_adjust_grid(&grid, &row, &coloff); + + col += coloff; + if (grid->chars != NULL && col > 0 + && grid->chars[grid->line_offset[row] + col][0] == 0) { + return col - 1 - coloff; } + return col - coloff; } +/// output a single character directly to the grid +void grid_putchar(ScreenGrid *grid, int c, int row, int col, int attr) +{ + char_u buf[MB_MAXBYTES + 1]; -/* - * Return TRUE if composing characters for screen posn "off" differs from - * composing characters in "u8cc". - * Only to be used when ScreenLinesUC[off] != 0. - */ -static int screen_comp_differs(int off, int *u8cc) + buf[utf_char2bytes(c, buf)] = NUL; + grid_puts(grid, buf, row, col, attr); +} + +/// get a single character directly from grid.chars into "bytes[]". +/// Also return its attribute in *attrp; +void grid_getbytes(ScreenGrid *grid, int row, int col, char_u *bytes, + int *attrp) { - int i; + unsigned off; - for (i = 0; i < Screen_mco; ++i) { - if (ScreenLinesC[i][off] != (u8char_T)u8cc[i]) - return TRUE; - if (u8cc[i] == 0) - break; + screen_adjust_grid(&grid, &row, &col); + + + // safety check + if (grid->chars != NULL && row < grid->Rows && col < grid->Columns) { + off = grid->line_offset[row] + col; + *attrp = grid->attrs[off]; + schar_copy(bytes, grid->chars[off]); } - return FALSE; } -/* - * Put string '*text' on the screen at position 'row' and 'col', with - * attributes 'attr', and update ScreenLines[] and ScreenAttrs[]. - * Note: only outputs within one row, message is truncated at screen boundary! - * Note: if ScreenLines[], row and/or col is invalid, nothing is done. - */ -void screen_puts(char_u *text, int row, int col, int attr) + +/// put string '*text' on the window grid at position 'row' and 'col', with +/// attributes 'attr', and update chars[] and attrs[]. +/// Note: only outputs within one row, message is truncated at grid boundary! +/// Note: if grid, row and/or col is invalid, nothing is done. +void grid_puts(ScreenGrid *grid, char_u *text, int row, int col, int attr) { - screen_puts_len(text, -1, row, col, attr); + grid_puts_len(grid, text, -1, row, col, attr); } -/* - * Like screen_puts(), but output "text[len]". When "len" is -1 output up to - * a NUL. - */ -void screen_puts_len(char_u *text, int textlen, int row, int col, int attr) +static int put_dirty_row = -1; +static int put_dirty_first = -1; +static int put_dirty_last = 0; + +/// Start a group of screen_puts_len calls that builds a single screen line. +/// +/// Must be matched with a screen_puts_line_flush call before moving to +/// another line. +void screen_puts_line_start(int row) +{ + assert(put_dirty_row == -1); + put_dirty_row = row; +} + +/// like grid_puts(), but output "text[len]". When "len" is -1 output up to +/// a NUL. +void grid_puts_len(ScreenGrid *grid, char_u *text, int textlen, int row, + int col, int attr) { unsigned off; char_u *ptr = text; @@ -5277,181 +5316,160 @@ void screen_puts_len(char_u *text, int textlen, int row, int col, int attr) int force_redraw_this; int force_redraw_next = FALSE; int need_redraw; + bool do_flush = false; - const int l_has_mbyte = has_mbyte; - const bool l_enc_utf8 = enc_utf8; - const int l_enc_dbcs = enc_dbcs; + screen_adjust_grid(&grid, &row, &col); - assert((l_has_mbyte == (l_enc_utf8 || l_enc_dbcs)) - && !(l_enc_utf8 && l_enc_dbcs)); - - if (ScreenLines == NULL || row >= screen_Rows) /* safety check */ + // safety check + if (grid->chars == NULL || row >= grid->Rows || col >= grid->Columns) { return; - off = LineOffset[row] + col; + } + + if (put_dirty_row == -1) { + screen_puts_line_start(row); + do_flush = true; + } else { + if (row != put_dirty_row) { + abort(); + } + } + off = grid->line_offset[row] + col; /* When drawing over the right halve of a double-wide char clear out the * left halve. Only needed in a terminal. */ - if (l_has_mbyte && col > 0 && col < screen_Columns - && mb_fix_col(col, row) != col) { - ScreenLines[off - 1] = ' '; - ScreenAttrs[off - 1] = 0; - if (l_enc_utf8) { - ScreenLinesUC[off - 1] = 0; - ScreenLinesC[0][off - 1] = 0; - } - /* redraw the previous cell, make it empty */ - screen_char(off - 1, row, col - 1); - /* force the cell at "col" to be redrawn */ - force_redraw_next = TRUE; - } - - max_off = LineOffset[row] + screen_Columns; - while (col < screen_Columns + if (col > 0 && col < grid->Columns && grid_fix_col(grid, col, row) != col) { + schar_from_ascii(grid->chars[off - 1], ' '); + grid->attrs[off - 1] = 0; + // redraw the previous cell, make it empty + if (put_dirty_first == -1) { + put_dirty_first = col-1; + } + put_dirty_last = col+1; + // force the cell at "col" to be redrawn + force_redraw_next = true; + } + + max_off = grid->line_offset[row] + grid->Columns; + while (col < grid->Columns && (len < 0 || (int)(ptr - text) < len) && *ptr != NUL) { c = *ptr; - /* check if this is the first byte of a multibyte */ - if (l_has_mbyte) { - if (l_enc_utf8 && len > 0) - mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr)); - else - mbyte_blen = (*mb_ptr2len)(ptr); - if (l_enc_dbcs == DBCS_JPNU && c == 0x8e) - mbyte_cells = 1; - else if (l_enc_dbcs != 0) - mbyte_cells = mbyte_blen; - else { /* enc_utf8 */ - if (len >= 0) - u8c = utfc_ptr2char_len(ptr, u8cc, - (int)((text + len) - ptr)); - else - u8c = utfc_ptr2char(ptr, u8cc); - mbyte_cells = utf_char2cells(u8c); - if (p_arshape && !p_tbidi && arabic_char(u8c)) { - /* Do Arabic shaping. */ - if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len) { - /* Past end of string to be displayed. */ - nc = NUL; - nc1 = NUL; - } else { - nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc, - (int)((text + len) - ptr - mbyte_blen)); - nc1 = pcc[0]; - } - pc = prev_c; - prev_c = u8c; - u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc); - } else - prev_c = u8c; - if (col + mbyte_cells > screen_Columns) { - /* Only 1 cell left, but character requires 2 cells: - * display a '>' in the last column to avoid wrapping. */ - c = '>'; - mbyte_cells = 1; - } + // check if this is the first byte of a multibyte + if (len > 0) { + mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr)); + } else { + mbyte_blen = utfc_ptr2len(ptr); + } + if (len >= 0) { + u8c = utfc_ptr2char_len(ptr, u8cc, (int)((text + len) - ptr)); + } else { + u8c = utfc_ptr2char(ptr, u8cc); + } + mbyte_cells = utf_char2cells(u8c); + if (p_arshape && !p_tbidi && arabic_char(u8c)) { + // Do Arabic shaping. + if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len) { + // Past end of string to be displayed. + nc = NUL; + nc1 = NUL; + } else { + nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc, + (int)((text + len) - ptr - mbyte_blen)); + nc1 = pcc[0]; } + pc = prev_c; + prev_c = u8c; + u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc); + } else { + prev_c = u8c; + } + if (col + mbyte_cells > grid->Columns) { + // Only 1 cell left, but character requires 2 cells: + // display a '>' in the last column to avoid wrapping. */ + c = '>'; + mbyte_cells = 1; } + schar_T buf; + schar_from_cc(buf, u8c, u8cc); + + force_redraw_this = force_redraw_next; force_redraw_next = FALSE; - need_redraw = ScreenLines[off] != c - || (mbyte_cells == 2 - && ScreenLines[off + 1] != (l_enc_dbcs ? ptr[1] : 0)) - || (l_enc_dbcs == DBCS_JPNU - && c == 0x8e - && ScreenLines2[off] != ptr[1]) - || (l_enc_utf8 - && (ScreenLinesUC[off] != - (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c) - || (ScreenLinesUC[off] != 0 - && screen_comp_differs(off, u8cc)))) - || ScreenAttrs[off] != attr + need_redraw = schar_cmp(grid->chars[off], buf) + || (mbyte_cells == 2 && grid->chars[off + 1][0] != 0) + || grid->attrs[off] != attr || exmode_active; - if (need_redraw - || force_redraw_this - ) { - /* When at the end of the text and overwriting a two-cell - * character with a one-cell character, need to clear the next - * cell. Also when overwriting the left halve of a two-cell char - * with the right halve of a two-cell char. Do this only once - * (mb_off2cells() may return 2 on the right halve). */ - if (clear_next_cell) - clear_next_cell = FALSE; - else if (l_has_mbyte - && (len < 0 ? ptr[mbyte_blen] == NUL - : ptr + mbyte_blen >= text + len) - && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1) - || (mbyte_cells == 2 - && (*mb_off2cells)(off, max_off) == 1 - && (*mb_off2cells)(off + 1, max_off) > 1))) - clear_next_cell = TRUE; - - /* Make sure we never leave a second byte of a double-byte behind, - * it confuses mb_off2cells(). */ - if (l_enc_dbcs - && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1) - || (mbyte_cells == 2 - && (*mb_off2cells)(off, max_off) == 1 - && (*mb_off2cells)(off + 1, max_off) > 1))) - ScreenLines[off + mbyte_blen] = 0; - ScreenLines[off] = c; - ScreenAttrs[off] = attr; - if (l_enc_utf8) { - if (c < 0x80 && u8cc[0] == 0) - ScreenLinesUC[off] = 0; - else { - int i; - - ScreenLinesUC[off] = u8c; - for (i = 0; i < Screen_mco; ++i) { - ScreenLinesC[i][off] = u8cc[i]; - if (u8cc[i] == 0) - break; - } - } - if (mbyte_cells == 2) { - ScreenLines[off + 1] = 0; - ScreenAttrs[off + 1] = attr; - } - screen_char(off, row, col); - } else if (mbyte_cells == 2) { - ScreenLines[off + 1] = ptr[1]; - ScreenAttrs[off + 1] = attr; - screen_char_2(off, row, col); - } else if (l_enc_dbcs == DBCS_JPNU && c == 0x8e) { - ScreenLines2[off] = ptr[1]; - screen_char(off, row, col); - } else - screen_char(off, row, col); - } - if (l_has_mbyte) { - off += mbyte_cells; - col += mbyte_cells; - ptr += mbyte_blen; + if (need_redraw || force_redraw_this) { + // When at the end of the text and overwriting a two-cell + // character with a one-cell character, need to clear the next + // cell. Also when overwriting the left halve of a two-cell char + // with the right halve of a two-cell char. Do this only once + // (utf8_off2cells() may return 2 on the right halve). if (clear_next_cell) { - // This only happens at the end, display one space next. - ptr = (char_u *)" "; - len = -1; + clear_next_cell = false; + } else if ((len < 0 ? ptr[mbyte_blen] == NUL + : ptr + mbyte_blen >= text + len) + && ((mbyte_cells == 1 + && grid_off2cells(grid, off, max_off) > 1) + || (mbyte_cells == 2 + && grid_off2cells(grid, off, max_off) == 1 + && grid_off2cells(grid, off + 1, max_off) > 1))) { + clear_next_cell = true; } - } else { - ++off; - ++col; - ++ptr; + + schar_copy(grid->chars[off], buf); + grid->attrs[off] = attr; + if (mbyte_cells == 2) { + grid->chars[off + 1][0] = 0; + grid->attrs[off + 1] = attr; + } + if (put_dirty_first == -1) { + put_dirty_first = col; + } + put_dirty_last = col+mbyte_cells; + } + + off += mbyte_cells; + col += mbyte_cells; + ptr += mbyte_blen; + if (clear_next_cell) { + // This only happens at the end, display one space next. + ptr = (char_u *)" "; + len = -1; } } - /* If we detected the next character needs to be redrawn, but the text - * doesn't extend up to there, update the character here. */ - if (force_redraw_next && col < screen_Columns) { - if (l_enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1) - screen_char_2(off, row, col); - else - screen_char(off, row, col); + if (do_flush) { + grid_puts_line_flush(grid, true); } } +/// End a group of screen_puts_len calls and send the screen buffer to the UI +/// layer. +/// +/// @param grid The grid which contains the buffer. +/// @param set_cursor Move the visible cursor to the end of the changed region. +/// This is a workaround for not yet refactored code paths +/// and shouldn't be used in new code. +void grid_puts_line_flush(ScreenGrid *grid, bool set_cursor) +{ + assert(put_dirty_row != -1); + if (put_dirty_first != -1) { + if (set_cursor) { + ui_grid_cursor_goto(grid->handle, put_dirty_row, + MIN(put_dirty_last, grid->Columns-1)); + } + ui_line(grid, put_dirty_row, put_dirty_first, put_dirty_last, + put_dirty_last, 0, false); + put_dirty_first = -1; + put_dirty_last = 0; + } + put_dirty_row = -1; +} + /* * Prepare for 'hlsearch' highlighting. */ @@ -5459,8 +5477,7 @@ static void start_search_hl(void) { if (p_hls && !no_hlsearch) { last_pat_prog(&search_hl.rm); - search_hl.attr = hl_attr(HLF_L); - /* Set the time limit to 'redrawtime'. */ + // Set the time limit to 'redrawtime'. search_hl.tm = profile_setlimit(p_rdt); } } @@ -5476,6 +5493,7 @@ static void end_search_hl(void) } } + /* * Init for calling prepare_search_hl(). */ @@ -5502,7 +5520,9 @@ static void init_search_hl(win_T *wp) search_hl.buf = wp->w_buffer; search_hl.lnum = 0; search_hl.first_lnum = 0; - /* time limit is set at the toplevel, for all windows */ + search_hl.attr = win_hl_attr(wp, HLF_L); + + // time limit is set at the toplevel, for all windows } /* @@ -5522,22 +5542,25 @@ static void prepare_search_hl(win_T *wp, linenr_T lnum) * Do this both for search_hl and the match list. */ cur = wp->w_match_head; - shl_flag = FALSE; - while (cur != NULL || shl_flag == FALSE) { - if (shl_flag == FALSE) { + shl_flag = false; + while (cur != NULL || shl_flag == false) { + if (shl_flag == false) { shl = &search_hl; - shl_flag = TRUE; - } else - shl = &cur->hl; + shl_flag = true; + } else { + shl = &cur->hl; // -V595 + } if (shl->rm.regprog != NULL && shl->lnum == 0 && re_multiline(shl->rm.regprog)) { if (shl->first_lnum == 0) { for (shl->first_lnum = lnum; - shl->first_lnum > wp->w_topline; --shl->first_lnum) - if (hasFoldingWin(wp, shl->first_lnum - 1, - NULL, NULL, TRUE, NULL)) + shl->first_lnum > wp->w_topline; + shl->first_lnum--) { + if (hasFoldingWin(wp, shl->first_lnum - 1, NULL, NULL, true, NULL)) { break; + } + } } if (cur != NULL) { cur->pos.cur = 0; @@ -5546,8 +5569,9 @@ static void prepare_search_hl(win_T *wp, linenr_T lnum) // in progress n = 0; while (shl->first_lnum < lnum && (shl->rm.regprog != NULL - || (cur != NULL && pos_inprogress))) { - next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur); + || (cur != NULL && pos_inprogress))) { + next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, + shl == &search_hl ? NULL : cur); pos_inprogress = !(cur == NULL || cur->pos.cur == 0); if (shl->lnum != 0) { shl->first_lnum = shl->lnum @@ -5585,6 +5609,7 @@ next_search_hl ( linenr_T l; colnr_T matchcol; long nmatched = 0; + int save_called_emsg = called_emsg; if (shl->lnum != 0) { /* Check for three situations: @@ -5677,9 +5702,14 @@ next_search_hl ( shl->lnum += shl->rm.startpos[0].lnum; break; /* useful match found */ } + + // Restore called_emsg for assert_fails(). + called_emsg = save_called_emsg; } } +/// If there is a match fill "shl" and return one. +/// Return zero otherwise. static int next_search_hl_pos( match_T *shl, // points to a match @@ -5689,254 +5719,148 @@ next_search_hl_pos( ) { int i; - int bot = -1; + int found = -1; shl->lnum = 0; for (i = posmatch->cur; i < MAXPOSMATCH; i++) { - if (posmatch->pos[i].lnum == 0) { + llpos_T *pos = &posmatch->pos[i]; + + if (pos->lnum == 0) { break; } - if (posmatch->pos[i].col < mincol) { + if (pos->len == 0 && pos->col < mincol) { continue; } - if (posmatch->pos[i].lnum == lnum) { - if (bot != -1) { - // partially sort positions by column numbers - // on the same line - if (posmatch->pos[i].col < posmatch->pos[bot].col) { - llpos_T tmp = posmatch->pos[i]; + if (pos->lnum == lnum) { + if (found >= 0) { + // if this match comes before the one at "found" then swap + // them + if (pos->col < posmatch->pos[found].col) { + llpos_T tmp = *pos; - posmatch->pos[i] = posmatch->pos[bot]; - posmatch->pos[bot] = tmp; + *pos = posmatch->pos[found]; + posmatch->pos[found] = tmp; } } else { - bot = i; - shl->lnum = lnum; + found = i; } } } posmatch->cur = 0; - if (bot != -1) { - colnr_T start = posmatch->pos[bot].col == 0 - ? 0: posmatch->pos[bot].col - 1; - colnr_T end = posmatch->pos[bot].col == 0 - ? MAXCOL : start + posmatch->pos[bot].len; + if (found >= 0) { + colnr_T start = posmatch->pos[found].col == 0 + ? 0: posmatch->pos[found].col - 1; + colnr_T end = posmatch->pos[found].col == 0 + ? MAXCOL : start + posmatch->pos[found].len; + shl->lnum = lnum; shl->rm.startpos[0].lnum = 0; shl->rm.startpos[0].col = start; shl->rm.endpos[0].lnum = 0; shl->rm.endpos[0].col = end; - posmatch->cur = bot + 1; - return true; + shl->is_addpos = true; + posmatch->cur = found + 1; + return 1; } - return false; + return 0; } -static void screen_start_highlight(int attr) -{ - screen_attr = attr; - ui_start_highlight(attr); -} -void screen_stop_highlight(void) +/// Fill the grid from 'start_row' to 'end_row', from 'start_col' to 'end_col' +/// with character 'c1' in first column followed by 'c2' in the other columns. +/// Use attributes 'attr'. +void grid_fill(ScreenGrid *grid, int start_row, int end_row, int start_col, + int end_col, int c1, int c2, int attr) { - ui_stop_highlight(); - screen_attr = 0; -} + schar_T sc; -/* - * Put character ScreenLines["off"] on the screen at position "row" and "col", - * using the attributes from ScreenAttrs["off"]. - */ -static void screen_char(unsigned off, int row, int col) -{ - int attr; - - /* Check for illegal values, just in case (could happen just after - * resizing). */ - if (row >= screen_Rows || col >= screen_Columns) - return; + int row_off = 0, col_off = 0; + screen_adjust_grid(&grid, &row_off, &col_off); + start_row += row_off; + end_row += row_off; + start_col += col_off; + end_col += col_off; - /* Outputting the last character on the screen may scrollup the screen. - * Don't to it! Mark the character invalid (update it when scrolled up) */ - if (row == screen_Rows - 1 && col == screen_Columns - 1 - /* account for first command-line character in rightleft mode */ - && !cmdmsg_rl - ) { - ScreenAttrs[off] = (sattr_T)-1; - return; + // safety check + if (end_row > grid->Rows) { + end_row = grid->Rows; } - - /* - * Stop highlighting first, so it's easier to move the cursor. - */ - attr = ScreenAttrs[off]; - if (screen_attr != attr) - screen_stop_highlight(); - - ui_cursor_goto(row, col); - - if (screen_attr != attr) - screen_start_highlight(attr); - - if (enc_utf8 && ScreenLinesUC[off] != 0) { - char_u buf[MB_MAXBYTES + 1]; - - // Convert UTF-8 character to bytes and write it. - buf[utfc_char2bytes(off, buf)] = NUL; - ui_puts(buf); - } else { - ui_putc(ScreenLines[off]); - // double-byte character in single-width cell - if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e) { - ui_putc(ScreenLines2[off]); - } + if (end_col > grid->Columns) { + end_col = grid->Columns; } -} - -/* - * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"] - * on the screen at position 'row' and 'col'. - * The attributes of the first byte is used for all. This is required to - * output the two bytes of a double-byte character with nothing in between. - */ -static void screen_char_2(unsigned off, int row, int col) -{ - /* Check for illegal values (could be wrong when screen was resized). */ - if (off + 1 >= (unsigned)(screen_Rows * screen_Columns)) - return; - /* Outputting the last character on the screen may scrollup the screen. - * Don't to it! Mark the character invalid (update it when scrolled up) */ - if (row == screen_Rows - 1 && col >= screen_Columns - 2) { - ScreenAttrs[off] = (sattr_T)-1; + // nothing to do + if (grid->chars == NULL || start_row >= end_row || start_col >= end_col) { return; } - /* Output the first byte normally (positions the cursor), then write the - * second byte directly. */ - screen_char(off, row, col); - ui_putc(ScreenLines[off + 1]); -} - -/* - * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col' - * with character 'c1' in first column followed by 'c2' in the other columns. - * Use attributes 'attr'. - */ -void screen_fill(int start_row, int end_row, int start_col, int end_col, int c1, int c2, int attr) -{ - int row; - int col; - int off; - int end_off; - int did_delete; - int c; - - if (end_row > screen_Rows) /* safety check */ - end_row = screen_Rows; - if (end_col > screen_Columns) /* safety check */ - end_col = screen_Columns; - if (ScreenLines == NULL - || start_row >= end_row - || start_col >= end_col) /* nothing to do */ - return; - - /* it's a "normal" terminal when not in a GUI or cterm */ - for (row = start_row; row < end_row; ++row) { + for (int row = start_row; row < end_row; row++) { if (has_mbyte) { // When drawing over the right halve of a double-wide char clear // out the left halve. When drawing over the left halve of a // double wide-char clear out the right halve. Only needed in a // terminal. - if (start_col > 0 && mb_fix_col(start_col, row) != start_col) { - screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0); + if (start_col > 0 && grid_fix_col(grid, start_col, row) != start_col) { + grid_puts_len(grid, (char_u *)" ", 1, row, start_col - 1, 0); } - if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col) { - screen_puts_len((char_u *)" ", 1, row, end_col, 0); + if (end_col < grid->Columns + && grid_fix_col(grid, end_col, row) != end_col) { + grid_puts_len(grid, (char_u *)" ", 1, row, end_col, 0); } } - /* - * Try to use delete-line termcap code, when no attributes or in a - * "normal" terminal, where a bold/italic space is just a - * space. - */ - did_delete = FALSE; - if (c2 == ' ' - && end_col == Columns - && attr == 0) { - /* - * check if we really need to clear something - */ - col = start_col; - if (c1 != ' ') /* don't clear first char */ - ++col; - - off = LineOffset[row] + col; - end_off = LineOffset[row] + end_col; - /* skip blanks (used often, keep it fast!) */ - if (enc_utf8) - while (off < end_off && ScreenLines[off] == ' ' - && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0) - ++off; - else - while (off < end_off && ScreenLines[off] == ' ' - && ScreenAttrs[off] == 0) - ++off; - if (off < end_off) { /* something to be cleared */ - col = off - LineOffset[row]; - screen_stop_highlight(); - ui_cursor_goto(row, col); // clear rest of this screen line - ui_eol_clear(); - col = end_col - col; - while (col--) { /* clear chars in ScreenLines */ - ScreenLines[off] = ' '; - if (enc_utf8) - ScreenLinesUC[off] = 0; - ScreenAttrs[off] = 0; - ++off; + // if grid was resized (in ext_multigrid mode), the UI has no redraw updates + // for the newly resized grid. It is better mark everything as dirty and + // send all the updates. + int dirty_first = INT_MAX; + int dirty_last = 0; + + int col = start_col; + schar_from_char(sc, c1); + int lineoff = grid->line_offset[row]; + for (col = start_col; col < end_col; col++) { + int off = lineoff + col; + if (schar_cmp(grid->chars[off], sc) + || grid->attrs[off] != attr) { + schar_copy(grid->chars[off], sc); + grid->attrs[off] = attr; + if (dirty_first == INT_MAX) { + dirty_first = col; + } + dirty_last = col+1; + } + if (col == start_col) { + schar_from_char(sc, c2); + } + } + if (dirty_last > dirty_first) { + // TODO(bfredl): support a cleared suffix even with a batched line? + if (put_dirty_row == row) { + if (put_dirty_first == -1) { + put_dirty_first = dirty_first; } + put_dirty_last = MAX(put_dirty_last, dirty_last); + } else { + int last = c2 != ' ' ? dirty_last : dirty_first + (c1 != ' '); + ui_line(grid, row, dirty_first, last, dirty_last, attr, false); } - did_delete = TRUE; /* the chars are cleared now */ } - off = LineOffset[row] + start_col; - c = c1; - for (col = start_col; col < end_col; ++col) { - if (ScreenLines[off] != c - || (enc_utf8 && (int)ScreenLinesUC[off] - != (c >= 0x80 ? c : 0)) - || ScreenAttrs[off] != attr - ) { - ScreenLines[off] = c; - if (enc_utf8) { - if (c >= 0x80) { - ScreenLinesUC[off] = c; - ScreenLinesC[0][off] = 0; - } else - ScreenLinesUC[off] = 0; - } - ScreenAttrs[off] = attr; - if (!did_delete || c != ' ') - screen_char(off, row, col); + if (end_col == grid->Columns) { + grid->line_wraps[row] = false; + } + + // TODO(bfredl): The relevant caller should do this + if (row == default_grid.Rows - 1) { // overwritten the command line + redraw_cmdline = true; + if (start_col == 0 && end_col == Columns + && c1 == ' ' && c2 == ' ' && attr == 0) { + clear_cmdline = false; // command line has been cleared } - ++off; - if (col == start_col) { - if (did_delete) - break; - c = c2; + if (start_col == 0) { + mode_displayed = false; // mode cleared or overwritten } } - if (end_col == Columns) - LineWraps[row] = FALSE; - if (row == Rows - 1) { /* overwritten the command line */ - redraw_cmdline = TRUE; - if (c1 == ' ' && c2 == ' ') - clear_cmdline = FALSE; /* command line has been cleared */ - if (start_col == 0) - mode_displayed = FALSE; /* mode cleared or overwritten */ - } } } @@ -5965,57 +5889,106 @@ void check_for_delay(int check_msg_scroll) */ int screen_valid(int doclear) { - screenalloc(doclear); /* allocate screen buffers if size changed */ - return ScreenLines != NULL; + screenalloc(doclear); // allocate screen buffers if size changed + return default_grid.chars != NULL; } -/* - * Resize the shell to Rows and Columns. - * Allocate ScreenLines[] and associated items. - * - * There may be some time between setting Rows and Columns and (re)allocating - * ScreenLines[]. This happens when starting up and when (manually) changing - * the shell size. Always use screen_Rows and screen_Columns to access items - * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the - * final size of the shell is needed. - */ +/// (Re)allocates a window grid if size changed while in ext_multigrid mode. +/// Updates size, offsets and handle for the grid regardless. +/// +/// If "doclear" is true, don't try to copy from the old grid rather clear the +/// resized grid. +void win_grid_alloc(win_T *wp) +{ + ScreenGrid *grid = &wp->w_grid; + + int rows = grid->requested_rows; + if (rows == 0) { + rows = wp->w_height; + } + + int columns = grid->requested_cols; + if (columns == 0) { + columns = wp->w_width; + } + + // TODO(bfredl): floating windows should force this to true + bool want_allocation = ui_is_external(kUIMultigrid); + bool has_allocation = (grid->chars != NULL); + + if (want_allocation && has_allocation && highlights_invalid) { + grid_invalidate(grid); + } + + int was_resized = false; + if ((has_allocation != want_allocation) + || grid->Rows != rows + || grid->Columns != columns) { + if (want_allocation) { + grid_alloc(grid, rows, columns, true); + win_free_lsize(wp); + win_alloc_lines(wp); + } else { + // Single grid mode, all rendering will be redirected to default_grid. + // Only keep track of the size and offset of the window. + grid_free(grid); + grid->Rows = rows; + grid->Columns = columns; + } + was_resized = true; + } + + grid->row_offset = wp->w_winrow; + grid->col_offset = wp->w_wincol; + + // send grid resize event if: + // - a grid was just resized + // - screen_resize was called and all grid sizes must be sent + // - the UI wants multigrid event (necessary) + if ((send_grid_resize || was_resized) && ui_is_external(kUIMultigrid)) { + ui_call_grid_resize(grid->handle, grid->Columns, grid->Rows); + } +} + +/// assign a handle to the grid. The grid need not be allocated. +void grid_assign_handle(ScreenGrid *grid) +{ + static int last_grid_handle = DEFAULT_GRID_HANDLE; + + // only assign a grid handle if not already + if (grid->handle == 0) { + grid->handle = ++last_grid_handle; + } +} + +/// Resize the screen to Rows and Columns. +/// +/// Allocate default_grid.chars[] and other grid arrays. +/// +/// There may be some time between setting Rows and Columns and (re)allocating +/// default_grid arrays. This happens when starting up and when +/// (manually) changing the shell size. Always use default_grid.Rows and +/// default_grid.Columns to access items in default_grid.chars[]. Use Rows +/// and Columns for positioning text etc. where the final size of the shell is +/// needed. void screenalloc(bool doclear) { - int new_row, old_row; - int outofmem = FALSE; - int len; - schar_T *new_ScreenLines; - u8char_T *new_ScreenLinesUC = NULL; - u8char_T *new_ScreenLinesC[MAX_MCO]; - schar_T *new_ScreenLines2 = NULL; - int i; - sattr_T *new_ScreenAttrs; - unsigned *new_LineOffset; - char_u *new_LineWraps; - StlClickDefinition *new_tab_page_click_defs; static bool entered = false; // avoid recursiveness - static bool done_outofmem_msg = false; int retry_count = 0; - const bool l_enc_utf8 = enc_utf8; - const int l_enc_dbcs = enc_dbcs; retry: - /* - * Allocation of the screen buffers is done only when the size changes and - * when Rows and Columns have been set and we have started doing full - * screen stuff. - */ - if ((ScreenLines != NULL - && Rows == screen_Rows - && Columns == screen_Columns - && l_enc_utf8 == (ScreenLinesUC != NULL) - && (l_enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL) - && p_mco == Screen_mco + // Allocation of the screen buffers is done only when the size changes and + // when Rows and Columns have been set and we have started doing full + // screen stuff. + if ((default_grid.chars != NULL + && Rows == default_grid.Rows + && Columns == default_grid.Columns ) || Rows == 0 || Columns == 0 - || (!full_screen && ScreenLines == NULL)) + || (!full_screen && default_grid.chars == NULL)) { return; + } /* * It's possible that we produce an out-of-memory message below, which @@ -6036,39 +6009,24 @@ retry: comp_col(); /* recompute columns for shown command and ruler */ - /* - * We're changing the size of the screen. - * - Allocate new arrays for ScreenLines and ScreenAttrs. - * - Move lines from the old arrays into the new arrays, clear extra - * lines (unless the screen is going to be cleared). - * - Free the old arrays. - * - * If anything fails, make ScreenLines NULL, so we don't do anything! - * Continuing with the old ScreenLines may result in a crash, because the - * size is wrong. - */ + // We're changing the size of the screen. + // - Allocate new arrays for default_grid + // - Move lines from the old arrays into the new arrays, clear extra + // lines (unless the screen is going to be cleared). + // - Free the old arrays. + // + // If anything fails, make grid arrays NULL, so we don't do anything! + // Continuing with the old arrays may result in a crash, because the + // size is wrong. FOR_ALL_TAB_WINDOWS(tp, wp) { win_free_lsize(wp); } if (aucmd_win != NULL) win_free_lsize(aucmd_win); - new_ScreenLines = xmalloc((size_t)((Rows + 1) * Columns * sizeof(schar_T))); - memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO); - if (l_enc_utf8) { - new_ScreenLinesUC = xmalloc( - (size_t)((Rows + 1) * Columns * sizeof(u8char_T))); - for (i = 0; i < p_mco; ++i) - new_ScreenLinesC[i] = xcalloc((Rows + 1) * Columns, sizeof(u8char_T)); - } - if (l_enc_dbcs == DBCS_JPNU) - new_ScreenLines2 = xmalloc( - (size_t)((Rows + 1) * Columns * sizeof(schar_T))); - new_ScreenAttrs = xmalloc((size_t)((Rows + 1) * Columns * sizeof(sattr_T))); - new_LineOffset = xmalloc((size_t)(Rows * sizeof(unsigned))); - new_LineWraps = xmalloc((size_t)(Rows * sizeof(char_u))); - new_tab_page_click_defs = xcalloc( - (size_t) Columns, sizeof(*new_tab_page_click_defs)); + grid_alloc(&default_grid, Rows, Columns, !doclear); + StlClickDefinition *new_tab_page_click_defs = xcalloc( + (size_t)Columns, sizeof(*new_tab_page_click_defs)); FOR_ALL_TAB_WINDOWS(tp, wp) { win_alloc_lines(wp); @@ -6077,128 +6035,15 @@ retry: win_alloc_lines(aucmd_win); } - for (i = 0; i < p_mco; ++i) - if (new_ScreenLinesC[i] == NULL) - break; - if (new_ScreenLines == NULL - || (l_enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco)) - || (l_enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL) - || new_ScreenAttrs == NULL - || new_LineOffset == NULL - || new_LineWraps == NULL - || new_tab_page_click_defs == NULL - || outofmem) { - if (ScreenLines != NULL || !done_outofmem_msg) { - /* guess the size */ - do_outofmem_msg((Rows + 1) * Columns); - - /* Remember we did this to avoid getting outofmem messages over - * and over again. */ - done_outofmem_msg = TRUE; - } - xfree(new_ScreenLines); - new_ScreenLines = NULL; - xfree(new_ScreenLinesUC); - new_ScreenLinesUC = NULL; - for (i = 0; i < p_mco; ++i) { - xfree(new_ScreenLinesC[i]); - new_ScreenLinesC[i] = NULL; - } - xfree(new_ScreenLines2); - new_ScreenLines2 = NULL; - xfree(new_ScreenAttrs); - new_ScreenAttrs = NULL; - xfree(new_LineOffset); - new_LineOffset = NULL; - xfree(new_LineWraps); - new_LineWraps = NULL; - xfree(new_tab_page_click_defs); - new_tab_page_click_defs = NULL; - } else { - done_outofmem_msg = FALSE; - - for (new_row = 0; new_row < Rows; ++new_row) { - new_LineOffset[new_row] = new_row * Columns; - new_LineWraps[new_row] = FALSE; - - /* - * If the screen is not going to be cleared, copy as much as - * possible from the old screen to the new one and clear the rest - * (used when resizing the window at the "--more--" prompt or when - * executing an external command, for the GUI). - */ - if (!doclear) { - (void)memset(new_ScreenLines + new_row * Columns, - ' ', (size_t)Columns * sizeof(schar_T)); - if (l_enc_utf8) { - (void)memset(new_ScreenLinesUC + new_row * Columns, - 0, (size_t)Columns * sizeof(u8char_T)); - for (i = 0; i < p_mco; ++i) - (void)memset(new_ScreenLinesC[i] - + new_row * Columns, - 0, (size_t)Columns * sizeof(u8char_T)); - } - if (l_enc_dbcs == DBCS_JPNU) - (void)memset(new_ScreenLines2 + new_row * Columns, - 0, (size_t)Columns * sizeof(schar_T)); - (void)memset(new_ScreenAttrs + new_row * Columns, - 0, (size_t)Columns * sizeof(sattr_T)); - old_row = new_row + (screen_Rows - Rows); - if (old_row >= 0 && ScreenLines != NULL) { - if (screen_Columns < Columns) - len = screen_Columns; - else - len = Columns; - /* When switching to utf-8 don't copy characters, they - * may be invalid now. Also when p_mco changes. */ - if (!(l_enc_utf8 && ScreenLinesUC == NULL) - && p_mco == Screen_mco) - memmove(new_ScreenLines + new_LineOffset[new_row], - ScreenLines + LineOffset[old_row], - (size_t)len * sizeof(schar_T)); - if (l_enc_utf8 && ScreenLinesUC != NULL - && p_mco == Screen_mco) { - memmove(new_ScreenLinesUC + new_LineOffset[new_row], - ScreenLinesUC + LineOffset[old_row], - (size_t)len * sizeof(u8char_T)); - for (i = 0; i < p_mco; ++i) - memmove(new_ScreenLinesC[i] - + new_LineOffset[new_row], - ScreenLinesC[i] + LineOffset[old_row], - (size_t)len * sizeof(u8char_T)); - } - if (l_enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL) - memmove(new_ScreenLines2 + new_LineOffset[new_row], - ScreenLines2 + LineOffset[old_row], - (size_t)len * sizeof(schar_T)); - memmove(new_ScreenAttrs + new_LineOffset[new_row], - ScreenAttrs + LineOffset[old_row], - (size_t)len * sizeof(sattr_T)); - } - } - } - /* Use the last line of the screen for the current line. */ - current_ScreenLine = new_ScreenLines + Rows * Columns; - } - - free_screenlines(); + clear_tab_page_click_defs(tab_page_click_defs, tab_page_click_defs_size); + xfree(tab_page_click_defs); - ScreenLines = new_ScreenLines; - ScreenLinesUC = new_ScreenLinesUC; - for (i = 0; i < p_mco; ++i) - ScreenLinesC[i] = new_ScreenLinesC[i]; - Screen_mco = p_mco; - ScreenLines2 = new_ScreenLines2; - ScreenAttrs = new_ScreenAttrs; - LineOffset = new_LineOffset; - LineWraps = new_LineWraps; tab_page_click_defs = new_tab_page_click_defs; - tab_page_click_defs_size = Columns; + tab_page_click_defs_size = default_grid.Columns; - /* It's important that screen_Rows and screen_Columns reflect the actual - * size of ScreenLines[]. Set them before calling anything. */ - screen_Rows = Rows; - screen_Columns = Columns; + default_grid.row_offset = 0; + default_grid.col_offset = 0; + default_grid.handle = DEFAULT_GRID_HANDLE; must_redraw = CLEAR; /* need to clear the screen later */ if (doclear) @@ -6220,20 +6065,75 @@ retry: } } -void free_screenlines(void) +void grid_alloc(ScreenGrid *grid, int rows, int columns, bool copy) { - int i; + int new_row; + ScreenGrid new = *grid; + + size_t ncells = (size_t)((rows+1) * columns); + new.chars = xmalloc(ncells * sizeof(schar_T)); + new.attrs = xmalloc(ncells * sizeof(sattr_T)); + new.line_offset = xmalloc((size_t)(rows * sizeof(unsigned))); + new.line_wraps = xmalloc((size_t)(rows * sizeof(char_u))); + + new.Rows = rows; + new.Columns = columns; + + for (new_row = 0; new_row < new.Rows; new_row++) { + new.line_offset[new_row] = new_row * new.Columns; + new.line_wraps[new_row] = false; + + grid_clear_line(&new, new.line_offset[new_row], columns, true); + + if (copy) { + // If the screen is not going to be cleared, copy as much as + // possible from the old screen to the new one and clear the rest + // (used when resizing the window at the "--more--" prompt or when + // executing an external command, for the GUI). + if (new_row < grid->Rows && grid->chars != NULL) { + int len = MIN(grid->Columns, new.Columns); + memmove(new.chars + new.line_offset[new_row], + grid->chars + grid->line_offset[new_row], + (size_t)len * sizeof(schar_T)); + memmove(new.attrs + new.line_offset[new_row], + grid->attrs + grid->line_offset[new_row], + (size_t)len * sizeof(sattr_T)); + } + } + } + grid_free(grid); + *grid = new; - xfree(ScreenLinesUC); - for (i = 0; i < Screen_mco; ++i) - xfree(ScreenLinesC[i]); - xfree(ScreenLines2); - xfree(ScreenLines); - xfree(ScreenAttrs); - xfree(LineOffset); - xfree(LineWraps); - clear_tab_page_click_defs(tab_page_click_defs, tab_page_click_defs_size); - xfree(tab_page_click_defs); + // Share a single scratch buffer for all grids, by + // ensuring it is as wide as the widest grid. + if (linebuf_size < (size_t)columns) { + xfree(linebuf_char); + xfree(linebuf_attr); + linebuf_char = xmalloc(columns * sizeof(schar_T)); + linebuf_attr = xmalloc(columns * sizeof(sattr_T)); + linebuf_size = columns; + } +} + +void grid_free(ScreenGrid *grid) +{ + xfree(grid->chars); + xfree(grid->attrs); + xfree(grid->line_offset); + xfree(grid->line_wraps); + + grid->chars = NULL; + grid->attrs = NULL; + grid->line_offset = NULL; + grid->line_wraps = NULL; +} + +/// Doesn't allow reinit, so must only be called by free_all_mem! +void screen_free_all_mem(void) +{ + grid_free(&default_grid); + xfree(linebuf_char); + xfree(linebuf_attr); } /// Clear tab_page_click_defs table @@ -6264,73 +6164,63 @@ static void screenclear2(void) { int i; - if (starting == NO_SCREEN || ScreenLines == NULL) { + if (starting == NO_SCREEN || default_grid.chars == NULL) { return; } - screen_stop_highlight(); /* don't want highlighting here */ + // blank out the default grid + for (i = 0; i < default_grid.Rows; i++) { + grid_clear_line(&default_grid, default_grid.line_offset[i], + (int)default_grid.Columns, true); + default_grid.line_wraps[i] = false; + } + ui_call_grid_clear(1); // clear the display + clear_cmdline = false; + mode_displayed = false; - /* blank out ScreenLines */ - for (i = 0; i < Rows; ++i) { - lineclear(LineOffset[i], (int)Columns); - LineWraps[i] = FALSE; + redraw_all_later(NOT_VALID); + redraw_cmdline = true; + redraw_tabline = true; + if (must_redraw == CLEAR) { + must_redraw = NOT_VALID; // no need to clear again } - - ui_clear(); // clear the display - clear_cmdline = FALSE; - mode_displayed = FALSE; - screen_cleared = TRUE; /* can use contents of ScreenLines now */ - - win_rest_invalid(firstwin); - redraw_cmdline = TRUE; - redraw_tabline = TRUE; - if (must_redraw == CLEAR) /* no need to clear again */ - must_redraw = NOT_VALID; compute_cmdrow(); - msg_row = cmdline_row; /* put cursor on last line for messages */ + msg_row = cmdline_row; // put cursor on last line for messages msg_col = 0; - msg_scrolled = 0; /* can't scroll back */ - msg_didany = FALSE; - msg_didout = FALSE; + msg_scrolled = 0; // can't scroll back + msg_didany = false; + msg_didout = false; } -/* - * Clear one line in ScreenLines. - */ -static void lineclear(unsigned off, int width) +/// clear a line in the grid starting at "off" until "width" characters +/// are cleared. +static void grid_clear_line(ScreenGrid *grid, unsigned off, int width, + bool valid) { - (void)memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T)); - if (enc_utf8) - (void)memset(ScreenLinesUC + off, 0, - (size_t)width * sizeof(u8char_T)); - (void)memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T)); + for (int col = 0; col < width; col++) { + schar_from_ascii(grid->chars[off + col], ' '); + } + int fill = valid ? 0 : -1; + (void)memset(grid->attrs + off, fill, (size_t)width * sizeof(sattr_T)); } -/* - * Copy part of a Screenline for vertically split window "wp". - */ -static void linecopy(int to, int from, win_T *wp) +static void grid_invalidate(ScreenGrid *grid) { - unsigned off_to = LineOffset[to] + wp->w_wincol; - unsigned off_from = LineOffset[from] + wp->w_wincol; - - memmove(ScreenLines + off_to, ScreenLines + off_from, - wp->w_width * sizeof(schar_T)); - if (enc_utf8) { - int i; - - memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from, - wp->w_width * sizeof(u8char_T)); - for (i = 0; i < p_mco; ++i) - memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from, - wp->w_width * sizeof(u8char_T)); - } - if (enc_dbcs == DBCS_JPNU) - memmove(ScreenLines2 + off_to, ScreenLines2 + off_from, - wp->w_width * sizeof(schar_T)); - memmove(ScreenAttrs + off_to, ScreenAttrs + off_from, - wp->w_width * sizeof(sattr_T)); + (void)memset(grid->attrs, -1, grid->Rows * grid->Columns * sizeof(sattr_T)); +} + + +/// Copy part of a grid line for vertically split window. +static void linecopy(ScreenGrid *grid, int to, int from, int col, int width) +{ + unsigned off_to = grid->line_offset[to] + col; + unsigned off_from = grid->line_offset[from] + col; + + memmove(grid->chars + off_to, grid->chars + off_from, + width * sizeof(schar_T)); + memmove(grid->attrs + off_to, grid->attrs + off_from, + width * sizeof(sattr_T)); } /* @@ -6340,186 +6230,62 @@ void setcursor(void) { if (redrawing()) { validate_cursor(); - ui_cursor_goto(curwin->w_winrow + curwin->w_wrow, - curwin->w_wincol + ( - /* With 'rightleft' set and the cursor on a double-wide - * character, position it on the leftmost column. */ - curwin->w_p_rl ? (curwin->w_width - curwin->w_wcol - ( - (has_mbyte - && (*mb_ptr2cells)(get_cursor_pos_ptr()) == 2 - && vim_isprintc(gchar_cursor())) ? 2 : - 1)) : - curwin->w_wcol)); - } -} - -/* - * insert 'line_count' lines at 'row' in window 'wp' - * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated. - * if 'mayclear' is TRUE the screen will be cleared if it is faster than - * scrolling. - * Returns FAIL if the lines are not inserted, OK for success. - */ -int win_ins_lines(win_T *wp, int row, int line_count, int invalid, int mayclear) -{ - int did_delete; - int nextrow; - int lastrow; - int retval; - - if (invalid) - wp->w_lines_valid = 0; - - if (wp->w_height < 5) - return FAIL; - - if (line_count > wp->w_height - row) - line_count = wp->w_height - row; - - retval = win_do_lines(wp, row, line_count, mayclear, FALSE); - if (retval != MAYBE) - return retval; - /* - * If there is a next window or a status line, we first try to delete the - * lines at the bottom to avoid messing what is after the window. - * If this fails and there are following windows, don't do anything to avoid - * messing up those windows, better just redraw. - */ - did_delete = FALSE; - if (wp->w_next != NULL || wp->w_status_height) { - if (screen_del_lines(0, wp->w_winrow + wp->w_height - line_count, - line_count, (int)Rows, NULL) == OK) - did_delete = TRUE; - else if (wp->w_next) - return FAIL; - } - /* - * if no lines deleted, blank the lines that will end up below the window - */ - if (!did_delete) { - wp->w_redr_status = TRUE; - redraw_cmdline = TRUE; - nextrow = wp->w_winrow + wp->w_height + wp->w_status_height; - lastrow = nextrow + line_count; - if (lastrow > Rows) - lastrow = Rows; - screen_fill(nextrow - line_count, lastrow - line_count, - wp->w_wincol, W_ENDCOL(wp), - ' ', ' ', 0); - } - - if (screen_ins_lines(0, wp->w_winrow + row, line_count, (int)Rows, NULL) - == FAIL) { - /* deletion will have messed up other windows */ - if (did_delete) { - wp->w_redr_status = TRUE; - win_rest_invalid(wp->w_next); + ScreenGrid *grid = &curwin->w_grid; + int row = curwin->w_wrow; + int col = curwin->w_wcol; + if (curwin->w_p_rl) { + // With 'rightleft' set and the cursor on a double-wide character, + // position it on the leftmost column. + col = curwin->w_grid.Columns - curwin->w_wcol + - ((utf_ptr2cells(get_cursor_pos_ptr()) == 2 + && vim_isprintc(gchar_cursor())) ? 2 : 1); } - return FAIL; - } - return OK; + screen_adjust_grid(&grid, &row, &col); + ui_grid_cursor_goto(grid->handle, row, col); + } } -/* - * delete "line_count" window lines at "row" in window "wp" - * If "invalid" is TRUE curwin->w_lines[] is invalidated. - * If "mayclear" is TRUE the screen will be cleared if it is faster than - * scrolling - * Return OK for success, FAIL if the lines are not deleted. - */ -int win_del_lines(win_T *wp, int row, int line_count, int invalid, int mayclear) +/// Insert 'line_count' lines at 'row' in window 'wp'. +/// Returns FAIL if the lines are not inserted, OK for success. +int win_ins_lines(win_T *wp, int row, int line_count) { - int retval; - - if (invalid) - wp->w_lines_valid = 0; - - if (line_count > wp->w_height - row) - line_count = wp->w_height - row; - - retval = win_do_lines(wp, row, line_count, mayclear, TRUE); - if (retval != MAYBE) - return retval; - - if (screen_del_lines(0, wp->w_winrow + row, line_count, - (int)Rows, NULL) == FAIL) { - return FAIL; - } + return win_do_lines(wp, row, line_count, false); +} - /* - * If there are windows or status lines below, try to put them at the - * correct place. If we can't do that, they have to be redrawn. - */ - if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1) { - if (screen_ins_lines(0, wp->w_winrow + wp->w_height - line_count, - line_count, (int)Rows, NULL) == FAIL) { - wp->w_redr_status = TRUE; - win_rest_invalid(wp->w_next); - } - } - /* - * If this is the last window and there is no status line, redraw the - * command line later. - */ - else - redraw_cmdline = TRUE; - return OK; +/// Delete "line_count" window lines at "row" in window "wp". +/// Return OK for success, FAIL if the lines are not deleted. +int win_del_lines(win_T *wp, int row, int line_count) +{ + return win_do_lines(wp, row, line_count, true); } // Common code for win_ins_lines() and win_del_lines(). // Returns OK or FAIL when the work has been done. -static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del) +static int win_do_lines(win_T *wp, int row, int line_count, int del) { if (!redrawing() || line_count <= 0) { return FAIL; } - // only a few lines left: redraw is faster - if (mayclear && Rows - line_count < 5 && wp->w_width == Columns) { - screenclear(); /* will set wp->w_lines_valid to 0 */ - return FAIL; - } - - // Delete all remaining lines - if (row + line_count >= wp->w_height) { - screen_fill(wp->w_winrow + row, wp->w_winrow + wp->w_height, - wp->w_wincol, W_ENDCOL(wp), - ' ', ' ', 0); + // No lines are being moved, just draw over the entire area + if (row + line_count >= wp->w_grid.Rows) { return OK; } - // when scrolling, the message on the command line should be cleared, - // otherwise it will stay there forever. - clear_cmdline = TRUE; int retval; - ui_set_scroll_region(wp, row); if (del) { - retval = screen_del_lines(wp->w_winrow + row, 0, line_count, - wp->w_height - row, wp); + retval = grid_del_lines(&wp->w_grid, row, line_count, + wp->w_grid.Rows, 0, wp->w_grid.Columns); } else { - retval = screen_ins_lines(wp->w_winrow + row, 0, line_count, - wp->w_height - row, wp); + retval = grid_ins_lines(&wp->w_grid, row, line_count, + wp->w_grid.Rows, 0, wp->w_grid.Columns); } - ui_reset_scroll_region(); return retval; } /* - * window 'wp' and everything after it is messed up, mark it for redraw - */ -static void win_rest_invalid(win_T *wp) -{ - while (wp != NULL) { - redraw_win_later(wp, NOT_VALID); - wp->w_redr_status = TRUE; - wp = wp->w_next; - } - redraw_cmdline = TRUE; -} - -/* * The rest of the routines in this file perform screen manipulations. The * given operation is performed physically on the screen. The corresponding * change is also made to the internal screen image. In this way, the editor @@ -6530,115 +6296,114 @@ static void win_rest_invalid(win_T *wp) */ -// insert lines on the screen and update ScreenLines[] -// 'end' is the line after the scrolled part. Normally it is Rows. -// When scrolling region used 'off' is the offset from the top for the region. -// 'row' and 'end' are relative to the start of the region. +/// insert lines on the screen and move the existing lines down +/// 'line_count' is the number of lines to be inserted. +/// 'end' is the line after the scrolled part. Normally it is Rows. +/// 'col' is the column from with we start inserting. // -// return FAIL for failure, OK for success. -int screen_ins_lines ( - int off, - int row, - int line_count, - int end, - win_T *wp /* NULL or window to use width from */ -) +/// 'row', 'col' and 'end' are relative to the start of the region. +/// +/// @return FAIL for failure, OK for success. +int grid_ins_lines(ScreenGrid *grid, int row, int line_count, int end, int col, + int width) { int i; int j; unsigned temp; + int row_off = 0; + screen_adjust_grid(&grid, &row_off, &col); + row += row_off; + end += row_off; + if (!screen_valid(TRUE) || line_count <= 0) { return FAIL; } - // Shift LineOffset[] line_count down to reflect the inserted lines. - // Clear the inserted lines in ScreenLines[]. - row += off; - end += off; - for (i = 0; i < line_count; ++i) { - if (wp != NULL && wp->w_width != Columns) { + // Shift line_offset[] line_count down to reflect the inserted lines. + // Clear the inserted lines. + for (i = 0; i < line_count; i++) { + if (width != grid->Columns) { // need to copy part of a line j = end - 1 - i; while ((j -= line_count) >= row) { - linecopy(j + line_count, j, wp); + linecopy(grid, j + line_count, j, col, width); } j += line_count; - lineclear(LineOffset[j] + wp->w_wincol, wp->w_width); - LineWraps[j] = FALSE; + grid_clear_line(grid, grid->line_offset[j] + col, width, false); + grid->line_wraps[j] = false; } else { j = end - 1 - i; - temp = LineOffset[j]; + temp = grid->line_offset[j]; while ((j -= line_count) >= row) { - LineOffset[j + line_count] = LineOffset[j]; - LineWraps[j + line_count] = LineWraps[j]; + grid->line_offset[j + line_count] = grid->line_offset[j]; + grid->line_wraps[j + line_count] = grid->line_wraps[j]; } - LineOffset[j + line_count] = temp; - LineWraps[j + line_count] = FALSE; - lineclear(temp, (int)Columns); + grid->line_offset[j + line_count] = temp; + grid->line_wraps[j + line_count] = false; + grid_clear_line(grid, temp, (int)grid->Columns, false); } } - ui_append_lines(line_count); + ui_call_grid_scroll(grid->handle, row, end, col, col+width, -line_count, 0); return OK; } -// delete lines on the screen and update ScreenLines[] -// 'end' is the line after the scrolled part. Normally it is Rows. -// When scrolling region used 'off' is the offset from the top for the region. -// 'row' and 'end' are relative to the start of the region. -// -// Return OK for success, FAIL if the lines are not deleted. -int screen_del_lines ( - int off, - int row, - int line_count, - int end, - win_T *wp /* NULL or window to use width from */ -) +/// delete lines on the screen and move lines up. +/// 'end' is the line after the scrolled part. Normally it is Rows. +/// When scrolling region used 'off' is the offset from the top for the region. +/// 'row' and 'end' are relative to the start of the region. +/// +/// Return OK for success, FAIL if the lines are not deleted. +int grid_del_lines(ScreenGrid *grid, int row, int line_count, int end, int col, + int width) { int j; int i; unsigned temp; + int row_off = 0; + screen_adjust_grid(&grid, &row_off, &col); + row += row_off; + end += row_off; + if (!screen_valid(TRUE) || line_count <= 0) { return FAIL; } - // Now shift LineOffset[] line_count up to reflect the deleted lines. - // Clear the inserted lines in ScreenLines[]. - row += off; - end += off; - for (i = 0; i < line_count; ++i) { - if (wp != NULL && wp->w_width != Columns) { + // Now shift line_offset[] line_count up to reflect the deleted lines. + // Clear the inserted lines. + for (i = 0; i < line_count; i++) { + if (width != grid->Columns) { // need to copy part of a line j = row + i; while ((j += line_count) <= end - 1) { - linecopy(j - line_count, j, wp); + linecopy(grid, j - line_count, j, col, width); } j -= line_count; - lineclear(LineOffset[j] + wp->w_wincol, wp->w_width); - LineWraps[j] = FALSE; + grid_clear_line(grid, grid->line_offset[j] + col, width, false); + grid->line_wraps[j] = false; } else { // whole width, moving the line pointers is faster j = row + i; - temp = LineOffset[j]; + temp = grid->line_offset[j]; while ((j += line_count) <= end - 1) { - LineOffset[j - line_count] = LineOffset[j]; - LineWraps[j - line_count] = LineWraps[j]; + grid->line_offset[j - line_count] = grid->line_offset[j]; + grid->line_wraps[j - line_count] = grid->line_wraps[j]; } - LineOffset[j - line_count] = temp; - LineWraps[j - line_count] = FALSE; - lineclear(temp, (int)Columns); + grid->line_offset[j - line_count] = temp; + grid->line_wraps[j - line_count] = false; + grid_clear_line(grid, temp, (int)grid->Columns, false); } } - ui_delete_lines(line_count); + ui_call_grid_scroll(grid->handle, row, end, col, col+width, line_count, 0); return OK; } + /* * show the current mode and ruler * @@ -6680,36 +6445,47 @@ int showmode(void) /* if the cmdline is more than one line high, erase top lines */ need_clear = clear_cmdline; - if (clear_cmdline && cmdline_row < Rows - 1) - msg_clr_cmdline(); /* will reset clear_cmdline */ + if (clear_cmdline && cmdline_row < default_grid.Rows - 1) { + msg_clr_cmdline(); // will reset clear_cmdline + } /* Position on the last line in the window, column 0 */ msg_pos_mode(); - attr = hl_attr(HLF_CM); /* Highlight mode */ + attr = HL_ATTR(HLF_CM); // Highlight mode + + // When the screen is too narrow to show the entire mode messsage, + // avoid scrolling and truncate instead. + msg_no_more = true; + int save_lines_left = lines_left; + lines_left = 0; + if (do_mode) { MSG_PUTS_ATTR("--", attr); // CTRL-X in Insert mode if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU)) { /* These messages can get long, avoid a wrap in a narrow * window. Prefer showing edit_submode_extra. */ - length = (Rows - msg_row) * Columns - 3; - if (edit_submode_extra != NULL) + length = (default_grid.Rows - msg_row) * default_grid.Columns - 3; + if (edit_submode_extra != NULL) { length -= vim_strsize(edit_submode_extra); + } if (length > 0) { if (edit_submode_pre != NULL) length -= vim_strsize(edit_submode_pre); if (length - vim_strsize(edit_submode) > 0) { - if (edit_submode_pre != NULL) - msg_puts_attr(edit_submode_pre, attr); - msg_puts_attr(edit_submode, attr); + if (edit_submode_pre != NULL) { + msg_puts_attr((const char *)edit_submode_pre, attr); + } + msg_puts_attr((const char *)edit_submode, attr); } if (edit_submode_extra != NULL) { - MSG_PUTS_ATTR(" ", attr); /* add a space in between */ - if ((int)edit_submode_highl < (int)HLF_COUNT) - sub_attr = hl_attr(edit_submode_highl); - else + MSG_PUTS_ATTR(" ", attr); // Add a space in between. + if ((int)edit_submode_highl < (int)HLF_COUNT) { + sub_attr = win_hl_attr(curwin, edit_submode_highl); + } else { sub_attr = attr; - msg_puts_attr(edit_submode_extra, sub_attr); + } + msg_puts_attr((const char *)edit_submode_extra, sub_attr); } } } else { @@ -6723,21 +6499,27 @@ int showmode(void) if (p_ri) MSG_PUTS_ATTR(_(" REVERSE"), attr); MSG_PUTS_ATTR(_(" INSERT"), attr); - } else if (restart_edit == 'I') + } else if (restart_edit == 'I' || restart_edit == 'i' + || restart_edit == 'a') { MSG_PUTS_ATTR(_(" (insert)"), attr); - else if (restart_edit == 'R') + } else if (restart_edit == 'R') { MSG_PUTS_ATTR(_(" (replace)"), attr); - else if (restart_edit == 'V') + } else if (restart_edit == 'V') { MSG_PUTS_ATTR(_(" (vreplace)"), attr); - if (p_hkmap) + } + if (p_hkmap) { MSG_PUTS_ATTR(_(" Hebrew"), attr); - if (p_fkmap) + } + if (p_fkmap) { MSG_PUTS_ATTR(farsi_text_5, attr); + } if (State & LANGMAP) { - if (curwin->w_p_arab) + if (curwin->w_p_arab) { MSG_PUTS_ATTR(_(" Arabic"), attr); - else - MSG_PUTS_ATTR(_(" (lang)"), attr); + } else if (get_keymap_str(curwin, (char_u *)" (%s)", + NameBuff, MAXPATHL)) { + MSG_PUTS_ATTR(NameBuff, attr); + } } if ((State & INSERT) && p_paste) MSG_PUTS_ATTR(_(" (paste)"), attr); @@ -6777,10 +6559,13 @@ int showmode(void) msg_didout = FALSE; /* overwrite this message */ length = msg_col; msg_col = 0; - need_wait_return = nwr_save; /* never ask for hit-return for this */ - } else if (clear_cmdline && msg_silent == 0) - /* Clear the whole command line. Will reset "clear_cmdline". */ + msg_no_more = false; + lines_left = save_lines_left; + need_wait_return = nwr_save; // never ask for hit-return for this + } else if (clear_cmdline && msg_silent == 0) { + // Clear the whole command line. Will reset "clear_cmdline". msg_clr_cmdline(); + } /* In Visual mode the size of the selected area must be redrawn. */ if (VIsual_active) @@ -6804,7 +6589,7 @@ int showmode(void) static void msg_pos_mode(void) { msg_col = 0; - msg_row = Rows - 1; + msg_row = default_grid.Rows - 1; } /// Delete mode message. Used when ESC is typed which is expected to end @@ -6816,12 +6601,18 @@ void unshowmode(bool force) if (!redrawing() || (!force && char_avail() && !KeyTyped)) { redraw_cmdline = true; // delete mode later } else { + clearmode(); + } +} + +// Clear the mode message. +void clearmode(void) +{ msg_pos_mode(); if (Recording) { - recording_mode(hl_attr(HLF_CM)); + recording_mode(HL_ATTR(HLF_CM)); } msg_clr_eos(); - } } static void recording_mode(int attr) @@ -6850,44 +6641,51 @@ static void draw_tabline(void) int modified; int c; int len; - int attr_sel = hl_attr(HLF_TPS); - int attr_nosel = hl_attr(HLF_TP); - int attr_fill = hl_attr(HLF_TPF); + int attr_nosel = HL_ATTR(HLF_TP); + int attr_fill = HL_ATTR(HLF_TPF); char_u *p; int room; int use_sep_chars = (t_colors < 8 ); - redraw_tabline = FALSE; + if (default_grid.chars == NULL) { + return; + } + redraw_tabline = false; + if (ui_is_external(kUITabline)) { + ui_ext_tabline_update(); + return; + } if (tabline_height() < 1) return; // Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. - assert(Columns == tab_page_click_defs_size); + assert(default_grid.Columns == tab_page_click_defs_size); clear_tab_page_click_defs(tab_page_click_defs, tab_page_click_defs_size); /* Use the 'tabline' option if it's set. */ if (*p_tal != NUL) { - int save_called_emsg = called_emsg; + int saved_did_emsg = did_emsg; - /* Check for an error. If there is one we would loop in redrawing the - * screen. Avoid that by making 'tabline' empty. */ - called_emsg = FALSE; - win_redr_custom(NULL, FALSE); - if (called_emsg) + // Check for an error. If there is one we would loop in redrawing the + // screen. Avoid that by making 'tabline' empty. + did_emsg = false; + win_redr_custom(NULL, false); + if (did_emsg) { set_string_option_direct((char_u *)"tabline", -1, - (char_u *)"", OPT_FREE, SID_ERROR); - called_emsg |= save_called_emsg; + (char_u *)"", OPT_FREE, SID_ERROR); + } + did_emsg |= saved_did_emsg; } else { FOR_ALL_TABS(tp) { ++tabcount; } if (tabcount > 0) { - tabwidth = (Columns - 1 + tabcount / 2) / tabcount; + tabwidth = (default_grid.Columns - 1 + tabcount / 2) / tabcount; } if (tabwidth < 6) { @@ -6898,22 +6696,12 @@ static void draw_tabline(void) tabcount = 0; FOR_ALL_TABS(tp) { - if (col >= Columns - 4) { + if (col >= default_grid.Columns - 4) { break; } scol = col; - if (tp->tp_topframe == topframe) - attr = attr_sel; - if (use_sep_chars && col > 0) - screen_putchar('|', 0, col++, attr); - - if (tp->tp_topframe != topframe) - attr = attr_nosel; - - screen_putchar(' ', 0, col++, attr); - if (tp == curtab) { cwp = curwin; wp = firstwin; @@ -6922,23 +6710,44 @@ static void draw_tabline(void) wp = tp->tp_firstwin; } - modified = FALSE; - for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount) - if (bufIsChanged(wp->w_buffer)) - modified = TRUE; + + if (tp->tp_topframe == topframe) { + attr = win_hl_attr(cwp, HLF_TPS); + } + if (use_sep_chars && col > 0) { + grid_putchar(&default_grid, '|', 0, col++, attr); + } + + if (tp->tp_topframe != topframe) { + attr = win_hl_attr(cwp, HLF_TP); + } + + grid_putchar(&default_grid, ' ', 0, col++, attr); + + modified = false; + + for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount) { + if (bufIsChanged(wp->w_buffer)) { + modified = true; + } + } + + if (modified || wincount > 1) { if (wincount > 1) { vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount); len = (int)STRLEN(NameBuff); - if (col + len >= Columns - 3) + if (col + len >= default_grid.Columns - 3) { break; - screen_puts_len(NameBuff, len, 0, col, - hl_combine_attr(attr, hl_attr(HLF_T))); + } + grid_puts_len(&default_grid, NameBuff, len, 0, col, + hl_combine_attr(attr, win_hl_attr(cwp, HLF_T))); col += len; } - if (modified) - screen_puts_len((char_u *)"+", 1, 0, col++, attr); - screen_putchar(' ', 0, col++, attr); + if (modified) { + grid_puts_len(&default_grid, (char_u *)"+", 1, 0, col++, attr); + } + grid_putchar(&default_grid, ' ', 0, col++, attr); } room = scol - col + tabwidth - 1; @@ -6951,19 +6760,20 @@ static void draw_tabline(void) if (has_mbyte) while (len > room) { len -= ptr2cells(p); - mb_ptr_adv(p); + MB_PTR_ADV(p); } else if (len > room) { p += len - room; len = room; } - if (len > Columns - col - 1) - len = Columns - col - 1; + if (len > default_grid.Columns - col - 1) { + len = default_grid.Columns - col - 1; + } - screen_puts_len(p, (int)STRLEN(p), 0, col, attr); + grid_puts_len(&default_grid, p, (int)STRLEN(p), 0, col, attr); col += len; } - screen_putchar(' ', 0, col++, attr); + grid_putchar(&default_grid, ' ', 0, col++, attr); // Store the tab page number in tab_page_click_defs[], so that // jump_to_mouse() knows where each one is. @@ -6981,12 +6791,14 @@ static void draw_tabline(void) c = '_'; else c = ' '; - screen_fill(0, 1, col, (int)Columns, c, c, attr_fill); + grid_fill(&default_grid, 0, 1, col, (int)default_grid.Columns, c, c, + attr_fill); /* Put an "X" for closing the current tab if there are several. */ if (first_tabpage->tp_next != NULL) { - screen_putchar('X', 0, (int)Columns - 1, attr_nosel); - tab_page_click_defs[Columns - 1] = (StlClickDefinition) { + grid_putchar(&default_grid, 'X', 0, (int)default_grid.Columns - 1, + attr_nosel); + tab_page_click_defs[default_grid.Columns - 1] = (StlClickDefinition) { .type = kStlClickTabClose, .tabnr = 999, .func = NULL, @@ -6999,6 +6811,22 @@ static void draw_tabline(void) redraw_tabline = FALSE; } +void ui_ext_tabline_update(void) +{ + Array tabs = ARRAY_DICT_INIT; + FOR_ALL_TABS(tp) { + Dictionary tab_info = ARRAY_DICT_INIT; + PUT(tab_info, "tab", TABPAGE_OBJ(tp->handle)); + + win_T *cwp = (tp == curtab) ? curwin : tp->tp_curwin; + get_trans_bufname(cwp->w_buffer); + PUT(tab_info, "name", STRING_OBJ(cstr_to_string((char *)NameBuff))); + + ADD(tabs, DICTIONARY_OBJ(tab_info)); + } + ui_call_tabline_update(curtab->handle, tabs); +} + /* * Get buffer name for "buf" into NameBuff[]. * Takes care of special buffer names and translates special characters. @@ -7015,25 +6843,28 @@ void get_trans_bufname(buf_T *buf) /* * Get the character to use in a status line. Get its attributes in "*attr". */ -static int fillchar_status(int *attr, int is_curwin) +static int fillchar_status(int *attr, win_T *wp) { int fill; + bool is_curwin = (wp == curwin); if (is_curwin) { - *attr = hl_attr(HLF_S); + *attr = win_hl_attr(wp, HLF_S); fill = fill_stl; } else { - *attr = hl_attr(HLF_SNC); + *attr = win_hl_attr(wp, HLF_SNC); fill = fill_stlnc; } /* Use fill when there is highlighting, and highlighting of current * window differs, or the fillchars differ, or this is not the * current window */ - if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC) - || !is_curwin || firstwin == lastwin) - || (fill_stl != fill_stlnc))) + if (*attr != 0 && ((win_hl_attr(wp, HLF_S) != win_hl_attr(wp, HLF_SNC) + || !is_curwin || ONE_WINDOW) + || (fill_stl != fill_stlnc))) { return fill; - if (is_curwin) + } + if (is_curwin) { return '^'; + } return '='; } @@ -7041,13 +6872,10 @@ static int fillchar_status(int *attr, int is_curwin) * Get the character to use in a separator between vertically split windows. * Get its attributes in "*attr". */ -static int fillchar_vsep(int *attr) +static int fillchar_vsep(win_T *wp, int *attr) { - *attr = hl_attr(HLF_C); - if (*attr == 0 && fill_vert == ' ') - return '|'; - else - return fill_vert; + *attr = win_hl_attr(wp, HLF_C); + return fill_vert; } /* @@ -7075,15 +6903,16 @@ void showruler(int always) { if (!always && !redrawing()) return; - if (pum_visible()) { - /* Don't redraw right now, do it later. */ - curwin->w_redr_status = TRUE; + if (pum_drawn()) { + // Don't redraw right now, do it later. + curwin->w_redr_status = true; return; } if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height) { redraw_custom_statusline(curwin); - } else + } else { win_redr_ruler(curwin, always); + } if (need_maketitle || (p_icon && (stl_syntax & STL_IN_ICON)) @@ -7113,9 +6942,10 @@ static void win_redr_ruler(win_T *wp, int always) if (wp == lastwin && lastwin->w_status_height == 0) if (edit_submode != NULL) return; - /* Don't draw the ruler when the popup menu is visible, it may overlap. */ - if (pum_visible()) + // Don't draw the ruler when the popup menu is visible, it may overlap. + if (pum_drawn()) { return; + } if (*p_ruf) { int save_called_emsg = called_emsg; @@ -7159,15 +6989,15 @@ static void win_redr_ruler(win_T *wp, int always) int off; if (wp->w_status_height) { - row = wp->w_winrow + wp->w_height; - fillchar = fillchar_status(&attr, wp == curwin); + row = W_ENDROW(wp); + fillchar = fillchar_status(&attr, wp); off = wp->w_wincol; width = wp->w_width; } else { - row = Rows - 1; + row = default_grid.Rows - 1; fillchar = ' '; attr = 0; - width = Columns; + width = default_grid.Columns; off = 0; } @@ -7202,46 +7032,42 @@ static void win_redr_ruler(win_T *wp, int always) int i = (int)STRLEN(buffer); get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1); int o = i + vim_strsize(buffer + i + 1); - if (wp->w_status_height == 0) /* can't use last char of screen */ - ++o; - int this_ru_col = ru_col - (Columns - width); - if (this_ru_col < 0) + if (wp->w_status_height == 0) { // can't use last char of screen + o++; + } + int this_ru_col = ru_col - (default_grid.Columns - width); + if (this_ru_col < 0) { this_ru_col = 0; - /* Never use more than half the window/screen width, leave the other - * half for the filename. */ - if (this_ru_col < (width + 1) / 2) + } + // Never use more than half the window/screen width, leave the other half + // for the filename. + if (this_ru_col < (width + 1) / 2) { this_ru_col = (width + 1) / 2; + } if (this_ru_col + o < width) { // Need at least 3 chars left for get_rel_pos() + NUL. while (this_ru_col + o < width && RULER_BUF_LEN > i + 4) { - if (has_mbyte) - i += (*mb_char2bytes)(fillchar, buffer + i); - else - buffer[i++] = fillchar; - ++o; + i += utf_char2bytes(fillchar, buffer + i); + o++; } get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i); } - /* Truncate at window boundary. */ - if (has_mbyte) { - o = 0; - for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i)) { - o += (*mb_ptr2cells)(buffer + i); - if (this_ru_col + o > width) { - buffer[i] = NUL; - break; - } + // Truncate at window boundary. + o = 0; + for (i = 0; buffer[i] != NUL; i += utfc_ptr2len(buffer + i)) { + o += utf_ptr2cells(buffer + i); + if (this_ru_col + o > width) { + buffer[i] = NUL; + break; } - } else if (this_ru_col + (int)STRLEN(buffer) > width) - buffer[width - this_ru_col] = NUL; + } - screen_puts(buffer, row, this_ru_col + off, attr); + grid_puts(&default_grid, buffer, row, this_ru_col + off, attr); i = redraw_cmdline; - screen_fill(row, row + 1, - this_ru_col + off + (int)STRLEN(buffer), - off + width, - fillchar, fillchar, attr); - /* don't redraw the cmdline because of showing the ruler */ + grid_fill(&default_grid, row, row + 1, + this_ru_col + off + (int)STRLEN(buffer), off + width, fillchar, + fillchar, attr); + // don't redraw the cmdline because of showing the ruler redraw_cmdline = i; wp->w_ru_cursor = wp->w_cursor; wp->w_ru_virtcol = wp->w_virtcol; @@ -7262,12 +7088,13 @@ int number_width(win_T *wp) int n; linenr_T lnum; - if (wp->w_p_rnu && !wp->w_p_nu) - /* cursor line shows "0" */ - lnum = wp->w_height; - else - /* cursor line shows absolute line number */ + if (wp->w_p_rnu && !wp->w_p_nu) { + // cursor line shows "0" + lnum = wp->w_grid.Rows; + } else { + // cursor line shows absolute line number lnum = wp->w_buffer->b_ml.ml_line_count; + } if (lnum == wp->w_nrwidth_line_count) return wp->w_nrwidth_width; @@ -7287,23 +7114,16 @@ int number_width(win_T *wp) return n; } -/* - * Set size of the Vim shell. - * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real - * window size (this is used for the :win command). - * If 'mustset' is FALSE, we may try to get the real window size and if - * it fails use 'width' and 'height'. - */ +/// Set dimensions of the Nvim application "shell". void screen_resize(int width, int height) { static int busy = FALSE; - /* - * Avoid recursiveness, can happen when setting the window size causes - * another window-changed signal. - */ - if (busy) + // Avoid recursiveness, can happen when setting the window size causes + // another window-changed signal. + if (updating_screen || busy) { return; + } if (width < 0 || height < 0) /* just checking... */ return; @@ -7330,6 +7150,8 @@ void screen_resize(int width, int height) width = Columns; ui_resize(width, height); + send_grid_resize = true; + /* The window layout used to be adjusted here, but it now happens in * screenalloc() (also invoked from screenclear()). That is because the * "busy" check above may skip this, but not screenalloc(). */ @@ -7351,7 +7173,7 @@ void screen_resize(int width, int height) * - in Ex mode, don't redraw anything. * - Otherwise, redraw right now, and position the cursor. * Always need to call update_screen() or screenalloc(), to make - * sure Rows/Columns and the size of ScreenLines[] is correct! + * sure Rows/Columns and the size of the screen is correct! */ if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM || exmode_active) { @@ -7365,13 +7187,14 @@ void screen_resize(int width, int height) redrawcmdline(); } else { update_topline(); - if (pum_visible()) { + if (pum_drawn()) { redraw_later(NOT_VALID); - ins_compl_show_pum(); /* This includes the redraw. */ - } else - update_screen(NOT_VALID); - if (redrawing()) + ins_compl_show_pum(); + } + update_screen(NOT_VALID); + if (redrawing()) { setcursor(); + } } } } @@ -7379,8 +7202,8 @@ void screen_resize(int width, int height) --busy; } -// Check if the new shell size is valid, correct it if it's too small or way -// too big. +/// Check if the new Nvim application "shell" dimensions are valid. +/// Correct it if it's too small or way too big. void check_shellsize(void) { if (Rows < min_rows()) { @@ -7422,3 +7245,13 @@ void win_new_shellsize(void) shell_new_columns(); // update window sizes } } + +win_T *get_win_by_grid_handle(handle_T handle) +{ + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + if (wp->w_grid.handle == handle) { + return wp; + } + } + return NULL; +} |