Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement :RandomColorScheme (mapped to <C-F8> by default)
  • Loading branch information
xolox committed Jun 19, 2014
1 parent 487260a commit 9c52c02
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 60 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -8,7 +8,7 @@ The colorscheme switcher plug-in for the [Vim text editor] [vim] makes it easy t

Unzip the most recent ZIP archives of the [vim-colorscheme-switcher] [dcs] and [vim-misc] [dms] plug-ins inside your Vim profile directory (usually this is `~/.vim` on UNIX and `%USERPROFILE%\vimfiles` on Windows), restart Vim and execute the command `:helptags ~/.vim/doc` (use `:helptags ~\vimfiles\doc` instead on Windows). Now try it out: Execute `:NextColorScheme` to switch to the next color scheme.

If you didn't change the plug-in's configuration you can now use the `F8` and `Shift-F8` keys to switch to the next/previous color scheme.
If you didn't change the plug-in's configuration you can now use the `F8` and `Shift-F8` keys to switch to the next/previous color scheme and `Control-F8` to switch to a random color scheme.

If you prefer you can also use [Pathogen] [pathogen], [Vundle] [vundle] or a similar tool to install & update the [vim-colorscheme-switcher] [github-colorscheme-switcher] and [vim-misc] [github-misc] plug-ins using a local clone of the git repository.

Expand All @@ -22,6 +22,10 @@ Switch to the next color scheme. After the last color scheme the cycle repeats f

Switch to the previous color scheme. After the first color scheme the cycle repeats from the last color scheme.

### The `:RandomColorScheme` command

Switch to a random color scheme. Because Vim doesn't actually expose random numbers the microseconds of the current time are used to improvise a source of randomness. It's nothing like real randomness but convincing enough for this plug-in :-).

## Options

The colorscheme switcher plug-in should work out of the box, but you can change the configuration defaults if you want to change how the plug-in works.
Expand Down Expand Up @@ -68,8 +72,8 @@ If you have questions, bug reports, suggestions, etc. the author can be contacte

## License

This software is licensed under the [MIT license] [mit].
© 2013 Peter Odding &lt;<peter@peterodding.com>&gt;.
This software is licensed under the [MIT license] [mit].
© 2014 Peter Odding &lt;<peter@peterodding.com>&gt;.


[bg]: http://vimdoc.sourceforge.net/htmldoc/options.html#'background'
Expand Down
106 changes: 54 additions & 52 deletions autoload/xolox/colorscheme_switcher.vim
@@ -1,9 +1,9 @@
" Vim plug-in
" Maintainer: Peter Odding <peter@peterodding.com>
" Last Change: August 19, 2013
" Last Change: June 19, 2014
" URL: http://peterodding.com/code/vim/colorscheme-switcher

let g:xolox#colorscheme_switcher#version = '0.2.5'
let g:xolox#colorscheme_switcher#version = '0.3'

" Dictionary with previously seen links between highlighting groups.
if !exists('s:known_links')
Expand All @@ -20,57 +20,42 @@ function! xolox#colorscheme_switcher#previous() " {{{1
return xolox#colorscheme_switcher#cycle(0)
endfunction

function! xolox#colorscheme_switcher#random() " {{{1
" Switch to a random color scheme.
let choices = xolox#colorscheme_switcher#find_names()
if exists('g:colors_name')
call filter(choices, 'v:val != g:colors_name')
endif
let original_background = &background
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
call xolox#misc#msg#info('colorscheme-switcher.vim %s: Loaded random color scheme (%s)', g:xolox#colorscheme_switcher#version, choices[index])
return
endif
endfor
call xolox#misc#msg#debug('colorscheme-switcher.vim %s: Ran out of color schemes!', g:xolox#colorscheme_switcher#version)
endfunction

function! xolox#colorscheme_switcher#cycle(forward) " {{{1
" Switch to the next or previous color scheme.

" Get a sorted list with the available color schemes.
let list = xolox#colorscheme_switcher#find_names()

