22if ! get (g: , ' eregex_incsearch_enable' , 1 )
33 \ || ! exists (' ##CmdlineChanged' )
44 \ || ! exists (' ##CmdlineLeave' )
5+ \ || ! has (' timers' )
56 finish
67endif
78
89augroup eregex_incsearch_augroup
910 autocmd !
10- autocmd CmdlineChanged * call s: onUpdate ()
11+ autocmd CmdlineChanged * call s: delayUpdate ()
1112 autocmd CmdlineLeave * call s: onLeave ()
1213augroup END
1314
@@ -19,6 +20,25 @@ if !has('nvim')
1920 cnoremap <expr> <cr> K_eregex_incsearch_abort_cr()
2021endif
2122
23+ " it's buggy if update immediately on #CmdlineChanged,
24+ " especially for keymap such as
25+ " nnoremap xxx :S/<c-r><c-w>
26+ function ! s: delayUpdate ()
27+ if get (s: , ' delayUpdateId' , -1 ) == -1
28+ let s: delayUpdateId = timer_start (10 , function (' s:delayUpdateAction' ))
29+ endif
30+ endfunction
31+ function ! s: delayUpdateCancel ()
32+ if get (s: , ' delayUpdateId' , -1 ) != -1
33+ call timer_stop (s: delayUpdateId )
34+ let s: delayUpdateId = -1
35+ endif
36+ endfunction
37+ function ! s: delayUpdateAction (... )
38+ let s: delayUpdateId = -1
39+ call s: onUpdate ()
40+ endfunction
41+
2242function ! s: onUpdate ()
2343 if getcmdtype () != ' :'
2444 \ || ! get (b: , ' eregex_incsearch' , get (g: , ' eregex_incsearch' , &incsearch ))
@@ -73,6 +93,8 @@ function! s:onUpdate()
7393endfunction
7494
7595function ! s: onLeave ()
96+ call s: delayUpdateCancel ()
97+
7698 if exists (' s:hlsearchSaved' )
7799 let hlsearchSaved = s: hlsearchSaved
78100 unlet s: hlsearchSaved
0 commit comments