Subject: Idle timer for desktop

From: Juri Linkov <[email-masked]>
Date: Tue, 10 Sep 2013 23:57:31 +0300

Tags: patch

It's very distracting when periodic desktop saves interrupt the
active editing for approximately 2 seconds to save the desktop.
This is because `desktop-auto-save-timeout' ignores idleness when
better to save the desktop in convenient time without distraction.

This means that another idle timer is necessary in addition
to the idleness-ignoring timer.  As were discussed some time ago
on emacs-devel both timers are needed.  This patch implements
proper interaction between these two timers.

=== modified file 'lisp/desktop.el'
--- lisp/desktop.el	2013-07-01 03:29:46 +0000
+++ lisp/desktop.el	2013-09-10 20:50:10 +0000
@@ -191,7 +191,14 @@ (defcustom desktop-save 'ask-if-new
 
 (defcustom desktop-auto-save-timeout nil
   "Number of seconds between auto-saves of the desktop.
-Zero or nil means disable timer-based auto-saving."
+Zero or nil means disable timer-based auto-saving.
+If `desktop-auto-save-idle-timeout' is non-nil then the desktop is
+not saved immediately after the timeout of `desktop-auto-save-timeout',
+but another idle timer is set that saves the desktop after the number
+of idle seconds defined by `desktop-auto-save-idle-timeout'.
+Thus the total timeout between desktop auto-saves is the sum
+of seconds of `desktop-auto-save-timeout' plus idle seconds
+of `desktop-auto-save-idle-timeout'."
   :type '(choice (const :tag "Off" nil)
                  (integer :tag "Seconds"))
   :set (lambda (symbol value)
@@ -202,6 +209,24 @@ (defcustom desktop-auto-save-timeout nil
   :group 'desktop
   :version "24.4")
 
+(defcustom desktop-auto-save-idle-timeout nil
+  "Number of idle seconds between auto-saves of the desktop.
+Zero or nil means disable timer-based idle auto-saving.
+If `desktop-auto-save-timeout' is non-nil then an idle timer
+is set after the timeout defined by `desktop-auto-save-timeout'.
+Thus the total timeout between desktop auto-saves is the sum
+of seconds of `desktop-auto-save-timeout' plus idle seconds
+of `desktop-auto-save-idle-timeout'."
+  :type '(choice (const :tag "Off" nil)
+                 (integer :tag "Idle Seconds"))
+  :set (lambda (symbol value)
+         (set-default symbol value)
+         (condition-case nil
+	     (desktop-auto-save-set-timer)
+	   (error nil)))
+  :group 'desktop
+  :version "24.4")
+
 (defcustom desktop-load-locked-desktop 'ask
   "Specifies whether the desktop should be loaded if locked.
 Possible values are:
@@ -1219,8 +1244,10 @@ (defun desktop-auto-save ()
   "Save the desktop periodically.
 Called by the timer created in `desktop-auto-save-set-timer'."
   (when (and desktop-save-mode
-	     (integerp desktop-auto-save-timeout)
-	     (> desktop-auto-save-timeout 0)
+	     (or (and (integerp desktop-auto-save-timeout)
+		      (> desktop-auto-save-timeout 0))
+		 (and (integerp desktop-auto-save-idle-timeout)
+		      (> desktop-auto-save-idle-timeout 0)))
 	     ;; Avoid desktop saving during lazy loading.
 	     (not desktop-lazy-timer)
 	     ;; Save only to own desktop file.
@@ -1230,17 +1257,46 @@ (defun desktop-auto-save ()
   (desktop-auto-save-set-timer))
 
 (defun desktop-auto-save-set-timer ()
-  "Reset the auto-save timer.
+  "Set the auto-save timer.
 Cancel any previous timer.  When `desktop-auto-save-timeout' is a positive
-integer, start a new timer to call `desktop-auto-save' in that many seconds."
+integer, start a new timer to call `desktop-auto-save' in that many seconds.
+When `desktop-auto-save-idle-timeout' is a positive integer,
+then start a new timer to call `desktop-auto-save-set-idle-timer'."
+  (when desktop-auto-save-timer
+    (cancel-timer desktop-auto-save-timer)
+    (setq desktop-auto-save-timer nil))
+  (if (and (integerp desktop-auto-save-timeout)
+	   (> desktop-auto-save-timeout 0))
+      (if (and (integerp desktop-auto-save-idle-timeout)
+	       (> desktop-auto-save-idle-timeout 0))
+	  (setq desktop-auto-save-timer
+		(run-with-timer desktop-auto-save-timeout nil
+				'desktop-auto-save-set-idle-timer))
+	(setq desktop-auto-save-timer
+	      (run-with-timer desktop-auto-save-timeout nil
+			      'desktop-auto-save)))
+    (if (and (integerp desktop-auto-save-idle-timeout)
+	     (> desktop-auto-save-idle-timeout 0))
+	(desktop-auto-save-set-idle-timer))))
+
+(defun desktop-auto-save-set-idle-timer ()
+  "Set the auto-save idle timer.
+Cancel any previous timer.  When `desktop-auto-save-idle-timeout' is a positive
+integer, start a new idle timer to call `desktop-auto-save' in that number
+of idle seconds."
   (when desktop-auto-save-timer
     (cancel-timer desktop-auto-save-timer)
     (setq desktop-auto-save-timer nil))
-  (when (and (integerp desktop-auto-save-timeout)
-	     (> desktop-auto-save-timeout 0))
+  (when (and (integerp desktop-auto-save-idle-timeout)
+	     (> desktop-auto-save-idle-timeout 0))
     (setq desktop-auto-save-timer
-	  (run-with-timer desktop-auto-save-timeout nil
-			  'desktop-auto-save))))
+	  (run-with-idle-timer
+	   ;; Compute an idle time desktop-auto-save-idle-timeout
+	   ;; more than the current value.
+	   (time-add (or (current-idle-time) '(0 0 0 0))
+		     (seconds-to-time desktop-auto-save-idle-timeout))
+	   nil
+	   'desktop-auto-save))))
 
 ;; ----------------------------------------------------------------------------
 ;;;###autoload