Add timers

This commit is contained in:
Case Duckworth 2022-05-12 22:38:33 -05:00
parent b3c2d9ded9
commit 02addc0aff
1 changed files with 51 additions and 0 deletions

View File

@ -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