diff --git a/CHANGELOG.md b/CHANGELOG.md index b2f146f..bd1a38c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ This is the Changelog for the vim-airline project. Set the `g:airline_statusline_ontop` to enable this experimental feature. - If `buffer_idx_mode=2`, up to 89 mappings will be exposed to access more buffers directly (issue #1823) + - Allow to use `random` as special theme name, which will switch to a random + airline theme (at least if a random number can be generated :() ## [0.10] - 2018-12-15 - New features diff --git a/autoload/airline/util.vim b/autoload/airline/util.vim index 925f06d..973cdfb 100644 --- a/autoload/airline/util.vim +++ b/autoload/airline/util.vim @@ -135,6 +135,6 @@ function! airline#util#doautocmd(event) endfunction function! airline#util#themes(match) - let files = split(globpath(&rtp, 'autoload/airline/themes/'.a:match.'*'), "\n") - return map(files, 'fnamemodify(v:val, ":t:r")') + let files = split(globpath(&rtp, 'autoload/airline/themes/'.a:match.'*.vim'), "\n") + return sort(map(files, 'fnamemodify(v:val, ":t:r")') + ['random']) endfunction diff --git a/doc/airline.txt b/doc/airline.txt index 521aec5..44dac5c 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -249,6 +249,7 @@ COMMANDS *airline-commands* :AirlineTheme {theme-name} *:AirlineTheme* Displays or changes the current theme. + Note: `random` will switch to a random theme. :AirlineToggleWhitespace *:AirlineToggleWhitespace* Toggles whitespace detection. diff --git a/plugin/airline.vim b/plugin/airline.vim index ae2170f..f3ffb3c 100644 --- a/plugin/airline.vim +++ b/plugin/airline.vim @@ -21,6 +21,9 @@ function! s:init() let s:theme_in_vimrc = exists('g:airline_theme') if s:theme_in_vimrc try + if g:airline_theme is# 'random' + let g:airline_theme=s:RandomTheme() + endif let palette = g:airline#themes#{g:airline_theme}#palette catch call airline#util#warning(printf('Could not resolve airline theme "%s". Themes have been migrated to github.com/vim-airline/vim-airline-themes.', g:airline_theme)) @@ -171,9 +174,16 @@ endfunction function! s:airline_theme(...) if a:0 try - call airline#switch_theme(a:1) + let theme = a:1 + if theme is# 'random' + let theme = s:RandomTheme() + endif + call airline#switch_theme(theme) catch " discard error endtry + if a:1 is# 'random' + echo g:airline_theme + endif else echo g:airline_theme endif @@ -215,6 +225,25 @@ function! s:airline_extensions() endfor endfunction +function! s:Rand(max) abort + if has("reltime") + let timerstr=reltimestr(reltime()) + let number=split(timerstr, '\.')[1]+0 + else + " best effort, bash and zsh provide $RANDOM + " cmd.exe on windows provides %random%, but expand() + " does not seem to be able to expand this correctly. + " In the worst case, this always returns zero + let number=expand("$RANDOM")+0 + endif + return number % a:max +endfunction + +function! s:RandomTheme() abort + let themes=airline#util#themes('') + return themes[s:Rand(len(themes))] +endfunction + command! -bar -nargs=? -complete=customlist,get_airline_themes AirlineTheme call airline_theme() command! -bar AirlineToggleWhitespace call airline#extensions#whitespace#toggle() command! -bar AirlineToggle call s:airline_toggle()