org-timer.el 11 KB

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