highlighter: Add condition for new vim9 script implementation

Add a new condition that will contain the new vim9script implementation
of the highlighter logic. Not done yet.

To test it out, you need a recent Vim 8.2 and in addition set the
configuration variable

    :let g:airline_experimental=1

Implementation for all the functions will follow.
This commit is contained in:
Christian 2020-11-18 08:12:58 +01:00 committed by Christian Brabandt
parent b0d4a44f0c
commit a8afa434e3
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09

View File

@ -13,298 +13,305 @@ let s:separators = {}
let s:accents = {} let s:accents = {}
let s:hl_groups = {} let s:hl_groups = {}
function! s:gui2cui(rgb, fallback) abort " {{{2 if !exists(":def") || (exists(":def") && get(g:, "airline_experimental", 0)==0)
if a:rgb == '' " Legacy VimScript implementation {{{1
return a:fallback function! s:gui2cui(rgb, fallback) abort " {{{2
elseif match(a:rgb, '^\%(NONE\|[fb]g\)$') > -1 if a:rgb == ''
return a:rgb return a:fallback
endif elseif match(a:rgb, '^\%(NONE\|[fb]g\)$') > -1
let rgb = map(split(a:rgb[1:], '..\zs'), '0 + ("0x".v:val)') return a:rgb
return airline#msdos#round_msdos_colors(rgb) endif
endfunction let rgb = map(split(a:rgb[1:], '..\zs'), '0 + ("0x".v:val)')
return airline#msdos#round_msdos_colors(rgb)
endfunction
function! s:group_not_done(list, name) abort " {{{2 function! s:group_not_done(list, name) abort " {{{2
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
else else
if &vbs if &vbs
echomsg printf("airline: group: %s already done, skipping", a:name) echomsg printf("airline: group: %s already done, skipping", a:name)
endif endif
return 0 return 0
endif endif
endfu endfu
function! s:get_syn(group, what, mode) abort "{{{2 function! s:get_syn(group, what, mode) abort "{{{2
let color = '' let color = ''
if hlexists(a:group) if hlexists(a:group)
let color = synIDattr(synIDtrans(hlID(a:group)), a:what, a: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 exist " should always exist
let color = synIDattr(synIDtrans(hlID('Normal')), a:what, a: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'
endif endif
endif endif
return color return color
endfunction endfunction
function! s:get_array(guifg, guibg, ctermfg, ctermbg, opts) abort " {{{2 function! s:get_array(guifg, guibg, ctermfg, ctermbg, opts) abort " {{{2
return [ a:guifg, a:guibg, a:ctermfg, a:ctermbg, empty(a:opts) ? '' : join(a:opts, ',') ] return [ a:guifg, a:guibg, a:ctermfg, a:ctermbg, empty(a:opts) ? '' : join(a:opts, ',') ]
endfunction endfunction
function! airline#highlighter#reset_hlcache() abort " {{{2 function! airline#highlighter#reset_hlcache() abort " {{{2
let s:hl_groups = {} let s:hl_groups = {}
endfunction endfunction
function! airline#highlighter#get_highlight(group, ...) abort " {{{2 function! airline#highlighter#get_highlight(group, ...) abort " {{{2
" only check for the cterm reverse attribute " only check for the cterm reverse attribute
" TODO: do we need to check all modes (gui, term, as well)? " TODO: do we need to check all modes (gui, term, as well)?
let reverse = synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'cterm') let reverse = synIDattr(synIDtrans(hlID(a:group)), 'reverse', 'cterm')
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 ctermfg = s:get_syn(a:group, 'fg', 'cterm') let ctermfg = s:get_syn(a:group, 'fg', 'cterm')
let ctermbg = s:get_syn(a:group, 'bg', 'cterm') let ctermbg = s:get_syn(a:group, 'bg', 'cterm')
let guifg = s:get_syn(a:group, 'fg', 'gui') let guifg = s:get_syn(a:group, 'fg', 'gui')
let guibg = s:get_syn(a:group, 'bg', '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(guibg, guifg, ctermbg, ctermfg, bold ? ['bold'] : a:000) let res = s:get_array(guibg, guifg, ctermbg, ctermfg, bold ? ['bold'] : a:000)
else else
let res = s:get_array(guifg, guibg, ctermfg, ctermbg, 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, ...) abort " {{{2 function! airline#highlighter#get_highlight2(fg, bg, ...) abort " {{{2
let guifg = s:get_syn(a:fg[0], a:fg[1], 'gui') 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 guibg = s:get_syn(a:bg[0], a:bg[1], 'gui')
let ctermfg = s:get_syn(a:fg[0], a:fg[1], 'cterm') let ctermfg = s:get_syn(a:fg[0], a:fg[1], 'cterm')
let ctermbg = s:get_syn(a:bg[0], a:bg[1], 'cterm') let ctermbg = s:get_syn(a:bg[0], a:bg[1], 'cterm')
return s:get_array(guifg, guibg, ctermfg, ctermbg, a:000) return s:get_array(guifg, guibg, ctermfg, ctermbg, a:000)
endfunction endfunction
function! s:hl_group_exists(group) abort " {{{2 function! s:hl_group_exists(group) abort " {{{2
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'))
return 0 return 0
endif endif
return 1 return 1
endfunction endfunction
function! airline#highlighter#exec(group, colors) abort " {{{2 function! airline#highlighter#exec(group, colors) abort " {{{2
if pumvisible() if pumvisible()
return return
endif endif
let colors = a:colors let colors = a:colors
if s:is_win32term if s:is_win32term
let colors[2] = s:gui2cui(get(colors, 0, ''), get(colors, 2, '')) let colors[2] = s:gui2cui(get(colors, 0, ''), get(colors, 2, ''))
let colors[3] = s:gui2cui(get(colors, 1, ''), get(colors, 3, '')) let colors[3] = s:gui2cui(get(colors, 1, ''), get(colors, 3, ''))
endif endif
let old_hi = airline#highlighter#get_highlight(a:group) let old_hi = airline#highlighter#get_highlight(a:group)
if len(colors) == 4 if len(colors) == 4
call add(colors, '') call add(colors, '')
endif endif
let new_hi = [colors[0], colors[1], printf('%s', colors[2]), printf('%s', colors[3]), colors[4]] let new_hi = [colors[0], colors[1], printf('%s', colors[2]), printf('%s', colors[3]), colors[4]]
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))
try try
exe cmd exe cmd
catch catch
echoerr "Error when running command: ". cmd echoerr "Error when running command: ". cmd
endtry endtry
if has_key(s:hl_groups, a:group) if has_key(s:hl_groups, a:group)
let s:hl_groups[a:group] = colors let s:hl_groups[a:group] = colors
endif endif
endif endif
endfunction endfunction
function! s:CheckDefined(colors) abort " {{{2 function! s:CheckDefined(colors) abort " {{{2
" 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
" that means to clear that highlighting group, therefore, fallback to Normal " that means to clear that highlighting group, therefore, fallback to Normal
" highlighting group for the cterm values " highlighting group for the cterm values
" This only works, if the Normal highlighting group is actually defined, so " This only works, if the Normal highlighting group is actually defined, so
" return early, if it has been cleared " return early, if it has been cleared
if !exists("g:airline#highlighter#normal_fg_hi") if !exists("g:airline#highlighter#normal_fg_hi")
let g:airline#highlighter#normal_fg_hi = synIDattr(synIDtrans(hlID('Normal')), 'fg', 'cterm') let g:airline#highlighter#normal_fg_hi = synIDattr(synIDtrans(hlID('Normal')), 'fg', 'cterm')
endif endif
if empty(g:airline#highlighter#normal_fg_hi) || g:airline#highlighter#normal_fg_hi < 0 if empty(g:airline#highlighter#normal_fg_hi) || g:airline#highlighter#normal_fg_hi < 0
return a:colors return a:colors
endif endif
for val in a:colors for val in a:colors
if !empty(val) && val !=# 'NONE' if !empty(val) && val !=# 'NONE'
return a:colors return a:colors
endif endif
endfor endfor
" this adds the bold attribute to the term argument of the :hi command, " this adds the bold attribute to the term argument of the :hi command,
" but at least this makes sure, the group will be defined " but at least this makes sure, the group will be defined
let fg = g:airline#highlighter#normal_fg_hi let fg = g:airline#highlighter#normal_fg_hi
let bg = synIDattr(synIDtrans(hlID('Normal')), 'bg', 'cterm') let bg = synIDattr(synIDtrans(hlID('Normal')), 'bg', 'cterm')
if bg < 0 if bg < 0
" in case there is no background color defined for Normal " in case there is no background color defined for Normal
let bg = a:colors[3] let bg = a:colors[3]
endif endif
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) abort " {{{2 function! s:GetHiCmd(list) abort " {{{2
" a:list needs to have 5 items! " a:list needs to have 5 items!
let res = '' let res = ''
let i = -1 let i = -1
while i < 4 while i < 4
let i += 1 let i += 1
let item = get(a:list, i, '') let item = get(a:list, i, '')
if item is '' if item is ''
continue continue
endif endif
if i == 0 if i == 0
let res .= ' guifg='.item let res .= ' guifg='.item
elseif i == 1 elseif i == 1
let res .= ' guibg='.item let res .= ' guibg='.item
elseif i == 2 elseif i == 2
let res .= ' ctermfg='.item let res .= ' ctermfg='.item
elseif i == 3 elseif i == 3
let res .= ' ctermbg='.item let res .= ' ctermbg='.item
elseif i == 4 elseif i == 4
let res .= printf(' gui=%s cterm=%s term=%s', item, item, item) let res .= printf(' gui=%s cterm=%s term=%s', item, item, item)
endif endif
endwhile endwhile
return res return res
endfunction endfunction
function! s:exec_separator(dict, from, to, inverse, suffix) abort " {{{2 function! s:exec_separator(dict, from, to, inverse, suffix) abort " {{{2
if pumvisible() if pumvisible()
return return
endif endif
let group = a:from.'_to_'.a:to.a:suffix let group = a:from.'_to_'.a:to.a:suffix
let l:from = airline#themes#get_highlight(a:from.a:suffix) let l:from = airline#themes#get_highlight(a:from.a:suffix)
let l:to = airline#themes#get_highlight(a:to.a:suffix) let l:to = airline#themes#get_highlight(a:to.a:suffix)
if a:inverse if a:inverse
let colors = [ l:from[1], l:to[1], l:from[3], l:to[3] ] let colors = [ l:from[1], l:to[1], l:from[3], l:to[3] ]
else else
let colors = [ l:to[1], l:from[1], l:to[3], l:from[3] ] let colors = [ l:to[1], l:from[1], l:to[3], l:from[3] ]
endif endif
let a:dict[group] = colors let a:dict[group] = colors
call airline#highlighter#exec(group, colors) call airline#highlighter#exec(group, colors)
endfunction endfunction
function! airline#highlighter#load_theme() abort " {{{2 function! airline#highlighter#load_theme() abort " {{{2
if pumvisible() if pumvisible()
return return
endif endif
for winnr in filter(range(1, winnr('$')), 'v:val != winnr()') for winnr in filter(range(1, winnr('$')), 'v:val != winnr()')
call airline#highlighter#highlight_modified_inactive(winbufnr(winnr)) call airline#highlighter#highlight_modified_inactive(winbufnr(winnr))
endfor endfor
call airline#highlighter#highlight(['inactive']) call airline#highlighter#highlight(['inactive'])
if getbufvar( bufnr('%'), '&modified' ) if getbufvar( bufnr('%'), '&modified' )
call airline#highlighter#highlight(['normal', 'modified']) call airline#highlighter#highlight(['normal', 'modified'])
else else
call airline#highlighter#highlight(['normal']) call airline#highlighter#highlight(['normal'])
endif endif
endfunction endfunction
function! airline#highlighter#add_separator(from, to, inverse) abort " {{{2 function! airline#highlighter#add_separator(from, to, inverse) abort " {{{2
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) abort " {{{2 function! airline#highlighter#add_accent(accent) abort " {{{2
let s:accents[a:accent] = 1 let s:accents[a:accent] = 1
endfunction endfunction
function! airline#highlighter#highlight_modified_inactive(bufnr) abort " {{{2 function! airline#highlighter#highlight_modified_inactive(bufnr) abort " {{{2
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 : []
else else
let colors = exists('g:airline#themes#{g:airline_theme}#palette.inactive.airline_c') let colors = exists('g:airline#themes#{g:airline_theme}#palette.inactive.airline_c')
\ ? g:airline#themes#{g:airline_theme}#palette.inactive.airline_c : [] \ ? g:airline#themes#{g:airline_theme}#palette.inactive.airline_c : []
endif endif
if !empty(colors) if !empty(colors)
call airline#highlighter#exec('airline_c'.(a:bufnr).'_inactive', colors) call airline#highlighter#exec('airline_c'.(a:bufnr).'_inactive', colors)
endif endif
endfunction endfunction
function! airline#highlighter#highlight(modes, ...) abort " {{{2 function! airline#highlighter#highlight(modes, ...) abort " {{{2
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
" draw the base mode, followed by any overrides " draw the base mode, followed by any overrides
let mapped = map(a:modes, 'v:val == a:modes[0] ? v:val : a:modes[0]."_".v:val') let mapped = map(a:modes, 'v:val == a:modes[0] ? v:val : a:modes[0]."_".v:val')
let suffix = a:modes[0] == 'inactive' ? '_inactive' : '' let suffix = a:modes[0] == 'inactive' ? '_inactive' : ''
let airline_grouplist = [] let airline_grouplist = []
let buffers_in_tabpage = sort(tabpagebuflist()) let buffers_in_tabpage = sort(tabpagebuflist())
if exists("*uniq") if exists("*uniq")
let buffers_in_tabpage = uniq(buffers_in_tabpage) let buffers_in_tabpage = uniq(buffers_in_tabpage)
endif endif
" mapped might be something like ['normal', 'normal_modified'] " mapped might be something like ['normal', 'normal_modified']
" if a group is in both modes available, only define the second " if a group is in both modes available, only define the second
" that is how this was done previously overwrite the previous definition " that is how this was done previously overwrite the previous definition
for mode in reverse(mapped) for mode in reverse(mapped)
if exists('g:airline#themes#{g:airline_theme}#palette[mode]') if exists('g:airline#themes#{g:airline_theme}#palette[mode]')
let dict = g:airline#themes#{g:airline_theme}#palette[mode] let dict = g:airline#themes#{g:airline_theme}#palette[mode]
for kvp in items(dict) for kvp in items(dict)
let mode_colors = kvp[1] let mode_colors = kvp[1]
let name = kvp[0] let name = kvp[0]
if name is# 'airline_c' && !empty(bufnr) && suffix is# '_inactive' if name is# 'airline_c' && !empty(bufnr) && suffix is# '_inactive'
let name = 'airline_c'.bufnr let name = 'airline_c'.bufnr
endif endif
" do not re-create highlighting for buffers that are no longer visible " do not re-create highlighting for buffers that are no longer visible
" in the current tabpage " in the current tabpage
if name =~# 'airline_c\d\+' if name =~# 'airline_c\d\+'
let bnr = matchstr(name, 'airline_c\zs\d\+') + 0 let bnr = matchstr(name, 'airline_c\zs\d\+') + 0
if bnr > 0 && index(buffers_in_tabpage, bnr) == -1 if bnr > 0 && index(buffers_in_tabpage, bnr) == -1
continue continue
endif endif
elseif (name =~# '_to_') || (name[0:10] is# 'airline_tab' && !empty(suffix)) elseif (name =~# '_to_') || (name[0:10] is# 'airline_tab' && !empty(suffix))
" group will be redefined below at exec_separator " group will be redefined below at exec_separator
" or is not needed for tabline with '_inactive' suffix " or is not needed for tabline with '_inactive' suffix
" since active flag is 1 for builder) " since active flag is 1 for builder)
continue continue
endif endif
if s:group_not_done(airline_grouplist, name.suffix) if s:group_not_done(airline_grouplist, name.suffix)
call airline#highlighter#exec(name.suffix, mode_colors) call airline#highlighter#exec(name.suffix, mode_colors)
endif endif
if !has_key(p, 'accents') if !has_key(p, 'accents')
" work around a broken installation " work around a broken installation
" shouldn't actually happen, p should always contain accents " shouldn't actually happen, p should always contain accents
continue continue
endif endif
for accent in keys(s:accents) for accent in keys(s:accents)
if !has_key(p.accents, accent) if !has_key(p.accents, accent)
continue continue
endif endif
let colors = copy(mode_colors) let colors = copy(mode_colors)
if p.accents[accent][0] != '' if p.accents[accent][0] != ''
let colors[0] = p.accents[accent][0] let colors[0] = p.accents[accent][0]
endif endif
if p.accents[accent][2] != '' if p.accents[accent][2] != ''
let colors[2] = p.accents[accent][2] let colors[2] = p.accents[accent][2]
endif endif
if len(colors) >= 5 if len(colors) >= 5
let colors[4] = get(p.accents[accent], 4, '') let colors[4] = get(p.accents[accent], 4, '')
else else
call add(colors, get(p.accents[accent], 4, '')) call add(colors, get(p.accents[accent], 4, ''))
endif endif
if s:group_not_done(airline_grouplist, name.suffix.'_'.accent) if s:group_not_done(airline_grouplist, name.suffix.'_'.accent)
call airline#highlighter#exec(name.suffix.'_'.accent, colors) call airline#highlighter#exec(name.suffix.'_'.accent, colors)
endif endif
endfor endfor
endfor endfor
if empty(s:separators) if empty(s:separators)
" nothing to be done " nothing to be done
continue continue
endif endif
" TODO: optimize this " TODO: optimize this
for sep in items(s:separators) for sep in items(s:separators)
" we cannot check, that the group already exists, else the separators " we cannot check, that the group already exists, else the separators
" might not be correctly defined. But perhaps we can skip above groups " might not be correctly defined. But perhaps we can skip above groups
" that match the '_to_' name, because they would be redefined here... " that match the '_to_' name, because they would be redefined here...
call <sid>exec_separator(dict, sep[1][0], sep[1][1], sep[1][2], suffix) call <sid>exec_separator(dict, sep[1][0], sep[1][1], sep[1][2], suffix)
endfor endfor
endif endif
endfor endfor
endfunction endfunction
finish
else
" This is using Vim9 script " {{{1
endif