org-timer.el 12 KB

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