]> code.delx.au - gnu-emacs/blobdiff - lisp/org/org-timer.el
Install org-mode version 7.3
[gnu-emacs] / lisp / org / org-timer.el
index f26060d4564b75818344941c67c47acbf464c845..6c1f4984cf12b583059c2c596497b18d33f880af 100644 (file)
@@ -5,7 +5,7 @@
 ;; Author: Carsten Dominik <carsten at orgmode dot org>
 ;; Keywords: outlines, hypermedia, calendar, wp
 ;; Homepage: http://orgmode.org
-;; Version: 6.33x
+;; Version: 7.3
 ;;
 ;; This file is part of GNU Emacs.
 ;;
 
 ;; This file contains the relative timer code for Org-mode
 
+;;; Code:
+
 (require 'org)
 
-(declare-function org-show-notification "org-clock" (parameters))
+(declare-function org-notify "org-clock" (notification &optional play-sound))
 (declare-function org-agenda-error "org-agenda" ())
 
 (defvar org-timer-start-time nil
@@ -48,6 +50,30 @@ the value of the relative timer."
   :group 'org-time
   :type 'string)
 
+(defcustom org-timer-default-timer 0
+  "The default timer when a timer is set.
+When 0, the user is prompted for a value."
+  :group 'org-time
+  :type 'number)
+
+(defvar org-timer-start-hook nil
+  "Hook run after relative timer is started.")
+
+(defvar org-timer-stop-hook nil
+  "Hook run before relative timer is stopped.")
+
+(defvar org-timer-pause-hook nil
+  "Hook run before relative timer is paused.")
+
+(defvar org-timer-set-hook nil
+  "Hook run after countdown timer is set.")
+
+(defvar org-timer-done-hook nil
+  "Hook run after countdown timer reaches zero.")
+
+(defvar org-timer-cancel-hook nil
+  "Hook run before countdown timer is canceled.")
+
 ;;;###autoload
 (defun org-timer-start (&optional offset)
   "Set the starting time for the relative timer to now.
@@ -78,14 +104,16 @@ the region 0:00:00."
          (setq delta (org-timer-hms-to-secs (org-timer-fix-incomplete s)))))
        (setq org-timer-start-time
              (seconds-to-time
-              (- (org-float-time) (org-timer-hms-to-secs s)))))
+              (- (org-float-time) delta))))
       (org-timer-set-mode-line 'on)
       (message "Timer start time set to %s, current value is %s"
               (format-time-string "%T" org-timer-start-time)
-              (org-timer-secs-to-hms (or delta 0))))))
+              (org-timer-secs-to-hms (or delta 0)))
+      (run-hooks 'org-timer-start-hook))))
 
 (defun org-timer-pause-or-continue (&optional stop)
-  "Pause or continue the relative timer.  With prefix arg, stop it entirely."
+  "Pause or continue the relative timer.
+With prefix arg STOP, stop it entirely."
   (interactive "P")
   (cond
    (stop (org-timer-stop))
@@ -103,6 +131,7 @@ the region 0:00:00."
     (message "Timer continues at %s" (org-timer-value-string)))
    (t
     ;; pause timer
+    (run-hooks 'org-timer-pause-hook)
     (setq org-timer-pause-time (current-time))
     (org-timer-set-mode-line 'pause)
     (message "Timer paused at %s" (org-timer-value-string)))))
@@ -110,29 +139,39 @@ the region 0:00:00."
 (defun org-timer-stop ()
   "Stop the relative timer."
   (interactive)
+  (run-hooks 'org-timer-stop-hook)
   (setq org-timer-start-time nil
        org-timer-pause-time nil)
   (org-timer-set-mode-line 'off))
 
 ;;;###autoload
-(defun org-timer (&optional restart)
+(defun org-timer (&optional restart no-insert-p)
   "Insert a H:MM:SS string from the timer into the buffer.
 The first time this command is used, the timer is started.  When used with
-a `C-u' prefix, force restarting the timer.
-When used with a double prefix arg `C-u C-u', change all the timer string
+a \\[universal-argument] prefix, force restarting the timer.
+When used with a double prefix argument \\[universal-argument], change all the timer string
 in the region by a fixed amount.  This can be used to recalibrate a timer
-that was not started at the correct moment."
+that was not started at the correct moment.
+
+If NO-INSERT-P is non-nil, return the string instead of inserting
+it in the buffer."
   (interactive "P")
-  (if (equal restart '(4)) (org-timer-start))
-  (or org-timer-start-time (org-timer-start))
-  (insert (org-timer-value-string)))
+  (when (or (equal restart '(4)) (not org-timer-start-time))
+    (org-timer-start))
+  (if no-insert-p
+      (org-timer-value-string)
+    (insert (org-timer-value-string))))
 
 (defun org-timer-value-string ()
   (format org-timer-format (org-timer-secs-to-hms (floor (org-timer-seconds)))))
 
+(defvar org-timer-timer-is-countdown nil)
 (defun org-timer-seconds ()
-  (- (org-float-time (or org-timer-pause-time (current-time)))
-     (org-float-time org-timer-start-time)))
+  (if org-timer-timer-is-countdown
+      (- (org-float-time org-timer-start-time)
+        (org-float-time (current-time)))
+    (- (org-float-time (or org-timer-pause-time (current-time)))
+       (org-float-time org-timer-start-time))))
 
 ;;;###autoload
 (defun org-timer-change-times-in-region (beg end delta)
@@ -164,19 +203,22 @@ that was not started at the correct moment."
 (defun org-timer-item (&optional arg)
   "Insert a description-type item with the current timer value."
   (interactive "P")
-  (let ((ind 0))
-    (save-excursion
-      (skip-chars-backward " \n\t")
-      (condition-case nil
-         (progn
-           (org-beginning-of-item)
-           (setq ind (org-get-indentation)))
-       (error nil)))
-    (or (bolp) (newline))
-    (org-indent-line-to ind)
-    (insert "- ")
-    (org-timer (if arg '(4)))
-    (insert ":: ")))
+  (cond
+   ;; In a timer list, insert with `org-list-insert-item-generic'.
+   ((and (org-in-item-p)
+        (save-excursion (org-beginning-of-item) (org-at-item-timer-p)))
+    (org-list-insert-item-generic
+     (point) nil (concat (org-timer (when arg '(4)) t) ":: ")))
+   ;; In a list of another type, don't break anything: throw an error.
+   ((org-in-item-p)
+    (error "This is not a timer list"))
+   ;; Else, insert the timer correctly indented at bol.
+   (t
+    (beginning-of-line)
+    (org-indent-line-function)
+    (insert  "- ")
+    (org-timer (when arg '(4)))
+    (insert ":: "))))
 
 (defun org-timer-fix-incomplete (hms)
   "If hms is a H:MM:SS string with missing hour or hour and minute, fix it."
@@ -254,45 +296,59 @@ VALUE can be `on', `off', or `pause'."
          (concat " <" (substring (org-timer-value-string) 0 -1) ">"))
     (force-mode-line-update)))
 
-(defvar org-timer-timer1 nil)
-(defvar org-timer-timer2 nil)
-(defvar org-timer-timer3 nil)
-(defvar org-timer-last-timer nil)
-
-(defun org-timer-cancel-timers ()
-  "Reset all timers."
+(defvar org-timer-current-timer nil)
+(defun org-timer-cancel-timer ()
+  "Cancel the current timer."
   (interactive)
-  (mapc (lambda(timer)
-         (when (eval timer)
-           (cancel-timer timer)
-           (setq timer nil)))
-       '(org-timer-timer1
-         org-timer-timer2
-         org-timer-timer3))
-  (message "All timers reset"))
+  (when (eval org-timer-current-timer)
+    (run-hooks 'org-timer-cancel-hook)
+    (cancel-timer org-timer-current-timer)
+    (setq org-timer-current-timer nil)
+    (setq org-timer-timer-is-countdown nil)
+    (org-timer-set-mode-line 'off))
+  (message "Last timer canceled"))
 
 (defun org-timer-show-remaining-time ()
   "Display the remaining time before the timer ends."
   (interactive)
   (require 'time)
-  (if (and (not org-timer-timer1)
-          (not org-timer-timer2)
-          (not org-timer-timer3))
+  (if (not org-timer-current-timer)
       (message "No timer set")
     (let* ((rtime (decode-time
-                  (time-subtract (timer--time org-timer-last-timer)
+                  (time-subtract (timer--time org-timer-current-timer)
                                  (current-time))))
           (rsecs (nth 0 rtime))
           (rmins (nth 1 rtime)))
-      (message "%d minutes %d seconds left before next time out"
+      (message "%d minute(s) %d seconds left before next time out"
               rmins rsecs))))
 
 ;;;###autoload
-(defun org-timer-set-timer (minutes)
-  "Set a timer."
-  (interactive "sTime out in (min)? ")
-  (if (not (string-match "[0-9]+" minutes))
-      (org-timer-show-remaining-time)
+(defun org-timer-set-timer (&optional opt)
+  "Prompt for a duration and set a timer.
+
+If `org-timer-default-timer' is not zero, suggest this value as
+the default duration for the timer.  If a timer is already set,
+prompt the user if she wants to replace it.
+
+Called with a numeric prefix argument, use this numeric value as
+the duration of the timer.
+
+Called with a `C-u' prefix arguments, use `org-timer-default-timer'
+without prompting the user for a duration.
+
+With two `C-u' prefix arguments, use `org-timer-default-timer'
+without prompting the user for a duration and automatically
+replace any running timer."
+  (interactive "P")
+  (let ((minutes (or (and (numberp opt) (number-to-string opt))
+                    (and (listp opt) (not (null opt))
+                         (number-to-string org-timer-default-timer))
+                    (read-from-minibuffer
+                     "How many minutes left? "
+                     (if (not (eq org-timer-default-timer 0))
+                         (number-to-string org-timer-default-timer))))))
+    (if (not (string-match "[0-9]+" minutes))
+       (org-timer-show-remaining-time)
     (let* ((mins (string-to-number (match-string 0 minutes)))
           (secs (* mins 60))
           (hl (cond
@@ -306,21 +362,35 @@ VALUE can be `on', `off', or `pause'."
                     (widen)
                     (goto-char pos)
                     (org-show-entry)
-                    (org-get-heading))))
+                    (or (ignore-errors (org-get-heading))
+                        (concat "File:" (file-name-nondirectory (buffer-file-name)))))))
                ((eq major-mode 'org-mode)
-                (org-get-heading))
+                (or (ignore-errors (org-get-heading))
+                    (concat "File:" (file-name-nondirectory (buffer-file-name)))))
                (t (error "Not in an Org buffer"))))
           timer-set)
-      (mapcar (lambda(timer)
-               (when (not (or (eval timer) timer-set))
-                 (setq timer-set t)
-                 (setq org-timer-last-timer
-                       (run-with-timer
-                       secs nil 'org-notify (format "%s: time out" hl) t))
-                 (set timer org-timer-last-timer)))
-             '(org-timer-timer1
-               org-timer-timer2
-               org-timer-timer3)))))
+      (if (or (and org-timer-current-timer
+                  (or (equal opt '(16))
+                      (y-or-n-p "Replace current timer? ")))
+             (not org-timer-current-timer))
+         (progn
+           (require 'org-clock)
+           (when org-timer-current-timer
+             (cancel-timer org-timer-current-timer))
+           (setq org-timer-current-timer
+                 (run-with-timer
+                  secs nil `(lambda ()
+                              (setq org-timer-current-timer nil)
+                              (org-notify ,(format "%s: time out" hl) t)
+                              (setq org-timer-timer-is-countdown nil)
+                              (org-timer-set-mode-line 'off)
+                              (run-hooks 'org-timer-done-hook))))
+           (run-hooks 'org-timer-set-hook)
+           (setq org-timer-timer-is-countdown t
+                 org-timer-start-time
+                 (time-add (current-time) (seconds-to-time (* mins 60))))
+           (org-timer-set-mode-line 'on))
+       (message "No timer set"))))))
 
 (provide 'org-timer)