基于环境变量值的条件执行elisp代码

时间:2012-11-04 15:59:06

标签: emacs terminal elisp

在我的.emacs配置文件中,我有以下条目:

(custom-set-variables
  (custom-set-faces
    '(font-lock-comment-face ((((class color)
                                (min-colors 8)
                                (background dark))
                                (:foreground "red"))))))

这会修复TERM环境变量设置为screen时的字体颜色,但会在TERM设置为xterm时将其中断。有没有办法读取TERM变量的值并仅在TERM的值为screen时才执行该代码?我发现this questin 略有帮助,但我仍然不知道如何在elisp中读取环境变量的值。

2 个答案:

答案 0 :(得分:7)

首先我会回答你的问题,下面我会回答你真正应该问的问题;)

我得到了一个使用函数getenv的环境变量的值。例如:

(getenv "TERM")   ->  "xterm-color"

然而,这是检查您的Emacs是否在终端中运行的相对笨拙的方法。相反,您可以使用以下内容:

(display-graphic-p &optional DISPLAY)

Return non-nil if DISPLAY is a graphic display.
Graphical displays are those which are capable of displaying several
frames and several different fonts at once.  This is true for displays
that use a window system such as X, and false for text-only terminals.
DISPLAY can be a display name, a frame, or nil (meaning the selected
frame's display).

较旧的,已弃用的版本是检查变量window-system

答案 1 :(得分:5)

(when (string= (getenv "TERM") "screen")
    .... your code
)