notes from /dev/null

by Charles Choi 최민수


Automatically adjusting Emacs to macOS switches in appearance

04 Jun 2023  Charles Choi

Count me among those who likes using the Auto setting when adjusting the appearance in macOS to either light or dark. Native macOS apps that are coded accordingly can automatically adjust as well, and it turns out that the Yamamoto Mitsuharu fork of Emacs (aka emacs-mac-app) can do this too. But with emacs-mac-app, just because it can detect the appearance change doesn’t mean that all the themes or faces will adjust accordingly too. As such it is typically up to the user to write a pair of custom light and dark mode functions to invoke when the appearance change happens.

For quite some time I’ve been calling these appearance mode functions manually like an animal. Then I recently learned that mac-effective-appearance-change-hook is a thing.

This hook gets invoked upon a macOS appearance change, so it becomes straightforward to write a hook function that tests for the :appearance property returned by
(mac-application-state).

1
2
3
4
5
6
7
(defun cc/reconfigure-nsappearance ()
  (let ((appearance (plist-get (mac-application-state) :appearance)))
    (if (string-equal appearance "NSAppearanceNameDarkAqua")
        (cc/dark-mode)
      (cc/light-mode))))

(add-hook 'mac-effective-appearance-change-hook 'cc/reconfigure-nsappearance)

A caveat though: the above code will only work with the Yamamoto fork of Emacs for macOS. As of now there seems to be no consensus on how to handle OS-level appearance changes by Emacs core. That said, if the above works for you, then no more manual appearance change calls!

References

emacs   dev   elisp   software   macos

 

AboutMastodonInstagramGitHub

Feeds & TagsGet Captee for macOS

Powered by Pelican