vim:autocmd: Check if same cword when highlighting

When highlighting the current word, check if it is the same as the
currently highlighted one, so that matchdelete and matchadd are not
called unnecessarily.
This commit is contained in:
2022-12-06 15:31:53 +01:00
parent 73c0864c8b
commit 5ee98950eb

View File

@@ -53,11 +53,16 @@ augroup END
" Highlight word under cursor
function! HighlightCurrentWord()
if exists('w:old_cword') && w:old_cword == expand('<cword>')
" Do not delete and readd the match if on the same word
return
endif
if exists('w:cword_match_id')
call matchdelete(w:cword_match_id)
unlet w:cword_match_id
endif
if (expand('<cword>') != '')
let w:old_cword = expand('<cword>')
let w:cword_match_id = matchadd(
\ 'CursorColumn',
\ '\V\<' . escape(expand('<cword>'), '/\') . '\>',