diff --git a/README.md b/README.md index 2a903b2..f513fdc 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,18 @@ Controls whether the match's line is vertically centered within the window when let g:LoupeCenterResults=0 ``` +

g:LoupeHlSearchTimeout

+### `g:LoupeHlSearchTimeout` (number, default: 0) + + " In milliseconds. + " To enable this feature, set to a number > 0: + +Controls how long (in milliseconds) hlsearch remains active after a search (`/`, `?`) is executed or a jump (`n`, `*` etc.) is performed. By default, there is no timeout. Setting this option to a positive number will enable the timeout if timers are supported (i.e. `has('timers')`). + +``` +let g:LoupeHlSearchTimeout=1000 +``` + ## Functions

loupe#hlmatch()

diff --git a/autoload/loupe.vim b/autoload/loupe.vim index 4c0a05a..0dbe762 100644 --- a/autoload/loupe.vim +++ b/autoload/loupe.vim @@ -41,3 +41,29 @@ function! loupe#hlmatch() abort let w:loupe_hlmatch=matchadd(l:highlight, l:pattern) endif endfunction + +"" +" @function loupe#hlsearch +" +" Make hlsearch transient. +" +function! loupe#hlsearch() abort + "" + " @option g:LoupeHlSearchTimeout number 0 + " How long after a search command before hlsearch is deactivated. + " In milliseconds. + " To enable this feature, set to a number > 0: + " + " ``` + " let g:LoupeHlSearchTimeout=1000 + " ``` + let l:time=get(g:, 'LoupeHlSearchTimeout', 0) + + if l:time > 0 && has('timers') + " activate hlsearch + set hlsearch + + " schedule hlsearch deactivation + let g:hlsearch_timer=timer_start(l:time, 'loupe#private#clear_hlsearch') + endif +endfunction diff --git a/autoload/loupe/private.vim b/autoload/loupe/private.vim index 245a341..754c7b1 100644 --- a/autoload/loupe/private.vim +++ b/autoload/loupe/private.vim @@ -70,7 +70,7 @@ function! loupe#private#prepare_highlight(result) abort if has('autocmd') augroup LoupeHightlightMatch autocmd! - autocmd CursorMoved * :call loupe#hlmatch() + autocmd CursorMoved * :call loupe#hlmatch() | :call loupe#hlsearch() augroup END endif return a:result @@ -89,6 +89,15 @@ function! loupe#private#clear_highlight() abort endif endfunction +" Deactivate hlsearch once the configured timeout has passed since the most +" recent search command. +function! loupe#private#clear_hlsearch(timer) abort + " only process the most recent timer + if a:timer == g:hlsearch_timer + set nohlsearch + endif +endfunction + " Called from WinEnter autocmd to clean up stray `matchadd()` vestiges. " If we switch into a window and there is no 'hlsearch' in effect but we do have " a `w:loupe_hlmatch` variable, it means that `:nohighight` was probably run diff --git a/doc/loupe.txt b/doc/loupe.txt index fd29e3b..8ad5e40 100644 --- a/doc/loupe.txt +++ b/doc/loupe.txt @@ -203,6 +203,17 @@ when jumping (via |n|, |N| etc). To disable, set to 0: > let g:LoupeCenterResults=0 < + + *g:LoupeHlSearchTimeout* +|g:LoupeHlSearchTimeout| number (default: 0) + +Controls how long (in milliseconds) hlsearch remains active after a search +(|/|, |?|) is executed or a jump (|n|, |star| etc.) is performed. By default, +there is no timeout. Setting this option to a positive number will enable the +timeout if timers are supported. +> + let g:LoupeHlSearchTimeout=1000 +< FUNCTIONS *loupe-functions* loupe#hlmatch() *loupe#hlmatch()* diff --git a/plugin/loupe.vim b/plugin/loupe.vim index ed00398..6172025 100644 --- a/plugin/loupe.vim +++ b/plugin/loupe.vim @@ -434,7 +434,8 @@ function! s:map(keys, name) \ a:keys . \ 'zv' . \ s:center_string . - \ ':call loupe#hlmatch()' + \ ':call loupe#hlmatch()' . + \ ':call loupe#hlsearch()' endfunction ""