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
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 (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."
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
60 (if (equal offset '(16))
61 (call-interactively 'org-timer-change-times-in-region)
64 (setq org-timer-start-time (current-time))
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)
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
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."
90 (stop (org-timer-stop))
91 ((not org-timer-start-time) (error "No timer is running"))
93 ;; timer is paused, continue
94 (setq org-timer-start-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)))
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."
112 (setq org-timer-start-time nil
113 org-timer-pause-time nil)
114 (org-timer-set-mode-line 'off))
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."
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)))
137 (defun org-timer-change-times-in-region (beg end delta)
138 "Change all h:mm:ss time in region by a DELTA."
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)
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"))
154 (while (re-search-backward re beg t)
158 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
163 (defun org-timer-item (&optional arg)
164 "Insert a description-type item with the current timer value."
168 (skip-chars-backward " \n\t")
171 (org-beginning-of-item)
172 (setq ind (org-get-indentation)))
174 (or (bolp) (newline))
175 (org-indent-line-to ind)
177 (org-timer (if arg '(4)))
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)
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)))
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\\}\\)"
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) "-")))
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 \"-\"."
209 (setq sign (if (< s 0) "-" "")
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))))
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)))
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 ;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
258 ;;; org-timer.el ends here