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
This commit is contained in:
Christian Brabandt 2020-10-26 21:16:31 +01:00
parent 38c9f9ca3d
commit c8c0e7d9ff
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
6 changed files with 73 additions and 63 deletions

View File

@ -133,11 +133,8 @@ function! airline#builder#should_change_group(group1, group2)
endif endif
let color1 = airline#highlighter#get_highlight(a:group1) let color1 = airline#highlighter#get_highlight(a:group1)
let color2 = airline#highlighter#get_highlight(a:group2) let color2 = airline#highlighter#get_highlight(a:group2)
if g:airline_gui_mode ==# 'gui'
return color1[1] != color2[1] || color1[0] != color2[0] return color1[1] != color2[1] || color1[0] != color2[0]
else \ || color1[2] != color2[2] || color1[3] != color2[3]
return color1[3] != color2[3] || color1[2] != color2[2]
endif
endfunction endfunction
function! s:get_transitioned_seperator(self, prev_group, group, side) function! s:get_transitioned_seperator(self, prev_group, group, side)

View File

@ -12,7 +12,7 @@ let s:separators = {}
let s:accents = {} let s:accents = {}
let s:hl_groups = {} let s:hl_groups = {}
function! s:gui2cui(rgb, fallback) function! s:gui2cui(rgb, fallback) abort
if a:rgb == '' if a:rgb == ''
return a:fallback return a:fallback
elseif match(a:rgb, '^\%(NONE\|[fb]g\)$') > -1 elseif match(a:rgb, '^\%(NONE\|[fb]g\)$') > -1
@ -22,7 +22,7 @@ function! s:gui2cui(rgb, fallback)
return airline#msdos#round_msdos_colors(rgb) return airline#msdos#round_msdos_colors(rgb)
endfunction endfunction
function! s:group_not_done(list, name) function! s:group_not_done(list, name) abort
if index(a:list, a:name) == -1 if index(a:list, a:name) == -1
call add(a:list, a:name) call add(a:list, a:name)
return 1 return 1
@ -34,17 +34,14 @@ function! s:group_not_done(list, name)
endif endif
endfu endfu
function! s:get_syn(group, what) function! s:get_syn(group, what, mode) abort
if !exists("g:airline_gui_mode")
let g:airline_gui_mode = airline#init#gui_mode()
endif
let color = '' let color = ''
if hlexists(a:group) 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 endif
if empty(color) || color == -1 if empty(color) || color == -1
" should always exists " should always exist
let color = synIDattr(synIDtrans(hlID('Normal')), a:what, g:airline_gui_mode) let color = synIDattr(synIDtrans(hlID('Normal')), a:what, a:mode)
" however, just in case " however, just in case
if empty(color) || color == -1 if empty(color) || color == -1
let color = 'NONE' let color = 'NONE'
@ -53,46 +50,46 @@ function! s:get_syn(group, what)
return color return color
endfunction endfunction
function! s:get_array(fg, bg, opts) function! s:get_array(guifg, guibg, ctermfg, ctermbg, opts) abort
let opts=empty(a:opts) ? '' : join(a:opts, ',') return [ a:guifg, a:guibg, a:ctermfg, a:ctermbg, empty(a:opts) ? '' : join(a:opts, ',') ]
return g:airline_gui_mode ==# 'gui'
\ ? [ a:fg, a:bg, '', '', opts ]
\ : [ '', '', a:fg, a:bg, opts ]
endfunction endfunction
function! airline#highlighter#reset_hlcache() function! airline#highlighter#reset_hlcache() abort
let s:hl_groups = {} let s:hl_groups = {}
endfunction endfunction
function! airline#highlighter#get_highlight(group, ...) function! airline#highlighter#get_highlight(group, ...) abort
let reverse = get(g:, 'airline_gui_mode', '') ==# 'gui' " only check for the cterm reverse attribute
\ ? synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'gui') " TODO: do we need to check all modes (gui, term, as well)?
\ : synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'cterm') let reverse = synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'cterm')
\|| synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'term')
if get(g:, 'airline_highlighting_cache', 0) && has_key(s:hl_groups, a:group) if get(g:, 'airline_highlighting_cache', 0) && has_key(s:hl_groups, a:group)
let res = 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 return reverse ? [ res[1], res[0], res[3], res[2], res[4] ] : res
else else
let fg = s:get_syn(a:group, 'fg') let ctermfg = s:get_syn(a:group, 'fg', 'cterm')
let bg = s:get_syn(a:group, 'bg') 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') let bold = synIDattr(synIDtrans(hlID(a:group)), 'bold')
if reverse 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 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
endif endif
let s:hl_groups[a:group] = res let s:hl_groups[a:group] = res
return res return res
endfunction endfunction
function! airline#highlighter#get_highlight2(fg, bg, ...) function! airline#highlighter#get_highlight2(fg, bg, ...) abort
let fg = s:get_syn(a:fg[0], a:fg[1]) let guifg = s:get_syn(a:fg[0], a:fg[1], 'gui')
let bg = s:get_syn(a:bg[0], a:bg[1]) let guibg = s:get_syn(a:bg[0], a:bg[1], 'gui')
return s:get_array(fg, bg, a:000) 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 endfunction
function! s:hl_group_exists(group) function! s:hl_group_exists(group) abort
if !hlexists(a:group) if !hlexists(a:group)
return 0 return 0
elseif empty(synIDattr(hlID(a:group), 'fg')) elseif empty(synIDattr(hlID(a:group), 'fg'))
@ -101,7 +98,7 @@ function! s:hl_group_exists(group)
return 1 return 1
endfunction endfunction
function! airline#highlighter#exec(group, colors) function! airline#highlighter#exec(group, colors) abort
if pumvisible() if pumvisible()
return return
endif endif
@ -114,11 +111,7 @@ function! airline#highlighter#exec(group, colors)
if len(colors) == 4 if len(colors) == 4
call add(colors, '') call add(colors, '')
endif endif
if g:airline_gui_mode ==# 'gui' let new_hi = [colors[0], colors[1], printf('%s', colors[2]), printf('%s', colors[3]), colors[4]]
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 colors = s:CheckDefined(colors) let colors = s:CheckDefined(colors)
if old_hi != new_hi || !s:hl_group_exists(a:group) if old_hi != new_hi || !s:hl_group_exists(a:group)
let cmd = printf('hi %s%s', a:group, s:GetHiCmd(colors)) let cmd = printf('hi %s%s', a:group, s:GetHiCmd(colors))
@ -129,7 +122,7 @@ function! airline#highlighter#exec(group, colors)
endif endif
endfunction 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 " Checks, whether the definition of the colors is valid and is not empty or NONE
" e.g. if the colors would expand to this: " e.g. if the colors would expand to this:
" hi airline_c ctermfg=NONE ctermbg=NONE " 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]] return a:colors[0:1] + [fg, bg] + [a:colors[4]]
endfunction endfunction
function! s:GetHiCmd(list) function! s:GetHiCmd(list) abort
" a:list needs to have 5 items! " a:list needs to have 5 items!
let res = '' let res = ''
let i = -1 let i = -1
@ -186,7 +179,7 @@ function! s:GetHiCmd(list)
return res return res
endfunction endfunction
function! s:exec_separator(dict, from, to, inverse, suffix) function! s:exec_separator(dict, from, to, inverse, suffix) abort
if pumvisible() if pumvisible()
return return
endif endif
@ -202,7 +195,7 @@ function! s:exec_separator(dict, from, to, inverse, suffix)
call airline#highlighter#exec(group, colors) call airline#highlighter#exec(group, colors)
endfunction endfunction
function! airline#highlighter#load_theme() function! airline#highlighter#load_theme() abort
if pumvisible() if pumvisible()
return return
endif endif
@ -217,16 +210,16 @@ function! airline#highlighter#load_theme()
endif endif
endfunction 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] let s:separators[a:from.a:to] = [a:from, a:to, a:inverse]
call <sid>exec_separator({}, a:from, a:to, a:inverse, '') call <sid>exec_separator({}, a:from, a:to, a:inverse, '')
endfunction endfunction
function! airline#highlighter#add_accent(accent) function! airline#highlighter#add_accent(accent) abort
let s:accents[a:accent] = 1 let s:accents[a:accent] = 1
endfunction endfunction
function! airline#highlighter#highlight_modified_inactive(bufnr) function! airline#highlighter#highlight_modified_inactive(bufnr) abort
if getbufvar(a:bufnr, '&modified') if getbufvar(a:bufnr, '&modified')
let colors = exists('g:airline#themes#{g:airline_theme}#palette.inactive_modified.airline_c') 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 : [] \ ? g:airline#themes#{g:airline_theme}#palette.inactive_modified.airline_c : []
@ -240,7 +233,7 @@ function! airline#highlighter#highlight_modified_inactive(bufnr)
endif endif
endfunction endfunction
function! airline#highlighter#highlight(modes, ...) function! airline#highlighter#highlight(modes, ...) abort
let bufnr = a:0 ? a:1 : '' let bufnr = a:0 ? a:1 : ''
let p = g:airline#themes#{g:airline_theme}#palette let p = g:airline#themes#{g:airline_theme}#palette

