From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#65854: Multi-file replacement diff Date: Fri, 15 Sep 2023 09:40:21 +0300 Organization: LINKOV.NET Message-ID: <86sf7g548a.fsf@mail.linkov.net> References: <86sf7mgd54.fsf@mail.linkov.net> <86bke943tp.fsf@mail.linkov.net> <83jzswrios.fsf@gnu.org> <86o7i7oo9t.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4587"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: Eshel Yaron , 65854@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Sep 15 08:48:09 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qh2cb-00013H-OL for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 15 Sep 2023 08:48:09 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qh2cP-0001f0-TV; Fri, 15 Sep 2023 02:47:57 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qh2cO-0001eM-OJ for bug-gnu-emacs@gnu.org; Fri, 15 Sep 2023 02:47:56 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qh2cO-0006zY-Gk for bug-gnu-emacs@gnu.org; Fri, 15 Sep 2023 02:47:56 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qh2cU-0005k2-NZ for bug-gnu-emacs@gnu.org; Fri, 15 Sep 2023 02:48:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 15 Sep 2023 06:48:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65854 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 65854-submit@debbugs.gnu.org id=B65854.169476042421880 (code B ref 65854); Fri, 15 Sep 2023 06:48:02 +0000 Original-Received: (at 65854) by debbugs.gnu.org; 15 Sep 2023 06:47:04 +0000 Original-Received: from localhost ([127.0.0.1]:41882 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qh2bX-0005gf-Cw for submit@debbugs.gnu.org; Fri, 15 Sep 2023 02:47:04 -0400 Original-Received: from relay3-d.mail.gandi.net ([217.70.183.195]:49287) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qh2bR-0005fc-Rj for 65854@debbugs.gnu.org; Fri, 15 Sep 2023 02:46:59 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 66F1260005; Fri, 15 Sep 2023 06:46:43 +0000 (UTC) In-Reply-To: <86o7i7oo9t.fsf@mail.linkov.net> (Juri Linkov's message of "Tue, 12 Sep 2023 09:52:42 +0300") X-GND-Sasl: juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:270493 Archived-At: --=-=-= Content-Type: text/plain > I don't know if users will find this too annoying, maybe not, > but probably this is what should be done to solve such dilemma that > the same diff can be used in two ways: by applying the diff to the > buffers with 'C-c C-a', or by applying to the files with 'git apply'. > Without saving the buffers before running the command, either > will fail: 'C-c C-a' will fail on changes produced from files, > 'git apply' will fail on changes produced from unsaved buffers. Here is a new customizable option 'multi-file-diff-unsaved' that defines what to do with unsaved changes. When it is 'use-buffer' then it handles the case Eshel demonstrated where changes are applied over unsaved buffers. But when the value is 'save-buffers', then 'save-some-buffers' is called before producing the diff. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=multi-file-diff-unsaved.patch diff --git a/lisp/misearch.el b/lisp/misearch.el index 9ac28c26c48..23872335c2d 100644 --- a/lisp/misearch.el +++ b/lisp/misearch.el @@ -387,6 +387,160 @@ 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 'use-file + "A choice defining what to do with unsaved changes. +If the value is `use-file', use text from the file. +If the value is `use-buffer', use text from the file-visiting buffer +to be able to use unsaved changes. However, when the file is +not visited in a buffer, read contents from the file. +If the value is `save-buffers', save unsaved buffers before creating diff." + :type '(choice + (const :tag "Use file" use-file) + (const :tag "Use buffer" use-buffer) + (const :tag "Save buffers" save-buffers)) + :version "30.1") + +(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*"))) + (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))) + (when file-name + (with-temp-buffer + (if (bufferp file-or-buffer) + (insert-buffer-substring file-or-buffer) + (if (or (eq multi-file-diff-unsaved 'use-file) + (not (find-buffer-visiting file-or-buffer))) + (insert-file-contents file-or-buffer) + (insert-buffer-substring (find-buffer-visiting 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))) + +(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))))) + +;;;###autoload +(defun multi-file-replace-regexp-as-diff (files regexp to-string &optional delimited) + "Show replacements in FILES matching REGEXP with TO-STRING as diff. +With a prefix argument, ask for a wildcard, and replace in 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 multi-buffer-replace-regexp-as-diff (buffers regexp to-string &optional delimited) + "Show replacements in file BUFFERS 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 ((buffers (if current-prefix-arg + (multi-isearch-read-matching-buffers) + (multi-isearch-read-buffers))) + (common + (query-replace-read-args + (concat "Replace" + (if current-prefix-arg " word" "") + " regexp as diff in buffers") + t t))) + (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) (defun multi-isearch-unload-function () --=-=-=--