-
Notifications
You must be signed in to change notification settings - Fork 18
/
theme-changer.el
136 lines (104 loc) · 4.59 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
;;; theme-changer.el --- Sunrise/Sunset Theme Changer for Emacs
;; Copyright (C) 2011 Joshua B. Griffith
;; Author: Joshua B. Griffith <josh.griffith@gmail.com>
;; Keywords: color-theme
;; 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 'color-theme-solarized-light 'color-theme-solarized-dark)
;; 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 Emacs 24 "deftheme" color theme facility
;; instead of the color-theme package, specify this (and use the
;; correct names for the themes):
;; (setq theme-changer-mode "deftheme")
;; (change-theme 'solarized-light 'solarized-dark)
;;; Code:
(require 'cl)
(require 'solar)
(defvar theme-changer-mode "color-theme"
"Specify the theme change mode: \"color-theme\" or Emacs 24's
\"deftheme\".")
(defun hour-fraction-to-time (date hour-fraction)
(let*
((now (decode-time (current-time)))
(month (first date))
(day (second date))
(year (third date))
(zone (ninth now))
(frac-hour (cl-truncate hour-fraction))
(hour (first frac-hour))
(frac-minutes (cl-truncate (* (second frac-hour) 60)))
(minute (first frac-minutes))
(frac-seconds (cl-truncate (* (second frac-minutes) 60)))
(sec (first frac-seconds)))
(encode-time sec minute hour day month year zone)))
(defun sunrise-sunset-times (date)
(let*
((l (solar-sunrise-sunset date))
(sunrise-time (hour-fraction-to-time date (caar l)))
(sunset-time (hour-fraction-to-time date (caadr l))))
(list sunrise-time sunset-time)))
(defun daytime-p (sunrise-time sunset-time)
(let* ((now (current-time)))
(and (time-less-p sunrise-time now)
(time-less-p now sunset-time))))
(defun today () (calendar-current-date))
(defun tomorrow ()
(calendar-gregorian-from-absolute
(+ 1 (calendar-absolute-from-gregorian (today)))))
(defun +second (time)
(time-add time (seconds-to-time 1)))
(defun switch-theme (old new)
"Change the theme from OLD to NEW, using Emacs 24's built-in
theme facility (\"deftheme\") or color-theme."
(if (string= theme-changer-mode "deftheme")
(progn
(disable-theme old)
(load-theme new t))
(apply (symbol-function new) '())))
(defun change-theme (day-theme night-theme)
(let*
((now (current-time))
(today-times (sunrise-sunset-times (today)))
(tomorrow-times (sunrise-sunset-times (tomorrow)))
(sunrise-today (first today-times))
(sunset-today (second today-times))
(sunrise-tomorrow (first tomorrow-times)))
(if (daytime-p sunrise-today sunset-today)
(progn
(switch-theme night-theme day-theme)
(run-at-time (+second sunset-today) nil
'change-theme day-theme night-theme))
(switch-theme day-theme night-theme)
(if (time-less-p now sunrise-today)
(run-at-time (+second sunrise-today) nil
'change-theme day-theme night-theme)
(run-at-time (+second sunrise-tomorrow) nil
'change-theme day-theme night-theme)))))
(provide 'theme-changer)