Skip to content

Instantly share code, notes, and snippets.

@haya14busa
Created April 15, 2017 04:54
Show Gist options
  • Save haya14busa/c129e2c4c5a225178a4cd357019897bc to your computer and use it in GitHub Desktop.
Save haya14busa/c129e2c4c5a225178a4cd357019897bc to your computer and use it in GitHub Desktop.
bikabika
command! -nargs=1 BikaBika call s:make_bikabika(<q-args>)
command! -nargs=0 BikaBikaStop call s:stop_bikabika()
highlight! BikaBika ctermbg=green guibg=green
let s:_bika = {}
function! s:make_bikabika(pattern) abort
if !empty(s:_bika)
call s:_bika.stop()
endif
let b = s:bikabika.new(a:pattern, 'BikaBika')
call b.start()
let s:_bika = b
endfunction
function! s:stop_bikabika() abort
if empty(s:_bika)
return
endif
call s:_bika.stop()
endfunction
let s:bikabika = {
\ 'pattern': '',
\ 'higroup': '',
\ 'interval': 400,
\
\ '_on': 0,
\ '_matchid': 0,
\ '_timer': {},
\ }
function! s:bikabika.new(pattern, higroup) abort
let s = deepcopy(self)
let s.pattern = a:pattern
let s.higroup = a:higroup
return s
endfunction
function! s:bikabika.start() abort
let opt = {'repeat': -1}
let self._timer = timer_start(self.interval, self.toggle, opt)
endfunction
function! s:bikabika.stop() abort
call self.off()
call timer_stop(self._timer)
endfunction
function! s:bikabika.toggle(timer) abort
if self._on ==# 0
call self.on()
else
call self.off()
endif
endfunction
function! s:bikabika.on() abort
call s:windo(self.on_win)
endfunction
function! s:bikabika.on_win() abort
if has_key(w:, 'bikabika_matchid')
silent! call matchdelete(w:bikabika_matchid)
endif
let w:bikabika_matchid = matchadd(self.higroup, self.pattern, 1000)
let self._on = 1
endfunction
function! s:bikabika.off() abort
call s:windo(self.off_win)
endfunction
function! s:bikabika.off_win() abort
if !has_key(w:, 'bikabika_matchid')
return
endif
silent! call matchdelete(w:bikabika_matchid)
let self._on = 0
endfunction
function! s:windo(F) abort
let nr = winnr()
try
for winnr in range(1, winnr('$'))
call s:move_to_win(winnr)
call a:F()
endfor
finally
call s:move_to_win(nr)
endtry
endfunction
function! s:move_to_win(winnr) abort
if a:winnr !=# winnr()
execute 'noautocmd' a:winnr . 'wincmd w'
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment