-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoggle_highlights.vim
85 lines (73 loc) · 2.91 KB
/
toggle_highlights.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
" toggle_highlights.vim
"
function! ToggleHighlights()
"
" Toggles visibility of search highlighting, 'list' characters (tabs, trailing
" spaces, newlines, etc.), cursor line/column, and the color column.
"
" Note: Search highlighting is a little odd, since (as far as I can tell)
" there's no way to determine if search highlighting is actually visible or
" not. You can check the 'hlsearch' setting, but that doesn't tell you if the
" user has executed the :nohlsearch command, which is what I really need to
" know.
"
" To deal with this, the first time this function is called, it acts as if the
" 'highlights are visible' flag is already true, indicating that search
" highlighting is on, since that will usually be the case. (The user will
" bounce on the toggle to turn off highlighting after a search.)
"
" Speaking of :nohlsearch... it doesn't accomplish much inside a function,
" since the highlighting state is restored when the function exits. To deal
" with this, the function doesn't attempt to run the command that affects
" search highlighting; instead, it *returns* the command as a string to the
" caller, so the caller can do this (usually inside a keymap)...
"
" :execute ToggleHighlights()
"
" ...and the effect will be that highlighting will be toggled appropriately.
" (Yes, this is hackish and weird, but I haven't yet come up with anything
" better.)
"
" XXX: This is still broken, or not-quite-unbroken-enough to make me happy.
" It doesn't even quite work the way I've described it above. :-/
"
"
" Flip the flag, or set it if it doesn't yet exist.
"
" Note that the flag starts off true (highlights are visible), since most
" of the time that this function is first called, it will be because the
" user wants to to turn *off* highlighting. Pretending it's already on
" when first run does a halfway-decent job of this.
"
let w:highlights_visible =
\ exists('w:highlights_visible') ? !w:highlights_visible : 1
let l:hlsearch_command = ''
" Change highlight visibility according to the state of the flag.
"
if w:highlights_visible " ...then turn them off
" Provide the command that the caller should use to temporarily
" turn off search highlighting.
"
let l:hlsearch_command = 'nohlsearch'
match none
set nolist
set nocursorline
set nocursorcolumn
set colorcolumn=
else " ...turn the highlights on
" Provide the command that the caller should use to (re)activate
" search highlighting. This will have no effect if 'hlsearch' is
" not set.
"
let l:hlsearch_command = 'let &hls = &hls'
set list
set cursorline
set cursorcolumn
set colorcolumn=80,96,132
endif
" Return the command that should be executed in order to
" affect search highlighting.
"
return l:hlsearch_command
endfunction
" end toggle_highlights.vim