Merge from emacs-23
[bpt/emacs.git] / lisp / org / org-timer.el
CommitLineData
0bd48b37 1;;; org-timer.el --- The relative timer code for Org-mode
bc23baaa 2
5df4f04c 3;; Copyright (C) 2008, 2009, 2010, 2011 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
acedf35c 8;; Version: 7.4
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
86fbb8ca
CD
30;;; Code:
31
bc23baaa
CD
32(require 'org)
33
afe98dfa 34(declare-function org-notify "org-clock" (notification &optional play-sound))
c8d0cf5c
CD
35(declare-function org-agenda-error "org-agenda" ())
36
bc23baaa
CD
37(defvar org-timer-start-time nil
38 "t=0 for the running timer.")
39
0bd48b37
CD
40(defvar org-timer-pause-time nil
41 "Time when the timer was paused.")
42
bc23baaa
CD
43(defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
44 "Regular expression used to match timer stamps.")
45
46(defcustom org-timer-format "%s "
47 "The format to insert the time of the timer.
48This format must contain one instance of \"%s\" which will be replaced by
49the value of the relative timer."
50 :group 'org-time
51 :type 'string)
52
86fbb8ca
CD
53(defcustom org-timer-default-timer 0
54 "The default timer when a timer is set.
55When 0, the user is prompted for a value."
56 :group 'org-time
57 :type 'number)
58
ed21c5c8
CD
59(defvar org-timer-start-hook nil
60 "Hook run after relative timer is started.")
61
62(defvar org-timer-stop-hook nil
63 "Hook run before relative timer is stopped.")
64
65(defvar org-timer-pause-hook nil
66 "Hook run before relative timer is paused.")
67
68(defvar org-timer-set-hook nil
69 "Hook run after countdown timer is set.")
70
71(defvar org-timer-done-hook nil
72 "Hook run after countdown timer reaches zero.")
73
74(defvar org-timer-cancel-hook nil
75 "Hook run before countdown timer is canceled.")
76
bc23baaa
CD
77;;;###autoload
78(defun org-timer-start (&optional offset)
79 "Set the starting time for the relative timer to now.
80When called with prefix argument OFFSET, prompt the user for an offset time,
81with the default taken from a timer stamp at point, if any.
82If OFFSET is a string or an integer, it is directly taken to be the offset
83without user interaction.
84When called with a double prefix arg, all timer strings in the active
85region will be shifted by a specific amount. You will be prompted for
86the amount, with the default to make the first timer string in
87the region 0:00:00."
88 (interactive "P")
89 (if (equal offset '(16))
90 (call-interactively 'org-timer-change-times-in-region)
91 (let (delta def s)
92 (if (not offset)
93 (setq org-timer-start-time (current-time))
94 (cond
95 ((integerp offset) (setq delta offset))
96 ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
97 (t
98 (setq def (if (org-in-regexp org-timer-re)
99 (match-string 0)
100 "0:00:00")
101 s (read-string
102 (format "Restart timer with offset [%s]: " def)))
103 (unless (string-match "\\S-" s) (setq s def))
104 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
105 (setq org-timer-start-time
106 (seconds-to-time
86fbb8ca 107 (- (org-float-time) delta))))
0bd48b37 108 (org-timer-set-mode-line 'on)
bc23baaa
CD
109 (message "Timer start time set to %s, current value is %s"
110 (format-time-string "%T" org-timer-start-time)
ed21c5c8
CD
111 (org-timer-secs-to-hms (or delta 0)))
112 (run-hooks 'org-timer-start-hook))))
bc23baaa 113
0bd48b37 114(defun org-timer-pause-or-continue (&optional stop)
86fbb8ca
CD
115 "Pause or continue the relative timer.
116With prefix arg STOP, stop it entirely."
0bd48b37
CD
117 (interactive "P")
118 (cond
119 (stop (org-timer-stop))
120 ((not org-timer-start-time) (error "No timer is running"))
121 (org-timer-pause-time
122 ;; timer is paused, continue
123 (setq org-timer-start-time
124 (seconds-to-time
125 (-
54a0dee5
CD
126 (org-float-time)
127 (- (org-float-time org-timer-pause-time)
128 (org-float-time org-timer-start-time))))
0bd48b37
CD
129 org-timer-pause-time nil)
130 (org-timer-set-mode-line 'on)
131 (message "Timer continues at %s" (org-timer-value-string)))
132 (t
133 ;; pause timer
ed21c5c8 134 (run-hooks 'org-timer-pause-hook)
0bd48b37
CD
135 (setq org-timer-pause-time (current-time))
136 (org-timer-set-mode-line 'pause)
137 (message "Timer paused at %s" (org-timer-value-string)))))
138
139(defun org-timer-stop ()
140 "Stop the relative timer."
141 (interactive)
ed21c5c8 142 (run-hooks 'org-timer-stop-hook)
0bd48b37
CD
143 (setq org-timer-start-time nil
144 org-timer-pause-time nil)
145 (org-timer-set-mode-line 'off))
146
bc23baaa 147;;;###autoload
afe98dfa 148(defun org-timer (&optional restart no-insert-p)
bc23baaa
CD
149 "Insert a H:MM:SS string from the timer into the buffer.
150The first time this command is used, the timer is started. When used with
86fbb8ca 151a \\[universal-argument] prefix, force restarting the timer.
afe98dfa 152When used with a double prefix argument \\[universal-argument], change all the timer string
bc23baaa 153in the region by a fixed amount. This can be used to recalibrate a timer
afe98dfa
CD
154that was not started at the correct moment.
155
156If NO-INSERT-P is non-nil, return the string instead of inserting
157it in the buffer."
bc23baaa 158 (interactive "P")
afe98dfa
CD
159 (when (or (equal restart '(4)) (not org-timer-start-time))
160 (org-timer-start))
161 (if no-insert-p
162 (org-timer-value-string)
163 (insert (org-timer-value-string))))
0bd48b37
CD
164
165(defun org-timer-value-string ()
166 (format org-timer-format (org-timer-secs-to-hms (floor (org-timer-seconds)))))
167
afe98dfa 168(defvar org-timer-timer-is-countdown nil)
0bd48b37 169(defun org-timer-seconds ()
afe98dfa
CD
170 (if org-timer-timer-is-countdown
171 (- (org-float-time org-timer-start-time)
172 (org-float-time (current-time)))
173 (- (org-float-time (or org-timer-pause-time (current-time)))
174 (org-float-time org-timer-start-time))))
bc23baaa
CD
175
176;;;###autoload
177(defun org-timer-change-times-in-region (beg end delta)
178 "Change all h:mm:ss time in region by a DELTA."
179 (interactive
180 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
181 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
182 (unless (string-match "\\S-" delta)
183 (save-excursion
184 (goto-char beg)
185 (when (re-search-forward re end t)
186 (setq delta (match-string 0))
187 (if (equal (string-to-char delta) ?-)
188 (setq delta (substring delta 1))
189 (setq delta (concat "-" delta))))))
190 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
191 (when (= delta 0) (error "No change"))
192 (save-excursion
193 (goto-char end)
194 (while (re-search-backward re beg t)
195 (setq p (point))
196 (replace-match
197 (save-match-data
198 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
199 t t)
200 (goto-char p)))))
201
202;;;###autoload
203(defun org-timer-item (&optional arg)
33306645 204 "Insert a description-type item with the current timer value."
bc23baaa 205 (interactive "P")
afe98dfa
CD
206 (cond
207 ;; In a timer list, insert with `org-list-insert-item-generic'.
208 ((and (org-in-item-p)
209 (save-excursion (org-beginning-of-item) (org-at-item-timer-p)))
210 (org-list-insert-item-generic
211 (point) nil (concat (org-timer (when arg '(4)) t) ":: ")))
212 ;; In a list of another type, don't break anything: throw an error.
213 ((org-in-item-p)
214 (error "This is not a timer list"))
215 ;; Else, insert the timer correctly indented at bol.
216 (t
217 (beginning-of-line)
218 (org-indent-line-function)
219 (insert "- ")
220 (org-timer (when arg '(4)))
221 (insert ":: "))))
bc23baaa
CD
222
223(defun org-timer-fix-incomplete (hms)
224 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
225 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
226 (replace-match
227 (format "%d:%02d:%02d"
228 (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
229 (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
230 (string-to-number (match-string 3 hms)))
231 t t hms)
33306645 232 (error "Cannot parse HMS string \"%s\"" hms)))
bc23baaa
CD
233
234(defun org-timer-hms-to-secs (hms)
235 "Convert h:mm:ss string to an integer time.
236If the string starts with a minus sign, the integer will be negative."
237 (if (not (string-match
238 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
239 hms))
240 0
241 (let* ((h (string-to-number (match-string 1 hms)))
242 (m (string-to-number (match-string 2 hms)))
243 (s (string-to-number (match-string 3 hms)))
244 (sign (equal (substring (match-string 1 hms) 0 1) "-")))
245 (setq h (abs h))
246 (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
247
248(defun org-timer-secs-to-hms (s)
249 "Convert integer S into h:mm:ss.
33306645 250If the integer is negative, the string will start with \"-\"."
bc23baaa
CD
251 (let (sign m h)
252 (setq sign (if (< s 0) "-" "")
253 s (abs s)
254 m (/ s 60) s (- s (* 60 m))
255 h (/ m 60) m (- m (* 60 h)))
256 (format "%s%d:%02d:%02d" sign h m s)))
257
0bd48b37
CD
258(defvar org-timer-mode-line-timer nil)
259(defvar org-timer-mode-line-string nil)
260
261(defun org-timer-set-mode-line (value)
8bfe682a 262 "Set the mode-line display of the relative timer.
0bd48b37
CD
263VALUE can be `on', `off', or `pause'."
264 (or global-mode-string (setq global-mode-string '("")))
265 (or (memq 'org-timer-mode-line-string global-mode-string)
266 (setq global-mode-string
267 (append global-mode-string '(org-timer-mode-line-string))))
268 (cond
269 ((equal value 'off)
270 (when org-timer-mode-line-timer
271 (cancel-timer org-timer-mode-line-timer)
272 (setq org-timer-mode-line-timer nil))
273 (setq global-mode-string
274 (delq 'org-timer-mode-line-string global-mode-string))
275 (force-mode-line-update))
276 ((equal value 'pause)
277 (when org-timer-mode-line-timer
278 (cancel-timer org-timer-mode-line-timer)
279 (setq org-timer-mode-line-timer nil)))
280 ((equal value 'on)
281 (or global-mode-string (setq global-mode-string '("")))
282 (or (memq 'org-timer-mode-line-string global-mode-string)
283 (setq global-mode-string
284 (append global-mode-string '(org-timer-mode-line-string))))
285 (org-timer-update-mode-line)
286 (when org-timer-mode-line-timer
287 (cancel-timer org-timer-mode-line-timer))
288 (setq org-timer-mode-line-timer
289 (run-with-timer 1 1 'org-timer-update-mode-line)))))
290
291(defun org-timer-update-mode-line ()
292 "Update the timer time in the mode line."
293 (if org-timer-pause-time
294 nil
295 (setq org-timer-mode-line-string
296 (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
297 (force-mode-line-update)))
298
ed21c5c8
CD
299(defvar org-timer-current-timer nil)
300(defun org-timer-cancel-timer ()
301 "Cancel the current timer."
c8d0cf5c 302 (interactive)
ed21c5c8
CD
303 (when (eval org-timer-current-timer)
304 (run-hooks 'org-timer-cancel-hook)
305 (cancel-timer org-timer-current-timer)
afe98dfa
CD
306 (setq org-timer-current-timer nil)
307 (setq org-timer-timer-is-countdown nil)
308 (org-timer-set-mode-line 'off))
ed21c5c8 309 (message "Last timer canceled"))
c8d0cf5c
CD
310
311(defun org-timer-show-remaining-time ()
312 "Display the remaining time before the timer ends."
313 (interactive)
314 (require 'time)
ed21c5c8 315 (if (not org-timer-current-timer)
c8d0cf5c
CD
316 (message "No timer set")
317 (let* ((rtime (decode-time
ed21c5c8 318 (time-subtract (timer--time org-timer-current-timer)
c8d0cf5c
CD
319 (current-time))))
320 (rsecs (nth 0 rtime))
321 (rmins (nth 1 rtime)))
ed21c5c8 322 (message "%d minute(s) %d seconds left before next time out"
c8d0cf5c
CD
323 rmins rsecs))))
324
325;;;###autoload
86fbb8ca
CD
326(defun org-timer-set-timer (&optional opt)
327 "Prompt for a duration and set a timer.
328
329If `org-timer-default-timer' is not zero, suggest this value as
330the default duration for the timer. If a timer is already set,
afe98dfa 331prompt the user if she wants to replace it.
86fbb8ca
CD
332
333Called with a numeric prefix argument, use this numeric value as
334the duration of the timer.
335
336Called with a `C-u' prefix arguments, use `org-timer-default-timer'
337without prompting the user for a duration.
338
339With two `C-u' prefix arguments, use `org-timer-default-timer'
340without prompting the user for a duration and automatically
341replace any running timer."
342 (interactive "P")
343 (let ((minutes (or (and (numberp opt) (number-to-string opt))
344 (and (listp opt) (not (null opt))
345 (number-to-string org-timer-default-timer))
346 (read-from-minibuffer
347 "How many minutes left? "
348 (if (not (eq org-timer-default-timer 0))
349 (number-to-string org-timer-default-timer))))))
350 (if (not (string-match "[0-9]+" minutes))
351 (org-timer-show-remaining-time)
c8d0cf5c
CD
352 (let* ((mins (string-to-number (match-string 0 minutes)))
353 (secs (* mins 60))
354 (hl (cond
355 ((string-match "Org Agenda" (buffer-name))
356 (let* ((marker (or (get-text-property (point) 'org-marker)
357 (org-agenda-error)))
358 (hdmarker (or (get-text-property (point) 'org-hd-marker)
359 marker))
360 (pos (marker-position marker)))
361 (with-current-buffer (marker-buffer marker)
362 (widen)
363 (goto-char pos)
364 (org-show-entry)
afe98dfa
CD
365 (or (ignore-errors (org-get-heading))
366 (concat "File:" (file-name-nondirectory (buffer-file-name)))))))
c8d0cf5c 367 ((eq major-mode 'org-mode)
afe98dfa
CD
368 (or (ignore-errors (org-get-heading))
369 (concat "File:" (file-name-nondirectory (buffer-file-name)))))
c8d0cf5c
CD
370 (t (error "Not in an Org buffer"))))
371 timer-set)
86fbb8ca
CD
372 (if (or (and org-timer-current-timer
373 (or (equal opt '(16))
374 (y-or-n-p "Replace current timer? ")))
375 (not org-timer-current-timer))
376 (progn
afe98dfa 377 (require 'org-clock)
86fbb8ca
CD
378 (when org-timer-current-timer
379 (cancel-timer org-timer-current-timer))
380 (setq org-timer-current-timer
381 (run-with-timer
382 secs nil `(lambda ()
383 (setq org-timer-current-timer nil)
384 (org-notify ,(format "%s: time out" hl) t)
afe98dfa
CD
385 (setq org-timer-timer-is-countdown nil)
386 (org-timer-set-mode-line 'off)
86fbb8ca 387 (run-hooks 'org-timer-done-hook))))
afe98dfa
CD
388 (run-hooks 'org-timer-set-hook)
389 (setq org-timer-timer-is-countdown t
390 org-timer-start-time
391 (time-add (current-time) (seconds-to-time (* mins 60))))
392 (org-timer-set-mode-line 'on))
86fbb8ca 393 (message "No timer set"))))))
c8d0cf5c 394
a2a2e7fb
CD
395(provide 'org-timer)
396
bc23baaa
CD
397;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
398
399;;; org-timer.el ends here