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

From: Juri Linkov <[email-masked]>
Date: Mon, 11 Sep 2023 09:33:06 +0300

>> +(defun multi-file-replace-as-diff (files-or-buffers from-string replacements regexp-flag delimited-flag)
>> +  (require 'diff)
>> +  (let ((inhibit-message t)
>> +        (diff-buffer (get-buffer-create "*replace-diff*")))
>> +    (with-current-buffer diff-buffer
>> +      (buffer-disable-undo (current-buffer))
>> +      (let ((inhibit-read-only t))
>> +        (erase-buffer))
>> +      (diff-mode))
>> +    (dolist (file-or-buffer files-or-buffers)
>> +      (let ((file-name (if (bufferp file-or-buffer) buffer-file-name file-or-buffer)))
>> +        (when file-name
>> +          (with-temp-buffer
>> +            (if (bufferp file-or-buffer)
>> +                (insert-buffer-substring file-or-buffer)
>> +              (insert-file-contents file-or-buffer))
>
> I wonder what happens if I call `multi-file-replace-regexp-as-diff` and
> select a file `foo.txt`, that I already have open and modified in a
> buffer.  IIUC, this will generate the diff based on the contents of the
> file on disk, not the buffer, so it might not match when I subsequently
> try to apply the diff to the buffer.  WDYT?

For such cases you can use multi-buffer-replace-regexp-as-diff
from this patch instead of multi-file-replace-regexp-as-diff.

The former generates the diff based on the contents of the
file in the buffer, the latter uses the contents on disk.