Implement timer for timed notes.
[org-mode.git] / lisp / org-timer.el
blob8ecfebd1775b3ef40b5703208c79c3eda44e240a
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
8 ;; Version: 6.13a
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 (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."
37 :group 'org-time
38 :type 'string)
40 ;;;###autoload
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
50 the region 0:00:00."
51 (interactive "P")
52 (if (equal offset '(16))
53 (call-interactively 'org-timer-change-times-in-region)
54 (let (delta des s)
55 (if (not offset)
56 (setq org-timer-start-time (current-time))
57 (cond
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)
62 (match-string 0)
63 "0:00:00")
64 s (read-string
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
69 (seconds-to-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))))))
77 ;;;###autoload
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."
85 (interactive "P")
86 (if (equal restart '(4)) (org-timer-start))
87 (or org-timer-start-time (org-timer-start))
88 (insert (format
89 org-timer-format
90 (org-timer-secs-to-hms
91 (floor
92 (- (time-to-seconds (current-time))
93 (time-to-seconds org-timer-start-time))))))))
95 ;;;###autoload
96 (defun org-timer-change-times-in-region (beg end delta)
97 "Change all h:mm:ss time in region by a DELTA."
98 (interactive
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)
102 (save-excursion
103 (goto-char beg)
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"))
111 (save-excursion
112 (goto-char end)
113 (while (re-search-backward re beg t)
114 (setq p (point))
115 (replace-match
116 (save-match-data
117 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
118 t t)
119 (goto-char p)))))
121 ;;;###autoload
122 (defun org-timer-item (arg)
123 "Insert a description-type item with the curren timer value."
124 (interactive "P")
125 (let ((ind 0))
126 (save-excursion
127 (skip-chars-backward " \n\t")
128 (condition-case nil
129 (progn
130 (org-beginning-of-item)
131 (setq ind (org-get-indentation)))
132 (error nil)))
133 (or (bolp) (newline))
134 (org-indent-line-to ind)
135 (insert "- ")
136 (org-timer (if arg '(4)))
137 (insert ":: ")))
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)
142 (replace-match
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)))
147 t t 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\\}\\)"
155 hms))
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) "-")))
161 (setq h (abs h))
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 \"-\"."
167 (let (sign m h)
168 (setq sign (if (< s 0) "-" "")
169 s (abs s)
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