" Find the index of the currently loaded color scheme.
let index = exists('g:colors_name') ? index(list, g:colors_name) : 0

" Find the color scheme that should be loaded next.
let old_bg = &background
let prev_name = exists('g:colors_name') ? g:colors_name : ''
while 1

" Get the index of the next/previous color scheme.
let choices = xolox#colorscheme_switcher#find_names()
let index = exists('g:colors_name') ? index(choices, g:colors_name) : 0
let original_background = &background
for i in range(len(choices))
if a:forward
let index = (index + 1) % len(list)
let index = (index + 1) % len(choices)
else
let index = (index ? index : len(list)) - 1
let index = (index ? index : len(choices)) - 1
endif

" Find and remember links between highlighting groups before switching to
" the next or previous color scheme.
call xolox#colorscheme_switcher#find_links()

" Load the selected color scheme.
execute 'colorscheme' fnameescape(list[index])

" For highlighting groups that used to be linked and are now "cleared",
" we'll restore the link to the last known target group.
call xolox#colorscheme_switcher#restore_links()

" Stop searching when we've found the right color scheme.
if !g:colorscheme_switcher_keep_background || &background == old_bg
break
call xolox#colorscheme_switcher#switch_to(choices[index])
if !g:colorscheme_switcher_keep_background || &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

let prev_name = list[index]

endwhile

" 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 = list[index]

" Enable the user to customize the loaded color scheme.
silent execute 'doautocmd ColorScheme' fnameescape(list[index])

" Print the name of the loaded color scheme?
call xolox#misc#msg#info('colorscheme-switcher.vim %s: Loaded color scheme %s (%i/%i)', g:xolox#colorscheme_switcher#version, list[index], index, len(list))

endfor
call xolox#misc#msg#debug('colorscheme-switcher.vim %s: Ran out of color schemes!', g:xolox#colorscheme_switcher#version)
endfunction

function! xolox#colorscheme_switcher#find_names() " {{{1
Expand All @@ -87,7 +72,7 @@ endfunction

function! xolox#colorscheme_switcher#find_links() " {{{1
" Find and remember links between highlighting groups.
call xolox#misc#msg#info('colorscheme-switcher.vim %s: Using :highlight command to discover links between highlighting groups ..', g:xolox#colorscheme_switcher#version)
call xolox#misc#msg#debug('colorscheme-switcher.vim %s: Using :highlight command to discover links between highlighting groups ..', g:xolox#colorscheme_switcher#version)
redir => listing
try
silent highlight
Expand All @@ -104,7 +89,7 @@ function! xolox#colorscheme_switcher#find_links() " {{{1
let s:known_links[fromgroup] = togroup
endif
endfor
call xolox#misc#msg#info('colorscheme-switcher.vim %s: Found %i links between highlighting groups in output of :highlight command.', g:xolox#colorscheme_switcher#version, len(s:known_links))
call xolox#misc#msg#debug('colorscheme-switcher.vim %s: Found %i links between highlighting groups in output of :highlight command.', g:xolox#colorscheme_switcher#version, len(s:known_links))
endfunction

function! xolox#colorscheme_switcher#restore_links() " {{{1
Expand All @@ -129,9 +114,26 @@ function! xolox#colorscheme_switcher#restore_links() " {{{1
endif
endif
endfor
if num_restored > 0
call xolox#misc#msg#info('colorscheme-switcher.vim %s: Restored %i links between highlighting groups.', g:xolox#colorscheme_switcher#version, num_restored)
endif
call xolox#misc#msg#debug('colorscheme-switcher.vim %s: Restored %i links between highlighting groups.', g:xolox#colorscheme_switcher#version, num_restored)
endfunction

function! xolox#colorscheme_switcher#switch_to(name) " {{{1
" Switch to the given color scheme.
call xolox#colorscheme_switcher#find_links()
execute 'colorscheme' fnameescape(a:name)
call xolox#colorscheme_switcher#restore_links()
" 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
" Enable the user to customize the loaded color scheme.
silent execute 'doautocmd ColorScheme' fnameescape(a:name)
endfunction

