aboutsummaryrefslogtreecommitdiffstats
path: root/plugin/nohai.vim
blob: 3129ad01b6797d4c956bbe010387857156cb2700 (plain)
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
" Remaps <CR> in command mode to run `nohlsearch` after accepting the command.
function! s:AddMapping()
	silent! cnoremap <expr> <CR> <SID>CRAndNohlsearch()
endfunction

" Removes the Nohai <CR> cmap.
function! s:RemoveMapping()
	silent! cunmap <CR>
endfunction

" Deactivates the Nohai <CR> cmap and turns off the augroup.
function! s:Deactivate()
	call s:RemoveMapping()
	call s:AutocmdOff()
endfunction

" Expr mapping function that runs the `nohlsearch` command after pressing <CR>.
function! s:CRAndNohlsearch()
	return "\<CR>:nohlsearch\<CR>"
endfunction

" Turn on Nohai autocmds.
function! s:AutocmdOn()
	augroup nohai
		autocmd!

		autocmd CmdlineEnter [/\?] call s:AddMapping()
		autocmd CmdlineLeave [/\?] call s:Deactivate()
	augroup END
endfunction

" Removes the Nohai augroup.
function! s:AutocmdOff()
	autocmd! nohai
endfunction

" Expr mapping function that turns on Nohai and starts the search command
" specified by `command`, either '/' or '?'.
function! s:Search(command)
	call s:AutocmdOn()

	return a:command
endfunction

nnoremap <expr> <Plug>(nohai-search-backward) <SID>Search('?')
nnoremap <expr> <Plug>(nohai-search) <SID>Search('/')

nmap g? <Plug>(nohai-search-backward)
nmap g/ <Plug>(nohai-search)