Subject: Re: bug#65854: Multi-file replacement diff

From: Juri Linkov <[email-masked]>
Date: Tue, 12 Sep 2023 09:49:17 +0300

>> Yes, but what is the use case for generating the diff based on the
>> contents on disk when the file is modified in Emacs?  Basically, my
>> suggestion is to check in `multi-file-replace-regexp-as-diff` if any of
>> the matching files are visited by some buffer, and if so simply pass the
>> buffer instead of the file name for that file to
>> `multi-file-replace-as-diff`.  That way you always get an up-to-date
>> diff, and you don't need to manually check that you don't have any of
>> the matching files open by any chance.  Does that make sense?
>
> Thanks for the idea, this makes sense and will help to
> reduce the number of commands from 2 to 1 by merging
> multi-buffer-replace-regexp-as-diff with
> multi-file-replace-regexp-as-diff.

Actually, separate commands are still needed when we will add
a command to show replacement diffs on the buffers marked on
the buffer list from M-x list-buffers.

But it seems we can't avoid the limitation that such buffers
should be file-visiting.  I see no way to generate a diff
for non-file buffers because 'C-c C-a' from diff-mode needs
a file name to apply the hunk to the file buffer.

Also another useful command that probably will be used the
most often is to show a replacement diff for the current buffer
as a counterpart of query-replace:

```
diff --git a/lisp/misearch.el b/lisp/misearch.el
index da70d708a9e..9aa639af5fe 100644
--- a/lisp/misearch.el
+++ b/lisp/misearch.el
@@ -500,6 +501,22 @@ multi-buffer-replace-regexp-as-diff
      (list buffers (nth 0 common) (nth 1 common) (nth 2 common))))
   (multi-file-replace-as-diff buffers regexp to-string t delimited))
 
+;;;###autoload
+(defun replace-regexp-as-diff (regexp to-string &optional delimited)
+  "Show replacements in the current file buffer matching REGEXP with TO-STRING as diff.
+With a prefix argument, ask for a regexp, and replace in file buffers
+whose names match the specified regexp."
+  (interactive
+   (let ((common
+          (query-replace-read-args
+           (concat "Replace"
+                   (if current-prefix-arg " word" "")
+                   " regexp as diff in buffers")
+           t t)))
+     (list (nth 0 common) (nth 1 common) (nth 2 common))))
+  (multi-file-replace-as-diff
+   (list (current-buffer)) regexp to-string t delimited))
+
 
 (defvar unload-function-defs-list)
 
```