all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@jurta.org>
To: 15331@debbugs.gnu.org
Subject: bug#15331: Idle timer for desktop
Date: Tue, 10 Sep 2013 23:57:31 +0300	[thread overview]
Message-ID: <87hadsgzpj.fsf@mail.jurta.org> (raw)

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






             reply	other threads:[~2013-09-10 20:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-10 20:57 Juri Linkov [this message]
2013-09-11 17:23 ` bug#15331: desktop should auto-save on idle time Sam Steingold
2013-09-11 19:45   ` Juri Linkov
2013-09-12  0:56     ` Juanma Barranquero
2013-09-12 20:58       ` Juri Linkov
2013-09-16 21:18         ` Juri Linkov
2013-12-16 21:49   ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87hadsgzpj.fsf@mail.jurta.org \
    --to=juri@jurta.org \
    --cc=15331@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.