From 3962ebb9bc68348992d442a414e75c46dd8fde0a Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 5 Jan 2022 11:25:37 -0600 Subject: Improve +sunrise-sunset TODO: At some point, I should break this into a standalone package. --- lisp/acdw.el | 34 +++++++++++++++++++++++++++------- 1 file 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." (when msg (message "%s" msg))))) +(defun +sunrise-sunset--encode (time) + "Encode diary-style time string into a time. +This is stolen from `run-at-time'." + (let ((hhmm (diary-entry-time time)) + (now (decode-time))) + (encode-time (list 0 (% hhmm 100) (/ hhmm 100) + (decoded-time-day now) + (decoded-time-month now) + (decoded-time-year now) + nil -1 + (decoded-time-zone now))))) + (defun +sunrise-sunset (sunrise-command sunset-command &optional reset) "Run SUNRISE-COMMAND at sunrise, and SUNSET-COMMAND at sunset. With RESET, this function will call itself with its own @@ -72,14 +84,22 @@ itself." (* nonl))) (ss (+suppress-messages #'sunrise-sunset)) (_m (string-match times-regex ss)) - (sunrise-time (match-string 1 ss)) - (sunset-time (match-string 2 ss))) - (run-at-time sunrise-time nil sunrise-command) - (run-at-time sunset-time nil sunset-command) - (run-at-time "12:00am" nil sunset-command) + (sunrise (match-string 1 ss)) + (sunset (match-string 2 ss)) + (sunrise-time (+sunrise-sunset--encode sunrise)) + (sunset-time (+sunrise-sunset--encode sunset))) + (cond + ((time-less-p nil sunrise-time) + ;; If it isn't sunrise yet, it's still dark---and so we need to run the + ;; sunset-command. + (funcall sunset-command) + (run-at-time sunrise nil sunrise-command)) + ((time-less-p nil sunset-time) (run-at-time sunset nil sunset-command)) + (t (run-at-time "12:00am" nil sunset-command))) ;; Reset everything at midnight - (run-at-time "12:00am" (* 60 60 24) - #'sunrise-sunset sunrise-command sunset-command t))) + (unless reset + (run-at-time "12:00am" (* 60 60 24) + #'+sunrise-sunset sunrise-command sunset-command t)))) (defun +ensure-after-init (function) "Ensure FUNCTION runs after init, or now if already initialized. -- cgit 1.4.1-21-gabe81