org-timer.el 14 KB

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