Skip to content
Merged
Changes from all commits
Commits
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
36 changes: 26 additions & 10 deletions plugin/better-whitespace.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ call s:InitVariable('g:better_whitespace_filetypes_blacklist', [])
" Only init once
let s:better_whitespace_initialized = 0

" Like windo but restore the current window.
function! s:Windo(command)
let currwin=winnr()
execute 'windo ' . a:command
execute currwin . 'wincmd w'
endfunction

" Like tabdo but restore the current tab.
function! s:Tabdo(command)
let currTab=tabpagenr()
execute 'tabdo ' . a:command
execute 'tabn ' . currTab
endfunction

" Execute command in all windows (across tabs).
function! s:InAllWindows(command)
call s:Tabdo("call s:Windo('".substitute(a:command, "'", "''", 'g')."')")
endfunction

" Ensure the 'ExtraWhitespace' highlight group has been defined
function! s:WhitespaceInit()
" Check if the user has already defined highlighting for this group
Expand All @@ -53,7 +72,7 @@ function! s:EnableWhitespace()
let g:better_whitespace_enabled = 1
call <SID>WhitespaceInit()
" Match default whitespace
match ExtraWhitespace /\s\+$/
call s:InAllWindows('match ExtraWhitespace /\s\+$/')
call <SID>SetupAutoCommands()
echo "Whitespace Highlighting: Enabled"
endif
Expand All @@ -64,8 +83,7 @@ function! s:DisableWhitespace()
if g:better_whitespace_enabled == 1
let g:better_whitespace_enabled = 0
" Clear current whitespace matches
match ExtraWhitespace ''
syn clear ExtraWhitespace
call s:InAllWindows("match ExtraWhitespace '' | syn clear ExtraWhitespace")
call <SID>SetupAutoCommands()
echo "Whitespace Highlighting: Disabled"
endif
Expand All @@ -92,13 +110,12 @@ function! s:CurrentLineWhitespaceOff( level )
if a:level == 'hard'
let g:current_line_whitespace_disabled_hard = 1
let g:current_line_whitespace_disabled_soft = 0
syn clear ExtraWhitespace
match ExtraWhitespace /\s\+$/
call s:InAllWindows('syn clear ExtraWhitespace | match ExtraWhitespace /\s\+$/')
echo "Current Line Hightlight Off (hard)"
elseif a:level == 'soft'
let g:current_line_whitespace_disabled_soft = 1
let g:current_line_whitespace_disabled_hard = 0
match ExtraWhitespace ''
call s:InAllWindows("match ExtraWhitespace ''")
echo "Current Line Hightlight Off (soft)"
endif
" Re-run auto commands with the new settings
Expand All @@ -112,8 +129,7 @@ function! s:CurrentLineWhitespaceOn()
let g:current_line_whitespace_disabled_hard = 0
let g:current_line_whitespace_disabled_soft = 0
call <SID>SetupAutoCommands()
syn clear ExtraWhitespace
match ExtraWhitespace /\s\+$/
call s:InAllWindows('syn clear ExtraWhitespace | match ExtraWhitespace /\s\+$/')
echo "Current Line Hightlight On"
endif
endfunction
Expand Down Expand Up @@ -198,8 +214,8 @@ function! <SID>SetupAutoCommands()
autocmd BufWinLeave * call clearmatches()
else
" Highlight extraneous whitespace at the end of lines, but not the
" current line
syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+$/
" current line.
call s:InAllWindows('syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+$/')
autocmd InsertEnter * syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+\%#\@!$/ containedin=ALL
autocmd InsertLeave,BufReadPost * syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+$/ containedin=ALL
endif
Expand Down