org-timer.el 18 KB

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