-
Notifications
You must be signed in to change notification settings - Fork 18
/
theme-changer.el
228 lines (174 loc) · 8.09 KB
/
theme-changer.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
;;; theme-changer.el --- Sunrise/Sunset Theme Changer for Emacs
;; Copyright (C) 2011-2013 Joshua B. Griffith
;; Copyright (C) 2023 Samuel W. Flint
;; Author: Joshua B. Griffith <[email protected]>
;; Maintainer: Samuel W. Flint <[email protected]>
;; Contributors: Corwin Brust, Göktuğ Kayaalp, Joe Snikeris,
;; Jonas Bernoulli, Julian Squires, Mike Fisher,
;; Sunn Yao
;; URL: https://github.com/hadronzoo/theme-changer
;; Created: 20 Jun 2011
;; Version: 2.2.0
;; Keywords: color-theme, deftheme, solar, sunrise, sunset
;; URL: https://github.com/hadronzoo/theme-changer
;; Package-Requires: (cl-lib)
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the
;; "Software"), to deal in the Software without restriction, including
;; without limitation the rights to use, copy, modify, merge, publish,
;; distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to
;; the following conditions:
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;; Given a location and day/night color themes, this file provides a
;; `change-theme` function that selects the appropriate theme based on
;; whether it is day or night. It will continue to change themes at
;; sunrise and sunset. To install:
;; Set the location:
;; (setq calendar-location-name "Dallas, TX")
;; (setq calendar-latitude 32.85)
;; (setq calendar-longitude -96.85)
;; Specify the day and night themes:
;; (require 'theme-changer)
;; (change-theme 'solarized-light 'solarized-dark)
;; If you specify a list of themes, a random one will be chosen at
;; each change:
;; (change-theme '(solarized-light sanityinc-tomorrow-day)
;; '(solarized-dark sanityinc-tomorrow-night))
;; You can also pass nil as either of parameters to change-theme, with the
;; effect of not using a theme (or using the default Emacs theme) during that
;; period of the day. For example:
;; (change-theme nil 'solarized-dark)
;; will result in setting the default Emacs theme during the day, and
;; solarized-dark during the night.
;; You may need to add this file path to your loadpath. For example:
;; (add-to-list 'load-path "~/.emacs.d/elisp/theme-changer")
;; If you want to use the color-theme package instead of the Emacs 24 color
;; theme facility:
;; (setq theme-changer-mode "color-theme")
;; (change-theme 'color-theme-solarized-light 'color-theme-solarized-dark)
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'solar)
;;; Customization
(defgroup theme-changer nil
"Customization for theme-changer.
theme-changer is a mechanism to automatically change themes based
on the current time and location."
:group 'convenience)
(defcustom theme-changer-mode "deftheme"
"Specify the theme change mode: `color-theme' or Emacs 24's `deftheme'."
:group 'theme-changer
:type 'string)
(defcustom theme-changer-delay-seconds 0
"Specify the delay seconds when switch themes at sunrise and sunset."
:type 'integer
:group 'theme-changer)
(defcustom theme-changer-pre-change-hook nil
"Functions to run before changing themes.
Functions should take one argument: name of theme being disabled."
:type 'hook
:group 'theme-changer)
(define-obsolete-variable-alias 'theme-changer-pre-change-function 'theme-change-pre-change-hook "theme-changer 2.2.0")
(defcustom theme-changer-post-change-hook nil
"Functions to run after changing themes.
Functions should take one argument: the name of the theme enabled."
:type 'hook
:group 'theme-changer)
(define-obsolete-variable-alias 'theme-changer-post-change-functions 'theme-change-post-change-hook "theme-changer 2.2.0")
;;; Utilities
(defun theme-changer-hour-fraction-to-time (date hour-fraction)
"Add HOUR-FRACTION to DATE."
(let*
((now (decode-time (current-time)))
(month (cl-first date))
(day (cl-second date))
(year (cl-third date))
(zone (cl-ninth now))
(frac-hour (cl-truncate hour-fraction))
(hour (cl-first frac-hour))
(frac-minutes (cl-truncate (* (cl-second frac-hour) 60)))
(minute (cl-first frac-minutes))
(frac-seconds (cl-truncate (* (cl-second frac-minutes) 60)))
(sec (cl-first frac-seconds)))
(encode-time sec minute hour day month year zone)))
(defun theme-changer-sunrise-sunset-times (date)
"Determine the sunrise and sunset times for DATE."
(let*
((l (solar-sunrise-sunset date))
(sunrise-time (time-add (theme-changer-hour-fraction-to-time date (caar l))
(seconds-to-time theme-changer-delay-seconds)))
(sunset-time (time-add (theme-changer-hour-fraction-to-time date (caadr l))
(seconds-to-time theme-changer-delay-seconds))))
(when (> emacs-major-version 26)
(setq sunrise-time (encode-time (decode-time sunrise-time)))
(setq sunset-time (encode-time (decode-time sunset-time))))
(list sunrise-time sunset-time)))
(defun theme-changer-today ()
"Determine current day."
(calendar-current-date))
(defun theme-changer-tomorrow ()
"Determine tomorrow."
(calendar-gregorian-from-absolute
(+ 1 (calendar-absolute-from-gregorian (theme-changer-today)))))
(defun theme-changer-add-second (time)
"Add a second to TIME."
(let ((newtime (time-add time (seconds-to-time 1))))
(if (> emacs-major-version 26)
(encode-time (decode-time newtime))
newtime)))
;;; Theme Switcher
(defun theme-changer-switch-theme (old new)
"Change the theme from OLD to NEW.
Uses Emacs 24's built-in theme facility (`deftheme') or
`color-theme', depending on `theme-changer-mode'.
NEW may be a list of themes, in which case a random theme is
chosen from that list.
If NEW is set to nil, shall switch to default Emacs theme.
Returns the theme that was enabled."
(let ((new (if (listp new)
(if (zerop (length new))
nil
(elt new (random (length new))))
new))
(enable (if (not (string= theme-changer-mode "deftheme"))
(lambda () (apply (symbol-function new) '()))
(lambda () (load-theme new t)))))
(run-hook-with-args 'theme-changer-pre-change-hook old)
(disable-theme old)
(if new (funcall enable))
(run-hook-with-args 'theme-changer-post-change-hook new)
new))
;;; Change Theme, Main entry point
(defun change-theme (day-theme night-theme &optional old-theme)
"Setup the DAY-THEME and NIGHT-THEME for time sensitive theme swapping.
Either or both may be a symbol or a list of symbols referencing
themes. OLD-THEME specifies the theme prior to setting up
switching, if any."
(let* ((now (current-time))
(sunrise-tomorrow (cl-first (theme-changer-sunrise-sunset-times
(theme-changer-tomorrow)))))
(cl-destructuring-bind (sunrise-today sunset-today)
(theme-changer-sunrise-sunset-times (theme-changer-today))
(cl-destructuring-bind (next-change . theme)
(cond ((time-less-p now sunrise-today)
(cons sunrise-today night-theme))
((time-less-p now sunset-today)
(cons sunset-today day-theme))
(t (cons sunrise-tomorrow night-theme)))
(let ((old-theme (theme-changer-switch-theme old-theme theme)))
(run-at-time (theme-changer-add-second next-change) nil
'change-theme day-theme night-theme old-theme))))))
(provide 'theme-changer)
;;; theme-changer.el ends here