diff --git a/autoload/airline.vim b/autoload/airline.vim index 5cea812b..9c9258e5 100644 --- a/autoload/airline.vim +++ b/autoload/airline.vim @@ -20,6 +20,23 @@ function! airline#switch_theme(name) call airline#check_mode() endfunction +function! airline#switch_matching_theme() + let v:errmsg = '' + silent! let palette = g:airline#themes#{g:colors_name}#palette + if empty(v:errmsg) + call airline#switch_theme(g:colors_name) + return 1 + else + for map in items(g:airline_theme_map) + if match(g:colors_name, map[0]) > -1 + call airline#switch_theme(map[1]) + return 1 + endif + endfor + endif + return 0 +endfunction + function! s:get_section(winnr, key, ...) let text = airline#util#getwinvar(a:winnr, 'airline_section_'.a:key, g:airline_section_{a:key}) let [prefix, suffix] = [get(a:000, 0, '%( '), get(a:000, 1, ' %)')] diff --git a/doc/airline.txt b/doc/airline.txt index b82d7732..df3f8fc9 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -61,8 +61,9 @@ values): only the filename of that buffer. > let g:airline_inactive_collapse=1 < -* change the default theme > - let g:airline_theme='dark' +* themes are automatically selected based on the matching colorscheme. this + can be overridden by defining a value. > + let g:airline_theme= < * enable/disable usage of patched powerline font symbols > let g:airline_powerline_fonts=0 diff --git a/plugin/airline.vim b/plugin/airline.vim index 5f8755d8..c6ca5d1b 100644 --- a/plugin/airline.vim +++ b/plugin/airline.vim @@ -31,7 +31,6 @@ call s:check_defined('g:airline_branch_prefix', exists('g:airline_powerline_font call s:check_defined('g:airline_readonly_symbol', exists('g:airline_powerline_fonts')?'':'RO') call s:check_defined('g:airline_linecolumn_prefix', exists('g:airline_powerline_fonts')?' ':':') call s:check_defined('g:airline_paste_symbol', (exists('g:airline_powerline_fonts') ? ' ' : '').'PASTE') -call s:check_defined('g:airline_theme', 'dark') call s:check_defined('g:airline_inactive_collapse', 1) call s:check_defined('g:airline_exclude_filenames', ['DebuggerWatch','DebuggerStack','DebuggerStatus']) call s:check_defined('g:airline_exclude_filetypes', []) @@ -53,6 +52,12 @@ call s:check_defined('g:airline_mode_map', { \ '' : 'S-BLOCK', \ }) +call s:check_defined('g:airline_theme_map', { + \ 'Tomorrow.*': 'tomorrow', + \ 'mo[l|n]okai': 'molokai', + \ 'wombat.*': 'wombat', + \ }) + call s:check_defined('g:airline_section_a', '%{get(w:, "airline_current_mode", "")}') call s:check_defined('g:airline_section_b', '%{get(w:, "airline_current_branch", "")}') call s:check_defined('g:airline_section_c', '%f%m') @@ -63,15 +68,31 @@ call s:check_defined('g:airline_section_z', '%3p%% '.g:airline_linecolumn_prefix call s:check_defined('g:airline_section_warning', '') let s:airline_initialized = 0 +let s:airline_theme_defined = 0 function! s:on_window_changed() if !s:airline_initialized call airline#extensions#load() - call airline#switch_theme(g:airline_theme) + + let s:airline_theme_defined = exists('g:airline_theme') + let g:airline_theme = get(g:, 'airline_theme', 'dark') + call on_colorscheme_changed() + let s:airline_initialized = 1 endif call airline#update_statusline() endfunction +function! s:on_colorscheme_changed() + if !s:airline_theme_defined + if airline#switch_matching_theme() + return + endif + endif + + " couldn't find a match, or theme was defined, just refresh + call airline#load_theme() +endfunction + function airline#cmdwinenter() call airline#extensions#apply_left_override('Command Line', '') endfunction @@ -95,7 +116,7 @@ function! s:airline_toggle() \ | call on_window_changed() autocmd CmdwinLeave * call remove(g:airline_statusline_funcrefs, -1) - autocmd ColorScheme * call airline#load_theme() + autocmd ColorScheme * call on_colorscheme_changed() autocmd WinEnter,BufWinEnter,FileType,BufUnload,ShellCmdPost * \ call on_window_changed() augroup END