org-timer.el 11 KB

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