org-timer.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. ;;; org-timer.el --- Timer code for Org mode -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2008-2019 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  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
  119. (seconds-to-time
  120. ;; Pass `current-time' result to `float-time' (instead
  121. ;; of calling without arguments) so that only
  122. ;; `current-time' has to be overridden in tests.
  123. (- (float-time (current-time)) delta))))
  124. (setq org-timer-pause-time nil)
  125. (org-timer-set-mode-line 'on)
  126. (message "Timer start time set to %s, current value is %s"
  127. (format-time-string "%T" org-timer-start-time)
  128. (org-timer-secs-to-hms (or delta 0)))
  129. (run-hooks 'org-timer-start-hook)))))
  130. ;;;###autoload
  131. (defun org-timer-pause-or-continue (&optional stop)
  132. "Pause or continue the relative or countdown timer.
  133. With prefix arg STOP, stop it entirely."
  134. (interactive "P")
  135. (cond
  136. (stop (org-timer-stop))
  137. ((not org-timer-start-time) (error "No timer is running"))
  138. (org-timer-pause-time
  139. (let ((start-secs (float-time org-timer-start-time))
  140. (pause-secs (float-time org-timer-pause-time)))
  141. (if org-timer-countdown-timer
  142. (let ((new-secs (- start-secs pause-secs)))
  143. (setq org-timer-countdown-timer
  144. (org-timer--run-countdown-timer
  145. new-secs org-timer-countdown-timer-title))
  146. (setq org-timer-start-time
  147. (time-add (current-time) (seconds-to-time new-secs))))
  148. (setq org-timer-start-time
  149. ;; Pass `current-time' result to `float-time' (instead
  150. ;; of calling without arguments) so that only
  151. ;; `current-time' has to be overridden in tests.
  152. (seconds-to-time (- (float-time (current-time))
  153. (- pause-secs start-secs)))))
  154. (setq org-timer-pause-time nil)
  155. (org-timer-set-mode-line 'on)
  156. (run-hooks 'org-timer-continue-hook)
  157. (message "Timer continues at %s" (org-timer-value-string))))
  158. (t
  159. ;; pause timer
  160. (when org-timer-countdown-timer
  161. (cancel-timer org-timer-countdown-timer)
  162. (setq org-timer-countdown-timer 'paused))
  163. (run-hooks 'org-timer-pause-hook)
  164. (setq org-timer-pause-time (current-time))
  165. (org-timer-set-mode-line 'paused)
  166. (message "Timer paused at %s" (org-timer-value-string)))))
  167. ;;;###autoload
  168. (defun org-timer-stop ()
  169. "Stop the relative or countdown timer."
  170. (interactive)
  171. (unless org-timer-start-time
  172. (user-error "No timer running"))
  173. (when (timerp org-timer-countdown-timer)
  174. (cancel-timer org-timer-countdown-timer))
  175. (run-hooks 'org-timer-stop-hook)
  176. (setq org-timer-start-time nil
  177. org-timer-pause-time nil
  178. org-timer-countdown-timer nil)
  179. (org-timer-set-mode-line 'off)
  180. (message "Timer stopped"))
  181. ;;;###autoload
  182. (defun org-timer (&optional restart no-insert)
  183. "Insert a H:MM:SS string from the timer into the buffer.
  184. The first time this command is used, the timer is started.
  185. When used with a `\\[universal-argument]' prefix, force restarting the timer.
  186. When used with a `\\[universal-argument] \\[universal-argument]' \
  187. prefix, change all the timer strings
  188. in the region by a fixed amount. This can be used to re-calibrate
  189. a timer that was not started at the correct moment.
  190. If NO-INSERT is non-nil, return the string instead of inserting
  191. it in the buffer."
  192. (interactive "P")
  193. (if (equal restart '(16))
  194. (org-timer-start restart)
  195. (when (or (equal restart '(4)) (not org-timer-start-time))
  196. (org-timer-start))
  197. (if no-insert
  198. (org-timer-value-string)
  199. (insert (org-timer-value-string)))))
  200. (defun org-timer-value-string ()
  201. "Set the timer string."
  202. (format org-timer-format
  203. (org-timer-secs-to-hms
  204. (abs (floor (org-timer-seconds))))))
  205. (defun org-timer-seconds ()
  206. ;; Pass `current-time' result to `float-time' (instead of calling
  207. ;; without arguments) so that only `current-time' has to be
  208. ;; overridden in tests.
  209. (if org-timer-countdown-timer
  210. (- (float-time org-timer-start-time)
  211. (float-time (or org-timer-pause-time (current-time))))
  212. (- (float-time (or org-timer-pause-time (current-time)))
  213. (float-time org-timer-start-time))))
  214. ;;;###autoload
  215. (defun org-timer-change-times-in-region (beg end delta)
  216. "Change all h:mm:ss time in region by a DELTA."
  217. (interactive
  218. "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
  219. (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
  220. (unless (string-match "\\S-" delta)
  221. (save-excursion
  222. (goto-char beg)
  223. (when (re-search-forward re end t)
  224. (setq delta (match-string 0))
  225. (if (equal (string-to-char delta) ?-)
  226. (setq delta (substring delta 1))
  227. (setq delta (concat "-" delta))))))
  228. (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
  229. (when (= delta 0) (error "No change"))
  230. (save-excursion
  231. (goto-char end)
  232. (while (re-search-backward re beg t)
  233. (setq p (point))
  234. (replace-match
  235. (save-match-data
  236. (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
  237. t t)
  238. (goto-char p)))))
  239. ;;;###autoload
  240. (defun org-timer-item (&optional arg)
  241. "Insert a description-type item with the current timer value."
  242. (interactive "P")
  243. (let ((itemp (org-in-item-p)) (pos (point)))
  244. (cond
  245. ;; In a timer list, insert with `org-list-insert-item',
  246. ;; then fix the list.
  247. ((and itemp (goto-char itemp) (org-at-item-timer-p))
  248. (let* ((struct (org-list-struct))
  249. (prevs (org-list-prevs-alist struct))
  250. (s (concat (org-timer (when arg '(4)) t) ":: ")))
  251. (setq struct (org-list-insert-item pos struct prevs nil s))
  252. (org-list-write-struct struct (org-list-parents-alist struct))
  253. (looking-at org-list-full-item-re)
  254. (goto-char (match-end 0))))
  255. ;; In a list of another type, don't break anything: throw an error.
  256. (itemp (goto-char pos) (error "This is not a timer list"))
  257. ;; Else, start a new list.
  258. (t
  259. (beginning-of-line)
  260. (org-indent-line)
  261. (insert "- ")
  262. (org-timer (when arg '(4)))
  263. (insert ":: ")))))
  264. (defun org-timer-fix-incomplete (hms)
  265. "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
  266. (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
  267. (replace-match
  268. (format "%d:%02d:%02d"
  269. (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
  270. (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
  271. (string-to-number (match-string 3 hms)))
  272. t t hms)
  273. (error "Cannot parse HMS string \"%s\"" hms)))
  274. (defun org-timer-hms-to-secs (hms)
  275. "Convert h:mm:ss string to an integer time.
  276. If the string starts with a minus sign, the integer will be negative."
  277. (if (not (string-match
  278. "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
  279. hms))
  280. 0
  281. (let* ((h (string-to-number (match-string 1 hms)))
  282. (m (string-to-number (match-string 2 hms)))
  283. (s (string-to-number (match-string 3 hms)))
  284. (sign (equal (substring (match-string 1 hms) 0 1) "-")))
  285. (setq h (abs h))
  286. (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
  287. (defun org-timer-secs-to-hms (s)
  288. "Convert integer S into h:mm:ss.
  289. If the integer is negative, the string will start with \"-\"."
  290. (let (sign m h)
  291. (setq sign (if (< s 0) "-" "")
  292. s (abs s)
  293. m (/ s 60) s (- s (* 60 m))
  294. h (/ m 60) m (- m (* 60 h)))
  295. (format "%s%d:%02d:%02d" sign h m s)))
  296. (defvar org-timer-mode-line-timer nil)
  297. (defvar org-timer-mode-line-string nil)
  298. (defun org-timer-set-mode-line (value)
  299. "Set the mode-line display for relative or countdown timer.
  300. VALUE can be `on', `off', or `paused'."
  301. (when (or (eq org-timer-display 'mode-line)
  302. (eq org-timer-display 'both))
  303. (or global-mode-string (setq global-mode-string '("")))
  304. (or (memq 'org-timer-mode-line-string global-mode-string)
  305. (setq global-mode-string
  306. (append global-mode-string '(org-timer-mode-line-string)))))
  307. (when (or (eq org-timer-display 'frame-title)
  308. (eq org-timer-display 'both))
  309. (or (memq 'org-timer-mode-line-string frame-title-format)
  310. (setq frame-title-format
  311. (append frame-title-format '(org-timer-mode-line-string)))))
  312. (cl-case value
  313. (off
  314. (when org-timer-mode-line-timer
  315. (cancel-timer org-timer-mode-line-timer)
  316. (setq org-timer-mode-line-timer nil))
  317. (when (or (eq org-timer-display 'mode-line)
  318. (eq org-timer-display 'both))
  319. (setq global-mode-string
  320. (delq 'org-timer-mode-line-string global-mode-string)))
  321. (when (or (eq org-timer-display 'frame-title)
  322. (eq org-timer-display 'both))
  323. (setq frame-title-format
  324. (delq 'org-timer-mode-line-string frame-title-format)))
  325. (force-mode-line-update))
  326. (paused
  327. (when org-timer-mode-line-timer
  328. (cancel-timer org-timer-mode-line-timer)
  329. (setq org-timer-mode-line-timer nil)))
  330. (on
  331. (when (or (eq org-timer-display 'mode-line)
  332. (eq org-timer-display 'both))
  333. (or global-mode-string (setq global-mode-string '("")))
  334. (or (memq 'org-timer-mode-line-string global-mode-string)
  335. (setq global-mode-string
  336. (append global-mode-string '(org-timer-mode-line-string)))))
  337. (when (or (eq org-timer-display 'frame-title)
  338. (eq org-timer-display 'both))
  339. (or (memq 'org-timer-mode-line-string frame-title-format)
  340. (setq frame-title-format
  341. (append frame-title-format '(org-timer-mode-line-string)))))
  342. (org-timer-update-mode-line)
  343. (when org-timer-mode-line-timer
  344. (cancel-timer org-timer-mode-line-timer)
  345. (setq org-timer-mode-line-timer nil))
  346. (when org-timer-display
  347. (setq org-timer-mode-line-timer
  348. (run-with-timer 1 1 'org-timer-update-mode-line))))))
  349. (defun org-timer-update-mode-line ()
  350. "Update the timer time in the mode line."
  351. (if org-timer-pause-time
  352. nil
  353. (setq org-timer-mode-line-string
  354. (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
  355. (force-mode-line-update)))
  356. (defun org-timer-show-remaining-time ()
  357. "Display the remaining time before the timer ends."
  358. (interactive)
  359. (require 'time)
  360. (if (not org-timer-countdown-timer)
  361. (message "No timer set")
  362. (let* ((rtime (decode-time
  363. (time-subtract (timer--time org-timer-countdown-timer)
  364. (current-time))))
  365. (rsecs (nth 0 rtime))
  366. (rmins (nth 1 rtime)))
  367. (message "%d minute(s) %d seconds left before next time out"
  368. rmins rsecs))))
  369. ;;;###autoload
  370. (defun org-timer-set-timer (&optional opt)
  371. "Prompt for a duration in minutes or hh:mm:ss and set a timer.
  372. If `org-timer-default-timer' is not \"0\", suggest this value as
  373. the default duration for the timer. If a timer is already set,
  374. prompt the user if she wants to replace it.
  375. Called with a numeric prefix argument, use this numeric value as
  376. the duration of the timer in minutes.
  377. Called with a `C-u' prefix arguments, use `org-timer-default-timer'
  378. without prompting the user for a duration.
  379. With two `C-u' prefix arguments, use `org-timer-default-timer'
  380. without prompting the user for a duration and automatically
  381. replace any running timer.
  382. By default, the timer duration will be set to the number of
  383. minutes in the Effort property, if any. You can ignore this by
  384. using three `C-u' prefix arguments."
  385. (interactive "P")
  386. (when (and org-timer-start-time
  387. (not org-timer-countdown-timer))
  388. (user-error "Relative timer is running. Stop first"))
  389. (let* ((default-timer
  390. ;; `org-timer-default-timer' used to be a number, don't choke:
  391. (if (numberp org-timer-default-timer)
  392. (number-to-string org-timer-default-timer)
  393. org-timer-default-timer))
  394. (effort-minutes (let ((effort (org-entry-get nil org-effort-property)))
  395. (when (org-string-nw-p effort)
  396. (floor (org-duration-to-minutes effort)))))
  397. (minutes (or (and (numberp opt) (number-to-string opt))
  398. (and (not (equal opt '(64)))
  399. effort-minutes
  400. (number-to-string effort-minutes))
  401. (and (consp opt) default-timer)
  402. (and (stringp opt) opt)
  403. (read-from-minibuffer
  404. "How much time left? (minutes or h:mm:ss) "
  405. (and (not (string-equal default-timer "0")) default-timer)))))
  406. (when (string-match "\\`[0-9]+\\'" minutes)
  407. (setq minutes (concat minutes ":00")))
  408. (if (not (string-match "[0-9]+" minutes))
  409. (org-timer-show-remaining-time)
  410. (let ((secs (org-timer-hms-to-secs (org-timer-fix-incomplete minutes))))
  411. (if (and org-timer-countdown-timer
  412. (not (or (equal opt '(16))
  413. (y-or-n-p "Replace current timer? "))))
  414. (message "No timer set")
  415. (when (timerp org-timer-countdown-timer)
  416. (cancel-timer org-timer-countdown-timer))
  417. (setq org-timer-countdown-timer-title
  418. (org-timer--get-timer-title))
  419. (setq org-timer-countdown-timer
  420. (org-timer--run-countdown-timer
  421. secs org-timer-countdown-timer-title))
  422. (run-hooks 'org-timer-set-hook)
  423. (setq org-timer-start-time
  424. (time-add (current-time) (seconds-to-time secs)))
  425. (setq org-timer-pause-time nil)
  426. (org-timer-set-mode-line 'on))))))
  427. (defun org-timer--run-countdown-timer (secs title)
  428. "Start countdown timer that will last SECS.
  429. TITLE will be appended to the notification message displayed when
  430. time is up."
  431. (let ((msg (format "%s: time out" title)))
  432. (run-with-timer
  433. secs nil `(lambda ()
  434. (setq org-timer-countdown-timer nil
  435. org-timer-start-time nil)
  436. (org-notify ,msg ,org-clock-sound)
  437. (org-timer-set-mode-line 'off)
  438. (run-hooks 'org-timer-done-hook)))))
  439. (defun org-timer--get-timer-title ()
  440. "Construct timer title from heading or file name of Org buffer."
  441. (cond
  442. ((derived-mode-p 'org-agenda-mode)
  443. (let* ((marker (or (get-text-property (point) 'org-marker)
  444. (org-agenda-error)))
  445. (hdmarker (or (get-text-property (point) 'org-hd-marker)
  446. marker)))
  447. (with-current-buffer (marker-buffer marker)
  448. (org-with-wide-buffer
  449. (goto-char hdmarker)
  450. (org-show-entry)
  451. (or (ignore-errors (org-get-heading))
  452. (buffer-name (buffer-base-buffer)))))))
  453. ((derived-mode-p 'org-mode)
  454. (or (ignore-errors (org-get-heading))
  455. (buffer-name (buffer-base-buffer))))
  456. (t (error "Not in an Org buffer"))))
  457. (provide 'org-timer)
  458. ;; Local variables:
  459. ;; generated-autoload-file: "org-loaddefs.el"
  460. ;; End:
  461. ;;; org-timer.el ends here