org-timer.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. ;;; org-timer.el --- Timer code for Org mode -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; URL: https://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file implements two types of timers for Org buffers:
  23. ;;
  24. ;; - A relative timer that counts up (from 0 or a specified offset)
  25. ;; - A countdown timer that counts down from a specified time
  26. ;;
  27. ;; The relative and countdown timers differ in their entry points.
  28. ;; Use `org-timer' or `org-timer-start' to start the relative timer,
  29. ;; and `org-timer-set-timer' to start the countdown timer.
  30. ;;; Code:
  31. (require 'org-macs)
  32. (org-assert-version)
  33. (require 'cl-lib)
  34. (require 'org-clock)
  35. (declare-function org-agenda-error "org-agenda" ())
  36. (defvar org-timer-start-time nil
  37. "t=0 for the running timer.")
  38. (defvar org-timer-pause-time nil
  39. "Time when the timer was paused.")
  40. (defvar org-timer-countdown-timer nil
  41. "Current countdown timer.
  42. This is a timer object if there is an active countdown timer,
  43. `paused' if there is a paused countdown timer, and nil
  44. otherwise.")
  45. (defvar org-timer-countdown-timer-title nil
  46. "Title for notification displayed when a countdown finishes.")
  47. (defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
  48. "Regular expression used to match timer stamps.")
  49. (defcustom org-timer-format "%s "
  50. "The format to insert the time of the timer.
  51. This format must contain one instance of \"%s\" which will be replaced by
  52. the value of the timer."
  53. :group 'org-time
  54. :type 'string)
  55. (defcustom org-timer-default-timer "0"
  56. "The default timer when a timer is set, in minutes or hh:mm:ss format.
  57. When 0, the user is prompted for a value."
  58. :group 'org-time
  59. :version "26.1"
  60. :package-version '(Org . "8.3")
  61. :type 'string)
  62. (defcustom org-timer-display 'mode-line
  63. "Define where running timer is displayed, if at all.
  64. When a timer is running, Org can display it in the mode line
  65. and/or frame title. Allowed values are:
  66. both displays in both mode line and frame title
  67. mode-line displays only in mode line (default)
  68. frame-title displays only in frame title
  69. nil current timer is not displayed"
  70. :group 'org-time
  71. :type '(choice
  72. (const :tag "Mode line" mode-line)
  73. (const :tag "Frame title" frame-title)
  74. (const :tag "Both" both)
  75. (const :tag "None" nil)))
  76. (defvar org-timer-start-hook nil
  77. "Hook run after relative timer is started.")
  78. (defvar org-timer-stop-hook nil
  79. "Hook run before relative or countdown timer is stopped.")
  80. (defvar org-timer-pause-hook nil
  81. "Hook run before relative or countdown timer is paused.")
  82. (defvar org-timer-continue-hook nil
  83. "Hook run after relative or countdown timer is continued.")
  84. (defvar org-timer-set-hook nil
  85. "Hook run after countdown timer is set.")
  86. (defvar org-timer-done-hook nil
  87. "Hook run after countdown timer reaches zero.")
  88. ;;;###autoload
  89. (defun org-timer-start (&optional offset)
  90. "Set the starting time for the relative timer to now.
  91. When called with prefix argument OFFSET, prompt the user for an offset time,
  92. with the default taken from a timer stamp at point, if any.
  93. If OFFSET is a string or an integer, it is directly taken to be the offset
  94. without user interaction.
  95. When called with a double prefix arg, all timer strings in the active
  96. region will be shifted by a specific amount. You will be prompted for
  97. the amount, with the default to make the first timer string in
  98. the region 0:00:00."
  99. (interactive "P")
  100. (cond
  101. ((equal offset '(16))
  102. (call-interactively 'org-timer-change-times-in-region))
  103. (org-timer-countdown-timer
  104. (user-error "Countdown timer is running. Cancel first"))
  105. (t
  106. (let (delta def s)
  107. (if (not offset)
  108. (setq org-timer-start-time (current-time))
  109. (cond
  110. ((integerp offset) (setq delta offset))
  111. ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
  112. (t
  113. (setq def (if (org-in-regexp org-timer-re)
  114. (match-string 0)
  115. "0:00:00")
  116. s (read-string
  117. (format "Restart timer with offset [%s]: " def)))
  118. (unless (string-match "\\S-" s) (setq s def))
  119. (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
  120. (setq org-timer-start-time (time-since delta)))
  121. (setq org-timer-pause-time nil)
  122. (org-timer-set-mode-line 'on)
  123. (message "Timer start time set to %s, current value is %s"
  124. (format-time-string "%T" org-timer-start-time)
  125. (org-timer-secs-to-hms (or delta 0)))
  126. (run-hooks 'org-timer-start-hook)))))
  127. ;;;###autoload
  128. (defun org-timer-pause-or-continue (&optional stop)
  129. "Pause or continue the relative or countdown timer.
  130. With prefix arg STOP, stop it entirely."
  131. (interactive "P")
  132. (cond
  133. (stop (org-timer-stop))
  134. ((not org-timer-start-time) (error "No timer is running"))
  135. (org-timer-pause-time
  136. (let ((start-secs (float-time org-timer-start-time))
  137. (pause-secs (float-time org-timer-pause-time)))
  138. (if org-timer-countdown-timer
  139. (let ((new-secs (- start-secs pause-secs)))
  140. (setq org-timer-countdown-timer
  141. (org-timer--run-countdown-timer
  142. new-secs org-timer-countdown-timer-title))
  143. (setq org-timer-start-time (time-add nil new-secs)))
  144. (setq org-timer-start-time
  145. (time-since (- pause-secs start-secs))))
  146. (setq org-timer-pause-time nil)
  147. (org-timer-set-mode-line 'on)
  148. (run-hooks 'org-timer-continue-hook)
  149. (message "Timer continues at %s" (org-timer-value-string))))
  150. (t
  151. ;; pause timer
  152. (when org-timer-countdown-timer
  153. (cancel-timer org-timer-countdown-timer)
  154. (setq org-timer-countdown-timer 'paused))
  155. (run-hooks 'org-timer-pause-hook)
  156. (setq org-timer-pause-time (current-time))
  157. (org-timer-set-mode-line 'paused)
  158. (message "Timer paused at %s" (org-timer-value-string)))))
  159. ;;;###autoload
  160. (defun org-timer-stop ()
  161. "Stop the relative or countdown timer."
  162. (interactive)
  163. (unless org-timer-start-time
  164. (user-error "No timer running"))
  165. (when (timerp org-timer-countdown-timer)
  166. (cancel-timer org-timer-countdown-timer))
  167. (run-hooks 'org-timer-stop-hook)
  168. (setq org-timer-start-time nil
  169. org-timer-pause-time nil
  170. org-timer-countdown-timer nil)
  171. (org-timer-set-mode-line 'off)
  172. (message "Timer stopped"))
  173. ;;;###autoload
  174. (defun org-timer (&optional restart no-insert)
  175. "Insert a H:MM:SS string from the timer into the buffer.
  176. The first time this command is used, the timer is started.
  177. When used with a `\\[universal-argument]' prefix, force restarting the timer.
  178. When used with a `\\[universal-argument] \\[universal-argument]' \
  179. prefix, change all the timer strings
  180. in the region by a fixed amount. This can be used to re-calibrate
  181. a timer that was not started at the correct moment.
  182. If NO-INSERT is non-nil, return the string instead of inserting
  183. it in the buffer."
  184. (interactive "P")
  185. (if (equal restart '(16))
  186. (org-timer-start restart)
  187. (when (or (equal restart '(4)) (not org-timer-start-time))
  188. (org-timer-start))
  189. (if no-insert
  190. (org-timer-value-string)
  191. (insert (org-timer-value-string)))))
  192. (defun org-timer-value-string ()
  193. "Return current timer string."
  194. (format org-timer-format
  195. (org-timer-secs-to-hms
  196. (let ((time (- (float-time org-timer-pause-time)
  197. (float-time org-timer-start-time))))
  198. (abs (floor (if org-timer-countdown-timer (- time) time)))))))
  199. ;;;###autoload
  200. (defun org-timer-change-times-in-region (beg end delta)
  201. "Change all h:mm:ss time in region by a DELTA."
  202. (interactive
  203. "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
  204. (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
  205. (unless (string-match "\\S-" delta)
  206. (save-excursion
  207. (goto-char beg)
  208. (when (re-search-forward re end t)
  209. (setq delta (match-string 0))
  210. (if (equal (string-to-char delta) ?-)
  211. (setq delta (substring delta 1))
  212. (setq delta (concat "-" delta))))))
  213. (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
  214. (when (= delta 0) (error "No change"))
  215. (save-excursion
  216. (goto-char end)
  217. (while (re-search-backward re beg t)
  218. (setq p (point))
  219. (replace-match
  220. (save-match-data
  221. (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
  222. t t)
  223. (goto-char p)))))
  224. ;;;###autoload
  225. (defun org-timer-item (&optional arg)
  226. "Insert a description-type item with the current timer value."
  227. (interactive "P")
  228. (let ((itemp (org-in-item-p)) (pos (point)))
  229. (cond
  230. ;; In a timer list, insert with `org-list-insert-item',
  231. ;; then fix the list.
  232. ((and itemp (goto-char itemp) (org-at-item-timer-p))
  233. (let* ((struct (org-list-struct))
  234. (prevs (org-list-prevs-alist struct))
  235. (s (concat (org-timer (when arg '(4)) t) ":: ")))
  236. (setq struct (org-list-insert-item pos struct prevs nil s))
  237. (org-list-write-struct struct (org-list-parents-alist struct))
  238. (looking-at org-list-full-item-re)
  239. (goto-char (match-end 0))))
  240. ;; In a list of another type, don't break anything: throw an error.
  241. (itemp (goto-char pos) (error "This is not a timer list"))
  242. ;; Else, start a new list.
  243. (t
  244. (beginning-of-line)
  245. (org-indent-line)
  246. (insert "- ")
  247. (org-timer (when arg '(4)))
  248. (insert ":: ")))))
  249. (defun org-timer-fix-incomplete (hms)
  250. "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
  251. (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
  252. (replace-match
  253. (format "%d:%02d:%02d"
  254. (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
  255. (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
  256. (string-to-number (match-string 3 hms)))
  257. t t hms)
  258. (error "Cannot parse HMS string \"%s\"" hms)))
  259. (defun org-timer-hms-to-secs (hms)
  260. "Convert h:mm:ss string to an integer time.
  261. If the string starts with a minus sign, the integer will be negative."
  262. (if (not (string-match
  263. "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
  264. hms))
  265. 0
  266. (let* ((h (string-to-number (match-string 1 hms)))
  267. (m (string-to-number (match-string 2 hms)))
  268. (s (string-to-number (match-string 3 hms)))
  269. (sign (equal (substring (match-string 1 hms) 0 1) "-")))
  270. (setq h (abs h))
  271. (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
  272. (defun org-timer-secs-to-hms (s)
  273. "Convert integer S into h:mm:ss.
  274. If the integer is negative, the string will start with \"-\"."
  275. (let (sign m h)
  276. (setq sign (if (< s 0) "-" "")
  277. s (abs s)
  278. m (/ s 60) s (- s (* 60 m))
  279. h (/ m 60) m (- m (* 60 h)))
  280. (format "%s%d:%02d:%02d" sign h m s)))
  281. (defvar org-timer-mode-line-timer nil)
  282. (defvar org-timer-mode-line-string nil)
  283. (defun org-timer-set-mode-line (value)
  284. "Set the mode-line display for relative or countdown timer.
  285. VALUE can be `on', `off', or `paused'."
  286. (when (or (eq org-timer-display 'mode-line)
  287. (eq org-timer-display 'both))
  288. (or global-mode-string (setq global-mode-string '("")))
  289. (or (memq 'org-timer-mode-line-string global-mode-string)
  290. (setq global-mode-string
  291. (append global-mode-string '(org-timer-mode-line-string)))))
  292. (when (or (eq org-timer-display 'frame-title)
  293. (eq org-timer-display 'both))
  294. (or (memq 'org-timer-mode-line-string frame-title-format)
  295. (setq frame-title-format
  296. (append frame-title-format '(org-timer-mode-line-string)))))
  297. (cl-case value
  298. (off
  299. (when org-timer-mode-line-timer
  300. (cancel-timer org-timer-mode-line-timer)
  301. (setq org-timer-mode-line-timer nil))
  302. (when (or (eq org-timer-display 'mode-line)
  303. (eq org-timer-display 'both))
  304. (setq global-mode-string
  305. (delq 'org-timer-mode-line-string global-mode-string)))
  306. (when (or (eq org-timer-display 'frame-title)
  307. (eq org-timer-display 'both))
  308. (setq frame-title-format
  309. (delq 'org-timer-mode-line-string frame-title-format)))
  310. (force-mode-line-update))
  311. (paused
  312. (when org-timer-mode-line-timer
  313. (cancel-timer org-timer-mode-line-timer)
  314. (setq org-timer-mode-line-timer nil)))
  315. (on
  316. (when (or (eq org-timer-display 'mode-line)
  317. (eq org-timer-display 'both))
  318. (or global-mode-string (setq global-mode-string '("")))
  319. (or (memq 'org-timer-mode-line-string global-mode-string)
  320. (setq global-mode-string
  321. (append global-mode-string '(org-timer-mode-line-string)))))
  322. (when (or (eq org-timer-display 'frame-title)
  323. (eq org-timer-display 'both))
  324. (or (memq 'org-timer-mode-line-string frame-title-format)
  325. (setq frame-title-format
  326. (append frame-title-format '(org-timer-mode-line-string)))))
  327. (org-timer-update-mode-line)
  328. (when org-timer-mode-line-timer
  329. (cancel-timer org-timer-mode-line-timer)
  330. (setq org-timer-mode-line-timer nil))
  331. (when org-timer-display
  332. (setq org-timer-mode-line-timer
  333. (run-with-timer 1 1 #'org-timer-update-mode-line))))))
  334. (defun org-timer-update-mode-line ()
  335. "Update the timer time in the mode line."
  336. (if org-timer-pause-time
  337. nil
  338. (setq org-timer-mode-line-string
  339. (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
  340. (force-mode-line-update)))
  341. (defun org-timer-show-remaining-time ()
  342. "Display the remaining time before the timer ends."
  343. (interactive)
  344. (message
  345. (if (not org-timer-countdown-timer)
  346. "No timer set"
  347. (format-seconds
  348. "%m minute(s) %s seconds left before next time out"
  349. ;; Note: Once our minimal require is Emacs 27, we can drop this
  350. ;; org-time-convert-to-integer call.
  351. (org-time-convert-to-integer
  352. (time-subtract (timer--time org-timer-countdown-timer) nil))))))
  353. ;;;###autoload
  354. (defun org-timer-set-timer (&optional opt)
  355. "Prompt for a duration in minutes or hh:mm:ss and set a timer.
  356. If `org-timer-default-timer' is not \"0\", suggest this value as
  357. the default duration for the timer. If a timer is already set,
  358. prompt the user if she wants to replace it.
  359. Called with a numeric prefix argument, use this numeric value as
  360. the duration of the timer in minutes.
  361. Called with a \\[universal-argument] prefix arguments, use `org-timer-default-timer'
  362. without prompting the user for a duration.
  363. With two \\[universal-argument] prefix arguments, use `org-timer-default-timer'
  364. without prompting the user for a duration and automatically
  365. replace any running timer.
  366. By default, the timer duration will be set to the number of
  367. minutes in the Effort property, if any. You can ignore this by
  368. using three \\[universal-argument] prefix arguments."
  369. (interactive "P")
  370. (when (and org-timer-start-time
  371. (not org-timer-countdown-timer))
  372. (user-error "Relative timer is running. Stop first"))
  373. (let* ((default-timer
  374. ;; `org-timer-default-timer' used to be a number, don't choke:
  375. (if (numberp org-timer-default-timer)
  376. (number-to-string org-timer-default-timer)
  377. org-timer-default-timer))
  378. (effort-minutes (let ((effort (org-entry-get nil org-effort-property)))
  379. (when (org-string-nw-p effort)
  380. (floor (org-duration-to-minutes effort)))))
  381. (minutes (or (and (numberp opt) (number-to-string opt))
  382. (and (not (equal opt '(64)))
  383. effort-minutes
  384. (number-to-string effort-minutes))
  385. (and (consp opt) default-timer)
  386. (and (stringp opt) opt)
  387. (read-from-minibuffer
  388. "How much time left? (minutes or h:mm:ss) "
  389. (and (not (string-equal default-timer "0")) default-timer)))))
  390. (when (string-match "\\`[0-9]+\\'" minutes)
  391. (setq minutes (concat minutes ":00")))
  392. (if (not (string-match "[0-9]+" minutes))
  393. (org-timer-show-remaining-time)
  394. (let ((secs (org-timer-hms-to-secs (org-timer-fix-incomplete minutes))))
  395. (if (and org-timer-countdown-timer
  396. (not (or (equal opt '(16))
  397. (y-or-n-p "Replace current timer? "))))
  398. (message "No timer set")
  399. (when (timerp org-timer-countdown-timer)
  400. (cancel-timer org-timer-countdown-timer))
  401. (setq org-timer-countdown-timer-title
  402. (org-timer--get-timer-title))
  403. (setq org-timer-countdown-timer
  404. (org-timer--run-countdown-timer
  405. secs org-timer-countdown-timer-title))
  406. (run-hooks 'org-timer-set-hook)
  407. (setq org-timer-start-time (time-add nil secs))
  408. (setq org-timer-pause-time nil)
  409. (org-timer-set-mode-line 'on))))))
  410. (defun org-timer--run-countdown-timer (secs title)
  411. "Start countdown timer that will last SECS.
  412. TITLE will be appended to the notification message displayed when
  413. time is up."
  414. (let ((msg (format "%s: time out" title))
  415. (sound org-clock-sound))
  416. (run-with-timer
  417. secs nil (lambda ()
  418. (setq org-timer-countdown-timer nil
  419. org-timer-start-time nil)
  420. (org-notify msg sound)
  421. (org-timer-set-mode-line 'off)
  422. (run-hooks 'org-timer-done-hook)))))
  423. (defun org-timer--get-timer-title ()
  424. "Construct timer title.
  425. Try to use an Org header, otherwise use the buffer name."
  426. (cond
  427. ((derived-mode-p 'org-agenda-mode)
  428. (let* ((marker (or (get-text-property (point) 'org-marker)))
  429. (hdmarker (or (get-text-property (point) 'org-hd-marker)
  430. marker)))
  431. (when (and marker (marker-buffer marker))
  432. (with-current-buffer (marker-buffer marker)
  433. (org-with-wide-buffer
  434. (goto-char hdmarker)
  435. (or (ignore-errors (org-get-heading))
  436. (buffer-name (buffer-base-buffer))))))))
  437. ((derived-mode-p 'org-mode)
  438. (ignore-errors (org-get-heading)))
  439. (t (buffer-name (buffer-base-buffer)))))
  440. (provide 'org-timer)
  441. ;; Local variables:
  442. ;; generated-autoload-file: "org-loaddefs.el"
  443. ;; End:
  444. ;;; org-timer.el ends here