org-timer.el 18 KB

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