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#66113: Apply the entire diff buffer Date: Wed, 20 Sep 2023 09:46:18 +0300 Organization: LINKOV.NET Message-ID: <86o7hx4a11.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="21063"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) To: 66113@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 20 08:51:21 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 1qir3Q-0005I0-4m for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 20 Sep 2023 08:51:20 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qir34-0007Vw-FL; Wed, 20 Sep 2023 02:50:58 -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 1qir31-0007VX-3y for bug-gnu-emacs@gnu.org; Wed, 20 Sep 2023 02:50: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 1qir2z-0003r8-BS for bug-gnu-emacs@gnu.org; Wed, 20 Sep 2023 02:50:53 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qir37-0001xu-Vs for bug-gnu-emacs@gnu.org; Wed, 20 Sep 2023 02:51: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: Wed, 20 Sep 2023 06:51:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 66113 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16951926067479 (code B ref -1); Wed, 20 Sep 2023 06:51:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 20 Sep 2023 06:50:06 +0000 Original-Received: from localhost ([127.0.0.1]:58132 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qir2E-0001wY-E7 for submit@debbugs.gnu.org; Wed, 20 Sep 2023 02:50:06 -0400 Original-Received: from lists.gnu.org ([2001:470:142::17]:50050) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qir28-0001vx-QN for submit@debbugs.gnu.org; Wed, 20 Sep 2023 02:50:04 -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 1qir1u-0007Pq-8y for bug-gnu-emacs@gnu.org; Wed, 20 Sep 2023 02:49:46 -0400 Original-Received: from relay9-d.mail.gandi.net ([2001:4b98:dc4:8::229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qir1r-0003R1-Ui for bug-gnu-emacs@gnu.org; Wed, 20 Sep 2023 02:49:46 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id AF523FF811 for ; Wed, 20 Sep 2023 06:49:36 +0000 (UTC) X-GND-Sasl: juri@linkov.net Received-SPF: pass client-ip=2001:4b98:dc4:8::229; envelope-from=juri@linkov.net; helo=relay9-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:270904 Archived-At: --=-=-= Content-Type: text/plain After discussing on emacs-devel, here is a complete patch for the feature of applying the entire diff buffer: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=diff-apply-buffer.patch diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index d776375d681..4633d5896a7 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -216,6 +216,7 @@ diff-mode-map "C-x 4 A" #'diff-add-change-log-entries-other-window ;; Misc operations. "C-c C-a" #'diff-apply-hunk + "C-c C-m a" #'diff-apply-buffer "C-c C-e" #'diff-ediff-patch "C-c C-n" #'diff-restrict-view "C-c C-s" #'diff-split-hunk @@ -2054,6 +2055,40 @@ diff-kill-applied-hunks (diff-hunk-kill) (diff-hunk-next))))) +(defun diff-apply-buffer () + "Apply the diff in the entire diff buffer. +When applying all hunks was successful, then save the changed buffers." + (interactive) + (let ((buffers nil) + (failures 0) + (diff-refine nil)) + (save-excursion + (goto-char (point-min)) + (diff-beginning-of-hunk t) + (while (pcase-let ((`(,buf ,line-offset ,pos ,_src ,dst ,switched) + (diff-find-source-location nil nil))) + (cond ((and line-offset (not switched)) + (with-current-buffer buf + (goto-char (car pos)) + (delete-region (car pos) (cdr pos)) + (insert (car dst)) + (when buffer-file-name + (push (current-buffer) buffers)))) + (t (setq failures (1+ failures)))) + (not (or (eq (prog1 (point) (diff-hunk-next)) (point)) + (eobp)))))) + (setq buffers (delete-dups buffers)) + (cond ((zerop failures) + (dolist (buf (reverse buffers)) + (with-current-buffer buf + (save-buffer))) + (message "Saved %d buffers" (length buffers))) + (t + (dolist (buf buffers) + (with-current-buffer buf + (display-buffer buf))) + (message "%d hunks failed; no buffers saved" failures))))) + (defalias 'diff-mouse-goto-source #'diff-goto-source) (defun diff-goto-source (&optional other-file event) --=-=-=--