org-timer.el 18 KB

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