unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Boruch Baum <boruch_baum@gmx.com>
To: 43714@debbugs.gnu.org
Subject: bug#43714: 28.1: auto-revert code improvements [PATCH]
Date: Wed, 30 Sep 2020 02:26:33 -0400	[thread overview]
Message-ID: <20200930062633.n3ntw2lmmantsx47@E15-2016.optimum.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 882 bytes --]

In pursuing a 'best' solution for bug-report#43412, I've taken the
plunge and gotten an emacs 28 snapshot installed locally, and in
examining the code, saw things that didn't make sense to me or were
unnecessary or could just do with better readability. My testing
indicates no harm done by the changes, but they should be peer-reviewed
and undergo further testing. Nothing in the attached patch is aimed at
changing functionality, so you may want to reject it on the basis of "if
it ain't broke, don't fix it", but it does improve the code.

Just as a 'for example': I started on this because I saw that the code
was saving match data, which had me on a wild goose chase hunting for
why and where it was necessary - I found nothing, and removing the code
doesn't seem to have ill effect.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0

[-- Attachment #2: auto-revert-28-cosmetic.patch --]
[-- Type: text/x-diff, Size: 6203 bytes --]

diff --git a/autorevert.el b/autorevert.el
index 011febf..de4407b 100644
--- a/autorevert.el
+++ b/autorevert.el
@@ -869,6 +869,62 @@ This is an internal function used by Auto-Revert Mode."
       (restore-buffer-modified-p modified)))
   (set-visited-file-modtime))

