Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transient hlsearch #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ivanbrennan
Copy link

@ivanbrennan ivanbrennan commented Jul 9, 2017

Why

It would be nice to be able to make 'hlsearch' automatically dissipate after a configured timeout period, leaving only the match under the cursor highlighted.

Demo

asciicast

How

This feature is off by default and can be activated by setting g:LoupeHlSearchTimeout to a positive number (of milliseconds) in a version of Vim (or Neovim) that supports timers.

The autocommand and mappings that currently call loupe#hlmatch (highlighting the match under the cursor) will also call loupe#hlsearch, which sets 'hlsearch' and schedules a timer to deactivate it. The callback that handles deactivation only does so for the most recent timer, so multiple n jumps, for example, extend the life of 'hlsearch'.

Notes

I initially tried to implement this using :nohlsearch rather than manipulating the 'hlsearch' user option, but Vim saves and restores the highlight state when calling and returning from user functions, so :nohlsearch was useless. Given the nature of this feature, manipulating 'hlsearch' seems pretty reasonable.

I suppose it might be nice to make the highlight under the cursor transient as well, but I haven't implemented that yet.

Let hlsearch automatically dissipate after a configured timeout period. This
feature is off by default and can be activated by setting g:LoupeHlSearchTimeout
to a positive number (of milliseconds) in a version of Vim (or Neovim) that
supports timers.

Events that call loupe#hlmatch (highlighting the match under the cursor)
will also call loupe#hlsearch, which schedules the deactivation of
hlsearch. The callback that actually turns off hlsearch will only
do so if called by the most recent timer, so multiple `n` jumps, for
example, effectively extend the life of hlsearch.

I had hoped to implement this using :nohlsearch rather than manipulating the
'hlsearch' user option, but Vim saves and restores the highlight state when
calling user functions, so :nohlsearch has no effect. Given the nature of this
feature, manipulating 'hlsearch' seems reasonable.
Scoping the hlsearch_timer to a window can lead to an error if you
change windows before the timer finishes. The callback
(loupe#private#clear_hlsearch) will run in a window that has no
w:hlsearch_timer defined,

  E121: Undefined variable: w:hlsearch_timer

or it might run in a window that has an outdated w:hlsearch_timer value,
and hlsearch will never be cleared.

Since 'hlsearch' is a global option anyway, the easy fix seems to be the
correct one: make hlsearch_timer global as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant