diff --git a/autoload/cursorword.vim b/autoload/cursorword.vim index 2359b2c..645b0e8 100644 --- a/autoload/cursorword.vim +++ b/autoload/cursorword.vim @@ -13,6 +13,14 @@ function! cursorword#highlight() abort highlight default CursorWord term=underline cterm=underline gui=underline endfunction +function! cursorword#clear() abort + if get(w:, 'cursorword_match') + silent! call matchdelete(w:cursorword_id) + let w:cursorword_match = 0 + let w:cursorword_state = [] + endif +endfunction + let s:alphabets = '^[\x00-\x7f\xb5\xc0-\xd6\xd8-\xf6\xf8-\u01bf\u01c4-\u02af\u0370-\u0373\u0376\u0377\u0386-\u0481\u048a-\u052f]\+$' function! cursorword#matchadd(...) abort @@ -37,11 +45,7 @@ let s:delay = get(g:, 'cursorword_delay', 50) if has('timers') && s:delay > 0 let s:timer = 0 function! cursorword#cursormoved() abort - if get(w:, 'cursorword_match') - silent! call matchdelete(w:cursorword_id) - let w:cursorword_match = 0 - let w:cursorword_state = [] - endif + call cursorword#clear() call timer_stop(s:timer) let s:timer = timer_start(s:delay, 'cursorword#timer_callback') endfunction diff --git a/plugin/cursorword.vim b/plugin/cursorword.vim index 5081bd9..69b9e17 100644 --- a/plugin/cursorword.vim +++ b/plugin/cursorword.vim @@ -22,6 +22,10 @@ augroup cursorword call cursorword#highlight() autocmd WinEnter,BufEnter * call cursorword#matchadd() endif + autocmd WinLeave,BufLeave * + \ if get(g:, 'cursorword_clear_on_leave', 0) | + \ call cursorword#clear() | + \ endif autocmd ColorScheme * call cursorword#highlight() autocmd CursorMoved,CursorMovedI * call cursorword#cursormoved() autocmd InsertEnter * call cursorword#matchadd(1)