+(defun auto-revert--buffer-candidates ()
+  "Return a prioritized list of buffers to maybe auto-revert.
+The differences between this return value and the reference
+variable `auto-revert-buffer-list'. include: 1) this has more
+entries when in global-auto-revert-mode; 2) this prioritizes
+buffers not reverted last time due to user interruption. "
+  (let (remaining new
+        (bufs (delq nil
+               ;; Buffers with remote contents shall be reverted only
+               ;; if the connection is established already.
+                    (mapcar
+                     (lambda (buf)
+                       (and (buffer-live-p buf)
+                            (with-current-buffer buf
+                              (and
+                               (or (not (file-remote-p default-directory))
+                                   (file-remote-p default-directory nil t))
+                                   buf))))
+                     (auto-revert--polled-buffers)))))
+    ;; Partition `bufs' into two halves depending on whether or not
+    ;; the buffers are in `auto-revert-remaining-buffers'.  The two
+    ;; halves are then re-joined with the "remaining" buffers at the
+    ;; head of the list.
+    (dolist (buf auto-revert-remaining-buffers)
+      (if (memq buf bufs)
+          (push buf remaining)))
+    (dolist (buf bufs)
+      (if (not (memq buf remaining))
+          (push buf new)))
+    (nreverse (nconc new remaining))))
+
+(defun auto-revert-buffer (buf)
+  "Revert a single buffer.
+
+This is performed as specified by Auto-Revert and Global
+Auto-Revert Modes."
+  (if (not (buffer-live-p buf))
+    (auto-revert-remove-current-buffer buf)
+   (with-current-buffer buf
+     ;; Test if someone has turned off Auto-Revert Mode
+     ;; in a non-standard way, for example by changing
+     ;; major mode.
+     (and (not auto-revert-mode)
+          (not auto-revert-tail-mode)
+          (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)
+                  (or buffer-file-name
+                      buffer-auto-revert-by-notification))
+         (auto-revert-notify-add-watch))
+       (auto-revert-handler)))))
+
 (defun auto-revert-buffers ()
   "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.

@@ -892,66 +948,17 @@ are checked first the next time this function is called.
 This function is also responsible for removing buffers no longer in
 Auto-Revert Mode from `auto-revert-buffer-list', and for canceling
 the timer when no buffers need to be checked."
-
-  (save-match-data
-    (let ((bufs (auto-revert--polled-buffers))
-	  remaining new)
-      ;; Buffers with remote contents shall be reverted only if the
-      ;; connection is established already.
-      (setq bufs (delq nil
-                       (mapcar
-                        (lambda (buf)
-                          (and (buffer-live-p buf)
-                               (with-current-buffer buf
-                                 (and
-                                  (or (not (file-remote-p default-directory))
-                                      (file-remote-p default-directory nil t))
-                                      buf))))
-                        bufs)))
-      ;; Partition `bufs' into two halves depending on whether or not
-      ;; the buffers are in `auto-revert-remaining-buffers'.  The two
-      ;; halves are then re-joined with the "remaining" buffers at the
-      ;; head of the list.
-      (dolist (buf auto-revert-remaining-buffers)
-	(if (memq buf bufs)
-	    (push buf remaining)))
-      (dolist (buf bufs)
-	(if (not (memq buf remaining))
-	    (push buf new)))
-      (setq bufs (nreverse (nconc new remaining)))
+    (let ((bufs (auto-revert--buffer-candidates)))
       (while (and bufs
 		  (not (and auto-revert-stop-on-user-input
 			    (input-pending-p))))
-	(let ((buf (car bufs)))
-          (if (not (buffer-live-p buf))
-              ;; Remove dead buffer from `auto-revert-buffer-list'.
-              (auto-revert-remove-current-buffer buf)
-            (with-current-buffer buf
-              ;; Test if someone has turned off Auto-Revert Mode
-              ;; in a non-standard way, for example by changing
-              ;; major mode.
-              (if (and (not auto-revert-mode)
-                       (not auto-revert-tail-mode)
-                       (memq buf auto-revert-buffer-list))
-                  (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)
-                           (or buffer-file-name
-                               buffer-auto-revert-by-notification))
-                  (auto-revert-notify-add-watch))
-                (auto-revert-handler)))))
-	(setq bufs (cdr bufs)))
+        (auto-revert-buffer (pop bufs)))
       (setq auto-revert-remaining-buffers bufs)
       ;; Check if we should cancel the timer.
       (unless (auto-revert--need-polling-p)
-        (if (timerp auto-revert-timer)
-            (cancel-timer auto-revert-timer))
-	(setq auto-revert-timer nil)))))
-
+        (when (timerp auto-revert-timer)
+          (cancel-timer auto-revert-timer))
+	(setq auto-revert-timer nil))))

 ;; The end:
 (provide 'autorevert)

             reply	other threads:[~2020-09-30  6:26 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-30  6:26 Boruch Baum [this message]
2020-09-30 13:48 ` bug#43714: 28.1: auto-revert code improvements [PATCH] Lars Ingebrigtsen
2020-09-30 14:25   ` Eli Zaretskii
2020-09-30 15:28     ` Boruch Baum
2020-09-30 16:03       ` Eli Zaretskii
2020-09-30 16:59         ` Boruch Baum
2020-09-30 17:01           ` Lars Ingebrigtsen
2020-09-30 17:13             ` Boruch Baum
2020-09-30 17:21     ` Michael Albinus
2020-09-30 15:09   ` Boruch Baum
2020-09-30 15:12     ` Lars Ingebrigtsen
2020-09-30 17:41     ` Glenn Morris
2020-09-30 18:33       ` Boruch Baum
2020-09-30 23:55         ` Lars Ingebrigtsen
2020-10-01  5:42           ` Boruch Baum
2020-09-30 16:51 ` bug#43714: [boruch_baum@gmx.com: Re: bug#43714: 28.1: auto-revert code improvements [PATCH]] Boruch Baum
2020-09-30 16:54   ` Lars Ingebrigtsen
2020-09-30 17:10     ` Boruch Baum

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=20200930062633.n3ntw2lmmantsx47@E15-2016.optimum.net \
    --to=boruch_baum@gmx.com \
    --cc=43714@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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).