vim-patch:8.1.2029: cannot control 'cursorline' highlighting well

Problem:    Cannot control 'cursorline' highlighting well.
Solution:   Add "screenline". (Christian Brabandt, closes vim/vim#4933)
017ba07fa2
This commit is contained in:
zeertzjq 2021-07-30 10:49:42 +08:00
parent 337b1b31ac
commit 1666fe9dfe
10 changed files with 305 additions and 47 deletions

View File

@ -1809,14 +1809,21 @@ A jump table for the options with a short description can be found at |Q_op|.
*'cursorlineopt'* *'culopt'*
'cursorlineopt' 'culopt' string (default: "both")
'cursorlineopt' 'culopt' string (default: "number,line")
local to window
Settings for how 'cursorline' is displayed. Valid values:
"line" Highlight the text line of the cursor with
Comma separated list of settings for how 'cursorline' is displayed.
Valid values:
"line" Highlight the text line of the cursor with
CursorLine |hl-CursorLine|.
"number" Highlight the line number of the cursor with
"screenline" Highlight only the screen line of the cursor with
CursorLine |hl-CursorLine|.
"number" Highlight the line number of the cursor with
CursorLineNr |hl-CursorLineNr|.
"both" Highlight as both "line" and "number" are set.
Special value:
"both" Alias for the values "line,number".
"line" and "screenline" cannot be used together.
*'debug'*

View File

@ -1388,6 +1388,7 @@ struct window_S {
uint32_t w_p_fde_flags; // flags for 'foldexpr'
uint32_t w_p_fdt_flags; // flags for 'foldtext'
int *w_p_cc_cols; // array of columns to highlight or NULL
char_u w_p_culopt_flags; // flags for cursorline highlighting
long w_p_siso; // 'sidescrolloff' local value
long w_p_so; // 'scrolloff' local value

View File

@ -292,9 +292,10 @@ static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume,
// Relative numbering may require updating more. Cursor line
// highlighting probably needs to be updated if it's below the
// change.
// change (or is using screenline highlighting).
if (wp->w_p_rnu
|| (wp->w_p_cul && lnum <= wp->w_last_cursorline)) {
|| ((wp->w_p_cul && lnum <= wp->w_last_cursorline)
|| (wp->w_p_culopt_flags & CULOPT_SCRLINE))) {
redraw_later(wp, SOME_VALID);
}
}

View File

@ -1275,10 +1275,25 @@ static void normal_redraw(NormalState *s)
redrawWinline(curwin, curwin->w_cursor.lnum);
}
if (curwin->w_p_cul && curwin->w_p_wrap
&& (curwin->w_p_culopt_flags & CULOPT_SCRLINE)) {
must_redraw = NOT_VALID;
}
if (VIsual_active) {
update_curbuf(INVERTED); // update inverted part
} else if (must_redraw) {
update_screen(0);
// Might need some more update for the cursorscreen line.
// TODO(vim): can we optimized this?
if (curwin->w_p_cul
&& curwin->w_p_wrap
&& (curwin->w_p_culopt_flags & CULOPT_SCRLINE)
&& !char_avail()) {
update_screen(VALID);
}
else {
update_screen(0);
}
} else if (redraw_cmdline || clear_cmdline) {
showmode();
}

View File

