【问题标题】:Conditional execution of elisp code based on value of environment variable基于环境变量值的elisp代码的条件执行
【发布时间】:2012-11-04 15:59:06
【问题描述】:

在我的.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 中读取环境变量的值。

【问题讨论】:

    标签: emacs terminal elisp


    【解决方案1】:

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

    我得到你使用函数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

    【讨论】:

    • 我没有说清楚 - 我总是在终端中运行 Emacs。只是我的终端模拟器设置了 TERM="xterm",而 Tmux(当从同一个终端运行时)设置了 TERM="screen"。
    • 在这种情况下,getenv 是要走的路。
    【解决方案2】:
    (when (string= (getenv "TERM") "screen")
        .... your code
    )
    

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 2017-11-14
      • 2016-01-17
      • 1970-01-01
      • 2016-05-23
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多