Skip to content

Commit 427b7c9

Browse files
committed
Merge branch 'Cimbali-master'
2 parents c77ffda + 110a9fd commit 427b7c9

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
7272
:ToggleStripWhitespaceOnSave
7373
```
7474
This will strip all trailing whitespace everytime you save the file for all file types.
75-
75+
7676
* If you want this behaviour by default for all filetypes, add the following to your `~/.vimrc`:
77-
77+
7878
```
7979
autocmd BufWritePre * StripWhitespace
8080
```
81-
81+
8282
For exceptions of all see ```g:better_whitespace_filetypes_blacklist```.
83-
83+
8484
* If you would prefer to only stip whitespace for certain filetypes, add
8585
the following to your `~/.vimrc`:
8686
@@ -95,17 +95,18 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
9595
9696
* To disable this plugin for specific file types, add the following to your `~/.vimrc`:
9797
```
98-
let g:better_whitespace_filetypes_blacklist+=['<filetype1>', '<filetype2>', '<etc>']
98+
let g:better_whitespace_filetypes_blacklist=['<filetype1>', '<filetype2>', '<etc>']
9999
```
100-
This adds filetypes to the default list of blacklisted filetypes. The
100+
This replaces the filetypes from the default list of blacklisted filetypes. The
101101
default types that are blacklisted are:
102102
```
103103
['diff', 'gitcommit', 'unite', 'qf', 'help']
104104
```
105-
If you do not want any of these filetypes ignored, simply reset the
106-
blacklist rather than append to it:
105+
If you do not want any of these filetypes unignored, simply include them in the
106+
blacklist:
107107
```
108-
let g:better_whitespace_filetypes_blacklist=['<filetype1>', '<filetype2>', '<etc>']
108+
let g:better_whitespace_filetypes_blacklist=['<filetype1>', '<filetype2>', '<etc>',
109+
'diff', 'gitcommit', 'unite', 'qf', 'help']
109110
```
110111
111112
* To enable verbose output for each command, set verbosity in your `.vimrc`:

plugin/better-whitespace.vim

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1212
function! 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
1616
endfunction
1717

1818
" Set this to enable/disable whitespace highlighting
@@ -79,30 +79,26 @@ endfunction
7979

8080
" Enable the whitespace highlighting
8181
function! 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
9088
endfunction
9189

9290
" Disable the whitespace highlighting
9391
function! 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
10197
endfunction
10298

10399
" Toggle whitespace highlighting on/off
104100
function! 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
194190
command! 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()
198195
autocmd 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
201215
function! <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
247256
endfunction
248257

249-
" Initial call to setup autocommands
250-
call <SID>SetupAutoCommands()

0 commit comments

Comments
 (0)