1 ;;; org-timer.el --- The relative timer code for Org-mode
3 ;; Copyright (C) 2008-2012 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."
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.
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"
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.")
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
108 (if (equal offset
'(16))
109 (call-interactively 'org-timer-change-times-in-region
)
112 (setq org-timer-start-time
(current-time))
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
)
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
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
))))
134 (defun org-timer-pause-or-continue (&optional stop
)
135 "Pause or continue the relative timer.
136 With prefix arg STOP, stop it entirely."
139 (stop (org-timer-stop))
140 ((not org-timer-start-time
) (error "No timer is running"))
141 (org-timer-pause-time
142 ;; timer is paused, continue
143 (setq org-timer-start-time
147 (- (org-float-time org-timer-pause-time
)
148 (org-float-time org-timer-start-time
))))
149 org-timer-pause-time nil
)
150 (org-timer-set-mode-line 'on
)
151 (run-hooks 'org-timer-continue-hook
)
152 (message "Timer continues at %s" (org-timer-value-string)))
155 (run-hooks 'org-timer-pause-hook
)
156 (setq org-timer-pause-time
(current-time))
157 (org-timer-set-mode-line 'pause
)
158 (message "Timer paused at %s" (org-timer-value-string)))))
161 (defun org-timer-stop ()
162 "Stop the relative timer."
164 (run-hooks 'org-timer-stop-hook
)
165 (setq org-timer-start-time nil
166 org-timer-pause-time nil
)
167 (org-timer-set-mode-line 'off
))
170 (defun org-timer (&optional restart no-insert-p
)
171 "Insert a H:MM:SS string from the timer into the buffer.
172 The first time this command is used, the timer is started. When used with
173 a \\[universal-argument] prefix, force restarting the timer.
174 When used with a double prefix argument \\[universal-argument], change all the timer string
175 in the region by a fixed amount. This can be used to recalibrate a timer
176 that was not started at the correct moment.
178 If NO-INSERT-P is non-nil, return the string instead of inserting
181 (when (or (equal restart
'(4)) (not org-timer-start-time
))
184 (org-timer-value-string)
185 (insert (org-timer-value-string))))
187 (defun org-timer-value-string ()
188 (format org-timer-format
(org-timer-secs-to-hms (floor (org-timer-seconds)))))
190 (defvar org-timer-timer-is-countdown nil
)
191 (defun org-timer-seconds ()
192 (if org-timer-timer-is-countdown
193 (- (org-float-time org-timer-start-time
)
194 (org-float-time (current-time)))
195 (- (org-float-time (or org-timer-pause-time
(current-time)))
196 (org-float-time org-timer-start-time
))))
199 (defun org-timer-change-times-in-region (beg end delta
)
200 "Change all h:mm:ss time in region by a DELTA."
202 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
203 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p
)
204 (unless (string-match "\\S-" delta
)
207 (when (re-search-forward re end t
)
208 (setq delta
(match-string 0))
209 (if (equal (string-to-char delta
) ?-
)
210 (setq delta
(substring delta
1))
211 (setq delta
(concat "-" delta
))))))
212 (setq delta
(org-timer-hms-to-secs (org-timer-fix-incomplete delta
)))
213 (when (= delta
0) (error "No change"))
216 (while (re-search-backward re beg t
)
220 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta
)))
225 (defun org-timer-item (&optional arg
)
226 "Insert a description-type item with the current timer value."
228 (let ((itemp (org-in-item-p)) (pos (point)))
230 ;; In a timer list, insert with `org-list-insert-item',
231 ;; then fix the list.
232 ((and itemp
(goto-char itemp
) (org-at-item-timer-p))
233 (let* ((struct (org-list-struct))
234 (prevs (org-list-prevs-alist struct
))
235 (s (concat (org-timer (when arg
'(4)) t
) ":: ")))
236 (setq struct
(org-list-insert-item pos struct prevs nil s
))
237 (org-list-write-struct struct
(org-list-parents-alist struct
))
238 (looking-at org-list-full-item-re
)
239 (goto-char (match-end 0))))
240 ;; In a list of another type, don't break anything: throw an error.
241 (itemp (goto-char pos
) (error "This is not a timer list"))
242 ;; Else, start a new list.
247 (org-timer (when arg
'(4)))
250 (defun org-timer-fix-incomplete (hms)
251 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
252 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms
)
254 (format "%d:%02d:%02d"
255 (if (match-end 1) (string-to-number (match-string 1 hms
)) 0)
256 (if (match-end 2) (string-to-number (match-string 2 hms
)) 0)
257 (string-to-number (match-string 3 hms
)))
259 (error "Cannot parse HMS string \"%s\"" hms
)))
261 (defun org-timer-hms-to-secs (hms)
262 "Convert h:mm:ss string to an integer time.
263 If the string starts with a minus sign, the integer will be negative."
264 (if (not (string-match
265 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
268 (let* ((h (string-to-number (match-string 1 hms
)))
269 (m (string-to-number (match-string 2 hms
)))
270 (s (string-to-number (match-string 3 hms
)))
271 (sign (equal (substring (match-string 1 hms
) 0 1) "-")))
273 (* (if sign -
1 1) (+ s
(* 60 (+ m
(* 60 h
))))))))
275 (defun org-timer-secs-to-hms (s)
276 "Convert integer S into h:mm:ss.
277 If the integer is negative, the string will start with \"-\"."
279 (setq sign
(if (< s
0) "-" "")
281 m
(/ s
60) s
(- s
(* 60 m
))
282 h
(/ m
60) m
(- m
(* 60 h
)))
283 (format "%s%d:%02d:%02d" sign h m s
)))
285 (defvar org-timer-mode-line-timer nil
)
286 (defvar org-timer-mode-line-string nil
)
288 (defun org-timer-set-mode-line (value)
289 "Set the mode-line display of the relative timer.
290 VALUE can be `on', `off', or `pause'."
291 (when (or (eq org-timer-display
'mode-line
)
292 (eq org-timer-display
'both
))
293 (or global-mode-string
(setq global-mode-string
'("")))
294 (or (memq 'org-timer-mode-line-string global-mode-string
)
295 (setq global-mode-string
296 (append global-mode-string
'(org-timer-mode-line-string)))))
297 (when (or (eq org-timer-display
'frame-title
)
298 (eq org-timer-display
'both
))
299 (or (memq 'org-timer-mode-line-string frame-title-format
)
300 (setq frame-title-format
301 (append frame-title-format
'(org-timer-mode-line-string)))))
304 (when org-timer-mode-line-timer
305 (cancel-timer org-timer-mode-line-timer
)
306 (setq org-timer-mode-line-timer nil
))
307 (when (or (eq org-timer-display
'mode-line
)
308 (eq org-timer-display
'both
))
309 (setq global-mode-string
310 (delq 'org-timer-mode-line-string global-mode-string
)))
311 (when (or (eq org-timer-display
'frame-title
)
312 (eq org-timer-display
'both
))
313 (setq frame-title-format
314 (delq 'org-timer-mode-line-string frame-title-format
)))
315 (force-mode-line-update))
316 ((equal value
'pause
)
317 (when org-timer-mode-line-timer
318 (cancel-timer org-timer-mode-line-timer
)
319 (setq org-timer-mode-line-timer nil
)))
321 (when (or (eq org-timer-display
'mode-line
)
322 (eq org-timer-display
'both
))
323 (or global-mode-string
(setq global-mode-string
'("")))
324 (or (memq 'org-timer-mode-line-string global-mode-string
)
325 (setq global-mode-string
326 (append global-mode-string
'(org-timer-mode-line-string)))))
327 (when (or (eq org-timer-display
'frame-title
)
328 (eq org-timer-display
'both
))
329 (or (memq 'org-timer-mode-line-string frame-title-format
)
330 (setq frame-title-format
331 (append frame-title-format
'(org-timer-mode-line-string)))))
332 (org-timer-update-mode-line)
333 (when org-timer-mode-line-timer
334 (cancel-timer org-timer-mode-line-timer
)
335 (setq org-timer-mode-line-timer nil
))
336 (when org-timer-display
337 (setq org-timer-mode-line-timer
338 (run-with-timer 1 1 'org-timer-update-mode-line
))))))
340 (defun org-timer-update-mode-line ()
341 "Update the timer time in the mode line."
342 (if org-timer-pause-time
344 (setq org-timer-mode-line-string
345 (concat " <" (substring (org-timer-value-string) 0 -
1) ">"))
346 (force-mode-line-update)))
348 (defvar org-timer-current-timer nil
)
349 (defun org-timer-cancel-timer ()
350 "Cancel the current timer."
352 (when (eval org-timer-current-timer
)
353 (run-hooks 'org-timer-cancel-hook
)
354 (cancel-timer org-timer-current-timer
)
355 (setq org-timer-current-timer nil
)
356 (setq org-timer-timer-is-countdown nil
)
357 (org-timer-set-mode-line 'off
))
358 (message "Last timer canceled"))
360 (defun org-timer-show-remaining-time ()
361 "Display the remaining time before the timer ends."
364 (if (not org-timer-current-timer
)
365 (message "No timer set")
366 (let* ((rtime (decode-time
367 (time-subtract (timer--time org-timer-current-timer
)
369 (rsecs (nth 0 rtime
))
370 (rmins (nth 1 rtime
)))
371 (message "%d minute(s) %d seconds left before next time out"
375 (defun org-timer-set-timer (&optional opt
)
376 "Prompt for a duration and set a timer.
378 If `org-timer-default-timer' is not zero, suggest this value as
379 the default duration for the timer. If a timer is already set,
380 prompt the user if she wants to replace it.
382 Called with a numeric prefix argument, use this numeric value as
383 the duration of the timer.
385 Called with a `C-u' prefix arguments, use `org-timer-default-timer'
386 without prompting the user for a duration.
388 With two `C-u' prefix arguments, use `org-timer-default-timer'
389 without prompting the user for a duration and automatically
390 replace any running timer."
392 (let ((minutes (or (and (numberp opt
) (number-to-string opt
))
393 (and (listp opt
) (not (null opt
))
394 (number-to-string org-timer-default-timer
))
395 (read-from-minibuffer
396 "How many minutes left? "
397 (if (not (eq org-timer-default-timer
0))
398 (number-to-string org-timer-default-timer
))))))
399 (if (not (string-match "[0-9]+" minutes
))
400 (org-timer-show-remaining-time)
401 (let* ((mins (string-to-number (match-string 0 minutes
)))
404 ((string-match "Org Agenda" (buffer-name))
405 (let* ((marker (or (get-text-property (point) 'org-marker
)
407 (hdmarker (or (get-text-property (point) 'org-hd-marker
)
409 (pos (marker-position marker
)))
410 (with-current-buffer (marker-buffer marker
)
414 (or (ignore-errors (org-get-heading))
415 (concat "File:" (file-name-nondirectory (buffer-file-name)))))))
416 ((derived-mode-p 'org-mode
)
417 (or (ignore-errors (org-get-heading))
418 (concat "File:" (file-name-nondirectory (buffer-file-name)))))
419 (t (error "Not in an Org buffer"))))
421 (if (or (and org-timer-current-timer
422 (or (equal opt
'(16))
423 (y-or-n-p "Replace current timer? ")))
424 (not org-timer-current-timer
))
427 (when org-timer-current-timer
428 (cancel-timer org-timer-current-timer
))
429 (setq org-timer-current-timer
432 (setq org-timer-current-timer nil
)
433 (org-notify ,(format "%s: time out" hl
) t
)
434 (setq org-timer-timer-is-countdown nil
)
435 (org-timer-set-mode-line 'off
)
436 (run-hooks 'org-timer-done-hook
))))
437 (run-hooks 'org-timer-set-hook
)
438 (setq org-timer-timer-is-countdown t
440 (time-add (current-time) (seconds-to-time (* mins
60))))
441 (org-timer-set-mode-line 'on
))
442 (message "No timer set"))))))
446 ;;; org-timer.el ends here