From 2f3b33daae69ca38ccc1ba9c7c4dcb4d36d46eb6 Mon Sep 17 00:00:00 2001 From: Bailey Ling Date: Fri, 27 Sep 2013 21:36:44 -0400 Subject: [PATCH 1/2] fix solarized theme being out of sync (#288). --- autoload/airline.vim | 19 +++++++----- autoload/airline/builder.vim | 1 - autoload/airline/highlighter.vim | 43 +++++++++++++-------------- autoload/airline/themes/solarized.vim | 9 ++---- t/highlighter.vim | 8 ++--- 5 files changed, 39 insertions(+), 41 deletions(-) diff --git a/autoload/airline.vim b/autoload/airline.vim index 2373668..133da4f 100644 --- a/autoload/airline.vim +++ b/autoload/airline.vim @@ -26,6 +26,18 @@ function! airline#add_inactive_statusline_func(name) endfunction function! airline#load_theme() + if exists('*airline#themes#{g:airline_theme}#refresh') + call airline#themes#{g:airline_theme}#refresh() + endif + + let palette = g:airline#themes#{g:airline_theme}#palette + call airline#themes#patch(palette) + + if exists('g:airline_theme_patch_func') + let Fn = function(g:airline_theme_patch_func) + call Fn(palette) + endif + call airline#highlighter#load_theme() call airline#extensions#load_theme() endfunction @@ -40,15 +52,8 @@ function! airline#switch_theme(name) return else let g:airline_theme = 'dark' - let palette = g:airline#themes#dark#palette endif endtry - call airline#themes#patch(palette) - - if exists('g:airline_theme_patch_func') - let Fn = function(g:airline_theme_patch_func) - call Fn(palette) - endif let w:airline_lastmode = '' call airline#update_statusline() diff --git a/autoload/airline/builder.vim b/autoload/airline/builder.vim index b1a1e27..a6c867c 100644 --- a/autoload/airline/builder.vim +++ b/autoload/airline/builder.vim @@ -28,7 +28,6 @@ function! s:prototype.add_section(group, contents) let content_parts = split(a:contents, '__accent') for cpart in content_parts let accent = matchstr(cpart, '_\zs[^#]*\ze') - call airline#highlighter#add_accent(a:group, accent) call add(contents, cpart) endfor let line = join(contents, a:group) diff --git a/autoload/airline/highlighter.vim b/autoload/airline/highlighter.vim index b677c2e..24aa6fa 100644 --- a/autoload/airline/highlighter.vim +++ b/autoload/airline/highlighter.vim @@ -94,28 +94,6 @@ function! airline#highlighter#add_separator(from, to, inverse) call exec_separator({}, a:from, a:to, a:inverse, '') endfunction -function! airline#highlighter#add_accent(group, accent) - let p = g:airline#themes#{g:airline_theme}#palette - if exists('p.accents') - if has_key(p.accents, a:accent) - for kvp in items(p) - let mode_colors = kvp[1] - if has_key(mode_colors, a:group) - let colors = copy(mode_colors[a:group]) - if p.accents[a:accent][0] != '' - let colors[0] = p.accents[a:accent][0] - endif - if p.accents[a:accent][2] != '' - let colors[2] = p.accents[a:accent][2] - endif - let colors[4] = get(p.accents[a:accent], 4, '') - let mode_colors[a:group.'_'.a:accent] = colors - endif - endfor - endif - endif -endfunction - function! airline#highlighter#highlight_modified_inactive(bufnr) if getbufvar(a:bufnr, '&modified') let colors = exists('g:airline#themes#{g:airline_theme}#palette.inactive_modified.airline_c') @@ -131,6 +109,8 @@ function! airline#highlighter#highlight_modified_inactive(bufnr) endfunction function! airline#highlighter#highlight(modes) + let p = g:airline#themes#{g:airline_theme}#palette + " 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 suffix = a:modes[0] == 'inactive' ? '_inactive' : '' @@ -138,7 +118,24 @@ function! airline#highlighter#highlight(modes) if exists('g:airline#themes#{g:airline_theme}#palette[mode]') let dict = g:airline#themes#{g:airline_theme}#palette[mode] for kvp in items(dict) - call airline#highlighter#exec(kvp[0].suffix, kvp[1]) + let mode_colors = kvp[1] + call airline#highlighter#exec(kvp[0].suffix, mode_colors) + + for accent in keys(p.accents) + let colors = copy(mode_colors) + if p.accents[accent][0] != '' + let colors[0] = p.accents[accent][0] + endif + if p.accents[accent][2] != '' + let colors[2] = p.accents[accent][2] + endif + if len(colors) >= 5 + let colors[4] = get(p.accents[accent], 4, '') + else + call add(colors, get(p.accents[accent], 4, '')) + endif + call airline#highlighter#exec(kvp[0].suffix.'_'.accent, colors) + endfor endfor " TODO: optimize this diff --git a/autoload/airline/themes/solarized.vim b/autoload/airline/themes/solarized.vim index 4516f95..db52bcf 100644 --- a/autoload/airline/themes/solarized.vim +++ b/autoload/airline/themes/solarized.vim @@ -1,6 +1,6 @@ let g:airline#themes#solarized#palette = {} -function! s:generate() +function! airline#themes#solarized#refresh() """""""""""""""""""""""""""""""""""""""""""""""" " Options """""""""""""""""""""""""""""""""""""""""""""""" @@ -169,8 +169,5 @@ function! s:generate() \ s:N2[0].g, s:N2[1].g, s:N2[0].t, s:N2[1].t, s:N2[2]] endfunction -call s:generate() -augroup airline_solarized - autocmd! - autocmd ColorScheme * call generate() | call airline#load_theme() -augroup END +call airline#themes#solarized#refresh() + diff --git a/t/highlighter.vim b/t/highlighter.vim index c0f7c38..0a10c19 100644 --- a/t/highlighter.vim +++ b/t/highlighter.vim @@ -11,10 +11,10 @@ describe 'highlighter' 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.insert.airline_c_red') to_be_false - call airline#highlighter#add_accent('airline_c', 'red') - Expect exists('g:airline#themes#dark#palette.normal.airline_c_red') to_be_true - Expect exists('g:airline#themes#dark#palette.insert.airline_c_red') to_be_true + Expect hlID('airline_c_red') == 0 + call airline#themes#patch(g:airline#themes#dark#palette) + call airline#highlighter#highlight(['normal']) + Expect hlID('airline_c_red') != 0 end end From a81d1cd9c73540ed831daf1351384c9d0bb532cd Mon Sep 17 00:00:00 2001 From: Bailey Ling Date: Fri, 27 Sep 2013 21:43:32 -0400 Subject: [PATCH 2/2] refactor the rest of the themes to use refresh. resolves #288. --- autoload/airline/themes/base16.vim | 95 ++++++++++++-------------- autoload/airline/themes/jellybeans.vim | 10 ++- autoload/airline/themes/monochrome.vim | 9 +-- autoload/airline/themes/tomorrow.vim | 9 +-- autoload/airline/themes/zenburn.vim | 8 +-- 5 files changed, 55 insertions(+), 76 deletions(-) diff --git a/autoload/airline/themes/base16.vim b/autoload/airline/themes/base16.vim index c5af59f..469435e 100644 --- a/autoload/airline/themes/base16.vim +++ b/autoload/airline/themes/base16.vim @@ -1,4 +1,4 @@ -function! s:load_constant() +if get(g:, 'airline#themes#base16#constant', 0) let g:airline#themes#base16#palette = {} " Color palette @@ -60,57 +60,48 @@ function! s:load_constant() let g:airline#themes#base16#palette.inactive_modified = { \ 'airline_c': [s:gui_orange, '', s:cterm_orange, '', ''], \ } -endfunction - -function! s:load_dynamic() - let g:airline#themes#base16#palette = {} - - let g:airline#themes#base16#palette.accents = { - \ 'red': airline#themes#get_highlight('Constant'), - \ } - - let s:N1 = airline#themes#get_highlight2(['DiffText', 'bg'], ['DiffText', 'fg'], 'bold') - let s:N2 = airline#themes#get_highlight('Visual') - let s:N3 = airline#themes#get_highlight('CursorLine') - let g:airline#themes#base16#palette.normal = airline#themes#generate_color_map(s:N1, s:N2, s:N3) - - let group = airline#themes#get_highlight('vimCommand') - let g:airline#themes#base16#palette.normal_modified = { - \ 'statusline': [ group[0], '', group[2], '', '' ] - \ } - - let s:I1 = airline#themes#get_highlight2(['DiffAdded', 'bg'], ['DiffAdded', 'fg'], 'bold') - let s:I2 = airline#themes#get_highlight2(['DiffAdded', 'fg'], ['Normal', 'bg']) - let s:I3 = s:N3 - let g:airline#themes#base16#palette.insert = airline#themes#generate_color_map(s:I1, s:I2, s:I3) - let g:airline#themes#base16#palette.insert_modified = g:airline#themes#base16#palette.normal_modified - - let s:R1 = airline#themes#get_highlight2(['WarningMsg', 'bg'], ['WarningMsg', 'fg'], 'bold') - let s:R2 = s:N2 - let s:R3 = s:N3 - let g:airline#themes#base16#palette.replace = airline#themes#generate_color_map(s:R1, s:R2, s:R3) - let g:airline#themes#base16#palette.replace_modified = g:airline#themes#base16#palette.normal_modified - - let s:V1 = airline#themes#get_highlight2(['Normal', 'bg'], ['Constant', 'fg'], 'bold') - let s:V2 = airline#themes#get_highlight2(['Constant', 'fg'], ['Normal', 'bg']) - let s:V3 = s:N3 - let g:airline#themes#base16#palette.visual = airline#themes#generate_color_map(s:V1, s:V2, s:V3) - let g:airline#themes#base16#palette.visual_modified = g:airline#themes#base16#palette.normal_modified - - let s:IA = airline#themes#get_highlight2(['NonText', 'fg'], ['CursorLine', 'bg']) - let g:airline#themes#base16#palette.inactive = airline#themes#generate_color_map(s:IA, s:IA, s:IA) - let g:airline#themes#base16#palette.inactive_modified = { - \ 'airline_c': [ group[0], '', group[2], '', '' ] - \ } -endfunction - -if get(g:, 'airline#themes#base16#constant', 0) - call s:load_constant() else - call s:load_dynamic() - augroup airline_base16 - autocmd! - autocmd ColorScheme * call load_dynamic() | call airline#load_theme() - augroup END + function! airline#themes#base16#refresh() + let g:airline#themes#base16#palette = {} + + let g:airline#themes#base16#palette.accents = { + \ 'red': airline#themes#get_highlight('Constant'), + \ } + + let s:N1 = airline#themes#get_highlight2(['DiffText', 'bg'], ['DiffText', 'fg'], 'bold') + let s:N2 = airline#themes#get_highlight('Visual') + let s:N3 = airline#themes#get_highlight('CursorLine') + let g:airline#themes#base16#palette.normal = airline#themes#generate_color_map(s:N1, s:N2, s:N3) + + let group = airline#themes#get_highlight('vimCommand') + let g:airline#themes#base16#palette.normal_modified = { + \ 'statusline': [ group[0], '', group[2], '', '' ] + \ } + + let s:I1 = airline#themes#get_highlight2(['DiffAdded', 'bg'], ['DiffAdded', 'fg'], 'bold') + let s:I2 = airline#themes#get_highlight2(['DiffAdded', 'fg'], ['Normal', 'bg']) + let s:I3 = s:N3 + let g:airline#themes#base16#palette.insert = airline#themes#generate_color_map(s:I1, s:I2, s:I3) + let g:airline#themes#base16#palette.insert_modified = g:airline#themes#base16#palette.normal_modified + + let s:R1 = airline#themes#get_highlight2(['WarningMsg', 'bg'], ['WarningMsg', 'fg'], 'bold') + let s:R2 = s:N2 + let s:R3 = s:N3 + let g:airline#themes#base16#palette.replace = airline#themes#generate_color_map(s:R1, s:R2, s:R3) + let g:airline#themes#base16#palette.replace_modified = g:airline#themes#base16#palette.normal_modified + + let s:V1 = airline#themes#get_highlight2(['Normal', 'bg'], ['Constant', 'fg'], 'bold') + let s:V2 = airline#themes#get_highlight2(['Constant', 'fg'], ['Normal', 'bg']) + let s:V3 = s:N3 + let g:airline#themes#base16#palette.visual = airline#themes#generate_color_map(s:V1, s:V2, s:V3) + let g:airline#themes#base16#palette.visual_modified = g:airline#themes#base16#palette.normal_modified + + let s:IA = airline#themes#get_highlight2(['NonText', 'fg'], ['CursorLine', 'bg']) + let g:airline#themes#base16#palette.inactive = airline#themes#generate_color_map(s:IA, s:IA, s:IA) + let g:airline#themes#base16#palette.inactive_modified = { + \ 'airline_c': [ group[0], '', group[2], '', '' ] + \ } + endfunction + call airline#themes#base16#refresh() endif diff --git a/autoload/airline/themes/jellybeans.vim b/autoload/airline/themes/jellybeans.vim index 332abf3..201068c 100644 --- a/autoload/airline/themes/jellybeans.vim +++ b/autoload/airline/themes/jellybeans.vim @@ -1,6 +1,7 @@ let g:airline#themes#jellybeans#palette = {} -function! s:generate() +" The name of the function must be 'refresh'. +function! airline#themes#jellybeans#refresh() " This theme is an example of how to use helper functions to extract highlight " values from the corresponding colorscheme. It was written in a hurry, so it " is very minimalistic. If you are a jellybeans user and want to make updates, @@ -47,8 +48,5 @@ function! s:generate() let g:airline#themes#jellybeans#palette.inactive_modified = g:airline#themes#jellybeans#palette.normal_modified endfunction -call s:generate() -augroup airline_jellybeans - autocmd! - autocmd ColorScheme * call generate() -augroup END +call airline#themes#jellybeans#refresh() + diff --git a/autoload/airline/themes/monochrome.vim b/autoload/airline/themes/monochrome.vim index 85c2c9a..7dd1a17 100644 --- a/autoload/airline/themes/monochrome.vim +++ b/autoload/airline/themes/monochrome.vim @@ -1,6 +1,6 @@ let g:airline#themes#monochrome#palette = {} -function! s:load() +function! airline#themes#monochrome#refresh() let s:SL = airline#themes#get_highlight('StatusLine') let g:airline#themes#monochrome#palette.normal = airline#themes#generate_color_map(s:SL, s:SL, s:SL) let g:airline#themes#monochrome#palette.insert = g:airline#themes#monochrome#palette.normal @@ -11,8 +11,5 @@ function! s:load() let g:airline#themes#monochrome#palette.inactive = airline#themes#generate_color_map(s:SLNC, s:SLNC, s:SLNC) endfunction -call s:load() -augroup airline_monochrome - autocmd! - autocmd ColorScheme * call load() -augroup END +call airline#themes#monochrome#refresh() + diff --git a/autoload/airline/themes/tomorrow.vim b/autoload/airline/themes/tomorrow.vim index 1df7805..f382fc1 100644 --- a/autoload/airline/themes/tomorrow.vim +++ b/autoload/airline/themes/tomorrow.vim @@ -1,6 +1,6 @@ let g:airline#themes#tomorrow#palette = {} -function! s:generate() +function! airline#themes#tomorrow#refresh() let g:airline#themes#tomorrow#palette.accents = { \ 'red': airline#themes#get_highlight('Constant'), \ } @@ -40,8 +40,5 @@ function! s:generate() \ } endfunction -call s:generate() -augroup airline_tomorrow - autocmd! - autocmd ColorScheme * call generate() | call airline#load_theme() -augroup END +call airline#themes#tomorrow#refresh() + diff --git a/autoload/airline/themes/zenburn.vim b/autoload/airline/themes/zenburn.vim index 311d033..9883c21 100644 --- a/autoload/airline/themes/zenburn.vim +++ b/autoload/airline/themes/zenburn.vim @@ -1,6 +1,6 @@ let g:airline#themes#zenburn#palette = {} -function! s:generate() +function! airline#themes#zenburn#refresh() let g:airline#themes#zenburn#palette.accents = { \ 'red': airline#themes#get_highlight('Constant'), \ } @@ -40,9 +40,5 @@ function! s:generate() \ } endfunction -call s:generate() -augroup airline_zenburn - autocmd! - autocmd ColorScheme * call generate() -augroup END +call airline#themes#zenburn#refresh()