From a57416e38998be44aa9075bf2615be20b3094e20 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Mon, 3 Jun 2013 18:29:05 +0200 Subject: [PATCH 01/27] Use a load guard and put setup code at the top-level Now a BadWhitespace highlight is always created independently of the filetype and the configuration of the file you start with. Fix error about missing color. --- plugin/bad-whitespace.vim | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index eece123..b657d35 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -2,11 +2,18 @@ " Maintainer: Bit Connor " Version: 0.3 +if exists('loaded_bad_whitespace') + finish +endif +let loaded_bad_whitespace = 1 + +highlight default BadWhitespace ctermbg=red guibg=red +autocmd BufWinEnter,WinEnter,FileType * call EnableShowBadWhitespace() + function! s:ShowBadWhitespace(force) if a:force let b:bad_whitespace_show = 1 endif - highlight default BadWhitespace ctermbg=red guibg=red autocmd ColorScheme highlight default BadWhitespace ctermbg=red guibg=red match BadWhitespace /\s\+$/ autocmd InsertLeave match BadWhitespace /\s\+$/ @@ -45,8 +52,6 @@ function! s:ToggleBadWhitespace() endif endfunction -autocmd BufWinEnter,WinEnter,FileType * call EnableShowBadWhitespace() - function! s:EraseBadWhitespace(line1,line2) let l:save_cursor = getpos(".") silent! execute ':' . a:line1 . ',' . a:line2 . 's/\s\+$//' From 1b2225ee7cd2f4aeef79d7ba19087ef08962afda Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Mon, 3 Jun 2013 18:32:20 +0200 Subject: [PATCH 02/27] Suppress highlighting empty +/- columns of patches You can now suppress highlighting of the first (and second in case of a combined diff) column for selected filetypes, by setting the following variable let g:bad_whitespace_patch_filetypes = ['diff', 'git'] The given value is the default. --- README | 3 +++ doc/bad-whitespace.txt | 8 ++++++++ plugin/bad-whitespace.vim | 30 +++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/README b/README index 6ab9429..d48ac49 100644 --- a/README +++ b/README @@ -32,3 +32,6 @@ This plugin is better than competing plugins because it doesn't show highlighting in buffers that are not modifiable, which is usually what you want. This makes it compatible with UI buffers that happen to contain bad whitespace, such as the quickfix window, or the fugitive plugin. + +This plugin can optionally suppress highlighting the the first two columns of a +patch in unified diff format. diff --git a/doc/bad-whitespace.txt b/doc/bad-whitespace.txt index 652e9a0..5078e2f 100644 --- a/doc/bad-whitespace.txt +++ b/doc/bad-whitespace.txt @@ -51,4 +51,12 @@ Disables bad whitespace highlighting for the current buffer. Toggles bad whitespace highlighting for the current buffer. +Configuration~ *g:bad_whitespace_patch_filetypes* + +You can suppress highlighting of the first (and second in case of a combined +diff) column for selected filetypes, by setting the following variable. The +given value is the default: > + let g:bad_whitespace_patch_filetypes = ['diff', 'git'] +< + vim:tw=78:et:ft=help: diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index b657d35..ad91458 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -10,14 +10,37 @@ let loaded_bad_whitespace = 1 highlight default BadWhitespace ctermbg=red guibg=red autocmd BufWinEnter,WinEnter,FileType * call EnableShowBadWhitespace() +if ! exists( "g:bad_whitespace_patch_filetypes" ) + let g:bad_whitespace_patch_filetypes = ['diff', 'git'] +endif + function! s:ShowBadWhitespace(force) if a:force let b:bad_whitespace_show = 1 endif autocmd ColorScheme highlight default BadWhitespace ctermbg=red guibg=red - match BadWhitespace /\s\+$/ - autocmd InsertLeave match BadWhitespace /\s\+$/ - autocmd InsertEnter match BadWhitespace /\s\+\%#\@ match BadWhitespace ' . l:whitespace_pattern_global + execute 'autocmd InsertEnter match BadWhitespace ' . l:whitespace_pattern_editing +endfunction + +function! s:SetBufferSpecificWhitespacePattern() + let l:patch_filtypes = filter(copy(g:bad_whitespace_patch_filetypes), 'v:val == &ft') + if empty(l:patch_filtypes) + return + endif + let l:start_colum = 2 + if search('^@@@ ', 'nw') + let l:start_colum = 3 + endif + let b:bad_whitespace_buffer_pattern_prefix = '/\%' . l:start_colum . 'c.\{-\}\zs\s\+\ze' endfunction function! s:HideBadWhitespace(force) @@ -31,6 +54,7 @@ function! s:EnableShowBadWhitespace() if exists("b:bad_whitespace_show") return endif + call s:SetBufferSpecificWhitespacePattern() if &modifiable call ShowBadWhitespace(0) else From 8041859bb374f4d0d4ab46caa29f8bd9e50de34e Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Mon, 3 Jun 2013 18:34:46 +0200 Subject: [PATCH 03/27] Introduce a BadWhitespace highlight link If you like to toggle colors by re-linking, you can now specify the default highlight for trailing whitespace in: let g:bad_whitespace_color_default = 'BadWhitespaceDefaultState The given value is the default link target if you don't create that variable. --- plugin/bad-whitespace.vim | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index ad91458..268a32a 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -7,18 +7,23 @@ if exists('loaded_bad_whitespace') endif let loaded_bad_whitespace = 1 -highlight default BadWhitespace ctermbg=red guibg=red -autocmd BufWinEnter,WinEnter,FileType * call EnableShowBadWhitespace() - if ! exists( "g:bad_whitespace_patch_filetypes" ) let g:bad_whitespace_patch_filetypes = ['diff', 'git'] endif +if ! exists( "g:bad_whitespace_color_default" ) + highlight default BadWhitespaceDefaultState ctermbg=red guibg=red + let g:bad_whitespace_color_default = 'BadWhitespaceDefaultState' +endif + +execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default +autocmd BufWinEnter,WinEnter,FileType * call EnableShowBadWhitespace() + function! s:ShowBadWhitespace(force) if a:force let b:bad_whitespace_show = 1 endif - autocmd ColorScheme highlight default BadWhitespace ctermbg=red guibg=red + autocmd ColorScheme execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default if exists('b:bad_whitespace_buffer_pattern_prefix') let l:pattern_prefix = b:bad_whitespace_buffer_pattern_prefix else From 7b008f09cff96bc4fa29516f0698895546f3d736 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Tue, 4 Jun 2013 17:42:18 +0200 Subject: [PATCH 04/27] Adapt patch-column width to the number of parents In the hunk header, Git uses a count of '@' characters that is equal to the number of parents plus one. That seems to be generally sensible. Count those characters and start the bad-whitespace match after that column. --- README | 2 +- doc/bad-whitespace.txt | 7 ++++--- plugin/bad-whitespace.vim | 10 +++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README b/README index d48ac49..249d3c4 100644 --- a/README +++ b/README @@ -33,5 +33,5 @@ highlighting in buffers that are not modifiable, which is usually what you want. This makes it compatible with UI buffers that happen to contain bad whitespace, such as the quickfix window, or the fugitive plugin. -This plugin can optionally suppress highlighting the the first two columns of a +This plugin can optionally suppress highlighting in the +/- columns of a patch in unified diff format. diff --git a/doc/bad-whitespace.txt b/doc/bad-whitespace.txt index 5078e2f..42cb9e2 100644 --- a/doc/bad-whitespace.txt +++ b/doc/bad-whitespace.txt @@ -53,9 +53,10 @@ Toggles bad whitespace highlighting for the current buffer. Configuration~ *g:bad_whitespace_patch_filetypes* -You can suppress highlighting of the first (and second in case of a combined -diff) column for selected filetypes, by setting the following variable. The -given value is the default: > +You can suppress highlighting of the +/- columns in patch filetypes, by setting +the variable 'g:bad_whitespace_patch_filetypes'. The column width is derived +from the number fo '@' characters introducing the hunk header. The given value +in this example is the default: > let g:bad_whitespace_patch_filetypes = ['diff', 'git'] < diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 268a32a..121b3bf 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -41,10 +41,14 @@ function! s:SetBufferSpecificWhitespacePattern() if empty(l:patch_filtypes) return endif - let l:start_colum = 2 - if search('^@@@ ', 'nw') - let l:start_colum = 3 + let l:save_cursor = getpos(".") + let l:start_colum = 0 + if search('^@\+', 'we') + let l:start_colum = col('.') + else + echomsg "bad-whitespace: could not find a sequence of @ characters" endif + call setpos('.', l:save_cursor) let b:bad_whitespace_buffer_pattern_prefix = '/\%' . l:start_colum . 'c.\{-\}\zs\s\+\ze' endfunction From cd9c62fc9a22431420fc8964a55ec89866aface8 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Tue, 4 Jun 2013 18:02:05 +0200 Subject: [PATCH 05/27] Add a fallback +/- patch column width configuration variable If the +/- column width could not be derived from the hunk header, the value of g:bad_whitespace_patch_column_width_fallback will be used. The default is 0. This could be useful if a patch is build up programmatically. --- doc/bad-whitespace.txt | 4 ++++ plugin/bad-whitespace.vim | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/bad-whitespace.txt b/doc/bad-whitespace.txt index 42cb9e2..6328b60 100644 --- a/doc/bad-whitespace.txt +++ b/doc/bad-whitespace.txt @@ -59,5 +59,9 @@ from the number fo '@' characters introducing the hunk header. The given value in this example is the default: > let g:bad_whitespace_patch_filetypes = ['diff', 'git'] < + *g:bad_whitespace_patch_column_width_fallback* + +If the +/- column width could not be derived from the hunk header, this value +will be used. The default is 0. vim:tw=78:et:ft=help: diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 121b3bf..324c574 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -11,6 +11,12 @@ if ! exists( "g:bad_whitespace_patch_filetypes" ) let g:bad_whitespace_patch_filetypes = ['diff', 'git'] endif +if ! exists( "g:bad_whitespace_patch_column_width_fallback" ) + " If the column width could not be derived from the hunk header, use this + " value as a fallback. + let g:bad_whitespace_patch_column_width_fallback = 0 +endif + if ! exists( "g:bad_whitespace_color_default" ) highlight default BadWhitespaceDefaultState ctermbg=red guibg=red let g:bad_whitespace_color_default = 'BadWhitespaceDefaultState' @@ -42,11 +48,13 @@ function! s:SetBufferSpecificWhitespacePattern() return endif let l:save_cursor = getpos(".") - let l:start_colum = 0 if search('^@\+', 'we') let l:start_colum = col('.') else - echomsg "bad-whitespace: could not find a sequence of @ characters" + let l:start_colum = g:bad_whitespace_patch_column_width_fallback + 1 + echomsg "bad-whitespace: could not find a sequence of @ characters. " + \ . "Will use fallback +/- column width " + \ . g:bad_whitespace_patch_column_width_fallback endif call setpos('.', l:save_cursor) let b:bad_whitespace_buffer_pattern_prefix = '/\%' . l:start_colum . 'c.\{-\}\zs\s\+\ze' From 4a54833fc795c62fda671d6d7c3012b7d330ce69 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Tue, 4 Jun 2013 18:32:47 +0200 Subject: [PATCH 06/27] Write the bad-whitespace pattern to the search register Add a command that writes the bad-whitespace pattern to the '/' register. This way you can jump to an error by creating a mapping like this: nnoremap W :SetSearchPatternToBadWhitespacen --- doc/bad-whitespace.txt | 8 ++++++++ plugin/bad-whitespace.vim | 25 ++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/doc/bad-whitespace.txt b/doc/bad-whitespace.txt index 6328b60..5187712 100644 --- a/doc/bad-whitespace.txt +++ b/doc/bad-whitespace.txt @@ -51,6 +51,14 @@ Disables bad whitespace highlighting for the current buffer. Toggles bad whitespace highlighting for the current buffer. + *SetSearchPatternToBadWhitespace * + +Write the bad-whitespace pattern for the current buffer to the last search +pattern register. This way you can jump to an error by creating a mapping +like this: > + nnoremap W :SetSearchPatternToBadWhitespacen +< + Configuration~ *g:bad_whitespace_patch_filetypes* You can suppress highlighting of the +/- columns in patch filetypes, by setting diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 324c574..06dd239 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -30,16 +30,26 @@ function! s:ShowBadWhitespace(force) let b:bad_whitespace_show = 1 endif autocmd ColorScheme execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default + let l:whitespace_pattern_global = '/' . s:GetBadWhitespacePattern(0) . '/' + let l:whitespace_pattern_editing = '/' . s:GetBadWhitespacePattern(1) . '/' + execute 'match BadWhitespace ' . l:whitespace_pattern_global + execute 'autocmd InsertLeave match BadWhitespace ' . l:whitespace_pattern_global + execute 'autocmd InsertEnter match BadWhitespace ' . l:whitespace_pattern_editing +endfunction + +function! s:GetBadWhitespacePattern(want_editing_pattern) + " Return the bad-whitespace pattern for the current buffer. If + " want_editing_pattern is nonzero return the pattern for insert mode. if exists('b:bad_whitespace_buffer_pattern_prefix') let l:pattern_prefix = b:bad_whitespace_buffer_pattern_prefix else - let l:pattern_prefix = '/\s\+' + let l:pattern_prefix = '\s\+' + endif + if a:want_editing_pattern + return l:pattern_prefix . '\%#\@ match BadWhitespace ' . l:whitespace_pattern_global - execute 'autocmd InsertEnter match BadWhitespace ' . l:whitespace_pattern_editing endfunction function! s:SetBufferSpecificWhitespacePattern() @@ -57,7 +67,7 @@ function! s:SetBufferSpecificWhitespacePattern() \ . g:bad_whitespace_patch_column_width_fallback endif call setpos('.', l:save_cursor) - let b:bad_whitespace_buffer_pattern_prefix = '/\%' . l:start_colum . 'c.\{-\}\zs\s\+\ze' + let b:bad_whitespace_buffer_pattern_prefix = '\%' . l:start_colum . 'c.\{-\}\zs\s\+\ze' endfunction function! s:HideBadWhitespace(force) @@ -104,3 +114,4 @@ command! -range=% EraseBadWhitespace call EraseBadWhitespace(,ShowBadWhitespace(1) command! HideBadWhitespace call HideBadWhitespace(1) command! ToggleBadWhitespace call ToggleBadWhitespace() +command! SetSearchPatternToBadWhitespace let @/ = GetBadWhitespacePattern(0) From 15f2aea483a7b8c0107635dbbb872d6bd3908808 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Tue, 4 Jun 2013 18:36:57 +0200 Subject: [PATCH 07/27] Use the buffer specific pattern to erase whitespace One assumes that only the highlighted whitespace will be removed by that command. --- plugin/bad-whitespace.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 06dd239..a9584fd 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -105,7 +105,8 @@ endfunction function! s:EraseBadWhitespace(line1,line2) let l:save_cursor = getpos(".") - silent! execute ':' . a:line1 . ',' . a:line2 . 's/\s\+$//' + silent! execute ':' . a:line1 . ',' . a:line2 + \ . 's/' . s:GetBadWhitespacePattern(0) . '//' call setpos('.', l:save_cursor) endfunction From 46891fea21e91b2ca7db10ade81f3ffa9bd575cb Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Wed, 5 Jun 2013 14:40:37 +0200 Subject: [PATCH 08/27] Extract patch pattern getting into a function There might be further pattern getters. Rename the original function to s:SetBufferSpecificPatternPrefix() which indicates that it does not set the full pattern. --- plugin/bad-whitespace.vim | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index a9584fd..660c1d2 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -52,11 +52,7 @@ function! s:GetBadWhitespacePattern(want_editing_pattern) endif endfunction -function! s:SetBufferSpecificWhitespacePattern() - let l:patch_filtypes = filter(copy(g:bad_whitespace_patch_filetypes), 'v:val == &ft') - if empty(l:patch_filtypes) - return - endif +function! s:GetPatternPrefixForPatches() let l:save_cursor = getpos(".") if search('^@\+', 'we') let l:start_colum = col('.') @@ -67,7 +63,14 @@ function! s:SetBufferSpecificWhitespacePattern() \ . g:bad_whitespace_patch_column_width_fallback endif call setpos('.', l:save_cursor) - let b:bad_whitespace_buffer_pattern_prefix = '\%' . l:start_colum . 'c.\{-\}\zs\s\+\ze' + return '\%' . l:start_colum . 'c.\{-\}\zs\s\+\ze' +endfunction + +function! s:SetBufferSpecificPatternPrefix() + let l:patch_filtypes = filter(copy(g:bad_whitespace_patch_filetypes), 'v:val == &ft') + if !empty(l:patch_filtypes) + let b:bad_whitespace_buffer_pattern_prefix = s:GetPatternPrefixForPatches() + endif endfunction function! s:HideBadWhitespace(force) @@ -81,7 +84,7 @@ function! s:EnableShowBadWhitespace() if exists("b:bad_whitespace_show") return endif - call s:SetBufferSpecificWhitespacePattern() + call s:SetBufferSpecificPatternPrefix() if &modifiable call ShowBadWhitespace(0) else From 57d9fcb74680a8453f0d057139f36eb6f2358e38 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Wed, 5 Jun 2013 14:47:35 +0200 Subject: [PATCH 09/27] Turn the highlight off initially for selected filetypes It is now possible to specify filetypes for which no trailing space will be highlighted initially. An example for the new variable: let g:bad_whitespace_off_filetypes = ['text', 'markdown'] --- doc/bad-whitespace.txt | 7 +++++++ plugin/bad-whitespace.vim | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/doc/bad-whitespace.txt b/doc/bad-whitespace.txt index 5187712..da9aee7 100644 --- a/doc/bad-whitespace.txt +++ b/doc/bad-whitespace.txt @@ -67,6 +67,13 @@ from the number fo '@' characters introducing the hunk header. The given value in this example is the default: > let g:bad_whitespace_patch_filetypes = ['diff', 'git'] < + *g:bad_whitespace_off_filetypes* + +Initially turn off bad-whitespace highlighting for the filetypes in this list. +Example: > + let g:bad_whitespace_off_filetypes = ['text', 'markdown'] +< + *g:bad_whitespace_patch_column_width_fallback* If the +/- column width could not be derived from the hunk header, this value diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 660c1d2..30b3f16 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -11,6 +11,10 @@ if ! exists( "g:bad_whitespace_patch_filetypes" ) let g:bad_whitespace_patch_filetypes = ['diff', 'git'] endif +if ! exists( "g:bad_whitespace_off_filetypes" ) + let g:bad_whitespace_off_filetypes = [] +endif + if ! exists( "g:bad_whitespace_patch_column_width_fallback" ) " If the column width could not be derived from the hunk header, use this " value as a fallback. @@ -73,6 +77,13 @@ function! s:SetBufferSpecificPatternPrefix() endif endfunction +function! s:TurnOffBadWhitespaceForConfiguredFiletypes() + let l:off_filtypes = filter(copy(g:bad_whitespace_off_filetypes), 'v:val == &ft') + if !empty(l:off_filtypes) + let b:bad_whitespace_show = 0 + endif +endfun + function! s:HideBadWhitespace(force) if a:force let b:bad_whitespace_show = 0 @@ -81,10 +92,11 @@ function! s:HideBadWhitespace(force) endfunction function! s:EnableShowBadWhitespace() + call s:SetBufferSpecificPatternPrefix() + call s:TurnOffBadWhitespaceForConfiguredFiletypes() if exists("b:bad_whitespace_show") return endif - call s:SetBufferSpecificPatternPrefix() if &modifiable call ShowBadWhitespace(0) else From b49e17f12ad1ffc292268ca72f726ab2014b9527 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Wed, 5 Jun 2013 17:55:09 +0200 Subject: [PATCH 10/27] Make the show/hide toggle persistent by clearing autocommands Clear autocommands when hiding whitespace. --- plugin/bad-whitespace.vim | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 30b3f16..b6cac0a 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -37,8 +37,10 @@ function! s:ShowBadWhitespace(force) let l:whitespace_pattern_global = '/' . s:GetBadWhitespacePattern(0) . '/' let l:whitespace_pattern_editing = '/' . s:GetBadWhitespacePattern(1) . '/' execute 'match BadWhitespace ' . l:whitespace_pattern_global - execute 'autocmd InsertLeave match BadWhitespace ' . l:whitespace_pattern_global - execute 'autocmd InsertEnter match BadWhitespace ' . l:whitespace_pattern_editing + augroup BadWhitespace + execute 'autocmd InsertLeave match BadWhitespace ' . l:whitespace_pattern_global + execute 'autocmd InsertEnter match BadWhitespace ' . l:whitespace_pattern_editing + augroup END endfunction function! s:GetBadWhitespacePattern(want_editing_pattern) @@ -89,6 +91,9 @@ function! s:HideBadWhitespace(force) let b:bad_whitespace_show = 0 endif match none BadWhitespace + augroup BadWhitespace + autocmd! + augroup END endfunction function! s:EnableShowBadWhitespace() From bfacc35815418859e7e897fb75f1a1b5376121cb Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Wed, 5 Jun 2013 18:37:35 +0200 Subject: [PATCH 11/27] Only delete autocommands Do that also before creating new ones to always have a clean slate. --- plugin/bad-whitespace.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index b6cac0a..b2c6296 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -38,6 +38,7 @@ function! s:ShowBadWhitespace(force) let l:whitespace_pattern_editing = '/' . s:GetBadWhitespacePattern(1) . '/' execute 'match BadWhitespace ' . l:whitespace_pattern_global augroup BadWhitespace + autocmd! * execute 'autocmd InsertLeave match BadWhitespace ' . l:whitespace_pattern_global execute 'autocmd InsertEnter match BadWhitespace ' . l:whitespace_pattern_editing augroup END @@ -92,7 +93,7 @@ function! s:HideBadWhitespace(force) endif match none BadWhitespace augroup BadWhitespace - autocmd! + autocmd! * augroup END endfunction From 417ea40bee49c9c99288a0f0e277dddd142b0fb9 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Wed, 5 Jun 2013 18:46:39 +0200 Subject: [PATCH 12/27] off-filetypes: Use the Hide* function after intialization This works for buffers loaded retroactively. --- plugin/bad-whitespace.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index b2c6296..858f9b8 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -83,7 +83,7 @@ endfunction function! s:TurnOffBadWhitespaceForConfiguredFiletypes() let l:off_filtypes = filter(copy(g:bad_whitespace_off_filetypes), 'v:val == &ft') if !empty(l:off_filtypes) - let b:bad_whitespace_show = 0 + call s:HideBadWhitespace(1) endif endfun @@ -99,7 +99,6 @@ endfunction function! s:EnableShowBadWhitespace() call s:SetBufferSpecificPatternPrefix() - call s:TurnOffBadWhitespaceForConfiguredFiletypes() if exists("b:bad_whitespace_show") return endif @@ -108,6 +107,7 @@ function! s:EnableShowBadWhitespace() else call HideBadWhitespace(0) endif + call s:TurnOffBadWhitespaceForConfiguredFiletypes() endfunction function! s:ToggleBadWhitespace() From 8bf566b3809958953dd08d391efa14ad09337306 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Wed, 5 Jun 2013 19:08:48 +0200 Subject: [PATCH 13/27] Configure an alternative error color for some filetypes This is getting a bit experimental, but it looks like it works. A use case is to have a faint color in files where you don't care about trailing highlights that much (notes). Or maybe files that require trailing whitespace (although the filetype specific coloring should handle that). It can be used in 'gitcommit' buffers created with 'git commit --amend --verbose' where you don't want to be distracted by the patch column but still want to see whitespace errors in the message. --- doc/bad-whitespace.txt | 16 ++++++++++++++++ plugin/bad-whitespace.vim | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/doc/bad-whitespace.txt b/doc/bad-whitespace.txt index da9aee7..0e4a2b0 100644 --- a/doc/bad-whitespace.txt +++ b/doc/bad-whitespace.txt @@ -67,6 +67,14 @@ from the number fo '@' characters introducing the hunk header. The given value in this example is the default: > let g:bad_whitespace_patch_filetypes = ['diff', 'git'] < + + *g:bad_whitespace_alternative_color_filetypes* + +Use the alternative color for the given filetypes. The default is empty. +Example: > + let g:bad_whitespace_alternative_color_filetypes = ['text', 'gitcommit'] +< + *g:bad_whitespace_off_filetypes* Initially turn off bad-whitespace highlighting for the filetypes in this list. @@ -74,6 +82,14 @@ Example: > let g:bad_whitespace_off_filetypes = ['text', 'markdown'] < + *g:bad_whitespace_color_alt_default* + +Specify the name of a color to be used for the alternative bad whitespace +highlighting. The default uses a gray background. Example: > + let g:bad_whitespace_color_alt_default = 'Special' +< + + *g:bad_whitespace_patch_column_width_fallback* If the +/- column width could not be derived from the hunk header, this value diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 858f9b8..cb5d366 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -21,26 +21,53 @@ if ! exists( "g:bad_whitespace_patch_column_width_fallback" ) let g:bad_whitespace_patch_column_width_fallback = 0 endif +if ! exists( "g:bad_whitespace_alternative_color_filetypes" ) + let g:bad_whitespace_alternative_color_filetypes = [] +endif + if ! exists( "g:bad_whitespace_color_default" ) highlight default BadWhitespaceDefaultState ctermbg=red guibg=red let g:bad_whitespace_color_default = 'BadWhitespaceDefaultState' endif +if ! exists( "g:bad_whitespace_color_alt_default" ) + highlight default BadWhitespaceAltDefaultState ctermbg=gray guibg=gray + let g:bad_whitespace_color_alt_default = 'BadWhitespaceAltDefaultState' +endif + execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default +execute 'highlight link BadWhitespaceAlternative ' . g:bad_whitespace_color_alt_default autocmd BufWinEnter,WinEnter,FileType * call EnableShowBadWhitespace() +function! s:IsAlternativeColorFiletype() + let l:alt_filtypes = filter(copy(g:bad_whitespace_alternative_color_filetypes), 'v:val == &ft') + if empty(l:alt_filtypes) + return 0 + else + return 1 + endif +endfunction + function! s:ShowBadWhitespace(force) if a:force let b:bad_whitespace_show = 1 endif autocmd ColorScheme execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default + autocmd ColorScheme execute 'highlight link BadWhitespaceAlternative ' . g:bad_whitespace_color_alt_default let l:whitespace_pattern_global = '/' . s:GetBadWhitespacePattern(0) . '/' let l:whitespace_pattern_editing = '/' . s:GetBadWhitespacePattern(1) . '/' - execute 'match BadWhitespace ' . l:whitespace_pattern_global + if s:IsAlternativeColorFiletype() + let l:active_colorscheme = 'BadWhitespaceAlternative' + match none BadWhitespace + else + let l:active_colorscheme = 'BadWhitespace' + match none BadWhitespaceAlternative + endif + execute 'match ' . l:active_colorscheme . ' ' . l:whitespace_pattern_global augroup BadWhitespace autocmd! * - execute 'autocmd InsertLeave match BadWhitespace ' . l:whitespace_pattern_global - execute 'autocmd InsertEnter match BadWhitespace ' . l:whitespace_pattern_editing + execute 'autocmd InsertLeave match ' . l:active_colorscheme . ' ' . l:whitespace_pattern_global + execute 'autocmd InsertEnter match ' . l:active_colorscheme . ' ' . l:whitespace_pattern_editing augroup END endfunction From 82f5ca3bea56d14f35be6d0644f00184966d1b47 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Wed, 5 Jun 2013 19:32:08 +0200 Subject: [PATCH 14/27] Wrap long lines --- plugin/bad-whitespace.vim | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index cb5d366..64e0bd3 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -36,11 +36,14 @@ if ! exists( "g:bad_whitespace_color_alt_default" ) endif execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default -execute 'highlight link BadWhitespaceAlternative ' . g:bad_whitespace_color_alt_default +execute 'highlight link BadWhitespaceAlternative ' + \ . g:bad_whitespace_color_alt_default autocmd BufWinEnter,WinEnter,FileType * call EnableShowBadWhitespace() function! s:IsAlternativeColorFiletype() - let l:alt_filtypes = filter(copy(g:bad_whitespace_alternative_color_filetypes), 'v:val == &ft') + let l:alt_filtypes = filter( + \ copy(g:bad_whitespace_alternative_color_filetypes), + \ 'v:val == &ft') if empty(l:alt_filtypes) return 0 else @@ -52,8 +55,11 @@ function! s:ShowBadWhitespace(force) if a:force let b:bad_whitespace_show = 1 endif - autocmd ColorScheme execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default - autocmd ColorScheme execute 'highlight link BadWhitespaceAlternative ' . g:bad_whitespace_color_alt_default + autocmd ColorScheme execute 'highlight link BadWhitespace ' + \ . g:bad_whitespace_color_default + autocmd ColorScheme + \ execute 'highlight link BadWhitespaceAlternative ' + \ . g:bad_whitespace_color_alt_default let l:whitespace_pattern_global = '/' . s:GetBadWhitespacePattern(0) . '/' let l:whitespace_pattern_editing = '/' . s:GetBadWhitespacePattern(1) . '/' if s:IsAlternativeColorFiletype() @@ -66,8 +72,10 @@ function! s:ShowBadWhitespace(force) execute 'match ' . l:active_colorscheme . ' ' . l:whitespace_pattern_global augroup BadWhitespace autocmd! * - execute 'autocmd InsertLeave match ' . l:active_colorscheme . ' ' . l:whitespace_pattern_global - execute 'autocmd InsertEnter match ' . l:active_colorscheme . ' ' . l:whitespace_pattern_editing + execute 'autocmd InsertLeave match ' . l:active_colorscheme + \ . ' ' . l:whitespace_pattern_global + execute 'autocmd InsertEnter match ' . l:active_colorscheme + \ . ' ' . l:whitespace_pattern_editing augroup END endfunction @@ -101,14 +109,16 @@ function! s:GetPatternPrefixForPatches() endfunction function! s:SetBufferSpecificPatternPrefix() - let l:patch_filtypes = filter(copy(g:bad_whitespace_patch_filetypes), 'v:val == &ft') + let l:patch_filtypes = filter(copy(g:bad_whitespace_patch_filetypes), + \ 'v:val == &ft') if !empty(l:patch_filtypes) let b:bad_whitespace_buffer_pattern_prefix = s:GetPatternPrefixForPatches() endif endfunction function! s:TurnOffBadWhitespaceForConfiguredFiletypes() - let l:off_filtypes = filter(copy(g:bad_whitespace_off_filetypes), 'v:val == &ft') + let l:off_filtypes = filter(copy(g:bad_whitespace_off_filetypes), + \ 'v:val == &ft') if !empty(l:off_filtypes) call s:HideBadWhitespace(1) endif @@ -159,8 +169,10 @@ function! s:EraseBadWhitespace(line1,line2) endfunction " Run :EraseBadWhitespace to remove end of line white space. -command! -range=% EraseBadWhitespace call EraseBadWhitespace(,) +command! -range=% EraseBadWhitespace + \ call EraseBadWhitespace(,) command! ShowBadWhitespace call ShowBadWhitespace(1) command! HideBadWhitespace call HideBadWhitespace(1) command! ToggleBadWhitespace call ToggleBadWhitespace() -command! SetSearchPatternToBadWhitespace let @/ = GetBadWhitespacePattern(0) +command! SetSearchPatternToBadWhitespace + \ let @/ = GetBadWhitespacePattern(0) From 51ce2503cf858e53b07332b1471fa064ff003649 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Wed, 5 Jun 2013 19:51:55 +0200 Subject: [PATCH 15/27] Add documentation for the normal color configuration --- doc/bad-whitespace.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/bad-whitespace.txt b/doc/bad-whitespace.txt index 0e4a2b0..f74018b 100644 --- a/doc/bad-whitespace.txt +++ b/doc/bad-whitespace.txt @@ -82,6 +82,13 @@ Example: > let g:bad_whitespace_off_filetypes = ['text', 'markdown'] < + *g:bad_whitespace_color_default* + +Specify the name of a color to be used for bad whitespace highlighting. The +default uses a red background. Example: > + let g:bad_whitespace_color_default = 'Error' +< + *g:bad_whitespace_color_alt_default* Specify the name of a color to be used for the alternative bad whitespace From 35605e2fb0b923a5b8800ee90ce1ec69b4e4a16f Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Thu, 6 Jun 2013 19:10:48 +0200 Subject: [PATCH 16/27] Handle editing and non-editing patterns separately as a whole This is generally cleaner. Use lists to hold both patterns for a buffer. Rename functions and the variable accordingly --- plugin/bad-whitespace.vim | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 64e0bd3..75822e7 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -82,19 +82,19 @@ endfunction function! s:GetBadWhitespacePattern(want_editing_pattern) " Return the bad-whitespace pattern for the current buffer. If " want_editing_pattern is nonzero return the pattern for insert mode. - if exists('b:bad_whitespace_buffer_pattern_prefix') - let l:pattern_prefix = b:bad_whitespace_buffer_pattern_prefix + if exists('b:bad_whitespace_buffer_specific_patterns') + let l:pattern_prefixes = b:bad_whitespace_buffer_specific_patterns else - let l:pattern_prefix = '\s\+' + let l:pattern_prefixes = ['\s\+$', '\s\+\%#\@ Date: Thu, 6 Jun 2013 18:28:41 +0200 Subject: [PATCH 17/27] Include trailing lines in the default pattern Trailing lines look a bit ugly because the newline on the last non-empty line is highlighted instead of the very last line. --- plugin/bad-whitespace.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 75822e7..7ff6c4a 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -85,7 +85,9 @@ function! s:GetBadWhitespacePattern(want_editing_pattern) if exists('b:bad_whitespace_buffer_specific_patterns') let l:pattern_prefixes = b:bad_whitespace_buffer_specific_patterns else - let l:pattern_prefixes = ['\s\+$', '\s\+\%#\@ Date: Sat, 8 Jun 2013 17:55:46 +0200 Subject: [PATCH 18/27] Silence +/- column fallback message when using 0 This message can bother a bit. When zero there is no bad whitespace hidden from the user. --- plugin/bad-whitespace.vim | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 7ff6c4a..18ac939 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -102,9 +102,11 @@ function! s:GetPatternsForPatches() let l:start_colum = col('.') else let l:start_colum = g:bad_whitespace_patch_column_width_fallback + 1 - echomsg "bad-whitespace: could not find a sequence of @ characters. " - \ . "Will use fallback +/- column width " - \ . g:bad_whitespace_patch_column_width_fallback + if g:bad_whitespace_patch_column_width_fallback != 0 + echomsg "bad-whitespace: No @ char match. " + \ . "Will use fallback +/- column width " + \ . g:bad_whitespace_patch_column_width_fallback + endif endif call setpos('.', l:save_cursor) let l:patch_pattern = '\%' . l:start_colum . 'c.\{-\}\zs\s\+\ze' From 40302200f7ab8d7af060a1abbafddc61f8035ef1 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Tue, 11 Jun 2013 14:23:22 +0200 Subject: [PATCH 19/27] README: Add a note about the new features --- README | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README b/README index 249d3c4..2eb6bc6 100644 --- a/README +++ b/README @@ -1,5 +1,15 @@ bad-whitespace - Highlights whitespace at the end of lines +Features +-------- +There are configuration options that allow you to adjust filetype-specific +behavior of the highlighting. You can turn the highlighting initially off, use an +alternative (e.g. faint) color, or suppress highlighting in the +/- columns of +patches in unified diff format. + +Original Note +------------- + http://www.vim.org/scripts/script.php?script_id=3735 Author: Bit Connor From 057f7fd86a08694168cb163466938ee591e4d876 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Fri, 16 Aug 2013 19:03:32 +0200 Subject: [PATCH 20/27] Rename variable to l:active_highlight It was never a colorscheme --- plugin/bad-whitespace.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 18ac939..35e473b 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -63,18 +63,18 @@ function! s:ShowBadWhitespace(force) let l:whitespace_pattern_global = '/' . s:GetBadWhitespacePattern(0) . '/' let l:whitespace_pattern_editing = '/' . s:GetBadWhitespacePattern(1) . '/' if s:IsAlternativeColorFiletype() - let l:active_colorscheme = 'BadWhitespaceAlternative' + let l:active_highlight = 'BadWhitespaceAlternative' match none BadWhitespace else - let l:active_colorscheme = 'BadWhitespace' + let l:active_highlight = 'BadWhitespace' match none BadWhitespaceAlternative endif - execute 'match ' . l:active_colorscheme . ' ' . l:whitespace_pattern_global + execute 'match ' . l:active_highlight . ' ' . l:whitespace_pattern_global augroup BadWhitespace autocmd! * - execute 'autocmd InsertLeave match ' . l:active_colorscheme + execute 'autocmd InsertLeave match ' . l:active_highlight \ . ' ' . l:whitespace_pattern_global - execute 'autocmd InsertEnter match ' . l:active_colorscheme + execute 'autocmd InsertEnter match ' . l:active_highlight \ . ' ' . l:whitespace_pattern_editing augroup END endfunction From d920a84c3f122bc92adf09b83f4d03869206f2b8 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Fri, 16 Aug 2013 20:20:05 +0200 Subject: [PATCH 21/27] Use the more versatile matchadd() mechanism This frees the :match highlighting for ad-hoc usage and will generally facilitate adding other matches independently from bad-whitespace. --- plugin/bad-whitespace.vim | 44 ++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 35e473b..11d0d78 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -35,6 +35,14 @@ if ! exists( "g:bad_whitespace_color_alt_default" ) let g:bad_whitespace_color_alt_default = 'BadWhitespaceAltDefaultState' endif +if ! exists( "g:bad_whitespace_match_priority" ) + " The priority used for the bad-whitespace match in matchadd() + let g:bad_whitespace_match_priority = -20 +endif + +" The IDs 1-3 are reserved and therefore never used by matchadd. +let s:InvalidMatchId = 1 + execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default execute 'highlight link BadWhitespaceAlternative ' \ . g:bad_whitespace_color_alt_default @@ -51,6 +59,22 @@ function! s:IsAlternativeColorFiletype() endif endfunction +fun! s:DeleteBadWhitespaceMatch() + " Delete the bad whitespace match in this window + if exists("w:bad_whitespace_match_id") + \ && w:bad_whitespace_match_id != s:InvalidMatchId + call matchdelete(w:bad_whitespace_match_id) + let w:bad_whitespace_match_id = s:InvalidMatchId + endif +endfun + +fun! s:SetBadWhitespaceMatch(highlight_group, whitespace_pattern) + " Set or update the bad whitespace match in this window + call s:DeleteBadWhitespaceMatch() + let w:bad_whitespace_match_id = matchadd(a:highlight_group, + \ a:whitespace_pattern, g:bad_whitespace_match_priority) +endfun + function! s:ShowBadWhitespace(force) if a:force let b:bad_whitespace_show = 1 @@ -60,22 +84,22 @@ function! s:ShowBadWhitespace(force) autocmd ColorScheme \ execute 'highlight link BadWhitespaceAlternative ' \ . g:bad_whitespace_color_alt_default - let l:whitespace_pattern_global = '/' . s:GetBadWhitespacePattern(0) . '/' - let l:whitespace_pattern_editing = '/' . s:GetBadWhitespacePattern(1) . '/' + let l:whitespace_pattern_global = s:GetBadWhitespacePattern(0) + let l:whitespace_pattern_editing = s:GetBadWhitespacePattern(1) if s:IsAlternativeColorFiletype() let l:active_highlight = 'BadWhitespaceAlternative' - match none BadWhitespace else let l:active_highlight = 'BadWhitespace' - match none BadWhitespaceAlternative endif - execute 'match ' . l:active_highlight . ' ' . l:whitespace_pattern_global + call s:SetBadWhitespaceMatch(l:active_highlight, l:whitespace_pattern_global) augroup BadWhitespace autocmd! * - execute 'autocmd InsertLeave match ' . l:active_highlight - \ . ' ' . l:whitespace_pattern_global - execute 'autocmd InsertEnter match ' . l:active_highlight - \ . ' ' . l:whitespace_pattern_editing + execute 'autocmd InsertLeave call SetBadWhitespaceMatch( "' + \ . l:active_highlight . '", ''' + \ . l:whitespace_pattern_global . ''')' + execute 'autocmd InsertEnter call SetBadWhitespaceMatch( "' + \ . l:active_highlight . '", ''' + \ . l:whitespace_pattern_editing . ''')' augroup END endfunction @@ -133,7 +157,7 @@ function! s:HideBadWhitespace(force) if a:force let b:bad_whitespace_show = 0 endif - match none BadWhitespace + call s:DeleteBadWhitespaceMatch() augroup BadWhitespace autocmd! * augroup END From 1020a16a413470b8e3eb30a69f71f49ae52e4f91 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Sat, 17 Aug 2013 13:38:49 +0200 Subject: [PATCH 22/27] Sync highlighting on WinEnter Every buffer has a variable that determines if bad whitespace should be displayed or not. The display will be synced on WinEnter. I expect the common use case is to either permanently view them or not. So this should work out fine in most cases. A window-specific variable would be initialized with the default on each split which is presumably not what is wanted. --- plugin/bad-whitespace.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index 11d0d78..c4467a3 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -165,10 +165,10 @@ endfunction function! s:EnableShowBadWhitespace() call s:SetBufferSpecificPatterns() - if exists("b:bad_whitespace_show") - return + if !exists("b:bad_whitespace_show") + let b:bad_whitespace_show = 1 endif - if &modifiable + if &modifiable && b:bad_whitespace_show call ShowBadWhitespace(0) else call HideBadWhitespace(0) From 68a3ac3d9f92cd3edf83ea0471c03d7853d60e17 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Sat, 17 Aug 2013 13:56:19 +0200 Subject: [PATCH 23/27] Refactor handling of the start-up state Obtain the default during buffer-init instead of turning the display off later. Rename accordingly. --- plugin/bad-whitespace.vim | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index c4467a3..c9f564f 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -145,11 +145,13 @@ function! s:SetBufferSpecificPatterns() endif endfunction -function! s:TurnOffBadWhitespaceForConfiguredFiletypes() +function! s:GetDisplayOnOffDefaultForFiletype() let l:off_filtypes = filter(copy(g:bad_whitespace_off_filetypes), \ 'v:val == &ft') - if !empty(l:off_filtypes) - call s:HideBadWhitespace(1) + if empty(l:off_filtypes) + return 1 + else + return 0 endif endfun @@ -166,14 +168,13 @@ endfunction function! s:EnableShowBadWhitespace() call s:SetBufferSpecificPatterns() if !exists("b:bad_whitespace_show") - let b:bad_whitespace_show = 1 + let b:bad_whitespace_show = s:GetDisplayOnOffDefaultForFiletype() endif if &modifiable && b:bad_whitespace_show call ShowBadWhitespace(0) else call HideBadWhitespace(0) endif - call s:TurnOffBadWhitespaceForConfiguredFiletypes() endfunction function! s:ToggleBadWhitespace() From 81daa36747e61a17c4ed64b4229dc2774318fd2d Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Sun, 18 Aug 2013 12:20:30 +0200 Subject: [PATCH 24/27] Incorporate &modifiable into buffer initialization This will now also sync toggling of unmodifiable buffers --- plugin/bad-whitespace.vim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index c9f564f..ccc8476 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -168,9 +168,13 @@ endfunction function! s:EnableShowBadWhitespace() call s:SetBufferSpecificPatterns() if !exists("b:bad_whitespace_show") - let b:bad_whitespace_show = s:GetDisplayOnOffDefaultForFiletype() + if &modifiable + let b:bad_whitespace_show = s:GetDisplayOnOffDefaultForFiletype() + else + let b:bad_whitespace_show = 0 + endif endif - if &modifiable && b:bad_whitespace_show + if b:bad_whitespace_show call ShowBadWhitespace(0) else call HideBadWhitespace(0) From e4f0ba91a289a972a8836a0004ca609cb95579d2 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Sun, 18 Aug 2013 12:30:36 +0200 Subject: [PATCH 25/27] Treat b:bad_whitespace_show as display state Remove the force argument from some functions. Show/Hide does now always change the display state. Previously, it was used do differentiate an explicit change request by the user from window/buffer setup, but the buffer setup is now only done once and windows are synced on enter. --- plugin/bad-whitespace.vim | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index ccc8476..ae35fe9 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -75,10 +75,8 @@ fun! s:SetBadWhitespaceMatch(highlight_group, whitespace_pattern) \ a:whitespace_pattern, g:bad_whitespace_match_priority) endfun -function! s:ShowBadWhitespace(force) - if a:force - let b:bad_whitespace_show = 1 - endif +function! s:ShowBadWhitespace() + let b:bad_whitespace_show = 1 autocmd ColorScheme execute 'highlight link BadWhitespace ' \ . g:bad_whitespace_color_default autocmd ColorScheme @@ -155,10 +153,8 @@ function! s:GetDisplayOnOffDefaultForFiletype() endif endfun -function! s:HideBadWhitespace(force) - if a:force - let b:bad_whitespace_show = 0 - endif +function! s:HideBadWhitespace() + let b:bad_whitespace_show = 0 call s:DeleteBadWhitespaceMatch() augroup BadWhitespace autocmd! * @@ -175,23 +171,17 @@ function! s:EnableShowBadWhitespace() endif endif if b:bad_whitespace_show - call ShowBadWhitespace(0) + call ShowBadWhitespace() else - call HideBadWhitespace(0) + call HideBadWhitespace() endif endfunction function! s:ToggleBadWhitespace() - if !exists("b:bad_whitespace_show") - let b:bad_whitespace_show = 0 - if &modifiable - let b:bad_whitespace_show = 1 - endif - endif if b:bad_whitespace_show - call HideBadWhitespace(1) + call HideBadWhitespace() else - call ShowBadWhitespace(1) + call ShowBadWhitespace() endif endfunction @@ -205,8 +195,8 @@ endfunction " Run :EraseBadWhitespace to remove end of line white space. command! -range=% EraseBadWhitespace \ call EraseBadWhitespace(,) -command! ShowBadWhitespace call ShowBadWhitespace(1) -command! HideBadWhitespace call HideBadWhitespace(1) +command! ShowBadWhitespace call ShowBadWhitespace() +command! HideBadWhitespace call HideBadWhitespace() command! ToggleBadWhitespace call ToggleBadWhitespace() command! SetSearchPatternToBadWhitespace \ let @/ = GetBadWhitespacePattern(0) From 816249ffb729df6d20a4ada22cb611f1a5ef4bff Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Tue, 10 Sep 2013 18:48:30 +0200 Subject: [PATCH 26/27] Reinitialize the buffer when the filetype changes This will apply the on/off setting when the filetype is set retroactively. --- plugin/bad-whitespace.vim | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugin/bad-whitespace.vim b/plugin/bad-whitespace.vim index ae35fe9..34a92a9 100644 --- a/plugin/bad-whitespace.vim +++ b/plugin/bad-whitespace.vim @@ -46,7 +46,8 @@ let s:InvalidMatchId = 1 execute 'highlight link BadWhitespace ' . g:bad_whitespace_color_default execute 'highlight link BadWhitespaceAlternative ' \ . g:bad_whitespace_color_alt_default -autocmd BufWinEnter,WinEnter,FileType * call EnableShowBadWhitespace() +autocmd BufWinEnter,WinEnter * call EnableShowBadWhitespace() +autocmd FileType * call ReInitBuffer() function! s:IsAlternativeColorFiletype() let l:alt_filtypes = filter( @@ -153,6 +154,13 @@ function! s:GetDisplayOnOffDefaultForFiletype() endif endfun +fun! s:ReInitBuffer() + if exists("b:bad_whitespace_show") + unlet b:bad_whitespace_show + endif + call s:EnableShowBadWhitespace() +endfun + function! s:HideBadWhitespace() let b:bad_whitespace_show = 0 call s:DeleteBadWhitespaceMatch() From 1c57e115901aa2a70ab37de62c60dd5760eecf36 Mon Sep 17 00:00:00 2001 From: Dirk Wallenstein Date: Sat, 12 Oct 2013 15:38:03 +0200 Subject: [PATCH 27/27] README: redirect to match-control Vim-Bad-Whitespace can be replaced with Vim-Match-Control. The latter offers a more general solution and allows you to have multiple highlighting control units, as it were. --- README | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README b/README index 2eb6bc6..aac3c81 100644 --- a/README +++ b/README @@ -1,3 +1,6 @@ +**UPDATE** For a general solution take a look at: +https://github.com/dirkwallenstein/vim-match-control + bad-whitespace - Highlights whitespace at the end of lines Features