【问题标题】:True Color (24 bit) in Terminal Emacs终端 Emacs 中的真彩色(24 位)
【发布时间】:2013-01-18 08:01:10
【问题描述】:

我正在寻找一种在使用图形终端仿真器时在 emacs 中具有 24 位颜色的好方法。 例如,Konsole 确实支持真彩色的转义码,如下所述:https://github.com/robertknight/konsole/blob/master/user-doc/README.moreColors

我的问题是我不明白 emacs 如何将人脸信息转换为终端的转义序列。我也没有管理某处是否存在对 24 位颜色的支持,或者是否有可能用 emacs lisp 实现它。我要的是指向相关 Emacs 文档的指针,或者是关于在终端 Emacs 中具有真实颜色是否可行的知情意见。

【问题讨论】:

    标签: emacs terminal konsole


    【解决方案1】:

    这最近包含在 emacs 26.1(2018 年 5 月 28 日)中,

    使用此文件:terminfo-24bit.src

    # Use colon separators.
    xterm-24bit|xterm with 24-bit direct color mode,
      use=xterm-256color,
      setb24=\E[48:2:%p1%{65536}%/%d:%p1%{256}%/%{255}%&%d:%p1%{255}%&%dm,
      setf24=\E[38:2:%p1%{65536}%/%d:%p1%{256}%/%{255}%&%d:%p1%{255}%&%dm,
    # Use semicolon separators.
    xterm-24bits|xterm with 24-bit direct color mode,
      use=xterm-256color,
      setb24=\E[48;2;%p1%{65536}%/%d;%p1%{256}%/%{255}%&%d;%p1%{255}%&%dm,
      setf24=\E[38;2;%p1%{65536}%/%d;%p1%{256}%/%{255}%&%d;%p1%{255}%&%dm,
    

    运行:

    tic -x -o ~/.terminfo terminfo-24bit.src
    

    现在您可以使用真彩色启动 emacs。

    TERM=xterm-24bit emacs -nw
    

    查看常见问题解答:https://www.gnu.org/software/emacs/manual/html_node/efaq/Colors-on-a-TTY.html

    【讨论】:

    • 当我使用上述方法时,Emacs 恢复为无颜色。这是emacs 26.1。我可能做错了什么?我
    • 这可能取决于您的终端,这对我来说适用于 gnome-terminal、urxvt 和 st。
    【解决方案2】:

    AFAIK 对此没有内置支持,因为终端中的 24 位色彩空间非常罕见(!?)。但是,鉴于 Emacs 对 add your own terminal support 开放,您可以尝试编写类似于 xterm-frobs.el 的包。

    顺便说一句,如果您只需要在终端中使用好的颜色主题,您可以尝试我的包https://github.com/tungd/color-theme-approximate,它将 GUI 颜色主题转换为终端。

    【讨论】:

    • 确实,据我所知,只有 Konsole 和(基于 Konsole 的)Yakuake 支持 24 位颜色。然而,这对我来说不是什么大问题,因为它在我工作的大多数机器上都可用。
    • 我最初的问题是,将 emacs 颜色减少为终端颜色(16 或 256 色)会使某些面孔非常难以阅读。而且因为我想使用浅色和深色主题,手动调整有问题的颜色会变得很棘手。你的包裹看起来很有前途,我会用我的设置检查它。
    【解决方案3】:

    在 emacs 中启用 24 位颜色有 3 种方法:
    (代码来自emacs的term.c

    (注意:测试颜色是否有效的一种简单方法是检查M-x list-colors-display

    正确的方法是将TERM设置为支持直接颜色的值(通过RGB terminfo 功能),如果您的终端可用(或者只是尝试xterm-direct)。
    这些通常命名为“(terminalName)-direct”(xterm-directvte-direct 等)
    例如:TERM=xterm-direct emacs,或通过配置终端正确设置 TERM。

    编辑: xterm-direct(及相关)的实现存在缺陷。某些蓝色阴影被视为索引颜色(因为它对 rgb 和索引颜色使用相同的序列),并且不会正确渲染。我推荐使用第二种方法。

    /* Standard support for 24-bit colors.  */
    else if (tigetflag ("RGB") > 0)
      {
        /* ...  */
        tty->TN_max_colors = 16777216;
      }
    

    另一种选择是使用具有非标准 terminfo 功能 setf24setb24 的 terminfo 文件(请参阅答案 https://stackoverflow.com/a/50577683/6232794)。与其他两个相比,旧版本的 emacs 可能支持此方法

    const char *fg = tigetstr ("setf24");
    const char *bg = tigetstr ("setb24");
    /* Non-standard support for 24-bit colors. */
    if (fg && bg
        && fg != (char *) (intptr_t) -1
        && bg != (char *) (intptr_t) -1)
      {
        tty->TS_set_foreground = fg;
        tty->TS_set_background = bg;
        tty->TN_max_colors = 16777216;
      }
    

    作为最后的手段,您可以将环境变量 COLORTERM 设置为“truecolor”,这可能适用于您的终端。

    /* Fall back to xterm+direct (semicolon version) if requested
       by the COLORTERM environment variable.  */
    else if ((bg = getenv("COLORTERM")) != NULL
             && strcasecmp(bg, "truecolor") == 0)
      {
        tty->TS_set_foreground = "\033[%?%p1%{8}%<%t3%p1%d%e38;2;%p1%{65536}%/%d;%p1%{256}%/%{255}%&%d;%p1%{255}%&%d%;m";
        tty->TS_set_background = "\033[%?%p1%{8}%<%t4%p1%d%e48;2;%p1%{65536}%/%d;%p1%{256}%/%{255}%&%d;%p1%{255}%&%d%;m";
        tty->TN_max_colors = 16777216;
      }                                                                                                                  
    

    【讨论】:

      【解决方案4】:

      这是可行的,但不能单独在 ELisp 中完成。

      这里有一个可爱的补丁列表,适用于各种版本的 emacs 和 tmux,让 trucolor 生活成为可能:

      https://gist.github.com/choppsv1

      【讨论】:

      • 为什么需要 tmux 才能在 Emacs 中实现这一点?
      【解决方案5】:

      我使用xterm-frobs.el 来获得 256 色术语支持(在与 xterm 兼容的终端中,如 konsole)。我使用术语设置“xterm-256color”。 256 色支持通常对我来说绰绰有余,因为我的配色方案中没有使用太多颜色。上述文件试图询问终端以找出它支持多少种颜色。我不知道它是否应该(或可以适应)能够在 konsole 中提供真彩色支持。

      更新: 请注意,从 26.1 版开始,emacs 现在支持真彩色终端。详情请查看answer below

      【讨论】:

        猜你喜欢
        • 2011-09-18
        • 2015-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多