* image-mode.el (image-mode): Add image-after-revert-hook to after-revert-hook.
[bpt/emacs.git] / lisp / org / org-timer.el
CommitLineData
0bd48b37 1;;; org-timer.el --- The relative timer code for Org-mode
bc23baaa 2
114f9c96 3;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
bc23baaa
CD
4
5;; Author: Carsten Dominik <carsten at orgmode dot org>
6;; Keywords: outlines, hypermedia, calendar, wp
7;; Homepage: http://orgmode.org
ed21c5c8 8;; Version: 6.35i
bc23baaa
CD
9;;
10;; This file is part of GNU Emacs.
11;;
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25;;
26;;; Commentary:
27
28;; This file contains the relative timer code for Org-mode
29
30(require 'org)
31
c8d0cf5c
CD
32(declare-function org-show-notification "org-clock" (parameters))
33(declare-function org-agenda-error "org-agenda" ())
34
bc23baaa
CD
35(defvar org-timer-start-time nil
36 "t=0 for the running timer.")
37
0bd48b37
CD
38(defvar org-timer-pause-time nil
39 "Time when the timer was paused.")
40
bc23baaa
CD
41(defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
42 "Regular expression used to match timer stamps.")
43
44(defcustom org-timer-format "%s "
45 "The format to insert the time of the timer.
46This format must contain one instance of \"%s\" which will be replaced by
47the value of the relative timer."
48 :group 'org-time
49 :type 'string)
50
ed21c5c8
CD
51(defvar org-timer-start-hook nil
52 "Hook run after relative timer is started.")
53
54(defvar org-timer-stop-hook nil
55 "Hook run before relative timer is stopped.")
56
57(defvar org-timer-pause-hook nil
58 "Hook run before relative timer is paused.")
59
60(defvar org-timer-set-hook nil
61 "Hook run after countdown timer is set.")
62
63(defvar org-timer-done-hook nil
64 "Hook run after countdown timer reaches zero.")
65
66(defvar org-timer-cancel-hook nil
67 "Hook run before countdown timer is canceled.")
68
bc23baaa
CD
69;;;###autoload
70(defun org-timer-start (&optional offset)
71 "Set the starting time for the relative timer to now.
72When called with prefix argument OFFSET, prompt the user for an offset time,
73with the default taken from a timer stamp at point, if any.
74If OFFSET is a string or an integer, it is directly taken to be the offset
75without user interaction.
76When called with a double prefix arg, all timer strings in the active
77region will be shifted by a specific amount. You will be prompted for
78the amount, with the default to make the first timer string in
79the region 0:00:00."
80 (interactive "P")
81 (if (equal offset '(16))
82 (call-interactively 'org-timer-change-times-in-region)
83 (let (delta def s)
84 (if (not offset)
85 (setq org-timer-start-time (current-time))
86 (cond
87 ((integerp offset) (setq delta offset))
88 ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
89 (t
90 (setq def (if (org-in-regexp org-timer-re)
91 (match-string 0)
92 "0:00:00")
93 s (read-string
94 (format "Restart timer with offset [%s]: " def)))
95 (unless (string-match "\\S-" s) (setq s def))
96 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
97 (setq org-timer-start-time
98 (seconds-to-time
54a0dee5 99 (- (org-float-time) (org-timer-hms-to-secs s)))))
0bd48b37 100 (org-timer-set-mode-line 'on)
bc23baaa
CD
101 (message "Timer start time set to %s, current value is %s"
102 (format-time-string "%T" org-timer-start-time)
ed21c5c8
CD
103 (org-timer-secs-to-hms (or delta 0)))
104 (run-hooks 'org-timer-start-hook))))
bc23baaa 105
0bd48b37
CD
106(defun org-timer-pause-or-continue (&optional stop)
107 "Pause or continue the relative timer. With prefix arg, stop it entirely."
108 (interactive "P")
109 (cond
110 (stop (org-timer-stop))
111 ((not org-timer-start-time) (error "No timer is running"))
112 (org-timer-pause-time
113 ;; timer is paused, continue
114 (setq org-timer-start-time
115 (seconds-to-time
116 (-
54a0dee5
CD
117 (org-float-time)
118 (- (org-float-time org-timer-pause-time)
119 (org-float-time org-timer-start-time))))
0bd48b37
CD
120 org-timer-pause-time nil)
121 (org-timer-set-mode-line 'on)
122 (message "Timer continues at %s" (org-timer-value-string)))
123 (t
124 ;; pause timer
ed21c5c8 125 (run-hooks 'org-timer-pause-hook)
0bd48b37
CD
126 (setq org-timer-pause-time (current-time))
127 (org-timer-set-mode-line 'pause)
128 (message "Timer paused at %s" (org-timer-value-string)))))
129
130(defun org-timer-stop ()
131 "Stop the relative timer."
132 (interactive)
ed21c5c8 133 (run-hooks 'org-timer-stop-hook)
0bd48b37
CD
134 (setq org-timer-start-time nil
135 org-timer-pause-time nil)
136 (org-timer-set-mode-line 'off))
137
bc23baaa
CD
138;;;###autoload
139(defun org-timer (&optional restart)
140 "Insert a H:MM:SS string from the timer into the buffer.
141The first time this command is used, the timer is started. When used with
142a `C-u' prefix, force restarting the timer.
143When used with a double prefix arg `C-u C-u', change all the timer string
144in the region by a fixed amount. This can be used to recalibrate a timer
145that was not started at the correct moment."
146 (interactive "P")
147 (if (equal restart '(4)) (org-timer-start))
148 (or org-timer-start-time (org-timer-start))
0bd48b37
CD
149 (insert (org-timer-value-string)))
150
151(defun org-timer-value-string ()
152 (format org-timer-format (org-timer-secs-to-hms (floor (org-timer-seconds)))))
153
154(defun org-timer-seconds ()
54a0dee5
CD
155 (- (org-float-time (or org-timer-pause-time (current-time)))
156 (org-float-time org-timer-start-time)))
bc23baaa
CD
157
158;;;###autoload
159(defun org-timer-change-times-in-region (beg end delta)
160 "Change all h:mm:ss time in region by a DELTA."
161 (interactive
162 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
163 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
164 (unless (string-match "\\S-" delta)
165 (save-excursion
166 (goto-char beg)
167 (when (re-search-forward re end t)
168 (setq delta (match-string 0))
169 (if (equal (string-to-char delta) ?-)
170 (setq delta (substring delta 1))
171 (setq delta (concat "-" delta))))))
172 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
173 (when (= delta 0) (error "No change"))
174 (save-excursion
175 (goto-char end)
176 (while (re-search-backward re beg t)
177 (setq p (point))
178 (replace-match
179 (save-match-data
180 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
181 t t)
182 (goto-char p)))))
183
184;;;###autoload
185(defun org-timer-item (&optional arg)
33306645 186 "Insert a description-type item with the current timer value."
bc23baaa
CD
187 (interactive "P")
188 (let ((ind 0))
189 (save-excursion
190 (skip-chars-backward " \n\t")
191 (condition-case nil
192 (progn
193 (org-beginning-of-item)
194 (setq ind (org-get-indentation)))
195 (error nil)))
196 (or (bolp) (newline))
197 (org-indent-line-to ind)
198 (insert "- ")
199 (org-timer (if arg '(4)))
200 (insert ":: ")))
201
202(defun org-timer-fix-incomplete (hms)
203 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
204 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
205 (replace-match
206 (format "%d:%02d:%02d"
207 (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
208 (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
209 (string-to-number (match-string 3 hms)))
210 t t hms)
33306645 211 (error "Cannot parse HMS string \"%s\"" hms)))
bc23baaa
CD
212
213(defun org-timer-hms-to-secs (hms)
214 "Convert h:mm:ss string to an integer time.
215If the string starts with a minus sign, the integer will be negative."
216 (if (not (string-match
217 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
218 hms))
219 0
220 (let* ((h (string-to-number (match-string 1 hms)))
221 (m (string-to-number (match-string 2 hms)))
222 (s (string-to-number (match-string 3 hms)))
223 (sign (equal (substring (match-string 1 hms) 0 1) "-")))
224 (setq h (abs h))
225 (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
226
227(defun org-timer-secs-to-hms (s)
228 "Convert integer S into h:mm:ss.
33306645 229If the integer is negative, the string will start with \"-\"."
bc23baaa
CD
230 (let (sign m h)
231 (setq sign (if (< s 0) "-" "")
232 s (abs s)
233 m (/ s 60) s (- s (* 60 m))
234 h (/ m 60) m (- m (* 60 h)))
235 (format "%s%d:%02d:%02d" sign h m s)))
236
0bd48b37
CD
237(defvar org-timer-mode-line-timer nil)
238(defvar org-timer-mode-line-string nil)
239
240(defun org-timer-set-mode-line (value)
8bfe682a 241 "Set the mode-line display of the relative timer.
0bd48b37
CD
242VALUE can be `on', `off', or `pause'."
243 (or global-mode-string (setq global-mode-string '("")))
244 (or (memq 'org-timer-mode-line-string global-mode-string)
245 (setq global-mode-string
246 (append global-mode-string '(org-timer-mode-line-string))))
247 (cond
248 ((equal value 'off)
249 (when org-timer-mode-line-timer
250 (cancel-timer org-timer-mode-line-timer)
251 (setq org-timer-mode-line-timer nil))
252 (setq global-mode-string
253 (delq 'org-timer-mode-line-string global-mode-string))
254 (force-mode-line-update))
255 ((equal value 'pause)
256 (when org-timer-mode-line-timer
257 (cancel-timer org-timer-mode-line-timer)
258 (setq org-timer-mode-line-timer nil)))
259 ((equal value 'on)
260 (or global-mode-string (setq global-mode-string '("")))
261 (or (memq 'org-timer-mode-line-string global-mode-string)
262 (setq global-mode-string
263 (append global-mode-string '(org-timer-mode-line-string))))
264 (org-timer-update-mode-line)
265 (when org-timer-mode-line-timer
266 (cancel-timer org-timer-mode-line-timer))
267 (setq org-timer-mode-line-timer
268 (run-with-timer 1 1 'org-timer-update-mode-line)))))
269
270(defun org-timer-update-mode-line ()
271 "Update the timer time in the mode line."
272 (if org-timer-pause-time
273 nil
274 (setq org-timer-mode-line-string
275 (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
276 (force-mode-line-update)))
277
ed21c5c8
CD
278(defvar org-timer-current-timer nil)
279(defun org-timer-cancel-timer ()
280 "Cancel the current timer."
c8d0cf5c 281 (interactive)
ed21c5c8
CD
282 (when (eval org-timer-current-timer)
283 (run-hooks 'org-timer-cancel-hook)
284 (cancel-timer org-timer-current-timer)
285 (setq org-timer-current-timer nil))
286 (message "Last timer canceled"))
c8d0cf5c
CD
287
288(defun org-timer-show-remaining-time ()
289 "Display the remaining time before the timer ends."
290 (interactive)
291 (require 'time)
ed21c5c8 292 (if (not org-timer-current-timer)
c8d0cf5c
CD
293 (message "No timer set")
294 (let* ((rtime (decode-time
ed21c5c8 295 (time-subtract (timer--time org-timer-current-timer)
c8d0cf5c
CD
296 (current-time))))
297 (rsecs (nth 0 rtime))
298 (rmins (nth 1 rtime)))
ed21c5c8 299 (message "%d minute(s) %d seconds left before next time out"
c8d0cf5c
CD
300 rmins rsecs))))
301
302;;;###autoload
303(defun org-timer-set-timer (minutes)
304 "Set a timer."
305 (interactive "sTime out in (min)? ")
306 (if (not (string-match "[0-9]+" minutes))
307 (org-timer-show-remaining-time)
308 (let* ((mins (string-to-number (match-string 0 minutes)))
309 (secs (* mins 60))
310 (hl (cond
311 ((string-match "Org Agenda" (buffer-name))
312 (let* ((marker (or (get-text-property (point) 'org-marker)
313 (org-agenda-error)))
314 (hdmarker (or (get-text-property (point) 'org-hd-marker)
315 marker))
316 (pos (marker-position marker)))
317 (with-current-buffer (marker-buffer marker)
318 (widen)
319 (goto-char pos)
320 (org-show-entry)
321 (org-get-heading))))
322 ((eq major-mode 'org-mode)
323 (org-get-heading))
324 (t (error "Not in an Org buffer"))))
325 timer-set)
ed21c5c8
CD
326 (if org-timer-current-timer
327 (error "You cannot run several timers at the same time")
328 (setq org-timer-current-timer
329 (run-with-timer
330 secs nil `(lambda ()
331 (setq org-timer-current-timer nil)
332 (org-notify ,(format "%s: time out" hl) t)
333 (run-hooks 'org-timer-done-hook))))
334 (run-hooks 'org-timer-set-hook)))))
c8d0cf5c 335
a2a2e7fb
CD
336(provide 'org-timer)
337
bc23baaa
CD
338;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
339
340;;; org-timer.el ends here