-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate_visual_highlight_color.vim
98 lines (81 loc) · 3.08 KB
/
update_visual_highlight_color.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
" update_visual_highlight_color.vim
"
" Experimental script to change the 'Visual' highlight group to different
" values depending on whether we're in 'Select' mode or not.
"
" To use, copy this script to ~/.vim and add this line to your .vimrc:
"
" runtime update_visual_highlight_color.vim
"
" Notes:
"
" At the moment, this script uses a 'statusline' function to work its magic,
" but that may not always be the case. A status line function is executed
" after nearly every user-visible Vim action, and I'm only using it because I
" haven't yet found a great combination of autocommands to provide the same
" capability. It has a few annoying drawbacks, one of which is that a
" status line function is only executed when there's a status line visible in
" the current window. `:set laststatus=2` to make sure.
"
"
" TODO:
" - Build the default highlighting groups from the the existing Visual
" highlighting group.
" - Consider converting to a plugin, with real documentation.
" - Handle Visual Block mode.
" - Complete the definitions of the default highlighting groups.
"
" Define default highlighting groups used for specific Visual sub-modes.
" (These can be overridden within a colorscheme.)
"
highlight default VisualOriginal guifg=#3c6182 guibg=White
highlight default Select guifg=White guibg=#3c6182
if hlexists('Visual')
" XXX: Left off here, working out how best to copy
" the Visual highlight definition into VisualOriginal.
endif
function! UpdateVisualHighlightColor()
" Grab the short version of the current mode,
" and convert it to uppercase so we don't have to
" worry about the characterwise vs. linewise sub-modes.
"
" XXX: This doesn't (yet) handle Visual Block mode.
"
let l:m = toupper(mode(0))
" Link the Visual highlighting to the appropriate highlighting group
" based on the current mode.
"
if l:m ==# "S"
highlight! link Visual Select
redraw
elseif l:m ==# "V"
highlight! link Visual VisualOriginal
redraw
endif
" Don't contribute anything to the statusline. This
" is all about the side effects.
"
return ""
endfunction
""
"" Initialization and Setup
""
" If 'statusline' isn't defined, define one that's reasonably close to the Vim
" default, to avoid displaying a blank status line.
"
" Here's why this is necessary: If you leave 'statusline' at its default empty
" value, Vim displays a reasonable default status line. Once
" UpdateVisualHighlightColor() is added, however, 'statusline' is no longer
" empty, so Vim uses the value the function returns -- that is, the empty
" string -- as the status line text. Setting 'statusline' to the value below
" (a reasonable approximation of Vim's default) before appending
" UpdateVisualHighlightColor() solves the problem.
"
if &statusline == ''
set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P0
endif
" Add the function to 'statusline', so the highlight color
" can (potentially) be updated whenever the status line changes.
"
set statusline+=%{UpdateVisualHighlightColor()}
" end update_visual_highlight_color.vim