org-timer.el 16 KB

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