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: [PATCH v4] bug#73407: 31.0.50; Add diff-discard-hunk Date: Tue, 24 Sep 2024 16:43:48 +0100 Message-ID: <87r0992h1n.fsf_-_@zephyr.silentflame.com> References: <87jzf58g2t.fsf@zephyr.silentflame.com> <865xqleexg.fsf@mail.linkov.net> <87y13hmold.fsf@zephyr.silentflame.com> <86ikuldyy1.fsf@gnu.org> 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="38576"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 73407@debbugs.gnu.org, juri@linkov.net To: Eli Zaretskii , dgutov@yandex.ru Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Sep 24 17:45:02 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 1st7in-0009qB-Nt for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 24 Sep 2024 17:45:02 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1st7iT-00053P-BA; Tue, 24 Sep 2024 11:44:41 -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 1st7iQ-000536-TB for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 11:44: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 1st7iP-0005xS-VX for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 11:44: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=M6lyXiBiZ5TRbHO+EYDlm6n5gYFjzw1GM5pYWlNYrk0=; b=rr2tfiEvut2uaLJ/TjVuhL5ukXUEoDIAFmX1xzYZ1g89A2do/gzvWKEaM4vWrjV2EfOnU1GLmL1XvP6yox/MeVB91OFwulDwH1y2AoItIvJgM7kgSa4zPn3W+Ybzazo1Q2Xq+PTaJP8NETkV4JhrtzoDYAr+44HH/fMe6fjz+vxaDgLf3cQM1NfpD0KO0knbG3tW8zxK7qNZ2VPvEAMEOanFmacsmV123A1rq2omXj/QTkAQnXqAC3GFtBdyKUuK7vq0U2HbqrI+c5EBYu7QMhn6sL7O+ofsx2H8YbMmLBEJq5um5vKsLPFftkyJIC7caOeoyDfEcB24qSrGT9vMoQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1st7io-0001lW-7p for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 11:45: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 15:45: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.17271926646652 (code B ref 73407); Tue, 24 Sep 2024 15:45:02 +0000 Original-Received: (at 73407) by debbugs.gnu.org; 24 Sep 2024 15:44:24 +0000 Original-Received: from localhost ([127.0.0.1]:50214 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1st7iB-0001jA-5j for submit@debbugs.gnu.org; Tue, 24 Sep 2024 11:44:24 -0400 Original-Received: from sendmail.purelymail.com ([34.202.193.197]:60282) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1st7i8-0001if-Lg for 73407@debbugs.gnu.org; Tue, 24 Sep 2024 11:44:21 -0400 DKIM-Signature: a=rsa-sha256; b=U8J5/pu7sXd3P6y3eXkrs8vk7p/0RHawKMaSjpEWYzl0x24k5+tCMHX++qYXg8GbJ/jIKu7G/ZPVRdiVXw8MSKLhkkSWxFfd1vglfgkJ9DDNSr/7haS8fXa2FKzGqUZxsNJcANqhStZKHhgHOwk/Lxr4NpXThO96qL0rPFdCDTBgxCo4Ul3BOG3CUJwHie5kDXEp3De58xaN+0S7GvBAzaWhQxToh7fg6WGQWswDgYD1lbuEC389nTglwZFhwVarzbgby0PYJRvNdzvz2fURDxLo2aNmQi4piPxKMtUCfFGsedCgvfM2JXhJ3oySP0JRnjbzJIcNNYGH+PufNwbpUw==; s=purelymail1; d=spwhitton.name; v=1; bh=EG24NayJkKTjMqlRCBEGo+dY4PGwN8GldoaXF4roSoE=; h=Received:Received:From:To:Subject:Date; DKIM-Signature: a=rsa-sha256; b=otdyaiCC2q5i/E7zuxQxwWs0pzCwmkz8Q9dBvFsS+i3Unuox83M8ndkOw4sRX6L7YA9EaEXy8IB7kqu9MQLIekkBZAWerBRxBc8+mJ1AcE3kTbjuX9Oe0sOVHwg7uidtWZ9sZKWvganzBTltEyNp2mc/K43X/FktnMX5TKQVfcjjd+tFoyFxBCZpppiPOJb5ptbYr/PV5uITL2ZBVSrvXKRzBwyWR+laYWMhmJVqCdHlDVwN0VOoP1YX4l8t+KyxdsE8bUs5tpU4s6BpW26GvQicQ7Kc8kqDiEnFxbgD6Vmt+8qX4cjESXRBEoME/jHMbf4/SwoE1/gBbCj0ta6Pzw==; s=purelymail1; d=purelymail.com; v=1; bh=EG24NayJkKTjMqlRCBEGo+dY4PGwN8GldoaXF4roSoE=; 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 1743115294; (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384); Tue, 24 Sep 2024 15:43:48 +0000 (UTC) Original-Received: by zephyr.silentflame.com (Postfix, from userid 1000) id 1F03D949F28; Tue, 24 Sep 2024 16:43:48 +0100 (BST) In-Reply-To: <86ikuldyy1.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 24 Sep 2024 15:21:42 +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:292343 Archived-At: --=-=-= Content-Type: text/plain Hello, On Tue 24 Sep 2024 at 03:21pm +03, Eli Zaretskii wrote: > We should come up with a better name for this command. > diff-discard-hunk tells me the command will discard the hunk, but says > nothing about applying it, let alone reverse-applying it. How about > diff-revert-hunk, or maybe diff-revert-and-discard? Oh, nice. I was just following Magit, but including "revert" is better, indeed. I think I prefer diff-revert-and-kill-hunk. >> +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. > > This text could also use some clarification: "remove hunks you never > intended to commit" only hints on what it actually does. A better > description would be something like "undo some of the changes you made > that you didn't intend". I wouldn't mention "commit" at all, since > AFAIU this command is not specific to VC and doesn't require a VCS. Yes, this is the diff-mode manual, so that makes sense. I've made a similar change in the docstring but kept a reference to committing, there. >> - (message "%d hunks failed; no buffers changed" failures))))) >> + (message "%d hunks failed; no buffers changed" failures) >> + failures)))) > > This comes from existing text, but still: what does the above say when > there's only 1 failure? does it say "1 hunks failed"? If so, can we > improve the handling of singular/plural here? I've installed a fix for that. On Tue 24 Sep 2024 at 03:22pm +03, Dmitry Gutov wrote: > I'm a little concerned about the binding. > > We have 'diff-hunk-kill' on M-k (and also 'k'), and this new addition is > actually more destructive. I'm expecting users will trip over the difference > and call one of them when they wanted to call the other. Yeah, and thinking about it more, it also doesn't really match what C-c C-k does elsewhere in Emacs. I've made it C-c M-r in the attached v4. -- Sean Whitton --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=v4-0001-New-command-diff-revert-and-kill-hunk.patch >From fc5e3a5be18f9b1532490c4c6b81b90b83c0acf6 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 24 Sep 2024 09:38:43 +0100 Subject: [PATCH v4] New command diff-revert-and-kill-hunk * lisp/vc/diff-mode.el (diff-revert-and-kill-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-revert-and-kill-hunk to C-c M-r. (diff-mode-menu): New entry "Revert and kill 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 | 41 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi index 709cb0910e6..434fb30484a 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-revert-and-kill-hunk +@item C-c M-r +Reverse-apply this hunk to the target file, and then kill it +(@code{diff-revert-and-kill-hunk}). Save the buffer visiting the target +file. + +This command is useful in buffers generated by @w{@kbd{C-x v =}} and +@w{@kbd{C-x v D}} (@pxref{Old Revisions}). These buffers present you +with a view of the changes you've made, and then you can use this +command to drop changes you didn't intend, or no longer want. + @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..7b85b9cbc04 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-revert-and-kill-hunk' bound to C-c M-r. +This command reverse-applies the hunk at point, and then kills it. +This is useful in buffers generated by 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 be3d94db011..9e39e58e69b 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 M-r" #'diff-revert-and-kill-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"] + ["Revert and kill hunk" diff-revert-and-kill-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,40 @@ diff-kill-applied-hunks (diff-hunk-kill) (diff-hunk-next))))) -(defun diff-apply-buffer () +(defun diff-revert-and-kill-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], +especially when preparing to commit the patch with \\[vc-next-action]. +You can use \\\\[diff-hunk-kill] to temporarily remove changes that you intend to +include in a separate commit or commits, and you can use this command +to permanently drop changes you didn't intend, or no longer want." + (interactive) + (when (yes-or-no-p "Really reverse-apply and kill 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 +2096,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,10 +2109,12 @@ 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 %s failed; no buffers changed" - failures (if (> failures 1) "hunks" "hunk")))))) + failures (if (> failures 1) "hunks" "hunk")) + failures)))) (defalias 'diff-mouse-goto-source #'diff-goto-source) -- 2.39.5 --=-=-=--