1
Fork 0
vim-highlight/plugin/hightlight.vim

132 lines
4.0 KiB
VimL
Raw Normal View History

2016-11-06 19:42:13 +00:00
" ======================================================================
" File: highlight.vim
" Maintainer: Joshua Potter <jrpotter2112@gmail.com>
"
" ======================================================================
if exists('g:loaded_highlight')
finish
endif
let g:loaded_highlight = 1
" SCRIPT VARIABLES:
" ======================================================================
2016-11-06 19:46:44 +00:00
" s:highlight_register_color :: { String : String } {{{2
2016-11-06 19:42:13 +00:00
" ----------------------------------------------------------------------
2016-11-06 19:46:44 +00:00
" Mapping between registry name and color that should be used for
" highlighting.
2016-11-06 19:42:13 +00:00
2016-11-06 19:46:44 +00:00
let s:highlight_register_color = {}
2016-11-06 19:42:13 +00:00
" s:matches :: { String : Match } {{{2
" ----------------------------------------------------------------------
2016-11-06 19:46:44 +00:00
" List of matches corresponding to the registry.
2016-11-06 19:42:13 +00:00
let s:matches = {}
" s:registry :: { String : [String] } {{{2
" ----------------------------------------------------------------------
2016-11-06 19:46:44 +00:00
" Name of register corresponding to a list of the keys used in the
" s:matches corresponding to the match of the word in said register.
" Used to find the matches that can be deleted later on.
2016-11-06 19:42:13 +00:00
let s:registry = {}
" FUNCTION: MatchName(reg, word) {{{1
" ======================================================================
function! s:MatchName(reg, word)
2016-11-06 20:13:11 +00:00
return a:reg . '_' . word
endfunction
2016-11-06 19:42:13 +00:00
" FUNCTION: GroupName(reg) {{{1
" ======================================================================
function! s:GroupName(reg)
2016-11-06 20:13:11 +00:00
return 'PlugHighlightRegister_' . a:reg
2016-11-06 19:42:13 +00:00
endfunction
" FUNCTION: ClearHighlightRegister(reg) {{{1
" ======================================================================
" Used to clear out the 'registers' that are used to hold which values are
" highlighted under a certain match group.
function! s:ClearHighlightRegister(reg)
2016-11-06 20:13:11 +00:00
exe 'hi clear ' . s:GroupName(a:reg)
2016-11-06 19:42:13 +00:00
unlet s:highlight_register_color[a:reg]
if has_key(s:registry, a:reg)
for m in s:registry[a:reg]
matchdelete(s:matches[s:registry[a:reg]])
unlet s:matches[s:registry[a:reg]]
endfor
unlet s:registry[a:reg]
endif
endfunction
" FUNCTION: InitHighlightRegister(reg) {{{1
" ======================================================================
" Setups the group and highlighting. Matches are added afterward
function! s:InitHighlightRegister(reg, color)
call c:ClearHighlightRegister(a:reg)
let s:highlight_register_color[a:reg] = a:color
2016-11-06 20:13:11 +00:00
exe 'hi ' . s:GroupName(a:reg) . ' cterm=bold, underline ctermbg=' . a:color
2016-11-06 19:42:13 +00:00
endfunction
" FUNCTION: AppendToSearch(reg, word) {{{1
" ======================================================================
function! s:AppendToSearch(reg, word)
2016-11-06 20:13:11 +00:00
let m = matchadd(s:GroupName(a:reg), '\<' . a:word . '\>')
2016-11-06 19:42:13 +00:00
let s:matches[s:MatchName(a:reg, a:word)] = l:m
if !has_key(s:registry, a:reg)
let s:registry[a:reg] = []
endif
append(s:registry[a:reg], s:MatchName(a:reg, a:word))
endfunction
" FUNCTION: RemoveFromSearch(reg, word) {{{1
" ======================================================================
function! s:RemoveFromSearch(reg, word)
matchdelete(s:matches[s:MatchName(a:reg, a:word)])
unlet s:matches[s:MatchName(a:reg, a:word)]
let i = 0
while i < len(s:registry[a:reg])
if s:registry[a:reg] == s:MatchName(a:reg, a:word)
unlet s:registry[a:reg][i]
break
endif
i = i + 1
endwhile
if len(s:registry[a:reg]) == 0
unlet s:registry[a:reg]
endif
endfunction
2016-11-06 20:13:11 +00:00
2016-11-06 19:42:13 +00:00
" FUNCTION: Initialize {{{1
" ======================================================================
call s:InitHighlightRegister('0', 'Yellow')
call s:InitHighlightRegister('1', 'DarkYellow')
call s:InitHighlightRegister('2', 'Red')
call s:InitHighlightRegister('3', 'DarkRed')
call s:InitHighlightRegister('4', 'Green')
call s:InitHighlightRegister('5', 'DarkGreen')
call s:InitHighlightRegister('6', 'Blue')
call s:InitHighlightRegister('7', 'DarkBlue')
call s:InitHighlightRegister('8', 'Magenta')
call s:InitHighlightRegister('9', 'DarkMagenta')