Subject: bug#15331: closed (bug#15331: desktop should auto-save on idle time)

From: [email-masked] (GNU bug Tracking System)
Date: Mon, 16 Dec 2013 21:50:05 +0000

Your bug report

#15331: Idle timer for desktop

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
15331: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15331
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: bug#15331: desktop should auto-save on idle time
From: Juri Linkov <[email-masked]>
To: Sam Steingold <[email-masked]>
Cc: [email-masked]
Date: Mon, 16 Dec 2013 23:49:06 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

> I think the proposed solution is overly complicated.
> I think the better solution is to use idle timer instead and default
> `desktop-auto-save-timeout' to `auto-save-timeout' (i.e., enable by default).

I like the simplicity too, and since there were less
requests for the real-time timer than for the idle timer,
I just replaced the former with the latter.

Subject: Idle timer for desktop
From: Juri Linkov <[email-masked]>
To: [email-masked]
Date: Tue, 10 Sep 2013 23:57:31 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

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