1 ;;; org-timer.el --- The relative timer code for Org-mode
3 ;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
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.
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.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the relative timer code for Org-mode
32 (declare-function org-show-notification
"org-clock" (parameters))
33 (declare-function org-agenda-error
"org-agenda" ())
35 (defvar org-timer-start-time nil
36 "t=0 for the running timer.")
38 (defvar org-timer-pause-time nil
39 "Time when the timer was paused.")
41 (defconst org-timer-re
"\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
42 "Regular expression used to match timer stamps.")
44 (defcustom org-timer-format
"%s "
45 "The format to insert the time of the timer.
46 This format must contain one instance of \"%s\" which will be replaced by
47 the value of the relative timer."
51 (defcustom org-timer-default-timer
0
52 "The default timer when a timer is set.
53 When 0, the user is prompted for a value."
57 (defvar org-timer-start-hook nil
58 "Hook run after relative timer is started.")
60 (defvar org-timer-stop-hook nil
61 "Hook run before relative timer is stopped.")
63 (defvar org-timer-pause-hook nil
64 "Hook run before relative timer is paused.")
66 (defvar org-timer-set-hook nil
67 "Hook run after countdown timer is set.")
69 (defvar org-timer-done-hook nil
70 "Hook run after countdown timer reaches zero.")
72 (defvar org-timer-cancel-hook nil
73 "Hook run before countdown timer is canceled.")
76 (defun org-timer-start (&optional offset
)
77 "Set the starting time for the relative timer to now.
78 When called with prefix argument OFFSET, prompt the user for an offset time,
79 with the default taken from a timer stamp at point, if any.
80 If OFFSET is a string or an integer, it is directly taken to be the offset
81 without user interaction.
82 When called with a double prefix arg, all timer strings in the active
83 region will be shifted by a specific amount. You will be prompted for
84 the amount, with the default to make the first timer string in
87 (if (equal offset
'(16))
88 (call-interactively 'org-timer-change-times-in-region
)
91 (setq org-timer-start-time
(current-time))
93 ((integerp offset
) (setq delta offset
))
94 ((stringp offset
) (setq delta
(org-timer-hms-to-secs offset
)))
96 (setq def
(if (org-in-regexp org-timer-re
)
100 (format "Restart timer with offset [%s]: " def
)))
101 (unless (string-match "\\S-" s
) (setq s def
))
102 (setq delta
(org-timer-hms-to-secs (org-timer-fix-incomplete s
)))))
103 (setq org-timer-start-time
105 (- (org-float-time) (org-timer-hms-to-secs s
)))))
106 (org-timer-set-mode-line 'on
)
107 (message "Timer start time set to %s, current value is %s"
108 (format-time-string "%T" org-timer-start-time
)
109 (org-timer-secs-to-hms (or delta
0)))
110 (run-hooks 'org-timer-start-hook
))))
112 (defun org-timer-pause-or-continue (&optional stop
)
113 "Pause or continue the relative timer. With prefix arg, stop it entirely."
116 (stop (org-timer-stop))
117 ((not org-timer-start-time
) (error "No timer is running"))
118 (org-timer-pause-time
119 ;; timer is paused, continue
120 (setq org-timer-start-time
124 (- (org-float-time org-timer-pause-time
)
125 (org-float-time org-timer-start-time
))))
126 org-timer-pause-time nil
)
127 (org-timer-set-mode-line 'on
)
128 (message "Timer continues at %s" (org-timer-value-string)))
131 (run-hooks 'org-timer-pause-hook
)
132 (setq org-timer-pause-time
(current-time))
133 (org-timer-set-mode-line 'pause
)
134 (message "Timer paused at %s" (org-timer-value-string)))))
136 (defun org-timer-stop ()
137 "Stop the relative timer."
139 (run-hooks 'org-timer-stop-hook
)
140 (setq org-timer-start-time nil
141 org-timer-pause-time nil
)
142 (org-timer-set-mode-line 'off
))
145 (defun org-timer (&optional restart
)
146 "Insert a H:MM:SS string from the timer into the buffer.
147 The first time this command is used, the timer is started. When used with
148 a `C-u' prefix, force restarting the timer.
149 When used with a double prefix arg `C-u C-u', change all the timer string
150 in the region by a fixed amount. This can be used to recalibrate a timer
151 that was not started at the correct moment."
153 (if (equal restart
'(4)) (org-timer-start))
154 (or org-timer-start-time
(org-timer-start))
155 (insert (org-timer-value-string)))
157 (defun org-timer-value-string ()
158 (format org-timer-format
(org-timer-secs-to-hms (floor (org-timer-seconds)))))
160 (defun org-timer-seconds ()
161 (- (org-float-time (or org-timer-pause-time
(current-time)))
162 (org-float-time org-timer-start-time
)))
165 (defun org-timer-change-times-in-region (beg end delta
)
166 "Change all h:mm:ss time in region by a DELTA."
168 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
169 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p
)
170 (unless (string-match "\\S-" delta
)
173 (when (re-search-forward re end t
)
174 (setq delta
(match-string 0))
175 (if (equal (string-to-char delta
) ?-
)
176 (setq delta
(substring delta
1))
177 (setq delta
(concat "-" delta
))))))
178 (setq delta
(org-timer-hms-to-secs (org-timer-fix-incomplete delta
)))
179 (when (= delta
0) (error "No change"))
182 (while (re-search-backward re beg t
)
186 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta
)))
191 (defun org-timer-item (&optional arg
)
192 "Insert a description-type item with the current timer value."
196 (skip-chars-backward " \n\t")
199 (org-beginning-of-item)
200 (setq ind
(org-get-indentation)))
202 (or (bolp) (newline))
203 (org-indent-line-to ind
)
205 (org-timer (if arg
'(4)))
208 (defun org-timer-fix-incomplete (hms)
209 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
210 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms
)
212 (format "%d:%02d:%02d"
213 (if (match-end 1) (string-to-number (match-string 1 hms
)) 0)
214 (if (match-end 2) (string-to-number (match-string 2 hms
)) 0)
215 (string-to-number (match-string 3 hms
)))
217 (error "Cannot parse HMS string \"%s\"" hms
)))
219 (defun org-timer-hms-to-secs (hms)
220 "Convert h:mm:ss string to an integer time.
221 If the string starts with a minus sign, the integer will be negative."
222 (if (not (string-match
223 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
226 (let* ((h (string-to-number (match-string 1 hms
)))
227 (m (string-to-number (match-string 2 hms
)))
228 (s (string-to-number (match-string 3 hms
)))
229 (sign (equal (substring (match-string 1 hms
) 0 1) "-")))
231 (* (if sign -
1 1) (+ s
(* 60 (+ m
(* 60 h
))))))))
233 (defun org-timer-secs-to-hms (s)
234 "Convert integer S into h:mm:ss.
235 If the integer is negative, the string will start with \"-\"."
237 (setq sign
(if (< s
0) "-" "")
239 m
(/ s
60) s
(- s
(* 60 m
))
240 h
(/ m
60) m
(- m
(* 60 h
)))
241 (format "%s%d:%02d:%02d" sign h m s
)))
243 (defvar org-timer-mode-line-timer nil
)
244 (defvar org-timer-mode-line-string nil
)
246 (defun org-timer-set-mode-line (value)
247 "Set the mode-line display of the relative timer.
248 VALUE can be `on', `off', or `pause'."
249 (or global-mode-string
(setq global-mode-string
'("")))
250 (or (memq 'org-timer-mode-line-string global-mode-string
)
251 (setq global-mode-string
252 (append global-mode-string
'(org-timer-mode-line-string))))
255 (when org-timer-mode-line-timer
256 (cancel-timer org-timer-mode-line-timer
)
257 (setq org-timer-mode-line-timer nil
))
258 (setq global-mode-string
259 (delq 'org-timer-mode-line-string global-mode-string
))
260 (force-mode-line-update))
261 ((equal value
'pause
)
262 (when org-timer-mode-line-timer
263 (cancel-timer org-timer-mode-line-timer
)
264 (setq org-timer-mode-line-timer nil
)))
266 (or global-mode-string
(setq global-mode-string
'("")))
267 (or (memq 'org-timer-mode-line-string global-mode-string
)
268 (setq global-mode-string
269 (append global-mode-string
'(org-timer-mode-line-string))))
270 (org-timer-update-mode-line)
271 (when org-timer-mode-line-timer
272 (cancel-timer org-timer-mode-line-timer
))
273 (setq org-timer-mode-line-timer
274 (run-with-timer 1 1 'org-timer-update-mode-line
)))))
276 (defun org-timer-update-mode-line ()
277 "Update the timer time in the mode line."
278 (if org-timer-pause-time
280 (setq org-timer-mode-line-string
281 (concat " <" (substring (org-timer-value-string) 0 -
1) ">"))
282 (force-mode-line-update)))
284 (defvar org-timer-current-timer nil
)
285 (defun org-timer-cancel-timer ()
286 "Cancel the current timer."
288 (when (eval org-timer-current-timer
)
289 (run-hooks 'org-timer-cancel-hook
)
290 (cancel-timer org-timer-current-timer
)
291 (setq org-timer-current-timer nil
))
292 (message "Last timer canceled"))
294 (defun org-timer-show-remaining-time ()
295 "Display the remaining time before the timer ends."
298 (if (not org-timer-current-timer
)
299 (message "No timer set")
300 (let* ((rtime (decode-time
301 (time-subtract (timer--time org-timer-current-timer
)
303 (rsecs (nth 0 rtime
))
304 (rmins (nth 1 rtime
)))
305 (message "%d minute(s) %d seconds left before next time out"
308 (defun bzg-test (&optional test
)
313 (defun org-timer-set-timer (&optional opt
)
314 "Prompt for a duration and set a timer.
316 If `org-timer-default-timer' is not zero, suggest this value as
317 the default duration for the timer. If a timer is already set,
318 prompt the use if she wants to replace it.
320 Called with a numeric prefix argument, use this numeric value as
321 the duration of the timer.
323 Called with a `C-u' prefix arguments, use `org-timer-default-timer'
324 without prompting the user for a duration.
326 With two `C-u' prefix arguments, use `org-timer-default-timer'
327 without prompting the user for a duration and automatically
328 replace any running timer."
330 (let ((minutes (or (and (numberp opt
) (number-to-string opt
))
331 (and (listp opt
) (not (null opt
))
332 (number-to-string org-timer-default-timer
))
333 (read-from-minibuffer
334 "How many minutes left? "
335 (if (not (eq org-timer-default-timer
0))
336 (number-to-string org-timer-default-timer
))))))
337 (if (not (string-match "[0-9]+" minutes
))
338 (org-timer-show-remaining-time)
339 (let* ((mins (string-to-number (match-string 0 minutes
)))
342 ((string-match "Org Agenda" (buffer-name))
343 (let* ((marker (or (get-text-property (point) 'org-marker
)
345 (hdmarker (or (get-text-property (point) 'org-hd-marker
)
347 (pos (marker-position marker
)))
348 (with-current-buffer (marker-buffer marker
)
353 ((eq major-mode
'org-mode
)
355 (t (error "Not in an Org buffer"))))
357 (if (or (and org-timer-current-timer
358 (or (equal opt
'(16))
359 (y-or-n-p "Replace current timer? ")))
360 (not org-timer-current-timer
))
362 (when org-timer-current-timer
363 (cancel-timer org-timer-current-timer
))
364 (setq org-timer-current-timer
367 (setq org-timer-current-timer nil
)
368 (org-notify ,(format "%s: time out" hl
) t
)
369 (run-hooks 'org-timer-done-hook
))))
370 (run-hooks 'org-timer-set-hook
))
371 (message "No timer set"))))))
375 ;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
377 ;;; org-timer.el ends here