summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2024-03-16 16:41:04 +0100
committerMartin Polden <mpolden@mpolden.no>2024-03-16 16:41:04 +0100
commit5a3847ab2e9935dbaeedfeb7b492b46f2a8d1bad (patch)
treecd36b577a2779ea2528da5bded7143d5b20305f6
parentcadd858a609c795c91171afea7f30c3491cad170 (diff)
theme: change theme according to system appearance
This only works when using emacs-plus, which supports the ns-system-appearance-change-functions hook.
-rw-r--r--lisp/init-theme.el21
1 files changed, 15 insertions, 6 deletions
diff --git a/lisp/init-theme.el b/lisp/init-theme.el
index 9d6954a..41d002f 100644
--- a/lisp/init-theme.el
+++ b/lisp/init-theme.el
@@ -27,20 +27,27 @@
(t (error "Failed to detect current theme. Enabled themes: %s"
custom-enabled-themes))))
-(defun mpolden/toggle-theme ()
+(defun mpolden/toggle-theme (&optional appearance)
"Toggle between dark and light themes.
The variables `mpolden/theme-light' and `mpolden/theme-dark'
decides the themes to toggle between.
A prefix argument prompts for a theme to load instead of toggling
-the current theme."
+the current theme.
+
+If APPEARANCE is either 'light or 'dark, change to the matching
+theme instead of toggling."
(interactive)
(if current-prefix-arg
(mpolden/switch-theme)
- (let* ((is-light (eq (mpolden/current-theme) 'light))
- (new-theme (if is-light mpolden/theme-dark mpolden/theme-light)))
- (mpolden/switch-theme new-theme))))
+ (if appearance
+ (pcase appearance
+ ('light (mpolden/switch-theme mpolden/theme-light))
+ ('dark (mpolden/switch-theme mpolden/theme-dark)))
+ (let* ((is-light (eq (mpolden/current-theme) 'light))
+ (new-theme (if is-light mpolden/theme-dark mpolden/theme-light)))
+ (mpolden/switch-theme new-theme)))))
(use-package doom-themes
:ensure t
@@ -48,7 +55,9 @@ the current theme."
:if (display-graphic-p)
:bind ("C-c t" . mpolden/toggle-theme)
:config
- (load-theme mpolden/theme-dark t))
+ (if (boundp 'ns-system-appearance-change-functions)
+ (add-hook 'ns-system-appearance-change-functions #'mpolden/toggle-theme)
+ (load-theme mpolden/theme-dark t)))
(provide 'init-theme)