From 259363fd4f21d796c3c6a35be6398aed3f493a73 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Tue, 3 Jan 2023 23:02:26 -0600 Subject: bleh --- lisp/dawn.el | 67 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 21 deletions(-) (limited to 'lisp/dawn.el') 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 @@ -;;; dawn.el --- Do things at dawn (and dusk) -*- lexical-binding: t; -*- +;;; dawn.el --- Lightweight dawn/dusk task scheduling -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Case Duckworth + +;; Author: Case Duckworth +;; Maintainer: Case Duckworth +;; URL: https://codeberg.org/acdw/dusk.el +;; Version: 0.3.0 +;; Keywords: calendar, themes, convenience +;; Package-Requires: ((emacs "24.3")) ;;; Commentary: @@ -12,6 +21,8 @@ (require 'cl-lib) (require 'solar) +;;; Timers + (defvar dawn--dawn-timer nil "Timer for dawn-command.") @@ -21,16 +32,19 @@ (defvar dawn--reset-timer nil "Timer to reset dawn at midnight.") +;;; Functions + (defun dawn-encode-time (f) - "Encode fractional time F." - (let ((hhmm (cl-floor f)) - (date (cdddr (decode-time)))) - (encode-time - (append (list 0 - (round (* 60 (cadr hhmm))) - (car hhmm) - ) - date)))) + "Encode fractional time F. +If F is nil, return nil." + (when f + (let ((hhmm (cl-floor f)) + (date (cdddr (decode-time)))) + (encode-time + (append (list 0 + (round (* 60 (cadr hhmm))) + (car hhmm)) + date))))) (defun dawn-midnight () "Return the time of the /next/ midnight." @@ -46,22 +60,34 @@ "Return the time of today's sunset." (dawn-encode-time (caadr (solar-sunrise-sunset (calendar-current-date))))) +;;; Interface + +;;;###autoload (defun dawn-schedule (dawn-command dusk-command) "Run DAWN-COMMAND at sunrise, and DUSK-COMMAND at dusk. -RESET is an argument for internal use." +Requires `calendar-longitude' and `calendar-latitude' to be set; +if they're not, it will prompt the user for them or error." (when (or (null calendar-longitude) - (null calendar-latitude)) - (user-error "`dawn' won't work without setting %s!" - (cond ((and (null calendar-longitude) - (null calendar-latitude)) - "`calendar-longitude' and `calendar-latitude'") - ((null calendar-longitude) - "`calendar-longitude'") - ((null calendar-latitude) - "`calendar-latitude'")))) + (null calendar-latitude)) + (or (solar-setup) + (user-error "`dawn' won't work without setting %s!" + (cond ((and (null calendar-longitude) + (null calendar-latitude)) + "`calendar-longitude' and `calendar-latitude'") + ((null calendar-longitude) + "`calendar-longitude'") + ((null calendar-latitude) + "`calendar-latitude'"))))) (let ((dawn (dawn-sunrise)) (dusk (dawn-sunset))) (cond + ((or (null dawn) (null dusk)) + ;; There is no sunrise or sunset, due to how close we are to the poles. + ;; In this case, we must figure out whether it's day or night. + (pcase (caddr (solar-sunrise-sunset (calendar-current-date))) + ("0:00" (funcall dusk-command)) ; 0 hours of daylight + ("24:00" (funcall dawn-command)) ; 24 hours of daylight + )) ((time-less-p nil dawn) ;; If it isn't dawn yet, it's still dark. Run DUSK-COMMAND and schedule ;; DAWN-COMMAND and DUSK-COMMAND for later. @@ -76,7 +102,6 @@ RESET is an argument for internal use." (t ;; Otherwise, it's past dusk, so run DUSK-COMMAND. (funcall dusk-command))) ;; Schedule a reset at midnight, to re-calculate dawn/dusk times. - ;(unless reset) (run-at-time (dawn-midnight) nil #'dawn-schedule dawn-command dusk-command))) -- cgit 1.4.1-21-gabe81