Skip to content

Commit

Permalink
Enable usage from vimrc (improved robustness, see issue #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jun 25, 2014
1 parent 1c87ba8 commit 689c86b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
14 changes: 8 additions & 6 deletions autoload/xolox/colorscheme_switcher.vim
@@ -1,9 +1,9 @@
" Vim plug-in
" Maintainer: Peter Odding <peter@peterodding.com>
" Last Change: June 20, 2014
" Last Change: June 25, 2014
" URL: http://peterodding.com/code/vim/colorscheme-switcher

let g:xolox#colorscheme_switcher#version = '0.4'
let g:xolox#colorscheme_switcher#version = '0.4.1'

" Dictionary with previously seen links between highlighting groups.
if !exists('s:known_links')
Expand All @@ -30,7 +30,7 @@ function! xolox#colorscheme_switcher#random() " {{{1
for i in range(len(choices))
let index = xolox#colorscheme_switcher#random_number(len(choices))
call xolox#colorscheme_switcher#switch_to(choices[index])
if !g:colorscheme_switcher_keep_background || &background == original_background
if !xolox#misc#option#get('colorscheme_switcher_keep_background', 0) || &background == original_background
call xolox#misc#msg#info('colorscheme-switcher.vim %s: Loaded random color scheme (%s)', g:xolox#colorscheme_switcher#version, choices[index])
return
endif
Expand All @@ -50,7 +50,7 @@ function! xolox#colorscheme_switcher#cycle(forward) " {{{1
let index = (index ? index : len(choices)) - 1
endif
call xolox#colorscheme_switcher#switch_to(choices[index])
if !g:colorscheme_switcher_keep_background || &background == original_background
if !xolox#misc#option#get('colorscheme_switcher_keep_background', 0) || &background == original_background
call xolox#misc#msg#info('colorscheme-switcher.vim %s: Loaded color scheme %s (%i/%i)', g:xolox#colorscheme_switcher#version, choices[index], index, len(choices))
return
endif
Expand All @@ -63,7 +63,8 @@ function! xolox#colorscheme_switcher#find_names() " {{{1
let list = []
for fname in split(globpath(&runtimepath, 'colors/*.vim'), '\n')
let name = fnamemodify(fname, ':t:r')
if index(list, name) == -1 && index(g:colorscheme_switcher_exclude, name) == -1
let exclude = xolox#misc#option#get('colorscheme_switcher_exclude', [])
if index(list, name) == -1 && index(exclude, name) == -1
call add(list, name)
endif
endfor
Expand Down Expand Up @@ -120,7 +121,8 @@ endfunction
function! xolox#colorscheme_switcher#switch_to(name) " {{{1
" Switch to the given color scheme.
call xolox#colorscheme_switcher#find_links()
execute g:colorscheme_switcher_command fnameescape(a:name)
let command = xolox#misc#option#get('colorscheme_switcher_command', 'colorscheme')
execute command fnameescape(a:name)
" Set the global colors_name variable because some color scheme scripts fail
" to do so or use the wrong name (for example rainbow_autumn uses autumn).
let g:colors_name = a:name
Expand Down
33 changes: 4 additions & 29 deletions plugin/colorscheme-switcher.vim
@@ -1,10 +1,10 @@
" Vim plug-in
" Maintainer: Peter Odding <peter@peterodding.com>
" Last Change: June 20, 2014
" Last Change: June 25, 2014
" URL: http://peterodding.com/code/vim/colorscheme-switcher

" This Vim plug-in defines two commands and four key mappings to quickly
" switch between color schemes (with the same &background if so desired).
" This Vim plug-in defines commands and key mappings to quickly switch between
" color schemes (with the same &background if so desired).

if &cp || exists('g:loaded_colorscheme_switcher')
finish
Expand All @@ -23,32 +23,7 @@ catch
finish
endtry

" You can set this variable to 0 (false) in your vimrc script to disable the
" default mappings (F8 in insert and normal mode).
if !exists('g:colorscheme_switcher_define_mappings')
let g:colorscheme_switcher_define_mappings = 1
endif

" You can set this variable to 1 in your "vimrc" or interactively when you
" only want to switch between color schemes of the same &background color.
if !exists('g:colorscheme_switcher_keep_background')
let g:colorscheme_switcher_keep_background = 0
endif

" If you want to ignore specific color schemes you can set this variable to a
" list with the names of the excluded color schemes.
if !exists('g:colorscheme_switcher_exclude')
" Note: The following color scheme scripts breaks cycling through the
" color schemes because of their use of linked highlight groups!
let g:colorscheme_switcher_exclude = []
endif

" Change this variable to set the Vim command used to activate color schemes.
if !exists('g:colorscheme_switcher_command')
let g:colorscheme_switcher_command = 'colorscheme'
endif

if g:colorscheme_switcher_define_mappings
if xolox#misc#option#get('colorscheme_switcher_define_mappings', 1)
inoremap <silent> <F8> <C-O>:NextColorScheme<CR>
nnoremap <silent> <F8> :NextColorScheme<CR>
inoremap <silent> <S-F8> <C-O>:PrevColorScheme<CR>
Expand Down

0 comments on commit 689c86b

Please sign in to comment.