@@ -32,16 +32,17 @@ endfunction
32
32
"
33
33
function ! indent_guides#enable ()
34
34
let g: indent_guides_autocmds_enabled = 1
35
+
36
+ call indent_guides#init_buffer_vars ()
35
37
call indent_guides#highlight_colors ()
36
38
call indent_guides#clear_matches ()
37
39
38
40
" loop through each indent level and define a highlight pattern
39
41
" will automagically figure out whether to use tabs or spaces
40
42
for l: level in range (1 , g: indent_guides_indent_levels )
41
43
let l: group = ' IndentGuides' . ((l: level % 2 == 0 ) ? ' Even' : ' Odd' )
42
- let l: multiplier = (&l: expandtab == 1 ) ? &l: shiftwidth : 1
43
- let l: pattern = ' ^\s\{' . (l: level * l: multiplier - l: multiplier ) . ' \}\zs'
44
- let l: pattern .= ' \s\{' . l: multiplier . ' \}'
44
+ let l: pattern = ' ^\s\{' . (l: level * b: indent_size - b: indent_size ) . ' \}\zs'
45
+ let l: pattern .= ' \s\{' . b: guide_size . ' \}'
45
46
let l: pattern .= ' \ze'
46
47
47
48
" define the higlight pattern and add to list
@@ -93,8 +94,7 @@ endfunction
93
94
" light or dark preset colors depending on the `set background=` value.
94
95
"
95
96
function ! indent_guides#cterm_highlight_colors ()
96
- let l: colors = (&g: background == ' dark' ) ?
97
- \ [' darkgrey' , ' black' ] : [' lightgrey' , ' white' ]
97
+ let l: colors = (&g: background == ' dark' ) ? [' darkgrey' , ' black' ] : [' lightgrey' , ' white' ]
98
98
99
99
exe ' hi IndentGuidesEven ctermbg=' . l: colors [0 ]
100
100
exe ' hi IndentGuidesOdd ctermbg=' . l: colors [1 ]
@@ -105,22 +105,20 @@ endfunction
105
105
" vim.
106
106
"
107
107
function ! indent_guides#gui_highlight_colors ()
108
- let l: hi_normal = indent_guides#capture_highlight (' Normal' )
109
- let l: hex_pattern = ' guibg=\zs' . g: indent_guides_hex_color_pattern . ' \ze'
110
- let l: name_pattern = " guibg='\\ ?\\ zs[0-9A-Za-z ]\\ +\\ ze'\\ ?"
111
108
let l: hi_normal_guibg = ' '
112
109
113
110
" capture the backgroud color from the normal highlight
114
- if l : hi_normal = ~ l: hex_pattern
111
+ if b : hi_normal = ~ g: indent_guides_color_hex_guibg_pattern
115
112
" hex color code is being used, eg. '#FFFFFF'
116
- let l: hi_normal_guibg = matchstr (l: hi_normal , l: hex_pattern )
117
- elseif l: hi_normal = ~ l: name_pattern
113
+ let l: hi_normal_guibg = matchstr (b: hi_normal , g: indent_guides_color_hex_guibg_pattern )
114
+
115
+ elseif b: hi_normal = ~ g: indent_guides_color_name_guibg_pattern
118
116
" color name is being used, eg. 'white'
119
- let l: color_name = matchstr (l : hi_normal , l: name_pattern )
117
+ let l: color_name = matchstr (b : hi_normal , g: indent_guides_color_name_guibg_pattern )
120
118
let l: hi_normal_guibg = color_helper#color_name_to_hex (l: color_name )
121
119
endif
122
120
123
- if l: hi_normal_guibg = ~ g: indent_guides_hex_color_pattern
121
+ if l: hi_normal_guibg = ~ g: indent_guides_color_hex_pattern
124
122
" calculate the highlight background colors
125
123
let l: hi_odd_bg = indent_guides#lighten_or_darken_color (l: hi_normal_guibg )
126
124
let l: hi_even_bg = indent_guides#lighten_or_darken_color (l: hi_odd_bg )
@@ -136,42 +134,81 @@ endfunction
136
134
" colorscheme is being used.
137
135
"
138
136
function ! indent_guides#lighten_or_darken_color (color )
139
- let l: percent = g: indent_guides_color_change_percent
137
+ let l: new_color = ' '
138
+ let l: percent = g: indent_guides_color_change_percent / 100.0
140
139
141
- let l: new_color = (&g: background == ' dark' ) ?
142
- \ color_helper#hex_color_lighten (a: color , l: percent ) :
143
- \ color_helper#hex_color_darken (a: color , l: percent )
140
+ if (&g: background == ' dark' )
141
+ let l: new_color = color_helper#hex_color_lighten (a: color , l: percent )
142
+ else
143
+ let l: new_color = color_helper#hex_color_darken (a: color , l: percent )
144
+ endif
144
145
145
146
return l: new_color
146
147
endfunction
147
148
148
149
"
149
- " Captures and returns the output of highlight group definitions.
150
- "
151
- " Example: indent_guides#capture_highlight('normal')
152
- " Returns: 'Normal xxx guifg=#323232 guibg=#ffffff
150
+ " Define default highlights.
153
151
"
154
- function ! indent_guides#capture_highlight (group_name)
155
- redir = > l: output
156
- exe " silent hi " . a: group_name
157
- redir END
158
-
159
- return l: output
152
+ function ! indent_guides#define_default_highlights ()
153
+ exe ' hi IndentGuidesOdd guibg=NONE ctermbg=NONE'
154
+ exe ' hi IndentGuidesEven guibg=NONE ctermbg=NONE'
160
155
endfunction
161
156
162
157
"
163
158
" Init the w:indent_guides_matches variable.
164
159
"
165
160
function ! indent_guides#init_matches ()
166
- let w: indent_guides_matches =
167
- \ exists (' w:indent_guides_matches' ) ? w: indent_guides_matches : []
161
+ let w: indent_guides_matches = exists (' w:indent_guides_matches' ) ? w: indent_guides_matches : []
168
162
endfunction
169
163
170
164
"
171
- " Define default highlights.
165
+ " We need to initialize these vars every time a buffer is entered while the
166
+ " plugin is enabled.
172
167
"
173
- function ! indent_guides#define_default_highlights ()
174
- exe ' hi IndentGuidesOdd guibg=NONE ctermbg=NONE'
175
- exe ' hi IndentGuidesEven guibg=NONE ctermbg=NONE'
168
+ function ! indent_guides#init_buffer_vars ()
169
+ let b: indent_size = indent_guides#get_indent_size ()
170
+ let b: guide_size = indent_guides#calculate_guide_size ()
171
+ let b: hi_normal = indent_guides#capture_highlight (' Normal' )
172
+ endfunction
173
+
174
+ "
175
+ " Calculate the indent guide size. Ensures the guide size is less than or
176
+ " equal to the actual indent size, otherwise some weird things can occur.
177
+ "
178
+ " NOTE: Currently, this only works when soft-tabs are being used.
179
+ "
180
+ function ! indent_guides#calculate_guide_size ()
181
+ let l: guide_size = g: indent_guides_indent_guide_size
182
+ let l: indent_size = indent_guides#get_indent_size ()
183
+
184
+ if l: indent_size > 1 && l: guide_size >= 1
185
+ let l: guide_size = (l: guide_size > b: indent_size ) ? b: indent_size : l: guide_size
186
+ else
187
+ let l: guide_size = b: indent_size
188
+ endif
189
+
190
+ return l: guide_size
191
+ endfunction
192
+
193
+ "
194
+ " Gets the indent size, which depends on whether soft-tabs or hard-tabs are
195
+ " being used.
196
+ "
197
+ function ! indent_guides#get_indent_size ()
198
+ return (&l: expandtab == 1 ) ? &l: shiftwidth : 1
199
+ endfunction
200
+
201
+ "
202
+ " Captures and returns the output of highlight group definitions.
203
+ "
204
+ " Example: indent_guides#capture_highlight('normal')
205
+ " Returns: 'Normal xxx guifg=#323232 guibg=#ffffff
206
+ "
207
+ function ! indent_guides#capture_highlight (group_name)
208
+ redir = > l: output
209
+ exe " silent hi " . a: group_name
210
+ redir END
211
+
212
+ return l: output
176
213
endfunction
177
214
0 commit comments