all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: me@eshelyaron.com, 65854@debbugs.gnu.org
Subject: bug#65854: Multi-file replacement diff
Date: Fri, 22 Sep 2023 19:02:33 +0300	[thread overview]
Message-ID: <86o7hunqyu.fsf@mail.linkov.net> (raw)
In-Reply-To: <83zg1e7jq3.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 22 Sep 2023 10:25:24 +0300")

[-- Attachment #1: Type: text/plain, Size: 1289 bytes --]

>> +(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."
>
> Please use consistent wording to describe the same entities.  If you
> use "file-visiting buffer", use it everywhere, when you sometimes use
> that and sometimes "visiting file buffer", you are already half way to
> confusing the reader.

Ok, fixed below.

> Also, this part:
>
>> +                                                    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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: multi-file-diff-unsaved.patch --]
[-- Type: text/x-diff, Size: 7139 bytes --]

diff --git a/lisp/misearch.el b/lisp/misearch.el
index 9ac28c26c48..25d1b115af8 100644
--- a/lisp/misearch.el
+++ b/lisp/misearch.el
@@ -387,6 +387,150 @@ multi-isearch-files-regexp
     (goto-char (if isearch-forward (point-min) (point-max)))
     (isearch-forward-regexp nil t)))
 
+\f
+;;; 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
+file-visiting 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."
+  :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")
+
+(declare-function diff-setup-whitespace "diff-mode" ())
+(declare-function diff-setup-buffer-type "diff-mode" ())
+
+(defun multi-file-replace-as-diff (files from-string replacements regexp-flag delimited-flag)
+  "Show as diffs replacements of FROM-STRING with REPLACEMENTS.
+FILES is a list of file names.  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)
+                                         (equal f-or-b buffer-file-name))
+                                       files))))
+    (with-current-buffer diff-buffer
+      (buffer-disable-undo (current-buffer))
+      (let ((inhibit-read-only t))
+        (erase-buffer))
+      ;; Make the *vc-diff* buffer read only, the diff-mode key
+      ;; bindings are nicer for read only buffers.
+      (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))))
+        (when file-name
+          (with-temp-buffer
+            (if (and file-buffer (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)))))
+    (with-current-buffer diff-buffer
+      (diff-setup-whitespace)
+      (diff-setup-buffer-type)
+      (buffer-enable-undo (current-buffer))
+      (setq-local revert-buffer-function
+                  (lambda (_ignore-auto _noconfirm)
+                    (multi-file-replace-as-diff
+                     files 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 buffer-file-name) regexp to-string t delimited))
+
+(defvar diff-use-labels)
+(declare-function diff-check-labels "diff" (&optional force))
+(declare-function diff-file-local-copy "diff" (file-or-buf))
+
+(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
+      (let ((inhibit-read-only t))
+        (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)))))
+
+\f
 (defvar unload-function-defs-list)
 
 (defun multi-isearch-unload-function ()

  reply	other threads:[~2023-09-22 16:02 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-10 17:18 bug#65854: Multi-file replacement diff Juri Linkov
2023-09-10 17:58 ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-11  6:33   ` Juri Linkov
2023-09-11  7:23     ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-11  7:38       ` Juri Linkov
2023-09-12  6:49         ` Juri Linkov
2023-09-11 12:35       ` Eli Zaretskii
2023-09-12  6:52         ` Juri Linkov
2023-09-15  6:40           ` Juri Linkov
2023-09-15  7:02             ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-15  7:38             ` Eli Zaretskii
2023-09-22  6:55               ` Juri Linkov
2023-09-22  7:25                 ` Eli Zaretskii
2023-09-22 16:02                   ` Juri Linkov [this message]
2023-09-22 16:06                     ` Eli Zaretskii
2023-09-23 17:36                       ` Juri Linkov
2023-09-23 18:56                         ` Eli Zaretskii
2023-09-24  7:37                           ` Juri Linkov
2023-09-24  8:12                             ` Eli Zaretskii
2023-09-25 18:18                               ` Juri Linkov
2023-09-24  1:43 ` Dmitry Gutov
2023-09-24  7:36   ` Juri Linkov
2023-09-24 11:09     ` Dmitry Gutov
2023-09-25 17:58       ` Juri Linkov
2023-09-26 21:39         ` Dmitry Gutov
2023-09-27 17:21           ` Juri Linkov
2023-09-30 17:42           ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86o7hunqyu.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=65854@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=me@eshelyaron.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.