function! xolox#colorscheme_switcher#random_number(limit) " {{{1
" Generate a `random' number (for some definition of the word).
let components = split(reltimestr(reltime()), '\.')
let microseconds = components[-1] + 0
return microseconds % a:limit
endfunction

" vim: ts=2 sw=2 et fdm=marker
14 changes: 12 additions & 2 deletions doc/colorscheme-switcher.txt
Expand Up @@ -8,6 +8,7 @@ Contents ~
3. Commands |colorscheme-switcher-commands|
1. The |:NextColorScheme| command
2. The |:PrevColorScheme| command
3. The |:RandomColorScheme| command
4. Options |colorscheme-switcher-options|
1. The |g:colorscheme_switcher_define_mappings| option
2. The |g:colorscheme_switcher_keep_background| option
Expand Down Expand Up @@ -40,7 +41,8 @@ Windows). Now try it out: Execute |:NextColorScheme| to switch to the next
color scheme.

If you didn't change the plug-in's configuration you can now use the 'F8' and
'Shift-F8' keys to switch to the next/previous color scheme.
'Shift-F8' keys to switch to the next/previous color scheme and 'Control-F8' to
switch to a random color scheme.

If you prefer you can also use Pathogen [3], Vundle [4] or a similar tool to
install & update the vim-colorscheme-switcher [5] and vim-misc [6] plug-ins
Expand All @@ -62,6 +64,14 @@ The *:PrevColorScheme* command
Switch to the previous color scheme. After the first color scheme the cycle
repeats from the last color scheme.

-------------------------------------------------------------------------------
The *:RandomColorScheme* command

Switch to a random color scheme. Because Vim doesn't actually expose random
numbers the microseconds of the current time are used to improvise a source of
randomness. It's nothing like real randomness but convincing enough for this
plug-in :-).

===============================================================================
*colorscheme-switcher-options*
Options ~
Expand Down Expand Up @@ -151,7 +161,7 @@ please vote for it on Vim Online [9].
*colorscheme-switcher-license*
License ~

This software is licensed under the MIT license [10]. Š 2013 Peter Odding
This software is licensed under the MIT license [10]. Š 2014 Peter Odding
<peter@peterodding.com>.

===============================================================================
Expand Down
9 changes: 6 additions & 3 deletions plugin/colorscheme-switcher.vim
@@ -1,6 +1,6 @@
" Vim plug-in
" Maintainer: Peter Odding <peter@peterodding.com>
" Last Change: August 19, 2013
" Last Change: June 19, 2014
" URL: http://peterodding.com/code/vim/colorscheme-switcher

" This Vim plug-in defines two commands and four key mappings to quickly
Expand Down Expand Up @@ -48,10 +48,13 @@ if g:colorscheme_switcher_define_mappings
nnoremap <silent> <F8> :NextColorScheme<CR>
inoremap <silent> <S-F8> <C-O>:PrevColorScheme<CR>
nnoremap <silent> <S-F8> :PrevColorScheme<CR>
inoremap <silent> <C-F8> <C-O>:RandomColorScheme<CR>
nnoremap <silent> <C-F8> :RandomColorScheme<CR>
endif

command! -bar -bang NextColorScheme call xolox#colorscheme_switcher#next()
command! -bar -bang PrevColorScheme call xolox#colorscheme_switcher#previous()
command! -bar NextColorScheme call xolox#colorscheme_switcher#next()
command! -bar PrevColorScheme call xolox#colorscheme_switcher#previous()
command! -bar RandomColorScheme call xolox#colorscheme_switcher#random()

" Don't reload the plug-in once it has loaded successfully.
let g:loaded_colorscheme_switcher = 1
Expand Down

0 comments on commit 9c52c02

Please sign in to comment.