romainl/vim-cool

Turn off highlight on CursorMoved and InsertEnter without remapping (doesn't break search operator pending mode)

purpleP opened this issue · 6 comments

Thanks to guys from neovim I've found a way to automatically turn off search highlight when it's no longer needed (for me).

Notice how I check if the cursor is on something that have been searched previously. In your plugin you're using if expand("<cword>") =~ @/ which wouldn't work if someone searched for more than one word.

So in case someone is wondering this would turn off highlight if you've moved cursor with anything except next/previous match motion or if you've entered insert mode (in operator pending mode also, yey!)

noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch()
    let s:pos = match(getline('.'), @/, col('.') - 1) + 1
    if s:pos != col('.')
        call StopHL()
    endif
endfu

fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    else
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

augroup SearchHighlight
au!
    au CursorMoved * call HlSearch()
    au InsertEnter * call StopHL()
augroup end

This works amazingly well and is a lot cleaner than what I did. Can I use it as starting point for a new branch?

Please take a look at branch "purplep": https://github.com/romainl/vim-cool/tree/purplep.

@romainl Yes, of course you can. I've leared about OptionSet event from you code. Cool.

I merged the "purplep" branch into "master". Thanks a lot!

Thanks to guys from neovim I've found a way to automatically turn off search highlight when it's no longer needed (for me).

Notice how I check if the cursor is on something that have been searched previously. In your plugin you're using if expand("<cword>") =~ @/ which wouldn't work if someone searched for more than one word.

So in case someone is wondering this would turn off highlight if you've moved cursor with anything except next/previous match motion or if you've entered insert mode (in operator pending mode also, yey!)

noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch()
    let s:pos = match(getline('.'), @/, col('.') - 1) + 1
    if s:pos != col('.')
        call StopHL()
    endif
endfu

fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    else
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

augroup SearchHighlight
au!
    au CursorMoved * call HlSearch()
    au InsertEnter * call StopHL()
augroup end

nice.. thanks a lot @purpleP

Thanks to guys from neovim I've found a way to automatically turn off search highlight when it's no longer needed (for me).
Notice how I check if the cursor is on something that have been searched previously. In your plugin you're using if expand("<cword>") =~ @/ which wouldn't work if someone searched for more than one word.
So in case someone is wondering this would turn off highlight if you've moved cursor with anything except next/previous match motion or if you've entered insert mode (in operator pending mode also, yey!)

noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch()
    let s:pos = match(getline('.'), @/, col('.') - 1) + 1
    if s:pos != col('.')
        call StopHL()
    endif
endfu

fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    else
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

augroup SearchHighlight
au!
    au CursorMoved * call HlSearch()
    au InsertEnter * call StopHL()
augroup end

nice.. thanks a lot @purpleP

🙏