Subject: bug#35418: [PATCH] Don't poll auto-revert files that use notification

From: Mattias Engdegård <[email-masked]>
Date: Tue, 14 May 2019 14:41:14 +0200

mån 2019-05-13 klockan 15:35 +0200 skrev Michael Albinus:
> 
> Do we want to say that this is related to a major mode in general?
> That
> means, that the mode function shall set this variable. And do we want
> to
> mention dired-mode as example?

Good point, paragraph changed.

> Same remark. Mention major mode dependency.

Done.

Updated patch attached.

From efda86ef03befb73356c7b23ee21e7d9efd8b181 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <[email-masked]>
Date: Wed, 8 May 2019 00:02:59 +0200
Subject: [PATCH] Don't use file notification on non-file buffers

Most non-file buffers aren't served by file notification in
auto-revert mode; typically, they need to be polled, like the Buffer List.
With `auto-revert-avoid-polling', setting a useless notification means
that such buffers may never be updated at all (bug#35418).

Non-file buffers can explicitly declare that notification on their
default-directory is sufficient to know when updates are required
by setting the new variable `buffer-auto-revert-by-notification' to
a non-nil value.

* lisp/autorevert.el (auto-revert-buffers):
Modify condition for using notification.
* lisp/files.el (buffer-auto-revert-by-notification): New variable.
* lisp/dired.el (dired-mode): Set buffer-auto-revert-by-notification.
* doc/emacs/arevert-xtra.texi (Non-File Buffers): Document new variable.
* etc/NEWS (Changes in Specialized Modes and Packages): Describe new variable.
---
 doc/emacs/arevert-xtra.texi |  8 ++++++++
 etc/NEWS                    |  7 +++++++
 lisp/autorevert.el          |  6 +++++-
 lisp/dired.el               |  1 +
 lisp/files.el               | 10 ++++++++++
 5 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/doc/emacs/arevert-xtra.texi b/doc/emacs/arevert-xtra.texi
index 9e01a10ace..37e2f9e581 100644
--- a/doc/emacs/arevert-xtra.texi
+++ b/doc/emacs/arevert-xtra.texi
@@ -35,6 +35,14 @@ Non-File Buffers
 messages while reverting, even when @code{auto-revert-verbose} is
 non-@code{nil}.
 
+@vindex buffer-auto-revert-by-notification
+Some non-file buffers can be updated reliably by file notification on
+their default directory; Dired buffers is an example.  The major mode
+can indicate this by setting @code{buffer-auto-revert-by-notification}
+to a non-@code{nil} value in that buffer, allowing Auto Revert to
+avoid periodic polling.  Such notification does not include changes to
+files in that directory, only to the directory itself.
+
 The details depend on the particular types of buffers and are
 explained in the corresponding sections.
 
diff --git a/etc/NEWS b/etc/NEWS
index 43ad8be1cc..57ef76614f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1428,6 +1428,13 @@ of an idle Emacs, but may fail on some network file systems; set
 notification is not supported.  The new variable currently has no
 effect in 'global-auto-revert-mode'.  The default value is nil.
 
+*** New variable 'buffer-auto-revert-by-notification'
+A major mode can declare that notification on the buffer's default
+directory is sufficient to know when updates are required, by setting
+the new variable `buffer-auto-revert-by-notification' to a non-nil
+value.  Auto Revert mode can use this information to avoid polling the
+buffer periodically when 'auto-revert-avoid-polling' is non-nil.
+
 
 * New Modes and Packages in Emacs 27.1
 
diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 7cd5e7ee8b..197a2bf157 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -858,8 +858,12 @@ auto-revert-buffers
                   (auto-revert-remove-current-buffer))
               (when (auto-revert-active-p)
                 ;; Enable file notification.
+                ;; Don't bother creating a notifier for non-file buffers
+                ;; unless it explicitly indicates that this works.
                 (when (and auto-revert-use-notify
-                           (not auto-revert-notify-watch-descriptor))
+                           (not auto-revert-notify-watch-descriptor)
+                           (or buffer-file-name
+                               buffer-auto-revert-by-notification))
                   (auto-revert-notify-add-watch))
                 (auto-revert-handler)))))
 	(setq bufs (cdr bufs)))
diff --git a/lisp/dired.el b/lisp/dired.el
index 385126514b..ea1943de1d 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2148,6 +2148,7 @@ dired-mode
     (setq buffer-invisibility-spec (list t)))
   (setq-local revert-buffer-function #'dired-revert)
   (setq-local buffer-stale-function #'dired-buffer-stale-p)
+  (setq-local buffer-auto-revert-by-notification t)
   (setq-local page-delimiter "\n\n")
   (setq-local dired-directory (or dirname default-directory))
   ;; list-buffers uses this to display the dir being edited in this buffer.
diff --git a/lisp/files.el b/lisp/files.el
index 8477c227bc..becb5aab6f 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -5843,6 +5843,16 @@ buffer-stale-function
 For more information on how this variable is used by Auto Revert mode,
 see Info node `(emacs)Supporting additional buffers'.")
 
+(defvar-local buffer-auto-revert-by-notification nil
+  "Whether a buffer can rely on notification in Auto-Revert mode.
+If non-nil, monitoring changes to the directory of the current
+buffer is sufficient for knowing when that buffer needs to be
+updated in Auto Revert Mode.  Such notification does not include
+changes to files in that directory, only to the directory itself.
+
+This variable only applies to buffers where `buffer-file-name' is
+nil; other buffers are tracked by their files.")
+
 (defvar before-revert-hook nil
   "Normal hook for `revert-buffer' to run before reverting.
 The function `revert-buffer--default' runs this.
-- 
2.20.1