aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md14
-rw-r--r--VERSION1
-rw-r--r--doc/cursorline_current.txt32
-rw-r--r--plugin/cursorline_current.vim76
4 files changed, 123 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..839a5ee
--- /dev/null
+++ b/README.md
@@ -0,0 +1,14 @@
+cursorline\_current.vim
+=======================
+
+This plugin tweaks the behaviour of the `'cursorline'` option to enable it only
+in the current window and when not in insert mode. It essentially makes
+`'cursorline'` follow the cursor in normal mode as much as possible.
+
+License
+-------
+
+Copyright (c) [Tom Ryder][1]. Distributed under the same terms as Vim itself.
+See `:help license`.
+
+[1]: https://sanctum.geek.nz/
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..6e8bf73
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+0.1.0
diff --git a/doc/cursorline_current.txt b/doc/cursorline_current.txt
new file mode 100644
index 0000000..33f645e
--- /dev/null
+++ b/doc/cursorline_current.txt
@@ -0,0 +1,32 @@
+*cursorline_current.txt* For Vim version 7.0 Last change: 2018 Aug 4
+
+DESCRIPTION *cursorline_current*
+
+This plugin tweaks the behaviour of the 'cursorline' option to enable it only
+in the current window and when not in insert mode. It essentially makes
+'cursorline' follow the cursor in normal mode as much as possible.
+
+It uses the global value of 'cursorline' as its default, and should also
+correctly handle local values for windows.
+
+REQUIREMENTS *cursorline_current-requirements*
+
+This plugin only loads if 'compatible' is not set. It requires the |+autocmd|
+and |+windows| features.
+
+OPTIONS *cursorline_current-options*
+
+ *g:cursorline_current_insert*
+Set `g:cursorline_current_insert` to 0 in your |vimrc| if you don't like the
+cursor line switching off while you're in insert mode. This option defaults to
+1.
+
+AUTHOR *cursorline_current-author*
+
+Written and maintained by Tom Ryder <tom@sanctum.geek.nz>.
+
+LICENSE *cursorline_current-license*
+
+Licensed for distribution under the same terms as Vim itself (see |license|).
+
+ vim:tw=78:ts=8:ft=help:norl:
diff --git a/plugin/cursorline_current.vim b/plugin/cursorline_current.vim
new file mode 100644
index 0000000..e42e96d
--- /dev/null
+++ b/plugin/cursorline_current.vim
@@ -0,0 +1,76 @@
+"
+" 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') || v:version < 700
+ 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
+ continue
+ endif
+ execute l:wnum . 'wincmd w'
+ call s:Suspend()
+ 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
+ autocmd VimEnter * call s:Load()
+
+ " Turn off 'cursorline' when leaving a window
+ autocmd WinLeave * call s:Suspend()
+ autocmd WinEnter * call s: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()
+ endif
+
+augroup END