From 02addc0aff6863007646bc02a53a182ed44678fe Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 12 May 2022 22:38:33 -0500 Subject: [PATCH] Add timers --- lisp/acdw.el | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lisp/acdw.el b/lisp/acdw.el index 3052976..9b3ab93 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el @@ -457,5 +457,56 @@ sort order." (car args)) (cdr args))) + +;;; Timers! +;; inspired by [[https://git.sr.ht/~protesilaos/tmr/tree/main/item/tmr.el][prot's tmr.el package]] + +(defvar +timer-string nil) +(defvar +timer-timer nil) + +(defcustom +timer-running-string "⏰" + "What to display when the timer is running." + :type 'string) +(defcustom +timer-done-string "❗" + "What to display when the timer is done." + :type 'string) + +(defun +timer (time) + "Set a timer for TIME." + (interactive (list (read-string "Set a timer for how long? "))) + (let ((secs (cond ((natnump time) (* time 60)) + ((and (stringp time) + (string-match-p "[0-9]\\'" time)) + (* (string-to-number time) 60)) + (t (let ((secs 0) + (time time)) + (save-match-data + (while (string-match "\\([0-9.]+\\)\\([hms]\\)" time) + (cl-incf secs + (* (string-to-number (match-string 1 time)) + (pcase (match-string 2 time) + ("h" 3600) + ("m" 60) + ("s" 1)))) + (setq time (substring time (match-end 0))))) + secs))))) + (message "Setting timer for \"%s\" (%S seconds)..." time secs) + (setq +timer-string +timer-running-string) + (setq +timer-timer (run-with-timer secs nil + (lambda () + (message "%S-second timer DONE!" secs) + (setq +timer-string +timer-done-string) + (ding)))))) + +(defun +timer-cancel () + "Cancel the running timer." + (interactive) + (cond ((not +timer-timer) + (message "No timer found!")) + (t + (cancel-timer +timer-timer) + (message "Timer canceled."))) + (setq +timer-string nil)) + (provide 'acdw) ;;; acdw.el ends here