org-timer.el 13 KB

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