aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-11-13 00:02:47 +1300
committerTom Ryder <tom@sanctum.geek.nz>2017-11-13 00:03:19 +1300
commit54caf02bd794e45ed5b4b181f1e314e924809b1e (patch)
treee3c97953e4d53c5f412d1a2a0b16829b176303e1
parent03919f1c03f89e17c90e398a7eaceabb21f1268c (diff)
downloaddotfiles-54caf02bd794e45ed5b4b181f1e314e924809b1e.tar.gz
Move 'hlsearch' insert-mode suspension into plugin
It's easily repackaged and it makes the configuration that much shorter, so I may as well. This version also correctly handles 'hlsearch' not being on in the first place.
-rw-r--r--vim/config/search.vim14
-rw-r--r--vim/doc/insert_suspend_hlsearch.txt28
-rw-r--r--vim/plugin/insert_suspend_hlsearch.vim48
3 files changed, 76 insertions, 14 deletions
diff --git a/vim/config/search.vim b/vim/config/search.vim
index 967146d4..fc861801 100644
--- a/vim/config/search.vim
+++ b/vim/config/search.vim
@@ -20,18 +20,4 @@ if has('extra_search')
\ <C-L>
\ :<C-U>nohlsearch<CR><C-L>
- " Clear search highlighting as soon as I enter insert mode, and restore it
- " once I leave it
- if has('autocmd') && v:version >= 701
- augroup dotfiles_highlight
- autocmd!
- autocmd InsertEnter
- \ *
- \ set nohlsearch
- autocmd InsertLeave
- \ *
- \ set hlsearch
- augroup END
- endif
-
endif
diff --git a/vim/doc/insert_suspend_hlsearch.txt b/vim/doc/insert_suspend_hlsearch.txt
new file mode 100644
index 00000000..edb51e38
--- /dev/null
+++ b/vim/doc/insert_suspend_hlsearch.txt
@@ -0,0 +1,28 @@
+*insert_suspend_hlsearch.txt* For Vim version 7.0 Last change: 2017 November 12
+
+DESCRIPTION *insert_suspend_hlsearch*
+
+This plugin quietly disables 'hlsearch' search highlighting if enabled when an
+insert operation is started, and puts it back once done, to avoid the
+distracting effect the highlighting can cause while writing.
+
+REQUIREMENTS *insert_suspend_hlsearch-requirements*
+
+This plugin is only available if 'compatible' is not set.
+
+AUTHOR *insert_suspend_hlsearch-author*
+
+Written and maintained by Tom Ryder <tom@sanctum.geek.nz>.
+
+LICENSE *insert_suspend_hlsearch-license*
+
+Licensed for distribution under the same terms as Vim itself (see |license|).
+
+DISTRIBUTION *insert_suspend_hlsearch-distribution*
+
+This plugin lives in Tom Ryder's "dotfiles" suite, and may eventually be spun
+off into a separate distribution as it solidifies and this documentation
+improves. See <https://sanctum.geek.nz/cgit/dotfiles.git/about/> for more
+information.
+
+ vim:tw=78:ts=8:ft=help:norl:
diff --git a/vim/plugin/insert_suspend_hlsearch.vim b/vim/plugin/insert_suspend_hlsearch.vim
new file mode 100644
index 00000000..378febc8
--- /dev/null
+++ b/vim/plugin/insert_suspend_hlsearch.vim
@@ -0,0 +1,48 @@
+"
+" insert_suspend_hlsearch.vim: If 'hlsearch' is enabled, switch it off when
+" the user starts an insert mode operation, and back on again when they're
+" done.
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if exists('g:loaded_insert_suspend_hlsearch') || &compatible
+ finish
+endif
+" InsertEnter isn't an autocmd event until 7.0
+if !has('autocmd') || v:version < 700
+ finish
+endif
+let g:loaded_insert_suspend_hlsearch = 1
+
+" When entering insert mode, copy the current value of the 'hlsearch' option
+" into a script variable; if it's enabled, suspend it
+function s:InsertEnter()
+ let s:hlsearch = &hlsearch
+ echo &hlsearch
+ if s:hlsearch
+ set nohlsearch
+ endif
+ return
+endfunction
+
+" When leaving insert mode, if 'hlsearch' was enabled when this operation
+" started, restore it
+function s:InsertLeave()
+ if s:hlsearch
+ set hlsearch
+ endif
+ return
+endfunction
+
+" Clear search highlighting as soon as I enter insert mode, and restore it
+" once I leave it
+augroup insert_suspend_hlsearch
+ autocmd!
+ autocmd InsertEnter
+ \ *
+ \ call <SID>InsertEnter()
+ autocmd InsertLeave
+ \ *
+ \ call <SID>InsertLeave()
+augroup END