Allow to switch to a random theme

You can now specify the special theme name `random` and vim-airline will
load a random theme from the ones installed. This works from either your
.vimrc as well as when calling `:AirlineTheme` command directly.

closes vim-airline/vim-airline-themes#170
This commit is contained in:
Christian Brabandt 2019-03-26 21:03:13 +01:00
parent a6fb6d9da0
commit 148eb6bb28
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
4 changed files with 35 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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,<sid>get_airline_themes AirlineTheme call <sid>airline_theme(<f-args>)
command! -bar AirlineToggleWhitespace call airline#extensions#whitespace#toggle()
command! -bar AirlineToggle call s:airline_toggle()