Subject: bug#44905: 27.1; Packages that customize xref-show-xrefs-function can break Dired's dired-do-find-regexp-and-replace

From: Dmitry Gutov <[email-masked]>
Date: Mon, 30 Nov 2020 03:00:17 +0200

On 29.11.2020 14:57, Daniel Martín via Bug reports for GNU Emacs, the 
Swiss army knife of text editors wrote:
> Dmitry Gutov <[email-masked]> writes:
>>
>> While this change in the Right Thing(tm), I have to question the
>> wisdom of setting xref-show-xrefs-function to an Ivy or Helm-based
>> function, though. Those UIs serve to help you choose one item, whereas
>> commands like dired-do-find-regexp and project-find-regexp show the
>> user a list of matches, to interact with (usually) several of them.
> 
> I agree with you, Ivy/Helm may not be the best UX for Xref, but this
> particular problem can still happen even if the user returns a buffer
> that represents the Xref data in a different way.  I think that
> xref-query-replace-in-results assumes certain invariants from the
> original *xref* buffer that are not documented.

True.

>>
>> diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
>> index 94a2bbf1f3..4caafc8df6 100644
>> --- a/lisp/dired-aux.el
>> +++ b/lisp/dired-aux.el
>> @@ -3140,7 +3140,10 @@ dired-do-find-regexp-and-replace
>>             (query-replace-read-args
>>              "Query replace regexp in marked files" t t)))
>>        (list (nth 0 common) (nth 1 common))))
>> -  (with-current-buffer (dired-do-find-regexp from)
>> +  (defvar xref-show-xrefs-function)
>> +  (with-current-buffer
>> +      (let ((xref-show-xrefs-function 'xref--show-xref-buffer))
>> +        (dired-do-find-regexp from))
>>       (xref-query-replace-in-results from to)))
>>
>>   (defun dired-nondirectory-p (file)
> 
> LGTM, thanks.

Eli, is this OK for Emacs 27.2?

Here's also a slightly more future-proofed version that avoids 
referencing the function we might want to rename/change later:

diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el
index 94a2bbf1f3..26155190d4 100644
--- a/lisp/dired-aux.el
+++ b/lisp/dired-aux.el
@@ -3140,7 +3140,13 @@ dired-do-find-regexp-and-replace
            (query-replace-read-args
             "Query replace regexp in marked files" t t)))
       (list (nth 0 common) (nth 1 common))))
-  (with-current-buffer (dired-do-find-regexp from)
+  (require 'xref)
+  (defvar xref-show-xrefs-function)
+  (with-current-buffer
+      (let ((xref-show-xrefs-function
+             ;; Some future-proofing (bug#44905).
+             (eval (car (get 'xref-show-xrefs-function 'standard-value)))))
+        (dired-do-find-regexp from))
      (xref-query-replace-in-results from to)))

  (defun dired-nondirectory-p (file)