View File

@ -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_filenames', ['DebuggerWatch','DebuggerStack','DebuggerStatus'])
call s:check_defined('g:airline_exclude_filetypes', []) call s:check_defined('g:airline_exclude_filetypes', [])
call s:check_defined('g:airline_exclude_preview', 0) 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 s:check_defined('g:airline_mode_map', {})
call extend(g:airline_mode_map, { call extend(g:airline_mode_map, {
@ -185,10 +184,6 @@ function! airline#init#bootstrap()
unlet g:airline#init#bootstrapping unlet g:airline#init#bootstrapping
endfunction endfunction
function! airline#init#gui_mode()
return has('gui_running') || (has("termguicolors") && &termguicolors == 1) ? 'gui' : 'cterm'
endfunction
function! airline#init#sections() function! airline#init#sections()
let spc = g:airline_symbols.space let spc = g:airline_symbols.space
if !exists('g:airline_section_a') if !exists('g:airline_section_a')

View File

@ -99,7 +99,6 @@ function! s:on_colorscheme_changed()
call s:init() call s:init()
unlet! g:airline#highlighter#normal_fg_hi unlet! g:airline#highlighter#normal_fg_hi
call airline#highlighter#reset_hlcache() call airline#highlighter#reset_hlcache()
let g:airline_gui_mode = airline#init#gui_mode()
if !s:theme_in_vimrc if !s:theme_in_vimrc
call airline#switch_matching_theme() call airline#switch_matching_theme()
endif endif
@ -139,11 +138,7 @@ function! s:airline_toggle()
\ | call <sid>on_window_changed('CmdwinEnter') \ | call <sid>on_window_changed('CmdwinEnter')
autocmd CmdwinLeave * call airline#remove_statusline_func('airline#cmdwinenter') autocmd CmdwinLeave * call airline#remove_statusline_func('airline#cmdwinenter')
autocmd GUIEnter,ColorScheme * call <sid>on_colorscheme_changed() autocmd ColorScheme * call <sid>on_colorscheme_changed()
if exists("##OptionSet")
" Make sure that g_airline_gui_mode is refreshed
autocmd OptionSet termguicolors call <sid>on_colorscheme_changed()
endif
" Set all statuslines to inactive " Set all statuslines to inactive
autocmd FocusLost * call airline#update_statusline_focuslost() autocmd FocusLost * call airline#update_statusline_focuslost()
" Refresh airline for :syntax off " Refresh airline for :syntax off

View File

@ -6,9 +6,20 @@ describe 'highlighter'
hi Foo2 ctermfg=3 ctermbg=4 hi Foo2 ctermfg=3 ctermbg=4
call airline#highlighter#add_separator('Foo1', 'Foo2', 0) call airline#highlighter#add_separator('Foo1', 'Foo2', 0)
let hl = airline#highlighter#get_highlight('Foo1_to_Foo2') let hl = airline#highlighter#get_highlight('Foo1_to_Foo2')
Expect hl == [ '', '', '4', '2', '' ] Expect hl == [ 'NONE', 'NONE', '4', '2', '' ]
end 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' it 'should populate accent colors'
Expect exists('g:airline#themes#dark#palette.normal.airline_c_red') to_be_false Expect exists('g:airline#themes#dark#palette.normal.airline_c_red') to_be_false
Expect hlID('airline_c_red') == 0 Expect hlID('airline_c_red') == 0

View File

@ -8,23 +8,42 @@ describe 'themes'
call airline#highlighter#reset_hlcache() call airline#highlighter#reset_hlcache()
highlight Foo ctermfg=1 ctermbg=2 highlight Foo ctermfg=1 ctermbg=2
let colors = airline#themes#get_highlight('Foo') let colors = airline#themes#get_highlight('Foo')
Expect colors[0] == 'NONE'
Expect colors[1] == 'NONE'
Expect colors[2] == '1' Expect colors[2] == '1'
Expect colors[3] == '2' Expect colors[3] == '2'
end 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' it 'should extract from normal if colors unavailable'
call airline#highlighter#reset_hlcache() call airline#highlighter#reset_hlcache()
highlight Normal ctermfg=100 ctermbg=200 highlight Normal ctermfg=100 ctermbg=200
highlight Foo ctermbg=2 highlight Foo ctermbg=2
let colors = airline#themes#get_highlight('Foo') let colors = airline#themes#get_highlight('Foo')
Expect colors[0] == 'NONE'
Expect colors[1] == 'NONE'
Expect colors[2] == '100' Expect colors[2] == '100'
Expect colors[3] == '2' Expect colors[3] == '2'
end end
it 'should flip target group if it is reversed' it 'should flip target group if it is reversed'
call airline#highlighter#reset_hlcache() 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') let colors = airline#themes#get_highlight('Foo')
Expect colors[0] == 'NONE'
Expect colors[1] == 'NONE'
Expect colors[2] == '222' Expect colors[2] == '222'
Expect colors[3] == '103' Expect colors[3] == '103'
end end
@ -33,10 +52,10 @@ describe 'themes'
call airline#highlighter#reset_hlcache() call airline#highlighter#reset_hlcache()
hi clear Normal hi clear Normal
let hl = airline#themes#get_highlight('Foo', 'bold', 'italic') 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') 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 end
it 'should generate color map with mirroring' it 'should generate color map with mirroring'