Automatic darkmode for Emacs

Emacs doesn’t have a native way of detecting dark mode (yet?). But since it’s emacs, you can build it yourself, here’s how.

macOS

On macOS you can use the following code to check the system appearance every two seconds and on change use a dark theme.

This is loosely based on Automatic Emacs theme switching in OSX dark mode , with the added bonnus that this is fully automatic:

(setq last-dark-mode-state 'unknown)

(defun check-and-set-dark-mode ()
  "Automatically set the theme to match if macOS is in dark mode."
  (let ((dark-mode-enabled (system-dark-mode-enabled-p)))
    (if (not (eq dark-mode-enabled last-dark-mode-state))
	(progn
	  (setq last-dark-mode-state dark-mode-enabled)
	  (if dark-mode-enabled
	    (load-theme 'doom-one       t)
	    (load-theme 'doom-one-light t))))))

(defun system-dark-mode-enabled-p ()
  "Check if dark mode is currently enabled on macOS."
  (if (string= system-type "darwin")
      (string=
       (shell-command-to-string "printf %s \"$( osascript -e \'tell application \"System Events\" to tell appearance preferences to return dark mode\' )\"")
       "true")
      nil))

(run-with-timer 0 2 'check-and-set-dark-mode)
Edit this page on GitHub