1 ;;; org-timer.el --- The relative timer code for Org-mode
3 ;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the relative timer code for Org-mode
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."
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."
58 (defvar org-timer-start-hook nil
59 "Hook run after relative timer is started.")
61 (defvar org-timer-stop-hook nil
62 "Hook run before relative timer is stopped.")
64 (defvar org-timer-pause-hook nil
65 "Hook run before relative timer is paused.")
67 (defvar org-timer-continue-hook nil
68 "Hook run after relative timer is continued.")
70 (defvar org-timer-set-hook nil
71 "Hook run after countdown timer is set.")
73 (defvar org-timer-done-hook nil
74 "Hook run after countdown timer reaches zero.")
76 (defvar org-timer-cancel-hook nil
77 "Hook run before countdown timer is canceled.")
80 (defun org-timer-start (&optional offset
)
81 "Set the starting time for the relative timer to now.
82 When called with prefix argument OFFSET, prompt the user for an offset time,
83 with the default taken from a timer stamp at point, if any.
84 If OFFSET is a string or an integer, it is directly taken to be the offset
85 without user interaction.
86 When called with a double prefix arg, all timer strings in the active
87 region will be shifted by a specific amount. You will be prompted for
88 the amount, with the default to make the first timer string in
91 (if (equal offset
'(16))
92 (call-interactively 'org-timer-change-times-in-region
)
95 (setq org-timer-start-time
(current-time))
97 ((integerp offset
) (setq delta offset
))
98 ((stringp offset
) (setq delta
(org-timer-hms-to-secs offset
)))
100 (setq def
(if (org-in-regexp org-timer-re
)
104 (format "Restart timer with offset [%s]: " def
)))
105 (unless (string-match "\\S-" s
) (setq s def
))
106 (setq delta
(org-timer-hms-to-secs (org-timer-fix-incomplete s
)))))
107 (setq org-timer-start-time
109 (- (org-float-time) delta
))))
110 (org-timer-set-mode-line 'on
)
111 (message "Timer start time set to %s, current value is %s"
112 (format-time-string "%T" org-timer-start-time
)
113 (org-timer-secs-to-hms (or delta
0)))
114 (run-hooks 'org-timer-start-hook
))))
116 (defun org-timer-pause-or-continue (&optional stop
)
117 "Pause or continue the relative timer.
118 With prefix arg STOP, stop it entirely."
121 (stop (org-timer-stop))
122 ((not org-timer-start-time
) (error "No timer is running"))
123 (org-timer-pause-time
124 ;; timer is paused, continue
125 (setq org-timer-start-time
129 (- (org-float-time org-timer-pause-time
)
130 (org-float-time org-timer-start-time
))))
131 org-timer-pause-time nil
)
132 (org-timer-set-mode-line 'on
)
133 (run-hooks 'org-timer-continue-hook
)
134 (message "Timer continues at %s" (org-timer-value-string)))
137 (run-hooks 'org-timer-pause-hook
)
138 (setq org-timer-pause-time
(current-time))
139 (org-timer-set-mode-line 'pause
)
140 (message "Timer paused at %s" (org-timer-value-string)))))
142 (defun org-timer-stop ()
143 "Stop the relative timer."
145 (run-hooks 'org-timer-stop-hook
)
146 (setq org-timer-start-time nil
147 org-timer-pause-time nil
)
148 (org-timer-set-mode-line 'off
))
151 (defun org-timer (&optional restart no-insert-p
)
152 "Insert a H:MM:SS string from the timer into the buffer.
153 The first time this command is used, the timer is started. When used with
154 a \\[universal-argument] prefix, force restarting the timer.
155 When used with a double prefix argument \\[universal-argument], change all the timer string
156 in the region by a fixed amount. This can be used to recalibrate a timer
157 that was not started at the correct moment.
159 If NO-INSERT-P is non-nil, return the string instead of inserting
162 (when (or (equal restart
'(4)) (not org-timer-start-time
))
165 (org-timer-value-string)
166 (insert (org-timer-value-string))))
168 (defun org-timer-value-string ()
169 (format org-timer-format
(org-timer-secs-to-hms (floor (org-timer-seconds)))))
171 (defvar org-timer-timer-is-countdown nil
)
172 (defun org-timer-seconds ()
173 (if org-timer-timer-is-countdown
174 (- (org-float-time org-timer-start-time
)
175 (org-float-time (current-time)))
176 (- (org-float-time (or org-timer-pause-time
(current-time)))
177 (org-float-time org-timer-start-time
))))
180 (defun org-timer-change-times-in-region (beg end delta
)
181 "Change all h:mm:ss time in region by a DELTA."
183 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
184 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p
)
185 (unless (string-match "\\S-" delta
)
188 (when (re-search-forward re end t
)
189 (setq delta
(match-string 0))
190 (if (equal (string-to-char delta
) ?-
)
191 (setq delta
(substring delta
1))
192 (setq delta
(concat "-" delta
))))))
193 (setq delta
(org-timer-hms-to-secs (org-timer-fix-incomplete delta
)))
194 (when (= delta
0) (error "No change"))
197 (while (re-search-backward re beg t
)
201 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta
)))
206 (defun org-timer-item (&optional arg
)
207 "Insert a description-type item with the current timer value."
209 (let ((itemp (org-in-item-p)) (pos (point)))
211 ;; In a timer list, insert with `org-list-insert-item',
212 ;; then fix the list.
213 ((and itemp
(goto-char itemp
) (org-at-item-timer-p))
214 (let* ((struct (org-list-struct))
215 (prevs (org-list-prevs-alist struct
))
216 (s (concat (org-timer (when arg
'(4)) t
) ":: ")))
217 (setq struct
(org-list-insert-item pos struct prevs nil s
))
218 (org-list-write-struct struct
(org-list-parents-alist struct
))
219 (looking-at org-list-full-item-re
)
220 (goto-char (match-end 0))))
221 ;; In a list of another type, don't break anything: throw an error.
222 (itemp (goto-char pos
) (error "This is not a timer list"))
223 ;; Else, start a new list.
226 (org-indent-line-function)
228 (org-timer (when arg
'(4)))
231 (defun org-timer-fix-incomplete (hms)
232 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
233 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms
)
235 (format "%d:%02d:%02d"
236 (if (match-end 1) (string-to-number (match-string 1 hms
)) 0)
237 (if (match-end 2) (string-to-number (match-string 2 hms
)) 0)
238 (string-to-number (match-string 3 hms
)))
240 (error "Cannot parse HMS string \"%s\"" hms
)))
242 (defun org-timer-hms-to-secs (hms)
243 "Convert h:mm:ss string to an integer time.
244 If the string starts with a minus sign, the integer will be negative."
245 (if (not (string-match
246 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
249 (let* ((h (string-to-number (match-string 1 hms
)))
250 (m (string-to-number (match-string 2 hms
)))
251 (s (string-to-number (match-string 3 hms
)))
252 (sign (equal (substring (match-string 1 hms
) 0 1) "-")))
254 (* (if sign -
1 1) (+ s
(* 60 (+ m
(* 60 h
))))))))
256 (defun org-timer-secs-to-hms (s)
257 "Convert integer S into h:mm:ss.
258 If the integer is negative, the string will start with \"-\"."
260 (setq sign
(if (< s
0) "-" "")
262 m
(/ s
60) s
(- s
(* 60 m
))
263 h
(/ m
60) m
(- m
(* 60 h
)))
264 (format "%s%d:%02d:%02d" sign h m s
)))
266 (defvar org-timer-mode-line-timer nil
)
267 (defvar org-timer-mode-line-string nil
)
269 (defun org-timer-set-mode-line (value)
270 "Set the mode-line display of the relative timer.
271 VALUE can be `on', `off', or `pause'."
272 (or global-mode-string
(setq global-mode-string
'("")))
273 (or (memq 'org-timer-mode-line-string global-mode-string
)
274 (setq global-mode-string
275 (append global-mode-string
'(org-timer-mode-line-string))))
278 (when org-timer-mode-line-timer
279 (cancel-timer org-timer-mode-line-timer
)
280 (setq org-timer-mode-line-timer nil
))
281 (setq global-mode-string
282 (delq 'org-timer-mode-line-string global-mode-string
))
283 (force-mode-line-update))
284 ((equal value
'pause
)
285 (when org-timer-mode-line-timer
286 (cancel-timer org-timer-mode-line-timer
)
287 (setq org-timer-mode-line-timer nil
)))
289 (or global-mode-string
(setq global-mode-string
'("")))
290 (or (memq 'org-timer-mode-line-string global-mode-string
)
291 (setq global-mode-string
292 (append global-mode-string
'(org-timer-mode-line-string))))
293 (org-timer-update-mode-line)
294 (when org-timer-mode-line-timer
295 (cancel-timer org-timer-mode-line-timer
))
296 (setq org-timer-mode-line-timer
297 (run-with-timer 1 1 'org-timer-update-mode-line
)))))
299 (defun org-timer-update-mode-line ()
300 "Update the timer time in the mode line."
301 (if org-timer-pause-time
303 (setq org-timer-mode-line-string
304 (concat " <" (substring (org-timer-value-string) 0 -
1) ">"))
305 (force-mode-line-update)))
307 (defvar org-timer-current-timer nil
)
308 (defun org-timer-cancel-timer ()
309 "Cancel the current timer."
311 (when (eval org-timer-current-timer
)
312 (run-hooks 'org-timer-cancel-hook
)
313 (cancel-timer org-timer-current-timer
)
314 (setq org-timer-current-timer nil
)
315 (setq org-timer-timer-is-countdown nil
)
316 (org-timer-set-mode-line 'off
))
317 (message "Last timer canceled"))
319 (defun org-timer-show-remaining-time ()
320 "Display the remaining time before the timer ends."
323 (if (not org-timer-current-timer
)
324 (message "No timer set")
325 (let* ((rtime (decode-time
326 (time-subtract (timer--time org-timer-current-timer
)
328 (rsecs (nth 0 rtime
))
329 (rmins (nth 1 rtime
)))
330 (message "%d minute(s) %d seconds left before next time out"
334 (defun org-timer-set-timer (&optional opt
)
335 "Prompt for a duration and set a timer.
337 If `org-timer-default-timer' is not zero, suggest this value as
338 the default duration for the timer. If a timer is already set,
339 prompt the user if she wants to replace it.
341 Called with a numeric prefix argument, use this numeric value as
342 the duration of the timer.
344 Called with a `C-u' prefix arguments, use `org-timer-default-timer'
345 without prompting the user for a duration.
347 With two `C-u' prefix arguments, use `org-timer-default-timer'
348 without prompting the user for a duration and automatically
349 replace any running timer."
351 (let ((minutes (or (and (numberp opt
) (number-to-string opt
))
352 (and (listp opt
) (not (null opt
))
353 (number-to-string org-timer-default-timer
))
354 (read-from-minibuffer
355 "How many minutes left? "
356 (if (not (eq org-timer-default-timer
0))
357 (number-to-string org-timer-default-timer
))))))
358 (if (not (string-match "[0-9]+" minutes
))
359 (org-timer-show-remaining-time)
360 (let* ((mins (string-to-number (match-string 0 minutes
)))
363 ((string-match "Org Agenda" (buffer-name))
364 (let* ((marker (or (get-text-property (point) 'org-marker
)
366 (hdmarker (or (get-text-property (point) 'org-hd-marker
)
368 (pos (marker-position marker
)))
369 (with-current-buffer (marker-buffer marker
)
373 (or (ignore-errors (org-get-heading))
374 (concat "File:" (file-name-nondirectory (buffer-file-name)))))))
375 ((eq major-mode
'org-mode
)
376 (or (ignore-errors (org-get-heading))
377 (concat "File:" (file-name-nondirectory (buffer-file-name)))))
378 (t (error "Not in an Org buffer"))))
380 (if (or (and org-timer-current-timer
381 (or (equal opt
'(16))
382 (y-or-n-p "Replace current timer? ")))
383 (not org-timer-current-timer
))
386 (when org-timer-current-timer
387 (cancel-timer org-timer-current-timer
))
388 (setq org-timer-current-timer
391 (setq org-timer-current-timer nil
)
392 (org-notify ,(format "%s: time out" hl
) t
)
393 (setq org-timer-timer-is-countdown nil
)
394 (org-timer-set-mode-line 'off
)
395 (run-hooks 'org-timer-done-hook
))))
396 (run-hooks 'org-timer-set-hook
)
397 (setq org-timer-timer-is-countdown t
399 (time-add (current-time) (seconds-to-time (* mins
60))))
400 (org-timer-set-mode-line 'on
))
401 (message "No timer set"))))))
405 ;;; org-timer.el ends here