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.
This commit is contained in:
Ingo Karkat
2020-12-20 11:08:07 +01:00
parent da39ee9c51
commit a12144ae80
2 changed files with 23 additions and 16 deletions

View File

@@ -46,27 +46,23 @@ function! s:HasVisibleBackground( syntaxId ) abort
return 1
endfunction
let s:didAutomaticBackground = 0
function! ShowTrailingWhitespace#Color#EnsureVisibleBackgroundColor() abort
let l:linkedSyntaxId = synIDtrans(hlID(g:ShowTrailingWhitespace#HighlightGroup))
if l: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.
return
endif
" 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.
if s:HasVisibleBackground(l:linkedSyntaxId)
if s:HasVisibleBackground(g:ShowTrailingWhitespace_LinkedSyntaxId)
if s:didAutomaticBackground
" Restore the original link that had been automatically adapted for
" a previous colorscheme that did not define a background color.
execute printf('highlight link %s %s', g:ShowTrailingWhitespace#HighlightGroup, synIDattr(g:ShowTrailingWhitespace_LinkedSyntaxId, 'name'))
let s:didAutomaticBackground = 0
endif
return
endif
for l:mode in s:GetColorModes()
let l:color = s:GetForegroundColor(l:linkedSyntaxId, l:mode)
let l:color = s:GetForegroundColor(g:ShowTrailingWhitespace_LinkedSyntaxId, l:mode)
if ! empty(l:color)
execute printf('highlight %s %sbg=%s', g:ShowTrailingWhitespace#HighlightGroup, l:mode, l:color)
let s:didAutomaticBackground = 1
endif
endfor
endfunction

View File

@@ -47,8 +47,19 @@ augroup END
execute printf('highlight def link %s Error', g:ShowTrailingWhitespace#HighlightGroup)
if g:ShowTrailingWhitespace_IsAutomaticBackground
call ShowTrailingWhitespace#Color#EnsureVisibleBackgroundColor()
autocmd ShowTrailingWhitespace ColorScheme * call ShowTrailingWhitespace#Color#EnsureVisibleBackgroundColor()
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 :