Commit | Line | Data |
---|---|---|
0bd48b37 | 1 | ;;; org-timer.el --- The relative timer code for Org-mode |
bc23baaa | 2 | |
b73f1974 | 3 | ;; Copyright (C) 2008-2012 Free Software Foundation, Inc. |
bc23baaa CD |
4 | |
5 | ;; Author: Carsten Dominik <carsten at orgmode dot org> | |
6 | ;; Keywords: outlines, hypermedia, calendar, wp | |
7 | ;; Homepage: http://orgmode.org | |
bc23baaa CD |
8 | ;; |
9 | ;; This file is part of GNU Emacs. | |
10 | ;; | |
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. | |
15 | ||
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. | |
20 | ||
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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
24 | ;; | |
25 | ;;; Commentary: | |
26 | ||
27 | ;; This file contains the relative timer code for Org-mode | |
28 | ||
86fbb8ca CD |
29 | ;;; Code: |
30 | ||
bc23baaa CD |
31 | (require 'org) |
32 | ||
afe98dfa | 33 | (declare-function org-notify "org-clock" (notification &optional play-sound)) |
c8d0cf5c CD |
34 | (declare-function org-agenda-error "org-agenda" ()) |
35 | ||
bc23baaa CD |
36 | (defvar org-timer-start-time nil |
37 | "t=0 for the running timer.") | |
38 | ||
0bd48b37 CD |
39 | (defvar org-timer-pause-time nil |
40 | "Time when the timer was paused.") | |
41 | ||
bc23baaa CD |
42 | (defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)" |
43 | "Regular expression used to match timer stamps.") | |
44 | ||
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) | |
51 | ||
86fbb8ca CD |
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 | |
372d7b21 | 56 | :version "24.1" |
86fbb8ca CD |
57 | :type 'number) |
58 | ||
ed21c5c8 CD |
59 | (defvar org-timer-start-hook nil |
60 | "Hook run after relative timer is started.") | |
61 | ||
62 | (defvar org-timer-stop-hook nil | |
63 | "Hook run before relative timer is stopped.") | |
64 | ||
65 | (defvar org-timer-pause-hook nil | |
66 | "Hook run before relative timer is paused.") | |
67 | ||
3ab2c837 BG |
68 | (defvar org-timer-continue-hook nil |
69 | "Hook run after relative timer is continued.") | |
70 | ||
ed21c5c8 CD |
71 | (defvar org-timer-set-hook nil |
72 | "Hook run after countdown timer is set.") | |
73 | ||
74 | (defvar org-timer-done-hook nil | |
75 | "Hook run after countdown timer reaches zero.") | |
76 | ||
77 | (defvar org-timer-cancel-hook nil | |
78 | "Hook run before countdown timer is canceled.") | |
79 | ||
bc23baaa CD |
80 | ;;;###autoload |
81 | (defun org-timer-start (&optional offset) | |
82 | "Set the starting time for the relative timer to now. | |
83 | When called with prefix argument OFFSET, prompt the user for an offset time, | |
84 | with the default taken from a timer stamp at point, if any. | |
85 | If OFFSET is a string or an integer, it is directly taken to be the offset | |
86 | without user interaction. | |
87 | When called with a double prefix arg, all timer strings in the active | |
88 | region will be shifted by a specific amount. You will be prompted for | |
89 | the amount, with the default to make the first timer string in | |
90 | the region 0:00:00." | |
91 | (interactive "P") | |
92 | (if (equal offset '(16)) | |
93 | (call-interactively 'org-timer-change-times-in-region) | |
94 | (let (delta def s) | |
95 | (if (not offset) | |
96 | (setq org-timer-start-time (current-time)) | |
97 | (cond | |
98 | ((integerp offset) (setq delta offset)) | |
99 | ((stringp offset) (setq delta (org-timer-hms-to-secs offset))) | |
100 | (t | |
101 | (setq def (if (org-in-regexp org-timer-re) | |
102 | (match-string 0) | |
103 | "0:00:00") | |
104 | s (read-string | |
105 | (format "Restart timer with offset [%s]: " def))) | |
106 | (unless (string-match "\\S-" s) (setq s def)) | |
107 | (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s))))) | |
108 | (setq org-timer-start-time | |
109 | (seconds-to-time | |
86fbb8ca | 110 | (- (org-float-time) delta)))) |
0bd48b37 | 111 | (org-timer-set-mode-line 'on) |
bc23baaa CD |
112 | (message "Timer start time set to %s, current value is %s" |
113 | (format-time-string "%T" org-timer-start-time) | |
ed21c5c8 CD |
114 | (org-timer-secs-to-hms (or delta 0))) |
115 | (run-hooks 'org-timer-start-hook)))) | |
bc23baaa | 116 | |
0bd48b37 | 117 | (defun org-timer-pause-or-continue (&optional stop) |
86fbb8ca CD |
118 | "Pause or continue the relative timer. |
119 | With prefix arg STOP, stop it entirely." | |
0bd48b37 CD |
120 | (interactive "P") |
121 | (cond | |
122 | (stop (org-timer-stop)) | |
123 | ((not org-timer-start-time) (error "No timer is running")) | |
124 | (org-timer-pause-time | |
125 | ;; timer is paused, continue | |
126 | (setq org-timer-start-time | |
127 | (seconds-to-time | |
128 | (- | |
54a0dee5 CD |
129 | (org-float-time) |
130 | (- (org-float-time org-timer-pause-time) | |
131 | (org-float-time org-timer-start-time)))) | |
0bd48b37 CD |
132 | org-timer-pause-time nil) |
133 | (org-timer-set-mode-line 'on) | |
3ab2c837 | 134 | (run-hooks 'org-timer-continue-hook) |
0bd48b37 CD |
135 | (message "Timer continues at %s" (org-timer-value-string))) |
136 | (t | |
137 | ;; pause timer | |
ed21c5c8 | 138 | (run-hooks 'org-timer-pause-hook) |
0bd48b37 CD |
139 | (setq org-timer-pause-time (current-time)) |
140 | (org-timer-set-mode-line 'pause) | |
141 | (message "Timer paused at %s" (org-timer-value-string))))) | |
142 | ||
143 | (defun org-timer-stop () | |
144 | "Stop the relative timer." | |
145 | (interactive) | |
ed21c5c8 | 146 | (run-hooks 'org-timer-stop-hook) |
0bd48b37 CD |
147 | (setq org-timer-start-time nil |
148 | org-timer-pause-time nil) | |
149 | (org-timer-set-mode-line 'off)) | |
150 | ||
bc23baaa | 151 | ;;;###autoload |
afe98dfa | 152 | (defun org-timer (&optional restart no-insert-p) |
bc23baaa CD |
153 | "Insert a H:MM:SS string from the timer into the buffer. |
154 | The first time this command is used, the timer is started. When used with | |
86fbb8ca | 155 | a \\[universal-argument] prefix, force restarting the timer. |
afe98dfa | 156 | When used with a double prefix argument \\[universal-argument], change all the timer string |
bc23baaa | 157 | in the region by a fixed amount. This can be used to recalibrate a timer |
afe98dfa CD |
158 | that was not started at the correct moment. |
159 | ||
160 | If NO-INSERT-P is non-nil, return the string instead of inserting | |
161 | it in the buffer." | |
bc23baaa | 162 | (interactive "P") |
afe98dfa CD |
163 | (when (or (equal restart '(4)) (not org-timer-start-time)) |
164 | (org-timer-start)) | |
165 | (if no-insert-p | |
166 | (org-timer-value-string) | |
167 | (insert (org-timer-value-string)))) | |
0bd48b37 CD |
168 | |
169 | (defun org-timer-value-string () | |
170 | (format org-timer-format (org-timer-secs-to-hms (floor (org-timer-seconds))))) | |
171 | ||
afe98dfa | 172 | (defvar org-timer-timer-is-countdown nil) |
0bd48b37 | 173 | (defun org-timer-seconds () |
afe98dfa CD |
174 | (if org-timer-timer-is-countdown |
175 | (- (org-float-time org-timer-start-time) | |
176 | (org-float-time (current-time))) | |
177 | (- (org-float-time (or org-timer-pause-time (current-time))) | |
178 | (org-float-time org-timer-start-time)))) | |
bc23baaa CD |
179 | |
180 | ;;;###autoload | |
181 | (defun org-timer-change-times-in-region (beg end delta) | |
182 | "Change all h:mm:ss time in region by a DELTA." | |
183 | (interactive | |
184 | "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ") | |
185 | (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p) | |
186 | (unless (string-match "\\S-" delta) | |
187 | (save-excursion | |
188 | (goto-char beg) | |
189 | (when (re-search-forward re end t) | |
190 | (setq delta (match-string 0)) | |
191 | (if (equal (string-to-char delta) ?-) | |
192 | (setq delta (substring delta 1)) | |
193 | (setq delta (concat "-" delta)))))) | |
194 | (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta))) | |
195 | (when (= delta 0) (error "No change")) | |
196 | (save-excursion | |
197 | (goto-char end) | |
198 | (while (re-search-backward re beg t) | |
199 | (setq p (point)) | |
200 | (replace-match | |
201 | (save-match-data | |
202 | (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta))) | |
203 | t t) | |
204 | (goto-char p))))) | |
205 | ||
206 | ;;;###autoload | |
207 | (defun org-timer-item (&optional arg) | |
33306645 | 208 | "Insert a description-type item with the current timer value." |
bc23baaa | 209 | (interactive "P") |
3ab2c837 BG |
210 | (let ((itemp (org-in-item-p)) (pos (point))) |
211 | (cond | |
212 | ;; In a timer list, insert with `org-list-insert-item', | |
213 | ;; then fix the list. | |
214 | ((and itemp (goto-char itemp) (org-at-item-timer-p)) | |
215 | (let* ((struct (org-list-struct)) | |
216 | (prevs (org-list-prevs-alist struct)) | |
217 | (s (concat (org-timer (when arg '(4)) t) ":: "))) | |
218 | (setq struct (org-list-insert-item pos struct prevs nil s)) | |
219 | (org-list-write-struct struct (org-list-parents-alist struct)) | |
220 | (looking-at org-list-full-item-re) | |
221 | (goto-char (match-end 0)))) | |
222 | ;; In a list of another type, don't break anything: throw an error. | |
223 | (itemp (goto-char pos) (error "This is not a timer list")) | |
224 | ;; Else, start a new list. | |
225 | (t | |
226 | (beginning-of-line) | |
227 | (org-indent-line-function) | |
228 | (insert "- ") | |
229 | (org-timer (when arg '(4))) | |
230 | (insert ":: "))))) | |
bc23baaa CD |
231 | |
232 | (defun org-timer-fix-incomplete (hms) | |
233 | "If hms is a H:MM:SS string with missing hour or hour and minute, fix it." | |
234 | (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms) | |
235 | (replace-match | |
236 | (format "%d:%02d:%02d" | |
237 | (if (match-end 1) (string-to-number (match-string 1 hms)) 0) | |
238 | (if (match-end 2) (string-to-number (match-string 2 hms)) 0) | |
239 | (string-to-number (match-string 3 hms))) | |
240 | t t hms) | |
33306645 | 241 | (error "Cannot parse HMS string \"%s\"" hms))) |
bc23baaa CD |
242 | |
243 | (defun org-timer-hms-to-secs (hms) | |
244 | "Convert h:mm:ss string to an integer time. | |
245 | If the string starts with a minus sign, the integer will be negative." | |
246 | (if (not (string-match | |
247 | "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)" | |
248 | hms)) | |
249 | 0 | |
250 | (let* ((h (string-to-number (match-string 1 hms))) | |
251 | (m (string-to-number (match-string 2 hms))) | |
252 | (s (string-to-number (match-string 3 hms))) | |
253 | (sign (equal (substring (match-string 1 hms) 0 1) "-"))) | |
254 | (setq h (abs h)) | |
255 | (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h)))))))) | |
256 | ||
257 | (defun org-timer-secs-to-hms (s) | |
258 | "Convert integer S into h:mm:ss. | |
33306645 | 259 | If the integer is negative, the string will start with \"-\"." |
bc23baaa CD |
260 | (let (sign m h) |
261 | (setq sign (if (< s 0) "-" "") | |
262 | s (abs s) | |
263 | m (/ s 60) s (- s (* 60 m)) | |
264 | h (/ m 60) m (- m (* 60 h))) | |
265 | (format "%s%d:%02d:%02d" sign h m s))) | |
266 | ||
0bd48b37 CD |
267 | (defvar org-timer-mode-line-timer nil) |
268 | (defvar org-timer-mode-line-string nil) | |
269 | ||
270 | (defun org-timer-set-mode-line (value) | |
8bfe682a | 271 | "Set the mode-line display of the relative timer. |
0bd48b37 CD |
272 | VALUE can be `on', `off', or `pause'." |
273 | (or global-mode-string (setq global-mode-string '(""))) | |
274 | (or (memq 'org-timer-mode-line-string global-mode-string) | |
275 | (setq global-mode-string | |
276 | (append global-mode-string '(org-timer-mode-line-string)))) | |
277 | (cond | |
278 | ((equal value 'off) | |
279 | (when org-timer-mode-line-timer | |
280 | (cancel-timer org-timer-mode-line-timer) | |
281 | (setq org-timer-mode-line-timer nil)) | |
282 | (setq global-mode-string | |
283 | (delq 'org-timer-mode-line-string global-mode-string)) | |
284 | (force-mode-line-update)) | |
285 | ((equal value 'pause) | |
286 | (when org-timer-mode-line-timer | |
287 | (cancel-timer org-timer-mode-line-timer) | |
288 | (setq org-timer-mode-line-timer nil))) | |
289 | ((equal value 'on) | |
290 | (or global-mode-string (setq global-mode-string '(""))) | |
291 | (or (memq 'org-timer-mode-line-string global-mode-string) | |
292 | (setq global-mode-string | |
293 | (append global-mode-string '(org-timer-mode-line-string)))) | |
294 | (org-timer-update-mode-line) | |
295 | (when org-timer-mode-line-timer | |
296 | (cancel-timer org-timer-mode-line-timer)) | |
297 | (setq org-timer-mode-line-timer | |
298 | (run-with-timer 1 1 'org-timer-update-mode-line))))) | |
299 | ||
300 | (defun org-timer-update-mode-line () | |
301 | "Update the timer time in the mode line." | |
302 | (if org-timer-pause-time | |
303 | nil | |
304 | (setq org-timer-mode-line-string | |
305 | (concat " <" (substring (org-timer-value-string) 0 -1) ">")) | |
306 | (force-mode-line-update))) | |
307 | ||
ed21c5c8 CD |
308 | (defvar org-timer-current-timer nil) |
309 | (defun org-timer-cancel-timer () | |
310 | "Cancel the current timer." | |
c8d0cf5c | 311 | (interactive) |
ed21c5c8 CD |
312 | (when (eval org-timer-current-timer) |
313 | (run-hooks 'org-timer-cancel-hook) | |
314 | (cancel-timer org-timer-current-timer) | |
afe98dfa CD |
315 | (setq org-timer-current-timer nil) |
316 | (setq org-timer-timer-is-countdown nil) | |
317 | (org-timer-set-mode-line 'off)) | |
ed21c5c8 | 318 | (message "Last timer canceled")) |
c8d0cf5c CD |
319 | |
320 | (defun org-timer-show-remaining-time () | |
321 | "Display the remaining time before the timer ends." | |
322 | (interactive) | |
323 | (require 'time) | |
ed21c5c8 | 324 | (if (not org-timer-current-timer) |
c8d0cf5c CD |
325 | (message "No timer set") |
326 | (let* ((rtime (decode-time | |
ed21c5c8 | 327 | (time-subtract (timer--time org-timer-current-timer) |
c8d0cf5c CD |
328 | (current-time)))) |
329 | (rsecs (nth 0 rtime)) | |
330 | (rmins (nth 1 rtime))) | |
ed21c5c8 | 331 | (message "%d minute(s) %d seconds left before next time out" |
c8d0cf5c CD |
332 | rmins rsecs)))) |
333 | ||
334 | ;;;###autoload | |
86fbb8ca CD |
335 | (defun org-timer-set-timer (&optional opt) |
336 | "Prompt for a duration and set a timer. | |
337 | ||
338 | If `org-timer-default-timer' is not zero, suggest this value as | |
339 | the default duration for the timer. If a timer is already set, | |
afe98dfa | 340 | prompt the user if she wants to replace it. |
86fbb8ca CD |
341 | |
342 | Called with a numeric prefix argument, use this numeric value as | |
343 | the duration of the timer. | |
344 | ||
345 | Called with a `C-u' prefix arguments, use `org-timer-default-timer' | |
346 | without prompting the user for a duration. | |
347 | ||
348 | With two `C-u' prefix arguments, use `org-timer-default-timer' | |
349 | without prompting the user for a duration and automatically | |
350 | replace any running timer." | |
351 | (interactive "P") | |
352 | (let ((minutes (or (and (numberp opt) (number-to-string opt)) | |
353 | (and (listp opt) (not (null opt)) | |
354 | (number-to-string org-timer-default-timer)) | |
355 | (read-from-minibuffer | |
356 | "How many minutes left? " | |
357 | (if (not (eq org-timer-default-timer 0)) | |
358 | (number-to-string org-timer-default-timer)))))) | |
359 | (if (not (string-match "[0-9]+" minutes)) | |
360 | (org-timer-show-remaining-time) | |
c8d0cf5c CD |
361 | (let* ((mins (string-to-number (match-string 0 minutes))) |
362 | (secs (* mins 60)) | |
363 | (hl (cond | |
364 | ((string-match "Org Agenda" (buffer-name)) | |
365 | (let* ((marker (or (get-text-property (point) 'org-marker) | |
366 | (org-agenda-error))) | |
367 | (hdmarker (or (get-text-property (point) 'org-hd-marker) | |
368 | marker)) | |
369 | (pos (marker-position marker))) | |
370 | (with-current-buffer (marker-buffer marker) | |
371 | (widen) | |
372 | (goto-char pos) | |
373 | (org-show-entry) | |
afe98dfa CD |
374 | (or (ignore-errors (org-get-heading)) |
375 | (concat "File:" (file-name-nondirectory (buffer-file-name))))))) | |
e66ba1df | 376 | ((eq major-mode 'org-mode) |
afe98dfa CD |
377 | (or (ignore-errors (org-get-heading)) |
378 | (concat "File:" (file-name-nondirectory (buffer-file-name))))) | |
c8d0cf5c CD |
379 | (t (error "Not in an Org buffer")))) |
380 | timer-set) | |
86fbb8ca CD |
381 | (if (or (and org-timer-current-timer |
382 | (or (equal opt '(16)) | |
383 | (y-or-n-p "Replace current timer? "))) | |
384 | (not org-timer-current-timer)) | |
385 | (progn | |
afe98dfa | 386 | (require 'org-clock) |
86fbb8ca CD |
387 | (when org-timer-current-timer |
388 | (cancel-timer org-timer-current-timer)) | |
389 | (setq org-timer-current-timer | |
390 | (run-with-timer | |
391 | secs nil `(lambda () | |
392 | (setq org-timer-current-timer nil) | |
393 | (org-notify ,(format "%s: time out" hl) t) | |
afe98dfa CD |
394 | (setq org-timer-timer-is-countdown nil) |
395 | (org-timer-set-mode-line 'off) | |
86fbb8ca | 396 | (run-hooks 'org-timer-done-hook)))) |
afe98dfa CD |
397 | (run-hooks 'org-timer-set-hook) |
398 | (setq org-timer-timer-is-countdown t | |
399 | org-timer-start-time | |
400 | (time-add (current-time) (seconds-to-time (* mins 60)))) | |
401 | (org-timer-set-mode-line 'on)) | |
86fbb8ca | 402 | (message "No timer set")))))) |
c8d0cf5c | 403 | |
a2a2e7fb CD |
404 | (provide 'org-timer) |
405 | ||
bc23baaa | 406 | ;;; org-timer.el ends here |