Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 281 lines (244 sloc) 9.044 kb
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
1 " Author: Nate Kane <nathanaelkane AT gmail DOT com>
2 " Homepage: http://github.com/nathanaelkane/vim-indent-guides
3
4 "
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
5 " Toggles the indent guides on and off.
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
6 "
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
7 function! indent_guides#toggle()
51d44e3 Nate Kane Refactored the usage of the indent_guides_matches variable
authored
8 call indent_guides#init_matches()
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
9
51d44e3 Nate Kane Refactored the usage of the indent_guides_matches variable
authored
10 if empty(w:indent_guides_matches)
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
11 call indent_guides#enable()
12 else
13 call indent_guides#disable()
14 endif
15 endfunction
16
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
17 "
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
18 " Called from autocmds, keeps indent guides enabled or disabled when entering
19 " other buffers and windows.
20 "
21 function! indent_guides#process_autocmds()
22 if g:indent_guides_autocmds_enabled
23 call indent_guides#enable()
24 else
25 call indent_guides#disable()
26 end
27 endfunction
28
29 "
30 " Enables the indent guides for the current buffer and any other buffer upon
31 " entering it.
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
32 "
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
33 function! indent_guides#enable()
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
34 let g:indent_guides_autocmds_enabled = 1
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
35
2903757 Nate Kane Disable the plugin when in a diff.
authored
36 if &diff || indent_guides#exclude_filetype()
997bede Nate Kane Add option g:indent_guides_exclude_filetypes.
authored
37 call indent_guides#clear_matches()
38 return
39 end
40
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
41 call indent_guides#init_script_vars()
6d903df Nate Kane Added early support for terminal vim
authored
42 call indent_guides#highlight_colors()
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
43 call indent_guides#clear_matches()
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
44
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
45 " loop through each indent level and define a highlight pattern
46 " will automagically figure out whether to use tabs or spaces
67398b8 Nate Kane Added new global option to configure which indent level to start the ind...
authored
47 for l:level in range(s:start_level, s:indent_levels)
7af506c Nate Kane Tweaked highlight patterns.
authored
48 let l:group = 'IndentGuides' . ((l:level % 2 == 0) ? 'Even' : 'Odd')
5f48d29 Nate Kane Refactored the regex for the highlight patterns.
authored
49 let l:column_start = (l:level - 1) * s:indent_size + 1
74c8bde Sergey Vlasov Added new `g:indent_guides_soft_pattern` option
sergey-vlasov authored
50 let l:soft_pattern = indent_guides#indent_highlight_pattern(g:indent_guides_soft_pattern, l:column_start, s:guide_size)
5f48d29 Nate Kane Refactored the regex for the highlight patterns.
authored
51 let l:hard_pattern = indent_guides#indent_highlight_pattern('\t', l:column_start, s:indent_size)
7af506c Nate Kane Tweaked highlight patterns.
authored
52
5f48d29 Nate Kane Refactored the regex for the highlight patterns.
authored
53 " define the higlight patterns and add to matches list
9d18930 Ryan Souza Added a flag for ignoring spaces
ryansouza authored
54 if g:indent_guides_space_guides
55 call add(w:indent_guides_matches, matchadd(l:group, l:soft_pattern))
56 end
7af506c Nate Kane Tweaked highlight patterns.
authored
57 call add(w:indent_guides_matches, matchadd(l:group, l:hard_pattern))
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
58 endfor
59 endfunction
60
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
61 "
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
62 " Disables the indent guides for the current buffer and any other buffer upon
63 " entering it.
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
64 "
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
65 function! indent_guides#disable()
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
66 let g:indent_guides_autocmds_enabled = 0
67 call indent_guides#clear_matches()
68 endfunction
51d44e3 Nate Kane Refactored the usage of the indent_guides_matches variable
authored
69
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
70 "
71 " Clear all highlight matches for the current window.
72 "
73 function! indent_guides#clear_matches()
74 call indent_guides#init_matches()
51d44e3 Nate Kane Refactored the usage of the indent_guides_matches variable
authored
75 if !empty(w:indent_guides_matches)
32b9f52 Nate Kane Added more explicit variable scopes
authored
76 let l:index = 0
77 for l:match_id in w:indent_guides_matches
57b6da6 Fixes E803 ID not found spam
Jeremy Mack authored
78 try
79 call matchdelete(l:match_id)
80 catch /E803:/
81 " Do nothing
82 endtry
32b9f52 Nate Kane Added more explicit variable scopes
authored
83 call remove(w:indent_guides_matches, l:index)
84 let l:index += l:index
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
85 endfor
86 endif
87 endfunction
88
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
89 "
5666f56 Nate Kane Added more function comments and renamed all instances of 'brighten' to ...
authored
90 " Automagically calculates and defines the indent highlight colors.
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
91 "
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
92 function! indent_guides#highlight_colors()
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
93 if s:auto_colors
6d903df Nate Kane Added early support for terminal vim
authored
94 if has('gui_running')
95 call indent_guides#gui_highlight_colors()
96 else
349edc9 Nate Kane Added support for gVim themes that don't specify a `hi Normal guibg` col...
authored
97 call indent_guides#basic_highlight_colors()
3bcd403 Nate Kane Added support for colorschemes that use color names instead of hex codes
authored
98 endif
6d903df Nate Kane Added early support for terminal vim
authored
99 endif
100 endfunction
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
101
6d903df Nate Kane Added early support for terminal vim
authored
102 "
349edc9 Nate Kane Added support for gVim themes that don't specify a `hi Normal guibg` col...
authored
103 " Defines some basic indent highlight colors that work for Terminal Vim and
104 " gVim when colors can't be automatically calculated.
046104c Nate Kane Tweaked the indent_guides#cterm_highlight_colors() function a bit
authored
105 "
349edc9 Nate Kane Added support for gVim themes that don't specify a `hi Normal guibg` col...
authored
106 function! indent_guides#basic_highlight_colors()
107 let l:cterm_colors = (&g:background == 'dark') ? ['darkgrey', 'black'] : ['lightgrey', 'white']
108 let l:gui_colors = (&g:background == 'dark') ? ['grey15', 'grey30'] : ['grey70', 'grey85']
046104c Nate Kane Tweaked the indent_guides#cterm_highlight_colors() function a bit
authored
109
d9fe8b1 Sergey Vlasov Setting foreground colors with respect to background
sergey-vlasov authored
110 exe 'hi IndentGuidesEven guibg=' . l:gui_colors[0] . ' guifg=' . l:gui_colors[1] . ' ctermbg=' . l:cterm_colors[0] . ' ctermfg=' . l:cterm_colors[1]
111 exe 'hi IndentGuidesOdd guibg=' . l:gui_colors[1] . ' guifg=' . l:gui_colors[0] . ' ctermbg=' . l:cterm_colors[1] . ' ctermfg=' . l:cterm_colors[0]
6d903df Nate Kane Added early support for terminal vim
authored
112 endfunction
113
114 "
115 " Automagically calculates and defines the indent highlight colors for gui
116 " vim.
117 "
118 function! indent_guides#gui_highlight_colors()
32b9f52 Nate Kane Added more explicit variable scopes
authored
119 let l:hi_normal_guibg = ''
6d903df Nate Kane Added early support for terminal vim
authored
120
121 " capture the backgroud color from the normal highlight
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
122 if s:hi_normal =~ s:color_hex_bg_pat
6d903df Nate Kane Added early support for terminal vim
authored
123 " hex color code is being used, eg. '#FFFFFF'
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
124 let l:hi_normal_guibg = matchstr(s:hi_normal, s:color_hex_bg_pat)
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
125
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
126 elseif s:hi_normal =~ s:color_name_bg_pat
6d903df Nate Kane Added early support for terminal vim
authored
127 " color name is being used, eg. 'white'
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
128 let l:color_name = matchstr(s:hi_normal, s:color_name_bg_pat)
32b9f52 Nate Kane Added more explicit variable scopes
authored
129 let l:hi_normal_guibg = color_helper#color_name_to_hex(l:color_name)
349edc9 Nate Kane Added support for gVim themes that don't specify a `hi Normal guibg` col...
authored
130
131 else
2ea3460 Nate Kane Altered some comment wording
authored
132 " background color could not be detected, default to basic colors
349edc9 Nate Kane Added support for gVim themes that don't specify a `hi Normal guibg` col...
authored
133 call indent_guides#basic_highlight_colors()
6d903df Nate Kane Added early support for terminal vim
authored
134 endif
135
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
136 if l:hi_normal_guibg =~ s:color_hex_pat
6d903df Nate Kane Added early support for terminal vim
authored
137 " calculate the highlight background colors
32b9f52 Nate Kane Added more explicit variable scopes
authored
138 let l:hi_odd_bg = indent_guides#lighten_or_darken_color(l:hi_normal_guibg)
139 let l:hi_even_bg = indent_guides#lighten_or_darken_color(l:hi_odd_bg)
6d903df Nate Kane Added early support for terminal vim
authored
140
141 " define the new highlights
d9fe8b1 Sergey Vlasov Setting foreground colors with respect to background
sergey-vlasov authored
142 exe 'hi IndentGuidesOdd guibg=' . l:hi_odd_bg . ' guifg=' . l:hi_even_bg
143 exe 'hi IndentGuidesEven guibg=' . l:hi_even_bg . ' guifg=' . l:hi_odd_bg
6d903df Nate Kane Added early support for terminal vim
authored
144 end
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
145 endfunction
146
147 "
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
148 " Takes a color and darkens or lightens it depending on whether a dark or light
149 " colorscheme is being used.
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
150 "
5666f56 Nate Kane Added more function comments and renamed all instances of 'brighten' to ...
authored
151 function! indent_guides#lighten_or_darken_color(color)
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
152 let l:new_color = ''
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
153
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
154 if (&g:background == 'dark')
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
155 let l:new_color = color_helper#hex_color_lighten(a:color, s:change_percent)
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
156 else
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
157 let l:new_color = color_helper#hex_color_darken (a:color, s:change_percent)
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
158 endif
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
159
32b9f52 Nate Kane Added more explicit variable scopes
authored
160 return l:new_color
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
161 endfunction
162
163 "
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
164 " Define default highlights.
8d0c589 Nate Kane The plugin now automatically calculates indent highlight colors
authored
165 "
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
166 function! indent_guides#define_default_highlights()
fa67d46 Nate Kane Removed unnecessary exe calls for default highlights
authored
167 hi default clear IndentGuidesOdd
168 hi default clear IndentGuidesEven
6fd0442 Nate Kane Renamed files so they have underscores instead of dashes and moved metho...
authored
169 endfunction
170
5e91b15 Nate Kane Refactored heaps of stuff and added autocmds
authored
171 "
172 " Init the w:indent_guides_matches variable.
173 "
51d44e3 Nate Kane Refactored the usage of the indent_guides_matches variable
authored
174 function! indent_guides#init_matches()
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
175 let w:indent_guides_matches = exists('w:indent_guides_matches') ? w:indent_guides_matches : []
51d44e3 Nate Kane Refactored the usage of the indent_guides_matches variable
authored
176 endfunction
177
14aae71 Nate Kane Improved support for terminal vim
authored
178 "
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
179 " We need to initialize these vars every time a buffer is entered while the
180 " plugin is enabled.
14aae71 Nate Kane Improved support for terminal vim
authored
181 "
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
182 function! indent_guides#init_script_vars()
60f435e Nate Kane Removed the `indent_guides#get_indent_size` function.
authored
183 let s:indent_size = &l:shiftwidth
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
184 let s:guide_size = indent_guides#calculate_guide_size()
185 let s:hi_normal = indent_guides#capture_highlight('Normal')
186
353bef3 Nate Kane Improved Windows support
authored
187 " remove 'font=<value>' from the s:hi_normal string (only seems to happen on Vim startup in Windows)
188 let s:hi_normal = substitute(s:hi_normal, ' font=[A-Za-z0-9:]\+', "", "")
189
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
190 " shortcuts to the global variables - this makes the code easier to read
191 let s:debug = g:indent_guides_debug
192 let s:indent_levels = g:indent_guides_indent_levels
193 let s:auto_colors = g:indent_guides_auto_colors
194 let s:color_hex_pat = g:indent_guides_color_hex_pattern
195 let s:color_hex_bg_pat = g:indent_guides_color_hex_guibg_pattern
196 let s:color_name_bg_pat = g:indent_guides_color_name_guibg_pattern
67398b8 Nate Kane Added new global option to configure which indent level to start the ind...
authored
197 let s:start_level = g:indent_guides_start_level
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
198
bdf4ec3 Sam Lidder Added test to only use str2float if compiled with +float
acx0 authored
199 " str2float not available in vim versions <= 7.1
200 if has('float')
201 let s:change_percent = g:indent_guides_color_change_percent / str2float('100.0')
202 else
203 let s:change_percent = g:indent_guides_color_change_percent / 100.0
204 endif
205
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
206 if s:debug
207 echo 's:indent_size = ' . s:indent_size
208 echo 's:guide_size = ' . s:guide_size
209 echo 's:hi_normal = ' . s:hi_normal
210 echo 's:indent_levels = ' . s:indent_levels
211 echo 's:auto_colors = ' . s:auto_colors
212 echo 's:change_percent = ' . string(s:change_percent)
213 echo 's:color_hex_pat = ' . s:color_hex_pat
214 echo 's:color_hex_bg_pat = ' . s:color_hex_bg_pat
215 echo 's:color_name_bg_pat = ' . s:color_name_bg_pat
67398b8 Nate Kane Added new global option to configure which indent level to start the ind...
authored
216 echo 's:start_level = ' . s:start_level
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
217 endif
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
218 endfunction
219
220 "
221 " Calculate the indent guide size. Ensures the guide size is less than or
222 " equal to the actual indent size, otherwise some weird things can occur.
223 "
224 " NOTE: Currently, this only works when soft-tabs are being used.
225 "
226 function! indent_guides#calculate_guide_size()
7af506c Nate Kane Tweaked highlight patterns.
authored
227 let l:guide_size = g:indent_guides_guide_size
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
228
d9c5963 Nate Kane Fixed bug with calculating guide size.
authored
229 if l:guide_size == 0 || l:guide_size > s:indent_size
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
230 let l:guide_size = s:indent_size
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
231 endif
232
233 return l:guide_size
234 endfunction
235
236 "
237 " Captures and returns the output of highlight group definitions.
238 "
239 " Example: indent_guides#capture_highlight('normal')
3b134f3 Nate Kane Fix comment.
authored
240 " Returns: 'Normal xxx guifg=#323232 guibg=#ffffff'
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
241 "
242 function! indent_guides#capture_highlight(group_name)
243 redir => l:output
244 exe "silent hi " . a:group_name
245 redir END
246
06d2b34 Refactored some core logic and added some simple debugging
Nate Kane authored
247 let l:output = substitute(l:output, "\n", "", "")
f2608db Nate Kane Refactored some core functionality and implemented an initial version of...
authored
248 return l:output
14aae71 Nate Kane Improved support for terminal vim
authored
249 endfunction
250
5f48d29 Nate Kane Refactored the regex for the highlight patterns.
authored
251 "
252 " Returns a regex pattern for highlighting an indent level.
253 "
254 " Example: indent_guides#indent_highlight_pattern(' ', 1, 4)
255 " Returns: /^ *\%1v\zs *\%5v\ze/
256 "
257 " Example: indent_guides#indent_highlight_pattern('\s', 5, 2)
258 " Returns: /^\s*\%5v\zs\s*\%7v\ze/
259 "
260 " Example: indent_guides#indent_highlight_pattern('\t', 9, 2)
261 " Returns: /^\t*\%9v\zs\t*\%11v\ze/
262 "
263 function! indent_guides#indent_highlight_pattern(indent_pattern, column_start, indent_size)
264 let l:pattern = '^' . a:indent_pattern . '*\%' . a:column_start . 'v\zs'
265 let l:pattern .= a:indent_pattern . '*\%' . (a:column_start + a:indent_size) . 'v'
266 let l:pattern .= '\ze'
267 return l:pattern
268 endfunction
997bede Nate Kane Add option g:indent_guides_exclude_filetypes.
authored
269
270 "
271 " Detect if any of the buffer filetypes should be excluded.
272 "
273 function! indent_guides#exclude_filetype()
a570008 Nate Kane Split multiple filetypes on ".".
authored
274 for ft in split(&ft, '\.')
997bede Nate Kane Add option g:indent_guides_exclude_filetypes.
authored
275 if index(g:indent_guides_exclude_filetypes, ft) > -1
276 return 1
277 end
278 endfor
279 return 0
280 endfunction
Something went wrong with that request. Please try again.