Vim能够正确猜测Xterm的背景色,以便根据终端的dark或white将其内部选项设置为white。当被设置为xterm{、-color、-256color}或linux,但没有其他像tmux或screen这样的设置时,Vim就能够正确地执行TERM,只有当TERM被设置为xterm{、-color、-256color}或linux时,TERM才能正确地执行。
怎么猜到呢?
我发现大多数人都会强制在dark或light文件中设置dark或light选项;但是,我希望有一种方式来猜测Vim的做法,而不是xterm、tmux、screen终端。
发布于 2016-07-10 02:16:57
默认设置是在Vim源代码(用C语言编写)中定义的(硬编码)。自从6.1.136版本以来,在Vim的GUI变体中使用"linux“术语有一个修正,它可以帮助我找到实际的代码:
http://ftp.twaren.net/vim/patches/6.1.136
Patch 6.1.136
Problem: When $TERM is "linux" the default for 'background' is "dark", even
though the GUI uses a light background. (Hugh Allen)
Solution: Don't mark the option as set when defaulting to "dark" for the
linux console. Also reset 'background' to "light" when the GUI
has a light background.
Files: src/option.c逻辑在这里,默认值:source.html#l00566
563 {"background", "bg", P_STRING|P_VI_DEF|P_RCLR,
564 (char_u *)&p_bg, PV_NONE,
565 {
566 #if (defined(MSDOS) || defined(OS2) || defined(WIN3264)) && !defined(FEAT_GUI)
567 (char_u *)"dark",
568 #else
569 (char_u *)"light",
570 #endif检测终端背景:source.html#l03754
3725 /* For DOS console the default is always black. */
3726 #if !((defined(MSDOS) || defined(OS2) || defined(WIN3264)) && !defined(FEAT_GUI))
3727 /*
3728 * If 'background' wasn't set by the user, try guessing the value,
3729 * depending on the terminal name. Only need to check for terminals
3730 * with a dark background, that can handle color.
3731 */
3732 idx = findoption((char_u *)"bg");
3733 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
3734 && *term_bg_default() == 'd')
3735 {
3736 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
3737 /* don't mark it as set, when starting the GUI it may be
3738 * changed again */
3739 options[idx].flags &= ~P_WAS_SET;
3740 }
3741 #endif
3754 /*
3755 * Return "dark" or "light" depending on the kind of terminal.
3756 * This is just guessing! Recognized are:
3757 * "linux" Linux console
3758 * "screen.linux" Linux console with screen
3759 * "cygwin" Cygwin shell
3760 * "putty" Putty program
3761 * We also check the COLORFGBG environment variable, which is set by
3762 * rxvt and derivatives. This variable contains either two or three
3763 * values separated by semicolons; we want the last value in either
3764 * case. If this value is 0-6 or 8, our background is dark.
3765 */
3766 static char_u *
3767 term_bg_default()
3768 {
3769 #if defined(MSDOS) || defined(OS2) || defined(WIN3264)
3770 /* DOS console nearly always black */
3771 return (char_u *)"dark";
3772 #else
3773 char_u *p;
3774
3775 if (STRCMP(T_NAME, "linux") == 0
3776 || STRCMP(T_NAME, "screen.linux") == 0
3777 || STRCMP(T_NAME, "cygwin") == 0
3778 || STRCMP(T_NAME, "putty") == 0
3779 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3780 && (p = vim_strrchr(p, ';')) != NULL
3781 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3782 && p[2] == NUL))
3783 return (char_u *)"dark";
3784 return (char_u *)"light";
3785 #endif
3786 }GUI fix:
4044 gui_bg_default()
4045 {
4046 if (gui_get_lightness(gui.back_pixel) < 127)
4047 return (char_u *)"dark";
4048 return (char_u *)"light";
4049 }更新:对于vim-7.4.1689 (debian/ubuntu),我使用调试信息(-O2 -g默认选项;在剥离和打包到deb之前)重新构建Vim包,并使用watch p_bg运行TERM=xterm gdb --args vim-7.4.1689/src/vim-basic/vim -e。p_bg的第一个变化是term_bg_default()到light,其次是从. main:may_req_termresponse() (如果定义FEAT_TERMRESPONSE ) -> vpeekc_nomap -> vpeekc vgetorpeek -> set_option_value("bg",..) set_string_option。
https://github.com/vim/vim/blob/54c10ccf9274880e83093a99690e7bfa9a2d2fa8/src/term.c
第3302行- may_req_bg_color()颜色,在main.c / starttermcap() / log消息“启动术语帽”之后调用;我添加了请求的预处理定义:
/*
* Similar to requesting the version string: Request the terminal background
* color when it is the right moment.
*/
void
may_req_bg_color(void)
...
{(int)KS_RBG, "\033]11;?\007",
{(int)KS_RBG, "[RBG]"},第4286行-处理来自termcap的响应:
check_termcode( ....
/* Check for background color response from the terminal:
*
* {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
*
* {lead} can be <Esc>] or OSC
* {tail} can be '\007', <Esc>\ or STERM.
*
* Consume any code that starts with "{lead}11;", it's also
* possible that "rgba" is following.
*/
if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
&& tp[j + 11] == '/' && tp[j + 16] == '/'
&& !option_was_set((char_u *)"bg"))
{/* TODO: don't set option when already the right value */
LOG_TR("Received RBG");
rbg_status = RBG_GOT;
set_option_value((char_u *)"bg", 0L, (char_u *)(
(3 * '6' < tp[j+7] + tp[j+12] + tp[j+17])
? "light" : "dark"), 0);
reset_option_was_set((char_u *)"bg");由7.4.757补丁http://ftp.vim.org/vim/patches/7.4/7.4.757添加
Patch 7.4.757
Problem: Cannot detect the background color of a terminal.
Solution: Add T_RBG to request the background color if possible. (Lubomir
Rintel)
Files: src/main.c, src/term.c, src/term.h, src/proto/term.pro
#define T_RBG (term_str(KS_RBG)) /* request background RGB */您的tmux/screen可能无法实现“RBG”请求;请在xterm和tmux中运行检查并比较输出:
echo -e '\033]11;?\007'您可以定义COLORFGBG (在neovim:https://github.com/neovim/neovim/issues/2764中也有关于它的bug ),gnome终端有错误报告来设置bug.cgi?id=733423。
各种终端,包括urxvt和konsole,设置了一个环境变量"COLORFGBG“,以允许应用程序检测前景和背景色。各种程序,如Vim,使用这个来确定是否使用在浅色或暗背景上最有效的配色方案。请考虑在gnome终端中设置此变量。
在bug bug.cgi?id=733423中,埃格蒙特有一些选项可以用.bashrc设置COLORFGBG。
您可以在您的"vimrc“(~/.vimrc)中,或者在您的操作系统上通过更改/usr/share/vim/vimrc或通过重新编译自定义版本在vim源中,或者通过向Vim作者报告一个bug /向他们发送补丁来改变这一点。
"bg“文件描述了部分逻辑,并给出了vimrc的解决方案:
将“后台”设置为默认值时,使用: *设定背景和 维姆会猜到价值。在GUI中,这应该是正确的,在其他情况下,Vim可能无法猜测正确的值。 启动GUI时,“
background”的默认值将是"light"。当没有在.gvimrc中设置值,并且Vim检测到背景实际上很暗时,'background‘被设置为"dark"。…… 通常,此选项将在.vimrc文件中设置。可能取决于终端的名称。示例: ::if &term == "pcterm“:set background=dark :endif 设置此选项后,突出显示组的默认设置将更改。若要使用其他设置,请在“背景”选项设置后放置":highlight“命令。
https://stackoverflow.com/questions/38287889
复制相似问题