From d9ff9bdda8a7f3248802f2bbdf0d55b7dab1dc76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=B6ltje?= Date: Sat, 7 Jan 2017 22:46:51 -0500 Subject: [PATCH] Added explicit Enable/Disable for strip-on-save This adds `EnableStripWhitespaceOnSave` and `DisableStripWhitespaceOnSave` functions as an alternative for `ToggleStripWhitespaceOnSave`. Using these you can (correctly) set the variables for the buffer to allow the toggle to work. I updated the readme to reflect this change. Closes #54 --- README.md | 4 ++-- plugin/better-whitespace.vim | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 659172c..53d6cd6 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red. * If you want this behaviour by default for all filetypes, add the following to your `~/.vimrc`: ``` - autocmd BufWritePre * StripWhitespace + autocmd BufEnter * EnableStripWhitespaceOnSave ``` For exceptions of all see ```g:better_whitespace_filetypes_blacklist```. @@ -85,7 +85,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red. the following to your `~/.vimrc`: ``` - autocmd FileType autocmd BufWritePre StripWhitespace + autocmd FileType autocmd BufEnter EnableStripWhitespaceOnSave ``` where `` is a comma separated list of the file types you want diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index ece91cc..9c526f3 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -156,16 +156,27 @@ function! s:StripWhitespace( line1, line2 ) call cursor(l, c) endfunction +" Strip whitespace on file save +function! s:EnableStripWhitespaceOnSave() + let g:strip_whitespace_on_save = 1 + call Echo("Strip Whitespace On Save: Enabled") + call SetupAutoCommands() +endfunction + +" Don't strip whitespace on file save +function! s:DisableStripWhitespaceOnSave() + let g:strip_whitespace_on_save = 0 + call Echo("Strip Whitespace On Save: Disabled") + call SetupAutoCommands() +endfunction + " Strips whitespace on file save function! s:ToggleStripWhitespaceOnSave() - if g:strip_whitespace_on_save == 0 - let g:strip_whitespace_on_save = 1 - call Echo("Strip Whitespace On Save: Enabled") + if g:strip_whitespace_on_save == 1 + call DisableStripWhitespaceOnSave() else - let g:strip_whitespace_on_save = 0 - call Echo("Strip Whitespace On Save: Disabled") + call EnableStripWhitespaceOnSave() endif - call SetupAutoCommands() endfunction " Determines if whitespace highlighting should currently be skipped @@ -175,6 +186,10 @@ endfunction " Run :StripWhitespace to remove end of line whitespace command! -range=% StripWhitespace call StripWhitespace( , ) +" Run :EnableStripWhitespaceOnSave to enable whitespace stripping on save +command! EnableStripWhitespaceOnSave call EnableStripWhitespaceOnSave() +" Run :DisableStripWhitespaceOnSave to disable whitespace stripping on save +command! DisableStripWhitespaceOnSave call DisableStripWhitespaceOnSave() " Run :ToggleStripWhitespaceOnSave to enable/disable whitespace stripping on save command! ToggleStripWhitespaceOnSave call ToggleStripWhitespaceOnSave() " Run :EnableWhitespace to enable whitespace highlighting