views:

203

answers:

2

I've just created my first VIM script, I wrote it in Python. It's a simple script to switch color schemes from a directory (/vim/etc/colors). I would like to know how to send a notification after the color scheme changed with the name of the selected color scheme to the vim 'statusline'.

rson gave an answer to my question, here is an updated (and debugged) version of the script for who is interested (works fine as far as I can test)

Implemented (kind of) the suggestions of AI and Caleb, thanks!:

" toggleColorScheme 0.9 (l) 2009 by Jasper Poppe <[email protected]>

" cycle through colorschemes with F8 and Shift+F8
nnoremap <silent><F8> :call ToggleColorScheme("1")<CR>
nnoremap <silent><s-F8> :call ToggleColorScheme("-1")<CR>

" set directory with color schemes to cycle through
let g:Toggle_Color_Scheme_Path = "/etc/vim/colors"


function! ToggleColorScheme(paramater)
python << endpython
import vim
import os

paramater = (vim.eval('a:paramater'))
scheme_path = vim.eval('g:Toggle_Color_Scheme_Path')

colorschemes = [color.split('.')[0] for color in os.listdir(scheme_path) if color.endswith('.vim')]
colorschemes.sort()

if not vars().has_key('position'):
    start_scheme = vim.eval('g:colors_name') + '.vim'
    if start_scheme in colorschemes:
        position = colorschemes.index(start_scheme)
    else:
        position = 0

position += int(paramater)
position %= len(colorschemes)

vim.command('colorscheme %s' % colorschemes[position])
vim.command('redraw | echo "%s"' % colorschemes[position])
vim.command('return 1')
endpython
endfunction
+3  A: 

vim.command('redraw | echo "%s"' % colorschemes[position])

From :help echo:

A later redraw may make the message disappear again. And since Vim mostly postpones redrawing until it's finished with a sequence of commands this happens quite often. To avoid that a command from before the ":echo" causes a redraw afterwards (redraws are often postponed until you type something), force a redraw with the |:redraw| command. Example:

:new | redraw | echo "there is a new window"

Randy Morris
Thanks a lot, that works perfectly!
Dwaalspoor98
A: 

Since you are updating the script here,

Instead of

if argument == 'next':
    position += 1
    if position == len(colorschemes) - 1:
        position = 0
elif argument == 'prev':
    position -= 1
    if position == -1:
        position = len(colorschemes) - 1

Perhaps

scroll['next'] = +1
scroll['prev'] = -1
position += scroll[argument]
position = position % len(colorschemes)
cjrh
Or even: `position %= len(colorschemes)`
Al
@Al Nice. I didn't realize that applied to mod too.
cjrh
@AI and Caleb: Since it was my first VIM script ever I was really happy it just worked, I will implement another fix (set t_Co=256 seems to get forgotten sometimes) Will improve the script with your suggestions later this week ;).
Dwaalspoor98
Updated the script, seems "set t_Co=256 fix" is not needed anymore :)
Dwaalspoor98