Setup to use flags to avoid repetition
parent
e8708d3a96
commit
8aa7e75926
|
@ -1,35 +1,88 @@
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
" File: highlight.vim
|
" File: highlight.vim
|
||||||
" Maintainer: Joshua Potter <jrpotter2112@gmail.com>
|
" Maintainer: Joshua Potter <jrpotter2112@gmail.com>
|
||||||
"
|
"
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
" SCRIPT VARIABLES:
|
" SCRIPT VARIABLES:
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
" s:active_register :: String {{{2
|
" s:active_register :: String {{{2
|
||||||
" ----------------------------------------------------------------------
|
" ------------------------------------------------------------------------------
|
||||||
" The register currently active. This defaults to the unnamed register.
|
" The register currently active. This defaults to the unnamed register.
|
||||||
|
|
||||||
let s:active_register = "\""
|
let s:active_register = "\""
|
||||||
|
|
||||||
|
|
||||||
" s:registry :: { String : { String : Match } } {{{2
|
" s:registry :: { String : { String : Match } } {{{2
|
||||||
" ----------------------------------------------------------------------
|
" ------------------------------------------------------------------------------
|
||||||
" Name of register corresponding to a dict of some unique identifier of the
|
" The keys of the outer dictionary are any active registers (that is, before a
|
||||||
" word being matched, paired with the actual match object.
|
" call to clear register is called). By default, this will be set to be
|
||||||
|
" populated with at least g:highlight_registry once the plugin is loaded.
|
||||||
|
"
|
||||||
|
" The corresponding values of the outer dictionary is a key value pairing of a
|
||||||
|
" matched identifier and the Match object corresponding to it. We must keep
|
||||||
|
" track of match objects as they must be deleted manually by matchdelete.
|
||||||
|
|
||||||
let s:registry = {}
|
let s:registry = {}
|
||||||
|
|
||||||
|
|
||||||
|
" 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.
|
||||||
|
|
||||||
|
function! highlight#expand_flag(a: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
|
||||||
|
" occurred in the current buffer.
|
||||||
|
|
||||||
|
function! highlight#count_pattern(flag)
|
||||||
|
let l:pattern = highlight#expand_flag(a:flag)
|
||||||
|
if len(@/) > 0
|
||||||
|
let pos = getpos('.')
|
||||||
|
exe ' %s/' . l:pattern . '//gne'
|
||||||
|
call setpos('.', pos)
|
||||||
|
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: Statusline() {{{1
|
" FUNCTION: Statusline() {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
" Allow for integrating the currently highlighted section into the statusline.
|
" Allow for integrating the currently highlighted section into the statusline.
|
||||||
" If airline is found, synchronize the accent with the highlighting.
|
" If airline is found, synchronize the accent with the highlighting.
|
||||||
" Can use as follows:
|
" Can use as follows:
|
||||||
" call airline#parts#define_function('foo', 'highlight#airline_status')
|
" call airline#parts#define_function('foo', 'highlight#airline_status()')
|
||||||
" call airline#parts#define_minwidth('foo', 50)
|
" call airline#parts#define_minwidth('foo', 50)
|
||||||
" call airline#parts#define_condition('foo', 'getcwd() =~ "work_dir"')
|
|
||||||
" let g:airline_section_y = airline#section#create_right(['ffenc', 'foo'])
|
" let g:airline_section_y = airline#section#create_right(['ffenc', 'foo'])
|
||||||
|
|
||||||
function! highlight#statusline(...)
|
function! highlight#statusline(...)
|
||||||
|
@ -48,7 +101,7 @@ endfunction
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: GetGroupName(reg) {{{1
|
" FUNCTION: GetGroupName(reg) {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
" 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.
|
||||||
|
|
||||||
|
@ -58,7 +111,7 @@ endfunction
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: InitRegister() {{{1
|
" FUNCTION: InitRegister() {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
" Setups the group and highlighting. Matches are added afterward.
|
" Setups the group and highlighting. Matches are added afterward.
|
||||||
|
|
||||||
function! highlight#init_register(reg, color)
|
function! highlight#init_register(reg, color)
|
||||||
|
@ -69,7 +122,7 @@ endfunction
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: ClearRegister() {{{1
|
" FUNCTION: ClearRegister() {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
" 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.
|
||||||
|
|
||||||
|
@ -87,7 +140,7 @@ endfunction
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: ClearAllRegisters() {{{1
|
" FUNCTION: ClearAllRegisters() {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
function! highlight#clear_all_registers()
|
function! highlight#clear_all_registers()
|
||||||
for key in keys(g:highlight_registry)
|
for key in keys(g:highlight_registry)
|
||||||
|
@ -101,20 +154,8 @@ function! highlight#clear_all_registers()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: CountPattern() {{{1
|
|
||||||
" ======================================================================
|
|
||||||
|
|
||||||
function! highlight#count_pattern(pattern)
|
|
||||||
if len(@/) > 0
|
|
||||||
let pos = getpos('.')
|
|
||||||
exe ' %s/' . a:pattern . '//gne'
|
|
||||||
call setpos('.', pos)
|
|
||||||
endif
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: ActivateRegister() {{{1
|
" FUNCTION: ActivateRegister() {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
" 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(reg)
|
function! highlight#activate_register(reg)
|
||||||
|
@ -134,7 +175,7 @@ endfunction
|
||||||
|
|
||||||
|
|
||||||
" FUNCTION: AppendToSearch(pattern) {{{1
|
" FUNCTION: AppendToSearch(pattern) {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
function! highlight#append_to_search(reg, pattern)
|
function! highlight#append_to_search(reg, pattern)
|
||||||
if len(a:pattern) == 0
|
if len(a:pattern) == 0
|
||||||
|
@ -154,21 +195,8 @@ function! highlight#append_to_search(reg, pattern)
|
||||||
endfunction
|
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: RemoveFromSearch(pattern) {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
function! highlight#remove_from_search(reg, pattern)
|
function! highlight#remove_from_search(reg, pattern)
|
||||||
if has_key(s:registry, a:reg)
|
if has_key(s:registry, a:reg)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
" File: highlight.vim
|
" File: highlight.vim
|
||||||
" Maintainer: Joshua Potter <jrpotter2112@gmail.com>
|
" Maintainer: Joshua Potter <jrpotter2112@gmail.com>
|
||||||
"
|
"
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
if exists('g:loaded_highlight_registry')
|
if exists('g:loaded_highlight_registry')
|
||||||
finish
|
finish
|
||||||
|
@ -11,10 +11,15 @@ let g:loaded_highlight_registry = 1
|
||||||
|
|
||||||
|
|
||||||
" GLOBAL VARIABLES:
|
" GLOBAL VARIABLES:
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
" g:highlight_registry :: { String : String } {{{2
|
" g:highlight_registry :: { String : String } {{{2
|
||||||
" ----------------------------------------------------------------------
|
" ------------------------------------------------------------------------------
|
||||||
|
" The following dictionary corresponds to registers 0-9 and their respective
|
||||||
|
" colors. Adjust this to set the colors for a given register. If a register
|
||||||
|
" isn't added to this dictionary before being attempted to be used, one of the
|
||||||
|
" least most colors will be chosen instead. See *cterm-colors*.
|
||||||
|
" TODO(jrpotter): Allow for better automatic color choices.
|
||||||
|
|
||||||
if !exists('g:highlight_registry')
|
if !exists('g:highlight_registry')
|
||||||
let g:highlight_registry = { '0' : 'Yellow',
|
let g:highlight_registry = { '0' : 'Yellow',
|
||||||
|
@ -32,18 +37,22 @@ endif
|
||||||
|
|
||||||
|
|
||||||
" MAPPINGS: {{{1
|
" MAPPINGS: {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
|
let s:word = 'expand("<cword>")'
|
||||||
|
let s:cword = '\<expand(<cword>)\>'
|
||||||
|
let s:vword = 'highlight#get_visual_selection()'
|
||||||
|
|
||||||
" Append Searches
|
" Append Searches
|
||||||
noremap <Plug>HRegistry_AppendToSearch
|
noremap <Plug>HRegistry_AppendToSearch
|
||||||
\ :call highlight#append_to_search(v:register, '\<'.expand('<cword>').'\>')<Bar>
|
\ :call highlight#append_to_search(v:register, 'c')<Bar>
|
||||||
\ call highlight#count_pattern('\<'.expand('<cword>').'\>')<CR>
|
\ call highlight#count_pattern(eval(s:cword))<CR>
|
||||||
noremap <Plug>HRegistry_GlobalAppendToSearch
|
noremap <Plug>HRegistry_GAppendToSearch
|
||||||
\ :call highlight#append_to_search(v:register, expand('<cword>'))<Bar>
|
\ :call highlight#append_to_search(v:register, 'g')<Bar>
|
||||||
\ call highlight#count_pattern(expand('<cword>'))<CR>
|
\ call highlight#count_pattern(eval(s:word))<CR>
|
||||||
noremap <Plug>HRegistry_VisualAppendToSearch
|
noremap <Plug>HRegistry_VisualAppendToSearch
|
||||||
\ :call highlight#append_to_search(v:register, highlight#get_visual_selection())<Bar>
|
\ :call highlight#append_to_search(v:register, 'v')<Bar>
|
||||||
\ call highlight#count_pattern(highlight#get_visual_selection())<CR>
|
\ call highlight#count_pattern(eval(s:vword))<CR>
|
||||||
|
|
||||||
" Remove Searches
|
" Remove Searches
|
||||||
noremap <Plug>HRegistry_RemoveFromSearch
|
noremap <Plug>HRegistry_RemoveFromSearch
|
||||||
|
@ -61,7 +70,7 @@ noremap <Plug>HRegistry_CountLastSeen
|
||||||
|
|
||||||
" Normal Mappings
|
" Normal Mappings
|
||||||
nmap <silent> & <Plug>HRegistry_AppendToSearch
|
nmap <silent> & <Plug>HRegistry_AppendToSearch
|
||||||
nmap <silent> g& <Plug>HRegistry_GlobalAppendToSearch
|
nmap <silent> g& <Plug>HRegistry_GAppendToSearch
|
||||||
nmap <silent> y& <Plug>HRegistry_ActivateRegister
|
nmap <silent> y& <Plug>HRegistry_ActivateRegister
|
||||||
nmap <silent> d& <Plug>HRegistry_RemoveFromSearch
|
nmap <silent> d& <Plug>HRegistry_RemoveFromSearch
|
||||||
nmap <silent> c& <Plug>HRegistry_ClearRegister
|
nmap <silent> c& <Plug>HRegistry_ClearRegister
|
||||||
|
@ -80,7 +89,7 @@ vmap <silent> # &N<Plug>HRegistry_CountLastSeen
|
||||||
|
|
||||||
|
|
||||||
" PROCEDURE: Commands {{1
|
" PROCEDURE: Commands {{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
function! s:ClearHighlightRegistry()
|
function! s:ClearHighlightRegistry()
|
||||||
call highlight#clear_all_registers()
|
call highlight#clear_all_registers()
|
||||||
|
@ -89,7 +98,7 @@ command ClearHighlightRegistry :call <SID>ClearHighlightRegistry()
|
||||||
|
|
||||||
|
|
||||||
" PROCEDURE: Initialize {{{1
|
" PROCEDURE: Initialize {{{1
|
||||||
" ======================================================================
|
" ==============================================================================
|
||||||
|
|
||||||
call s:ClearHighlightRegistry()
|
call s:ClearHighlightRegistry()
|
||||||
call highlight#append_to_search(v:register, @/)
|
call highlight#append_to_search(v:register, @/)
|
||||||
|
|
Loading…
Reference in New Issue