org-element: Elements can accept multiple secondary strings
[org-mode.git] / lisp / org-timer.el
blob0593573bc679f5e53c1ceed4b66543b0aa706fed
1 ;;; org-timer.el --- Timer code for Org mode
3 ;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file implements two types of timers for Org buffers:
29 ;; - A relative timer that counts up (from 0 or a specified offset)
30 ;; - A countdown timer that counts down from a specified time
32 ;; The relative and countdown timers differ in their entry points.
33 ;; Use `org-timer' or `org-timer-start' to start the relative timer,
34 ;; and `org-timer-set-timer' to start the countdown timer.
36 ;;; Code:
38 (require 'org)
39 (require 'org-clock)
41 (declare-function org-agenda-error "org-agenda" ())
43 (defvar org-timer-start-time nil
44 "t=0 for the running timer.")
46 (defvar org-timer-pause-time nil
47 "Time when the timer was paused.")
49 (defvar org-timer-countdown-timer nil
50 "Current countdown timer.
51 This is a timer object if there is an active countdown timer,
52 'paused' if there is a paused countdown timer, and nil
53 otherwise.")
55 (defvar org-timer-countdown-timer-title nil
56 "Title for notification displayed when a countdown finishes.")
58 (defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
59 "Regular expression used to match timer stamps.")
61 (defcustom org-timer-format "%s "
62 "The format to insert the time of the timer.
63 This format must contain one instance of \"%s\" which will be replaced by
64 the value of the timer."
65 :group 'org-time
66 :type 'string)
68 (defcustom org-timer-default-timer 0
69 "The default timer when a timer is set.
70 When 0, the user is prompted for a value."
71 :group 'org-time
72 :version "24.1"
73 :type 'number)
75 (defcustom org-timer-display 'mode-line
76 "When a timer is running, org-mode can display it in the mode
77 line and/or frame title.
78 Allowed values are:
80 both displays in both mode line and frame title
81 mode-line displays only in mode line (default)
82 frame-title displays only in frame title
83 nil current timer is not displayed"
84 :group 'org-time
85 :type '(choice
86 (const :tag "Mode line" mode-line)
87 (const :tag "Frame title" frame-title)
88 (const :tag "Both" both)
89 (const :tag "None" nil)))
91 (defvar org-timer-start-hook nil
92 "Hook run after relative timer is started.")
94 (defvar org-timer-stop-hook nil
95 "Hook run before relative or countdown timer is stopped.")
97 (defvar org-timer-pause-hook nil
98 "Hook run before relative or countdown timer is paused.")
100 (defvar org-timer-continue-hook nil
101 "Hook run after relative or countdown timer is continued.")
103 (defvar org-timer-set-hook nil
104 "Hook run after countdown timer is set.")
106 (defvar org-timer-done-hook nil
107 "Hook run after countdown timer reaches zero.")
109 ;;;###autoload
110 (defun org-timer-start (&optional offset)
111 "Set the starting time for the relative timer to now.
112 When called with prefix argument OFFSET, prompt the user for an offset time,
113 with the default taken from a timer stamp at point, if any.
114 If OFFSET is a string or an integer, it is directly taken to be the offset
115 without user interaction.
116 When called with a double prefix arg, all timer strings in the active
117 region will be shifted by a specific amount. You will be prompted for
118 the amount, with the default to make the first timer string in
119 the region 0:00:00."
120 (interactive "P")
121 (cond
122 ((equal offset '(16))
123 (call-interactively 'org-timer-change-times-in-region))
124 (org-timer-countdown-timer
125 (user-error "Countdown timer is running. Cancel first"))
127 (let (delta def s)
128 (if (not offset)
129 (setq org-timer-start-time (current-time))
130 (cond
131 ((integerp offset) (setq delta offset))
132 ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
134 (setq def (if (org-in-regexp org-timer-re)
135 (match-string 0)
136 "0:00:00")
137 s (read-string
138 (format "Restart timer with offset [%s]: " def)))
139 (unless (string-match "\\S-" s) (setq s def))
140 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
141 (setq org-timer-start-time
142 (seconds-to-time
143 ;; Pass `current-time' result to `org-float-time'
144 ;; (instead of calling without arguments) so that only
145 ;; `current-time' has to be overriden in tests.
146 (- (org-float-time (current-time)) delta))))
147 (setq org-timer-pause-time nil)
148 (org-timer-set-mode-line 'on)
149 (message "Timer start time set to %s, current value is %s"
150 (format-time-string "%T" org-timer-start-time)
151 (org-timer-secs-to-hms (or delta 0)))
152 (run-hooks 'org-timer-start-hook)))))
154 (defun org-timer-pause-or-continue (&optional stop)
155 "Pause or continue the relative or countdown timer.
156 With prefix arg STOP, stop it entirely."
157 (interactive "P")
158 (cond
159 (stop (org-timer-stop))
160 ((not org-timer-start-time) (error "No timer is running"))
161 (org-timer-pause-time
162 (let ((start-secs (org-float-time org-timer-start-time))
163 (pause-secs (org-float-time org-timer-pause-time)))
164 (if org-timer-countdown-timer
165 (progn
166 (let ((new-secs (- start-secs pause-secs)))
167 (setq org-timer-countdown-timer
168 (org-timer--run-countdown-timer
169 new-secs org-timer-countdown-timer-title))
170 (setq org-timer-start-time
171 (time-add (current-time) (seconds-to-time new-secs)))))
172 (setq org-timer-start-time
173 ;; Pass `current-time' result to `org-float-time'
174 ;; (instead of calling without arguments) so that only
175 ;; `current-time' has to be overriden in tests.
176 (seconds-to-time (- (org-float-time (current-time))
177 (- pause-secs start-secs)))))
178 (setq org-timer-pause-time nil)
179 (org-timer-set-mode-line 'on)
180 (run-hooks 'org-timer-continue-hook)
181 (message "Timer continues at %s" (org-timer-value-string))))
183 ;; pause timer
184 (when org-timer-countdown-timer
185 (cancel-timer org-timer-countdown-timer)
186 (setq org-timer-countdown-timer 'pause))
187 (run-hooks 'org-timer-pause-hook)
188 (setq org-timer-pause-time (current-time))
189 (org-timer-set-mode-line 'pause)
190 (message "Timer paused at %s" (org-timer-value-string)))))
192 (defun org-timer-stop ()
193 "Stop the relative or countdown timer."
194 (interactive)
195 (unless org-timer-start-time
196 (user-error "No timer running"))
197 (when (timerp org-timer-countdown-timer)
198 (cancel-timer org-timer-countdown-timer))
199 (run-hooks 'org-timer-stop-hook)
200 (setq org-timer-start-time nil
201 org-timer-pause-time nil
202 org-timer-countdown-timer nil)
203 (org-timer-set-mode-line 'off)
204 (message "Timer stopped"))
206 ;;;###autoload
207 (defun org-timer (&optional restart no-insert-p)
208 "Insert a H:MM:SS string from the timer into the buffer.
209 The first time this command is used, the timer is started. When used with
210 a \\[universal-argument] prefix, force restarting the timer.
211 When used with a double prefix argument \\[universal-argument], change all the timer string
212 in the region by a fixed amount. This can be used to recalibrate a timer
213 that was not started at the correct moment.
215 If NO-INSERT-P is non-nil, return the string instead of inserting
216 it in the buffer."
217 (interactive "P")
218 (if (equal restart '(16))
219 (org-timer-start restart)
220 (when (or (equal restart '(4)) (not org-timer-start-time))
221 (org-timer-start))
222 (if no-insert-p
223 (org-timer-value-string)
224 (insert (org-timer-value-string)))))
226 (defun org-timer-value-string ()
227 "Set the timer string."
228 (format org-timer-format
229 (org-timer-secs-to-hms
230 (abs (floor (org-timer-seconds))))))
232 (defun org-timer-seconds ()
233 (if org-timer-countdown-timer
234 (- (org-float-time org-timer-start-time)
235 (org-float-time (or org-timer-pause-time (current-time))))
236 (- (org-float-time (or org-timer-pause-time (current-time)))
237 (org-float-time org-timer-start-time))))
239 ;;;###autoload
240 (defun org-timer-change-times-in-region (beg end delta)
241 "Change all h:mm:ss time in region by a DELTA."
242 (interactive
243 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
244 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
245 (unless (string-match "\\S-" delta)
246 (save-excursion
247 (goto-char beg)
248 (when (re-search-forward re end t)
249 (setq delta (match-string 0))
250 (if (equal (string-to-char delta) ?-)
251 (setq delta (substring delta 1))
252 (setq delta (concat "-" delta))))))
253 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
254 (when (= delta 0) (error "No change"))
255 (save-excursion
256 (goto-char end)
257 (while (re-search-backward re beg t)
258 (setq p (point))
259 (replace-match
260 (save-match-data
261 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
262 t t)
263 (goto-char p)))))
265 ;;;###autoload
266 (defun org-timer-item (&optional arg)
267 "Insert a description-type item with the current timer value."
268 (interactive "P")
269 (let ((itemp (org-in-item-p)) (pos (point)))
270 (cond
271 ;; In a timer list, insert with `org-list-insert-item',
272 ;; then fix the list.
273 ((and itemp (goto-char itemp) (org-at-item-timer-p))
274 (let* ((struct (org-list-struct))
275 (prevs (org-list-prevs-alist struct))
276 (s (concat (org-timer (when arg '(4)) t) ":: ")))
277 (setq struct (org-list-insert-item pos struct prevs nil s))
278 (org-list-write-struct struct (org-list-parents-alist struct))
279 (looking-at org-list-full-item-re)
280 (goto-char (match-end 0))))
281 ;; In a list of another type, don't break anything: throw an error.
282 (itemp (goto-char pos) (error "This is not a timer list"))
283 ;; Else, start a new list.
285 (beginning-of-line)
286 (org-indent-line)
287 (insert "- ")
288 (org-timer (when arg '(4)))
289 (insert ":: ")))))
291 (defun org-timer-fix-incomplete (hms)
292 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
293 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
294 (replace-match
295 (format "%d:%02d:%02d"
296 (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
297 (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
298 (string-to-number (match-string 3 hms)))
299 t t hms)
300 (error "Cannot parse HMS string \"%s\"" hms)))
302 (defun org-timer-hms-to-secs (hms)
303 "Convert h:mm:ss string to an integer time.
304 If the string starts with a minus sign, the integer will be negative."
305 (if (not (string-match
306 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
307 hms))
309 (let* ((h (string-to-number (match-string 1 hms)))
310 (m (string-to-number (match-string 2 hms)))
311 (s (string-to-number (match-string 3 hms)))
312 (sign (equal (substring (match-string 1 hms) 0 1) "-")))
313 (setq h (abs h))
314 (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
316 (defun org-timer-secs-to-hms (s)
317 "Convert integer S into h:mm:ss.
318 If the integer is negative, the string will start with \"-\"."
319 (let (sign m h)
320 (setq sign (if (< s 0) "-" "")
321 s (abs s)
322 m (/ s 60) s (- s (* 60 m))
323 h (/ m 60) m (- m (* 60 h)))
324 (format "%s%d:%02d:%02d" sign h m s)))
326 (defvar org-timer-mode-line-timer nil)
327 (defvar org-timer-mode-line-string nil)
329 (defun org-timer-set-mode-line (value)
330 "Set the mode-line display for relative or countdown timer.
331 VALUE can be `on', `off', or `pause'."
332 (when (or (eq org-timer-display 'mode-line)
333 (eq org-timer-display 'both))
334 (or global-mode-string (setq global-mode-string '("")))
335 (or (memq 'org-timer-mode-line-string global-mode-string)
336 (setq global-mode-string
337 (append global-mode-string '(org-timer-mode-line-string)))))
338 (when (or (eq org-timer-display 'frame-title)
339 (eq org-timer-display 'both))
340 (or (memq 'org-timer-mode-line-string frame-title-format)
341 (setq frame-title-format
342 (append frame-title-format '(org-timer-mode-line-string)))))
343 (cond
344 ((equal value 'off)
345 (when org-timer-mode-line-timer
346 (cancel-timer org-timer-mode-line-timer)
347 (setq org-timer-mode-line-timer nil))
348 (when (or (eq org-timer-display 'mode-line)
349 (eq org-timer-display 'both))
350 (setq global-mode-string
351 (delq 'org-timer-mode-line-string global-mode-string)))
352 (when (or (eq org-timer-display 'frame-title)
353 (eq org-timer-display 'both))
354 (setq frame-title-format
355 (delq 'org-timer-mode-line-string frame-title-format)))
356 (force-mode-line-update))
357 ((equal value 'pause)
358 (when org-timer-mode-line-timer
359 (cancel-timer org-timer-mode-line-timer)
360 (setq org-timer-mode-line-timer nil)))
361 ((equal value 'on)
362 (when (or (eq org-timer-display 'mode-line)
363 (eq org-timer-display 'both))
364 (or global-mode-string (setq global-mode-string '("")))
365 (or (memq 'org-timer-mode-line-string global-mode-string)
366 (setq global-mode-string
367 (append global-mode-string '(org-timer-mode-line-string)))))
368 (when (or (eq org-timer-display 'frame-title)
369 (eq org-timer-display 'both))
370 (or (memq 'org-timer-mode-line-string frame-title-format)
371 (setq frame-title-format
372 (append frame-title-format '(org-timer-mode-line-string)))))
373 (org-timer-update-mode-line)
374 (when org-timer-mode-line-timer
375 (cancel-timer org-timer-mode-line-timer)
376 (setq org-timer-mode-line-timer nil))
377 (when org-timer-display
378 (setq org-timer-mode-line-timer
379 (run-with-timer 1 1 'org-timer-update-mode-line))))))
381 (defun org-timer-update-mode-line ()
382 "Update the timer time in the mode line."
383 (if org-timer-pause-time
385 (setq org-timer-mode-line-string
386 (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
387 (force-mode-line-update)))
389 (defun org-timer-show-remaining-time ()
390 "Display the remaining time before the timer ends."
391 (interactive)
392 (require 'time)
393 (if (not org-timer-countdown-timer)
394 (message "No timer set")
395 (let* ((rtime (decode-time
396 (time-subtract (timer--time org-timer-countdown-timer)
397 (current-time))))
398 (rsecs (nth 0 rtime))
399 (rmins (nth 1 rtime)))
400 (message "%d minute(s) %d seconds left before next time out"
401 rmins rsecs))))
403 ;;;###autoload
404 (defun org-timer-set-timer (&optional opt)
405 "Prompt for a duration and set a timer.
407 If `org-timer-default-timer' is not zero, suggest this value as
408 the default duration for the timer. If a timer is already set,
409 prompt the user if she wants to replace it.
411 Called with a numeric prefix argument, use this numeric value as
412 the duration of the timer.
414 Called with a `C-u' prefix arguments, use `org-timer-default-timer'
415 without prompting the user for a duration.
417 With two `C-u' prefix arguments, use `org-timer-default-timer'
418 without prompting the user for a duration and automatically
419 replace any running timer.
421 By default, the timer duration will be set to the number of
422 minutes in the Effort property, if any. You can ignore this by
423 using three `C-u' prefix arguments."
424 (interactive "P")
425 (when (and org-timer-start-time
426 (not org-timer-countdown-timer))
427 (user-error "Relative timer is running. Stop first"))
428 (let* ((effort-minutes (ignore-errors (org-get-at-eol 'effort-minutes 1)))
429 (minutes (or (and (not (equal opt '(64)))
430 effort-minutes
431 (number-to-string effort-minutes))
432 (and (numberp opt) (number-to-string opt))
433 (and (listp opt) (not (null opt))
434 (number-to-string org-timer-default-timer))
435 (read-from-minibuffer
436 "How many minutes left? "
437 (if (not (eq org-timer-default-timer 0))
438 (number-to-string org-timer-default-timer))))))
439 (if (not (string-match "[0-9]+" minutes))
440 (org-timer-show-remaining-time)
441 (let* ((mins (string-to-number (match-string 0 minutes)))
442 (secs (* mins 60))
443 (hl (org-timer--get-timer-title)))
444 (if (or (not org-timer-countdown-timer)
445 (equal opt '(16))
446 (y-or-n-p "Replace current timer? "))
447 (progn
448 (when (timerp org-timer-countdown-timer)
449 (cancel-timer org-timer-countdown-timer))
450 (setq org-timer-countdown-timer-title
451 (org-timer--get-timer-title))
452 (setq org-timer-countdown-timer
453 (org-timer--run-countdown-timer
454 secs org-timer-countdown-timer-title))
455 (run-hooks 'org-timer-set-hook)
456 (setq org-timer-start-time
457 (time-add (current-time) (seconds-to-time secs)))
458 (setq org-timer-pause-time nil)
459 (org-timer-set-mode-line 'on))
460 (message "No timer set"))))))
462 (defun org-timer--run-countdown-timer (secs title)
463 "Start countdown timer that will last SECS.
464 TITLE will be appended to the notification message displayed when
465 time is up."
466 (let ((msg (format "%s: time out" title)))
467 (run-with-timer
468 secs nil `(lambda ()
469 (setq org-timer-countdown-timer nil
470 org-timer-start-time nil)
471 (org-notify ,msg ,org-clock-sound)
472 (org-timer-set-mode-line 'off)
473 (run-hooks 'org-timer-done-hook)))))
475 (defun org-timer--get-timer-title ()
476 "Construct timer title from heading or file name of Org buffer."
477 (cond
478 ((derived-mode-p 'org-agenda-mode)
479 (let* ((marker (or (get-text-property (point) 'org-marker)
480 (org-agenda-error)))
481 (hdmarker (or (get-text-property (point) 'org-hd-marker)
482 marker)))
483 (with-current-buffer (marker-buffer marker)
484 (org-with-wide-buffer
485 (goto-char hdmarker)
486 (org-show-entry)
487 (or (ignore-errors (org-get-heading))
488 (buffer-name (buffer-base-buffer)))))))
489 ((derived-mode-p 'org-mode)
490 (or (ignore-errors (org-get-heading))
491 (buffer-name (buffer-base-buffer))))
492 (t (error "Not in an Org buffer"))))
494 (provide 'org-timer)
496 ;; Local variables:
497 ;; generated-autoload-file: "org-loaddefs.el"
498 ;; End:
500 ;;; org-timer.el ends here