4

Is there a good way (e.g. examining the value of some variable or the existence of some function) for Elisp code to determine whether the current Emacs instance is running as an X11 application?

kjo
  • 3,145
  • 14
  • 42
  • possible duplicate of [Is there a way to detect that emacs is running in a terminal?](http://emacs.stackexchange.com/questions/7151/is-there-a-way-to-detect-that-emacs-is-running-in-a-terminal) – PythonNut Feb 12 '15 at 22:10
  • It's hard to tell if you want to know if Emacs is running graphically at all, or you mean it's specifically running under X11. – PythonNut Feb 12 '15 at 22:10
  • @PythonNut (responding to your first comment): not so: in OS X, for example, Emacs can run in graphics mode either under X11 or under the native (OS X) window system, without either being running in a terminal. – kjo Feb 12 '15 at 22:12
  • Alright then. I'll retract my vote. – PythonNut Feb 12 '15 at 22:12

2 Answers2

5

To quote the documentation:

window-system is a variable defined in `C source code'. Its value is x It is a terminal-local variable; global value is the same.

Documentation: Name of window system through which the selected frame is displayed. The value is a symbol: nil for a termcap frame (a character-only terminal), 'x' for an Emacs frame that is really an X window, 'w32' for an Emacs frame that is a window on MS-Windows display, 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display, 'pc' for a direct-write MS-DOS frame.

So you can test like this:

(if (eq window-system 'x)
   ;; do this in X11
   (blah blah blah)
   ;; otherwise, do this
   (blah blah blah)
   )
PythonNut
  • 10,243
  • 2
  • 29
  • 75
  • You should be using `display-graphic-p`. –  Feb 12 '15 at 23:09
  • 1
    @lunaryorn not necessarily, please read the comments under the OP. `display-graphic-p` is _not_ strong enough for this purpose. In this case, the user wants to distinguish X11 and Quartz, both of which support all of the features Emacs can detect with `display-*-p` predicates. – PythonNut Feb 12 '15 at 23:24
3

You can use the function display-graphics-p:

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

Note that use of the variable window-system is not recommended, from the elisp manual:

Do not use window-system and initial-window-system as predicates or boolean flag variables, if you want to write code that works differently on text terminals and graphic displays. That is because window-system is not a good indicator of Emacs capabilities on a given display type. Instead, use display-graphic-p or any of the other display-*-p predicates described in *note Display Feature Testing::.

Lindydancer
  • 6,095
  • 1
  • 13
  • 25
  • 2
    I'd say this is complementary, but sometimes, Emacs feature detection just doesn't support the feature the user is thinking of, in which case you'll have to fall back on more primitive methods. In this case, the user wants to distinguish X11 and Quartz, both of which support all of the features Emacs can detect with `display-*-p` predicates. – PythonNut Feb 12 '15 at 23:25