add credit where credit's due ;)
Source Link
Rich
  • 26.6k
  • 3
  • 51
  • 111

I have a functionan adapted version of Damian Conway's Die Blinkënmatchen in my ~/.vimrc that "highlight-flashes" search matches as I step through them with n / N (something I found online somewhere and adapted slightly, unfortunately I no longer know where I found it so I cannot give credit):

" 'hlsearch' makes it hard to tell which is the *current* match, so we
" do some trickery to highlight that one in a special way (flashing for
" 0.4 seconds).

" function to flash the current match a number of times
function! HLNext (blink)
    let [bufnum, lnum, col, off] = getpos('.')
    let matchlen = strlen(matchstr(strpart(getline('.'),col-1),@/))
    let target_pat = '\c\%#\%('.@/.'\)'
    let i = 1
    while i < a:blink
        exec 'sleep 50m'
        let ring = matchadd('Comment', target_pat, 101)
        redraw
        exec 'sleep 50m'
        call matchdelete(ring)
        redraw
        let i += 1
    endwhile
endfunction

" Rewire n and N to step-and-call-function
nnoremap <silent> n   n:call HLNext(5)<cr>
nnoremap <silent> N   N:call HLNext(5)<cr>

This works nice, but has one drawback -- skipping through many matches takes much longer, as the next match is not highlighted until the flashing of the current one has finished.

Is there a way to have the function interrupt the highlighting / flashing if n / N is pressed again while the function still runs?

I am afraid my knowledge of Vim programming is very limited (to copy & paste, mostly), so I hope to find the necessary brainpower among you lot.

I have a function in my ~/.vimrc that "highlight-flashes" search matches as I step through them with n / N (something I found online somewhere and adapted slightly, unfortunately I no longer know where I found it so I cannot give credit):

" 'hlsearch' makes it hard to tell which is the *current* match, so we
" do some trickery to highlight that one in a special way (flashing for
" 0.4 seconds).

" function to flash the current match a number of times
function! HLNext (blink)
    let [bufnum, lnum, col, off] = getpos('.')
    let matchlen = strlen(matchstr(strpart(getline('.'),col-1),@/))
    let target_pat = '\c\%#\%('.@/.'\)'
    let i = 1
    while i < a:blink
        exec 'sleep 50m'
        let ring = matchadd('Comment', target_pat, 101)
        redraw
        exec 'sleep 50m'
        call matchdelete(ring)
        redraw
        let i += 1
    endwhile
endfunction

" Rewire n and N to step-and-call-function
nnoremap <silent> n   n:call HLNext(5)<cr>
nnoremap <silent> N   N:call HLNext(5)<cr>

This works nice, but has one drawback -- skipping through many matches takes much longer, as the next match is not highlighted until the flashing of the current one has finished.

Is there a way to have the function interrupt the highlighting / flashing if n / N is pressed again while the function still runs?

I am afraid my knowledge of Vim programming is very limited (to copy & paste, mostly), so I hope to find the necessary brainpower among you lot.

I have an adapted version of Damian Conway's Die Blinkënmatchen in my ~/.vimrc that "highlight-flashes" search matches as I step through them with n / N:

" 'hlsearch' makes it hard to tell which is the *current* match, so we
" do some trickery to highlight that one in a special way (flashing for
" 0.4 seconds).

" function to flash the current match a number of times
function! HLNext (blink)
    let [bufnum, lnum, col, off] = getpos('.')
    let matchlen = strlen(matchstr(strpart(getline('.'),col-1),@/))
    let target_pat = '\c\%#\%('.@/.'\)'
    let i = 1
    while i < a:blink
        exec 'sleep 50m'
        let ring = matchadd('Comment', target_pat, 101)
        redraw
        exec 'sleep 50m'
        call matchdelete(ring)
        redraw
        let i += 1
    endwhile
endfunction

" Rewire n and N to step-and-call-function
nnoremap <silent> n   n:call HLNext(5)<cr>
nnoremap <silent> N   N:call HLNext(5)<cr>

This works nice, but has one drawback -- skipping through many matches takes much longer, as the next match is not highlighted until the flashing of the current one has finished.

Is there a way to have the function interrupt the highlighting / flashing if n / N is pressed again while the function still runs?

I am afraid my knowledge of Vim programming is very limited (to copy & paste, mostly), so I hope to find the necessary brainpower among you lot.

Source Link
DevSolar
  • 749
  • 1
  • 6
  • 14

Interrupting (blink-highlighting) function if mapping is invoked again

I have a function in my ~/.vimrc that "highlight-flashes" search matches as I step through them with n / N (something I found online somewhere and adapted slightly, unfortunately I no longer know where I found it so I cannot give credit):

" 'hlsearch' makes it hard to tell which is the *current* match, so we
" do some trickery to highlight that one in a special way (flashing for
" 0.4 seconds).

" function to flash the current match a number of times
function! HLNext (blink)
    let [bufnum, lnum, col, off] = getpos('.')
    let matchlen = strlen(matchstr(strpart(getline('.'),col-1),@/))
    let target_pat = '\c\%#\%('.@/.'\)'
    let i = 1
    while i < a:blink
        exec 'sleep 50m'
        let ring = matchadd('Comment', target_pat, 101)
        redraw
        exec 'sleep 50m'
        call matchdelete(ring)
        redraw
        let i += 1
    endwhile
endfunction

" Rewire n and N to step-and-call-function
nnoremap <silent> n   n:call HLNext(5)<cr>
nnoremap <silent> N   N:call HLNext(5)<cr>

This works nice, but has one drawback -- skipping through many matches takes much longer, as the next match is not highlighted until the flashing of the current one has finished.

Is there a way to have the function interrupt the highlighting / flashing if n / N is pressed again while the function still runs?

I am afraid my knowledge of Vim programming is very limited (to copy & paste, mostly), so I hope to find the necessary brainpower among you lot.