vim:aucmd: Highlight selection in all windows

Highlight the visual selection in all windows containing the current
buffer.
This commit is contained in:
2023-08-22 19:27:12 +02:00
parent 50eecf7e01
commit 424be02df4

View File

@@ -58,9 +58,13 @@ function! ClearHighlights()
unlet w:cword_match_id unlet w:cword_match_id
unlet w:old_cword unlet w:old_cword
endif endif
if exists('w:visual_match_id') if exists('w:visual_match_ids')
call matchdelete(w:visual_match_id) for l:pairs in w:visual_match_ids
unlet w:visual_match_id let l:id = l:pairs[0]
let l:win = l:pairs[1]
call matchdelete(l:id, l:win)
endfor
unlet w:visual_match_ids
endif endif
endfunction endfunction
@@ -86,15 +90,25 @@ function! HighlightVisualSel()
let l:old_regtype = getregtype('"') let l:old_regtype = getregtype('"')
silent! norm ygv silent! norm ygv
let w:visual_match_id = matchadd( let w:visual_match_ids = []
\ 'CursorColumn',
\ '\V' . escape(@", '\'), " Add match to all windows containing the current buffer
\ -1) for l:win in win_findbuf(bufnr())
let w:visual_match_ids += [[
\ matchadd(
\ 'CursorColumn',
\ '\V' . escape(@", '\'),
\ -1,
\ -1,
\ {'window': l:win}),
\ l:win
\ ]]
endfor
call setreg('"', l:old_reg, l:old_regtype) call setreg('"', l:old_reg, l:old_regtype)
endfunction endfunction
augroup highlight_current_word augroup highlight_current
au! au!
au CursorMoved * if mode() == 'n' | au CursorMoved * if mode() == 'n' |
\ call HighlightCurrentWord() | \ call HighlightCurrentWord() |
@@ -102,6 +116,7 @@ augroup highlight_current_word
\ call HighlightVisualSel() | \ call HighlightVisualSel() |
\ endif \ endif
au CursorMovedI * call HighlightCurrentWord() au CursorMovedI * call HighlightCurrentWord()
au WinLeave * call ClearHighlights()
augroup END augroup END
" When switching focus to another window, keep the cursor location underlined. " When switching focus to another window, keep the cursor location underlined.