Here's an Emacs Lisp function that implements a Pomodoro-style timer as per your specifications:
(defvar my-pomodoro-timer--timer nil "Internal timer object for pomodoro timer.")
(defvar my-pomodoro-timer--total-time 0 "Total time for the pomodoro timer in seconds.")
(defvar my-pomodoro-timer--time-left 0 "Time left for the pomodoro timer in seconds.")
(defvar my-pomodoro-timer--current-interval-time-left 0 "Time left in the current interval in seconds.")
(defvar my-pomodoro-timer--state 'work "Current state of the pomodoro timer: 'work or 'play.")
(defvar my-pomodoro-timer--mode-line-string "" "String to display in the mode line for the pomodoro timer.")
(defun my-pomodoro-timer (minutes)
"Start a Pomodoro-style timer for MINUTES minutes.
The timer will alternate between 'work' and 'play' every 10 minutes,
and display the time left in the mode line. It sends a desktop
notification on macOS every time it switches."
(interactive "nTotal minutes: ")
;; Initialize variables
(setq my-pomodoro-timer--total-time (* minutes 60)) ;; total time in seconds
(setq my-pomodoro-timer--time-left my-pomodoro-timer--total-time)
(setq my-pomodoro-timer--state 'work)
(setq my-pomodoro-timer--current-interval-time-left (min 600 my-pomodoro-timer--time-left))
;; Start the timer
(when my-pomodoro-timer--timer
(cancel-timer my-pomodoro-timer--timer))
(setq my-pomodoro-timer--timer (run-at-time 0 1 #'my-pomodoro-timer--tick))
;; Add the mode line string
(unless (member 'my-pomodoro-timer--mode-line-string global-mode-string)
(setq global-mode-string (append global-mode-string '(my-pomodoro-timer--mode-line-string))))
(force-mode-line-update)
(message "Pomodoro Timer Started for %d minutes." minutes))
(defun my-pomodoro-timer--tick ()
"Function called every second to update the pomodoro timer."
(setq my-pomodoro-timer--time-left (1- my-pomodoro-timer--time-left))
(setq my-pomodoro-timer--current-interval-time-left (1- my-pomodoro-timer--current-interval-time-left))
;; Check if total time is up
(when (<= my-pomodoro-timer--time-left 0)
(my-pomodoro-timer--stop)
(my-pomodoro-timer--notify "Pomodoro Timer Finished" "Time is up!")
(message "Pomodoro Timer Finished"))
;; Check if interval is up
(when (<= my-pomodoro-timer--current-interval-time-left 0)
(my-pomodoro-timer--toggle-state)
;; Set interval time left: either 10 minutes or remaining time
(setq my-pomodoro-timer--current-interval-time-left (min 600 my-pomodoro-timer--time-left)))
;; Update mode line
(my-pomodoro-timer--update-mode-line))
(defun my-pomodoro-timer--toggle-state ()
"Toggle the pomodoro timer state between 'work' and 'play', and send notification."
(setq my-pomodoro-timer--state (if (eq my-pomodoro-timer--state 'work) 'play 'work))
(my-pomodoro-timer--notify (format "Pomodoro Timer: %s" (capitalize (symbol-name my-pomodoro-timer--state)))
(format "Switch to %s mode." (symbol-name my-pomodoro-timer--state)))
(message "Switched to %s mode." (capitalize (symbol-name my-pomodoro-timer--state))))
(defun my-pomodoro-timer--update-mode-line ()
"Update the mode line string for the pomodoro timer."
(let* ((total-minutes (/ my-pomodoro-timer--time-left 60))
(total-seconds (% my-pomodoro-timer--time-left 60))
(interval-minutes (/ my-pomodoro-timer--current-interval-time-left 60))
(interval-seconds (% my-pomodoro-timer--current-interval-time-left 60)))
(setq my-pomodoro-timer--mode-line-string
(format " [%s: %d:%02d left (%d:%02d)]"
(capitalize (symbol-name my-pomodoro-timer--state))
total-minutes total-seconds
interval-minutes interval-seconds)))
(force-mode-line-update))
(defun my-pomodoro-timer--notify (title message)
"Send a desktop notification with TITLE and MESSAGE on macOS."
(start-process "pomodoro-notify" nil "osascript" "-e"
(format "display notification \"%s\" with title \"%s\""
message title)))
(defun my-pomodoro-timer--stop ()
"Stop the pomodoro timer."
(when my-pomodoro-timer--timer
(cancel-timer my-pomodoro-timer--timer)
(setq my-pomodoro-timer--timer nil))
;; Remove mode line string
(setq global-mode-string (remove 'my-pomodoro-timer--mode-line-string global-mode-string))
(force-mode-line-update))
(defun my-pomodoro-timer-cancel ()
"Cancel the pomodoro timer."
(interactive)
(my-pomodoro-timer--stop)
(message "Pomodoro Timer Cancelled"))
To use this function:
- Save the code in your Emacs configuration or evaluate it in an Emacs buffer.
- Run
M-x my-pomodoro-timer and enter the total number of minutes when prompted.
- The time left will be displayed in the mode line, updating every second.
- Every 10 minutes, it will switch between 'work' and 'play' modes, sending a desktop notification on macOS each time it switches.
- When the total time elapses, it will stop the timer and send a final notification.
To manually cancel the timer at any time, run M-x my-pomodoro-timer-cancel.
This function uses osascript to send desktop notifications on macOS. It updates the mode line every second to display both the total time left and the time left in the current interval.
Note: The time intervals switch every 10 minutes, regardless of the total time. If the remaining total time is less than 10 minutes, the last interval will be shorter than 10 minutes.