From c27128fc5a16d7da18f57b1056ce0fa6e19b4364 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Mon, 30 Sep 2024 11:42:58 +0200 Subject: [PATCH] vim:aucmd: Delay cword highlighting by 100ms This should lead to better performance while scrolling through a file. --- .config/vim/vimrc.d/80-autocommands.vim | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.config/vim/vimrc.d/80-autocommands.vim b/.config/vim/vimrc.d/80-autocommands.vim index 53b9626..91e815e 100644 --- a/.config/vim/vimrc.d/80-autocommands.vim +++ b/.config/vim/vimrc.d/80-autocommands.vim @@ -65,6 +65,21 @@ function! HighlightCurrentWord() return endif + " Delay the highlight by 100ms so that not every word is highlighted + " while moving the cursor fast. (This kind of simulates a CursorHold + " event with a custom time) + if exists('w:cword_timer_id') + " Abort the already running timer and its callback + call timer_stop(w:cword_timer_id) + call ClearHighlights(s:CLEAR_HIGHS_CWORD) + endif + let w:cword_timer_id = timer_start(100, "_HighlightCurrentWord") +endfunction + +function! _HighlightCurrentWord(timer_id) + " TODO: there is probably some kind of race condition here + unlet w:cword_timer_id + let l:cword = expand('') if exists('w:old_cword') && w:old_cword == l:cword " Do not delete and readd the match if on the same word