@@ -10,9 +10,9 @@ let g:loaded_better_whitespace_plugin = 1
1010" Initializes a given variable to a given value. The variable is only
1111" initialized if it does not exist prior.
1212function ! s: InitVariable (var , value)
13- if ! exists (a: var )
14- execute ' let ' . a: var . ' = ' . string (a: value )
15- endif
13+ if ! exists (a: var )
14+ execute ' let ' . a: var . ' = ' . string (a: value )
15+ endif
1616endfunction
1717
1818" Set this to enable/disable whitespace highlighting
@@ -79,30 +79,26 @@ endfunction
7979
8080" Enable the whitespace highlighting
8181function ! s: EnableWhitespace ()
82- if g : better_whitespace_enabled == 0
83- let g : better_whitespace_enabled = 1
82+ if b : better_whitespace_enabled == 0
83+ let b : better_whitespace_enabled = 1
8484 call <SID> WhitespaceInit ()
85- " Match default whitespace
86- call s: InAllWindows (' match ExtraWhitespace /\s\+$/' )
8785 call <SID> SetupAutoCommands ()
8886 call <SID> Echo (" Whitespace Highlighting: Enabled" )
8987 endif
9088endfunction
9189
9290" Disable the whitespace highlighting
9391function ! s: DisableWhitespace ()
94- if g: better_whitespace_enabled == 1
95- let g: better_whitespace_enabled = 0
96- " Clear current whitespace matches
97- call s: InAllWindows (" match ExtraWhitespace '' | syn clear ExtraWhitespace" )
92+ if b: better_whitespace_enabled == 1
93+ let b: better_whitespace_enabled = 0
9894 call <SID> SetupAutoCommands ()
9995 call <SID> Echo (" Whitespace Highlighting: Disabled" )
10096 endif
10197endfunction
10298
10399" Toggle whitespace highlighting on/off
104100function ! s: ToggleWhitespace ()
105- if g : better_whitespace_enabled == 1
101+ if b : better_whitespace_enabled == 1
106102 call <SID> DisableWhitespace ()
107103 else
108104 call <SID> EnableWhitespace ()
@@ -193,21 +189,34 @@ command! -nargs=* CurrentLineWhitespaceOff call <SID>CurrentLineWhitespaceOff( <
193189" Run :CurrentLineWhitespaceOn to turn on whitespace for the current line
194190command ! CurrentLineWhitespaceOn call <SID> CurrentLineWhitespaceOn ()
195191
196- " Process auto commands upon load
197- autocmd BufWinEnter ,FileType * call <SID> SetupAutoCommands ()
192+ " Process auto commands upon load, update local enabled on filetype change
193+ autocmd FileType * let b: better_whitespace_enabled = ! <SID> ShouldSkipHighlight () | call <SID> SetupAutoCommands ()
194+ autocmd BufWinEnter * call <SID> SetupAutoCommands ()
198195autocmd ColorScheme * call <SID> WhitespaceInit ()
199196
197+ function ! s: PerformMatchHighlight (pattern)
198+ call s: InitVariable (' b:better_whitespace_enabled' , ! <SID> ShouldSkipHighlight ())
199+ if b: better_whitespace_enabled == 1
200+ exe ' match ExtraWhitespace ' . a: pattern
201+ else
202+ match ExtraWhitespace ' '
203+ endif
204+ endfunction
205+
206+ function ! s: PerformSyntaxHighlight (pattern)
207+ syn clear ExtraWhitespace
208+ call s: InitVariable (' b:better_whitespace_enabled' , ! <SID> ShouldSkipHighlight ())
209+ if b: better_whitespace_enabled == 1
210+ exe ' syn match ExtraWhitespace excludenl ' . a: pattern
211+ endif
212+ endfunction
213+
200214" Executes all auto commands
201215function ! <SID> SetupAutoCommands ()
202216 " Auto commands group
203217 augroup better_whitespace
204218 autocmd !
205219
206- if <SID> ShouldSkipHighlight ()
207- match ExtraWhitespace ' '
208- return
209- endif
210-
211220 if g: better_whitespace_enabled == 1
212221 if s: better_whitespace_initialized == 0
213222 call <SID> WhitespaceInit ()
@@ -216,25 +225,25 @@ function! <SID>SetupAutoCommands()
216225 " Check if current line is disabled softly
217226 if g: current_line_whitespace_disabled_soft == 0
218227 " Highlight all whitespace upon entering buffer
219- autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
228+ call <SID> PerformMatchHighlight ( ' /\s\+$/' )
220229 " Check if current line highglighting is disabled
221230 if g: current_line_whitespace_disabled_hard == 1
222231 " Never highlight whitespace on current line
223- autocmd InsertEnter ,CursorMoved ,CursorMovedI * exe ' match ExtraWhitespace ' . ' /\%<' . line (" ." ) . ' l\s\+$\|\%>' . line (" ." ) . ' l\s\+$/'
232+ autocmd InsertEnter ,CursorMoved ,CursorMovedI * call <SID> PerformMatchHighlight ( ' /\%<' . line (" ." ) . ' l\s\+$\|\%>' . line (" ." ) . ' l\s\+$/' )
224233 else
225234 " When in insert mode, do not highlight whitespace on the current line
226- autocmd InsertEnter ,CursorMovedI * exe ' match ExtraWhitespace ' . ' /\%<' . line (" ." ) . ' l\s\+$\|\%>' . line (" ." ) . ' l\s\+$/'
235+ autocmd InsertEnter ,CursorMovedI * call <SID> PerformMatchHighlight ( ' /\%<' . line (" ." ) . ' l\s\+$\|\%>' . line (" ." ) . ' l\s\+$/' )
227236 endif
228237 " Highlight all whitespace when exiting insert mode
229- autocmd InsertLeave ,BufReadPost * match ExtraWhitespace /\s\+$/
238+ autocmd InsertLeave ,BufReadPost * call <SID> PerformMatchHighlight ( ' /\s\+$/' )
230239 " Clear whitespace highlighting when leaving buffer
231240 autocmd BufWinLeave * match ExtraWhitespace ' '
232241 else
233242 " Highlight extraneous whitespace at the end of lines, but not the
234243 " current line.
235- call s: InAllWindows ( ' syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+$/' )
236- autocmd InsertEnter * syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+\%#\@!$/ containedin = ALLBUT ,gitcommitDiff
237- autocmd InsertLeave ,BufReadPost * syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+$/ containedin = ALLBUT ,gitcommitDiff
244+ call <SID> PerformSyntaxHighlight ( ' /\s\+$/' )
245+ autocmd InsertEnter * call <SID> PerformSyntaxHighlight ( ' /\s\+\%#\@!$/' )
246+ autocmd InsertLeave ,BufReadPost * call <SID> PerformSyntaxHighlight ( ' /\s\+$/' )
238247 endif
239248 endif
240249
@@ -246,5 +255,3 @@ function! <SID>SetupAutoCommands()
246255 augroup END
247256endfunction
248257
249- " Initial call to setup autocommands
250- call <SID> SetupAutoCommands ()
0 commit comments