Subject: bug#71466: 30.0.50; Buffer-menu-group-by non-nil resets point when Buffer List is reverted

From: Eshel Yaron <[email-masked]>
Date: Wed, 26 Jun 2024 18:56:53 +0200

Eli Zaretskii <[email-masked]> writes:

>> Date: Wed, 26 Jun 2024 02:14:45 +0300
>> Cc: [email-masked], [email-masked], [email-masked]
>> From: Dmitry Gutov <[email-masked]>
>> 
>> >> That is my opinion as well: better obsolete it this way.
>> > Why obsolete it at all?  If we use an alias without obsoleting, I
>> > think everyone wins.  No?
>> 
>> Well, we normally obsolete functions that aren't in use anymore, nor 
>> recommended for third parties. Right?
>> 
>> We can stop from obsoleting it now (just make an alias), but add a 
>> comment to do that in the next Emacs release. How about that?
>
> Better.

All right, so here's an updated patch.  This is based on emacs-30, let
me know if this should go to master instead.

>From 0b6756fd31b071a2c88f4731d4dc3a74fcfac5f8 Mon Sep 17 00:00:00 2001
From: Eshel Yaron <[email-masked]>
Date: Wed, 26 Jun 2024 18:51:32 +0200
Subject: [PATCH] Use 'revert-function' in *xref* buffer

* lisp/progmodes/xref.el (xref--xref-buffer-mode-map): Cease
binding 'g' to 'xref-revert-buffer'.
(xref--xref-buffer-mode): Set 'revert-buffer-function' to...
(xref--revert-buffer): ...this.  New function, renamed from...
(xref-revert-buffer): ...this.  Make it an alias of
'revert-buffer'.
* etc/NEWS: Announce it.
* doc/emacs/maintaining.texi (Xref Commands): Update docs.
---
 doc/emacs/maintaining.texi |  6 +++---
 etc/NEWS                   |  7 +++++++
 lisp/progmodes/xref.el     | 17 +++++++----------
 3 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/doc/emacs/maintaining.texi b/doc/emacs/maintaining.texi
index 3a9bef9884a..64d77fb88a5 100644
--- a/doc/emacs/maintaining.texi
+++ b/doc/emacs/maintaining.texi
@@ -2466,10 +2466,10 @@ Xref Commands
 @file{*xref*} buffers that show all the matches for an identifier in
 all the relevant files.  @xref{Identifier Search}.
 
+@cindex revert-buffer, in @file{*xref*} buffers
 @item g
-@findex xref-revert-buffer
-Refresh the contents of the @file{*xref*} buffer
-(@code{xref-revert-buffer}).
+Refresh the contents of the @file{*xref*} buffer (@code{revert-buffer}).
+@xref{Reverting}.
 
 @item M-,
 @findex xref-quit-and-pop-marker-stack
diff --git a/etc/NEWS b/etc/NEWS
index 716eb612720..8d360063f7a 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1916,6 +1916,13 @@ the 'widget-inactive' face).
 If non-nil, moving point forward or backward between widgets by typing
 'TAB' or 'S-TAB' skips over inactive widgets.  The default value is nil.
 
+** Xref
+
+*** 'xref-revert-buffer' is now an alias of 'revert-buffer'.
+The Xref buffer now sets up 'revert-buffer-function' such that
+'revert-buffer' behaves like 'xref-revert-buffer' did in previous Emacs
+versions, and the latter is now an alias of the former.
+
 ** Ruby mode
 
 *** New user option 'ruby-rubocop-use-bundler'.
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index fb6c9dad73b..0b069941438 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -993,7 +993,6 @@ xref--xref-buffer-mode-map
     ;; suggested by Johan Claesson "to further reduce finger movement":
     (define-key map (kbd ".") #'xref-next-line)
     (define-key map (kbd ",") #'xref-prev-line)
-    (define-key map (kbd "g") #'xref-revert-buffer)
     (define-key map (kbd "M-,") #'xref-quit-and-pop-marker-stack)
     map))
 
@@ -1011,6 +1010,7 @@ xref--xref-buffer-mode
         #'xref--imenu-extract-index-name)
   (setq-local add-log-current-defun-function
               #'xref--add-log-current-defun)
+  (setq-local revert-buffer-function #'xref--revert-buffer)
   (setq-local outline-minor-mode-cycle t)
   (setq-local outline-minor-mode-use-buttons 'insert)
   (setq-local outline-search-function
@@ -1273,22 +1273,16 @@ xref--show-common-initialize
           xref--original-window-intent (assoc-default 'display-action alist))
     (setq xref--fetcher fetcher)))
 
-(defun xref-revert-buffer ()
+(defun xref--revert-buffer (&rest _)    ; Ignore `revert-buffer' args.
   "Refresh the search results in the current buffer."
-  (interactive)
   (let ((inhibit-read-only t)
-        (buffer-undo-list t)
-        restore-functions)
-    (when (boundp 'revert-buffer-restore-functions)
-      (run-hook-wrapped 'revert-buffer-restore-functions
-                        (lambda (f) (push (funcall f) restore-functions) nil)))
+        (buffer-undo-list t))
     (save-excursion
       (condition-case err
           (let ((alist (xref--analyze (funcall xref--fetcher)))
                 (inhibit-modification-hooks t))
             (erase-buffer)
-            (prog1 (xref--insert-xrefs alist)
-              (mapc #'funcall (delq nil restore-functions))))
+            (xref--insert-xrefs alist))
         (user-error
          (erase-buffer)
          (insert
@@ -1296,6 +1290,9 @@ xref-revert-buffer
            (error-message-string err)
            'face 'error)))))))
 
+;;; FIXME: Make this alias obsolete in future release.
+(defalias 'xref-revert-buffer #'revert-buffer)
+
 (defun xref--auto-jump-first (buf value)
   (when value
     (select-window (get-buffer-window buf))
-- 
2.45.0