Subject: bug#43412: [FEATURE] autorevert-only-if-visible [PATCH]

From: Boruch Baum <[email-masked]>
Date: Tue, 15 Sep 2020 00:07:28 -0400

The attached proposed patch adds a defcustom and makes a change to two
existing functions in order to allow a user the option to only have
autorevert mode operate on visible buffers. The change to function
'after-find-files' may also fix a bug (or change behavior) in that
sometimes when being prompted to manually revert a buffer that had been
in auto-revert-mode, the auto-revert-mode status would be lost, ie. set
to nil.

Use of this feature:

1) Reduces CPU workload when using autorevert

2) Reduces messages when using `auto-revert-verbose'

Possible disadvantage:

1) When making a buffer visible, it might take as long as
   'auto-revert-interval' seconds for any accumulated changes to
   auto-revert. The default for that value is five seconds.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0
diff --git a/autorevert.el b/autorevert.el
index 7b83026..ace010e 100644
--- a/autorevert.el
+++ b/autorevert.el
@@ -302,6 +302,12 @@ You should set this variable through Custom."
   :type 'regexp
   :version "24.4")

+(defcustom auto-revert-only-if-visible nil
+  "If non-nil, suppress Auto-Revert Mode when a buffer isn't visible."
+  :group 'auto-revert
+  :type 'boolean
+  :version "28")
+
 ;; Internal variables:

 (defvar auto-revert-buffer-list ()
@@ -686,8 +692,14 @@ This is an internal function used by Auto-Revert Mode."
                               #'buffer-stale--default-function)
                           t))))
          eob eoblist)
-    (setq auto-revert-notify-modified-p nil)
-    (when revert
+    (when (and revert
+               (or (not auto-revert-only-if-visible)
+                   (member buffer
+                     (mapcar 'window-buffer
+                       (get-buffer-window-list
+                         nil nil
+                         (if (display-graphic-p) 'visible (window-normalize-frame nil)))))))
+      (setq auto-revert-notify-modified-p nil)
       (when (and auto-revert-verbose
                  (not (eq revert 'fast)))
         (message "Reverting buffer `%s'." (buffer-name)))
diff --git a/files.el b/files.el
index e6629d2..2cdf451 100644
--- a/files.el
+++ b/files.el
@@ -2570,7 +2570,10 @@ unless NOMODES is non-nil."
   (unless nomodes
     (when (and view-read-only view-mode)
       (view-mode -1))
-    (normal-mode t)
+    (let ((auto-revert auto-revert-mode))
+      (normal-mode t)
+      (when auto-revert
+        (auto-revert-mode 1)))
     ;; If requested, add a newline at the end of the file.
     (and (memq require-final-newline '(visit visit-save))
 	 (> (point-max) (point-min))
diff --git a/NEWS b/NEWS
index 4076630..579f1f9 100644
--- a/NEWS
+++ b/NEWS
@@ -1157,6 +1157,12 @@ type symbols.
 messages, contain the error name of that message now.  They can be
 made visible by setting user variable 'dbus-show-dbus-errors' to
 non-nil, even if protected by 'dbus-ignore-errors' otherwise.
+** Autorevert
+
++++
+*** autorevert can be restricted to act only when a buffer is visible
+
+Enable the feature using defcustom variable 'auto-revert-only-if-visible'.

 
 * New Modes and Packages in Emacs 28.1