Skip to content

Don't highlight +/- columns in patches (configurable) (take 2) #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a57416e
Use a load guard and put setup code at the top-level
dirkwallenstein Jun 3, 2013
1b2225e
Suppress highlighting empty +/- columns of patches
dirkwallenstein Jun 3, 2013
8041859
Introduce a BadWhitespace highlight link
dirkwallenstein Jun 3, 2013
7b008f0
Adapt patch-column width to the number of parents
dirkwallenstein Jun 4, 2013
cd9c62f
Add a fallback +/- patch column width configuration variable
dirkwallenstein Jun 4, 2013
4a54833
Write the bad-whitespace pattern to the search register
dirkwallenstein Jun 4, 2013
15f2aea
Use the buffer specific pattern to erase whitespace
dirkwallenstein Jun 4, 2013
46891fe
Extract patch pattern getting into a function
dirkwallenstein Jun 5, 2013
57d9fcb
Turn the highlight off initially for selected filetypes
dirkwallenstein Jun 5, 2013
b49e17f
Make the show/hide toggle persistent by clearing autocommands
dirkwallenstein Jun 5, 2013
bfacc35
Only delete <buffer> autocommands
dirkwallenstein Jun 5, 2013
417ea40
off-filetypes: Use the Hide* function after intialization
dirkwallenstein Jun 5, 2013
8bf566b
Configure an alternative error color for some filetypes
dirkwallenstein Jun 5, 2013
82f5ca3
Wrap long lines
dirkwallenstein Jun 5, 2013
51ce250
Add documentation for the normal color configuration
dirkwallenstein Jun 5, 2013
35605e2
Handle editing and non-editing patterns separately as a whole
dirkwallenstein Jun 6, 2013
da3fa9b
Include trailing lines in the default pattern
dirkwallenstein Jun 6, 2013
940c7e1
Silence +/- column fallback message when using 0
dirkwallenstein Jun 8, 2013
4030220
README: Add a note about the new features
dirkwallenstein Jun 11, 2013
057f7fd
Rename variable to l:active_highlight
dirkwallenstein Aug 16, 2013
d920a84
Use the more versatile matchadd() mechanism
dirkwallenstein Aug 16, 2013
1020a16
Sync highlighting on WinEnter
dirkwallenstein Aug 17, 2013
68a3ac3
Refactor handling of the start-up state
dirkwallenstein Aug 17, 2013
81daa36
Incorporate &modifiable into buffer initialization
dirkwallenstein Aug 18, 2013
e4f0ba9
Treat b:bad_whitespace_show as display state
dirkwallenstein Aug 18, 2013
816249f
Reinitialize the buffer when the filetype changes
dirkwallenstein Sep 10, 2013
1c57e11
README: redirect to match-control
dirkwallenstein Oct 12, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
**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
--------
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 <[email protected]>
Expand Down Expand Up @@ -32,3 +45,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 in the +/- columns of a
patch in unified diff format.
51 changes: 51 additions & 0 deletions doc/bad-whitespace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,55 @@ 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 <Leader>W :<C-U>SetSearchPatternToBadWhitespace<CR>n
<

Configuration~ *g:bad_whitespace_patch_filetypes*

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']
<

*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.
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
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
will be used. The default is 0.

vim:tw=78:et:ft=help:
210 changes: 180 additions & 30 deletions plugin/bad-whitespace.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,209 @@
" Maintainer: Bit Connor <[email protected]>
" Version: 0.3

function! s:ShowBadWhitespace(force)
if a:force
let b:bad_whitespace_show = 1
if exists('loaded_bad_whitespace')
finish
endif
let loaded_bad_whitespace = 1

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.
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

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
autocmd BufWinEnter,WinEnter * call <SID>EnableShowBadWhitespace()
autocmd FileType * call <SID>ReInitBuffer()

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
highlight default BadWhitespace ctermbg=red guibg=red
autocmd ColorScheme <buffer> highlight default BadWhitespace ctermbg=red guibg=red
match BadWhitespace /\s\+$/
autocmd InsertLeave <buffer> match BadWhitespace /\s\+$/
autocmd InsertEnter <buffer> match BadWhitespace /\s\+\%#\@<!$/
endfunction

