Setup reg/flag expansion in plugin
parent
1b1839092d
commit
eaac1fd4cd
|
@ -30,54 +30,15 @@ let s:active_register = "\""
|
||||||
let s:registry = {}
|
let s:registry = {}
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: ExpandRegister(reg) {{{1
|
" FUNCTION: CountPattern(pattern) {{{1
|
||||||
" ==============================================================================
|
|
||||||
" Convenience method to determine which register is being currently used.
|
|
||||||
" The unnamed register defaults to the last used register to avoid having to
|
|
||||||
" constantly prefix registration. This can be changed by setting the value of
|
|
||||||
" g:persist_unnamed_register to 1.
|
|
||||||
|
|
||||||
function! highlight#expand_register(reg)
|
|
||||||
if !g:persist_unnamed_register && a:reg ==# '"'
|
|
||||||
return s:active_register
|
|
||||||
endif
|
|
||||||
return a:reg
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: ExpandFlag(flag) {{{1
|
|
||||||
" ==============================================================================
|
|
||||||
" Convenience method used to make the mappings in plugin/highlight.vim a bit
|
|
||||||
" easier to read through. The passed flag can be:
|
|
||||||
"
|
|
||||||
" c: Indicates the current word, with word boundary.
|
|
||||||
" g: Indicates the current word, without word boundary.
|
|
||||||
" v: Indicates the current visual selection.
|
|
||||||
"
|
|
||||||
" Throws an error otherwise.
|
|
||||||
|
|
||||||
function! highlight#expand_flag(flag) abort
|
|
||||||
if a:flag ==# 'c'
|
|
||||||
return '\<' . expand('<cword>') . '\>'
|
|
||||||
elseif a:flag ==# 'g'
|
|
||||||
return expand('<cword>')
|
|
||||||
elseif a:flag ==# 'v'
|
|
||||||
return highlight#get_visual_selection()
|
|
||||||
endif
|
|
||||||
throw 'Could not expand passed flag: ' . a:flag
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: CountPattern(flag) {{{1
|
|
||||||
" ==============================================================================
|
" ==============================================================================
|
||||||
" Convenience method used to display the number of times the passed pattern has
|
" Convenience method used to display the number of times the passed pattern has
|
||||||
" occurred in the current buffer.
|
" occurred in the current buffer.
|
||||||
|
|
||||||
function! highlight#count_pattern(flag)
|
function! highlight#count_pattern(pattern)
|
||||||
let l:pattern = highlight#expand_flag(a:flag)
|
|
||||||
if len(@/) > 0
|
if len(@/) > 0
|
||||||
let pos = getpos('.')
|
let pos = getpos('.')
|
||||||
exe ' %s/' . l:pattern . '//gne'
|
exe ' %s/' . a:pattern . '//gne'
|
||||||
call setpos('.', pos)
|
call setpos('.', pos)
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
@ -224,23 +185,22 @@ function! highlight#activate_register(reg)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: AppendToSearch(reg, flag) {{{1
|
" FUNCTION: AppendToSearch(reg, pattern) {{{1
|
||||||
" ==============================================================================
|
" ==============================================================================
|
||||||
" Extends the current matches of h_register reg with the pattern found once flag
|
" Extends the current matches of h_register reg with the pattern found once flag
|
||||||
" is expanded. If the h_register specified has not yet been initialized, simply
|
" is expanded. If the h_register specified has not yet been initialized, simply
|
||||||
" create a new h_register and continue.
|
" create a new h_register and continue.
|
||||||
|
|
||||||
function! highlight#append_to_search(reg, flag)
|
function! highlight#append_to_search(reg, pattern)
|
||||||
let l:pattern = highlight#expand_flag(a:flag)
|
if len(a:pattern) > 0
|
||||||
if len(l:pattern) > 0
|
|
||||||
if !has_key(s:registry, a:reg)
|
if !has_key(s:registry, a:reg)
|
||||||
" TODO(jrpotter): Choose color better.
|
" TODO(jrpotter): Choose color better.
|
||||||
call highlight#init_register(a:reg, 'Yellow')
|
call highlight#init_register(a:reg, 'Yellow')
|
||||||
endif
|
endif
|
||||||
" Don't want to add multiple match objects into registry
|
" Don't want to add multiple match objects into registry
|
||||||
if !has_key(s:registry[a:reg], l:pattern)
|
if !has_key(s:registry[a:reg], a:pattern)
|
||||||
let s:registry[a:reg][l:pattern] =
|
let s:registry[a:reg][a:pattern] =
|
||||||
\ matchadd(highlight#get_group_name(a:reg), l:pattern)
|
\ matchadd(highlight#get_group_name(a:reg), a:pattern)
|
||||||
endif
|
endif
|
||||||
" Updates the search register
|
" Updates the search register
|
||||||
call highlight#activate_register(a:reg)
|
call highlight#activate_register(a:reg)
|
||||||
|
@ -248,20 +208,19 @@ function! highlight#append_to_search(reg, flag)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: RemoveFromSearch(reg, flag) {{{1
|
" FUNCTION: RemoveFromSearch(reg, pattern) {{{1
|
||||||
" ==============================================================================
|
" ==============================================================================
|
||||||
" Removes the given pattern found once flag is expanded from the passed
|
" Removes the given pattern found once flag is expanded from the passed
|
||||||
" h_register reg. If the h_register will be emptied as a result of this call,
|
" h_register reg. If the h_register will be emptied as a result of this call,
|
||||||
" instead delegating to clearing out the register instead.
|
" instead delegating to clearing out the register instead.
|
||||||
|
|
||||||
function! highlight#remove_from_search(reg, flag)
|
function! highlight#remove_from_search(reg, pattern)
|
||||||
let l:pattern = highlight#expand_flag(a:flag)
|
if has_key(s:registry, a:reg) && has_key(s:registry[a:reg], a:pattern)
|
||||||
if has_key(s:registry, a:reg) && has_key(s:registry[a:reg], l:pattern)
|
|
||||||
if len(s:registry[a:reg]) == 1
|
if len(s:registry[a:reg]) == 1
|
||||||
call highlight#clear_register(a:reg)
|
call highlight#clear_register(a:reg)
|
||||||
else
|
else
|
||||||
silent! call matchdelete(s:registry[a:reg][l:pattern])
|
silent! call matchdelete(s:registry[a:reg][a:pattern])
|
||||||
unlet s:registry[a:reg][l:pattern]
|
unlet s:registry[a:reg][a:pattern]
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
" Updates the search register
|
" Updates the search register
|
||||||
|
|
|
@ -55,34 +55,72 @@ if !exists('g:persist_unnamed_register')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
" FUNCTION: ExpandRegister(reg) {{{1
|
||||||
|
" ==============================================================================
|
||||||
|
" Convenience method to determine which register is being currently used.
|
||||||
|
" The unnamed register defaults to the last used register to avoid having to
|
||||||
|
" constantly prefix registration. This can be changed by setting the value of
|
||||||
|
" g:persist_unnamed_register to 1.
|
||||||
|
|
||||||
|
function! s:ExpandRegister(reg)
|
||||||
|
if !g:persist_unnamed_register && a:reg ==# '"'
|
||||||
|
return s:active_register
|
||||||
|
endif
|
||||||
|
return a:reg
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" FUNCTION: ExpandFlag(flag) {{{1
|
||||||
|
" ==============================================================================
|
||||||
|
" Convenience method used to make the mappings in plugin/highlight.vim a bit
|
||||||
|
" easier to read through. The passed flag can be:
|
||||||
|
"
|
||||||
|
" c: Indicates the current word, with word boundary.
|
||||||
|
" g: Indicates the current word, without word boundary.
|
||||||
|
" v: Indicates the current visual selection.
|
||||||
|
"
|
||||||
|
" Throws an error otherwise.
|
||||||
|
|
||||||
|
function! s:ExpandFlag(flag) abort
|
||||||
|
if a:flag ==# 'c'
|
||||||
|
return '\<' . expand('<cword>') . '\>'
|
||||||
|
elseif a:flag ==# 'g'
|
||||||
|
return expand('<cword>')
|
||||||
|
elseif a:flag ==# 'v'
|
||||||
|
return highlight#get_visual_selection()
|
||||||
|
endif
|
||||||
|
throw 'Could not expand passed flag: ' . a:flag
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" MAPPINGS: {{{1
|
" MAPPINGS: {{{1
|
||||||
" ==============================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
" Append Searches
|
" Append Searches
|
||||||
noremap <Plug>HRegistry_AppendToSearch
|
noremap <Plug>HRegistry_AppendToSearch
|
||||||
\ :call highlight#append_to_search(v:register, 'c')<Bar>
|
\ :call highlight#append_to_search(s:ExpandRegister(v:register), s:ExpandFlag('c'))<Bar>
|
||||||
\ call highlight#count_pattern('c')<CR>
|
\ call highlight#count_pattern(s:ExpandFlag('c'))<CR>
|
||||||
noremap <Plug>HRegistry_GAppendToSearch
|
noremap <Plug>HRegistry_GAppendToSearch
|
||||||
\ :call highlight#append_to_search(v:register, 'g')<Bar>
|
\ :call highlight#append_to_search(s:ExpandRegister(v:register), s:ExpandFlag('g'))<Bar>
|
||||||
\ call highlight#count_pattern('g')<CR>
|
\ call highlight#count_pattern(s:ExpandFlag('g'))<CR>
|
||||||
noremap <Plug>HRegistry_VisualAppendToSearch
|
noremap <Plug>HRegistry_VisualAppendToSearch
|
||||||
\ :call highlight#append_to_search(v:register, 'v')<Bar>
|
\ :call highlight#append_to_search(s:ExpandRegister(v:register), s:ExpandFlag('v'))<Bar>
|
||||||
\ call highlight#count_pattern('v')<CR>
|
\ call highlight#count_pattern(s:ExpandFlag('v'))<CR>
|
||||||
|
|
||||||
" Remove Searches
|
" Remove Searches
|
||||||
noremap <Plug>HRegistry_RemoveFromSearch
|
noremap <Plug>HRegistry_RemoveFromSearch
|
||||||
\ :call highlight#remove_from_search(v:register, 'c')<CR>
|
\ :call highlight#remove_from_search(s:ExpandRegister(v:register), s:ExpandFlag('c'))<CR>
|
||||||
noremap <Plug>HRegistry_VisualRemoveFromSearch
|
noremap <Plug>HRegistry_VisualRemoveFromSearch
|
||||||
\ :call highlight#remove_from_search(v:register, 'v')<CR>
|
\ :call highlight#remove_from_search(s:ExpandRegister(v:register), s:ExpandFlag('v'))<CR>
|
||||||
|
|
||||||
" Other Modifications
|
" Other Modifications
|
||||||
noremap <Plug>HRegistry_ClearRegister
|
noremap <Plug>HRegistry_ClearRegister
|
||||||
\ :call highlight#clear_register(v:register)<Bar>
|
\ :call highlight#clear_register(s:ExpandRegister(v:register))<Bar>
|
||||||
\ call highlight#activate_register(v:register)<CR>
|
\ call highlight#activate_register(s:ExpandRegister(v:register))<CR>
|
||||||
noremap <Plug>HRegistry_ActivateRegister
|
noremap <Plug>HRegistry_ActivateRegister
|
||||||
\ :call highlight#activate_register(v:register)<CR>
|
\ :call highlight#activate_register(s:ExpandRegister(v:register))<CR>
|
||||||
noremap <Plug>HRegistry_CountLastSeen
|
noremap <Plug>HRegistry_CountLastSeen
|
||||||
\ :call highlight#count_pattern('c')<CR>
|
\ :call highlight#count_pattern(s:ExpandFlag('c'))<CR>
|
||||||
|
|
||||||
" Normal Mappings
|
" Normal Mappings
|
||||||
nmap <silent> & <Plug>HRegistry_AppendToSearch
|
nmap <silent> & <Plug>HRegistry_AppendToSearch
|
||||||
|
|
Loading…
Reference in New Issue