From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Sean Whitton Newsgroups: gmane.emacs.bugs Subject: bug#73407: 31.0.50; Add diff-discard-hunk Date: Tue, 24 Sep 2024 09:40:30 +0100 Message-ID: <87y13hmold.fsf@zephyr.silentflame.com> References: <87jzf58g2t.fsf@zephyr.silentflame.com> <865xqleexg.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="8714"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 73407@debbugs.gnu.org To: Juri Linkov , dgutov@yandex.ru Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Sep 24 10:41:59 2024 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 1st17N-00020n-Q6 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 24 Sep 2024 10:41:58 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1st176-0006ND-GL; Tue, 24 Sep 2024 04:41:40 -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 1st174-0006N4-V2 for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 04:41:39 -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 1st174-00038T-H5 for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 04:41:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=95XotaL5ow9t6NHQ5rBrLhMU1pOnR4yZ432sC77Yxo4=; b=kd+pg7M683erZWCCcjtOy0shLb7qryfE8sSw5Gz5V8saWjzyz5OK/0IoIGX8mYeKFdam4uItDW8SzXwQwdbzdH8XbGCIya2UF1kNXpETLBY72Fr6Jaf4Y4wwwpMMaP0TaTZLYREFYnaJYQfoCbbrp/sNwIDd0WqF6fzwVR/0JoES4Rk4pzlmLGSTYiCciwH3kEFbHsFD9UDeXeK6l9jrt3+7VGIYnn7fdfAkzNDjBmcxHB0aN/Tb5WLXCX5Nx8paDvNfL1mouGc/GTLg6/T9GNT+T7HSSTPinjeD8tL0z+ScX82o7n582rMU5lZ9Ge3k8+sO6v2JWVYSVhi9AvN94w==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1st17S-0002MB-9S for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 04:42:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Sean Whitton Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 24 Sep 2024 08:42:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 73407 X-GNU-PR-Package: emacs Original-Received: via spool by 73407-submit@debbugs.gnu.org id=B73407.17271672688983 (code B ref 73407); Tue, 24 Sep 2024 08:42:02 +0000 Original-Received: (at 73407) by debbugs.gnu.org; 24 Sep 2024 08:41:08 +0000 Original-Received: from localhost ([127.0.0.1]:45252 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1st16a-0002Ko-4G for submit@debbugs.gnu.org; Tue, 24 Sep 2024 04:41:08 -0400 Original-Received: from sendmail.purelymail.com ([34.202.193.197]:57022) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1st16W-0002K9-Fw for 73407@debbugs.gnu.org; Tue, 24 Sep 2024 04:41:06 -0400 DKIM-Signature: a=rsa-sha256; b=VvzpMyq375GDybq2oeb/3yZyGSSAZqEeAiuTltaZIPLOPMoBN5lsfBqfFR+1yks2zOaT0LKsw8KSOGUu9ndlw4c0H5yZukln3pX6OsZmpZjS2W/0n/w9AzjrNbjeSAquHg5X1cx+j74+Uxfw7xJKf6h3gbzrP0gSFp1UaC7pgfHjaYuin9LKtlVVQRQxXkhyho8a2l/5kXu292sXOYUp1/gtdqlG5sO7HdfDsw1qe9c4PS2X4zZzrvyPrQSESSj7YNKe+/kGFZG8slJrHo4yAjuZyuQ95xvGIHk8X8+QP0DKaRUjiGcWJp8oeiUpeptMnBOQaxhXMJUS45aAaTOXZw==; s=purelymail1; d=spwhitton.name; v=1; bh=P7WT7q5uebArbw2ujwDMutyZMqo8rvnQcaIpPo3zNc4=; h=Received:Received:From:To:Subject:Date; DKIM-Signature: a=rsa-sha256; b=l1753x4c5EXn5hPRcuZcGkLatep7A7LmraM2lRBNlow3zJ2gJfkO3QRxPYk8a92G9NiurmVxUjyRVkN1tDM3s3A+Ccx1VKU4oOwWo+agfDIWCbshmXt3tw/FBeAc+XokYHo5rxQcjnLMa6yc8E2WBAmXJSSykxCTympMsxW0hrwL4ALPTNjyrvPC/9j8ivWhl66jmzDTqn2JAVcxep5YSqCN6cbDiFvYythITM6CAl/jyNYKSwRJBJ90ypYtacVzb1XD/k0mdkZTIDeMe1l70ZPxfRYN2OHqSpnVBT9nxIIIcNuEr3V6zEENtI125vQl8v4VyyOYdgt2rL6IrSnnaA==; s=purelymail1; d=purelymail.com; v=1; bh=P7WT7q5uebArbw2ujwDMutyZMqo8rvnQcaIpPo3zNc4=; h=Feedback-ID:Received:Received:From:To:Subject:Date; Feedback-ID: 20115:3760:null:purelymail X-Pm-Original-To: 73407@debbugs.gnu.org Original-Received: by smtp.purelymail.com (Purelymail SMTP) with ESMTPSA id 1350465131; (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384); Tue, 24 Sep 2024 08:40:31 +0000 (UTC) Original-Received: by zephyr.silentflame.com (Postfix, from userid 1000) id 9E4CF94194D; Tue, 24 Sep 2024 09:40:30 +0100 (BST) In-Reply-To: <865xqleexg.fsf@mail.linkov.net> (Juri Linkov's message of "Tue, 24 Sep 2024 09:36:27 +0300") 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:292313 Archived-At: --=-=-= Content-Type: text/plain Hello, Thank you both for looking. Here is a revised patch based on Juri's ideas about implementation, and also adding a y/n confirmation, since this operation is destructive. Let me know what you think! -- Sean Whitton --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=v2-0001-New-command-diff-discard-hunk.patch >From 2eccebb6159e6c384e1cb7a543f259d2146d8b81 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 24 Sep 2024 09:38:43 +0100 Subject: [PATCH v2] New command diff-discard-hunk * lisp/vc/diff-mode.el (diff-discard-hunk): New command (bug#73407). (diff-apply-buffer): New optional BEG and END arguments. Return nil if buffers were saved, or the number of failed applications. (diff-mode-map): Bind diff-discard-hunk to C-c C-k. (diff-mode-menu): New entry "Discard hunk". * doc/emacs/files.texi (Diff Mode): * etc/NEWS: Document the new command. --- doc/emacs/files.texi | 11 +++++++++++ etc/NEWS | 5 +++++ lisp/vc/diff-mode.el | 40 +++++++++++++++++++++++++++++++++++----- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi index 709cb0910e6..04ecfabc1ee 100644 --- a/doc/emacs/files.texi +++ b/doc/emacs/files.texi @@ -1682,6 +1682,17 @@ Diff Mode version. If @code{diff-jump-to-old-file} is non-@code{nil}, apply the hunk to the ``old'' version of the file instead. +@findex diff-discard-hunk +@item C-c C-k +Reverse-apply this hunk to the target file, and then kill it +(@code{diff-discard-hunk}). Unless the buffer visiting the target file +was already modified, save it. + +This command is useful in buffers generated by @w{@kbd{C-x v =}} and +@w{@kbd{C-x v D}} (@pxref{Old Revisions}). You can use this command to +remove hunks you never intend to commit, such as temporary debug prints, +and the like. + @findex diff-apply-buffer @item C-c @key{RET} a Apply all the hunks in the buffer (@code{diff-apply-buffer}). If the diff --git a/etc/NEWS b/etc/NEWS index c1a0524c8ba..3edd827d15c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -341,6 +341,11 @@ according to diffs in the current buffer, but without applying the diffs to the original text. If the selected range extends a hunk, the command attempts to look up and copy the text in-between the hunks. ++++ +*** New command 'diff-discard-hunk' bound to C-c C-k. +This command reverse-applies the hunk at point, and then kills it. +This is useful in buffers generated C-x v = and C-x v D. + ** php-ts-mode --- diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 4810b9ce01c..a48ba8e717a 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -218,6 +218,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-k" #'diff-discard-hunk "C-c C-m a" #'diff-apply-buffer "C-c C-e" #'diff-ediff-patch "C-c C-n" #'diff-restrict-view @@ -242,6 +243,8 @@ diff-mode-menu :help "Apply the current hunk to the source file and go to the next"] ["Test applying hunk" diff-test-hunk :help "See whether it's possible to apply the current hunk"] + ["Discard hunk" diff-discard-hunk + :help "Reverse-apply and then kill the current hunk."] ["Apply all hunks" diff-apply-buffer :help "Apply all hunks in the current diff buffer"] ["Apply diff with Ediff" diff-ediff-patch @@ -2050,15 +2053,39 @@ diff-kill-applied-hunks (diff-hunk-kill) (diff-hunk-next))))) -(defun diff-apply-buffer () +(defun diff-discard-hunk () + "Reverse-apply and then kill the hunk at point. Save changed buffer. + +This command is useful in buffers generated by \\[vc-diff] and \\[vc-root-diff]. +You can use `diff-hunk-kill' to remove hunks you intend to commit later. +You can use this command to remove hunks you never intend to commit, +such as temporary debug prints, and the like." + (interactive) + (when (yes-or-no-p "Discard this hunk?") + (cl-destructuring-bind (beg end) (diff-bounds-of-hunk) + (diff-reverse-direction beg end) + (condition-case ret + (diff-apply-buffer beg end) + ;; Reversing the hunk is an implementation detail, so ensure the + ;; user never sees it. + (error (diff-reverse-direction beg end) + (signal (car ret) (cdr ret))) + (:success (if ret + (diff-reverse-direction beg end) + (diff-hunk-kill))))))) + +(defun diff-apply-buffer (&optional beg end) "Apply the diff in the entire diff buffer. -When applying all hunks was successful, then save the changed buffers." +When applying all hunks was successful, then save the changed buffers. +When called from Lisp with optional arguments, restrict the application +to hunks lying between BEG and END. Returns nil if buffers were saved, +or the number of failed applications." (interactive) (let ((buffer-edits nil) (failures 0) (diff-refine nil)) (save-excursion - (goto-char (point-min)) + (goto-char (or beg (point-min))) (diff-beginning-of-hunk t) (while (pcase-let ((`(,buf ,line-offset ,pos ,_src ,dst ,switched) (diff-find-source-location nil nil))) @@ -2068,6 +2095,7 @@ diff-apply-buffer (t (setq failures (1+ failures)))) (and (not (eq (prog1 (point) (ignore-errors (diff-hunk-next))) (point))) + (or (not end) (< (point) end)) (looking-at-p diff-hunk-header-re))))) (cond ((zerop failures) (dolist (buf-edits (reverse buffer-edits)) @@ -2080,9 +2108,11 @@ diff-apply-buffer (delete-region (car pos) (cdr pos)) (insert (car dst)))) (save-buffer))) - (message "Saved %d buffers" (length buffer-edits))) + (message "Saved %d buffers" (length buffer-edits)) + nil) (t - (message "%d hunks failed; no buffers changed" failures))))) + (message "%d hunks failed; no buffers changed" failures) + failures)))) (defalias 'diff-mouse-goto-source #'diff-goto-source) -- 2.39.5 --=-=-=--