Make #+BIND work correctly, and ask the user to confirm using BIND.
[org-mode/org-tableheadings.git] / lisp / org-timer.el
bloba21e01fdaf01592b71b07316af186f8093cda9df
1 ;;; org-timer.el --- The relative timer code for Org-mode
3 ;; Copyright (C) 2008, 2009 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 ;; Version: 6.29b
9 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains the relative timer code for Org-mode
30 (require 'org)
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."
48 :group 'org-time
49 :type 'string)
51 ;;;###autoload
52 (defun org-timer-start (&optional offset)
53 "Set the starting time for the relative timer to now.
54 When called with prefix argument OFFSET, prompt the user for an offset time,
55 with the default taken from a timer stamp at point, if any.
56 If OFFSET is a string or an integer, it is directly taken to be the offset
57 without user interaction.
58 When called with a double prefix arg, all timer strings in the active
59 region will be shifted by a specific amount. You will be prompted for
60 the amount, with the default to make the first timer string in
61 the region 0:00:00."
62 (interactive "P")
63 (if (equal offset '(16))
64 (call-interactively 'org-timer-change-times-in-region)
65 (let (delta def s)
66 (if (not offset)
67 (setq org-timer-start-time (current-time))
68 (cond
69 ((integerp offset) (setq delta offset))
70 ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
72 (setq def (if (org-in-regexp org-timer-re)
73 (match-string 0)
74 "0:00:00")
75 s (read-string
76 (format "Restart timer with offset [%s]: " def)))
77 (unless (string-match "\\S-" s) (setq s def))
78 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
79 (setq org-timer-start-time
80 (seconds-to-time
82 (time-to-seconds (current-time))
83 (org-timer-hms-to-secs s)))))
84 (org-timer-set-mode-line 'on)
85 (message "Timer start time set to %s, current value is %s"
86 (format-time-string "%T" org-timer-start-time)
87 (org-timer-secs-to-hms (or delta 0))))))
89 (defun org-timer-pause-or-continue (&optional stop)
90 "Pause or continue the relative timer. With prefix arg, stop it entirely."
91 (interactive "P")
92 (cond
93 (stop (org-timer-stop))
94 ((not org-timer-start-time) (error "No timer is running"))
95 (org-timer-pause-time
96 ;; timer is paused, continue
97 (setq org-timer-start-time
98 (seconds-to-time
100 (time-to-seconds (current-time))
101 (- (time-to-seconds org-timer-pause-time)
102 (time-to-seconds org-timer-start-time))))
103 org-timer-pause-time nil)
104 (org-timer-set-mode-line 'on)
105 (message "Timer continues at %s" (org-timer-value-string)))
107 ;; pause timer
108 (setq org-timer-pause-time (current-time))
109 (org-timer-set-mode-line 'pause)
110 (message "Timer paused at %s" (org-timer-value-string)))))
112 (defun org-timer-stop ()
113 "Stop the relative timer."
114 (interactive)
115 (setq org-timer-start-time nil
116 org-timer-pause-time nil)
117 (org-timer-set-mode-line 'off))
119 ;;;###autoload
120 (defun org-timer (&optional restart)
121 "Insert a H:MM:SS string from the timer into the buffer.
122 The first time this command is used, the timer is started. When used with
123 a `C-u' prefix, force restarting the timer.
124 When used with a double prefix arg `C-u C-u', change all the timer string
125 in the region by a fixed amount. This can be used to recalibrate a timer
126 that was not started at the correct moment."
127 (interactive "P")
128 (if (equal restart '(4)) (org-timer-start))
129 (or org-timer-start-time (org-timer-start))
130 (insert (org-timer-value-string)))
132 (defun org-timer-value-string ()
133 (format org-timer-format (org-timer-secs-to-hms (floor (org-timer-seconds)))))
135 (defun org-timer-seconds ()
136 (- (time-to-seconds (or org-timer-pause-time (current-time)))
137 (time-to-seconds org-timer-start-time)))
139 ;;;###autoload
140 (defun org-timer-change-times-in-region (beg end delta)
141 "Change all h:mm:ss time in region by a DELTA."
142 (interactive
143 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
144 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
145 (unless (string-match "\\S-" delta)
146 (save-excursion
147 (goto-char beg)
148 (when (re-search-forward re end t)
149 (setq delta (match-string 0))
150 (if (equal (string-to-char delta) ?-)
151 (setq delta (substring delta 1))
152 (setq delta (concat "-" delta))))))
153 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
154 (when (= delta 0) (error "No change"))
155 (save-excursion
156 (goto-char end)
157 (while (re-search-backward re beg t)
158 (setq p (point))
159 (replace-match
160 (save-match-data
161 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
162 t t)
163 (goto-char p)))))
165 ;;;###autoload
166 (defun org-timer-item (&optional arg)
167 "Insert a description-type item with the current timer value."
168 (interactive "P")
169 (let ((ind 0))
170 (save-excursion
171 (skip-chars-backward " \n\t")
172 (condition-case nil
173 (progn
174 (org-beginning-of-item)
175 (setq ind (org-get-indentation)))
176 (error nil)))
177 (or (bolp) (newline))
178 (org-indent-line-to ind)
179 (insert "- ")
180 (org-timer (if arg '(4)))
181 (insert ":: ")))
183 (defun org-timer-fix-incomplete (hms)
184 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
185 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
186 (replace-match
187 (format "%d:%02d:%02d"
188 (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
189 (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
190 (string-to-number (match-string 3 hms)))
191 t t hms)
192 (error "Cannot parse HMS string \"%s\"" hms)))
194 (defun org-timer-hms-to-secs (hms)
195 "Convert h:mm:ss string to an integer time.
196 If the string starts with a minus sign, the integer will be negative."
197 (if (not (string-match
198 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
199 hms))
201 (let* ((h (string-to-number (match-string 1 hms)))
202 (m (string-to-number (match-string 2 hms)))
203 (s (string-to-number (match-string 3 hms)))
204 (sign (equal (substring (match-string 1 hms) 0 1) "-")))
205 (setq h (abs h))
206 (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
208 (defun org-timer-secs-to-hms (s)
209 "Convert integer S into h:mm:ss.
210 If the integer is negative, the string will start with \"-\"."
211 (let (sign m h)
212 (setq sign (if (< s 0) "-" "")
213 s (abs s)
214 m (/ s 60) s (- s (* 60 m))
215 h (/ m 60) m (- m (* 60 h)))
216 (format "%s%d:%02d:%02d" sign h m s)))
218 (defvar org-timer-mode-line-timer nil)
219 (defvar org-timer-mode-line-string nil)
221 (defun org-timer-set-mode-line (value)
222 "Set the mode-line dispay of the relative timer.
223 VALUE can be `on', `off', or `pause'."
224 (or global-mode-string (setq global-mode-string '("")))
225 (or (memq 'org-timer-mode-line-string global-mode-string)
226 (setq global-mode-string
227 (append global-mode-string '(org-timer-mode-line-string))))
228 (cond
229 ((equal value 'off)
230 (when org-timer-mode-line-timer
231 (cancel-timer org-timer-mode-line-timer)
232 (setq org-timer-mode-line-timer nil))
233 (setq global-mode-string
234 (delq 'org-timer-mode-line-string global-mode-string))
235 (force-mode-line-update))
236 ((equal value 'pause)
237 (when org-timer-mode-line-timer
238 (cancel-timer org-timer-mode-line-timer)
239 (setq org-timer-mode-line-timer nil)))
240 ((equal value 'on)
241 (or global-mode-string (setq global-mode-string '("")))
242 (or (memq 'org-timer-mode-line-string global-mode-string)
243 (setq global-mode-string
244 (append global-mode-string '(org-timer-mode-line-string))))
245 (org-timer-update-mode-line)
246 (when org-timer-mode-line-timer
247 (cancel-timer org-timer-mode-line-timer))
248 (setq org-timer-mode-line-timer
249 (run-with-timer 1 1 'org-timer-update-mode-line)))))
251 (defun org-timer-update-mode-line ()
252 "Update the timer time in the mode line."
253 (if org-timer-pause-time
255 (setq org-timer-mode-line-string
256 (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
257 (force-mode-line-update)))
259 (defvar org-timer-timer1 nil)
260 (defvar org-timer-timer2 nil)
261 (defvar org-timer-timer3 nil)
262 (defvar org-timer-last-timer nil)
264 (defun org-timer-cancel-timers ()
265 "Reset all timers."
266 (interactive)
267 (mapc (lambda(timer)
268 (when (eval timer)
269 (cancel-timer timer)
270 (setq timer nil)))
271 '(org-timer-timer1
272 org-timer-timer2
273 org-timer-timer3))
274 (message "All timers reset"))
276 (defun org-timer-show-remaining-time ()
277 "Display the remaining time before the timer ends."
278 (interactive)
279 (require 'time)
280 (if (and (not org-timer-timer1)
281 (not org-timer-timer2)
282 (not org-timer-timer3))
283 (message "No timer set")
284 (let* ((rtime (decode-time
285 (time-subtract (timer--time org-timer-last-timer)
286 (current-time))))
287 (rsecs (nth 0 rtime))
288 (rmins (nth 1 rtime)))
289 (message "%d minutes %d secondes left before next time out"
290 rmins rsecs))))
292 ;;;###autoload
293 (defun org-timer-set-timer (minutes)
294 "Set a timer."
295 (interactive "sTime out in (min)? ")
296 (if (not (string-match "[0-9]+" minutes))
297 (org-timer-show-remaining-time)
298 (let* ((mins (string-to-number (match-string 0 minutes)))
299 (secs (* mins 60))
300 (hl (cond
301 ((string-match "Org Agenda" (buffer-name))
302 (let* ((marker (or (get-text-property (point) 'org-marker)
303 (org-agenda-error)))
304 (hdmarker (or (get-text-property (point) 'org-hd-marker)
305 marker))
306 (pos (marker-position marker)))
307 (with-current-buffer (marker-buffer marker)
308 (widen)
309 (goto-char pos)
310 (org-show-entry)
311 (org-get-heading))))
312 ((eq major-mode 'org-mode)
313 (org-get-heading))
314 (t (error "Not in an Org buffer"))))
315 timer-set)
316 (mapcar (lambda(timer)
317 (if (not (or (eval timer) timer-set))
318 (setq timer-set t
319 timer
320 (run-with-timer secs nil 'org-show-notification
321 (format "%s: time out" hl))
322 org-timer-last-timer timer)))
323 '(org-timer-timer1
324 org-timer-timer2
325 org-timer-timer3)))))
327 (provide 'org-timer)
329 ;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
331 ;;; org-timer.el ends here