branch: elpa/moe-theme
commit 21f83424b199d9e0c9524c4bfe4b8f945984ffb3
Author: Tobias J <[email protected]>
Commit: Tobias J <[email protected]>
Complete redone of the theme switcher.
The function that ran every 24hrs is gone, instead the calculation is done
every time.
Instead of doing weird and possibly fragile string manipulation, it now
uses the function solar-sunrise-sunset.
---
moe-theme-switcher.el | 127 +++++++++++++++++---------------------------------
1 file changed, 44 insertions(+), 83 deletions(-)
diff --git a/moe-theme-switcher.el b/moe-theme-switcher.el
index 309d4321a3..82c2700b2e 100644
--- a/moe-theme-switcher.el
+++ b/moe-theme-switcher.el
@@ -1,7 +1,7 @@
;; moe-theme-switcher.el
;; Author: kuanyui ([email protected])
;; Date: 2013/05/11 11:39
-;;
+;;
;; This file is not a part of GNU Emacs,
;; but this file is released under GPL v3.
@@ -11,7 +11,7 @@
(defvar moe-theme-switch-by-sunrise-and-sunset t
"Automatically switch between dark and light moe-theme.
-If this value is nil, moe-theme will switch at fixed time (06:00 and 18:00).
+If this value is nil, moe-theme will switch at fixed time (06:00 and 18:00).
If this value is t and both calendar-latitude and calendar-longitude are set
properly, the switching will be triggered at the sunrise and sunset time of the
local calendar.
@@ -27,97 +27,58 @@ Take Keelung, Taiwan(25N,121E) for example, you can set
like this:
(load-theme 'moe-light t) (load-theme 'moe-dark t))
nil))
-;; (Thanks for letoh!)
-;; Fix strange bahavior of sunrise-sunset when buffer's width is too narrow.
-(defun get-sunrise-sunset-string ()
- "get the real result from `sunrise-sunset'"
- (save-window-excursion
- (let ((regex "[0-9]+:[0-9]+[ap]m")
- (s (sunrise-sunset))
- (buf (get-buffer "*temp*")))
- (unless (and (stringp s)
- (string-match-p regex s))
- (when buf
- (with-current-buffer buf
- (let* ((s1 (buffer-string))
- (s2 (if (string-match-p regex s1)
- s1 nil)))
- (setq s s2)
- (kill-buffer buf)))))
- s)))
-
-;; Convert am/pm to 24hr and save to 24h/sunrise & 24h/set
-;; Excute every 24 hr
-(defun convert-time-format-of-sunrise-and-sunset ()
- (let (rise_set a b c d e f)
- (setq rise_set (get-sunrise-sunset-string))
- (if (string-match "0:00 hours daylight" rise_set) ;If polar-night
- (progn
- (setq 24h/sunrise 'polar-night
- 24h/sunset 'polar-night))
- (if (string-match "24:00 hours daylight" rise_set) ;If midnight-sun
- (progn
- (setq 24h/sunrise 'midnight-sun
- 24h/sunset 'midnight-sun))
- (progn ;Convert 12hr to 24hr
- (string-match
"\\([0-9][0-9]?\\):\\([0-9][0-9]\\)\\([ap]m\\).+\\([0-9][0-9]?\\):\\([0-9][0-9]\\)\\([ap]m\\)"
rise_set)
- (setq a (string-to-number (match-string 1 rise_set))
- b (string-to-number (match-string 2 rise_set))
- c (match-string 3 rise_set)
- d (string-to-number (match-string 4 rise_set))
- e (string-to-number (match-string 5 rise_set))
- f (match-string 6 rise_set))
- (if (equal c "pm")
- (setq 24h/sunrise (list (+ 12 a) b))
- (setq 24h/sunrise (list a b)))
- (if (equal f "pm")
- (setq 24h/sunset (list (+ 12 d) e))
- (setq 24h/sunset (list d e))))))))
+(defun float-to-time-list (time)
+ "Converts time represented as a float to a list
+Example:
+> (float-to-time-list 4.5)
+=> (4 30)
+> (float-to-time-list 8.633333332836628)
+=> (8 37)"
+ (let* ((hours (truncate time))
+ (minutes (truncate (* (- time hours) 60))))
+ (list hours minutes)))
;; Excute every minute.
(defun switch-by-locale ()
- (if (equal 24h/sunrise 'polar-night) ;If polar-night...moe-dark!
- (load-theme 'moe-dark t)
- (if (equal 24h/sunrise 'midnight-sun) ;If midnight-sun...moe-light!
- (load-theme 'moe-light t)
- (progn
- (let ((now (list (string-to-number (format-time-string "%H"))
- (string-to-number (format-time-string "%M")))))
- (if (and (or ;magic-logic [tm]
- (> (car now) (car 24h/sunrise))
- (and
- (= (car now) (car 24h/sunrise))
- (>= (second now) (second 24h/sunrise))))
- (or
- (< (car now) (car 24h/sunset))
- (and
- (= (car now) (car 24h/sunset))
- (< (second now) (second 24h/sunset)))))
- (load-theme 'moe-light t)
- (load-theme 'moe-dark t)
- ))))))
+ (let ((sunrise-sunset a b c)
+ (length-of-day a b)
+ (sunrise a)
+ (sunset a))
+ (setq sunrise-sunset (solar-sunrise-sunset (calendar-current-date)))
+ ; length of day should never be nil
+ (setq length-of-day (mapcar
+ (lambda (x) (string-to-number x))
+ (split-string (car (cddr sunrise-sunset ":")))))
+
+ (if (equal length-of-day '(0 0)) ; Polar night
+ (load-theme 'moe-dark t)
+ (if (equal length-of-day '(24 0)) ; Midnight sun
+ (load-theme 'moe-light t)
+ (let ((now (list (string-to-number (format-time-string "%H"))
+ (string-to-number (format-time-string "%M"))))
+ (sunrise (float-to-time-list (caar sunrise-sunset)))
+ (sunset (float-to-time-list (car (cadr sunrise-sunset)))))
+ (if (and
+ (or (> (car now) (car sunrise))
+ (and (= (car now) (car sunrise))
+ (>= (cdr now) (cdr sunrise))))
+ (or (< (car now) (car sunset))
+ (and (= (car now) (car sunset))
+ (< (cdr now) (cdr sunset)))))
+ (load-theme 'moe-light t)
+ (load-theme 'moe-dark t)))))))
(defun moe-theme-auto-switch ()
(interactive)
- (if (boundp '24h/sunrise)
+ (if (and moe-theme-switch-by-sunrise-and-sunset
+ (boundp 'calendar-longitude)
+ (boundp 'calendar-latitude))
(switch-by-locale)
- (switch-at-fixed-time))
- )
-
-(if (and
- (boundp 'calendar-longitude)
- (boundp 'calendar-latitude)
- (eql moe-theme-switch-by-sunrise-and-sunset t))
- (progn
- (convert-time-format-of-sunrise-and-sunset)
- (run-with-timer 0 (* 60 60 24)
'convert-time-format-of-sunrise-and-sunset))
- ()
- )
-
+ (switch-at-fixed-time)))
(moe-theme-auto-switch)
-(run-with-timer 0 (* 1 60) 'moe-theme-auto-switch)
+(run-with-timer 0 (* 1 60) 'moe-theme-auto-switch)
(provide 'moe-theme-switcher)