org-timer.el 18 KB

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