From: Sean Whitton <spwhitton@spwhitton.name>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: 73387@debbugs.gnu.org, monnier@iro.umontreal.ca,
Juri Linkov <juri@linkov.net>
Subject: bug#73387: 30.0.90; C-x v v in diff-mode doesn't work after C-c C-n
Date: Mon, 30 Sep 2024 21:25:26 +0800 [thread overview]
Message-ID: <87a5fpqnnd.fsf@melete.silentflame.com> (raw)
In-Reply-To: <87ikudqocz.fsf@melete.silentflame.com> (Sean Whitton's message of "Mon, 30 Sep 2024 21:10:04 +0800")
[-- Attachment #1: Type: text/plain, Size: 598 bytes --]
Hello,
On Mon 30 Sep 2024 at 09:10pm +08, Sean Whitton wrote:
> Hello,
>
> On Mon 30 Sep 2024 at 01:11pm +03, Dmitry Gutov wrote:
>
>> Just these two points:
>>
>> - add a command which does the kill-all-but-this-hunk (or hunks in
>> region if mark active) thing -- it's generally useful.
>>
>> - make C-x v v on a narrowed buffer, by default, issue a message saying
>> "Cannot commit patch when narrowed, consider <binding of new command>"
>
> Okay, what do you think to the attached?
Slightly simplified v2 attached -- uses mapconcat instead of (apply
#'user-error ...).
--
Sean Whitton
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2-0001-New-command-diff-delete-other-hunks.patch --]
[-- Type: text/x-diff, Size: 5477 bytes --]
From 61342a10072ddfb0e8f89fbfa5a86cd4284c3142 Mon Sep 17 00:00:00 2001
From: Sean Whitton <spwhitton@spwhitton.name>
Date: Mon, 30 Sep 2024 21:08:38 +0800
Subject: [PATCH v2] New command diff-delete-other-hunks
* lisp/vc/diff-mode.el (diff-delete-other-hunks): New
command (bug#73387).
(diff-mode-map): Bind the new command to C-c RET k.
(diff-mode-menu): New entry for the new command.
(vc-next-action): Stop, and warn, if the user attempts to commit
a patch from a narrowed buffer (bug#73387).
* doc/emacs/files.texi (Diff Mode):
* etc/NEWS: Document the new command.
---
doc/emacs/files.texi | 5 +++++
etc/NEWS | 5 +++++
lisp/vc/diff-mode.el | 35 +++++++++++++++++++++++++++++++++++
lisp/vc/vc.el | 11 +++++++++++
4 files changed, 56 insertions(+)
diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi
index a3a8c854aa6..a32689552b1 100644
--- a/doc/emacs/files.texi
+++ b/doc/emacs/files.texi
@@ -1703,6 +1703,11 @@ Diff Mode
Apply all the hunks in the buffer (@code{diff-apply-buffer}). If the
diffs were applied successfully, save the changed buffers.
+@findex diff-delete-other-hunks
+@item C-c @key{RET} k
+Delete all hunks other than the current hunk. If the region is active,
+then delete all hunks other than those the region overlaps.
+
@findex diff-refine-hunk
@item C-c C-b
Highlight the changes of the hunk at point with a finer granularity
diff --git a/etc/NEWS b/etc/NEWS
index cdc7f47b7a9..90213cf342a 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -369,6 +369,11 @@ This command reverts the hunk at point (i.e., applies the reverse of the
hunk), and then removes the hunk from the diffs. This is useful to undo
commits when you are in buffers generated by 'C-x v =' and 'C-x v D'.
++++
+*** New command 'diff-delete-other-hunks' bound to C-c RET k.
+This command deletes all hunks other than the current hunk.
+It is useful to prepare a *vc-diff* buffer for committing a single hunk.
+
** php-ts-mode
---
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 25c6238765d..ff141e19c50 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -220,6 +220,7 @@ diff-mode-map
"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-m k" #'diff-delete-other-hunks
"C-c C-e" #'diff-ediff-patch
"C-c C-n" #'diff-restrict-view
"C-c C-s" #'diff-split-hunk
@@ -278,6 +279,8 @@ diff-mode-menu
:help "Kill current hunk"]
["Kill current file's hunks" diff-file-kill
:help "Kill all current file's hunks"]
+ ["Delete other hunks" diff-delete-other-hunks
+ :help "Delete hunks other than the current hunk"]
"-----"
["Previous Hunk" diff-hunk-prev
:help "Go to the previous count'th hunk"]
@@ -814,6 +817,38 @@ diff-hunk-kill
(goto-char (car bounds))
(ignore-errors (diff-beginning-of-hunk t)))))
+;; This is not `diff-kill-other-hunks' because we might need to make
+;; copies of file headers in order to ensure the new kill ring entry
+;; would be a patch with the same meaning. That is not implemented
+;; because it does not seem like it would be useful to anyone.
+(defun diff-delete-other-hunks (&optional beg end)
+ "Delete hunks other than this one.
+Interactively, if the region is active, then delete all hunks that the
+region does not overlap.
+When called from Lisp, the region to act upon is specified by optional
+arguments BEG and END."
+ (interactive (list (use-region-beginning) (use-region-end)))
+ (when (buffer-narrowed-p)
+ (user-error "Command is not safe in a narrowed buffer"))
+ (let ((inhibit-read-only t))
+ (save-excursion
+ (cond ((xor beg end)
+ (error "Require exactly zero or two arguments"))
+ (beg
+ (goto-char beg)
+ (setq beg (car (diff-bounds-of-hunk)))
+ (goto-char end)
+ (setq end (cadr (diff-bounds-of-hunk))))
+ (t
+ (pcase-setq `(,beg ,end) (diff-bounds-of-hunk))))
+ (delete-region end (point-max))
+ (goto-char beg)
+ (diff-beginning-of-file)
+ (diff-hunk-next)
+ (delete-region (point) beg)
+ (diff-beginning-of-file-and-junk)
+ (delete-region (point-min) (point)))))
+
(defun diff-beginning-of-file-and-junk ()
"Go to the beginning of file-related diff-info.
This is like `diff-beginning-of-file' except it tries to skip back over leading
diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index 597a1622f5a..6ebfedc9555 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -1302,6 +1302,17 @@ vc-next-action
;; Fileset comes from a diff-mode buffer, see
;; 'diff-vc-deduce-fileset', and the buffer is the patch to apply.
((eq model 'patch)
+ (when (buffer-narrowed-p)
+ ;; If user used `diff-restrict-view' then we may not have the
+ ;; file header and the commit will not succeed (bug#73387).
+ (user-error "Cannot commit patch when narrowed; consider %s"
+ (mapconcat (lambda (c)
+ (key-description
+ (where-is-internal c nil t)))
+ '(widen
+ diff-delete-other-hunks
+ vc-next-action)
+ " ")))
(vc-checkin files backend nil nil nil (buffer-string)))
((or (null state) (eq state 'unregistered))
(cond (verbose
--
2.45.2
next prev parent reply other threads:[~2024-09-30 13:25 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-20 16:08 bug#73387: 30.0.90; C-x v v in diff-mode doesn't work after C-c C-n Sean Whitton
2024-09-22 12:46 ` Sean Whitton
2024-09-23 22:41 ` Dmitry Gutov
2024-09-23 22:41 ` Dmitry Gutov
2024-09-24 6:32 ` Juri Linkov
2024-09-24 15:54 ` Sean Whitton
2024-09-24 17:36 ` Dmitry Gutov
2024-09-25 6:34 ` Sean Whitton
2024-09-25 23:46 ` Dmitry Gutov
2024-09-27 11:55 ` Sean Whitton
2024-09-27 19:13 ` Dmitry Gutov
2024-09-29 23:46 ` Sean Whitton
2024-09-30 0:27 ` Dmitry Gutov
2024-09-30 9:38 ` Sean Whitton
2024-09-30 10:11 ` Dmitry Gutov
2024-09-30 13:10 ` Sean Whitton
2024-09-30 13:25 ` Sean Whitton [this message]
2024-09-30 14:15 ` Eli Zaretskii
2024-10-01 0:50 ` Sean Whitton
2024-10-01 15:51 ` Eli Zaretskii
2024-10-01 19:13 ` Dmitry Gutov
2024-10-02 6:17 ` Eli Zaretskii
2024-10-02 1:26 ` Sean Whitton
2024-10-02 7:15 ` Eli Zaretskii
2024-10-03 0:50 ` Sean Whitton
2024-10-03 6:33 ` Eli Zaretskii
2024-10-03 7:06 ` Sean Whitton
2024-10-03 11:07 ` Eli Zaretskii
2024-10-03 11:15 ` Dmitry Gutov
2024-10-03 11:36 ` Sean Whitton
2024-10-03 12:15 ` Eli Zaretskii
2024-10-04 1:41 ` Sean Whitton
2024-10-01 0:27 ` Dmitry Gutov
2024-10-01 0:57 ` Sean Whitton
2024-10-01 0:39 ` Dmitry Gutov
2024-10-01 1:01 ` Sean Whitton
2024-10-01 1:15 ` Dmitry Gutov
2024-10-01 1:40 ` Sean Whitton
2024-10-01 1:57 ` Dmitry Gutov
2024-10-01 2:41 ` Sean Whitton
2024-10-01 13:55 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-02 1:24 ` Sean Whitton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a5fpqnnd.fsf@melete.silentflame.com \
--to=spwhitton@spwhitton.name \
--cc=73387@debbugs.gnu.org \
--cc=dgutov@yandex.ru \
--cc=juri@linkov.net \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.