From f38747fe2912bfaae120512dd79eb528a3f46e99 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 19 Jun 2019 02:39:16 +1200 Subject: Complete overhaul for new major release Fixes -o, -O, and -S related issues. --- README.md | 11 ++++-- doc/cursorline_current.txt | 36 ++++++++++++++--- plugin/cursorline_current.vim | 89 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 110 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 24c2b35..3edd2d9 100644 --- a/README.md +++ b/README.md @@ -1,9 +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. +This plugin tweaks the behaviour of the `'cursorline'` and `'cursorcolumn'` +options to enable them only in the current window, when not in insert mode, +and/or when Vim has focus. + +In its default configuration, it essentially makes `'cursorline'` and +`'cursorcolumn'` follow the cursor around in normal mode as much as possible. +It uses each window's global value of both options as its default, so setting +each option in your `vimrc` before the plugin loads should do the trick. License ------- diff --git a/doc/cursorline_current.txt b/doc/cursorline_current.txt index 6a54bb6..4984fec 100644 --- a/doc/cursorline_current.txt +++ b/doc/cursorline_current.txt @@ -1,16 +1,42 @@ -*cursorline_current.txt* For Vim version 7.0 Last change: 2019 Jun 13 +*cursorline_current.txt* For Vim version 7.0 Last change: 2019 Jun 19 DESCRIPTION *cursorline_current* -This plugin tweaks the behaviour of the 'cursorline' option to enable it only -in the current window, when not in insert mode, and (if supported) when Vim -has focus. It essentially makes 'cursorline' follow the cursor in normal mode -as much as possible. It uses the global value of 'cursorline' as its default. +This plugin tweaks the behaviour of the 'cursorline' and 'cursorcolumn' +options to enable them only in the current window, when not in insert mode, +and/or when Vim has focus. + +In its default configuration, it essentially makes 'cursorline' and +'cursorcolumn' follow the cursor around in normal mode as much as possible. +It uses each window's global value of both options as its default, so setting +each option in your |vimrc| before the plugin loads should do the trick. REQUIREMENTS *cursorline_current-requirements* This plugin only loads if 'compatible' is not set. +OPTIONS *cursorline_current-options* + + *g:cursorline_current_line* +Set `g:cursorline_current_line` to 0 in your |vimrc| if you don't want +'cursorline' to follow the current window around, and instead prefer each +window to track its own state. This option defaults to 1. + + *g:cursorcolumn_current_column* +Set `g:cursorline_current_column` to 0 in your |vimrc| if you don't want +'cursorcolumn' to follow the current window around, and instead prefer each +window to track its own state. This option defaults to 1. + + *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. + + *g:cursorline_current_focus* +Set `g:cursorline_current_focus` to 0 in your |vimrc| if you don't like the +cursor line switching off when Vim loses focus, which probably only works in +the GUI. This option defaults to 1. + AUTHOR *cursorline_current-author* Written and maintained by Tom Ryder . diff --git a/plugin/cursorline_current.vim b/plugin/cursorline_current.vim index 27cc1a0..64278cd 100644 --- a/plugin/cursorline_current.vim +++ b/plugin/cursorline_current.vim @@ -1,6 +1,8 @@ " -" cursorline_current.vim: Apply 'cursorline' only in normal mode in the active -" window. +" cursorline_current.vim: Set 'cursorline' and/or 'cursorcolumn' only in the +" current window, when not in insert mode, and/or when Vim has focus, with +" each of those being configurable with global variables that are checked at +" plugin load time. " " Author: Tom Ryder " License: Same as Vim itself @@ -13,27 +15,78 @@ let loaded_cursorline_current = 1 augroup cursorline_current autocmd! - " On opening Vim, we might have to get initial windows into the right state. - " Run the hook for leaving windows on every window, and then move back to - " the first window and run the hook for entering a window. + " If g:cursorline_current_line is true at plugin load time (defaults to on + " if unset), set 'cursorline' to the user-configured window-global value on + " entering a window, and unset it on leaving. + " + if get(g:, 'cursorline_current_line', 1) + autocmd WinEnter * + \ setlocal cursorline< + autocmd WinLeave * + \ setlocal nocursorline + endif + + " Do the same for 'cursorcolumn' if g:cursorline_current_column is true at + " load time, which again defaults to being on if unset. + " + if get(g:, 'cursorline_current_column', 1) + autocmd WinEnter * + \ setlocal cursorcolumn< + autocmd WinLeave * + \ setlocal nocursorcolumn + endif + + " If g:cursorline_current_insert is set at plugin load time (defaults to on + " if unset), also blank 'cursorline' even in the current window while in + " insert mode. Note that CTRL-C's default behaviour breaks this. + " + if get(g:, 'cursorline_current_insert', 1) + autocmd InsertEnter * + \ doautocmd cursorline_current WinLeave + autocmd InsertLeave * + \ doautocmd cursorline_current WinEnter + endif + + " If g:cursorline_current_focus is set at plugin load time (defaults to on + " if unset), also blank 'cursorline' even in the current window if Vim loses + " focus. This probably only works in the GUI. + " + if get(g:, 'cursorline_current_focus', 1) + autocmd FocusGained * + \ doautocmd cursorline_current WinEnter + autocmd FocusLost * + \ doautocmd cursorline_current WinLeave + endif + + " Stack up BufEnter and BufLeave events to trigger the corresponding window + " events, too; although this often means we set or unset the same option + " twice, it correctly handles entering a new window for a buffer that had + " already loaded. + " + autocmd BufEnter * + \ doautocmd cursorline_current WinEnter + autocmd BufLeave * + \ doautocmd cursorline_current WinLeave + + " When Vim starts up, go through all of the windows in all of the tabs and + " trigger the leave hooks to set 'cursorline' and/or 'cursorcolumn' locally + " off, and then return to the first window of the first tab, and trigger the + " enter hook to restore it to its user-configured window-global value again. + " This is intended to correctly handle -o and -O options from the command + " line, or vimrc files or plugins that open their own windows on Vim + " startup. " autocmd VimEnter * - \ tabdo windo doautocmd WinLeave + \ tabdo windo doautocmd cursorline_current WinLeave autocmd VimEnter * \ tabfirst | 1 wincmd w | doautocmd WinEnter - " On entering a buffer, the Vim application gaining focus, leaving insert - " mode, or entering a window, set the local value of the 'cursorline' option - " to the same as the global value, to correspond with an active state. - " - autocmd BufEnter,FocusGained,InsertLeave,WinEnter * - \ setlocal cursorline< - - " On leaving a buffer, the Vim application window losing focus, entering - " insert mode, or leaving a window, turn off the 'cursorline' option for the - " linked window, so that if it's on, it will only be in the active one. + " If we just loaded a session, however, prevent those VimEnter hooks from + " running by deleting them. The session should have recorded values for + " both options in each of the windows it saved, so we don't need to (and + " shouldn't) mess with them. " - autocmd BufLeave,FocusGained,InsertEnter,WinLeave * - \ setlocal nocursorline + autocmd SessionLoadPost * + \ autocmd! cursorline_current VimEnter augroup END -- cgit v1.2.3