How to Check If Emacs in Frame or in Terminal

how to check if emacs in frame or in terminal?

(defun my-frame-config (frame)
"Custom behaviours for new frames."
(with-selected-frame frame
(when (display-graphic-p)
(set-background-color "#101416")
(set-foreground-color "#f6f3e8"))))
;; run now
(my-frame-config (selected-frame))
;; and later
(add-hook 'after-make-frame-functions 'my-frame-config)

How to detect that emacs is in terminal-mode?

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

Emacs: check for no-window-system in .emacs

The variable window-system is nil if the selected frame is on a text-only terminal.

Why Emacs (as daemon) gives 1 more frame than is opened?

It's a "physically invisible" frame (even though frame-visible-p says otherwise) associated with initial terminal where the daemon was started. I suspect that a sole reason for its existence is that emacs is not ready to run with no frames at all, and it's hard enough to fix it.

For filtering it out I would use this test:

(string-equal "initial_terminal" (terminal-name <frame>)) 
;;; => t for the "pseudo-"frame created by emacs -daemon

There might be better tests, but as far as I know this one is reliable enough: terminal-name returns something like "/dev/tty" for tty frames and the X11 display name like ":0" for X11 frames (I can't recall what it returns on other platforms, like in a Windows console window, but I believe it can't be "initial_terminal" by accident).

How can I detect that emacs-server is running from a shell prompt?

You're making this too hard. From the the emacsclient(1) man page:

-a, --alternate-editor=EDITOR

if the Emacs server is not running, run the specified editor instead. This can also be specified via the `ALTERNATE_EDITOR' environment variable.

If the value of EDITOR is the empty string, then Emacs is started in daemon mode and emacsclient will try to connect to it.

Settings only for GUI/Terminal emacs

The emacs lisp function,
(display-graphic-p)
Will return true if emacs is running in a GUI.

In your .emacs, add the following to switch between your GUI and terminal themes

(if (display-graphic-p)
(load-GUI-theme)
(load-Terminal-theme))

For easier configuration, I have a simple function called is-in-terminal

(defun is-in-terminal()
(not (display-graphic-p)))

you could use this to write an easier to read function

(if (is-in-terminal)
(load-Terminal-theme)
(load-GUI-theme))

For a more complete approach to Terminal Only configuration I have a macro that works just like progn but only evaluates the body when Emacs is running without a GUI

(defmacro when-term (&rest body)
"Works just like `progn' but will only evaluate expressions in VAR when Emacs is running in a terminal else just nil."
`(when (is-in-terminal) ,@body))

Example Usage:

(when-term
(load-my-term-theme)
(set-some-keybindings)
(foo-bar))

This entire block will be totally ignored if running in a GUI, but will run if in Terminal.

All this code was taken from a file in my config, if interested you can check it out here:

https://github.com/jordonbiondo/Emacs/blob/master/Jorbi/jorbi-util.el

Function that checks if current frame is not an X window

You may look at the window-system function. It accepts a frame optional argument (defaults to current frame). Alternatively, display-graphic-p is more recent (as per the documentation) and allows checking a whole display containing several frames. In your example, you could just write:

(if (display-graphic-p) ...)


Related Topics



Leave a reply



Submit