summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lisp/acdw.el34
1 files changed, 27 insertions, 7 deletions
diff --git a/lisp/acdw.el b/lisp/acdw.el index 47df30d..a1d3a0b 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el
@@ -54,6 +54,18 @@ ARGS."
54 (when msg 54 (when msg
55 (message "%s" msg))))) 55 (message "%s" msg)))))
56 56
57(defun +sunrise-sunset--encode (time)
58 "Encode diary-style time string into a time.
59This is stolen from `run-at-time'."
60 (let ((hhmm (diary-entry-time time))
61 (now (decode-time)))
62 (encode-time (list 0 (% hhmm 100) (/ hhmm 100)
63 (decoded-time-day now)
64 (decoded-time-month now)
65 (decoded-time-year now)
66 nil -1
67 (decoded-time-zone now)))))
68
57(defun +sunrise-sunset (sunrise-command sunset-command &optional reset) 69(defun +sunrise-sunset (sunrise-command sunset-command &optional reset)
58 "Run SUNRISE-COMMAND at sunrise, and SUNSET-COMMAND at sunset. 70 "Run SUNRISE-COMMAND at sunrise, and SUNSET-COMMAND at sunset.
59With RESET, this function will call itself with its own 71With RESET, this function will call itself with its own
@@ -72,14 +84,22 @@ itself."
72 (* nonl))) 84 (* nonl)))
73 (ss (+suppress-messages #'sunrise-sunset)) 85 (ss (+suppress-messages #'sunrise-sunset))
74 (_m (string-match times-regex ss)) 86 (_m (string-match times-regex ss))
75 (sunrise-time (match-string 1 ss)) 87 (sunrise (match-string 1 ss))
76 (sunset-time (match-string 2 ss))) 88 (sunset (match-string 2 ss))
77 (run-at-time sunrise-time nil sunrise-command) 89 (sunrise-time (+sunrise-sunset--encode sunrise))
78 (run-at-time sunset-time nil sunset-command) 90 (sunset-time (+sunrise-sunset--encode sunset)))
79 (run-at-time "12:00am" nil sunset-command) 91 (cond
92 ((time-less-p nil sunrise-time)
93 ;; If it isn't sunrise yet, it's still dark---and so we need to run the
94 ;; sunset-command.
95 (funcall sunset-command)
96 (run-at-time sunrise nil sunrise-command))
97 ((time-less-p nil sunset-time) (run-at-time sunset nil sunset-command))
98 (t (run-at-time "12:00am" nil sunset-command)))
80 ;; Reset everything at midnight 99 ;; Reset everything at midnight
81 (run-at-time "12:00am" (* 60 60 24) 100 (unless reset
82 #'sunrise-sunset sunrise-command sunset-command t))) 101 (run-at-time "12:00am" (* 60 60 24)
102 #'+sunrise-sunset sunrise-command sunset-command t))))
83 103
84(defun +ensure-after-init (function) 104(defun +ensure-after-init (function)
85 "Ensure FUNCTION runs after init, or now if already initialized. 105 "Ensure FUNCTION runs after init, or now if already initialized.