1 ;;; org-clock.el --- The time clocking code for Org-mode
3 ;; Copyright (C) 2008 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
30 (defconst org-timer-re
"\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
31 "Regular expression used to match timer stamps.")
33 (defcustom org-timer-format
"%s "
34 "The format to insert the time of the timer.
35 This format must contain one instance of \"%s\" which will be replaced by
36 the value of the relative timer."
41 (defun org-timer-start (&optional offset
)
42 "Set the starting time for the relative timer to now.
43 When called with prefix argument OFFSET, prompt the user for an offset time,
44 with the default taken from a timer stamp at point, if any.
45 If OFFSET is a string or an integer, it is directly taken to be the offset
46 without user interaction.
47 When called with a double prefix arg, all timer strings in the active
48 region will be shifted by a specific amount. You will be prompted for
49 the amount, with the default to make the first timer string in
52 (if (equal offset
'(16))
53 (call-interactively 'org-timer-change-times-in-region
)
56 (setq org-timer-start-time
(current-time))
58 ((integerp offset
) (setq delta offset
))
59 ((stringp offset
) (setq delta
(org-timer-hms-to-secs offset
)))
61 (setq def
(if (org-in-regexp org-timer-re
)
65 (format "Restart timer with offset [%s]: " def
)))
66 (unless (string-match "\\S-" s
) (setq s def
))
67 (setq delta
(org-timer-hms-to-secs (org-timer-fix-incomplete s
)))))
68 (setq org-timer-start-time
71 (time-to-seconds (current-time))
72 (org-timer-hms-to-secs s
)))))
73 (message "Timer start time set to %s, current value is %s"
74 (format-time-string "%T" org-timer-start-time
)
75 (org-timer-secs-to-hms (or delta
0))))))
78 (defun org-timer (&optional restart
)
79 "Insert a H:MM:SS string from the timer into the buffer.
80 The first time this command is used, the timer is started. When used with
81 a `C-u' prefix, force restarting the timer.
82 When used with a double prefix arg `C-u C-u', change all the timer string
83 in the region by a fixed amount. This can be used to recalibrate a timer
84 that was not started at the correct moment."
86 (if (equal restart
'(4)) (org-timer-start))
87 (or org-timer-start-time
(org-timer-start))
90 (org-timer-secs-to-hms
92 (- (time-to-seconds (current-time))
93 (time-to-seconds org-timer-start-time
))))))))
96 (defun org-timer-change-times-in-region (beg end delta
)
97 "Change all h:mm:ss time in region by a DELTA."
99 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
100 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p
)
101 (unless (string-match "\\S-" delta
)
104 (when (re-search-forward re end t
)
105 (setq delta
(match-string 0))
106 (if (equal (string-to-char delta
) ?-
)
107 (setq delta
(substring delta
1))
108 (setq delta
(concat "-" delta
))))))
109 (setq delta
(my-hms-to-secs (org-timer-fix-incomplete delta
)))
110 (when (= delta
0) (error "No change"))
113 (while (re-search-backward re beg t
)
117 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta
)))
122 (defun org-timer-item (arg)
123 "Insert a description-type item with the curren timer value."
127 (skip-chars-backward " \n\t")
130 (org-beginning-of-item)
131 (setq ind
(org-get-indentation)))
133 (or (bolp) (newline))
134 (org-indent-line-to ind
)
136 (org-timer (if arg
'(4)))
139 (defun org-timer-fix-incomplete (hms)
140 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
141 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms
)
143 (format "%d:%02d:%02d"
144 (if (match-end 1) (string-to-int (match-string 1 hms
)) 0)
145 (if (match-end 2) (string-to-int (match-string 2 hms
)) 0)
146 (string-to-int (match-string 3 hms
)))
148 (error "Canot parse HMS string \"%s\"" hms
)))
150 (defun org-timer-hms-to-secs (hms)
151 "Convert h:mm:ss string to an integer time.
152 If the string starts with a minus sign, the integer will be negative."
153 (if (not (string-match
154 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
157 (let* ((h (string-to-int (match-string 1 hms
)))
158 (m (string-to-int (match-string 2 hms
)))
159 (s (string-to-int (match-string 3 hms
)))
160 (sign (equal (substring (match-string 1 hms
) 0 1) "-")))
162 (* (if sign -
1 1) (+ s
(* 60 (+ m
(* 60 h
))))))))
164 (defun org-timer-secs-to-hms (s)
165 "Convert integer S into h:mm:ss.
166 If the integer is negative, the strig will start with \"-\"."
168 (setq sign
(if (< s
0) "-" "")
170 m
(/ s
60) s
(- s
(* 60 m
))
171 h
(/ m
60) m
(- m
(* 60 h
)))
172 (format "%s%d:%02d:%02d" sign h m s
)))
174 ;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
176 ;;; org-timer.el ends here