From c8c0e7d9ff05e1fb4d2076b583bca2abd8dddd2e Mon Sep 17 00:00:00 2001 From: Christian Brabandt Date: Mon, 26 Oct 2020 21:16:31 +0100 Subject: [PATCH] Get rid of g:airline_gui_mode This is needed for Neovim, because an external UI could be attached to the same neovim server, so it does not make sense to define highlighting groups with either only the cterm or the guifg attribute set. So refactor the code slightly got get rid of this variable (and since this variable is not needed anymore, we can also get rid of the guienter and OptionSet autocommand). fixes: #2261 --- autoload/airline/builder.vim | 7 +-- autoload/airline/highlighter.vim | 79 +++++++++++++++----------------- autoload/airline/init.vim | 5 -- plugin/airline.vim | 7 +-- t/highlighter.vim | 13 +++++- t/themes.vim | 25 ++++++++-- 6 files changed, 73 insertions(+), 63 deletions(-) diff --git a/autoload/airline/builder.vim b/autoload/airline/builder.vim index 345297b1..c09df414 100644 --- a/autoload/airline/builder.vim +++ b/autoload/airline/builder.vim @@ -133,11 +133,8 @@ function! airline#builder#should_change_group(group1, group2) endif let color1 = airline#highlighter#get_highlight(a:group1) let color2 = airline#highlighter#get_highlight(a:group2) - if g:airline_gui_mode ==# 'gui' - return color1[1] != color2[1] || color1[0] != color2[0] - else - return color1[3] != color2[3] || color1[2] != color2[2] - endif + return color1[1] != color2[1] || color1[0] != color2[0] + \ || color1[2] != color2[2] || color1[3] != color2[3] endfunction function! s:get_transitioned_seperator(self, prev_group, group, side) diff --git a/autoload/airline/highlighter.vim b/autoload/airline/highlighter.vim index 4e121c2c..e0ace222 100644 --- a/autoload/airline/highlighter.vim +++ b/autoload/airline/highlighter.vim @@ -12,7 +12,7 @@ let s:separators = {} let s:accents = {} let s:hl_groups = {} -function! s:gui2cui(rgb, fallback) +function! s:gui2cui(rgb, fallback) abort if a:rgb == '' return a:fallback elseif match(a:rgb, '^\%(NONE\|[fb]g\)$') > -1 @@ -22,7 +22,7 @@ function! s:gui2cui(rgb, fallback) return airline#msdos#round_msdos_colors(rgb) endfunction -function! s:group_not_done(list, name) +function! s:group_not_done(list, name) abort if index(a:list, a:name) == -1 call add(a:list, a:name) return 1 @@ -34,17 +34,14 @@ function! s:group_not_done(list, name) endif endfu -function! s:get_syn(group, what) - if !exists("g:airline_gui_mode") - let g:airline_gui_mode = airline#init#gui_mode() - endif +function! s:get_syn(group, what, mode) abort let color = '' if hlexists(a:group) - let color = synIDattr(synIDtrans(hlID(a:group)), a:what, g:airline_gui_mode) + let color = synIDattr(synIDtrans(hlID(a:group)), a:what, a:mode) endif if empty(color) || color == -1 - " should always exists - let color = synIDattr(synIDtrans(hlID('Normal')), a:what, g:airline_gui_mode) + " should always exist + let color = synIDattr(synIDtrans(hlID('Normal')), a:what, a:mode) " however, just in case if empty(color) || color == -1 let color = 'NONE' @@ -53,46 +50,46 @@ function! s:get_syn(group, what) return color endfunction -function! s:get_array(fg, bg, opts) - let opts=empty(a:opts) ? '' : join(a:opts, ',') - return g:airline_gui_mode ==# 'gui' - \ ? [ a:fg, a:bg, '', '', opts ] - \ : [ '', '', a:fg, a:bg, opts ] +function! s:get_array(guifg, guibg, ctermfg, ctermbg, opts) abort + return [ a:guifg, a:guibg, a:ctermfg, a:ctermbg, empty(a:opts) ? '' : join(a:opts, ',') ] endfunction -function! airline#highlighter#reset_hlcache() +function! airline#highlighter#reset_hlcache() abort let s:hl_groups = {} endfunction -function! airline#highlighter#get_highlight(group, ...) - let reverse = get(g:, 'airline_gui_mode', '') ==# 'gui' - \ ? synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'gui') - \ : synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'cterm') - \|| synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'term') +function! airline#highlighter#get_highlight(group, ...) abort + " only check for the cterm reverse attribute + " TODO: do we need to check all modes (gui, term, as well)? + let reverse = synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'cterm') if get(g:, 'airline_highlighting_cache', 0) && has_key(s:hl_groups, a:group) let res = s:hl_groups[a:group] return reverse ? [ res[1], res[0], res[3], res[2], res[4] ] : res else - let fg = s:get_syn(a:group, 'fg') - let bg = s:get_syn(a:group, 'bg') + let ctermfg = s:get_syn(a:group, 'fg', 'cterm') + let ctermbg = s:get_syn(a:group, 'bg', 'cterm') + let guifg = s:get_syn(a:group, 'fg', 'gui') + let guibg = s:get_syn(a:group, 'bg', 'gui') let bold = synIDattr(synIDtrans(hlID(a:group)), 'bold') if reverse - let res = s:get_array(bg, fg, bold ? ['bold'] : a:000) + let res = s:get_array(guibg, guifg, ctermbg, ctermfg, bold ? ['bold'] : a:000) else - let res = s:get_array(fg, bg, bold ? ['bold'] : a:000) + let res = s:get_array(guifg, guibg, ctermfg, ctermbg, bold ? ['bold'] : a:000) endif endif let s:hl_groups[a:group] = res return res endfunction -function! airline#highlighter#get_highlight2(fg, bg, ...) - let fg = s:get_syn(a:fg[0], a:fg[1]) - let bg = s:get_syn(a:bg[0], a:bg[1]) - return s:get_array(fg, bg, a:000) +function! airline#highlighter#get_highlight2(fg, bg, ...) abort + let guifg = s:get_syn(a:fg[0], a:fg[1], 'gui') + let guibg = s:get_syn(a:bg[0], a:bg[1], 'gui') + let ctermfg = s:get_syn(a:fg[0], a:fg[1], 'cterm') + let ctermbg = s:get_syn(a:bg[0], a:bg[1], 'cterm') + return s:get_array(guifg, guibg, ctermfg, ctermbg, a:000) endfunction -function! s:hl_group_exists(group) +function! s:hl_group_exists(group) abort if !hlexists(a:group) return 0 elseif empty(synIDattr(hlID(a:group), 'fg')) @@ -101,7 +98,7 @@ function! s:hl_group_exists(group) return 1 endfunction -function! airline#highlighter#exec(group, colors) +function! airline#highlighter#exec(group, colors) abort if pumvisible() return endif @@ -114,11 +111,7 @@ function! airline#highlighter#exec(group, colors) if len(colors) == 4 call add(colors, '') endif - if g:airline_gui_mode ==# 'gui' - let new_hi = [colors[0], colors[1], '', '', colors[4]] - else - let new_hi = ['', '', printf("%s", colors[2]), printf("%s", colors[3]), colors[4]] - endif + let new_hi = [colors[0], colors[1], printf('%s', colors[2]), printf('%s', colors[3]), colors[4]] let colors = s:CheckDefined(colors) if old_hi != new_hi || !s:hl_group_exists(a:group) let cmd = printf('hi %s%s', a:group, s:GetHiCmd(colors)) @@ -129,7 +122,7 @@ function! airline#highlighter#exec(group, colors) endif endfunction -function! s:CheckDefined(colors) +function! s:CheckDefined(colors) abort " Checks, whether the definition of the colors is valid and is not empty or NONE " e.g. if the colors would expand to this: " hi airline_c ctermfg=NONE ctermbg=NONE @@ -161,7 +154,7 @@ function! s:CheckDefined(colors) return a:colors[0:1] + [fg, bg] + [a:colors[4]] endfunction -function! s:GetHiCmd(list) +function! s:GetHiCmd(list) abort " a:list needs to have 5 items! let res = '' let i = -1 @@ -186,7 +179,7 @@ function! s:GetHiCmd(list) return res endfunction -function! s:exec_separator(dict, from, to, inverse, suffix) +function! s:exec_separator(dict, from, to, inverse, suffix) abort if pumvisible() return endif @@ -202,7 +195,7 @@ function! s:exec_separator(dict, from, to, inverse, suffix) call airline#highlighter#exec(group, colors) endfunction -function! airline#highlighter#load_theme() +function! airline#highlighter#load_theme() abort if pumvisible() return endif @@ -217,16 +210,16 @@ function! airline#highlighter#load_theme() endif endfunction -function! airline#highlighter#add_separator(from, to, inverse) +function! airline#highlighter#add_separator(from, to, inverse) abort let s:separators[a:from.a:to] = [a:from, a:to, a:inverse] call exec_separator({}, a:from, a:to, a:inverse, '') endfunction -function! airline#highlighter#add_accent(accent) +function! airline#highlighter#add_accent(accent) abort let s:accents[a:accent] = 1 endfunction -function! airline#highlighter#highlight_modified_inactive(bufnr) +function! airline#highlighter#highlight_modified_inactive(bufnr) abort if getbufvar(a:bufnr, '&modified') let colors = exists('g:airline#themes#{g:airline_theme}#palette.inactive_modified.airline_c') \ ? g:airline#themes#{g:airline_theme}#palette.inactive_modified.airline_c : [] @@ -240,7 +233,7 @@ function! airline#highlighter#highlight_modified_inactive(bufnr) endif endfunction -function! airline#highlighter#highlight(modes, ...) +function! airline#highlighter#highlight(modes, ...) abort let bufnr = a:0 ? a:1 : '' let p = g:airline#themes#{g:airline_theme}#palette diff --git a/autoload/airline/init.vim b/autoload/airline/init.vim index fc47e3e8..3e29da06 100644 --- a/autoload/airline/init.vim +++ b/autoload/airline/init.vim @@ -31,7 +31,6 @@ function! airline#init#bootstrap() call s:check_defined('g:airline_exclude_filenames', ['DebuggerWatch','DebuggerStack','DebuggerStatus']) call s:check_defined('g:airline_exclude_filetypes', []) call s:check_defined('g:airline_exclude_preview', 0) - call s:check_defined('g:airline_gui_mode', airline#init#gui_mode()) call s:check_defined('g:airline_mode_map', {}) call extend(g:airline_mode_map, { @@ -185,10 +184,6 @@ function! airline#init#bootstrap() unlet g:airline#init#bootstrapping endfunction -function! airline#init#gui_mode() - return has('gui_running') || (has("termguicolors") && &termguicolors == 1) ? 'gui' : 'cterm' -endfunction - function! airline#init#sections() let spc = g:airline_symbols.space if !exists('g:airline_section_a') diff --git a/plugin/airline.vim b/plugin/airline.vim index 5ffcfc89..c38dbcb3 100644 --- a/plugin/airline.vim +++ b/plugin/airline.vim @@ -99,7 +99,6 @@ function! s:on_colorscheme_changed() call s:init() unlet! g:airline#highlighter#normal_fg_hi call airline#highlighter#reset_hlcache() - let g:airline_gui_mode = airline#init#gui_mode() if !s:theme_in_vimrc call airline#switch_matching_theme() endif @@ -139,11 +138,7 @@ function! s:airline_toggle() \ | call on_window_changed('CmdwinEnter') autocmd CmdwinLeave * call airline#remove_statusline_func('airline#cmdwinenter') - autocmd GUIEnter,ColorScheme * call on_colorscheme_changed() - if exists("##OptionSet") - " Make sure that g_airline_gui_mode is refreshed - autocmd OptionSet termguicolors call on_colorscheme_changed() - endif + autocmd ColorScheme * call on_colorscheme_changed() " Set all statuslines to inactive autocmd FocusLost * call airline#update_statusline_focuslost() " Refresh airline for :syntax off diff --git a/t/highlighter.vim b/t/highlighter.vim index a38a5c67..36356e8d 100644 --- a/t/highlighter.vim +++ b/t/highlighter.vim @@ -6,9 +6,20 @@ describe 'highlighter' hi Foo2 ctermfg=3 ctermbg=4 call airline#highlighter#add_separator('Foo1', 'Foo2', 0) let hl = airline#highlighter#get_highlight('Foo1_to_Foo2') - Expect hl == [ '', '', '4', '2', '' ] + Expect hl == [ 'NONE', 'NONE', '4', '2', '' ] end + if exists("+termguicolors") + it 'should create separator highlight groups with termguicolors' + set termguicolors + hi Foo1 guifg=#cd0000 guibg=#00cd00 ctermfg=1 ctermbg=2 + hi Foo2 guifg=#cdcd00 guibg=#0000ee ctermfg=3 ctermbg=4 + call airline#highlighter#add_separator('Foo1', 'Foo2', 0) + let hl = airline#highlighter#get_highlight('Foo1_to_Foo2') + Expect hl == [ '#0000ee', '#00cd00', '4', '2', '' ] + end + endif + it 'should populate accent colors' Expect exists('g:airline#themes#dark#palette.normal.airline_c_red') to_be_false Expect hlID('airline_c_red') == 0 diff --git a/t/themes.vim b/t/themes.vim index bff302b8..6e50d08f 100644 --- a/t/themes.vim +++ b/t/themes.vim @@ -8,23 +8,42 @@ describe 'themes' call airline#highlighter#reset_hlcache() highlight Foo ctermfg=1 ctermbg=2 let colors = airline#themes#get_highlight('Foo') + Expect colors[0] == 'NONE' + Expect colors[1] == 'NONE' Expect colors[2] == '1' Expect colors[3] == '2' end + if exists("+termguicolors") + it 'should extract correct colors with termguicolors' + call airline#highlighter#reset_hlcache() + set termguicolors + highlight Foo guifg=#cd0000 guibg=#00cd00 ctermfg=1 ctermbg=2 + let colors = airline#themes#get_highlight('Foo') + Expect colors[0] == '#cd0000' + Expect colors[1] == '#00cd00' + Expect colors[2] == '1' + Expect colors[3] == '2' + end + endif + it 'should extract from normal if colors unavailable' call airline#highlighter#reset_hlcache() highlight Normal ctermfg=100 ctermbg=200 highlight Foo ctermbg=2 let colors = airline#themes#get_highlight('Foo') + Expect colors[0] == 'NONE' + Expect colors[1] == 'NONE' Expect colors[2] == '100' Expect colors[3] == '2' end it 'should flip target group if it is reversed' call airline#highlighter#reset_hlcache() - highlight Foo ctermbg=222 ctermfg=103 term=reverse + highlight Foo ctermbg=222 ctermfg=103 cterm=reverse let colors = airline#themes#get_highlight('Foo') + Expect colors[0] == 'NONE' + Expect colors[1] == 'NONE' Expect colors[2] == '222' Expect colors[3] == '103' end @@ -33,10 +52,10 @@ describe 'themes' call airline#highlighter#reset_hlcache() hi clear Normal let hl = airline#themes#get_highlight('Foo', 'bold', 'italic') - Expect hl == ['', '', 'NONE', 'NONE', 'bold,italic'] + Expect hl == ['NONE', 'NONE', 'NONE', 'NONE', 'bold,italic'] let hl = airline#themes#get_highlight2(['Foo','bg'], ['Foo','fg'], 'italic', 'bold') - Expect hl == ['', '', 'NONE', 'NONE', 'italic,bold'] + Expect hl == ['NONE', 'NONE', 'NONE', 'NONE', 'italic,bold'] end it 'should generate color map with mirroring'