Emacs and Dark Mode

This handles Dark Mode (on mac). If you're running GUI Emacs, this code will automatically swap between themes based on Dark Mode status. In terminal, upon the launch of Emacs, it will choose the appropriate theme. If you change Dark Mode status if will not automatically update, but you can M-x reapply-theme at any point to update the theme based upon current Dark Mode. Importantly, this requires Emacs Plus for the functionality to detect the current Dark Mode status. If you'd prefer not to use Emacs Plus, you can make GUI work like the terminal version easily.
(use-package solarized-theme
  :ensure t)

(setq my-light-theme 'solarized-light)
(setq my-dark-theme 'solarized-dark)

;;;; Do not change anything below here, just update the two variables above.

;; If not on a Mac, just load a theme
(when (not (eq system-type 'darwin))
    (load-theme my-dark-theme t))

;; On a Mac
(when (eq system-type 'darwin)
  ;; Check if in terminal or GUI
  (if window-system
      ;; GUI Emacs
      (progn
        (defun my/apply-theme (appearance)
          "Load theme, taking current system APPEARANCE into consideration."
          (mapc #'disable-theme custom-enabled-themes)
          (pcase appearance
            ('light (load-theme my-light-theme t))
            ('dark  (load-theme my-dark-theme  t))))
        (add-hook 'ns-system-appearance-change-functions #'my/apply-theme))
    ;; Terminal Emacs
    (progn
      (defun reapply-theme ()
        "Check status of Dark Mode and apply appropriate pre-defined theme."
        (interactive)

        (let ((cmd-output (shell-command-to-string "defaults read -g AppleInterfaceStyle")))
          (if (string-match-p "Dark" cmd-output)
              (load-theme my-dark-theme t)
            (load-theme my-light-theme t))))
      (reapply-theme))))
Home | Back to blog

This work is licensed under CC BY-NC 4.0 Creative Commons BY-NC image