org-timer.el 18 KB

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