patch 9.1.1703: Cannot react to terminal OSC responses

Problem:  Cannot react to terminal OSC responses
Solution: Allow TermResponseAll to be triggered by Terminal OSC
          responses (Foxe Chen)

fixes: #14995
closes: #17975

Signed-off-by: Foxe Chen <chen.foxe@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Foxe Chen
2025-08-27 21:15:47 +02:00
committed by Christian Brabandt
parent 7f380259cf
commit 1f51bbc3b9
30 changed files with 404 additions and 303 deletions

View File

@ -3,6 +3,7 @@ The plugin directory is for standard Vim plugin scripts.
All files here ending in .vim will be sourced by Vim when it starts up.
Look in the file for hints on how it can be disabled without deleting it.
colorresp.vim used to detect terminal background and foreground colours
getscriptPlugin.vim get latest version of Vim scripts
gzip.vim edit compressed files
logiPat.vim logical operators on patterns
@ -15,7 +16,3 @@ tarPlugin.vim edit (compressed) tar files
tohtml.vim convert a file with syntax highlighting to HTML
vimballPlugin.vim create and unpack .vba files
zipPlugin.vim edit zip archives
Note: the explorer.vim plugin is no longer here, the netrw.vim plugin has
taken over browsing directories (also for remote directories).

View File

@ -0,0 +1,51 @@
vim9script
# Vim plugin for setting the background and foreground colours depending on
# the terminal response.
#
# Maintainer: The Vim Project <https://github.com/vim/vim>
# Last Change: 2025 August 13
if exists("loaded_colorresp")
finish
endif
g:loaded_colorresp = 1
augroup ColorResp
au!
au TermResponseAll osc {
var parts: list<string> = matchlist(v:termosc, '\(\d\+\);rgb:\(\w\+\)/\(\w\+\)/\(\w\+\)')
if len(parts) >= 5
var type: string = parts[1]
var rval: number = str2nr(parts[2][: 1], 16)
var gval: number = str2nr(parts[3][: 1], 16)
var bval: number = str2nr(parts[4][: 1], 16)
if type == '11'
# Detect light or dark background by parsing OSC 11 RGB background reply
# from terminal. Sum the RGB values roughly; if bright enough, set
# 'background' to 'light', otherwise set it to 'dark'.
var new_bg_val: string = (3 * char2nr('6') < char2nr(parts[2]) + char2nr(parts[3]) + char2nr(parts[4])) ? "light" : "dark"
v:termrbgresp = v:termosc
&background = new_bg_val
# For backwards compatibility
if exists('#TermResponseAll#background')
doautocmd <nomodeline> TermResponseAll background
endif
else
v:termrfgresp = v:termosc
# For backwards compatibility
if exists('#TermResponseAll#foreground')
doautocmd <nomodeline> TermResponseAll foreground
endif
endif
endif
}
au VimEnter * ++once {
call echoraw(&t_RB)
call echoraw(&t_RF)
}
augroup END
# vim: set sw=2 sts=2 :