org-timer.el 14 KB

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