2016-11-18 23:11:12 +00:00
|
|
|
" ======================================================================
|
|
|
|
" File: highlight.vim
|
|
|
|
" Maintainer: Joshua Potter <jrpotter2112@gmail.com>
|
|
|
|
"
|
|
|
|
" ======================================================================
|
|
|
|
|
|
|
|
" SCRIPT VARIABLES:
|
|
|
|
" ======================================================================
|
|
|
|
|
2017-05-17 06:55:52 +00:00
|
|
|
" s:last_pattern_seen :: String {{{2
|
2016-11-18 23:11:12 +00:00
|
|
|
" ----------------------------------------------------------------------
|
|
|
|
" The pattern last appended to a registry list.
|
|
|
|
|
2017-05-17 06:55:52 +00:00
|
|
|
let s:last_pattern_seen = @/
|
2016-11-18 23:11:12 +00:00
|
|
|
|
|
|
|
|
2017-05-17 06:55:52 +00:00
|
|
|
" s:active_register :: String {{{2
|
2016-11-18 23:11:12 +00:00
|
|
|
" ----------------------------------------------------------------------
|
2017-05-17 06:55:52 +00:00
|
|
|
" The register currently active. This defaults to the unnamed register.
|
2016-11-18 23:11:12 +00:00
|
|
|
|
2017-05-17 06:55:52 +00:00
|
|
|
let s:active_register = "\""
|
2016-11-18 23:11:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
" s:registry :: { String : { String : Match } } {{{2
|
|
|
|
" ----------------------------------------------------------------------
|
|
|
|
" Name of register corresponding to a dict of some unique identifier of the
|
|
|
|
" word being matched, paired with the actual match object.
|
|
|
|
|
|
|
|
let s:registry = {}
|
|
|
|
|
|
|
|
|
2017-05-17 06:55:52 +00:00
|
|
|
" FUNCTION: Statusline() {{{1
|
|
|
|
" ======================================================================
|
|
|
|
" Allow for integrating the currently highlighted section into the statusline.
|
|
|
|
" If airline is found, synchronize the accent with the highlighting.
|
|
|
|
" Can use as follows:
|
|
|
|
" call airline#parts#define_function('foo', 'highlight#airline_status')
|
|
|
|
" 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'])
|
|
|
|
|
|
|
|
function! highlight#statusline(...)
|
|
|
|
let l:group_name = highlight#get_group_name(s:active_register)
|
|
|
|
" If airline is defined, this function should be called in the context of
|
|
|
|
" airline#parts#define_function('foo', 'highlight#airline_status'). Thus it
|
|
|
|
" should be sufficient to check that airline#parts#define_accent exists to
|
|
|
|
" ensure airline is defined.
|
|
|
|
if a:0 > 0 && exists('*airline#parts#define_accent')
|
|
|
|
call airline#parts#define_accent(a:1, l:group_name)
|
|
|
|
return airline#section#create_right([a:1])
|
|
|
|
else
|
|
|
|
return '%#' . l:group_name . '#xxx (" . s:active_register . ")%*'
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2016-11-18 23:11:12 +00:00
|
|
|
" FUNCTION: GetGroupName(reg) {{{1
|
|
|
|
" ======================================================================
|
|
|
|
" Note group names are not allowed to have special characters; they
|
|
|
|
" must be alphanumeric or underscores.
|
|
|
|
|
2016-11-18 23:38:01 +00:00
|
|
|
function! highlight#get_group_name(reg)
|
|
|
|
return 'highlight_registry_' . char2nr(a:reg)
|
2016-11-18 23:11:12 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
" FUNCTION: InitRegister() {{{1
|
|
|
|
" ======================================================================
|
|
|
|
" Setups the group and highlighting. Matches are added afterward.
|
|
|
|
|
2016-11-18 23:38:01 +00:00
|
|
|
function! highlight#init_register(reg, color)
|
|
|
|
call highlight#clear_register(a:reg)
|
|
|
|
exe 'hi ' . highlight#get_group_name(a:reg) .
|
2016-11-18 23:11:12 +00:00
|
|
|
\ ' cterm=bold,underline ctermfg=' . a:color
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
" FUNCTION: ClearRegister() {{{1
|
|
|
|
" ======================================================================
|
|
|
|
" Used to clear out the 'registers' that are used to hold which values are
|
|
|
|
" highlighted under a certain match group.
|
|
|
|
|
2016-11-18 23:38:01 +00:00
|
|
|
function! highlight#clear_register(reg)
|
|
|
|
exe 'hi clear ' . highlight#get_group_name(a:reg)
|
|
|
|
if has_key(s:registry, a:reg)
|
|
|
|
for key in keys(s:registry[a:reg])
|
2017-05-17 06:55:52 +00:00
|
|
|
silent! call matchdelete(s:registry[a:reg][key])
|
2016-11-18 23:38:01 +00:00
|
|
|
unlet s:registry[a:reg][key]
|
2016-11-18 23:11:12 +00:00
|
|
|
endfor
|
2016-11-18 23:38:01 +00:00
|
|
|
unlet s:registry[a:reg]
|
2016-11-18 23:11:12 +00:00
|
|
|
endif
|
2016-11-18 23:38:01 +00:00
|
|
|
call highlight#activate_register(a:reg)
|
2016-11-18 23:11:12 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2016-12-16 16:21:40 +00:00
|
|
|
" FUNCTION: ClearAllRegisters() {{{1
|
|
|
|
" ======================================================================
|
|
|
|
|
|
|
|
function! highlight#clear_all_registers()
|
|
|
|
for key in keys(g:highlight_registry)
|
|
|
|
call highlight#init_register(key, g:highlight_registry[key])
|
|
|
|
endfor
|
|
|
|
for key in keys(s:registry)
|
|
|
|
if !has_key(g:highlight_registry, key)
|
|
|
|
call highlight#clear_register(key)
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2016-11-18 23:11:12 +00:00
|
|
|
" FUNCTION: CountLastSeen() {{{1
|
|
|
|
" ======================================================================
|
|
|
|
|
2017-05-17 06:55:52 +00:00
|
|
|
function! highlight#count_last_pattern_seen()
|
2016-11-18 23:11:12 +00:00
|
|
|
if len(@/) > 0
|
|
|
|
let pos = getpos('.')
|
2017-05-17 06:55:52 +00:00
|
|
|
exe ' %s/' . s:last_pattern_seen . '//gne'
|
2016-11-18 23:11:12 +00:00
|
|
|
call setpos('.', pos)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
" FUNCTION: ActivateRegister() {{{1
|
|
|
|
" ======================================================================
|
|
|
|
" We must actively set the search register to perform searches as expected.
|
|
|
|
|
2016-11-18 23:38:01 +00:00
|
|
|
function! highlight#activate_register(reg)
|
2017-05-17 06:55:52 +00:00
|
|
|
let s:active_register = a:reg
|
|
|
|
if has_key(s:registry, a:reg)
|
2016-11-18 23:11:12 +00:00
|
|
|
let search = ''
|
2016-11-18 23:38:01 +00:00
|
|
|
for key in keys(s:registry[a:reg])
|
2016-11-18 23:11:12 +00:00
|
|
|
let search = search . key . '\|'
|
|
|
|
endfor
|
|
|
|
let @/ = search[:-3]
|
2017-05-17 06:55:52 +00:00
|
|
|
exe 'hi! link Search ' . highlight#get_group_name(a:reg)
|
2016-11-18 23:11:12 +00:00
|
|
|
set hlsearch
|
|
|
|
else
|
|
|
|
let @/ = ''
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2016-11-18 23:38:01 +00:00
|
|
|
" FUNCTION: AppendToSearch(pattern) {{{1
|
2016-11-18 23:11:12 +00:00
|
|
|
" ======================================================================
|
|
|
|
|
2016-11-18 23:38:01 +00:00
|
|
|
function! highlight#append_to_search(reg, pattern)
|
2017-05-17 06:55:52 +00:00
|
|
|
let s:last_pattern_seen = a:pattern
|
2016-11-18 23:38:01 +00:00
|
|
|
if len(a:pattern) == 0
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
if !has_key(s:registry, a:reg)
|
2017-05-17 06:55:52 +00:00
|
|
|
" TODO(jrpotter): Change to one of least used color.
|
|
|
|
call highlight#init_register(a:reg, 'Yellow')
|
2016-11-18 23:38:01 +00:00
|
|
|
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)
|
2016-11-18 23:11:12 +00:00
|
|
|
endif
|
2016-11-18 23:38:01 +00:00
|
|
|
call highlight#activate_register(a:reg)
|
2016-11-18 23:11:12 +00:00
|
|
|
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
|
|
|
|
" ======================================================================
|
|
|
|
|
2016-11-18 23:38:01 +00:00
|
|
|
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]
|
2016-11-18 23:11:12 +00:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
2016-11-18 23:38:01 +00:00
|
|
|
call highlight#activate_register(a:reg)
|
2016-11-18 23:11:12 +00:00
|
|
|
endfunction
|
|
|
|
|