summary refs log tree commit diff stats
path: root/lisp/dawn.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/dawn.el')
-rw-r--r--lisp/dawn.el109
1 files changed, 0 insertions, 109 deletions
diff --git a/lisp/dawn.el b/lisp/dawn.el deleted file mode 100644 index 30aab7c..0000000 --- a/lisp/dawn.el +++ /dev/null
@@ -1,109 +0,0 @@
1;;; dawn.el --- Lightweight dawn/dusk task scheduling -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2022 Case Duckworth
4
5;; Author: Case Duckworth
6;; Maintainer: Case Duckworth <acdw@acdw.net>
7;; URL: https://codeberg.org/acdw/dusk.el
8;; Version: 0.3.0
9;; Keywords: calendar, themes, convenience
10;; Package-Requires: ((emacs "24.3"))
11
12;;; Commentary:
13
14;; There is also circadian.el, but it doesn't quite work for me.
15;; This code comes mostly from https://gnu.xyz/auto_theme.html, but also
16;; somewhere else (which I've forgotten) and my own brain :)
17
18;;; Code:
19
20(require 'calendar)
21(require 'cl-lib)
22(require 'solar)
23
24;;; Timers
25
26(defvar dawn--dawn-timer nil
27 "Timer for dawn-command.")
28
29(defvar dawn--dusk-timer nil
30 "Timer for dusk-command.")
31
32(defvar dawn--reset-timer nil
33 "Timer to reset dawn at midnight.")
34
35;;; Functions
36
37(defun dawn-encode-time (f)
38 "Encode fractional time F.
39If F is nil, return nil."
40 (when f
41 (let ((hhmm (cl-floor f))
42 (date (cdddr (decode-time))))
43 (encode-time
44 (append (list 0
45 (round (* 60 (cadr hhmm)))
46 (car hhmm))
47 date)))))
48
49(defun dawn-midnight ()
50 "Return the time of the /next/ midnight."
51 (let ((date (cdddr (decode-time))))
52 (encode-time
53 (append (list 0 0 0 (1+ (car date))) (cdr date)))))
54
55(defun dawn-sunrise ()
56 "Return the time of today's sunrise."
57 (dawn-encode-time (caar (solar-sunrise-sunset (calendar-current-date)))))
58
59(defun dawn-sunset ()
60 "Return the time of today's sunset."
61 (dawn-encode-time (caadr (solar-sunrise-sunset (calendar-current-date)))))
62
63;;; Interface
64
65;;;###autoload
66(defun dawn-schedule (dawn-command dusk-command)
67 "Run DAWN-COMMAND at sunrise, and DUSK-COMMAND at dusk.
68Requires `calendar-longitude' and `calendar-latitude' to be set;
69if they're not, it will prompt the user for them or error."
70 (when (or (null calendar-longitude)
71 (null calendar-latitude))
72 (or (solar-setup)
73 (user-error "`dawn' won't work without setting %s!"
74 (cond ((and (null calendar-longitude)
75 (null calendar-latitude))
76 "`calendar-longitude' and `calendar-latitude'")
77 ((null calendar-longitude)
78 "`calendar-longitude'")
79 ((null calendar-latitude)
80 "`calendar-latitude'")))))
81 (let ((dawn (dawn-sunrise))
82 (dusk (dawn-sunset)))
83 (cond
84 ((or (null dawn) (null dusk))
85 ;; There is no sunrise or sunset, due to how close we are to the poles.
86 ;; In this case, we must figure out whether it's day or night.
87 (pcase (caddr (solar-sunrise-sunset (calendar-current-date)))
88 ("0:00" (funcall dusk-command)) ; 0 hours of daylight
89 ("24:00" (funcall dawn-command)) ; 24 hours of daylight
90 ))
91 ((time-less-p nil dawn)
92 ;; If it isn't dawn yet, it's still dark. Run DUSK-COMMAND and schedule
93 ;; DAWN-COMMAND and DUSK-COMMAND for later.
94 (funcall dusk-command)
95 (run-at-time dawn nil dawn-command)
96 (run-at-time dusk nil dusk-command))
97 ((time-less-p nil dusk)
98 ;; If it isn't dusk yet, it's still light. Run DAWN-COMMAND and schedule
99 ;; DUSK-COMMAND.
100 (funcall dawn-command)
101 (run-at-time dusk nil dusk-command))
102 (t ;; Otherwise, it's past dusk, so run DUSK-COMMAND.
103 (funcall dusk-command)))
104 ;; Schedule a reset at midnight, to re-calculate dawn/dusk times.
105 (run-at-time (dawn-midnight) nil
106 #'dawn-schedule dawn-command dusk-command)))
107
108(provide 'dawn)
109;;; dawn.el ends here