junegunn/vim-slash

Autocmd listening to `CursorHold` disables highlighting even without moving cursor.

lacygoill opened this issue · 4 comments

Hello,

Consider this minimal vimrc (/tmp/vimrc):

set rtp+=~/.vim/plugged/vim-slash
augroup my_autocmd
    au!
    au CursorHold * exe ''
augroup END

If I start Vim like this:

vim -Nu /tmp/vimrc /tmp/vimrc

Then, search for the pattern au, the matches are correctly highlighted, but if I wait 4 seconds, it disappears, even without moving the cursor. 'hls' should stay enabled as long as the cursor doesn't move, but here, it's disabled after &updatetime ms. Here's a gif to illustrate.

The problem seems to come from the autocmd listening to CursorHold. I encountered this issue with 2 autocmds which automatically save the buffer if it's changed:

augroup autoSaveAndRead
    au!
    au CursorHold * sil! checktime
    au BufLeave,CursorHold,WinLeave * nested sil! update
augroup END

I don't understand why they make the highlighting disappear. They don't fire the CursorMoved event. I've checked using a plugin.

I found 2 solutions:

In vimrc, install any autocmd listening to CursorMoved (not a fire-once autocmd, contrary to the one which disables 'hls' in vim-slash):

au CursorMoved * exe ''

Or, in a filetype plugin matching the filetype of the current buffer, set the value of the 'conceallevel' option to any value greater than 0 (1, 2, or 3):

setl cole=1

And since it's a window-local option, it's better to install an autocmd listening to BufWinEnter, so that the option is properly set every time the same buffer is displayed in a new window:

augroup my_filetype_autocmd
    au! BufWinEnter  <buffer>
    au  BufWinEnter  <buffer> setl cole=1
augroup END

I have no idea why any of these 2 solutions work.

Thank you very much for your plugin.

Edit:

It happens in Vim version 8.0 (patches 1-995 included).

Edit 2:

In a real use case, you may miss the issue, because you have an autocmd listening to CursorMoved. For example, you probably have one because of the matchparen plugin. You can toggle the latter with :NoMatchParen / :DoMatchParen.

Here's another gif. In it, I use the same minimal vimrc, but before performing the search, I increase Vim's verbosity level to 8. As you can see, I don't type any key after the search, I just wait. After 4s, the highlighting disappears, and in the bottom left corner of the screen, Vim reports the following message:

executing CursorMoved Auto commands for "*"

I only found this issue after I started disabling the matchparen plugin.

Edit: I don't think it's linked to the plugin, it's probably an issue in Vim itself. I'll see if I can report on Vim's issue tracker.

Hi, sorry for not responding earlier. I tested with the minimal vimrc above, but I was unable to reproduce the problem. I even added echom to the autocmd to make sure that the event is fired, but as you can see in the asciinema, it's not reproducible.

https://asciinema.org/a/7GPoDdpYP8fyXfBDdqsglOkdO

set rtp+=~/.vim/plugged/vim-slash
augroup my_autocmd
    au!
    au CursorHold * echom 'hold' | exe ''
augroup END

@junegunn Thank you very much for your answer. No need to be sorry, I know you're busy, and it's not a problem with your plugin, but with Vim itself. The reason why you can't reproduce is probably because you have an autocmd listening to CursorMoved, installed by the default matchparen plugin (defined in $VIMRUNTIME/plugin/matchparen.vim).

If you could retry with the same minimal vimrc, but just disable matchparen (adding let g:loaded_matchparen = 1):

set rtp+=~/.vim/plugged/vim-slash
let g:loaded_matchparen = 1
augroup my_autocmd
    au!
    au CursorHold * echom 'hold' | exe ''
augroup END

... and if you could confirm that you can reproduce, it would really help me. I've already opened an issue on the Vim repository.

Ok this is probably an edge case, see here for an explanation.