diff --git a/plugin/hightlight.vim b/plugin/hightlight.vim index fa936b7..c8a4823 100644 --- a/plugin/hightlight.vim +++ b/plugin/hightlight.vim @@ -19,6 +19,13 @@ let g:highlight_register_default_color = 'Yellow' " 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 @@ -77,13 +84,13 @@ function! s:ClearRegister(reg) endfunction -" FUNCTION: CountOccurrences() {{{1 +" FUNCTION: CountLastSeen() {{{1 " ====================================================================== -function! s:CountOccurrences(pattern) +function! s:CountLastSeen() if len(@/) > 0 let pos = getpos('.') - exe ' %s/' . a:pattern . '//gne' + exe ' %s/' . s:last_seen . '//gne' call setpos('.', pos) endif endfunction @@ -112,6 +119,7 @@ endfunction " ====================================================================== 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) @@ -126,7 +134,19 @@ function! s:AppendToSearch(reg, pattern) endif call s:ActivateRegister(a:reg) endif - call s:CountOccurrences(a:pattern) +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 @@ -160,28 +180,48 @@ 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, @/) noremap HighlightRegistry_AppendToSearch \ :call AppendToSearch(v:register, '\<'.expand('').'\>') -noremap HighlightRegistry_Forward_AppendToSearch - \ :call AppendToSearch(v:register, '\<'.expand('').'\>') -noremap HighlightRegistry_Backward_AppendToSearch - \ :call AppendToSearch(v:register, '\<'.expand('').'\>') + \ :call CountLastSeen() noremap HighlightRegistry_RemoveFromSearch \ :call RemoveFromSearch(v:register, '\<'.expand('').'\>') + +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()) + noremap HighlightRegistry_ClearRegister \ :call ClearRegister(v:register) noremap HighlightRegistry_ActivateRegister \ :call ActivateRegister(v:register) +noremap HighlightRegistry_CountLastSeen + \ :call CountLastSeen() + " Basic Mappings nmap & HighlightRegistry_AppendToSearch -nmap * :silent norm! *HighlightRegistry_Forward_AppendToSearch -nmap # :silent norm! #HighlightRegistry_Backward_AppendToSearch +nmap * :silent norm! *& +nmap # :silent norm! #& + +nmap g& HighlightRegistry_GlobalAppendToSearch +nmap g* :silent norm! *g& +nmap g# :silent norm! #g& -" Additional Register Modifiers 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'< +