如何在 Emacs 中获取背景颜色?

Jef*_*eff 5 terminal emacs gnome-terminal

在 Emacs 无窗口模式下,背景自动与终端(gnome-terminal)相同。当我看时,(frame-parameters)我看到了(background-color . "unspecified-bg")。嗯,对我来说,背景恰好是黑色的。有没有办法找出 Emacs 会话的实际背景颜色?

Ema*_*erg 2

如果您知道如何从终端查找,则可以使用相同的命令从 Emacs 查找。

就我而言,我会制作这样的脚本:

#!/bin/zsh

cat .Xresources | grep 'URxvt\*background\:' | cut -d" " -f2
Run Code Online (Sandbox Code Playgroud)

(注:-d是设置字段分隔符,-f是设置显示什么字段:第一个字段是1,不是0

该命令之所以如此,是因为设置.Xresources背景颜色的文件如下所示:

# ...
URxvt*background: black
# ...
Run Code Online (Sandbox Code Playgroud)

使脚本可执行 ( chmod +x),并将其放入您的PATH( echo $PATH) 中。

what_bg如果该脚本在 Emacs 中被称为, M-x shell-command RET what_bg.

编辑(回应评论):

看看这是否有效。我在 Emacs、urxvt、xterm 和 rxvt 中测试了它。虽然它比第一个脚本更可移植,但它采用.Xresources配置(这虽然并不罕见,但显然并非无处不在)。

不过,我开始想知道为什么你需要这个?

而且,如果您确实需要它,难道您不能只看窗户来确定它的颜色吗?

无论如何,脚本:

#!/bin/zsh

terminal_emulator_parents=`pstree -As $$`
tep_list=`echo $terminal_emulator_parents | tr -s "-" | tr "-" " " \
          | tac -s' ' | tr '\n' ' '`

found="false"
for process in `echo $tep_list`; do
    if [[ $process =~ ("urxvt"|"xterm"|"rxvt") ]]; then # here: add all
        found="true"                                    # terminal emulators
        break                                           # configurable
    fi                                                  # (and *configured*)
done                                                    # in ~/.Xresources

if [[ $found == "true" ]]; then
    echo -n "$process: "
    cat ~/.Xresources | grep -ie ^$process'\*background\:' \
                      | tr -s " " | cut -d" " -f2
else
    echo "Couldn't determine the terminal emulator."
fi
Run Code Online (Sandbox Code Playgroud)