summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-10-16 22:28:15 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-10-17 03:51:32 -0400
commitd3f994e01b3734822180879c5233899eda0e013b (patch)
tree6d78f4654e654eaa797f4769730483ef791bbd1c
parentdefe6c383819af743bc4aba985111a8a3823c1b7 (diff)
downloadorg-mode-d3f994e01b3734822180879c5233899eda0e013b.tar.gz
Added functions for determining the user's idle time
(org-clock-idle-time): New user customizable option for detecting whether the user has left a clock idle. Note: it is only used in this commit to test whether it's worthwhile to check OS X to get the Mac user's current idle time. If the Emacs idle time is less than the value, the user hasn't been away long enough to be worth checking (a more expensive test than just getting Emacs idle time). (org-user-idle-seconds, org-mac-idle-seconds) (org-emacs-idle-seconds): This three functions, in conjunction with the user customization variable `org-clock-idle-time', return the number of seconds (as a floating point) that the user has been away from their Emacs (or, if running on OS X, their computer).
-rwxr-xr-xlisp/ChangeLog13
-rw-r--r--lisp/org-clock.el31
2 files changed, 44 insertions, 0 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 59578ad..c6db905 100755
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,18 @@
2009-10-17 John Wiegley <johnw@newartisans.com>
+ * org-clock.el (org-clock-idle-time): New user customizable option
+ for detecting whether the user has left a clock idle. Note: it is
+ only used in this commit to test whether it's worthwhile to check
+ OS X to get the Mac user's current idle time. If the Emacs idle
+ time is less than the value, the user hasn't been away long enough
+ to be worth checking (a more expensive test than just getting
+ Emacs idle time).
+ (org-user-idle-seconds, org-mac-idle-seconds)
+ (org-emacs-idle-seconds): This three functions, in conjunction
+ with the user customization variable `org-clock-idle-time', return
+ the number of seconds (as a floating point) that the user has been
+ away from their Emacs (or, if running on OS X, their computer).
+
* org-clock.el (org-find-open-clocks): New function that returns a
list of all open clocks in the given FILE. Note that each clock
it returns is a cons cell of the format (MARKER . START-TIME).
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 5067d2f..057de9d 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -206,6 +206,13 @@ string as argument."
:group 'org-clock
:type 'plist)
+(defcustom org-clock-idle-time nil
+ "When non-nil, resolve open clocks if the user is idle more than X minutes."
+ :group 'org-clock
+ :type '(choice
+ (const :tag "Never" nil)
+ (integer :tag "After N minutes")))
+
(defvar org-clock-in-prepare-hook nil
"Hook run when preparing the clock.
@@ -568,6 +575,30 @@ If necessary, clock-out of the currently active clock."
(org-clock-cancel)))
(setcar clock temp)))
+(defun org-emacs-idle-seconds ()
+ "Return the current Emacs idle time in seconds, or nil if not idle."
+ (let ((idle-time (current-idle-time)))
+ (if idle-time
+ (time-to-seconds idle-time)
+ 0)))
+
+(defun org-mac-idle-seconds ()
+ "Return the current Mac idle time in seconds"
+ (string-to-number (shell-command-to-string "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle; last}'")))
+
+(defun org-user-idle-seconds ()
+ "Return the number of seconds the user has been idle for.
+This routine returns a floating point number."
+ (if (eq system-type 'darwin)
+ (let ((emacs-idle (org-emacs-idle-seconds)))
+ ;; If Emacs has been idle for longer than the user's
+ ;; `org-clock-idle-time' value, check whether the whole system has
+ ;; really been idle for that long.
+ (if (> emacs-idle (* 60 org-clock-idle-time))
+ (min emacs-idle (org-mac-idle-seconds))
+ emacs-idle))
+ (org-emacs-idle-seconds)))
+
(defun org-clock-in (&optional select)
"Start the clock on the current item.
If necessary, clock-out of the currently active clock.