From 1943220d1b7ed28b2708c86acfe5493d5c6e3089 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Wed, 8 May 2019 00:02:59 +0200 Subject: [PATCH] Don't use file notification on non-file buffers Allow non-file buffers to declare that notification on their default-directory is sufficient to know when auto-revert updates are required by setting the new variable `buffer-auto-revert-by-notification' to non-nil. If nil, the default, then auto-revert will poll those buffers instead. (bug#35418). Currently, only Dired sets that variable. * 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 9e01a10ace9..37e2f9e5818 100644 --- a/doc/emacs/arevert-xtra.texi +++ b/doc/emacs/arevert-xtra.texi @@ -35,6 +35,14 @@ the Buffer Menu.) In this case, Auto Revert does not print any 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 8c059157ba5..b4aa8d98ffa 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1446,6 +1446,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 7cd5e7ee8bf..197a2bf1578 100644 --- a/lisp/autorevert.el +++ b/lisp/autorevert.el @@ -858,8 +858,12 @@ the timer when no buffers need to be checked." (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 385126514b3..ea1943de1db 100644 --- a/lisp/dired.el +++ b/lisp/dired.el @@ -2148,6 +2148,7 @@ Keybindings: (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 8fa7f16de01..1dec0ed7ca9 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5890,6 +5890,16 @@ This should not be relied upon. 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.39.2