如何检测 emacs 是否处于终端模式?

发布于 2024-11-03 11:24:36 字数 167 浏览 1 评论 0原文

在我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡笑忘祈一世凡恋 2024-11-10 11:24:36

window-system 变量告诉 Lisp 程序 Emacs 运行在哪个窗口系统下。可能的值是

x
Emacs 正在使用 X 显示框架。Emacs
w32
正在使用本机 MS-Windows GUI 显示框架。
ns
Emacs 使用 Nextstep 界面(在 GNUstep 和 Mac OS X 上使用)显示框架。
pc
Emacs 使用 MS-DOS 直接屏幕写入来显示框架。
nil
Emacs 正在基于字符的终端上显示该框架。

来自文档

编辑:似乎window-system已被弃用,取而代之的是display-graphic-p(来源:Ch f window-system RET on emacs 23.3.1)。

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

The window-system variable tells Lisp programs what window system Emacs is running under. The possible values are

x
Emacs is displaying the frame using X.
w32
Emacs is displaying the frame using native MS-Windows GUI.
ns
Emacs is displaying the frame using the Nextstep interface (used on GNUstep and Mac OS X).
pc
Emacs is displaying the frame using MS-DOS direct screen writes.
nil
Emacs is displaying the frame on a character-based terminal.

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

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

So what you want to do is :

(if (display-graphic-p)
    (progn
    ;; if graphic
      (your)
      (code))
    ;; else (optional)
    (your)
    (code))

And if you don't have an else clause, you can just:

;; more readable :)
(when (display-graphic-p)
    (your)
    (code))
離殇 2024-11-10 11:24:36

提到 window-systemdisplay-graphic-p 的答案并没有错,但它们并没有说明完整的情况。

实际上,单个 Emacs 实例可以有多个框架,其中一些可能位于终端上,另一些可能位于窗口系统上。也就是说,即使在单个 Emacs 实例中,您也可以获得不同的 window-system 值。

例如,您可以启动窗口系统 Emacs,然后在终端中通过 emacsclient -t 连接到它;生成的终端框架将看到 window-systemnil 值。同样,您可以在守护程序模式下启动 emacs,然后告诉它创建一个图形框架。

因此,请避免将依赖于 window-system 的代码放入 .emacs 中。相反,请将类似于 set-frame-size 示例的代码放入创建框架后运行的钩子函数中:

(add-hook 'after-make-frame-functions
  (lambda (frame)
    (with-selected-frame frame
      (if window-system
        (set-frame-size frame 166 100))))))

请注意,'after-make-frame-functions 钩子不会在初始帧运行,因此通常还需要向 'after-init-hook 添加与上面类似的与帧相关的钩子函数。

The answers mentioning window-system and display-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 of nil for window-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 your set-frame-size example in a hook function which runs after a frame is created:

(add-hook 'after-make-frame-functions
  (lambda (frame)
    (with-selected-frame frame
      (if window-system
        (set-frame-size frame 166 100))))))

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.

看春风乍起 2024-11-10 11:24:36

window-system 是一个定义在
“C 源代码”。其值为x

文档:窗口系统的名称
通过它所选择的帧是
显示。该值是一个符号——用于
例如,“x”代表 X 窗口。价值
如果所选帧位于
纯文本终端。

基本上做一个:

(if window-system
    (progn
      (something)
      (something-else)))

window-system is a variable defined in
`C source code'. Its value is x

Documentation: Name of window system
through which the selected frame is
displayed. The value is a symbol--for
instance, `x' for X windows. The value
is nil if the selected frame is on a
text-only-terminal.

Basically do a:

(if window-system
    (progn
      (something)
      (something-else)))
忘年祭陌 2024-11-10 11:24:36

如果处于 Gui 模式,则以下情况为真。

(如果是窗口系统)

If its in Gui mode, then the following would be true.

(if window-system )

迷路的信 2024-11-10 11:24:36

我定义了一个额外的函数来包装窗口名称功能,因为我在任何地方都使用 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 或旧系统,其中使用串行终端。但我没有时间这样做;-)

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:

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

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 ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文