lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / org-timer.el
blob2aead5dca3c83e2f731911ca1e1a5589c30e7c04
1 ;;; org-timer.el --- Timer code for Org mode -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2008-2019 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: https://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file implements two types of timers for Org buffers:
29 ;; - A relative timer that counts up (from 0 or a specified offset)
30 ;; - A countdown timer that counts down from a specified time
32 ;; The relative and countdown timers differ in their entry points.
33 ;; Use `org-timer' or `org-timer-start' to start the relative timer,
34 ;; and `org-timer-set-timer' to start the countdown timer.
36 ;;; Code:
38 (require 'cl-lib)
39 (require 'org-clock)
41 (declare-function org-agenda-error "org-agenda" ())
43 (defvar org-timer-start-time nil
44 "t=0 for the running timer.")
46 (defvar org-timer-pause-time nil
47 "Time when the timer was paused.")
49 (defvar org-timer-countdown-timer nil
50 "Current countdown timer.
51 This is a timer object if there is an active countdown timer,
52 `paused' if there is a paused countdown timer, and nil
53 otherwise.")
55 (defvar org-timer-countdown-timer-title nil
56 "Title for notification displayed when a countdown finishes.")
58 (defconst org-timer-re "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
59 "Regular expression used to match timer stamps.")
61 (defcustom org-timer-format "%s "
62 "The format to insert the time of the timer.
63 This format must contain one instance of \"%s\" which will be replaced by
64 the value of the timer."
65 :group 'org-time
66 :type 'string)
68 (defcustom org-timer-default-timer "0"
69 "The default timer when a timer is set, in minutes or hh:mm:ss format.
70 When 0, the user is prompted for a value."
71 :group 'org-time
72 :version "26.1"
73 :package-version '(Org . "8.3")
74 :type 'string)
76 (defcustom org-timer-display 'mode-line
77 "Define where running timer is displayed, if at all.
78 When a timer is running, Org can display it in the mode line
79 and/or frame title. Allowed values are:
81 both displays in both mode line and frame title
82 mode-line displays only in mode line (default)
83 frame-title displays only in frame title
84 nil current timer is not displayed"
85 :group 'org-time
86 :type '(choice
87 (const :tag "Mode line" mode-line)
88 (const :tag "Frame title" frame-title)
89 (const :tag "Both" both)
90 (const :tag "None" nil)))
92 (defvar org-timer-start-hook nil
93 "Hook run after relative timer is started.")
95 (defvar org-timer-stop-hook nil
96 "Hook run before relative or countdown timer is stopped.")
98 (defvar org-timer-pause-hook nil
99 "Hook run before relative or countdown timer is paused.")
101 (defvar org-timer-continue-hook nil
102 "Hook run after relative or countdown timer is continued.")
104 (defvar org-timer-set-hook nil
105 "Hook run after countdown timer is set.")
107 (defvar org-timer-done-hook nil
108 "Hook run after countdown timer reaches zero.")
110 ;;;###autoload
111 (defun org-timer-start (&optional offset)
112 "Set the starting time for the relative timer to now.
113 When called with prefix argument OFFSET, prompt the user for an offset time,
114 with the default taken from a timer stamp at point, if any.
115 If OFFSET is a string or an integer, it is directly taken to be the offset
116 without user interaction.
117 When called with a double prefix arg, all timer strings in the active
118 region will be shifted by a specific amount. You will be prompted for
119 the amount, with the default to make the first timer string in
120 the region 0:00:00."
121 (interactive "P")
122 (cond
123 ((equal offset '(16))
124 (call-interactively 'org-timer-change-times-in-region))
125 (org-timer-countdown-timer
126 (user-error "Countdown timer is running. Cancel first"))
128 (let (delta def s)
129 (if (not offset)
130 (setq org-timer-start-time (current-time))
131 (cond
132 ((integerp offset) (setq delta offset))
133 ((stringp offset) (setq delta (org-timer-hms-to-secs offset)))
135 (setq def (if (org-in-regexp org-timer-re)
136 (match-string 0)
137 "0:00:00")
138 s (read-string
139 (format "Restart timer with offset [%s]: " def)))
140 (unless (string-match "\\S-" s) (setq s def))
141 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
142 (setq org-timer-start-time
143 (seconds-to-time
144 (- (float-time) delta))))
145 (setq org-timer-pause-time nil)
146 (org-timer-set-mode-line 'on)
147 (message "Timer start time set to %s, current value is %s"
148 (format-time-string "%T" org-timer-start-time)
149 (org-timer-secs-to-hms (or delta 0)))
150 (run-hooks 'org-timer-start-hook)))))
152 ;;;###autoload
153 (defun org-timer-pause-or-continue (&optional stop)
154 "Pause or continue the relative or countdown timer.
155 With prefix arg STOP, stop it entirely."
156 (interactive "P")
157 (cond
158 (stop (org-timer-stop))
159 ((not org-timer-start-time) (error "No timer is running"))
160 (org-timer-pause-time
161 (let ((start-secs (float-time org-timer-start-time))
162 (pause-secs (float-time org-timer-pause-time)))
163 ;; Note: We pass the result of `current-time' to `time-add' and
164 ;; `float-time' below so that we can easily override the value
165 ;; in tests.
166 (if org-timer-countdown-timer
167 (let ((new-secs (- start-secs pause-secs)))
168 (setq org-timer-countdown-timer
169 (org-timer--run-countdown-timer
170 new-secs org-timer-countdown-timer-title))
171 (setq org-timer-start-time
172 (time-add (current-time) (seconds-to-time new-secs))))
173 (setq org-timer-start-time
174 (seconds-to-time (- (float-time)
175 (- pause-secs start-secs)))))
176 (setq org-timer-pause-time nil)
177 (org-timer-set-mode-line 'on)
178 (run-hooks 'org-timer-continue-hook)
179 (message "Timer continues at %s" (org-timer-value-string))))
181 ;; pause timer
182 (when org-timer-countdown-timer
183 (cancel-timer org-timer-countdown-timer)
184 (setq org-timer-countdown-timer 'paused))
185 (run-hooks 'org-timer-pause-hook)
186 (setq org-timer-pause-time (current-time))
187 (org-timer-set-mode-line 'paused)
188 (message "Timer paused at %s" (org-timer-value-string)))))
190 ;;;###autoload
191 (defun org-timer-stop ()
192 "Stop the relative or countdown timer."
193 (interactive)
194 (unless org-timer-start-time
195 (user-error "No timer running"))
196 (when (timerp org-timer-countdown-timer)
197 (cancel-timer org-timer-countdown-timer))
198 (run-hooks 'org-timer-stop-hook)
199 (setq org-timer-start-time nil
200 org-timer-pause-time nil
201 org-timer-countdown-timer nil)
202 (org-timer-set-mode-line 'off)
203 (message "Timer stopped"))
205 ;;;###autoload
206 (defun org-timer (&optional restart no-insert)
207 "Insert a H:MM:SS string from the timer into the buffer.
208 The first time this command is used, the timer is started.
210 When used with a `\\[universal-argument]' prefix, force restarting the timer.
212 When used with a `\\[universal-argument] \\[universal-argument]' \
213 prefix, change all the timer strings
214 in the region by a fixed amount. This can be used to re-calibrate
215 a timer that was not started at the correct moment.
217 If NO-INSERT is non-nil, return the string instead of inserting
218 it in the buffer."
219 (interactive "P")
220 (if (equal restart '(16))
221 (org-timer-start restart)
222 (when (or (equal restart '(4)) (not org-timer-start-time))
223 (org-timer-start))
224 (if no-insert
225 (org-timer-value-string)
226 (insert (org-timer-value-string)))))
228 (defun org-timer-value-string ()
229 "Return current timer string."
230 (format org-timer-format
231 (org-timer-secs-to-hms
232 (let ((time (- (float-time org-timer-pause-time)
233 (float-time org-timer-start-time))))
234 (abs (floor (if org-timer-countdown-timer (- time) time)))))))
236 ;;;###autoload
237 (defun org-timer-change-times-in-region (beg end delta)
238 "Change all h:mm:ss time in region by a DELTA."
239 (interactive
240 "r\nsEnter time difference like \"-1:08:26\". Default is first time to zero: ")
241 (let ((re "[-+]?[0-9]+:[0-9]\\{2\\}:[0-9]\\{2\\}") p)
242 (unless (string-match "\\S-" delta)
243 (save-excursion
244 (goto-char beg)
245 (when (re-search-forward re end t)
246 (setq delta (match-string 0))
247 (if (equal (string-to-char delta) ?-)
248 (setq delta (substring delta 1))
249 (setq delta (concat "-" delta))))))
250 (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete delta)))
251 (when (= delta 0) (error "No change"))
252 (save-excursion
253 (goto-char end)
254 (while (re-search-backward re beg t)
255 (setq p (point))
256 (replace-match
257 (save-match-data
258 (org-timer-secs-to-hms (+ (org-timer-hms-to-secs (match-string 0)) delta)))
259 t t)
260 (goto-char p)))))
262 ;;;###autoload
263 (defun org-timer-item (&optional arg)
264 "Insert a description-type item with the current timer value."
265 (interactive "P")
266 (let ((itemp (org-in-item-p)) (pos (point)))
267 (cond
268 ;; In a timer list, insert with `org-list-insert-item',
269 ;; then fix the list.
270 ((and itemp (goto-char itemp) (org-at-item-timer-p))
271 (let* ((struct (org-list-struct))
272 (prevs (org-list-prevs-alist struct))
273 (s (concat (org-timer (when arg '(4)) t) ":: ")))
274 (setq struct (org-list-insert-item pos struct prevs nil s))
275 (org-list-write-struct struct (org-list-parents-alist struct))
276 (looking-at org-list-full-item-re)
277 (goto-char (match-end 0))))
278 ;; In a list of another type, don't break anything: throw an error.
279 (itemp (goto-char pos) (error "This is not a timer list"))
280 ;; Else, start a new list.
282 (beginning-of-line)
283 (org-indent-line)
284 (insert "- ")
285 (org-timer (when arg '(4)))
286 (insert ":: ")))))
288 (defun org-timer-fix-incomplete (hms)
289 "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
290 (if (string-match "\\(?:\\([0-9]+:\\)?\\([0-9]+:\\)\\)?\\([0-9]+\\)" hms)
291 (replace-match
292 (format "%d:%02d:%02d"
293 (if (match-end 1) (string-to-number (match-string 1 hms)) 0)
294 (if (match-end 2) (string-to-number (match-string 2 hms)) 0)
295 (string-to-number (match-string 3 hms)))
296 t t hms)
297 (error "Cannot parse HMS string \"%s\"" hms)))
299 (defun org-timer-hms-to-secs (hms)
300 "Convert h:mm:ss string to an integer time.
301 If the string starts with a minus sign, the integer will be negative."
302 (if (not (string-match
303 "\\([-+]?[0-9]+\\):\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)"
304 hms))
306 (let* ((h (string-to-number (match-string 1 hms)))
307 (m (string-to-number (match-string 2 hms)))
308 (s (string-to-number (match-string 3 hms)))
309 (sign (equal (substring (match-string 1 hms) 0 1) "-")))
310 (setq h (abs h))
311 (* (if sign -1 1) (+ s (* 60 (+ m (* 60 h))))))))
313 (defun org-timer-secs-to-hms (s)
314 "Convert integer S into h:mm:ss.
315 If the integer is negative, the string will start with \"-\"."
316 (let (sign m h)
317 (setq sign (if (< s 0) "-" "")
318 s (abs s)
319 m (/ s 60) s (- s (* 60 m))
320 h (/ m 60) m (- m (* 60 h)))
321 (format "%s%d:%02d:%02d" sign h m s)))
323 (defvar org-timer-mode-line-timer nil)
324 (defvar org-timer-mode-line-string nil)
326 (defun org-timer-set-mode-line (value)
327 "Set the mode-line display for relative or countdown timer.
328 VALUE can be `on', `off', or `paused'."
329 (when (or (eq org-timer-display 'mode-line)
330 (eq org-timer-display 'both))
331 (or global-mode-string (setq global-mode-string '("")))
332 (or (memq 'org-timer-mode-line-string global-mode-string)
333 (setq global-mode-string
334 (append global-mode-string '(org-timer-mode-line-string)))))
335 (when (or (eq org-timer-display 'frame-title)
336 (eq org-timer-display 'both))
337 (or (memq 'org-timer-mode-line-string frame-title-format)
338 (setq frame-title-format
339 (append frame-title-format '(org-timer-mode-line-string)))))
340 (cl-case value
341 (off
342 (when org-timer-mode-line-timer
343 (cancel-timer org-timer-mode-line-timer)
344 (setq org-timer-mode-line-timer nil))
345 (when (or (eq org-timer-display 'mode-line)
346 (eq org-timer-display 'both))
347 (setq global-mode-string
348 (delq 'org-timer-mode-line-string global-mode-string)))
349 (when (or (eq org-timer-display 'frame-title)
350 (eq org-timer-display 'both))
351 (setq frame-title-format
352 (delq 'org-timer-mode-line-string frame-title-format)))
353 (force-mode-line-update))
354 (paused
355 (when org-timer-mode-line-timer
356 (cancel-timer org-timer-mode-line-timer)
357 (setq org-timer-mode-line-timer nil)))
359 (when (or (eq org-timer-display 'mode-line)
360 (eq org-timer-display 'both))
361 (or global-mode-string (setq global-mode-string '("")))
362 (or (memq 'org-timer-mode-line-string global-mode-string)
363 (setq global-mode-string
364 (append global-mode-string '(org-timer-mode-line-string)))))
365 (when (or (eq org-timer-display 'frame-title)
366 (eq org-timer-display 'both))
367 (or (memq 'org-timer-mode-line-string frame-title-format)
368 (setq frame-title-format
369 (append frame-title-format '(org-timer-mode-line-string)))))
370 (org-timer-update-mode-line)
371 (when org-timer-mode-line-timer
372 (cancel-timer org-timer-mode-line-timer)
373 (setq org-timer-mode-line-timer nil))
374 (when org-timer-display
375 (setq org-timer-mode-line-timer
376 (run-with-timer 1 1 'org-timer-update-mode-line))))))
378 (defun org-timer-update-mode-line ()
379 "Update the timer time in the mode line."
380 (if org-timer-pause-time
382 (setq org-timer-mode-line-string
383 (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
384 (force-mode-line-update)))
386 (defun org-timer-show-remaining-time ()
387 "Display the remaining time before the timer ends."
388 (interactive)
389 (require 'time)
390 (if (not org-timer-countdown-timer)
391 (message "No timer set")
392 (let* ((rtime (decode-time
393 (time-subtract (timer--time org-timer-countdown-timer)
394 (current-time))))
395 (rsecs (nth 0 rtime))
396 (rmins (nth 1 rtime)))
397 (message "%d minute(s) %d seconds left before next time out"
398 rmins rsecs))))
400 ;;;###autoload
401 (defun org-timer-set-timer (&optional opt)
402 "Prompt for a duration in minutes or hh:mm:ss and set a timer.
404 If `org-timer-default-timer' is not \"0\", suggest this value as
405 the default duration for the timer. If a timer is already set,
406 prompt the user if she wants to replace it.
408 Called with a numeric prefix argument, use this numeric value as
409 the duration of the timer in minutes.
411 Called with a `C-u' prefix arguments, use `org-timer-default-timer'
412 without prompting the user for a duration.
414 With two `C-u' prefix arguments, use `org-timer-default-timer'
415 without prompting the user for a duration and automatically
416 replace any running timer.
418 By default, the timer duration will be set to the number of
419 minutes in the Effort property, if any. You can ignore this by
420 using three `C-u' prefix arguments."
421 (interactive "P")
422 (when (and org-timer-start-time
423 (not org-timer-countdown-timer))
424 (user-error "Relative timer is running. Stop first"))
425 (let* ((default-timer
426 ;; `org-timer-default-timer' used to be a number, don't choke:
427 (if (numberp org-timer-default-timer)
428 (number-to-string org-timer-default-timer)
429 org-timer-default-timer))
430 (effort-minutes (let ((effort (org-entry-get nil org-effort-property)))
431 (when (org-string-nw-p effort)
432 (floor (org-duration-to-minutes effort)))))
433 (minutes (or (and (numberp opt) (number-to-string opt))
434 (and (not (equal opt '(64)))
435 effort-minutes
436 (number-to-string effort-minutes))
437 (and (consp opt) default-timer)
438 (and (stringp opt) opt)
439 (read-from-minibuffer
440 "How much time left? (minutes or h:mm:ss) "
441 (and (not (string-equal default-timer "0")) default-timer)))))
442 (when (string-match "\\`[0-9]+\\'" minutes)
443 (setq minutes (concat minutes ":00")))
444 (if (not (string-match "[0-9]+" minutes))
445 (org-timer-show-remaining-time)
446 (let ((secs (org-timer-hms-to-secs (org-timer-fix-incomplete minutes))))
447 (if (and org-timer-countdown-timer
448 (not (or (equal opt '(16))
449 (y-or-n-p "Replace current timer? "))))
450 (message "No timer set")
451 (when (timerp org-timer-countdown-timer)
452 (cancel-timer org-timer-countdown-timer))
453 (setq org-timer-countdown-timer-title
454 (org-timer--get-timer-title))
455 (setq org-timer-countdown-timer
456 (org-timer--run-countdown-timer
457 secs org-timer-countdown-timer-title))
458 (run-hooks 'org-timer-set-hook)
459 ;; Pass `current-time' result to `time-add' (instead of nil)
460 ;; for for Emacs 24 compatibility.
461 (setq org-timer-start-time
462 (time-add (current-time) (seconds-to-time secs)))
463 (setq org-timer-pause-time nil)
464 (org-timer-set-mode-line 'on))))))
466 (defun org-timer--run-countdown-timer (secs title)
467 "Start countdown timer that will last SECS.
468 TITLE will be appended to the notification message displayed when
469 time is up."
470 (let ((msg (format "%s: time out" title)))
471 (run-with-timer
472 secs nil `(lambda ()
473 (setq org-timer-countdown-timer nil
474 org-timer-start-time nil)
475 (org-notify ,msg ,org-clock-sound)
476 (org-timer-set-mode-line 'off)
477 (run-hooks 'org-timer-done-hook)))))
479 (defun org-timer--get-timer-title ()
480 "Construct timer title from heading or file name of Org buffer."
481 (cond
482 ((derived-mode-p 'org-agenda-mode)
483 (let* ((marker (or (get-text-property (point) 'org-marker)
484 (org-agenda-error)))
485 (hdmarker (or (get-text-property (point) 'org-hd-marker)
486 marker)))
487 (with-current-buffer (marker-buffer marker)
488 (org-with-wide-buffer
489 (goto-char hdmarker)
490 (org-show-entry)
491 (or (ignore-errors (org-get-heading))
492 (buffer-name (buffer-base-buffer)))))))
493 ((derived-mode-p 'org-mode)
494 (or (ignore-errors (org-get-heading))
495 (buffer-name (buffer-base-buffer))))
496 (t (error "Not in an Org buffer"))))
498 (provide 'org-timer)
500 ;; Local variables:
501 ;; generated-autoload-file: "org-loaddefs.el"
502 ;; End:
504 ;;; org-timer.el ends here