function! s:HideBadWhitespace(force)
if a:force
let b:bad_whitespace_show = 0
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()
let b:bad_whitespace_show = 1
autocmd ColorScheme <buffer> execute 'highlight link BadWhitespace '
\ . g:bad_whitespace_color_default
autocmd ColorScheme <buffer>
\ 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()
let l:active_highlight = 'BadWhitespaceAlternative'
else
let l:active_highlight = 'BadWhitespace'
endif
match none BadWhitespace
call s:SetBadWhitespaceMatch(l:active_highlight, l:whitespace_pattern_global)
augroup BadWhitespace
autocmd! * <buffer>
execute 'autocmd InsertLeave <buffer> call <SID>SetBadWhitespaceMatch( "'
\ . l:active_highlight . '", '''
\ . l:whitespace_pattern_global . ''')'
execute 'autocmd InsertEnter <buffer> call <SID>SetBadWhitespaceMatch( "'
\ . l:active_highlight . '", '''
\ . l:whitespace_pattern_editing . ''')'
augroup END
endfunction

function! s:EnableShowBadWhitespace()
if exists("b:bad_whitespace_show")
return
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_specific_patterns')
let l:pattern_prefixes = b:bad_whitespace_buffer_specific_patterns
else
let l:pattern_prefixes = [
\ '\_s\+\%$\|\s\+$',
\ '\_s\+\%#\@<!\%$\|\s\+\%#\@<!$']
endif
if &modifiable
call <SID>ShowBadWhitespace(0)
if a:want_editing_pattern
return l:pattern_prefixes[1]
else
call <SID>HideBadWhitespace(0)
return l:pattern_prefixes[0]
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
function! s:GetPatternsForPatches()
let l:save_cursor = getpos(".")
if search('^@\+', 'we')
let l:start_colum = col('.')
else
let l:start_colum = g:bad_whitespace_patch_column_width_fallback + 1
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'
return [l:patch_pattern . '$', l:patch_pattern . '\%#\@<!$']
endfunction

function! s:SetBufferSpecificPatterns()
let l:patch_filtypes = filter(copy(g:bad_whitespace_patch_filetypes),
\ 'v:val == &ft')
if !empty(l:patch_filtypes)
let b:bad_whitespace_buffer_specific_patterns = s:GetPatternsForPatches()
endif
endfunction

function! s:GetDisplayOnOffDefaultForFiletype()
let l:off_filtypes = filter(copy(g:bad_whitespace_off_filetypes),
\ 'v:val == &ft')
if empty(l:off_filtypes)
return 1
else
return 0
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()
augroup BadWhitespace
autocmd! * <buffer>
augroup END
endfunction

function! s:EnableShowBadWhitespace()
call s:SetBufferSpecificPatterns()
if !exists("b:bad_whitespace_show")
if &modifiable
let b:bad_whitespace_show = s:GetDisplayOnOffDefaultForFiletype()
else
let b:bad_whitespace_show = 0
endif
endif
if b:bad_whitespace_show
call <SID>HideBadWhitespace(1)
call <SID>ShowBadWhitespace()
else
call <SID>ShowBadWhitespace(1)
call <SID>HideBadWhitespace()
endif
endfunction

autocmd BufWinEnter,WinEnter,FileType * call <SID>EnableShowBadWhitespace()
function! s:ToggleBadWhitespace()
if b:bad_whitespace_show
call <SID>HideBadWhitespace()
else
call <SID>ShowBadWhitespace()
endif
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

" Run :EraseBadWhitespace to remove end of line white space.
command! -range=% EraseBadWhitespace call <SID>EraseBadWhitespace(<line1>,<line2>)
command! ShowBadWhitespace call <SID>ShowBadWhitespace(1)
command! HideBadWhitespace call <SID>HideBadWhitespace(1)
command! -range=% EraseBadWhitespace
\ call <SID>EraseBadWhitespace(<line1>,<line2>)
command! ShowBadWhitespace call <SID>ShowBadWhitespace()
command! HideBadWhitespace call <SID>HideBadWhitespace()
command! ToggleBadWhitespace call <SID>ToggleBadWhitespace()
command! SetSearchPatternToBadWhitespace
\ let @/ = <SID>GetBadWhitespacePattern(0)