Vim如何猜测xterm上的背景颜色?

Bac*_*aco 3 vim xterm tmux

Vim能够正确猜测Xterm的背景色,以便将其内部选项bg设置为darkwhite根据终端的选项。Vim的是能够正确地做,只有TERM被设置为xterm{, -color-256color}或者linux,但没有其他类似tmuxscreen

Vim如何猜测呢?

我发现大多数人都强迫在文件中或文件中设置background选项。但我想一个办法猜测同样的方式Vim也不会做,独立于终端之中的,,。darklight.vimrcxtermtmuxscreen

osg*_*sgx 5

默认设置在Vim源代码(用C编程语言编写)中定义(硬编码)。自6.1.136版以来,有一个修复程序,在Vim的GUI变体中不使用带有“ linux” TERM的“深色”,它可以帮助我找到实际的代码:

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
Run Code Online (Sandbox Code Playgroud)

逻辑在此处,默认值为:https : //fossies.org/dox/vim-7.4/option_8c_source.html#l00566

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
Run Code Online (Sandbox Code Playgroud)

检测终端背景:https//fossies.org/dox/vim-7.4/option_8c_source.html#l03754

  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
Run Code Online (Sandbox Code Playgroud)

界面修复:

 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 }
Run Code Online (Sandbox Code Playgroud)

更新:对于VIM-1689年7月4日(于Debian / Ubuntu)我重修Vim软件包与调试信息(-O2 -g;以及RAN默认选项条和包装成DEB之前)TERM=xterm gdb --args vim-7.4.1689/src/vim-basic/vim -ewatch p_bg。的第一个更改p_bg是由term_bg_default()light,第二个更改是从... main:(may_req_termresponse()如果已定义FEAT_TERMRESPONSE)-> vpeekc_nomap-> vpeekc-> vgetorpeek-> check_termcode-> set_option_value("bg",..)-> set_string_option

https://github.com/vim/vim/blob/54c10ccf9274880e83093a99690e7bfa9a2d2fa8/src/term.c

第3302行- may_req_bg_color()颜色,从/ log消息main.c后立即调用starttermcap()“ start termcap”;我添加了请求的预处理定义:

 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 }
Run Code Online (Sandbox Code Playgroud)

第4286行-处理termcap请求的响应:

/*
 * 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]"},
Run Code Online (Sandbox Code Playgroud)

由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 */
Run Code Online (Sandbox Code Playgroud)

您的tmux /屏幕可能未实现“ [RBG]”请求;通过在xterm和tmux中运行进行检查并比较输出:

echo -e '\033]11;?\007'
Run Code Online (Sandbox Code Playgroud)

您可以定义COLORFGBG(并且neovim中有关于它的错误:https : //github.com/neovim/neovim/issues/2764)有gnome-terminal的错误报告,用于将其设置为 https://bugzilla.gnome.org /show_bug.cgi?id=733423

包括urxvt和konsole在内的各种终端都设置了环境变量“ COLORFGBG”,以允许应用程序检测前景色和背景色。诸如Vim之类的各种程序都使用此程序来确定是否使用最适合浅色或深色背景的配色方案。请考虑同时在gnome-terminal中设置此变量。

Egmont在错误https://bugzilla.gnome.org/show_bug.cgi?id=733423中设置COLORFGBG了一些选项,以设置为.bashrc

您可以在“ vimrc”(~/.vimrc)中进行更改,/usr/share/vim/vimrc也可以在操作系统上进行全局更改(通过重新编译自定义版本来更改或在vim源中进行更改),或者在Vim源中通过向Vim作者报告错误/向他们发送补丁程序来更改此更改。

“BG”的文件描述了逻辑的一部分,并给出了解决方案的vimrc:

通过以下方式将“背景”设置为默认值时:

  :set background&
Run Code Online (Sandbox Code Playgroud)

Vim会猜测该值。在GUI中,这应该可以正常工作,在其他情况下,Vim可能无法猜测正确的值。

启动GUI时,默认值为' background' "light"。如果未在中设置该值.gvimrc,并且Vim检测到背景实际上很暗,background则将'设置为 "dark"。....

通常,此选项将在.vimrc文件中设置。可能取决于终端名称。例:

      :if &term == "pcterm"
        :  set background=dark
        :endif
Run Code Online (Sandbox Code Playgroud)

设置此选项后,高光组的默认设置将更改。要使用其他设置,请:highlight在“背景”选项设置之后放置“ ”命令。