diff options
Diffstat (limited to 'lisp/acdw.el')
-rw-r--r-- | lisp/acdw.el | 52 |
1 files changed, 1 insertions, 51 deletions
diff --git a/lisp/acdw.el b/lisp/acdw.el index 34d1bc4..4e5afb5 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el | |||
@@ -20,6 +20,7 @@ | |||
20 | ;;; Code: | 20 | ;;; Code: |
21 | 21 | ||
22 | (require 'diary-lib) | 22 | (require 'diary-lib) |
23 | (require 'solar) ; for +sunrise-sunset | ||
23 | 24 | ||
24 | ;;; Define a directory and an expanding function | 25 | ;;; Define a directory and an expanding function |
25 | 26 | ||
@@ -55,57 +56,6 @@ ARGS." | |||
55 | (when msg | 56 | (when msg |
56 | (message "%s" msg))))) | 57 | (message "%s" msg))))) |
57 | 58 | ||
58 | (defun +sunrise-sunset--encode (time) | ||
59 | "Encode diary-style time string into a time. | ||
60 | This is stolen from `run-at-time'." | ||
61 | (let ((hhmm (diary-entry-time time)) | ||
62 | (now (decode-time))) | ||
63 | (encode-time (list 0 (% hhmm 100) (/ hhmm 100) | ||
64 | (decoded-time-day now) | ||
65 | (decoded-time-month now) | ||
66 | (decoded-time-year now) | ||
67 | nil -1 | ||
68 | (decoded-time-zone now))))) | ||
69 | |||
70 | (defun +sunrise-sunset (sunrise-command sunset-command &optional reset) | ||
71 | "Run SUNRISE-COMMAND at sunrise, and SUNSET-COMMAND at sunset. | ||
72 | With RESET, this function will call itself with its own | ||
73 | arguments. That's really only useful within this function | ||
74 | itself." | ||
75 | (let* ((times-regex (rx (* nonl) | ||
76 | (: (any ?s ?S) "unrise") " " | ||
77 | (group (repeat 1 2 digit) ":" | ||
78 | (repeat 1 2 digit) | ||
79 | (: (any ?a ?A ?p ?P) (any ?m ?M))) | ||
80 | (* nonl) | ||
81 | (: (any ?s ?S) "unset") " " | ||
82 | (group (repeat 1 2 digit) ":" | ||
83 | (repeat 1 2 digit) | ||
84 | (: (any ?a ?A ?p ?P) (any ?m ?M))) | ||
85 | (* nonl))) | ||
86 | (ss (+suppress-messages #'sunrise-sunset)) | ||
87 | (_m (string-match times-regex ss)) | ||
88 | (sunrise (match-string 1 ss)) | ||
89 | (sunset (match-string 2 ss)) | ||
90 | (sunrise-time (+sunrise-sunset--encode sunrise)) | ||
91 | (sunset-time (+sunrise-sunset--encode sunset))) | ||
92 | (cond | ||
93 | ((time-less-p nil sunrise-time) | ||
94 | ;; If it isn't sunrise yet, it's still dark---and so we need to run the | ||
95 | ;; sunset-command. | ||
96 | (funcall sunset-command) | ||
97 | (run-at-time sunrise nil sunrise-command)) | ||
98 | ((time-less-p nil sunset-time) | ||
99 | ;; If it isn't sunset yet, it's still light---so we need to run the | ||
100 | ;; sunrise-command. | ||
101 | (funcall sunrise-command) | ||
102 | (run-at-time sunset nil sunset-command)) | ||
103 | (t (run-at-time "12:00am" nil sunset-command))) | ||
104 | ;; Reset everything at midnight | ||
105 | (unless reset | ||
106 | (run-at-time "12:00am" (* 60 60 24) | ||
107 | #'+sunrise-sunset sunrise-command sunset-command t)))) | ||
108 | |||
109 | (defun +ensure-after-init (function) | 59 | (defun +ensure-after-init (function) |
110 | "Ensure FUNCTION runs after init, or now if already initialized. | 60 | "Ensure FUNCTION runs after init, or now if already initialized. |
111 | If Emacs is already started, run FUNCTION. Otherwise, add it to | 61 | If Emacs is already started, run FUNCTION. Otherwise, add it to |