Files
Ingo Karkat a12144ae80 FIX: Subsequent automatic background adaptations only work in recent Vim and :hi def link
Vim 8.2.2034 somehow restores a :hi def link after a colorscheme change, so the override of the adapted ShowTrailingWhitespace highlight group is automatically undone. But this does not happen in Vim 7.4.1689 and also not with :hi link (what a customization would use), so it cannot be relied on.
Instead, check for a linked highlight group only once on plugin startup and store a linked ID in g:ShowTrailingWhitespace_LinkedSyntaxId.
ShowTrailingWhitespace#Color#EnsureVisibleBackgroundColor() then reuses that original ID. It also sets a s:didAutomaticBackground flag if it had modified the ShowTrailingWhitespace highlight group, and restores the link if for a changed colorscheme this adaptation is no longer necessary.
2020-12-20 11:08:07 +01:00

66 lines
2.6 KiB
VimL

" ShowTrailingWhitespace.vim: Detect unwanted whitespace at the end of lines.
"
" DEPENDENCIES:
"
" Copyright: (C) 2012-2020 Ingo Karkat
" The VIM LICENSE applies to this script; see ':help copyright'.
"
" Maintainer: Ingo Karkat <ingo@karkat.de>
" Avoid installing twice or when in unsupported Vim version.
if exists('g:loaded_ShowTrailingWhitespace') || (v:version == 701 && ! exists('*matchadd')) || (v:version < 701)
finish
endif
let g:loaded_ShowTrailingWhitespace = 1
"- configuration ---------------------------------------------------------------
let g:ShowTrailingWhitespace#HighlightGroup = 'ShowTrailingWhitespace'
if ! exists('g:ShowTrailingWhitespace')
let g:ShowTrailingWhitespace = 1
endif
if ! exists('g:ShowTrailingWhitespace_FilterFunc')
if v:version < 702
" Vim 7.0/1 need preloading of functions referenced in Funcrefs.
runtime autoload/ShowTrailingWhitespace/Filter.vim
endif
let g:ShowTrailingWhitespace_FilterFunc = function('ShowTrailingWhitespace#Filter#Default')
endif
if ! exists('g:ShowTrailingWhitespace_IsAutomaticBackground')
let g:ShowTrailingWhitespace_IsAutomaticBackground = 1
endif
"- autocmds --------------------------------------------------------------------
augroup ShowTrailingWhitespace
autocmd!
autocmd BufWinEnter,InsertLeave * call ShowTrailingWhitespace#Detect(0)
autocmd InsertEnter * call ShowTrailingWhitespace#Detect(1)
augroup END
"- highlight groups ------------------------------------------------------------
execute printf('highlight def link %s Error', g:ShowTrailingWhitespace#HighlightGroup)
if g:ShowTrailingWhitespace_IsAutomaticBackground
let g:ShowTrailingWhitespace_LinkedSyntaxId = synIDtrans(hlID(g:ShowTrailingWhitespace#HighlightGroup))
if g:ShowTrailingWhitespace_LinkedSyntaxId == hlID(g:ShowTrailingWhitespace#HighlightGroup)
" The highlight group is not linked; i.e. the user set up their own
" custom highlighting. They are responsible that this is visible.
else
" Especially the default linked highlight group may (depending on the
" colorscheme) not have a visible background color. In that case, we
" should take the foreground color as the background color instead, so
" that the trailing whitespace (that, being whitespace, has no visible
" foreground color, unless we've :set list) actually shows up.
call ShowTrailingWhitespace#Color#EnsureVisibleBackgroundColor()
autocmd ShowTrailingWhitespace ColorScheme * call ShowTrailingWhitespace#Color#EnsureVisibleBackgroundColor()
endif
endif
" vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax :