bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#65854: Multi-file replacement diff


From: Juri Linkov
Subject: bug#65854: Multi-file replacement diff
Date: Fri, 22 Sep 2023 09:55:40 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu)

>> +(defcustom multi-file-diff-unsaved 'use-file
>> +  "A choice defining what to do with unsaved changes.
>
> This first sentence is too general.  I suggest
>
>   What to do with unsaved edits when showing multi-file replacements as diffs.

Fixed, and changed the default to 'save-buffers' like Eshel suggested.

>> +(defun multi-file-replace-regexp-as-diff (files regexp to-string &optional 
>> delimited)
>> +  "Show replacements in FILES matching REGEXP with TO-STRING as diff.
>
>   Show as diffs replacements of REGEXP with TO-STRING in FILES.

Fixed with more small changes:

diff --git a/lisp/misearch.el b/lisp/misearch.el
index 9ac28c26c48..44023d029d9 100644
--- a/lisp/misearch.el
+++ b/lisp/misearch.el
@@ -387,6 +387,148 @@ multi-isearch-files-regexp
     (goto-char (if isearch-forward (point-min) (point-max)))
     (isearch-forward-regexp nil t)))
 
+
+;;; Global multi-file and multi-buffer replacements as diff
+
+(defcustom multi-file-diff-unsaved 'save-buffers
+  "What to do with unsaved edits when showing multi-file replacements as diffs.
+If the value is `save-buffers', save unsaved buffers before creating diff.
+If the value is `use-file', use text from the file even when the visiting
+file buffer is modified.
+If the value is `use-modified-buffer', use text from the file-visiting
+modified buffer to be able to use unsaved changes.  However, when the file
+is not visited in a buffer, or the buffer is not modified, still read
+contents from the file."
+  :type '(choice
+          (const :tag "Save buffers" save-buffers)
+          (const :tag "Use file" use-file)
+          (const :tag "Use modified buffer" use-modified-buffer))
+  :version "30.1")
+
+(defun multi-file-replace-as-diff (files-or-buffers from-string replacements 
regexp-flag delimited-flag)
+  "Show as diffs replacements of FROM-STRING with REPLACEMENTS.
+FILES-OR-BUFFERS is a list of either file names or buffers.
+REGEXP-FLAG and DELIMITED-FLAG have the same meaning as in `perform-replace'."
+  (require 'diff)
+  (let ((inhibit-message t)
+        (diff-buffer (get-buffer-create "*replace-diff*")))
+    (when (eq multi-file-diff-unsaved 'save-buffers)
+      (save-some-buffers t (lambda ()
+                             (seq-some (lambda (f-or-b)
+                                         (if (bufferp f-or-b)
+                                             (eq f-or-b (current-buffer))
+                                           (equal f-or-b (buffer-file-name))))
+                                       files-or-buffers))))
+    (with-current-buffer diff-buffer
+      (setq-local buffer-read-only t)
+      (erase-buffer)
+      (diff-mode)
+      (setq-local buffer-read-only nil)
+      (buffer-disable-undo (current-buffer)))
+    (dolist (file-or-buffer files-or-buffers)
+      (let ((file-name (if (bufferp file-or-buffer) buffer-file-name 
file-or-buffer))
+            (file-buffer (when (eq multi-file-diff-unsaved 
'use-modified-buffer)
+                           (find-buffer-visiting file-or-buffer))))
+        (when file-name
+          (with-temp-buffer
+            (if (bufferp file-or-buffer)
+                (insert-buffer-substring file-or-buffer)
+              (if (and file-buffer (buffer-modified-p file-buffer))
+                  (insert-buffer-substring file-buffer)
+                (insert-file-contents file-or-buffer)))
+            (goto-char (point-min))
+            (perform-replace from-string replacements nil regexp-flag 
delimited-flag)
+            (multi-file-diff-no-select file-or-buffer (current-buffer) nil 
diff-buffer
+                                       (concat file-name "~") file-name)))))
+    (with-current-buffer diff-buffer
+      (diff-setup-whitespace)
+      (font-lock-ensure)
+      (buffer-enable-undo (current-buffer))
+      (setq-local buffer-read-only t)
+      (setq-local revert-buffer-function
+                  (lambda (_ignore-auto _noconfirm)
+                    (multi-file-replace-as-diff
+                     files-or-buffers from-string replacements regexp-flag 
delimited-flag)))
+      (goto-char (point-min)))
+    (pop-to-buffer diff-buffer)))
+
+;;;###autoload
+(defun multi-file-replace-regexp-as-diff (files regexp to-string &optional 
delimited)
+  "Show as diffs replacements of REGEXP with TO-STRING in FILES.
+DELIMITED has the same meaning as in `replace-regexp'.
+With a prefix argument, ask for a wildcard, and show diffs for files
+whose file names match the specified wildcard."
+  (interactive
+   (let ((files (if current-prefix-arg
+                    (multi-isearch-read-matching-files)
+                  (multi-isearch-read-files)))
+         (common
+          (query-replace-read-args
+           (concat "Replace"
+                   (if current-prefix-arg " word" "")
+                   " regexp as diff in files")
+           t t)))
+     (list files (nth 0 common) (nth 1 common) (nth 2 common))))
+  (multi-file-replace-as-diff files regexp to-string t delimited))
+
+;;;###autoload
+(defun replace-regexp-as-diff (regexp to-string &optional delimited)
+  "Show as diffs replacements of REGEXP with TO-STRING in the current buffer.
+DELIMITED has the same meaning as in `replace-regexp'."
+  (interactive
+   (let ((common
+          (query-replace-read-args
+           (concat "Replace"
+                   (if current-prefix-arg " word" "")
+                   " regexp as diff")
+           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))
+
+(defun multi-file-diff-no-select (old new &optional switches buf label-old 
label-new)
+  ;; Based on `diff-no-select' tailored to multi-file diffs.
+  "Compare the OLD and NEW file/buffer.
+If the optional SWITCHES is nil, the switches specified in the
+variable `diff-switches' are passed to the diff command,
+otherwise SWITCHES is used.  SWITCHES can be a string or a list
+of strings.  BUF should be non-nil.  LABEL-OLD and LABEL-NEW
+specify labels to use for file names."
+  (unless (bufferp new) (setq new (expand-file-name new)))
+  (unless (bufferp old) (setq old (expand-file-name old)))
+  (or switches (setq switches diff-switches)) ; If not specified, use default.
+  (setq switches (ensure-list switches))
+  (diff-check-labels)
+  (let* ((old-alt (diff-file-local-copy old))
+         (new-alt (diff-file-local-copy new))
+         (command
+          (mapconcat #'identity
+                     `(,diff-command
+                       ;; Use explicitly specified switches
+                       ,@switches
+                       ,@(mapcar #'shell-quote-argument
+                                 (nconc
+                                  (and (or old-alt new-alt)
+                                       (eq diff-use-labels t)
+                                       (list "--label"
+                                             (cond ((stringp label-old) 
label-old)
+                                                   ((stringp old) old)
+                                                   ((prin1-to-string old)))
+                                             "--label"
+                                             (cond ((stringp label-new) 
label-new)
+                                                   ((stringp new) new)
+                                                   ((prin1-to-string new)))))
+                                  (list (or old-alt old)
+                                        (or new-alt new)))))
+                     " ")))
+    (with-current-buffer buf
+      (insert command "\n")
+      (call-process shell-file-name nil buf nil
+                    shell-command-switch command)
+      (if old-alt (delete-file old-alt))
+      (if new-alt (delete-file new-alt)))))
+
+
 (defvar unload-function-defs-list)
 
 (defun multi-isearch-unload-function ()

reply via email to

[Prev in Thread] Current Thread [Next in Thread]