From 16668b84818c21f6e00ef328a2ab79c25672eab8 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 30 May 2019 20:49:01 +1200 Subject: Factor out functions into autoload This isn't actually very helpful at the moment, because the autoload file gets included on VimEnter. I'm hoping that I can defer that in most circumstances. --- autoload/cursorline_current.vim | 31 ++++++++++++++++++++++++++++++ plugin/cursorline_current.vim | 42 +++++------------------------------------ 2 files changed, 36 insertions(+), 37 deletions(-) create mode 100644 autoload/cursorline_current.vim diff --git a/autoload/cursorline_current.vim b/autoload/cursorline_current.vim new file mode 100644 index 0000000..36bb210 --- /dev/null +++ b/autoload/cursorline_current.vim @@ -0,0 +1,31 @@ +" Suspend 'cursorline' when a window is inactive or inserting +function! cursorline_current#Suspend() abort + let w:cursorline_current = &l:cursorline + let &l:cursorline = 0 +endfunction + +" Restore 'cursorline' when a window is active and non-insert +function! cursorline_current#Restore() abort + let &l:cursorline = get(w:, 'cursorline_current', &g:cursorline) + let w:cursorline_current = &l:cursorline +endfunction + +" Call cursorline_current#Suspend() on all windows besides the current one +function! cursorline_current#Load() abort + + " Cache current window index + let wcur = winnr() + + " Iterate through all the windows and suspend all but the current one + for wnum in range(1, winnr('$')) + if wnum == wcur + continue + endif + execute wnum . 'wincmd w' + call cursorline_current#Suspend() + endfor + + " Return to the window in which we started + execute wcur . 'wincmd w' + +endfunction diff --git a/plugin/cursorline_current.vim b/plugin/cursorline_current.vim index 9cdb25d..234450b 100644 --- a/plugin/cursorline_current.vim +++ b/plugin/cursorline_current.vim @@ -11,56 +11,24 @@ if exists('loaded_cursorline_current') || &compatible || v:version < 700 endif let loaded_cursorline_current = 1 -" Suspend 'cursorline' when a window is inactive or inserting -function! s:Suspend() abort - let w:cursorline_current = &l:cursorline - let &l:cursorline = 0 -endfunction - -" Restore 'cursorline' when a window is active and non-insert -function! s:Restore() abort - let &l:cursorline = get(w:, 'cursorline_current', &g:cursorline) - let w:cursorline_current = &l:cursorline -endfunction - -" Call s:Suspend() on all windows besides the current one -function! s:Load() abort - - " Cache current window index - let wcur = winnr() - - " Iterate through all the windows and suspend all but the current one - for wnum in range(1, winnr('$')) - if wnum == wcur - continue - endif - execute wnum . 'wincmd w' - call s:Suspend() - endfor - - " Return to the window in which we started - execute wcur . 'wincmd w' - -endfunction - " Set up hooks for toggling 'cursorline' augroup cursorline_current autocmd! " Turn off 'cursorline' for other windows on load - autocmd VimEnter * call s:Load() + autocmd VimEnter * call cursorline_current#Load() " Turn off 'cursorline' when leaving a window or losing focus. We call the " restore again on BufEnter to handle existent local buffer values for the " option overwriting the window value (Vim bug?) - autocmd WinLeave,FocusLost * call s:Suspend() - autocmd BufEnter,WinEnter,FocusGained * call s:Restore() + autocmd WinLeave,FocusLost * call cursorline_current#Suspend() + autocmd BufEnter,WinEnter,FocusGained * call cursorline_current#Restore() " Turn off 'cursorline' when in insert mode " Check g:cursorline_current_insert, in case the user doesn't want it if get(g:, 'cursorline_current_insert', 1) - autocmd InsertEnter * call s:Suspend() - autocmd InsertLeave * call s:Restore() + autocmd InsertEnter * call cursorline_current#Suspend() + autocmd InsertLeave * call cursorline_current#Restore() endif augroup END -- cgit v1.2.3