org-timer.el 13 KB

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