org-timer.el 18 KB

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