1
Fork 0

Correct mappings

master
Joshua Potter 2016-11-18 15:38:01 -08:00
parent 0af5873a04
commit dbee0ab483
2 changed files with 81 additions and 78 deletions

View File

@ -35,8 +35,8 @@ let s:registry = {}
" 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)
function! highlight#get_group_name(reg)
return 'highlight_registry_' . char2nr(a:reg)
endfunction
@ -44,10 +44,10 @@ endfunction
" ======================================================================
" 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() .
function! highlight#init_register(reg, color)
call highlight#clear_register(a:reg)
let s:registry_colors[a:reg] = a:color
exe 'hi ' . highlight#get_group_name(a:reg) .
\ ' cterm=bold,underline ctermfg=' . a:color
endfunction
@ -57,19 +57,19 @@ endfunction
" 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]
function! highlight#clear_register(reg)
exe 'hi clear ' . highlight#get_group_name(a:reg)
if has_key(s:registry_colors, a:reg)
unlet s:registry_colors[a:reg]
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]
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[v:register]
unlet s:registry[a:reg]
endif
call highlight#activate_register()
call highlight#activate_register(a:reg)
endfunction
@ -79,7 +79,6 @@ endfunction
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
@ -90,15 +89,15 @@ endfunction
" ======================================================================
" 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)
function! highlight#activate_register(reg)
if has_key(s:registry, a:reg) && has_key(s:registry_colors, a:reg)
let search = ''
for key in keys(s:registry[v:register])
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[v:register]
\ s:registry_colors[a:reg]
set hlsearch
else
let @/ = ''
@ -106,25 +105,26 @@ function! highlight#activate_register()
endfunction
" FUNCTION: AppendToSearch(pattern, ...) {{{1
" FUNCTION: AppendToSearch(pattern) {{{1
" ======================================================================
function! highlight#append_to_search(pattern)
function! highlight#append_to_search(reg, 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)
if len(a:pattern) == 0
return
endif
if !has_key(s:registry, v:register)
let s:registry[v:register] = {}
if !has_key(s:registry_colors, a:reg)
call highlight#init_register(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[v:register], a:pattern)
let s:registry[v:register][a:pattern] =
\ matchadd(highlight#get_group_name(), a:pattern)
endif
call highlight#activate_register()
if !has_key(s:registry[a:reg], a:pattern)
let s:registry[a:reg][a:pattern] =
\ matchadd(highlight#get_group_name(a:reg), a:pattern)
endif
call highlight#activate_register(a:reg)
endfunction
@ -144,16 +144,16 @@ 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]
function! highlight#remove_from_search(reg, pattern)
if has_key(s:registry, a:reg)
if 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
endif
call highlight#activate_register()
call highlight#activate_register(a:reg)
endfunction

View File

@ -25,16 +25,16 @@ endif
" ----------------------------------------------------------------------
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'
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
@ -44,44 +44,47 @@ endif
" Append Searches
noremap <Plug>HRegistry_AppendToSearch
\ :call highlight#append_to_search('\<'.expand('<cword>').'\>')<CR>
\ :call highlight#count_last_seen()<CR>
\ :call highlight#append_to_search(v:register, '\<'.expand('<cword>').'\>')<Bar>
\ call highlight#count_last_seen()<CR>
noremap <Plug>HRegistry_GlobalAppendToSearch
\ :call highlight#append_to_search(expand('<cword>'))<CR>
\ :call highlight#count_last_seen()<CR>
\ :call highlight#append_to_search(v:register, expand('<cword>'))<Bar>
\ call highlight#count_last_seen()<CR>
noremap <Plug>HRegistry_VisualAppendToSearch
\ :call highlight#append_to_search(highlight#get_visual_selection())<CR>
\ :call highlight#count_last_seen()<CR>'<
\ :call highlight#append_to_search(v:register, highlight#get_visual_selection())<Bar>
\ call highlight#count_last_seen()<CR>
" Remove Searches
noremap <Plug>HRegistry_RemoveFromSearch
\ :call highlight#remove_from_search('\<'.expand('<cword>').'\>')<CR>
\ :call highlight#remove_from_search(v:register, '\<'.expand('<cword>').'\>')<CR>
noremap <Plug>HRegistry_VisualRemoveFromSearch
\ :call highlight#remove_from_search(highlight#get_visual_selection())<CR>'<
\ :call highlight#remove_from_search(v:register, highlight#get_visual_selection())<CR>
" Other Modifications
noremap <Plug>HRegistry_ClearRegister :call highlight#clear_register()<CR>
noremap <Plug>HRegistry_ActivateRegister :call highlight#activate_register()<CR>
noremap <Plug>HRegistry_CountLastSeen :call highlight#count_last_seen()<CR>
noremap <Plug>HRegistry_ClearRegister
\ :call highlight#clear_register(v:register)<CR>
noremap <Plug>HRegistry_ActivateRegister
\ :call highlight#activate_register(v:register)<CR>
noremap <Plug>HRegistry_CountLastSeen
\ :call highlight#count_last_seen()<CR>
" Normal Mappings
nmap & <Plug>HRegistry_AppendToSearch
nmap g& <Plug>HRegistry_GlobalAppendToSearch
nmap y& <Plug>HRegistry_ActivateRegister
nmap d& <Plug>HRegistry_RemoveFromSearch
nmap c& <Plug>HRegistry_ClearRegister
nmap <silent> & <Plug>HRegistry_AppendToSearch
nmap <silent> g& <Plug>HRegistry_GlobalAppendToSearch
nmap <silent> y& <Plug>HRegistry_ActivateRegister
nmap <silent> d& <Plug>HRegistry_RemoveFromSearch
nmap <silent> c& <Plug>HRegistry_ClearRegister
nmap * :silent norm! *<CR>&
nmap g* :silent norm! *<CR>g&
nmap <silent> * :silent norm! *<CR>&
nmap <silent> g* :silent norm! *<CR>g&
nmap # :silent norm! #<CR>&
nmap g# :silent norm! #<CR>g&
nmap <silent> # :silent norm! #<CR>&
nmap <silent> g# :silent norm! #<CR>g&
" Visual Mappings
vmap & <Plug>HRegistry_VisualAppendToSearch
vmap d& <Plug>HRegistry_VisualRemoveFromSearch
vmap * &n<Plug>HRegistry_CountLastSeen
vmap # &N<Plug>HRegistry_CountLastSeen
vmap <silent> & <Plug>HRegistry_VisualAppendToSearch'<
vmap <silent> d& <Plug>HRegistry_VisualRemoveFromSearch'<
vmap <silent> * &n<Plug>HRegistry_CountLastSeen
vmap <silent> # &N<Plug>HRegistry_CountLastSeen
" PROCEDURE: Initialize {{{1