@@ -51,7 +51,7 @@ call s:InitVariable('strip_whitelines_at_eof', 0)
5151call s: InitVariable (' strip_whitespace_confirm' , 1 )
5252
5353" Set this to blacklist specific filetypes
54- call s: InitVariable (' better_whitespace_filetypes_blacklist' , [' diff' , ' gitcommit' , ' unite' , ' qf' , ' help' , ' markdown' ])
54+ call s: InitVariable (' better_whitespace_filetypes_blacklist' , [' diff' , ' gitcommit' , ' unite' , ' qf' , ' help' , ' markdown' , ' fugitive ' ])
5555
5656" Skip empty (whitespace-only) lines for highlighting
5757call s: InitVariable (' better_whitespace_skip_empty_lines' , 0 )
@@ -116,19 +116,27 @@ else
116116 endfunction
117117endif
118118
119+ " Function that clears the search entries of BetterWhiteSpace by rolling back to the given index
120+ function ! s: RestoreSearchHistory (index )
121+ while histnr (' search' ) > a: index
122+ call histdel (' search' , -1 )
123+ endwhile
124+ let @/ = histget (' search' , -1 )
125+ endfunction
126+
119127" query per-buffer setting for whitespace highlighting
120128function ! s: ShouldHighlight ()
121129 " Guess from the filetype if a) not locally decided, b) globally enabled, c) there is enough information
122- if ! exists ( ' b:better_whitespace_enabled ' ) && g: better_whitespace_enabled == 1 && ! ( empty (& buftype ) && empty (& filetype ))
123- let b: better_whitespace_enabled = &buftype != ' nofile ' && & buftype != ' popup ' && index (g: better_whitespace_filetypes_blacklist , &ft ) == -1
130+ if get ( b: , ' better_whitespace_guess ' , 1 ) && g: better_whitespace_enabled == 1
131+ let b: better_whitespace_enabled = ( empty ( &buftype ) || & buftype == ' acwrite ' ) && index (g: better_whitespace_filetypes_blacklist , &ft ) == -1
124132 endif
125133 return get (b: , ' better_whitespace_enabled' , g: better_whitespace_enabled )
126134endfunction
127135
128136" query per-buffer setting for whitespace stripping
129137function ! s: ShouldStripWhitespaceOnSave ()
130138 " Guess from local whitespace enabled-ness and global whitespace setting
131- if ! exists ( ' b:strip_whitespace_on_save ' ) && exists (' b:better_whitespace_enabled' )
139+ if get ( b: , ' strip_whitespace_guess ' , 1 ) && exists (' b:better_whitespace_enabled' )
132140 let b: strip_whitespace_on_save = b: better_whitespace_enabled && g: strip_whitespace_on_save && &modifiable &&
133141 \ (g: strip_max_file_size == 0 || g: strip_max_file_size >= line (' $' ))
134142 endif
@@ -193,7 +201,7 @@ endfunction
193201" Removes all extraneous whitespace in the file
194202function ! s: StripWhitespace (line1, line2)
195203 " Save the current search and cursor position
196- let _s= @/
204+ let _s = histnr ( ' search ' )
197205 let l = line (' .' )
198206 let c = col (' .' )
199207
@@ -205,7 +213,7 @@ function! s:StripWhitespace(line1, line2)
205213 endif
206214
207215 " Restore the saved search and cursor position
208- let @/ = _s
216+ call <SID> RestoreSearchHistory (_s)
209217 call cursor (l , c )
210218endfunction
211219
@@ -374,31 +382,37 @@ autocmd ColorScheme * call <SID>WhitespaceInit()
374382
375383function ! s: EnableWhitespace ()
376384 let b: better_whitespace_enabled = 1
385+ let b: better_whitespace_guess = 0
377386 call <SID> SetupAutoCommands ()
378387endfunction
379388
380389function ! s: DisableWhitespace ()
381390 let b: better_whitespace_enabled = 0
391+ let b: better_whitespace_guess = 0
382392 call <SID> SetupAutoCommands ()
383393endfunction
384394
385395function ! s: ToggleWhitespace ()
386396 let b: better_whitespace_enabled = 1 - <SID> ShouldHighlight ()
397+ let b: better_whitespace_guess = 0
387398 call <SID> SetupAutoCommands ()
388399endfunction
389400
390401function ! s: EnableStripWhitespaceOnSave ()
391402 let b: strip_whitespace_on_save = 1
403+ let b: strip_whitespace_guess = 0
392404 call <SID> SetupAutoCommands ()
393405endfunction
394406
395407function ! s: DisableStripWhitespaceOnSave ()
396408 let b: strip_whitespace_on_save = 0
409+ let b: strip_whitespace_guess = 0
397410 call <SID> SetupAutoCommands ()
398411endfunction
399412
400413function ! s: ToggleStripWhitespaceOnSave ()
401414 let b: strip_whitespace_on_save = 1 - <SID> ShouldStripWhitespaceOnSave ()
415+ let b: strip_whitespace_guess = 0
402416 call <SID> SetupAutoCommands ()
403417endfunction
404418
0 commit comments