Release 6.26b
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-timer.el
blob8e11d852f823900ec0c60ba83fc67eb6bfbf615f
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.26b
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 (defvar org-timer-start-time nil
33 "t=0 for the running timer.")
35 (defvar org-timer-pause-time nil
36 "Time when the timer was paused.")
38 (defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
39 "Regular expression used to match timer stamps.")
41 (defcustom org-timer-format "%s "
42 "The format to insert the time of the timer.
43 This format must contain one instance of \"%s\" which will be replaced by
44 the value of the relative timer."
45 :group 'org-time
46 :type 'string)
48 ;;;###autoload
49 (defun org-timer-start (&optional offset)
50 "Set the starting time for the relative timer to now.
51 When called with prefix argument OFFSET, prompt the user for an offset time,
52 with the default taken from a timer stamp at point, if any.
53 If OFFSET is a string or an integer, it is directly taken to be the offset
54 without user interaction.
55 When called with a double prefix arg, all timer strings in the active
56 region will be shifted by a specific amount. You will be prompted for
57 the amount, with the default to make the first timer string in
58 the region 0:00:00."
59 (interactive "P")
60 (if (equal offset '(16))
61 (call-interactively 'org-timer-change-times-in-region)
62 (let (delta def s)
63 (if (not offset)
64 (setq org-timer-start-time (current-time))
65 (cond
66 ((integerp offset) (setq delta offset))
67 ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
69 (setq def (if (org-in-regexp org-timer-re)
70 (match-string 0)
71 "0:00:00")
72 s (read-string
73 (format "Restart timer with offset [%s]: " def)))
74 (unless (string-match "\\S-" s) (setq s def))
75 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
76 (setq org-timer-start-time
77 (seconds-to-time
79 (time-to-seconds (current-time))
80 (org-timer-hms-to-secs s)))))
81 (org-timer-set-mode-line 'on)
82 (message "Timer start time set to %s, current value is %s"
83 (format-time-string "%T" org-timer-start-time)
84 (org-timer-secs-to-hms (or delta 0))))))
86 (defun org-timer-pause-or-continue (&optional stop)
87 "Pause or continue the relative timer. With prefix arg, stop it entirely."
88 (interactive "P")
89 (cond
90 (stop (org-timer-stop))
91 ((not org-timer-start-time) (error "No timer is running"))
92 (org-timer-pause-time
93 ;; timer is paused, continue
94 (setq org-timer-start-time
95 (seconds-to-time
97 (time-to-seconds (current-time))
98 (- (time-to-seconds org-timer-pause-time)
99 (time-to-seconds org-timer-start-time))))
100 org-timer-pause-time nil)
101 (org-timer-set-mode-line 'on)
102 (message "Timer continues at %s" (org-timer-value-string)))
104 ;; pause timer
105 (setq org-timer-pause-time (current-time))
106 (org-timer-set-mode-line 'pause)
107 (message "Timer paused at %s" (org-timer-value-string)))))
109 (defun org-timer-stop ()
110 "Stop the relative timer."
111 (interactive)
112 (setq org-timer-start-time nil
113 org-timer-pause-time nil)
114 (org-timer-set-mode-line 'off))
116 ;;;###autoload
117 (defun org-timer (&optional restart)
118 "Insert a H:MM:SS string from the timer into the buffer.
119 The first time this command is used, the timer is started. When used with
120 a `C-u' prefix, force restarting the timer.
121 When used with a double prefix arg `C-u C-u', change all the timer string
122 in the region by a fixed amount. This can be used to recalibrate a timer
123 that was not started at the correct moment."
124 (interactive "P")
125 (if (equal restart '(4)) (org-timer-start))
126 (or org-timer-start-time (org-timer-start))
127 (insert (org-timer-value-string)))
129 (defun org-timer-value-string ()
130 (format org-timer-format (org-timer-secs-to-hms (floor (org-timer-seconds)))))
132 (defun org-timer-seconds ()
133 (- (time-to-seconds (or org-timer-pause-time (current-time)))
134 (time-to-seconds org-timer-start-time)))
136 ;;;###autoload
137 (defun org-timer-change-times-in-region (beg end delta)
138 "Change all h:mm:ss time in region by a DELTA."
139 (interactive
140 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
141 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
142 (unless (string-match "\\S-" delta)
143 (save-excursion
144 (goto-char beg)
145 (when (re-search-forward re end t)
146 (setq delta (match-string 0))
147 (if (equal (string-to-char delta) ?-)
148 (setq delta (substring delta 1))
149 (setq delta (concat "-" delta))))))
150 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
151 (when (= delta 0) (error "No change"))
152 (save-excursion
153 (goto-char end)
154 (while (re-search-backward re beg t)
155 (setq p (point))
156 (replace-match
157 (save-match-data
158 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
159 t t)
160 (goto-char p)))))
162 ;;;###autoload
163 (defun org-timer-item (&optional arg)
164 "Insert a description-type item with the current timer value."
165 (interactive "P")
166 (let ((ind 0))
167 (save-excursion
168 (skip-chars-backward " \n\t")
169 (condition-case nil
170 (progn
171 (org-beginning-of-item)
172 (setq ind (org-get-indentation)))
173 (error nil)))
174 (or (bolp) (newline))
175 (org-indent-line-to ind)
176 (insert "- ")
177 (org-timer (if arg '(4)))
178 (insert ":: ")))
180 (defun org-timer-fix-incomplete (hms)
181 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
182 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
183 (replace-match
184 (format "%d:%02d:%02d"
185 (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
186 (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
187 (string-to-number (match-string 3 hms)))
188 t t hms)
189 (error "Cannot parse HMS string \"%s\"" hms)))
191 (defun org-timer-hms-to-secs (hms)
192 "Convert h:mm:ss string to an integer time.
193 If the string starts with a minus sign, the integer will be negative."
194 (if (not (string-match
195 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
196 hms))
198 (let* ((h (string-to-number (match-string 1 hms)))
199 (m (string-to-number (match-string 2 hms)))
200 (s (string-to-number (match-string 3 hms)))
201 (sign (equal (substring (match-string 1 hms) 0 1) "-")))
202 (setq h (abs h))
203 (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
205 (defun org-timer-secs-to-hms (s)
206 "Convert integer S into h:mm:ss.
207 If the integer is negative, the string will start with \"-\"."
208 (let (sign m h)
209 (setq sign (if (< s 0) "-" "")
210 s (abs s)
211 m (/ s 60) s (- s (* 60 m))
212 h (/ m 60) m (- m (* 60 h)))
213 (format "%s%d:%02d:%02d" sign h m s)))
215 (defvar org-timer-mode-line-timer nil)
216 (defvar org-timer-mode-line-string nil)
218 (defun org-timer-set-mode-line (value)
219 "Set the mode-line dispay of the relative timer.
220 VALUE can be `on', `off', or `pause'."
221 (or global-mode-string (setq global-mode-string '("")))
222 (or (memq 'org-timer-mode-line-string global-mode-string)
223 (setq global-mode-string
224 (append global-mode-string '(org-timer-mode-line-string))))
225 (cond
226 ((equal value 'off)
227 (when org-timer-mode-line-timer
228 (cancel-timer org-timer-mode-line-timer)
229 (setq org-timer-mode-line-timer nil))
230 (setq global-mode-string
231 (delq 'org-timer-mode-line-string global-mode-string))
232 (force-mode-line-update))
233 ((equal value 'pause)
234 (when org-timer-mode-line-timer
235 (cancel-timer org-timer-mode-line-timer)
236 (setq org-timer-mode-line-timer nil)))
237 ((equal value 'on)
238 (or global-mode-string (setq global-mode-string '("")))
239 (or (memq 'org-timer-mode-line-string global-mode-string)
240 (setq global-mode-string
241 (append global-mode-string '(org-timer-mode-line-string))))
242 (org-timer-update-mode-line)
243 (when org-timer-mode-line-timer
244 (cancel-timer org-timer-mode-line-timer))
245 (setq org-timer-mode-line-timer
246 (run-with-timer 1 1 'org-timer-update-mode-line)))))
248 (defun org-timer-update-mode-line ()
249 "Update the timer time in the mode line."
250 (if org-timer-pause-time
252 (setq org-timer-mode-line-string
253 (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
254 (force-mode-line-update)))
256 (provide 'org-timer)
258 ;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
260 ;;; org-timer.el ends here