]> code.delx.au - gnu-emacs/blobdiff - lisp/org/org-timer.el
Fix typos.
[gnu-emacs] / lisp / org / org-timer.el
index 91664eb7b1af8d055449a554e5ca5cc1e69826d5..b8fbc4ff3886c55913e86d274d31ef8f4ec70b8c 100644 (file)
@@ -1,11 +1,11 @@
 ;;; org-timer.el --- The relative timer code for Org-mode
 
-;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
+;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
 
 ;; Author: Carsten Dominik <carsten at orgmode dot org>
 ;; Keywords: outlines, hypermedia, calendar, wp
 ;; Homepage: http://orgmode.org
-;; Version: 6.35i
+;; Version: 7.7
 ;;
 ;; 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,12 @@ 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.")
 
@@ -57,6 +65,9 @@ the value of the relative timer."
 (defvar org-timer-pause-hook nil
   "Hook run before relative timer is paused.")
 
+(defvar org-timer-continue-hook nil
+ "Hook run after relative timer is continued.")
+
 (defvar org-timer-set-hook nil
   "Hook run after countdown timer is set.")
 
@@ -96,7 +107,7 @@ 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)
@@ -104,7 +115,8 @@ the region 0:00:00."
       (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))
@@ -119,6 +131,7 @@ the region 0:00:00."
               (org-float-time org-timer-start-time))))
          org-timer-pause-time nil)
     (org-timer-set-mode-line 'on)
+    (run-hooks 'org-timer-continue-hook)
     (message "Timer continues at %s" (org-timer-value-string)))
    (t
     ;; pause timer
@@ -136,24 +149,33 @@ the region 0:00:00."
   (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)
@@ -185,19 +207,27 @@ 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 ":: ")))
+  (let ((itemp (org-in-item-p)) (pos (point)))
+    (cond
+     ;; In a timer list, insert with `org-list-insert-item',
+     ;; then fix the list.
+     ((and itemp (goto-char itemp) (org-at-item-timer-p))
+      (let* ((struct (org-list-struct))
+            (prevs (org-list-prevs-alist struct))
+            (s (concat (org-timer (when arg '(4)) t) ":: ")))
+       (setq struct (org-list-insert-item pos struct prevs nil s))
+       (org-list-write-struct struct (org-list-parents-alist struct))
+       (looking-at org-list-full-item-re)
+       (goto-char (match-end 0))))
+     ;; In a list of another type, don't break anything: throw an error.
+     (itemp (goto-char pos) (error "This is not a timer list"))
+     ;; Else, start a new list.
+     (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."
@@ -282,7 +312,9 @@ VALUE can be `on', `off', or `pause'."
   (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-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 ()
@@ -300,11 +332,32 @@ VALUE can be `on', `off', or `pause'."
               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
@@ -318,23 +371,38 @@ VALUE can be `on', `off', or `pause'."
                     (widen)
                     (goto-char pos)
                     (org-show-entry)
-                    (org-get-heading))))
-               ((eq major-mode 'org-mode)
-                (org-get-heading))
+                    (or (ignore-errors (org-get-heading))
+                        (concat "File:" (file-name-nondirectory (buffer-file-name)))))))
+               ((org-mode-p)
+                (or (ignore-errors (org-get-heading))
+                    (concat "File:" (file-name-nondirectory (buffer-file-name)))))
                (t (error "Not in an Org buffer"))))
           timer-set)
-      (if org-timer-current-timer
-         (error "You cannot run several timers at the same time")
-       (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)
-                          (run-hooks 'org-timer-done-hook))))
-       (run-hooks 'org-timer-set-hook)))))
+      (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)
 
-;; arch-tag: 97538f8c-3871-4509-8f23-1e7b3ff3d107
+
 
 ;;; org-timer.el ends here