forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhlsearch.vim
53 lines (44 loc) · 1.16 KB
/
hlsearch.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
" Disable hlsearch when you are done searching
" Credits: https://github.com/romainl/vim-cool
let s:save_cpo = &cpoptions
set cpoptions&vim
augroup hlsearch
autocmd!
" trigger when hlsearch is toggled
autocmd OptionSet hlsearch call <SID>toggle(v:option_old, v:option_new)
augroup END
function! s:StartHL()
silent! if v:hlsearch && !search('\%#\zs'.@/,'cnW')
call <SID>StopHL()
endif
endfunction
function! s:StopHL()
if ! v:hlsearch || mode() !=? 'n'
return
else
silent call feedkeys("\<Plug>(StopHL)", 'm')
endif
endfunction
function! s:toggle(old, new)
if a:old == 0 && a:new == 1
" nohls --> hls
" set up
noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
autocmd hlsearch CursorMoved * call <SID>StartHL()
autocmd hlsearch InsertEnter * call <SID>StopHL()
elseif a:old == 1 && a:new == 0
" hls --> nohls
" tear down
nunmap <expr> <Plug>(StopHL)
unmap! <expr> <Plug>(StopHL)
autocmd! hlsearch CursorMoved
autocmd! hlsearch InsertEnter
else
" nohls --> nohls
" do nothing
return
endif
endfunction
call <SID>toggle(0, &hlsearch)
let &cpoptions = s:save_cpo