Subject: bug#75626: 31.0.50; Dired misses or double-processes files when auto-revert-mode is enabled

From: Michael Albinus <[email-masked]>
Date: Sun, 02 Feb 2025 18:22:21 +0100

Tassilo Horn <[email-masked]> writes:

Hi Tassilo,

> That's the part I don't understand, yet.  Auto-reverts are skipped by
> auto-revert-handler now for buffers in inhibit-auto-revert-buffers.
> That's what I had expected and did in my previous inhibit-auto-revert
> patch, too.
>
> But my reading of the code is that the polling timer calls
>
>   auto-revert-buffers -> auto-revert-buffer -> auto-revert-handler
>
> where auto-revert-buffer now removes the inhibited buffer from
> auto-revert-buffer-list.  So in addition to skipping it in
> auto-revert-handler this time, the future polling rounds won't see it
> anymore, too, because auto-revert--buffer-candidates uses
> auto-revert--polled-buffers which is basically auto-revert-buffer-list +
> auto-revert-remaining-buffers, at least if global-auto-revert-mode isn't
> used.
>
> So when you say "we disable auto-reverting for a given time" where I
> assume "given time" is not "forever", what am I missing?  If anything, I
> would expect that auto-revert-buffer pushes the inhibited buffer onto
> auto-revert-remaining-buffers to prioritize it for next time.

Below is an updated patch for autorevert.el. I control this now via
auto-revert-active-p; no further need to check in auto-revert-buffer.

Could you, pls, test?

> Bye,
> Tassilo

Best regards, Michael.

diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 1dcfe8e911f..5acb1220471 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -774,9 +774,10 @@ auto-revert--end-lockout

 (defun auto-revert-active-p ()
   "Check if auto-revert is active in current buffer."
-  (or auto-revert-mode
-      auto-revert-tail-mode
-      auto-revert--global-mode))
+  (and (or auto-revert-mode
+           auto-revert-tail-mode
+           auto-revert--global-mode)
+       (not (memq (current-buffer) inhibit-auto-revert-buffers))))

 (defun auto-revert-handler ()
   "Revert current buffer, if appropriate.
@@ -798,14 +799,17 @@ auto-revert-handler
                                 (setq size
                                       (file-attribute-size
                                        (file-attributes buffer-file-name)))))
-                     (funcall (or buffer-stale-function
-                                  #'buffer-stale--default-function)
-                              t)))
+                     (and (not (memq (current-buffer)
+                                     inhibit-auto-revert-buffers))
+                          (funcall (or buffer-stale-function
+                                       #'buffer-stale--default-function)
+                                   t))))
             (and (or auto-revert-mode
                      global-auto-revert-non-file-buffers)
-                 (funcall (or buffer-stale-function
-                              #'buffer-stale--default-function)
-                          t))))
+                 (and (not (memq (current-buffer) inhibit-auto-revert-buffers))
+                      (funcall (or buffer-stale-function
+                                   #'buffer-stale--default-function)
+                               t)))))
          eob eoblist)
     (setq auto-revert-notify-modified-p nil
           auto-revert--last-time (current-time))
@@ -897,6 +901,31 @@ auto-revert--buffer-candidates
         (push buf new)))
     (nreverse (nconc new remaining))))

+;;;###autoload
+(progn
+  (defvar inhibit-auto-revert-buffers nil
+    "A list of buffers with suppressed auto-revert.")
+
+  (defmacro inhibit-auto-revert (&rest body)
+    "Deactivate auto-reverting of current buffer temporarily.
+Run BODY."
+    (declare (indent 0) (debug ((form body) body)))
+    `(progn
+       ;; Cleanup.
+       (dolist (buf inhibit-auto-revert-buffers)
+         (unless (buffer-live-p buf)
+           (setq inhibit-auto-revert-buffers
+                 (delq buf inhibit-auto-revert-buffers))))
+       (let ((buf (and (not (memq (current-buffer) inhibit-auto-revert-buffers))
+                       (current-buffer))))
+         (unwind-protect
+             (progn
+               (when buf (add-to-list 'inhibit-auto-revert-buffers buf))
+               ,@body)
+           (when buf
+             (setq inhibit-auto-revert-buffers
+                   (delq buf inhibit-auto-revert-buffers))))))))
+
 (defun auto-revert-buffer (buf)
   "Revert a single buffer BUF.