判断 Emacs 是否在图形环境中的正确方法
有些时候我们会需要判断 Emacs 是运行在图形环境下还是纯文本环境下的。我之前用的方法都是通过调用 (display-graphic-p)
来判断的,然而最近发现这种方案在处理 emacs --daemon
时是有问题的。
我们可以做个实验:在图形环境中启动 emacs --daemon
, 这时尚未有 frame 存在,我们在 terminator 中执行
#+BEGIN_SRC sh
emacsclient --eval '(display-graphic-p)'
#+END_SRC
会发现结果为 nil
然后我们运行 emacsclient -c -n
启动一个 emacsclient frame,再次执行
#+BEGIN_SRC sh
emacsclient --eval '(display-graphic-p)'
#+END_SRC
会发现结果为 t
其实这种情况也好理解,用 C-h f display-graphic-p
查看该函数的说明如下:
#+BEGIN_QUOTE
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).
#+END_QUOTE
也就是说,DISPLAY 为 nil 时会返回当前 frame 的显示属性,然而刚启动 emacs --daemon
时,会产生一个 frame,且这个 frame 是处于纯文本环境中的
#+BEGIN_SRC sh
$ emacsclient --eval '(frame-list)'
# (#<frame F1 0xc4bed0>)
$ emacsclient --eval '(display-graphic-p (car (frame-list)))'
# nil
#+END_SRC
而比较奇怪的是,我试着执行
#+BEGIN_SRC emacs-lisp
(display-graphic-p ":0")
#+END_SRC
发现会提示错误 Display :0.0 does not exist
。不过,其实当我们跳出 Emacs 来考虑这个问题,问题就变得简单了。启动 X 时,一般都会设置环境变量 DISPLAY
,而纯文本环境下这个环境变量的值一般为空,所以我们可以通过判断 DISPLAY
的值来确定当前环境是否为图形环境。
#+BEGIN_SRC emacs-lisp
(defun graphic-p ()
"判断当前环境是否为图形环境"
(getenv "DISPLAY"))
#+END_SRC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论