Skip to content

Commit c0ad20c

Browse files
committed
Buffer-local stripping of whitespace
1 parent 688837f commit c0ad20c

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
3434
highlight ExtraWhitespace ctermbg=<desired_color>
3535
```
3636
37-
* To toggle whitespace highlighting on/off, call:
37+
* To enable/disable/toggle whitespace highlighting in a buffer, call one of:
3838
```
39+
:EnableWhitespace
40+
:DisableWhitespace
3941
:ToggleWhitespace
4042
```
4143
@@ -51,7 +53,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
5153
5254
* The level `soft` will use syntax based highlighting, so there shouldn't be
5355
a performance hit like with the `hard` option. The drawback is that this
54-
highlighting will have a lower priority and may be overwritten by higher
56+
highlighting will have a lower priority and may be overwritten by higher
5557
priority highlighting.
5658
5759
* To re-enable highlighting for the current line in normal mode:
@@ -67,16 +69,18 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
6769
the file that it cleans, either give it a range or select a group of lines
6870
in visual mode and then execute it.
6971
70-
* To enable/disable stripping of extra whitespace on file save, call:
72+
* To enable/disable stripping of extra whitespace on file save for a buffer, call one of:
7173
```
74+
:EnableStripWhitespaceOnSave
75+
:DisableStripWhitespaceOnSave
7276
:ToggleStripWhitespaceOnSave
7377
```
7478
This will strip all trailing whitespace everytime you save the file for all file types.
7579
7680
* If you want this behaviour by default for all filetypes, add the following to your `~/.vimrc`:
7781
7882
```
79-
autocmd BufEnter * EnableStripWhitespaceOnSave
83+
let g:strip_whitespace_on_save = 1
8084
```
8185
8286
For exceptions of all see ```g:better_whitespace_filetypes_blacklist```.
@@ -85,13 +89,11 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
8589
the following to your `~/.vimrc`:
8690
8791
```
88-
autocmd FileType <desired_filetypes> autocmd BufEnter <buffer> EnableStripWhitespaceOnSave
92+
autocmd FileType <desired_filetypes> EnableStripWhitespaceOnSave
8993
```
9094
9195
where `<desired_filetypes>` is a comma separated list of the file types you want
9296
to be stripped of whitespace on file save ( ie. `javascript,c,cpp,java,html,ruby` )
93-
Note that `<buffer>` is a keyword here denoting operation on the current buffer and
94-
should stay just as it appears in the line above.
9597
9698
* To disable this plugin for specific file types, add the following to your `~/.vimrc`:
9799
```
@@ -109,6 +111,15 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
109111
'diff', 'gitcommit', 'unite', 'qf', 'help']
110112
```
111113
114+
This blacklist can be overriden on a per-buffer basis using the buffer toggle enable and
115+
disable commands presented above. For example:
116+
```vim
117+
" highlight whitespace in markdown files, though stripping remains disabled by the blacklist
118+
:autocmd FileType markdown EnableWhitespace
119+
" Do not modify kernel files, even though their type is not blacklisted and highlighting is enabled
120+
:autocmd BufRead /usr/src/linux* DisableStripWhitespaceOnSave
121+
```
122+
112123
* To enable verbose output for each command, set verbosity in your `.vimrc`:
113124
```
114125
let g:better_whitespace_verbosity=1

plugin/better-whitespace.vim

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,22 @@ endfunction
163163

164164
" Strip whitespace on file save
165165
function! s:EnableStripWhitespaceOnSave()
166-
let g:strip_whitespace_on_save = 1
166+
let b:strip_whitespace_on_save = 1
167167
call <SID>Echo("Strip Whitespace On Save: Enabled")
168168
call <SID>SetupAutoCommands()
169169
endfunction
170170

171171
" Don't strip whitespace on file save
172172
function! s:DisableStripWhitespaceOnSave()
173-
let g:strip_whitespace_on_save = 0
173+
let b:strip_whitespace_on_save = 0
174174
call <SID>Echo("Strip Whitespace On Save: Disabled")
175175
call <SID>SetupAutoCommands()
176176
endfunction
177177

178178
" Strips whitespace on file save
179179
function! s:ToggleStripWhitespaceOnSave()
180-
if g:strip_whitespace_on_save == 1
180+
call <SID>ShouldSkipHighlight()
181+
if b:strip_whitespace_on_save == 1
181182
call <SID>DisableStripWhitespaceOnSave()
182183
else
183184
call <SID>EnableStripWhitespaceOnSave()
@@ -186,7 +187,12 @@ endfunction
186187

187188
" Determines if whitespace highlighting should currently be skipped
188189
function! s:ShouldSkipHighlight()
189-
return &buftype == 'nofile' || index(g:better_whitespace_filetypes_blacklist, &ft) >= 0
190+
if !exists('b:better_whitespace_enabled')
191+
let b:better_whitespace_enabled = &buftype != 'nofile' && index(g:better_whitespace_filetypes_blacklist, &ft) == -1
192+
endif
193+
if !exists('b:strip_whitespace_on_save')
194+
let b:strip_whitespace_on_save = b:better_whitespace_enabled && g:strip_whitespace_on_save
195+
endif
190196
endfunction
191197

192198
" Run :StripWhitespace to remove end of line whitespace
@@ -210,12 +216,12 @@ command! -nargs=* CurrentLineWhitespaceOff call <SID>CurrentLineWhitespaceOff( <
210216
command! CurrentLineWhitespaceOn call <SID>CurrentLineWhitespaceOn()
211217

212218
" Process auto commands upon load, update local enabled on filetype change
213-
autocmd FileType * let b:better_whitespace_enabled = !<SID>ShouldSkipHighlight() | call <SID>SetupAutoCommands()
219+
autocmd FileType * call <SID>ShouldSkipHighlight() | call <SID>SetupAutoCommands()
214220
autocmd WinEnter,BufWinEnter * call <SID>SetupAutoCommands()
215221
autocmd ColorScheme * call <SID>WhitespaceInit()
216222

217223
function! s:PerformMatchHighlight(pattern)
218-
call s:InitVariable('b:better_whitespace_enabled', !<SID>ShouldSkipHighlight())
224+
call <SID>ShouldSkipHighlight()
219225
if b:better_whitespace_enabled == 1
220226
exe 'match ExtraWhitespace "' . a:pattern . '"'
221227
else
@@ -225,7 +231,7 @@ endfunction
225231

226232
function! s:PerformSyntaxHighlight(pattern)
227233
syn clear ExtraWhitespace
228-
call s:InitVariable('b:better_whitespace_enabled', !<SID>ShouldSkipHighlight())
234+
call <SID>ShouldSkipHighlight()
229235
if b:better_whitespace_enabled == 1
230236
exe 'syn match ExtraWhitespace excludenl "' . a:pattern . '"'
231237
endif
@@ -285,8 +291,8 @@ function! <SID>SetupAutoCommands()
285291
endif
286292
endif
287293

288-
" Strip whitespace on save if enabled
289-
if g:strip_whitespace_on_save == 1
294+
" Strip whitespace on save if enabled.
295+
if (exists('b:strip_whitespace_on_save') ? b:strip_whitespace_on_save : g:strip_whitespace_on_save) == 1
290296
autocmd BufWritePre * call <SID>StripWhitespace( 0, line("$") )
291297
endif
292298

0 commit comments

Comments
 (0)