@ -322,7 +322,6 @@ static char *(p_scl_values[]) = { "yes", "no", "auto", "auto:1", "auto:2",
static char *(p_fdc_values[]) = { "auto", "auto:1", "auto:2",
"auto:3", "auto:4", "auto:5", "auto:6", "auto:7", "auto:8", "auto:9",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", NULL };
static char *(p_culopt_values[]) = { "line", "number", "both", NULL };
/// All possible flags for 'shm'.
static char_u SHM_ALL[] = {
@ -1963,6 +1962,7 @@ static void didset_options(void)
briopt_check(curwin);
// initialize the table for 'breakat'.
fill_breakat_flags();
fill_culopt_flags(NULL, curwin);
}
// More side effects of setting options.
@ -2414,8 +2414,7 @@ did_set_string_option(
}
} else if (varp == &curwin->w_p_culopt
|| gvarp == &curwin->w_allbuf_opt.wo_culopt) { // 'cursorlineopt'
if (**varp == NUL
|| check_opt_strings(*varp, p_culopt_values, false) != OK) {
if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK) {
errmsg = e_invarg;
}
} else if (varp == &curwin->w_p_cc) { // 'colorcolumn'
@ -5894,6 +5893,7 @@ void didset_window_options(win_T *wp)
{
check_colorcolumn(wp);
briopt_check(wp);
fill_culopt_flags(NULL, wp);
set_chars_option(wp, &wp->w_p_fcs, true);
set_chars_option(wp, &wp->w_p_lcs, true);
parse_winhl_opt(wp); // sets w_hl_needs_update also for w_p_winbl
@ -6901,6 +6901,49 @@ static void fill_breakat_flags(void)
}
}
/// fill_culopt_flags() -- called when 'culopt' changes value
static int fill_culopt_flags(char_u *val, win_T *wp)
{
char_u *p;
char_u culopt_flags_new = 0;
if (val == NULL) {
p = wp->w_p_culopt;
} else {
p = val;
}
while (*p != NUL) {
if (STRNCMP(p, "line", 4) == 0) {
p += 4;
culopt_flags_new |= CULOPT_LINE;
} else if (STRNCMP(p, "both", 4) == 0) {
p += 4;
culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
} else if (STRNCMP(p, "number", 6) == 0) {
p += 6;
culopt_flags_new |= CULOPT_NBR;
} else if (STRNCMP(p, "screenline", 10) == 0) {
p += 10;
culopt_flags_new |= CULOPT_SCRLINE;
}
if (*p != ',' && *p != NUL) {
return FAIL;
}
if (*p == ',') {
p++;
}
}
// Can't have both "line" and "screenline".
if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE)) {
return FAIL;
}
wp->w_p_culopt_flags = culopt_flags_new;
return OK;
}
/// Check an option that can be a range of string values.
///
/// Return OK for correct value, FAIL otherwise.

View File

