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

192 lines
6.2 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
2016-11-07 08:30:51 +00:00
" GLOBAL VARIABLES:
2016-11-06 19:42:13 +00:00
" ======================================================================
2016-11-07 08:30:51 +00:00
let g:highlight_register_default_color = 'Yellow'
2016-11-06 19:42:13 +00:00
2016-11-07 08:30:51 +00:00
" SCRIPT VARIABLES:
" ======================================================================
2016-11-06 19:42:13 +00:00
2016-11-07 08:30:51 +00:00
" s:registry_colors :: { String : String } {{{2
2016-11-06 19:42:13 +00:00
" ----------------------------------------------------------------------
2016-11-07 08:30:51 +00:00
" Mapping between registry name and color that should be used for
" highlighting.
2016-11-06 19:42:13 +00:00
2016-11-07 08:30:51 +00:00
let s:registry_colors = {}
2016-11-06 19:42:13 +00:00
2016-11-07 08:30:51 +00:00
" s:registry :: { String : { String : Match } } {{{2
2016-11-06 19:42:13 +00:00
" ----------------------------------------------------------------------
2016-11-07 08:30:51 +00:00
" Name of register corresponding to a dict of some unique identifier of the
" word being matched, paired with the actual match object.
2016-11-06 19:42:13 +00:00
let s:registry = {}
2016-11-07 08:30:51 +00:00
" FUNCTION: GroupName(reg) {{{1
2016-11-06 19:42:13 +00:00
" ======================================================================
2016-11-07 08:30:51 +00:00
" Note group names are not allowed to have special characters; they
" must be alphanumeric or underscores.
2016-11-06 19:42:13 +00:00
2016-11-07 08:30:51 +00:00
function! s:GroupName(reg)
return 'HighlightRegistry_' . char2nr(a:reg)
2016-11-06 20:13:11 +00:00
endfunction
2016-11-06 19:42:13 +00:00
2016-11-07 08:30:51 +00:00
" FUNCTION: InitRegister(reg) {{{1
2016-11-06 19:42:13 +00:00
" ======================================================================
2016-11-07 08:30:51 +00:00
" Setups the group and highlighting. Matches are added afterward.
2016-11-06 19:42:13 +00:00
2016-11-07 08:30:51 +00:00
function! s:InitRegister(reg, color)
call s:ClearRegister(a:reg)
let s:registry_colors[a:reg] = a:color
exe 'hi ' . s:GroupName(a:reg) . ' cterm=bold,underline ctermfg=' . a:color
2016-11-06 19:42:13 +00:00
endfunction
2016-11-07 08:30:51 +00:00
" FUNCTION: ClearRegister(reg) {{{1
2016-11-06 19:42:13 +00:00
" ======================================================================
" Used to clear out the 'registers' that are used to hold which values are
" highlighted under a certain match group.
2016-11-07 08:30:51 +00:00
function! s:ClearRegister(reg)
2016-11-06 20:13:11 +00:00
exe 'hi clear ' . s:GroupName(a:reg)
2016-11-07 08:30:51 +00:00
if has_key(s:registry_colors, a:reg)
unlet s:registry_colors[a:reg]
endif
2016-11-06 19:42:13 +00:00
if has_key(s:registry, a:reg)
2016-11-07 08:30:51 +00:00
for key in keys(s:registry[a:reg])
call matchdelete(s:registry[a:reg][key])
unlet s:registry[a:reg][key]
2016-11-06 19:42:13 +00:00
endfor
unlet s:registry[a:reg]
endif
2016-11-07 08:30:51 +00:00
call s:ActivateRegister(a:reg)
2016-11-06 19:42:13 +00:00
endfunction
2016-11-07 08:30:51 +00:00
" FUNCTION: ActivateRegister(reg) {{{1
2016-11-06 19:42:13 +00:00
" ======================================================================
2016-11-07 08:30:51 +00:00
" We must actively set the search register to perform searches as expected.
2016-11-06 19:42:13 +00:00
2016-11-07 08:30:51 +00:00
function! s:ActivateRegister(reg)
if has_key(s:registry, a:reg) && has_key(s:registry_colors, a:reg)
let search = ''
for key in keys(s:registry[a:reg])
let search = search . key . '\|'
endfor
let @/ = search[:-3]
exe 'hi Search cterm=bold,underline ctermbg=none ctermfg=' . s:registry_colors[a:reg]
set hlsearch
else
let @/ = ''
endif
2016-11-06 19:42:13 +00:00
endfunction
2016-11-07 08:30:51 +00:00
" FUNCTION: AppendToSearch(reg, pattern) {{{1
2016-11-06 19:42:13 +00:00
" ======================================================================
2016-11-07 08:30:51 +00:00
function! s:AppendToSearch(reg, pattern)
let s:last_seen = a:pattern
if !has_key(s:registry_colors, a:reg)
call s:InitRegister(a:reg, g:highlight_register_default_color)
endif
2016-11-06 19:42:13 +00:00
if !has_key(s:registry, a:reg)
2016-11-07 08:30:51 +00:00
let s:registry[a:reg] = {}
endif
" Don't want to add multiple match objects into registry
if !has_key(s:registry[a:reg], a:pattern)
let s:registry[a:reg][a:pattern] =
\ matchadd(s:GroupName(a:reg), a:pattern)
2016-11-06 19:42:13 +00:00
endif
2016-11-07 08:30:51 +00:00
call s:ActivateRegister(a:reg)
endfunction
" FUNCTION: AppendToSearchForward(reg, pattern) {{{1
" ======================================================================
function! s:AppendToSearchForward(reg, pattern)
call s:AppendToSearch(a:reg, a:pattern)
normal! *
2016-11-06 19:42:13 +00:00
endfunction
2016-11-07 08:30:51 +00:00
" FUNCTION: AppendToSearchBackward(reg, pattern) {{{1
2016-11-06 19:42:13 +00:00
" ======================================================================
2016-11-07 08:30:51 +00:00
function! s:AppendToSearchBackward(reg, pattern)
call s:AppendToSearch(a:reg, a:pattern)
normal! #
endfunction
" FUNCTION: RemoveFromSearch(reg, pattern) {{{1
" ======================================================================
function! s:RemoveFromSearch(reg, pattern)
if has_key(s:registry, a:reg) && has_key(s:registry[a:reg], a:pattern)
call matchdelete(s:registry[a:reg][a:pattern])
unlet s:registry[a:reg][a:pattern]
if len(s:registry[a:reg]) == 0
unlet s:registry[a:reg]
2016-11-06 19:42:13 +00:00
endif
endif
2016-11-07 08:30:51 +00:00
call s:ActivateRegister(a:reg)
2016-11-06 19:42:13 +00:00
endfunction
2016-11-07 08:30:51 +00:00
" PROCEDURE: Initialize {{{1
2016-11-06 19:42:13 +00:00
" ======================================================================
2016-11-07 08:30:51 +00:00
exe 'hi Search cterm=bold,underline ctermbg=none ctermfg=' . g:highlight_register_default_color
call s:InitRegister('0', 'Yellow')
call s:InitRegister('1', 'DarkYellow')
call s:InitRegister('2', 'Red')
call s:InitRegister('3', 'Magenta')
call s:InitRegister('4', 'Green')
call s:InitRegister('5', 'Cyan')
call s:InitRegister('6', 'Blue')
call s:InitRegister('7', 'White')
call s:InitRegister('8', 'Gray')
call s:InitRegister('9', 'Black')
noremap <unique> <silent> <Plug>HighlightRegistry_AppendToSearch
\ :call <SID>AppendToSearch(v:register, '\<'.expand('<cword>').'\>')<CR>
noremap <unique> <silent> <Plug>HighlightRegistry_Forward_AppendToSearch
\ :call <SID>AppendToSearchForward(v:register, '\<'.expand('<cword>').'\>')<CR>
noremap <unique> <silent> <Plug>HighlightRegistry_Backward_AppendToSearch
\ :call <SID>AppendToSearchBackward(v:register, '\<'.expand('<cword>').'\>')<CR>
noremap <unique> <silent> <Plug>HighlightRegistry_RemoveFromSearch
\ :call <SID>RemoveFromSearch(v:register, '\<'.expand('<cword>').'\>')<CR>
noremap <unique> <silent> <Plug>HighlightRegistry_ClearRegister
\ :call <SID>ClearRegister(v:register)<CR>
" Basic Mappings
nmap & <Plug>HighlightRegistry_AppendToSearch
nmap * <Plug>HighlightRegistry_Forward_AppendToSearch
nmap # <Plug>HighlightRegistry_Backward_AppendToSearch
" Additional Register Modifiers
nmap d& <Plug>HighlightRegistry_RemoveFromSearch
nmap c& <Plug>HighlightRegistry_ClearRegister
" Other Mappings
nmap n :norm! nzzzv<CR>
nmap N :norm! Nzzzv<CR>
2016-11-06 19:42:13 +00:00