about 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.el67
1 files changed, 46 insertions, 21 deletions
diff --git a/lisp/dawn.el b/lisp/dawn.el index 806c422..30aab7c 100644 --- a/lisp/dawn.el +++ b/lisp/dawn.el
@@ -1,4 +1,13 @@
1;;; dawn.el --- Do things at dawn (and dusk) -*- lexical-binding: t; -*- 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"))
2 11
3;;; Commentary: 12;;; Commentary:
4 13
@@ -12,6 +21,8 @@
12(require 'cl-lib) 21(require 'cl-lib)
13(require 'solar) 22(require 'solar)
14 23
24;;; Timers
25
15(defvar dawn--dawn-timer nil 26(defvar dawn--dawn-timer nil
16 "Timer for dawn-command.") 27 "Timer for dawn-command.")
17 28
@@ -21,16 +32,19 @@
21(defvar dawn--reset-timer nil 32(defvar dawn--reset-timer nil
22 "Timer to reset dawn at midnight.") 33 "Timer to reset dawn at midnight.")
23 34
35;;; Functions
36
24(defun dawn-encode-time (f) 37(defun dawn-encode-time (f)
25 "Encode fractional time F." 38 "Encode fractional time F.
26 (let ((hhmm (cl-floor f)) 39If F is nil, return nil."
27 (date (cdddr (decode-time)))) 40 (when f
28 (encode-time 41 (let ((hhmm (cl-floor f))
29 (append (list 0 42 (date (cdddr (decode-time))))
30 (round (* 60 (cadr hhmm))) 43 (encode-time
31 (car hhmm) 44 (append (list 0
32 ) 45 (round (* 60 (cadr hhmm)))
33 date)))) 46 (car hhmm))
47 date)))))
34 48
35(defun dawn-midnight () 49(defun dawn-midnight ()
36 "Return the time of the /next/ midnight." 50 "Return the time of the /next/ midnight."
@@ -46,22 +60,34 @@
46 "Return the time of today's sunset." 60 "Return the time of today's sunset."
47 (dawn-encode-time (caadr (solar-sunrise-sunset (calendar-current-date))))) 61 (dawn-encode-time (caadr (solar-sunrise-sunset (calendar-current-date)))))
48 62
63;;; Interface
64
65;;;###autoload
49(defun dawn-schedule (dawn-command dusk-command) 66(defun dawn-schedule (dawn-command dusk-command)
50 "Run DAWN-COMMAND at sunrise, and DUSK-COMMAND at dusk. 67 "Run DAWN-COMMAND at sunrise, and DUSK-COMMAND at dusk.
51RESET is an argument for internal use." 68Requires `calendar-longitude' and `calendar-latitude' to be set;
69if they're not, it will prompt the user for them or error."
52 (when (or (null calendar-longitude) 70 (when (or (null calendar-longitude)
53 (null calendar-latitude)) 71 (null calendar-latitude))
54 (user-error "`dawn' won't work without setting %s!" 72 (or (solar-setup)
55 (cond ((and (null calendar-longitude) 73 (user-error "`dawn' won't work without setting %s!"
56 (null calendar-latitude)) 74 (cond ((and (null calendar-longitude)
57 "`calendar-longitude' and `calendar-latitude'") 75 (null calendar-latitude))
58 ((null calendar-longitude) 76 "`calendar-longitude' and `calendar-latitude'")
59 "`calendar-longitude'") 77 ((null calendar-longitude)
60 ((null calendar-latitude) 78 "`calendar-longitude'")
61 "`calendar-latitude'")))) 79 ((null calendar-latitude)
80 "`calendar-latitude'")))))
62 (let ((dawn (dawn-sunrise)) 81 (let ((dawn (dawn-sunrise))
63 (dusk (dawn-sunset))) 82 (dusk (dawn-sunset)))
64 (cond 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 ))
65 ((time-less-p nil dawn) 91 ((time-less-p nil dawn)
66 ;; If it isn't dawn yet, it's still dark. Run DUSK-COMMAND and schedule 92 ;; If it isn't dawn yet, it's still dark. Run DUSK-COMMAND and schedule
67 ;; DAWN-COMMAND and DUSK-COMMAND for later. 93 ;; DAWN-COMMAND and DUSK-COMMAND for later.
@@ -76,7 +102,6 @@ RESET is an argument for internal use."
76 (t ;; Otherwise, it's past dusk, so run DUSK-COMMAND. 102 (t ;; Otherwise, it's past dusk, so run DUSK-COMMAND.
77 (funcall dusk-command))) 103 (funcall dusk-command)))
78 ;; Schedule a reset at midnight, to re-calculate dawn/dusk times. 104 ;; Schedule a reset at midnight, to re-calculate dawn/dusk times.
79 ;(unless reset)
80 (run-at-time (dawn-midnight) nil 105 (run-at-time (dawn-midnight) nil
81 #'dawn-schedule dawn-command dusk-command))) 106 #'dawn-schedule dawn-command dusk-command)))
82 107