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: Sat, 21 Sep 2024 11:19:06 +0100 Message-ID: <87jzf58g2t.fsf@zephyr.silentflame.com> 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="11641"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: dgutov@yandex.ru, juri@linkov.net To: 73407@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 21 12:19:57 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 1srxDU-0002oC-F9 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 21 Sep 2024 12:19:52 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1srxDM-0007yb-D2; Sat, 21 Sep 2024 06:19:44 -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 1srxDK-0007yQ-Hp for bug-gnu-emacs@gnu.org; Sat, 21 Sep 2024 06:19:42 -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 1srxDK-0003yU-8q for bug-gnu-emacs@gnu.org; Sat, 21 Sep 2024 06:19:42 -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:From:To:Subject; bh=eGXJoM88MPYwl1N5Uj5G3zoLWx1c217OZLfS331LHWs=; b=Np2GXwsciAjIZuWlfDMauXzX6dCK3wP4MbQanelz1h14HkA6LJl16COZBTaOrf4cT1ssB0xN+D56ZLmVP+mK9LsOMEJ1gFTxBhkRzcQlJ99IOmLXKOJsaPtHZlUY8TYIMBSgQo35PNapunsGyQYJ2PV0e+36ndSvDZ+VwU6+96gSVZpIBbFnoBW+eEionhiyuDs4lxC+eIThincM9+PqplI+/YqBg4COr6PMNAqEpUU6MsaNggJlIuNwq6XffO+B2IU1WTVklFNMowCPvvdpIS5a6YSCk7AGPdlcBITRv1qR66NvFAvF6gQqnAOuIE+zE018LfnNlerPvwk/Vu6QvA==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1srxDd-0005Um-S6; Sat, 21 Sep 2024 06:20:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Sean Whitton Original-Sender: "Debbugs-submit" Resent-CC: dgutov@yandex.ru, juri@linkov.net, bug-gnu-emacs@gnu.org Resent-Date: Sat, 21 Sep 2024 10:20:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 73407 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: dgutov@yandex.ru, juri@linkov.net Original-Received: via spool by submit@debbugs.gnu.org id=B.172691397721075 (code B ref -1); Sat, 21 Sep 2024 10:20:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 21 Sep 2024 10:19:37 +0000 Original-Received: from localhost ([127.0.0.1]:37255 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1srxDE-0005Tq-Q2 for submit@debbugs.gnu.org; Sat, 21 Sep 2024 06:19:37 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:39200) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1srxDC-0005Tg-IP for submit@debbugs.gnu.org; Sat, 21 Sep 2024 06:19:35 -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 1srxCs-0007tE-5u for bug-gnu-emacs@gnu.org; Sat, 21 Sep 2024 06:19:14 -0400 Original-Received: from sendmail.purelymail.com ([34.202.193.197]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1srxCp-0003wI-OT for bug-gnu-emacs@gnu.org; Sat, 21 Sep 2024 06:19:13 -0400 DKIM-Signature: a=rsa-sha256; b=XixtdokDImaiT/WusKKOHj91oLsQUQKRKBbYRXQ8H88QFr23V3CyuP414EhLmDTChJqaxHwoFc+qI0EvriRfOAi+xUr6uVoLGqPJQKUzS+btWlPdK/9YkIfu3NbW2wDPNW49RppOmXgzLsjACrJji7wpJxAaQdWsnDl2vk6eowvPcPl448btcaV7yupDKoFMe0T9guAsiEf9XUpgs7Gpo8Y80NGVCTjwBtNgAM/n/rGicrwAxR43w7tszU6C8okzpJw4RipeeDp0IwmqDqa5Ll8UbYjD37EF8EIbIhuXyqaaI2F2lqNxuKlAwMBxgC3pxGe4t6p3EKEJ+uJRsQGEGA==; s=purelymail3; d=spwhitton.name; v=1; bh=EkMtUjZSQeotVEn0UUsaZXD3KyKxSkjTDbE1VN7XnwI=; h=Received:Received:From:To:Subject:Date; DKIM-Signature: a=rsa-sha256; b=jlqWhMZ/asvQzBuqB5cGzEn2spO+lqRDEziAZbCWJLyNs5djRG10XT5TFoWWT/LVFoljYxAgpV3rjuQUHPrLyCPZ76E7FqsKJ2eSzV/2Lbigb6cIU3ZXcZcl+XvEaFyS8UT1TTGQNFzYN0PI3IQXCNRXmWp9DnoUsY+DVj9aL6HCsFDjDGLyCorGdfI9Gn69r9OB++p6JYXmXyjnuXYTRP05tgxsKlt2/5tnvyTnIjhO/DUyqIvbMHxHfBZd2ru1zBFnAmGwjZmhDsrI06vkRO8cKuNJoNmjHXbuBR1IwZFrsoccuE6UT1Wg1IB9Vc2V6i73lIQnwF0QOvmzOopmFg==; s=purelymail3; d=purelymail.com; v=1; bh=EkMtUjZSQeotVEn0UUsaZXD3KyKxSkjTDbE1VN7XnwI=; h=Feedback-ID:Received:Received:From:To:Subject:Date; Feedback-ID: 20115:3760:null:purelymail X-Pm-Original-To: bug-gnu-emacs@gnu.org Original-Received: by smtp.purelymail.com (Purelymail SMTP) with ESMTPSA id -2089262969 for (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384); Sat, 21 Sep 2024 10:19:07 +0000 (UTC) Original-Received: by zephyr.silentflame.com (Postfix, from userid 1000) id 2C762949FDC; Sat, 21 Sep 2024 11:19:06 +0100 (BST) Received-SPF: pass client-ip=34.202.193.197; envelope-from=spwhitton@spwhitton.name; helo=sendmail.purelymail.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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:292172 Archived-At: --=-=-= Content-Type: text/plain X-debbugs-cc: dgutov@yandex.ru, juri@linkov.net These patches add a new command, diff-discard-hunk, inspired by some functionality from Magit. It nicely complements a workflow based around committing with C-x v v from C-x v D. I have been using it every day for a year or so, and I think others will find it useful, too. Posting for comments. Thanks! -- Sean Whitton --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-New-user-option-diff-display-after-apply-hunk.patch >From 8d4911ed2db8a1ace388616bff35857559920f84 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 21 Sep 2024 10:51:23 +0100 Subject: [PATCH 1/2] New user option 'diff-display-after-apply-hunk' * lisp/vc/diff-mode.el (diff-display-after-apply-hunk): New option. (diff-apply-hunk): Use it. * etc/NEWS: Announce the new option. --- etc/NEWS | 6 ++++++ lisp/vc/diff-mode.el | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index 8bd5c7c9515..65e3a484e7a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -304,6 +304,12 @@ 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 user option 'diff-display-after-apply-hunk'. +This option can be customized to nil to inhibit how, by default, +'diff-apply-hunk' displays the changed text after applying the hunk. +It can also be let-bound by Lisp code wrapping 'diff-apply-hunk'. + ** php-ts-mode --- diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 4810b9ce01c..8af155c79e0 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -95,6 +95,10 @@ diff-advance-after-apply-hunk "Non-nil means `diff-apply-hunk' will move to the next hunk after applying." :type 'boolean) +(defcustom diff-display-after-apply-hunk t + "Non-nil means `diff-apply-hunk' will display the buffer it just changed." + :type 'boolean) + (defcustom diff-mode-hook nil "Run after setting up the `diff-mode' major mode." :type 'hook @@ -2024,7 +2028,8 @@ diff-apply-hunk (delete-region (car pos) (cdr pos)) (insert (car new))) ;; Display BUF in a window - (set-window-point (display-buffer buf) (+ (car pos) (cdr new))) + (when diff-display-after-apply-hunk + (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))) (diff-hunk-status-msg line-offset (xor switched reverse) nil) (when diff-advance-after-apply-hunk (diff-hunk-next)))))) -- 2.39.5 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-New-command-diff-discard-hunk.patch >From 076a74fd4798bd3908aa0cd4ed07ed85e03559c1 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 21 Sep 2024 11:16:18 +0100 Subject: [PATCH 2/2] New command 'diff-discard-hunk' * lisp/vc/diff-mode.el (diff-discard-hunk): New command. (diff-mode-map): Bind it 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 | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+) 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 65e3a484e7a..8da68eded82 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -310,6 +310,11 @@ This option can be customized to nil to inhibit how, by default, 'diff-apply-hunk' displays the changed text after applying the hunk. It can also be let-bound by Lisp code wrapping 'diff-apply-hunk'. ++++ +*** 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 8af155c79e0..3a6bc62b78a 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -222,6 +222,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 @@ -246,6 +247,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 @@ -2055,6 +2058,26 @@ diff-kill-applied-hunks (diff-hunk-kill) (diff-hunk-next))))) +(defun diff-discard-hunk () + "Reverse-apply and then kill the hunk at point. +Unless the buffer was already modified, save the 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) + (let* ((diff-advance-after-apply-hunk nil) + (diff-display-after-apply-hunk nil) + (buffer (car (diff-find-source-location))) + (to-save (and (not (buffer-modified-p buffer)) + buffer))) + (diff-apply-hunk t) + (diff-hunk-kill) + (when to-save + (with-current-buffer to-save + (save-buffer))))) + (defun diff-apply-buffer () "Apply the diff in the entire diff buffer. When applying all hunks was successful, then save the changed buffers." -- 2.39.5 --=-=-=--