-
Notifications
You must be signed in to change notification settings - Fork 35
/
auto-dark.el
385 lines (333 loc) · 14.7 KB
/
auto-dark.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
;;; auto-dark.el --- Automatically sets the dark-mode theme based on macOS/Linux/Windows status -*- lexical-binding: t; -*-
;; Author: Rahul M. Juliato
;; Tim Harper <timcharper at gmail dot com>
;; Vincent Zhang <[email protected]>
;; Jonathan Arnett <[email protected]>
;; Greg Pfeil <[email protected]>
;; Created: July 16 2019
;; Version: 0.13.2
;; Keywords: macos, windows, linux, themes, tools, faces
;; URL: https://github.com/LionyxML/auto-dark-emacs
;; Package-Requires: ((emacs "24.4"))
;; SPDX-License-Identifier: GPL-2.0-or-later
;;; Commentary:
;; Auto-Dark is an auto-changer between 2 themes, dark/light, respecting the
;; overall settings of MacOS, Linux and Windows.
;; To enable it, install the package and add it to your load path:
;;
;; (require 'auto-dark)
;; (auto-dark-mode t)
;;
;; To customize the themes used by light/dark mode:
;;
;; M-x customize-group auto-dark
;;
;; If you're using Doom Emacs or Spacemacs, follow the installation tips
;; on https://github.com/LionyxML/auto-dark-emacs.
;;
;;; Code:
;; Optional require of dbus squelches elisp compilation warnings
(require 'dbus nil t)
(defgroup auto-dark nil
"Automatically changes Emacs theme acording to MacOS/Windows dark-mode status."
:group 'tools
:prefix "auto-dark-*")
;; Dark & light themes need to be set together because enabling and disabling
;; themes modifies `custom-enabled-themes', so if only one mode were expected to
;; default to `custom-enabled-themes', it would use the wrong list of themes
;; after the first time the other mode enables its themes.
(defcustom auto-dark-themes nil
"The themes to enable for dark and light modes.
The default is to use the themes in `custom-enabled-themes', but that only works
if the themes are aware of `frame-background-mode', which many aren’t.
If your themes aren’t aware of `frame-background-mode' (or you just prefer
different themes for dark and light modes), you can set explicit lists of themes
for each mode. Like with `custom-enabled-themes', the earlier themes in the list
have higher precedence."
:group 'auto-dark
:type '(choice
(const :tag "Use custom-enabled-themes" nil)
(list :tag "Use distinct dark & light lists"
(repeat :tag "Dark" symbol)
(repeat :tag "Light" symbol))))
(defcustom auto-dark-dark-theme 'wombat
"The theme to enable when dark-mode is active.
This variable is obsolete. You should set `auto-dark-themes' instead."
:group 'auto-dark
:type '(choice symbol (const nil)))
(defcustom auto-dark-light-theme 'leuven
"The theme to enable when dark-mode is inactive.
This variable is obsolete. You should set `auto-dark-themes' instead."
:group 'auto-dark
:type '(choice symbol (const nil)))
(make-obsolete-variable 'auto-dark-dark-theme 'auto-dark-themes "0.13")
(make-obsolete-variable 'auto-dark-light-theme 'auto-dark-themes "0.13")
(defun auto-dark--patch-theme-list (themes deprecated-theme)
"Support the deprecated ‘auto-dark-*-theme’ variables.
If THEMES is non-nil, it’s simply returned. If ‘custom-enabled-themes’ isn’t
set, a list containing only DEPRECATED-THEME is returned, otherwise nil.
Once the deprecated variables are removed “(auto-dark--patch-theme-list a b)”
can be replaced by “a”."
(or themes
(unless (and custom-enabled-themes
(not (equal custom-enabled-themes
(list auto-dark-dark-theme)))
(not (equal custom-enabled-themes
(list auto-dark-light-theme))))
(when deprecated-theme (list deprecated-theme)))))
(defun auto-dark--dark-themes ()
"Return the set of themes to be used in dark mode."
(auto-dark--patch-theme-list (car auto-dark-themes) auto-dark-dark-theme))
(defun auto-dark--light-themes ()
"Return the set of themes to be used in light mode."
(auto-dark--patch-theme-list (cadr auto-dark-themes) auto-dark-light-theme))
(defcustom auto-dark-polling-interval-seconds 5
"The number of seconds between which to poll for dark mode state.
Emacs must be restarted for this value to take effect."
:group 'auto-dark
:type 'integer)
(defcustom auto-dark-allow-osascript nil
"Whether to allow function `auto-dark-mode' to shell out to osascript:
to check dark-mode state, if `ns-do-applescript' or `mac-do-applescript'
is not available."
:group 'auto-dark
:type 'boolean)
(defcustom auto-dark-allow-powershell nil
"Whether to allow function `auto-dark-mode' to shell out to powershell:
to check dark-mode state."
:group 'auto-dark
:type 'boolean)
(defcustom auto-dark-detection-method nil
"The method auto-dark should use to detect the system theme.
Defaults to nil and will be populated through feature detection
if left as such. Only change this value if you know what you're
doing!"
:group 'auto-dark
:type 'symbol
:options '(applescript osascript dbus powershell winreg termux))
(defvar auto-dark--last-dark-mode-state 'unknown)
(defvar auto-dark--dbus-listener-object nil)
(defun auto-dark--is-dark-mode-applescript ()
"Invoke AppleScript using Emacs built-in AppleScript support.
In order to check if dark mode is enabled. Return true if it is."
(if (fboundp 'ns-do-applescript)
(auto-dark--is-dark-mode-ns)
(if (fboundp 'mac-do-applescript)
(auto-dark--is-dark-mode-mac)
(error "No AppleScript support available in this Emacs build. Try setting `auto-dark-allow-osascript` to t"))))
(defun auto-dark--is-dark-mode-ns ()
"Check if dark mode is enabled using ns-do-applescript."
(string-equal "true" (ns-do-applescript "tell application \"System Events\"
tell appearance preferences
if (dark mode) then
return \"true\"
else
return \"false\"
end if
end tell
end tell")))
(defun auto-dark--is-dark-mode-mac ()
"Check if dark mode is enabled using `mac-do-applescript'."
(string-equal "\"true\"" (mac-do-applescript "tell application \"System Events\"
tell appearance preferences
if (dark mode) then
return \"true\"
else
return \"false\"
end if
end tell
end tell")))
(defun auto-dark--is-dark-mode-osascript ()
"Invoke applescript using Emacs using external shell command;
this is less efficient, but works for non-GUI Emacs."
(string-equal "true" (string-trim (shell-command-to-string "osascript -e 'tell application \"System Events\" to tell appearance preferences to return dark mode'"))))
(defun auto-dark--is-dark-mode-dbus ()
"Use Emacs built-in D-Bus function to determine if dark theme is enabled."
(eq 1 (caar (dbus-ignore-errors
(dbus-call-method
:session
"org.freedesktop.portal.Desktop"
"/org/freedesktop/portal/desktop"
"org.freedesktop.portal.Settings" "Read"
"org.freedesktop.appearance" "color-scheme")))))
(defun auto-dark--is-dark-mode-powershell ()
"Invoke powershell using Emacs using external shell command."
(string-equal "0" (string-trim (shell-command-to-string "powershell -noprofile -noninteractive \
-nologo -ex bypass -command Get-ItemPropertyValue \
HKCU:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize \
-Name AppsUseLightTheme"))))
(defun auto-dark--is-dark-mode-winreg ()
"Use Emacs built-in Windows Registry function.
In order to determine if dark theme is enabled."
(eq 0 (w32-read-registry 'HKCU
"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"
"AppsUseLightTheme")))
(defun auto-dark--is-dark-mode-termux ()
"Use Termux way to determine if dark theme is enabled. ref: https://github.com/termux/termux-api/issues/425."
(string-equal "Night mode: yes"
(shell-command-to-string "echo -n $(cmd uimode night 2>&1 </dev/null)")))
(defvar auto-dark-dark-mode-hook nil
"List of hooks to run after dark mode is loaded." )
(defvar auto-dark-light-mode-hook nil
"List of hooks to run after light mode is loaded." )
(defun auto-dark--is-dark-mode ()
"If dark mode is enabled."
(pcase auto-dark-detection-method
('applescript
(auto-dark--is-dark-mode-applescript))
('osascript
(auto-dark--is-dark-mode-osascript))
('dbus
(auto-dark--is-dark-mode-dbus))
('powershell
(auto-dark--is-dark-mode-powershell))
('winreg
(auto-dark--is-dark-mode-winreg))
('termux
(auto-dark--is-dark-mode-termux))))
(defun auto-dark--check-and-set-dark-mode ()
"Set the theme according to the OS's dark mode state.
In order to prevent flickering, we only set the theme if we haven't
already set the theme for the current dark mode state."
(let ((appearance (if (auto-dark--is-dark-mode) 'dark 'light)))
(unless (eq appearance auto-dark--last-dark-mode-state)
(auto-dark--set-theme appearance))))
(defun auto-dark--update-frame-backgrounds (appearance)
"Set the `frame-background-mode' for all frames to APPEARANCE."
(setq frame-background-mode appearance)
(mapc #'frame-set-background-mode (frame-list)))
(defun auto-dark--enable-themes (&optional themes)
"Re-enable THEMES, which defaults to ‘custom-enabled-themes’.
This will load themes if necessary."
(interactive)
(let ((full-themes (remq 'user
(delete-dups (or themes custom-enabled-themes)))))
;; Disable only the themes we’re not going to re-enable.
(mapc (lambda (theme)
(unless (memq theme full-themes)
(disable-theme theme)))
custom-enabled-themes)
(let ((failures (mapcan (lambda (theme)
(condition-case nil
;; Enable instead of load when possible.
(if (custom-theme-p theme)
(enable-theme theme)
(load-theme theme))
(:success nil)
(error (list theme))))
(reverse full-themes))))
(when failures
(warn "Failed to enable theme(s): %s"
(mapconcat #'symbol-name failures ", "))))))
(defun auto-dark--set-theme (appearance)
"Set light/dark theme Argument APPEARANCE should be light or dark."
(setq auto-dark--last-dark-mode-state appearance)
(auto-dark--update-frame-backgrounds appearance)
(pcase appearance
('dark
(auto-dark--enable-themes (auto-dark--dark-themes))
(run-hooks 'auto-dark-dark-mode-hook))
('light
(auto-dark--enable-themes (auto-dark--light-themes))
(run-hooks 'auto-dark-light-mode-hook))))
(defvar auto-dark--timer nil)
(defun auto-dark-start-timer ()
"Start auto-dark timer."
(setq auto-dark--timer
(run-with-timer 0 auto-dark-polling-interval-seconds #'auto-dark--check-and-set-dark-mode)))
(defun auto-dark-stop-timer ()
"Stop auto-dark timer."
(when (timerp auto-dark--timer)
(cancel-timer auto-dark--timer)))
(defun auto-dark--register-dbus-listener ()
"Register a callback function with D-Bus.
Ask D-Bus to send us a signal on theme change and add a callback
to change the theme."
(setq auto-dark--dbus-listener-object
(dbus-register-signal
:session
"org.freedesktop.portal.Desktop"
"/org/freedesktop/portal/desktop"
"org.freedesktop.portal.Settings"
"SettingChanged"
(lambda (path var val)
(when (and (string= path "org.freedesktop.appearance")
(string= var "color-scheme"))
(auto-dark--set-theme (pcase (car val) (0 'light) (1 'dark) (2 'light))))))))
(defun auto-dark--unregister-dbus-listener ()
"Unregister our callback function with D-Bus.
Remove theme change callback registered with D-Bus."
(dbus-unregister-object auto-dark--dbus-listener-object))
(defun auto-dark--register-change-listener ()
"Register a listener to listen for the system theme to change."
(cond
((auto-dark--use-ns-system-appearance)
(add-hook 'ns-system-appearance-change-functions #'auto-dark--set-theme))
((auto-dark--use-mac-system-appearance)
(add-hook 'mac-effective-appearance-change-hook #'auto-dark--check-and-set-dark-mode))
((auto-dark--use-dbus)
(auto-dark--register-dbus-listener))
(t (auto-dark-start-timer))))
(defun auto-dark--unregister-change-listener ()
"Remove an existing listener for the system theme."
(cond
((auto-dark--use-ns-system-appearance)
(remove-hook 'ns-system-appearance-change-functions #'auto-dark--set-theme))
((auto-dark--use-mac-system-appearance)
(remove-hook 'mac-effective-appearance-change-hook #'auto-dark--check-and-set-dark-mode))
((auto-dark--use-dbus)
(auto-dark--unregister-dbus-listener))
(t (auto-dark-stop-timer))))
(defun auto-dark--use-ns-system-appearance ()
"Determine whether we should use the ns-system-appearance-* functions."
(boundp 'ns-system-appearance-change-functions))
(defun auto-dark--use-mac-system-appearance ()
"Determine whether we should use the `mac-effective-appearance-change-hook'."
(boundp 'mac-effective-appearance-change-hook))
(defun auto-dark--use-dbus ()
"Determine whether we should use the dbus-* functions."
(eq auto-dark-detection-method 'dbus))
(defun auto-dark--determine-detection-method ()
"Determine which theme detection method auto-dark should use."
(cond
((and (eq system-type 'darwin)
(or (fboundp 'ns-do-applescript)
(fboundp 'mac-do-applescript))
(or (eq window-system 'ns)
(eq window-system 'mac)))
'applescript)
((and (eq system-type 'darwin)
auto-dark-allow-osascript)
'osascript)
((and (eq system-type 'gnu/linux)
(member 'dbus features)
(member "org.freedesktop.portal.Desktop"
(dbus-list-activatable-names :session)))
'dbus)
((and (eq system-type 'gnu/linux)
(member 'dbus features)
(cl-search "termux-fix-shebang"
(shell-command-to-string "command -v termux-fix-shebang")))
'termux)
((and (eq system-type 'windows-nt)
auto-dark-allow-powershell)
'powershell)
((eq system-type 'windows-nt)
'winreg)
(t
(error "Could not determine a viable theme detection mechanism!"))))
;;;###autoload
(define-minor-mode auto-dark-mode
"Toggle `auto-dark-mode' on or off."
:group 'auto-dark
:global t
:lighter " AD"
(if auto-dark-mode
(progn
(unless auto-dark-detection-method
(setq auto-dark-detection-method (auto-dark--determine-detection-method)))
(auto-dark--check-and-set-dark-mode)
(auto-dark--register-change-listener))
(auto-dark--unregister-change-listener)))
(provide 'auto-dark)
;;; auto-dark.el ends here