org-element: Fix docstring
[org-mode/org-tableheadings.git] / lisp / org-timer.el
blobf44b1cf62c9eddb446d4c2c48618995596b82e82
1 ;;; org-timer.el --- The relative 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 contains the relative timer code for Org-mode
29 ;;; Code:
31 (require 'org)
33 (declare-function org-notify "org-clock" (notification &optional play-sound))
34 (declare-function org-agenda-error "org-agenda" ())
36 (defvar org-timer-start-time nil
37 "t=0 for the running timer.")
39 (defvar org-timer-pause-time nil
40 "Time when the timer was paused.")
42 (defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
43 "Regular expression used to match timer stamps.")
45 (defcustom org-timer-format "%s "
46 "The format to insert the time of the timer.
47 This format must contain one instance of \"%s\" which will be replaced by
48 the value of the relative timer."
49 :group 'org-time
50 :type 'string)
52 (defcustom org-timer-default-timer 0
53 "The default timer when a timer is set.
54 When 0, the user is prompted for a value."
55 :group 'org-time
56 :version "24.1"
57 :type 'number)
59 (defcustom org-timer-display 'mode-line
60 "When a timer is running, org-mode can display it in the mode
61 line and/or frame title.
62 Allowed values are:
64 both displays in both mode line and frame title
65 mode-line displays only in mode line (default)
66 frame-title displays only in frame title
67 nil current timer is not displayed"
68 :group 'org-time
69 :type '(choice
70 (const :tag "Mode line" mode-line)
71 (const :tag "Frame title" frame-title)
72 (const :tag "Both" both)
73 (const :tag "None" nil)))
75 (defvar org-timer-start-hook nil
76 "Hook run after relative timer is started.")
78 (defvar org-timer-stop-hook nil
79 "Hook run before relative timer is stopped.")
81 (defvar org-timer-pause-hook nil
82 "Hook run before relative timer is paused.")
84 (defvar org-timer-continue-hook nil
85 "Hook run after relative timer is continued.")
87 (defvar org-timer-set-hook nil
88 "Hook run after countdown timer is set.")
90 (defvar org-timer-done-hook nil
91 "Hook run after countdown timer reaches zero.")
93 (defvar org-timer-cancel-hook nil
94 "Hook run before countdown timer is canceled.")
96 ;;;###autoload
97 (defun org-timer-start (&optional offset)
98 "Set the starting time for the relative timer to now.
99 When called with prefix argument OFFSET, prompt the user for an offset time,
100 with the default taken from a timer stamp at point, if any.
101 If OFFSET is a string or an integer, it is directly taken to be the offset
102 without user interaction.
103 When called with a double prefix arg, all timer strings in the active
104 region will be shifted by a specific amount. You will be prompted for
105 the amount, with the default to make the first timer string in
106 the region 0:00:00."
107 (interactive "P")
108 (if (equal offset '(16))
109 (call-interactively 'org-timer-change-times-in-region)
110 (let (delta def s)
111 (if (not offset)
112 (setq org-timer-start-time (current-time))
113 (cond
114 ((integerp offset) (setq delta offset))
115 ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
117 (setq def (if (org-in-regexp org-timer-re)
118 (match-string 0)
119 "0:00:00")
120 s (read-string
121 (format "Restart timer with offset [%s]: " def)))
122 (unless (string-match "\\S-" s) (setq s def))
123 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
124 (setq org-timer-start-time
125 (seconds-to-time
126 (- (org-float-time) delta))))
127 (org-timer-set-mode-line 'on)
128 (message "Timer start time set to %s, current value is %s"
129 (format-time-string "%T" org-timer-start-time)
130 (org-timer-secs-to-hms (or delta 0)))
131 (run-hooks 'org-timer-start-hook))))
133 (defun org-timer-pause-or-continue (&optional stop)
134 "Pause or continue the relative timer.
135 With prefix arg STOP, stop it entirely."
136 (interactive "P")
137 (cond
138 (stop (org-timer-stop))
139 ((not org-timer-start-time) (error "No timer is running"))
140 (org-timer-pause-time
141 ;; timer is paused, continue
142 (setq org-timer-start-time
143 (seconds-to-time
145 (org-float-time)
146 (- (org-float-time org-timer-pause-time)
147 (org-float-time org-timer-start-time))))
148 org-timer-pause-time nil)
149 (org-timer-set-mode-line 'on)
150 (run-hooks 'org-timer-continue-hook)
151 (message "Timer continues at %s" (org-timer-value-string)))
153 ;; pause timer
154 (run-hooks 'org-timer-pause-hook)
155 (setq org-timer-pause-time (current-time))
156 (org-timer-set-mode-line 'pause)
157 (message "Timer paused at %s" (org-timer-value-string)))))
159 (defvar org-timer-current-timer nil)
160 (defun org-timer-stop ()
161 "Stop the relative timer."
162 (interactive)
163 (if (not org-timer-current-timer)
164 (message "No running timer")
165 (run-hooks 'org-timer-stop-hook)
166 (setq org-timer-start-time nil
167 org-timer-pause-time nil
168 org-timer-current-timer nil)
169 (org-timer-set-mode-line 'off)
170 (message "Timer stopped")))
172 ;;;###autoload
173 (defun org-timer (&optional restart no-insert-p)
174 "Insert a H:MM:SS string from the timer into the buffer.
175 The first time this command is used, the timer is started. When used with
176 a \\[universal-argument] prefix, force restarting the timer.
177 When used with a double prefix argument \\[universal-argument], change all the timer string
178 in the region by a fixed amount. This can be used to recalibrate a timer
179 that was not started at the correct moment.
181 If NO-INSERT-P is non-nil, return the string instead of inserting
182 it in the buffer."
183 (interactive "P")
184 (when (or (equal restart '(4)) (not org-timer-start-time))
185 (org-timer-start))
186 (if no-insert-p
187 (org-timer-value-string)
188 (insert (org-timer-value-string))))
190 (defun org-timer-value-string ()
191 "Set the timer string."
192 (format org-timer-format
193 (org-timer-secs-to-hms
194 (abs (floor (org-timer-seconds))))))
196 (defvar org-timer-timer-is-countdown nil)
197 (defun org-timer-seconds ()
198 (if org-timer-timer-is-countdown
199 (- (org-float-time org-timer-start-time)
200 (org-float-time (current-time)))
201 (- (org-float-time (or org-timer-pause-time (current-time)))
202 (org-float-time org-timer-start-time))))
204 ;;;###autoload
205 (defun org-timer-change-times-in-region (beg end delta)
206 "Change all h:mm:ss time in region by a DELTA."
207 (interactive
208 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
209 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
210 (unless (string-match "\\S-" delta)
211 (save-excursion
212 (goto-char beg)
213 (when (re-search-forward re end t)
214 (setq delta (match-string 0))
215 (if (equal (string-to-char delta) ?-)
216 (setq delta (substring delta 1))
217 (setq delta (concat "-" delta))))))
218 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
219 (when (= delta 0) (error "No change"))
220 (save-excursion
221 (goto-char end)
222 (while (re-search-backward re beg t)
223 (setq p (point))
224 (replace-match
225 (save-match-data
226 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
227 t t)
228 (goto-char p)))))
230 ;;;###autoload
231 (defun org-timer-item (&optional arg)
232 "Insert a description-type item with the current timer value."
233 (interactive "P")
234 (let ((itemp (org-in-item-p)) (pos (point)))
235 (cond
236 ;; In a timer list, insert with `org-list-insert-item',
237 ;; then fix the list.
238 ((and itemp (goto-char itemp) (org-at-item-timer-p))
239 (let* ((struct (org-list-struct))
240 (prevs (org-list-prevs-alist struct))
241 (s (concat (org-timer (when arg '(4)) t) ":: ")))
242 (setq struct (org-list-insert-item pos struct prevs nil s))
243 (org-list-write-struct struct (org-list-parents-alist struct))
244 (looking-at org-list-full-item-re)
245 (goto-char (match-end 0))))
246 ;; In a list of another type, don't break anything: throw an error.
247 (itemp (goto-char pos) (error "This is not a timer list"))
248 ;; Else, start a new list.
250 (beginning-of-line)
251 (org-indent-line)
252 (insert "- ")
253 (org-timer (when arg '(4)))
254 (insert ":: ")))))
256 (defun org-timer-fix-incomplete (hms)
257 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
258 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
259 (replace-match
260 (format "%d:%02d:%02d"
261 (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
262 (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
263 (string-to-number (match-string 3 hms)))
264 t t hms)
265 (error "Cannot parse HMS string \"%s\"" hms)))
267 (defun org-timer-hms-to-secs (hms)
268 "Convert h:mm:ss string to an integer time.
269 If the string starts with a minus sign, the integer will be negative."
270 (if (not (string-match
271 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
272 hms))
274 (let* ((h (string-to-number (match-string 1 hms)))
275 (m (string-to-number (match-string 2 hms)))
276 (s (string-to-number (match-string 3 hms)))
277 (sign (equal (substring (match-string 1 hms) 0 1) "-")))
278 (setq h (abs h))
279 (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
281 (defun org-timer-secs-to-hms (s)
282 "Convert integer S into h:mm:ss.
283 If the integer is negative, the string will start with \"-\"."
284 (let (sign m h)
285 (setq sign (if (< s 0) "-" "")
286 s (abs s)
287 m (/ s 60) s (- s (* 60 m))
288 h (/ m 60) m (- m (* 60 h)))
289 (format "%s%d:%02d:%02d" sign h m s)))
291 (defvar org-timer-mode-line-timer nil)
292 (defvar org-timer-mode-line-string nil)
294 (defun org-timer-set-mode-line (value)
295 "Set the mode-line display of the relative timer.
296 VALUE can be `on', `off', or `pause'."
297 (when (or (eq org-timer-display 'mode-line)
298 (eq org-timer-display 'both))
299 (or global-mode-string (setq global-mode-string '("")))
300 (or (memq 'org-timer-mode-line-string global-mode-string)
301 (setq global-mode-string
302 (append global-mode-string '(org-timer-mode-line-string)))))
303 (when (or (eq org-timer-display 'frame-title)
304 (eq org-timer-display 'both))
305 (or (memq 'org-timer-mode-line-string frame-title-format)
306 (setq frame-title-format
307 (append frame-title-format '(org-timer-mode-line-string)))))
308 (cond
309 ((equal value 'off)
310 (when org-timer-mode-line-timer
311 (cancel-timer org-timer-mode-line-timer)
312 (setq org-timer-mode-line-timer nil))
313 (when (or (eq org-timer-display 'mode-line)
314 (eq org-timer-display 'both))
315 (setq global-mode-string
316 (delq 'org-timer-mode-line-string global-mode-string)))
317 (when (or (eq org-timer-display 'frame-title)
318 (eq org-timer-display 'both))
319 (setq frame-title-format
320 (delq 'org-timer-mode-line-string frame-title-format)))
321 (force-mode-line-update))
322 ((equal value 'pause)
323 (when org-timer-mode-line-timer
324 (cancel-timer org-timer-mode-line-timer)
325 (setq org-timer-mode-line-timer nil)))
326 ((equal value 'on)
327 (when (or (eq org-timer-display 'mode-line)
328 (eq org-timer-display 'both))
329 (or global-mode-string (setq global-mode-string '("")))
330 (or (memq 'org-timer-mode-line-string global-mode-string)
331 (setq global-mode-string
332 (append global-mode-string '(org-timer-mode-line-string)))))
333 (when (or (eq org-timer-display 'frame-title)
334 (eq org-timer-display 'both))
335 (or (memq 'org-timer-mode-line-string frame-title-format)
336 (setq frame-title-format
337 (append frame-title-format '(org-timer-mode-line-string)))))
338 (org-timer-update-mode-line)
339 (when org-timer-mode-line-timer
340 (cancel-timer org-timer-mode-line-timer)
341 (setq org-timer-mode-line-timer nil))
342 (when org-timer-display
343 (setq org-timer-mode-line-timer
344 (run-with-timer 1 1 'org-timer-update-mode-line))))))
346 (defun org-timer-update-mode-line ()
347 "Update the timer time in the mode line."
348 (if org-timer-pause-time
350 (setq org-timer-mode-line-string
351 (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
352 (force-mode-line-update)))
354 (defun org-timer-cancel-timer ()
355 "Cancel the current timer."
356 (interactive)
357 (if (not org-timer-current-timer)
358 (message "No timer to cancel")
359 (run-hooks 'org-timer-cancel-hook)
360 (cancel-timer org-timer-current-timer)
361 (setq org-timer-current-timer nil
362 org-timer-timer-is-countdown nil)
363 (org-timer-set-mode-line 'off)
364 (message "Last timer canceled")))
366 (defun org-timer-show-remaining-time ()
367 "Display the remaining time before the timer ends."
368 (interactive)
369 (require 'time)
370 (if (not org-timer-current-timer)
371 (message "No timer set")
372 (let* ((rtime (decode-time
373 (time-subtract (timer--time org-timer-current-timer)
374 (current-time))))
375 (rsecs (nth 0 rtime))
376 (rmins (nth 1 rtime)))
377 (message "%d minute(s) %d seconds left before next time out"
378 rmins rsecs))))
380 (defvar org-clock-sound)
382 ;;;###autoload
383 (defun org-timer-set-timer (&optional opt)
384 "Prompt for a duration and set a timer.
386 If `org-timer-default-timer' is not zero, suggest this value as
387 the default duration for the timer. If a timer is already set,
388 prompt the user if she wants to replace it.
390 Called with a numeric prefix argument, use this numeric value as
391 the duration of the timer.
393 Called with a `C-u' prefix arguments, use `org-timer-default-timer'
394 without prompting the user for a duration.
396 With two `C-u' prefix arguments, use `org-timer-default-timer'
397 without prompting the user for a duration and automatically
398 replace any running timer.
400 By default, the timer duration will be set to the number of
401 minutes in the Effort property, if any. You can ignore this by
402 using three `C-u' prefix arguments."
403 (interactive "P")
404 (let* ((effort-minutes (org-get-at-eol 'effort-minutes 1))
405 (minutes (or (and (not (equal opt '(64)))
406 effort-minutes
407 (number-to-string effort-minutes))
408 (and (numberp opt) (number-to-string opt))
409 (and (listp opt) (not (null opt))
410 (number-to-string org-timer-default-timer))
411 (read-from-minibuffer
412 "How many minutes left? "
413 (if (not (eq org-timer-default-timer 0))
414 (number-to-string org-timer-default-timer))))))
415 (if (not (string-match "[0-9]+" minutes))
416 (org-timer-show-remaining-time)
417 (let* ((mins (string-to-number (match-string 0 minutes)))
418 (secs (* mins 60))
419 (hl (cond
420 ((string-match "Org Agenda" (buffer-name))
421 (let* ((marker (or (get-text-property (point) 'org-marker)
422 (org-agenda-error)))
423 (hdmarker (or (get-text-property (point) 'org-hd-marker)
424 marker))
425 (pos (marker-position marker)))
426 (with-current-buffer (marker-buffer marker)
427 (widen)
428 (goto-char pos)
429 (org-show-entry)
430 (or (ignore-errors (org-get-heading))
431 (concat "File:" (file-name-nondirectory (buffer-file-name)))))))
432 ((derived-mode-p 'org-mode)
433 (or (ignore-errors (org-get-heading))
434 (concat "File:" (file-name-nondirectory (buffer-file-name)))))
435 (t (error "Not in an Org buffer"))))
436 timer-set)
437 (if (or (and org-timer-current-timer
438 (or (equal opt '(16))
439 (y-or-n-p "Replace current timer? ")))
440 (not org-timer-current-timer))
441 (progn
442 (require 'org-clock)
443 (when org-timer-current-timer
444 (cancel-timer org-timer-current-timer))
445 (setq org-timer-current-timer
446 (run-with-timer
447 secs nil `(lambda ()
448 (setq org-timer-current-timer nil)
449 (org-notify ,(format "%s: time out" hl) ,org-clock-sound)
450 (setq org-timer-timer-is-countdown nil)
451 (org-timer-set-mode-line 'off)
452 (run-hooks 'org-timer-done-hook))))
453 (run-hooks 'org-timer-set-hook)
454 (setq org-timer-timer-is-countdown t
455 org-timer-start-time
456 (time-add (current-time) (seconds-to-time (* mins 60))))
457 (org-timer-set-mode-line 'on))
458 (message "No timer set"))))))
460 (provide 'org-timer)
462 ;; Local variables:
463 ;; generated-autoload-file: "org-loaddefs.el"
464 ;; End:
466 ;;; org-timer.el ends here