Created
August 28, 2021 10:50
-
-
Save jaandrle/9356d737ef5dfda2efbe50248d32cb78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" ========================================================================/// | |
" File: cwordhi.vim | |
" Description: highlight other occurrences of current word | |
" Version: 0.0.1 | |
" Author: Gianmaria Bajo <[email protected]> | |
" License: MIT | |
" Created: mer 20 novembre 2019 07:20:10 | |
" Modified: ven 31 gennaio 2020 14:40:16 | |
" ========================================================================/// | |
" GUARD {{{1 | |
if exists('g:loaded_cwordhi') | |
finish | |
endif | |
let g:loaded_cwordhi = 1 | |
let s:save_cpo = &cpo | |
set cpo&vim | |
" }}} | |
let g:cwordhi_enabled = get(g:, 'cwordhi_enabled', 0) | |
command! CwordHiToggle call <sid>cword_toggle() | |
fun! s:cword_toggle() | |
" Toggle word highlight {{{1 | |
if g:cwordhi_enabled | |
call s:cword_off() | |
else | |
call s:cword_on() | |
endif | |
endfun "}}} | |
fun! s:cword_on() | |
" Enable word highlight {{{1 | |
let g:cwordhi_enabled = 1 | |
augroup cwordhi | |
au! | |
au WinLeave * call s:cword_clear() | |
au InsertEnter * call s:cword_clear() | |
au InsertLeave * call s:cword_hi() | |
au CursorMoved * call s:cword_hi() | |
augroup END | |
doautocmd CursorMoved | |
endfun "}}} | |
fun! s:cword_off() | |
" Disable word highlight {{{1 | |
let g:cwordhi_enabled = 0 | |
call s:cword_clear() | |
autocmd! cwordhi | |
augroup! cwordhi | |
endfun "}}} | |
fun! s:cword_hi() | |
" Reapply word highlight {{{1 | |
call s:cword_clear() | |
if mode() != 'n' | |
return | |
endif | |
let word = escape(expand('<cword>'), '\') | |
if word =~ '\k' && matchstr(getline('.'), '\%' . col('.') . 'c.') =~ '\k' | |
let word = '\C\<' . word . '\>' | |
" restrict pattern matching to top and bottom visible lines | |
let below = '\%>' . line('.') . 'l\%<' . line('w$') . 'l' | |
let above = '\%<' . line('.') . 'l\%>' . line('w0') . 'l' | |
let right = '\%>' . col('.') . 'c' | |
let left = '\%<' . max([0, col('.') - strlen(expand('<cword>'))]) . 'c' | |
let h = get(g:, 'cwordhi', 'VisualNOS') | |
let w:illuminated_words_below = matchadd(h, '\V' . below . word) | |
let w:illuminated_words_above = matchadd(h, '\V' . above . word) | |
let w:illuminated_words_right = matchadd(h, '\V' . right . word) | |
let w:illuminated_words_left = matchadd(h, '\V' . left . word) | |
endif | |
endfun "}}} | |
fun! s:cword_clear() | |
" Clear word highlight {{{1 | |
silent! call matchdelete(w:illuminated_words_above) | |
silent! call matchdelete(w:illuminated_words_below) | |
silent! call matchdelete(w:illuminated_words_right) | |
silent! call matchdelete(w:illuminated_words_left) | |
endfun "}}} | |
" FINISH {{{1 | |
let &cpo = s:save_cpo | |
unlet s:save_cpo | |
let g:cwordhi_autoload = get(g:, 'cwordhi#autoload', 0) | |
if g:cwordhi_autoload | |
call s:cword_on() | |
endif | |
" vim: et sw=2 ts=2 sts=2 fdm=marker | |
" }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment