如何检测 emacs 是否处于终端模式?
在我的 .emacs
文件中,我的命令仅在图形模式下有意义(例如 (set-frame-size (selected-frame) 166 100)
)。如何仅在图形模式而不是终端模式下运行它们(即emacs -nw
)。
谢谢!
In my .emacs
file, I have commands that only makes sense in graphical mode (like (set-frame-size (selected-frame) 166 100)
). How do I run these only in graphical mode and not in terminal mode (i.e. emacs -nw
).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
window-system
变量告诉 Lisp 程序 Emacs 运行在哪个窗口系统下。可能的值是来自文档。
编辑:似乎window-system已被弃用,取而代之的是
display-graphic-p
(来源:Ch f window-system RET on emacs 23.3.1)。所以你想做的是:
如果你没有 else 子句,你可以:
The
window-system
variable tells Lisp programs what window system Emacs is running under. The possible values areFrom the doc.
Edit: it seems that window-system is deprecated in favor of
display-graphic-p
(source: C-h f window-system RET on emacs 23.3.1).So what you want to do is :
And if you don't have an else clause, you can just:
提到
window-system
和display-graphic-p
的答案并没有错,但它们并没有说明完整的情况。实际上,单个 Emacs 实例可以有多个框架,其中一些可能位于终端上,另一些可能位于窗口系统上。也就是说,即使在单个 Emacs 实例中,您也可以获得不同的
window-system
值。例如,您可以启动窗口系统 Emacs,然后在终端中通过
emacsclient -t
连接到它;生成的终端框架将看到window-system
的nil
值。同样,您可以在守护程序模式下启动 emacs,然后告诉它创建一个图形框架。因此,请避免将依赖于
window-system
的代码放入 .emacs 中。相反,请将类似于set-frame-size
示例的代码放入创建框架后运行的钩子函数中:请注意,
'after-make-frame-functions
钩子不会在初始帧运行,因此通常还需要向'after-init-hook
添加与上面类似的与帧相关的钩子函数。The answers mentioning
window-system
anddisplay-graphic-p
aren't wrong, but they don't tell the complete picture.In reality, a single Emacs instance can have multiple frames, some of which might be on a terminal, and others of which might be on a window system. That is to say, you can get different values of
window-system
even within a single Emacs instance.For example, you can start a window-system Emacs and then connect to it via
emacsclient -t
in a terminal; the resulting terminal frame will see a value ofnil
forwindow-system
. Similarly, you can start emacs in daemon mode, then later tell it to create a graphical frame.As a result of this, avoid putting code in your .emacs that depends on
window-system
. Instead, put code like yourset-frame-size
example in a hook function which runs after a frame is created:Note that the
'after-make-frame-functions
hook isn't run for the initial frame, so it's often necessary to also add frame-related hook functions like that above to'after-init-hook
.基本上做一个:
Basically do a:
如果处于 Gui 模式,则以下情况为真。
(如果是窗口系统)
If its in Gui mode, then the following would be true.
(if window-system )
我定义了一个额外的函数来包装窗口名称功能,因为我在任何地方都使用 Emacs,即从终端、图形模式以及 Linux 和 MacOS 中:
它可以扩展到覆盖其他系统,例如 Windows 或旧系统,其中使用串行终端。但我没有时间这样做;-)
I have defined an extra function to wrap the window-name functionality because I'm using Emacs everywhere, i.e. from the terminal and in graphics mode and in Linux and MacOS:
It can be extended to cover other systems like Windows or older systems where a serial terminal is used. But I Have no time to do so ;-)