From 0af5873a040503833887d748d73707391743f5fe Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 18 Nov 2016 15:11:12 -0800 Subject: [PATCH] moved to autoload --- autoload/highlight.vim | 159 +++++++++++++++++++++++++ plugin/hightlight.vim | 258 ++++++++++------------------------------- 2 files changed, 219 insertions(+), 198 deletions(-) create mode 100644 autoload/highlight.vim diff --git a/autoload/highlight.vim b/autoload/highlight.vim new file mode 100644 index 0000000..26cb649 --- /dev/null +++ b/autoload/highlight.vim @@ -0,0 +1,159 @@ +" ====================================================================== +" File: highlight.vim +" Maintainer: Joshua Potter +" +" ====================================================================== + +" SCRIPT VARIABLES: +" ====================================================================== + +" s:last_seen :: String {{{2 +" ---------------------------------------------------------------------- +" The pattern last appended to a registry list. + +let s:last_seen = @/ + + +" s:registry_colors :: { String : String } {{{2 +" ---------------------------------------------------------------------- +" Mapping between registry name and color that should be used for +" highlighting. + +let s:registry_colors = {} + + +" s:registry :: { String : { String : Match } } {{{2 +" ---------------------------------------------------------------------- +" Name of register corresponding to a dict of some unique identifier of the +" word being matched, paired with the actual match object. + +let s:registry = {} + + +" FUNCTION: GetGroupName(reg) {{{1 +" ====================================================================== +" Note group names are not allowed to have special characters; they +" must be alphanumeric or underscores. + +function! highlight#get_group_name() + return 'highlight_registry_' . char2nr(v:register) +endfunction + + +" FUNCTION: InitRegister() {{{1 +" ====================================================================== +" Setups the group and highlighting. Matches are added afterward. + +function! highlight#init_register(color) + call highlight#clear_register() + let s:registry_colors[v:register] = a:color + exe 'hi ' . highlight#get_group_name() . + \ ' cterm=bold,underline ctermfg=' . a:color +endfunction + + +" FUNCTION: ClearRegister() {{{1 +" ====================================================================== +" Used to clear out the 'registers' that are used to hold which values are +" highlighted under a certain match group. + +function! highlight#clear_register() + exe 'hi clear ' . highlight#get_group_name() + if has_key(s:registry_colors, v:register) + unlet s:registry_colors[v:register] + endif + if has_key(s:registry, v:register) + for key in keys(s:registry[v:register]) + call matchdelete(s:registry[v:register][key]) + unlet s:registry[v:register][key] + endfor + unlet s:registry[v:register] + endif + call highlight#activate_register() +endfunction + + +" FUNCTION: CountLastSeen() {{{1 +" ====================================================================== + +function! highlight#count_last_seen() + if len(@/) > 0 + let pos = getpos('.') + let pos[2] = pos[2] - 1 + exe ' %s/' . s:last_seen . '//gne' + call setpos('.', pos) + endif +endfunction + + +" FUNCTION: ActivateRegister() {{{1 +" ====================================================================== +" We must actively set the search register to perform searches as expected. + +function! highlight#activate_register() + if has_key(s:registry, v:register) && has_key(s:registry_colors, v:register) + let search = '' + for key in keys(s:registry[v:register]) + let search = search . key . '\|' + endfor + let @/ = search[:-3] + exe 'hi Search cterm=bold,underline ctermbg=none ctermfg=' . + \ s:registry_colors[v:register] + set hlsearch + else + let @/ = '' + endif +endfunction + + +" FUNCTION: AppendToSearch(pattern, ...) {{{1 +" ====================================================================== + +function! highlight#append_to_search(pattern) + let s:last_seen = a:pattern + if len(a:pattern) > 0 + if !has_key(s:registry_colors, v:register) + call highlight#init_register(g:highlight_register_default_color) + endif + if !has_key(s:registry, v:register) + let s:registry[v:register] = {} + endif + " Don't want to add multiple match objects into registry + if !has_key(s:registry[v:register], a:pattern) + let s:registry[v:register][a:pattern] = + \ matchadd(highlight#get_group_name(), a:pattern) + endif + call highlight#activate_register() + endif +endfunction + + +" FUNCTION: GetVisualSelection {{{1 +" ====================================================================== + +function! highlight#get_visual_selection() + let [lnum1, col1] = getpos("'<")[1:2] + let [lnum2, col2] = getpos("'>")[1:2] + let lines = getline(lnum1, lnum2) + let lines[-1] = lines[-1][:col2 - (&selection == 'inclusive' ? 1 : 2)] + let lines[0] = lines[0][col1 - 1:] + return substitute(escape(join(lines, "\n"), '\\/.*$%~[]'), '\n', '\\n', 'g') +endfunction + + +" FUNCTION: RemoveFromSearch(pattern) {{{1 +" ====================================================================== + +function! highlight#remove_from_search(pattern) + if has_key(s:registry, v:register) + if has_key(s:registry[v:register], a:pattern) + call matchdelete(s:registry[v:register][a:pattern]) + unlet s:registry[v:register][a:pattern] + if len(s:registry[v:register]) == 0 + unlet s:registry[v:register] + endif + endif + endif + call highlight#activate_register() +endfunction + diff --git a/plugin/hightlight.vim b/plugin/hightlight.vim index 9d09ee2..90c0a99 100644 --- a/plugin/hightlight.vim +++ b/plugin/hightlight.vim @@ -4,229 +4,91 @@ " " ====================================================================== -if exists('g:loaded_highlight') - finish +if exists('g:loaded_highlight_registry') + finish endif -let g:loaded_highlight = 1 +let g:loaded_highlight_registry = 1 " GLOBAL VARIABLES: " ====================================================================== -let g:highlight_register_default_color = 'Yellow' - - -" SCRIPT VARIABLES: -" ====================================================================== - -" s:last_seen :: String {{{2 +" g:highlight_register_default_color :: String {{{2 " ---------------------------------------------------------------------- -" The pattern last appended to a registry list. -let s:last_seen = @/ +if !exists('g:highlight_register_default_color') + let g:highlight_register_default_color = 'Yellow' +endif -" s:registry_colors :: { String : String } {{{2 +" g:highlight_registry :: { String : String } {{{2 " ---------------------------------------------------------------------- -" Mapping between registry name and color that should be used for -" highlighting. -let s:registry_colors = {} +if !exists('g:highlight_registry') + let g:highlight_registry = { '0' : 'Yellow' + \ '1' : 'Blue' + \ '2' : 'Red' + \ '3' : 'Magenta' + \ '4' : 'Green' + \ '5' : 'Cyan' + \ '6' : 'DarkYellow' + \ '7' : 'White' + \ '8' : 'Gray' + \ '9' : 'Black' + \ } +endif -" s:registry :: { String : { String : Match } } {{{2 -" ---------------------------------------------------------------------- -" Name of register corresponding to a dict of some unique identifier of the -" word being matched, paired with the actual match object. - -let s:registry = {} - - -" FUNCTION: GroupName(reg) {{{1 -" ====================================================================== -" Note group names are not allowed to have special characters; they -" must be alphanumeric or underscores. - -function! s:GroupName(reg) - return 'HighlightRegistry_' . char2nr(a:reg) -endfunction - - -" FUNCTION: InitRegister(reg) {{{1 -" ====================================================================== -" Setups the group and highlighting. Matches are added afterward. - -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 -endfunction - - -" FUNCTION: ClearRegister(reg) {{{1 -" ====================================================================== -" Used to clear out the 'registers' that are used to hold which values are -" highlighted under a certain match group. - -function! s:ClearRegister(reg) - exe 'hi clear ' . s:GroupName(a:reg) - if has_key(s:registry_colors, a:reg) - unlet s:registry_colors[a:reg] - endif - if has_key(s:registry, a:reg) - for key in keys(s:registry[a:reg]) - call matchdelete(s:registry[a:reg][key]) - unlet s:registry[a:reg][key] - endfor - unlet s:registry[a:reg] - endif - call s:ActivateRegister(a:reg) -endfunction - - -" FUNCTION: CountLastSeen() {{{1 +" MAPPINGS: {{{1 " ====================================================================== -function! s:CountLastSeen() - if len(@/) > 0 - let pos = getpos('.') - let pos[2] = pos[2] - 1 - exe ' %s/' . s:last_seen . '//gne' - call setpos('.', pos) - endif -endfunction +" Append Searches +noremap HRegistry_AppendToSearch + \ :call highlight#append_to_search('\<'.expand('').'\>') + \ :call highlight#count_last_seen() +noremap HRegistry_GlobalAppendToSearch + \ :call highlight#append_to_search(expand('')) + \ :call highlight#count_last_seen() +noremap HRegistry_VisualAppendToSearch + \ :call highlight#append_to_search(highlight#get_visual_selection()) + \ :call highlight#count_last_seen()'< +" Remove Searches +noremap HRegistry_RemoveFromSearch + \ :call highlight#remove_from_search('\<'.expand('').'\>') +noremap HRegistry_VisualRemoveFromSearch + \ :call highlight#remove_from_search(highlight#get_visual_selection())'< -" FUNCTION: ActivateRegister(reg) {{{1 -" ====================================================================== -" We must actively set the search register to perform searches as expected. +" Other Modifications +noremap HRegistry_ClearRegister :call highlight#clear_register() +noremap HRegistry_ActivateRegister :call highlight#activate_register() +noremap HRegistry_CountLastSeen :call highlight#count_last_seen() -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 -endfunction +" Normal Mappings +nmap & HRegistry_AppendToSearch +nmap g& HRegistry_GlobalAppendToSearch +nmap y& HRegistry_ActivateRegister +nmap d& HRegistry_RemoveFromSearch +nmap c& HRegistry_ClearRegister +nmap * :silent norm! *& +nmap g* :silent norm! *g& -" FUNCTION: AppendToSearch(reg, pattern, ...) {{{1 -" ====================================================================== +nmap # :silent norm! #& +nmap g# :silent norm! #g& -function! s:AppendToSearch(reg, pattern) - let s:last_seen = a:pattern - if len(a:pattern) > 0 - if !has_key(s:registry_colors, a:reg) - call s:InitRegister(a:reg, g:highlight_register_default_color) - endif - if !has_key(s:registry, a:reg) - 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) - endif - call s:ActivateRegister(a:reg) - endif -endfunction - - -" FUNCTION: GetVisualSelection {{{1 -" ====================================================================== - -function! s:GetVisualSelection() - let [lnum1, col1] = getpos("'<")[1:2] - let [lnum2, col2] = getpos("'>")[1:2] - let lines = getline(lnum1, lnum2) - let lines[-1] = lines[-1][:col2 - (&selection == 'inclusive' ? 1 : 2)] - let lines[0] = lines[0][col1 - 1:] - return substitute(escape(join(lines, "\n"), '\\/.*$%~[]'), '\n', '\\n', 'g') -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] - endif - endif - call s:ActivateRegister(a:reg) -endfunction +" Visual Mappings +vmap & HRegistry_VisualAppendToSearch +vmap d& HRegistry_VisualRemoveFromSearch +vmap * &nHRegistry_CountLastSeen +vmap # &NHRegistry_CountLastSeen " PROCEDURE: Initialize {{{1 " ====================================================================== -exe 'hi Search cterm=bold,underline ctermbg=none ctermfg=' . g:highlight_register_default_color - -call s:InitRegister('0', 'Yellow') -call s:InitRegister('1', 'Blue') -call s:InitRegister('2', 'Red') -call s:InitRegister('3', 'Magenta') -call s:InitRegister('4', 'Green') -call s:InitRegister('5', 'Cyan') -call s:InitRegister('6', 'DarkYellow') -call s:InitRegister('7', 'White') -call s:InitRegister('8', 'Gray') -call s:InitRegister('9', 'Black') - -call s:AppendToSearch(v:register, @/) - -" Word Boundary Search Modifications -noremap HighlightRegistry_AppendToSearch - \ :call AppendToSearch(v:register, '\<'.expand('').'\>') - \ :call CountLastSeen() -noremap HighlightRegistry_RemoveFromSearch - \ :call RemoveFromSearch(v:register, '\<'.expand('').'\>') - -" Arbitrary Search Modifications -noremap HighlightRegistry_GlobalAppendToSearch - \ :call AppendToSearch(v:register, expand('')) - \ :call CountLastSeen() -noremap HighlightRegistry_VisualAppendToSearch - \ :call AppendToSearch(v:register, GetVisualSelection()) - \ :call CountLastSeen() -noremap HighlightRegistry_VisualRemoveFromSearch - \ :call RemoveFromSearch(v:register, GetVisualSelection()) - -" Register Modifications -noremap HighlightRegistry_ClearRegister - \ :call ClearRegister(v:register) -noremap HighlightRegistry_ActivateRegister - \ :call ActivateRegister(v:register) - -" Miscellaneous Mappings -noremap HighlightRegistry_CountLastSeen - \ :call CountLastSeen() - -" Default Mappings -nmap & HighlightRegistry_AppendToSearch -nmap * :silent norm! *& -nmap # :silent norm! #& - -nmap g& HighlightRegistry_GlobalAppendToSearch -nmap g* :silent norm! *g& -nmap g# :silent norm! #g& - -nmap y& HighlightRegistry_ActivateRegister -nmap d& HighlightRegistry_RemoveFromSearch -nmap c& HighlightRegistry_ClearRegister - -vmap & HighlightRegistry_VisualAppendToSearch'< -vmap * &nHighlightRegistry_CountLastSeen -vmap # &NHighlightRegistry_CountLastSeen - -vmap d& HighlightRegistry_VisualRemoveFromSearch'< +for key in keys(g:highlight_registry) + call highlight#init_register(key, g:highlight_registry[key]) +endfor +call highlight#append_to_search(v:register, @/)