【发布时间】:2011-08-13 07:40:08
【问题描述】:
在我的.emacs 文件中,我的命令仅在图形模式下才有意义(例如(set-frame-size (selected-frame) 166 100))。如何仅在图形模式下而不是终端模式下运行这些(即emacs -nw)。
谢谢!
【问题讨论】:
标签: emacs configuration terminal
在我的.emacs 文件中,我的命令仅在图形模式下才有意义(例如(set-frame-size (selected-frame) 166 100))。如何仅在图形模式下而不是终端模式下运行这些(即emacs -nw)。
谢谢!
【问题讨论】:
标签: emacs configuration terminal
window-system 变量告诉 Lisp 程序 Emacs 在哪个窗口系统下运行。可能的值是
来自the doc。
编辑:似乎不推荐使用窗口系统以支持display-graphic-p(来源:emacs 23.3.1 上的 C-h f 窗口系统 RET)。
(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).
所以你想做的是:
(if (display-graphic-p)
(progn
;; if graphic
(your)
(code))
;; else (optional)
(your)
(code))
如果你没有 else 子句,你可以:
;; more readable :)
(when (display-graphic-p)
(your)
(code))
【讨论】:
提到window-system 和display-graphic-p 的答案没有错,但它们并不能说明完整的情况。
实际上,一个 Emacs 实例可以有多个框架,其中一些可能在终端上,而另一些可能在窗口系统上。也就是说,即使在单个 Emacs 实例中,您也可以获得不同的 window-system 值。
例如,您可以启动一个窗口系统 Emacs,然后在终端中通过emacsclient -t 连接到它;生成的终端框架将看到window-system 的值nil。同样,您可以在守护程序模式下启动 emacs,然后告诉它创建一个图形框架。
因此,请避免将依赖于 window-system 的代码放入 .emacs。相反,将类似 set-frame-size 示例的代码放在一个钩子函数中,该函数在创建框架后运行:
(add-hook 'after-make-frame-functions
(lambda ()
(if window-system
(set-frame-size (selected-frame) 166 100)))))
请注意,'after-make-frame-functions 挂钩不会针对初始帧运行,因此通常还需要将上述与帧相关的挂钩函数添加到 'after-init-hook。
【讨论】:
split-window-horizontally 将当前窗口(“框架”)中的当前活动窗格(“窗口”)拆分为两个窗格(“窗口”)。跨度>
'after-init-hook 中添加与框架相关的钩子函数。
window-system 是一个定义在 `C 源代码'。它的值为x
文档:窗口系统的名称 通过它选定的框架是 显示。该值是一个符号——对于 例如,“x”代表 X 窗口。价值 如果选定的帧在 a 上,则为 nil 纯文本终端。
基本上做一个:
(if window-system
(progn
(something)
(something-else)))
【讨论】:
如果它处于 Gui 模式,则以下情况为真。
(如果是窗口系统)
【讨论】:
我已经定义了一个额外的函数来包装窗口名称功能,因为我在任何地方都使用 Emacs,即在终端和图形模式下以及在 Linux 和 MacOS 中:
(defun window-system-name()
(cond ((eq system-type 'gnu/linux) (if (display-graphic-p) "x" "nox"))
((eq system-type 'darwin) (if (display-graphic-p) "mac" "nox"))
(t (error "Unsupported window-system") nil)))
它可以扩展到其他系统,如 Windows 或使用串行终端的旧系统。但我没有时间这样做;-)
【讨论】: