您当前的位置:首页 > 计算机 > 软件应用 > 开发工具(IDE)

判断 Emacs 是否在图形环境中的正确方法

时间:12-14来源:作者:点击数:

有些时候我们会需要判断 Emacs 是运行在图形环境下还是纯文本环境下的。我之前用的方法都是通过调用 (display-graphic-p) 来判断的,然而最近发现这种方案在处理 emacs --daemon 时是有问题的。

我们可以做个实验。在图形环境中启动 emacs --daemon, 这时尚未有 frame 存在,我们在 terminator 中执行

emacsclient --eval '(display-graphic-p)'

会发现结果为 nil,然后我们运行 emacsclient -c -n 启动一个 emacsclient frame,再次执行

emacsclient --eval '(display-graphic-p)'

会发现结果为 t,其实这种情况也好理解,用 C-h f display-graphic-p 查看该函数的说明如下:

display-graphic-p is a compiled Lisp function in ‘frame.el’.

(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).

也就是说,DISPLAY 为 nil 时会返回当前 frame 的显示属性,然而刚启动 emacs --daemon 时,会产生一个 frame,且这个 frame 是处于纯文本环境中的

$ emacsclient --eval '(frame-list)'
# (#<frame F1 0xc4bed0>)
$ emacsclient --eval '(display-graphic-p (car (frame-list)))'
# nil

而比较奇怪的是,我试着执行

(display-graphic-p ":0")

发现会提示错误 Display :0 does not exist。不过,其实当我们跳出Emacs来考虑这个问题,问题就变得简单了。启动X时,一般都会设置环境变量 DISPLAY,而纯文本环境下这个环境变量的值一般为空,所以我们可以通过判断 DISPLAY 的值来确定当前环境是否为图形环境。

(defun graphic-p ()
  "判断当前环境是否为图形环境"
  (getenv "DISPLAY"))
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