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 " Note group names are not allowed to have special characters; they
" must be alphanumeric or underscores. " must be alphanumeric or underscores.
function! highlight#get_group_name() function! highlight#get_group_name(reg)
return 'highlight_registry_' . char2nr(v:register) return 'highlight_registry_' . char2nr(a:reg)
endfunction endfunction
@ -44,10 +44,10 @@ endfunction
" ====================================================================== " ======================================================================
" Setups the group and highlighting. Matches are added afterward. " Setups the group and highlighting. Matches are added afterward.
function! highlight#init_register(color) function! highlight#init_register(reg, color)
call highlight#clear_register() call highlight#clear_register(a:reg)
let s:registry_colors[v:register] = a:color let s:registry_colors[a:reg] = a:color
exe 'hi ' . highlight#get_group_name() . exe 'hi ' . highlight#get_group_name(a:reg) .
\ ' cterm=bold,underline ctermfg=' . a:color \ ' cterm=bold,underline ctermfg=' . a:color
endfunction endfunction
@ -57,19 +57,19 @@ endfunction
" Used to clear out the 'registers' that are used to hold which values are " Used to clear out the 'registers' that are used to hold which values are
" highlighted under a certain match group. " highlighted under a certain match group.
function! highlight#clear_register() function! highlight#clear_register(reg)
exe 'hi clear ' . highlight#get_group_name() exe 'hi clear ' . highlight#get_group_name(a:reg)
if has_key(s:registry_colors, v:register) if has_key(s:registry_colors, a:reg)
unlet s:registry_colors[v:register] unlet s:registry_colors[a:reg]
endif endif
if has_key(s:registry, v:register) if has_key(s:registry, a:reg)
for key in keys(s:registry[v:register]) for key in keys(s:registry[a:reg])
call matchdelete(s:registry[v:register][key]) call matchdelete(s:registry[a:reg][key])
unlet s:registry[v:register][key] unlet s:registry[a:reg][key]
endfor endfor
unlet s:registry[v:register] unlet s:registry[a:reg]
endif endif
call highlight#activate_register() call highlight#activate_register(a:reg)
endfunction endfunction
@ -79,7 +79,6 @@ endfunction
function! highlight#count_last_seen() function! highlight#count_last_seen()
if len(@/) > 0 if len(@/) > 0
let pos = getpos('.') let pos = getpos('.')
let pos[2] = pos[2] - 1
exe ' %s/' . s:last_seen . '//gne' exe ' %s/' . s:last_seen . '//gne'
call setpos('.', pos) call setpos('.', pos)
endif endif
@ -90,15 +89,15 @@ endfunction
" ====================================================================== " ======================================================================
" We must actively set the search register to perform searches as expected. " We must actively set the search register to perform searches as expected.
function! highlight#activate_register() function! highlight#activate_register(reg)
if has_key(s:registry, v:register) && has_key(s:registry_colors, v:register) if has_key(s:registry, a:reg) && has_key(s:registry_colors, a:reg)
let search = '' let search = ''
for key in keys(s:registry[v:register]) for key in keys(s:registry[a:reg])
let search = search . key . '\|' let search = search . key . '\|'
endfor endfor
let @/ = search[:-3] let @/ = search[:-3]
exe 'hi Search cterm=bold,underline ctermbg=none ctermfg=' . exe 'hi Search cterm=bold,underline ctermbg=none ctermfg=' .
\ s:registry_colors[v:register] \ s:registry_colors[a:reg]
set hlsearch set hlsearch
else else
let @/ = '' let @/ = ''
@ -106,25 +105,26 @@ function! highlight#activate_register()
endfunction 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 let s:last_seen = a:pattern
if len(a:pattern) > 0 if len(a:pattern) == 0
if !has_key(s:registry_colors, v:register) return
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 endif
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[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 endfunction
@ -144,16 +144,16 @@ endfunction
" FUNCTION: RemoveFromSearch(pattern) {{{1 " FUNCTION: RemoveFromSearch(pattern) {{{1
" ====================================================================== " ======================================================================
function! highlight#remove_from_search(pattern) function! highlight#remove_from_search(reg, pattern)
if has_key(s:registry, v:register) if has_key(s:registry, a:reg)
if has_key(s:registry[v:register], a:pattern) if has_key(s:registry[a:reg], a:pattern)
call matchdelete(s:registry[v:register][a:pattern]) call matchdelete(s:registry[a:reg][a:pattern])
unlet s:registry[v:register][a:pattern] unlet s:registry[a:reg][a:pattern]
if len(s:registry[v:register]) == 0 if len(s:registry[a:reg]) == 0
unlet s:registry[v:register] unlet s:registry[a:reg]
endif endif
endif endif
endif endif
call highlight#activate_register() call highlight#activate_register(a:reg)
endfunction endfunction

View File

@ -25,16 +25,16 @@ endif
" ---------------------------------------------------------------------- " ----------------------------------------------------------------------
if !exists('g:highlight_registry') if !exists('g:highlight_registry')
let g:highlight_registry = { '0' : 'Yellow' let g:highlight_registry = { '0' : 'Yellow',
\ '1' : 'Blue' \ '1' : 'Blue',
\ '2' : 'Red' \ '2' : 'Red',
\ '3' : 'Magenta' \ '3' : 'Magenta',
\ '4' : 'Green' \ '4' : 'Green',
\ '5' : 'Cyan' \ '5' : 'Cyan',
\ '6' : 'DarkYellow' \ '6' : 'DarkYellow',
\ '7' : 'White' \ '7' : 'White',
\ '8' : 'Gray' \ '8' : 'Gray',
\ '9' : 'Black' \ '9' : 'Black',
\ } \ }
endif endif
@ -44,44 +44,47 @@ endif
" Append Searches " Append Searches
noremap <Plug>HRegistry_AppendToSearch noremap <Plug>HRegistry_AppendToSearch
\ :call highlight#append_to_search('\<'.expand('<cword>').'\>')<CR> \ :call highlight#append_to_search(v:register, '\<'.expand('<cword>').'\>')<Bar>
\ :call highlight#count_last_seen()<CR> \ call highlight#count_last_seen()<CR>
noremap <Plug>HRegistry_GlobalAppendToSearch noremap <Plug>HRegistry_GlobalAppendToSearch
\ :call highlight#append_to_search(expand('<cword>'))<CR> \ :call highlight#append_to_search(v:register, expand('<cword>'))<Bar>
\ :call highlight#count_last_seen()<CR> \ call highlight#count_last_seen()<CR>
noremap <Plug>HRegistry_VisualAppendToSearch noremap <Plug>HRegistry_VisualAppendToSearch
\ :call highlight#append_to_search(highlight#get_visual_selection())<CR> \ :call highlight#append_to_search(v:register, highlight#get_visual_selection())<Bar>
\ :call highlight#count_last_seen()<CR>'< \ call highlight#count_last_seen()<CR>
" Remove Searches " Remove Searches
noremap <Plug>HRegistry_RemoveFromSearch 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 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 " Other Modifications
noremap <Plug>HRegistry_ClearRegister :call highlight#clear_register()<CR> noremap <Plug>HRegistry_ClearRegister
noremap <Plug>HRegistry_ActivateRegister :call highlight#activate_register()<CR> \ :call highlight#clear_register(v:register)<CR>
noremap <Plug>HRegistry_CountLastSeen :call highlight#count_last_seen()<CR> noremap <Plug>HRegistry_ActivateRegister
\ :call highlight#activate_register(v:register)<CR>
noremap <Plug>HRegistry_CountLastSeen
\ :call highlight#count_last_seen()<CR>
" Normal Mappings " Normal Mappings
nmap & <Plug>HRegistry_AppendToSearch nmap <silent> & <Plug>HRegistry_AppendToSearch
nmap g& <Plug>HRegistry_GlobalAppendToSearch nmap <silent> g& <Plug>HRegistry_GlobalAppendToSearch
nmap y& <Plug>HRegistry_ActivateRegister nmap <silent> y& <Plug>HRegistry_ActivateRegister
nmap d& <Plug>HRegistry_RemoveFromSearch nmap <silent> d& <Plug>HRegistry_RemoveFromSearch
nmap c& <Plug>HRegistry_ClearRegister nmap <silent> c& <Plug>HRegistry_ClearRegister
nmap * :silent norm! *<CR>& nmap <silent> * :silent norm! *<CR>&
nmap g* :silent norm! *<CR>g& nmap <silent> g* :silent norm! *<CR>g&
nmap # :silent norm! #<CR>& nmap <silent> # :silent norm! #<CR>&
nmap g# :silent norm! #<CR>g& nmap <silent> g# :silent norm! #<CR>g&
" Visual Mappings " Visual Mappings
vmap & <Plug>HRegistry_VisualAppendToSearch vmap <silent> & <Plug>HRegistry_VisualAppendToSearch'<
vmap d& <Plug>HRegistry_VisualRemoveFromSearch vmap <silent> d& <Plug>HRegistry_VisualRemoveFromSearch'<
vmap * &n<Plug>HRegistry_CountLastSeen vmap <silent> * &n<Plug>HRegistry_CountLastSeen
vmap # &N<Plug>HRegistry_CountLastSeen vmap <silent> # &N<Plug>HRegistry_CountLastSeen
" PROCEDURE: Initialize {{{1 " PROCEDURE: Initialize {{{1