org-timer.el 12 KB

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