sgur
7/31/2014 - 3:16 AM

カーソル行下のハイライトを行う (https://github.com/osyo-manga/vim-brightest) が出るまでの暫定版もしくは1ファイルのお手軽・計量版

カーソル行下のハイライトを行う (https://github.com/osyo-manga/vim-brightest) が出るまでの暫定版もしくは1ファイルのお手軽・計量版

" HI-C.vim (HIghlight-Cword)
" Version: 0.0.2
" Author: sgur
" License: MIT

if exists('g:loaded_hi_c') && g:loaded_hi_c
  finish
endif
let g:loaded_hi_c = 1

let s:save_cpo = &cpo
set cpo&vim



" Avoid highlighting the word under cursor [Defualt: 0]
let g:hi_c_avoid_cursor = get(g:, 'hi_c_avoid_cursor', 0)
" Highlight group to use (Default: 'Underlined')
let g:hi_c_highlight_group = get(g:, 'hi_c_highlight_group', 'Underlined')
" Filetype blacklist (Default: ["help"])
let g:hi_c_filetype_blacklist = get(g:, 'hi_c_filetype_blacklist', ['help'])
" Syntax pattern to ignore highlighting
let g:hi_c_ignore_syntax_pattern = get(g:, 'hi_c_ignore_syntax_pattern', 'comment')



" Enable
command! -nargs=0 HiCEnable  call s:enable()
" Disable
command! -nargs=0 HiCDisable  call s:disable()
" Toggle
command! -nargs=0 HiCToggle  call s:toggle()



let s:hi_c_enabled = 1
let s:highlighted_ids = []


function! s:enable()
  let s:hi_c_enabled = 1
  augroup plugin-hi_c
    autocmd!
    autocmd CursorMoved *  call s:highlight(s:cword())
    autocmd WinLeave *  call s:clear()
  augroup END
endfunction


function! s:disable()
  let s:hi_c_enabled = 0
  autocmd! plugin-hi_c
  augroup! plugin-hi_c
  call s:clear()
endfunction


function! s:toggle()
  if s:hi_c_enabled
    call s:disable()
  else
    call s:enable()
  endif
endfunction


function! s:highlight(term)
  call s:clear()
  if empty(a:term) || s:is_filetype_blacklisted() || s:is_syntax_pattern_ignored() || s:is_multibyte(a:term)
    return
  endif
  let id = matchadd(g:hi_c_highlight_group, printf('\V%s\<%s\>', s:pattern(&cursorline || g:hi_c_avoid_cursor), escape(a:term, '/\')), -1)
  call add(s:highlighted_ids, id)
endfunction


function! s:clear()
  for m in filter(getmatches(), 'index(s:highlighted_ids, v:val.id) > -1')
    call matchdelete(m.id)
  endfor
endfunction


function! s:pattern(enabled)
  if !a:enabled
    return
  endif
  let nr = line('.')
  return printf('\V\%(\%%<%sl\|\%%>%sl\)', nr, nr)
endfunction


function! s:is_multibyte(expr)
  return strlen(a:expr) != strchars(a:expr)
endfunction


function! s:is_filetype_blacklisted()
  return index(g:hi_c_filetype_blacklist, &filetype) > -1
endfunction


function! s:is_syntax_pattern_ignored()
  let syn = synIDattr(synID(line("."), col("."), 1), "name")
  return syn =~? g:hi_c_ignore_syntax_pattern
endfunction


function! s:cword()
  return getline('.')[col('.')-1] =~# '\k' ? expand('<cword>') : ''
endfunction



let &cpo = s:save_cpo
unlet s:save_cpo