GNU bug report logs - #65854
Multi-file replacement diff

Previous Next

Package: emacs;

Reported by: Juri Linkov <juri <at> linkov.net>

Date: Sun, 10 Sep 2023 17:24:01 UTC

Severity: wishlist

Tags: patch

Fixed in version 30.0.50

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Juri Linkov <juri <at> linkov.net>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: me <at> eshelyaron.com, 65854 <at> debbugs.gnu.org
Subject: bug#65854: Multi-file replacement diff
Date: Mon, 25 Sep 2023 21:18:49 +0300
[Message part 1 (text/plain, inline)]
>> >> >> >> +                                                    However, when the file
>> >> >> >> +is not visited in a buffer, or the buffer is not modified, still read
>> >> >> >> +contents from the file."
>> >> >> >
>> >> >> > Seems to describe an implementation detail, and I don't think it
>> >> >> > should be there.  E.g., what if the file visited by the buffer no
>> >> >> > longer exists?
>> >> >>
>> >> >> If the file visited by the buffer no longer exists, then
>> >> >> the standard error is signaled.
>> >> >
>> >> > Which means in that case it is better to use the buffer text, no?
>> >>
>> >> Since replacement diffs are not supported in non-file buffers,
>> >> better to signal an error for heads up.
>> >
>> > But it _is_ a file-visiting buffer.  It's just that its file was
>> > deleted meanwhile.
>>
>> The generated diff could not be applied to the deleted file.
>> So generating a diff to the deleted file makes no sense anyway.
>
> I suggest not to second-guess what the user wants to do with the
> generated diffs.  What if they just want to email it or something?
>
> The basic rule of the least surprise is pertinent here: we have the
> data, so why not generate the diffs when we can?
>
> But if you feel strongly about signaling an error in that case, I
> won't object.

I don't disagree.  My only concern was extra complexity and
performance of file-exists-p for such rare case.  But if this
is not a problem, here is a patch over the previous patch:

[multi-file-replace-as-diff.patch (text/x-diff, inline)]
diff --git a/lisp/misearch.el b/lisp/misearch.el
index 25d1b115af8..06bb6f57777 100644
--- a/lisp/misearch.el
+++ b/lisp/misearch.el
@@ -427,17 +427,24 @@ multi-file-replace-as-diff
       (setq buffer-read-only t)
       (diff-mode))
     (dolist (file-name files)
-      (let ((file-buffer (when (eq multi-file-diff-unsaved 'use-modified-buffer)
-                           (find-buffer-visiting file-name))))
+      (let* ((file-exists (file-exists-p file-name))
+             (file-buffer
+              (when (or (not file-exists)
+                        (eq multi-file-diff-unsaved 'use-modified-buffer))
+                (find-buffer-visiting file-name))))
         (when file-name
           (with-temp-buffer
-            (if (and file-buffer (buffer-modified-p file-buffer))
+            (if (and file-buffer
+                     (or (not file-exists)
+                         (buffer-modified-p file-buffer)))
                 (insert-buffer-substring file-buffer)
               (insert-file-contents file-name))
             (goto-char (point-min))
             (perform-replace from-string replacements nil regexp-flag delimited-flag)
-            (multi-file-diff-no-select file-name (current-buffer) nil diff-buffer
-                                       (concat file-name "~") file-name)))))
+            (multi-file-diff-no-select
+             (if file-exists file-name file-buffer)
+             (current-buffer) nil diff-buffer
+             (concat file-name "~") file-name)))))
     (with-current-buffer diff-buffer
       (diff-setup-whitespace)
       (diff-setup-buffer-type)

This bug report was last modified 1 year and 236 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.