diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2018-08-04 20:20:41 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2018-08-04 20:20:41 +1200 |
commit | c4501004740d1a95fd7bf61aa4f19ff19a10eaec (patch) | |
tree | 24c7841f7bf1634548de9d5cec47b6441d10ce4f | |
parent | Correct comments on Vim maps to show mappings (diff) | |
download | dotfiles-c4501004740d1a95fd7bf61aa4f19ff19a10eaec.tar.gz dotfiles-c4501004740d1a95fd7bf61aa4f19ff19a10eaec.zip |
Add cursorline_current.vim plugin
Also turn on 'cursorline' option in .vimrc, and update sahara.vim
colorscheme, which has a nice subdued 'cursorline'.
m--------- | vim/bundle/sahara | 0 | ||||
-rw-r--r-- | vim/plugin/cursorline_current.vim | 77 | ||||
-rw-r--r-- | vim/vimrc | 5 |
3 files changed, 82 insertions, 0 deletions
diff --git a/vim/bundle/sahara b/vim/bundle/sahara -Subproject 6818d1555a9d733f33f805e91b7147cdfddde04 +Subproject 5b24c60103d7ee056b88f1c0cc799d6479d75ec diff --git a/vim/plugin/cursorline_current.vim b/vim/plugin/cursorline_current.vim new file mode 100644 index 00000000..1ebf5821 --- /dev/null +++ b/vim/plugin/cursorline_current.vim @@ -0,0 +1,77 @@ +" +" cursorline_current: If 'cursorline' is globally on, only enable it for the +" current window, and only when not in insert mode. Essentially, make +" 'cursorline' follow the actual normal-mode cursor as much as possible. +" +" Author: Tom Ryder <tom@sanctum.geek.nz> +" License: Same as Vim itself +" +if exists('g:loaded_cursorline_current') || &compatible + finish +endif +if !has('autocmd') || !has('windows') + finish +endif +let g:loaded_cursorline_current = 1 + +" Suspend 'cursorline' when a window is inactive or inserting +function! s:Suspend() abort + let w:cursorline_current_cache = &l:cursorline + setlocal nocursorline +endfunction + +" Restore 'cursorline' when a window is active and non-insert +function! s:Restore() abort + + " If we don't have a value for 'cursorline' from a previous s:Suspend(), use + " the global value as the default + if !exists('w:cursorline_current_cache') + let w:cursorline_current_cache = &g:cursorline + endif + + " Restore local value to the cached value and clear it + let &l:cursorline = w:cursorline_current_cache + unlet w:cursorline_current_cache + +endfunction + +" Call s:Suspend() on all windows besides the current one +function! s:Load() abort + + " Cache current window index + let l:wcur = winnr() + + " Iterate through all the windows and suspend all but the current one + for l:wnum in range(1, winnr('$')) + if l:wnum != l:wcur + execute l:wnum . 'windo call s:Suspend()' + endif + endfor + + " Return to the window in which we started + execute l:wcur . 'wincmd w' + +endfunction + +" Set up hooks for toggling 'cursorline' +augroup cursorline_current + autocmd! + + " Turn off 'cursorline' for other windows on load + if exists('##VimEnter') + autocmd VimEnter * call s:Load() + endif + + " Turn off 'cursorline' when leaving a window + if exists('##WinEnter') && exists('##WinLeave') + autocmd WinLeave * call s:Suspend() + autocmd WinEnter * call s:Restore() + endif + + " Turn off 'cursorline' when in insert mode + if exists('##InsertEnter') && exists('##InsertLeave') + autocmd InsertEnter * call s:Suspend() + autocmd InsertLeave * call s:Restore() + endif + +augroup END @@ -61,6 +61,11 @@ set comments= " Give me a prompt instead of just rejecting risky :write, :saveas set confirm +" Only turn on 'cursorline' if my colorscheme loaded +if exists('g:colors_name') && g:colors_name ==# 'sahara' + set cursorline +endif + " Try to keep swapfiles in one system-appropriate dir set directory^=~/.vim/cache/swap//,~/vimfiles/cache/swap// |