diff --git a/plugin/better-whitespace.vim b/plugin/better-whitespace.vim index 353e43b..f584a48 100644 --- a/plugin/better-whitespace.vim +++ b/plugin/better-whitespace.vim @@ -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 @@ -53,7 +72,7 @@ function! s:EnableWhitespace() let g:better_whitespace_enabled = 1 call WhitespaceInit() " Match default whitespace - match ExtraWhitespace /\s\+$/ + call s:InAllWindows('match ExtraWhitespace /\s\+$/') call SetupAutoCommands() echo "Whitespace Highlighting: Enabled" endif @@ -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 SetupAutoCommands() echo "Whitespace Highlighting: Disabled" endif @@ -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 @@ -112,8 +129,7 @@ function! s:CurrentLineWhitespaceOn() let g:current_line_whitespace_disabled_hard = 0 let g:current_line_whitespace_disabled_soft = 0 call SetupAutoCommands() - syn clear ExtraWhitespace - match ExtraWhitespace /\s\+$/ + call s:InAllWindows('syn clear ExtraWhitespace | match ExtraWhitespace /\s\+$/') echo "Current Line Hightlight On" endif endfunction @@ -198,8 +214,8 @@ function! 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