Search code examples
emacselisp

Settings only for GUI/Terminal emacs


I'm trying to set a theme - one only for terminal, and one only for gui. I've read this thread: Run certain Emacs init commands only in GUI mode

Which led me here: https://superuser.com/questions/165335/how-can-i-show-the-emacs-menu-in-gui-emacs-frames-but-not-in-tty-frames-when-usi

And tried to create a function to suit my need.

(defun set-frame-theme (frame)
  (let ((want-theme (memq (framep frame) '(x w32 ns))))
    (set-frame-parameter frame '(load-theme '(if want-theme monokai solarized-dark) t))))
(add-hook 'after-make-frame-functions 'set-frame-theme)

It doesn't work. I'm trying him to load monokai only if gui, otherwise load solarized-dark. It does work for the GUI interface, but causes the terminal to seemingly crash.

Suggestions?


Solution

  • 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