@ -276,10 +276,10 @@ enum {
})
// flags used for parsed 'wildmode'
#define WIM_FULL 1
#define WIM_LONGEST 2
#define WIM_LIST 4
#define WIM_BUFLASTUSED 8
#define WIM_FULL 0x01
#define WIM_LONGEST 0x02
#define WIM_LIST 0x04
#define WIM_BUFLASTUSED 0x08
// arguments for can_bs()
// each defined char should be unique over all values
@ -291,6 +291,11 @@ enum {
#define BS_START 's' // "Start"
#define BS_NOSTOP 'p' // "nostoP
// flags for the 'culopt' option
#define CULOPT_LINE 0x01 // Highlight complete line
#define CULOPT_SCRLINE 0x02 // Highlight screen line
#define CULOPT_NBR 0x04 // Highlight Number column
#define LISPWORD_VALUE \
"defun,define,defmacro,set!,lambda,if,case,let,flet,let*,letrec,do,do*,define-syntax,let-syntax,letrec-syntax,destructuring-bind,defpackage,defparameter,defstruct,deftype,defvar,do-all-symbols,do-external-symbols,do-symbols,dolist,dotimes,ecase,etypecase,eval-when,labels,macrolet,multiple-value-bind,multiple-value-call,multiple-value-prog1,multiple-value-setq,prog1,progv,typecase,unless,unwind-protect,when,with-input-from-string,with-open-file,with-open-stream,with-output-to-string,with-package-iterator,define-condition,handler-bind,handler-case,restart-bind,restart-case,with-simple-restart,store-value,use-value,muffle-warning,abort,continue,with-slots,with-slots*,with-accessors,with-accessors*,defclass,defmethod,print-unreadable-object"

View File

@ -558,7 +558,8 @@ return {
{
full_name='cursorlineopt', abbreviation='culopt',
short_desc=N_("settings for 'cursorline'"),
type='string', scope={'window'},
type='string', list='onecomma', scope={'window'},
deny_duplicates=true,
redraw={'current_window_only'},
defaults={if_true={vi="both"}}
},

View File

@ -2090,7 +2090,9 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
sign_attrs_T sattrs[SIGN_SHOW_MAX]; // attributes for signs
int num_signs; // number of signs for line
int line_attr = 0; // attribute for the whole line
int line_attr_save;
int line_attr_lowprio = 0; // low-priority attribute for the line
int line_attr_lowprio_save;
matchitem_T *cur; // points to the match list
match_T *shl; // points to search_hl or a match
bool shl_flag; // flag to indicate whether search_hl
@ -2109,6 +2111,14 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
bool area_active = false;
int cul_attr = 0; // set when 'cursorline' active
// 'cursorlineopt' has "screenline" and cursor is in this line
bool cul_screenline = false;
// margin columns for the screen line, needed for when 'cursorlineopt'
// contains "screenline"
int left_curline_col = 0;
int right_curline_col = 0;
/* draw_state: items that are drawn in sequence: */
#define WL_START 0 /* nothing done yet */
# define WL_CMDLINE WL_START + 1 /* cmdline window column */
@ -2360,26 +2370,34 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
// Cursor line highlighting for 'cursorline' in the current window.
if (lnum == wp->w_cursor.lnum) {
// Do not show the cursor line when Visual mode is active, because it's
// not clear what is selected then.
// Do not show the cursor line in the text when Visual mode is active,
// because it's not clear what is selected then.
if (wp->w_p_cul && !(wp == curwin && VIsual_active)
&& *wp->w_p_culopt != 'n') {
int cul_attr = win_hl_attr(wp, HLF_CUL);
&& wp->w_p_culopt_flags != CULOPT_NBR) {
cul_screenline = (wp->w_p_wrap
&& (wp->w_p_culopt_flags & CULOPT_SCRLINE));
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);
if (!cul_screenline) {
if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) {
line_attr_lowprio = cul_attr;
} else {
line_attr = cul_attr;
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;
}
}
} else {
cul_attr = 0;
margin_columns_win(wp, &left_curline_col, &right_curline_col);
}
area_highlighting = true;
}
// Update w_last_cursorline even if Visual mode is active.
wp->w_last_cursorline = wp->w_cursor.lnum;
@ -2404,6 +2422,11 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
area_highlighting = true;
}
if (cul_screenline) {
line_attr_save = line_attr;
line_attr_lowprio_save = line_attr_lowprio;
}
line = ml_get_buf(wp->w_buffer, lnum, FALSE);
ptr = line;
@ -2787,11 +2810,15 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
// :sign defined with "numhl" highlight.
char_attr = num_sattr->sat_numhl;
} else if ((wp->w_p_cul || wp->w_p_rnu)
&& *wp->w_p_culopt != 'l'
&& (wp->w_p_culopt_flags & CULOPT_NBR)
&& (row == startrow
|| wp->w_p_culopt_flags & CULOPT_LINE)
&& lnum == wp->w_cursor.lnum
&& filler_todo == 0) {
// When 'cursorline' is set highlight the line number of
// the current line differently.
// When 'cursorlineopt' has "screenline" only highlight
// the line number itself.
// TODO(vim): Can we use CursorLine instead of CursorLineNr
// when CursorLineNr isn't set?
char_attr = win_hl_attr(wp, HLF_CLN);
@ -2823,9 +2850,8 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
if (diff_hlf != (hlf_T)0) {
char_attr = win_hl_attr(wp, diff_hlf);
if (wp->w_p_cul && lnum == wp->w_cursor.lnum
&& *wp->w_p_culopt != 'n') {
char_attr = hl_combine_attr(char_attr, win_hl_attr(wp, HLF_CUL));
if (cul_attr) {
char_attr = hl_combine_attr(char_attr, cul_attr);
}
}
p_extra = NULL;
@ -2884,9 +2910,8 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
if (tocol == vcol)
tocol += n_extra;
// Combine 'showbreak' with 'cursorline', prioritizing 'showbreak'.
if (wp->w_p_cul && lnum == wp->w_cursor.lnum
&& *wp->w_p_culopt != 'n') {
char_attr = hl_combine_attr(win_hl_attr(wp, HLF_CUL), char_attr);
if (cul_attr) {
char_attr = hl_combine_attr(cul_attr, char_attr);
}
}
}
@ -2913,6 +2938,24 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
}
}
if (cul_screenline) {
if (draw_state == WL_LINE
&& vcol >= left_curline_col
&& vcol < right_curline_col) {
cul_attr = win_hl_attr(wp, HLF_CUL);
HlAttrs ae = syn_attr2entry(cul_attr);
if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) {
line_attr_lowprio = cul_attr;
} else {
line_attr = cul_attr;
}
} else {
cul_attr = 0;
line_attr = line_attr_save;
line_attr_lowprio = line_attr_lowprio_save;
}
}
// When still displaying '$' of change command, stop at cursor
if (((dollar_vcol >= 0
&& wp == curwin
@ -3120,13 +3163,11 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
}
line_attr = win_hl_attr(wp, diff_hlf);
// Overlay CursorLine onto diff-mode highlight.
if (wp->w_p_cul && lnum == wp->w_cursor.lnum
&& *wp->w_p_culopt != 'n') {
if (cul_attr) {
line_attr = 0 != line_attr_lowprio // Low-priority CursorLine
? hl_combine_attr(hl_combine_attr(win_hl_attr(wp, HLF_CUL),
line_attr),
? hl_combine_attr(hl_combine_attr(cul_attr, line_attr),
hl_get_underline())
: hl_combine_attr(line_attr, win_hl_attr(wp, HLF_CUL));
: hl_combine_attr(line_attr, cul_attr);
}
}
@ -3202,6 +3243,12 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
(void)mb_l;
multi_attr = win_hl_attr(wp, HLF_AT);
if (cul_attr) {
multi_attr = 0 != line_attr_lowprio
? hl_combine_attr(cul_attr, multi_attr)
: hl_combine_attr(multi_attr, cul_attr);
}
// put the pointer back to output the double-width
// character at the start of the next line.
n_extra++;
@ -3362,7 +3409,13 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
ptr = line + v;
if (!attr_pri) {
char_attr = syntax_attr;
if (cul_attr) {
char_attr = 0 != line_attr_lowprio
? hl_combine_attr(cul_attr, syntax_attr)
: hl_combine_attr(syntax_attr, cul_attr);
} else {
char_attr = syntax_attr;
}
} else {
char_attr = hl_combine_attr(syntax_attr, char_attr);
}
@ -3929,9 +3982,8 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
}
int eol_attr = char_attr;
if (wp->w_p_cul && lnum == wp->w_cursor.lnum
&& *wp->w_p_culopt != 'n') {
eol_attr = hl_combine_attr(win_hl_attr(wp, HLF_CUL), eol_attr);
if (cul_attr) {
eol_attr = hl_combine_attr(cul_attr, eol_attr);
}
linebuf_attr[off] = eol_attr;
if (wp->w_p_rl) {
@ -7516,6 +7568,49 @@ int number_width(win_T *wp)
return n;
}
/// Used when 'cursorlineopt' contains "screenline": compute the margins between
/// which the highlighting is used.
static void margin_columns_win(win_T *wp, int *left_col, int *right_col)
{
// cache previous calculations depending on w_virtcol
static int saved_w_virtcol;
static win_T *prev_wp;
static int prev_left_col;
static int prev_right_col;
static int prev_col_off;
int cur_col_off = win_col_off(wp);
int width1;
int width2;
if (saved_w_virtcol == wp->w_virtcol && prev_wp == wp
&& prev_col_off == cur_col_off) {
*right_col = prev_right_col;
*left_col = prev_left_col;
return;
}
width1 = wp->w_width - cur_col_off;
width2 = width1 + win_col_off2(wp);
*left_col = 0;
*right_col = width1;
if (wp->w_virtcol >= (colnr_T)width1) {
*right_col = width1 + ((wp->w_virtcol - width1) / width2 + 1) * width2;
}
if (wp->w_virtcol >= (colnr_T)width1 && width2 > 0) {
*left_col = (wp->w_virtcol - width1) / width2 * width2 + width1;
}
// cache values
prev_left_col = *left_col;
prev_right_col = *right_col;
prev_wp = wp;
saved_w_virtcol = wp->w_virtcol;
prev_col_off = cur_col_off;
}
/// Set dimensions of the Nvim application "shell".
void screen_resize(int width, int height)
{

View File

@ -6081,7 +6081,7 @@ static const char *highlight_init_light[] = {
"ColorColumn ctermbg=LightRed guibg=LightRed",
"CursorColumn ctermbg=LightGrey guibg=Grey90",
"CursorLine cterm=underline guibg=Grey90",
"CursorLineNr ctermfg=Brown gui=bold guifg=Brown",
"CursorLineNr cterm=underline ctermfg=Brown gui=bold guifg=Brown",
"DiffAdd ctermbg=LightBlue guibg=LightBlue",
"DiffChange ctermbg=LightMagenta guibg=LightMagenta",
"DiffDelete ctermfg=Blue ctermbg=LightCyan gui=bold guifg=Blue guibg=LightCyan",
@ -6132,7 +6132,7 @@ static const char *highlight_init_dark[] = {
"ColorColumn ctermbg=DarkRed guibg=DarkRed",
"CursorColumn ctermbg=DarkGrey guibg=Grey40",
"CursorLine cterm=underline guibg=Grey40",
"CursorLineNr ctermfg=Yellow gui=bold guifg=Yellow",
"CursorLineNr cterm=underline ctermfg=Yellow gui=bold guifg=Yellow",
"DiffAdd ctermbg=DarkBlue guibg=DarkBlue",
"DiffChange ctermbg=DarkMagenta guibg=DarkMagenta",
"DiffDelete ctermfg=Blue ctermbg=DarkCyan gui=bold guifg=Blue guibg=DarkCyan",

View File

@ -1,7 +1,7 @@
" Test for cursorline and cursorlineopt
"
source view_util.vim
source check.vim
source screendump.vim
function! s:screen_attr(lnum) abort
return map(range(1, 8), 'screenattr(a:lnum, v:val)')
@ -106,3 +106,93 @@ func Test_cursorline_highlight2()
exe 'hi' save_hi
endtry
endfunc
func Test_cursorline_screenline()
CheckScreendump
CheckOption cursorlineopt
let filename='Xcursorline'
let lines = []
let file_content =<< trim END
1 foooooooo ar einszwei drei vier fünf sechs sieben acht un zehn elf zwöfl dreizehn v ierzehn fünfzehn
2 foooooooo bar eins zwei drei vier fünf sechs sieben
3 foooooooo bar eins zwei drei vier fünf sechs sieben
4 foooooooo bar eins zwei drei vier fünf sechs sieben
END
let lines1 =<< trim END1
set nocp
set display=lastline
set cursorlineopt=screenline cursorline nu wrap sbr=>
hi CursorLineNr ctermfg=blue
25vsp
END1
let lines2 =<< trim END2
call cursor(1,1)
END2
call extend(lines, lines1)
call extend(lines, ["call append(0, ".. string(file_content).. ')'])
call extend(lines, lines2)
call writefile(lines, filename)
" basic test
let buf = RunVimInTerminal('-S '. filename, #{rows: 20})
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_1', {})
call term_sendkeys(buf, "fagj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_2', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_3', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_4', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_5', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_6', {})
" test with set list and cursorlineopt containing number
call term_sendkeys(buf, "gg0")
call term_sendkeys(buf, ":set list cursorlineopt+=number listchars=space:-\<cr>")
call VerifyScreenDump(buf, 'Test_'. filename. '_7', {})
call term_sendkeys(buf, "fagj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_8', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_9', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_10', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_11', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_12', {})
if exists("+foldcolumn") && exists("+signcolumn") && exists("+breakindent")
" test with set foldcolumn signcoloumn and breakindent
call term_sendkeys(buf, "gg0")
call term_sendkeys(buf, ":set breakindent foldcolumn=2 signcolumn=yes\<cr>")
call VerifyScreenDump(buf, 'Test_'. filename. '_13', {})
call term_sendkeys(buf, "fagj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_14', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_15', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_16', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_17', {})
call term_sendkeys(buf, "gj")
call term_wait(buf)
call VerifyScreenDump(buf, 'Test_'. filename. '_18', {})
endif
call StopVimInTerminal(buf)
call delete(filename)
endfunc