From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#65380: [PATCH] Add command to copy contents in a diff-mode buffer Date: Tue, 20 Aug 2024 07:46:43 +0000 Message-ID: <8734mzws9o.fsf@posteo.net> References: <87wmxrwe6h.fsf@posteo.net> <83o7j3z4wm.fsf@gnu.org> <87pm3jwboc.fsf@posteo.net> <83lee7z3xs.fsf@gnu.org> <87jztrvxx2.fsf@posteo.net> <83r0nyyhlz.fsf@gnu.org> <86o7j2qgmc.fsf@mail.linkov.net> <83cyzhwpcj.fsf@gnu.org> <87a5ultvuy.fsf@posteo.net> <83a5ulwos6.fsf@gnu.org> <87fs4bgwuj.fsf@posteo.net> <83fs4btiw1.fsf@gnu.org> <871q2n152u.fsf@posteo.net> <87frr1zw6i.fsf@posteo.net> <864j7hzviv.fsf@gnu.org> <87bk1pztsr.fsf@posteo.net> <861q2lzp70.fsf@gnu.org> <87ed6kxq5j.fsf@posteo.net> <86a5h73d89.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="23740"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 65380@debbugs.gnu.org, Eli Zaretskii To: Juri Linkov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Aug 20 09:47:52 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 1sgJao-0005xD-N6 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 20 Aug 2024 09:47:51 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sgJaL-00074m-PW; Tue, 20 Aug 2024 03:47:22 -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 1sgJaJ-00074N-JA for bug-gnu-emacs@gnu.org; Tue, 20 Aug 2024 03:47:19 -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 1sgJaJ-00064Y-A4 for bug-gnu-emacs@gnu.org; Tue, 20 Aug 2024 03:47:19 -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=Bc3ViKCKwxhP9yvFD7oh4z5UCqBFeTeTMGdngUA7GnQ=; b=RsxR8QfN8IcxMnKb6H4r3zTkn3m8zrbrcvcnsb/1TNiD30HPZZFHNhS+4uRrQd8EdLl3bdIJ/wRmXYO8p2+B7QYB8TqR/0RBmxYnHZ429bCFbQb64JHsu/zDZC5y9z3qutEFRRtv928mKdKf4chfqUCJlRXvBWu8oGo07loBVyAhLst4TiVhb9ajdpsuEL5TM60CqHgjtEmEOH8TYKDxWt85I0YaVVAg/s6VQalrNs69kklRXFjiv5rb6D9nlirkeb+EbhP5YHNk5B+gCPFi26bIvfDxds32HCQDl0PwLrULiwb2NOknvyy+Slqkxm/+Ze1wJMP5U4Mt1HBZlfPbGQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sgJaz-00017t-KP for bug-gnu-emacs@gnu.org; Tue, 20 Aug 2024 03:48:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 20 Aug 2024 07:48:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65380 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 65380-submit@debbugs.gnu.org id=B65380.17241400694309 (code B ref 65380); Tue, 20 Aug 2024 07:48:01 +0000 Original-Received: (at 65380) by debbugs.gnu.org; 20 Aug 2024 07:47:49 +0000 Original-Received: from localhost ([127.0.0.1]:59749 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sgJam-00017R-IX for submit@debbugs.gnu.org; Tue, 20 Aug 2024 03:47:49 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:57499) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sgJaZ-000170-Gf for 65380@debbugs.gnu.org; Tue, 20 Aug 2024 03:47:48 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 4A082240029 for <65380@debbugs.gnu.org>; Tue, 20 Aug 2024 09:46:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1724140006; bh=nEZFeVPQHR7FmVnGcD9tA12vH6KQGFTzh2BmkpfQGIM=; h=From:To:Cc:Subject:Autocrypt:OpenPGP:Date:Message-ID:MIME-Version: Content-Type:From; b=HYhNBX8ztpG+pJlT90LRiKeq4aZUKFcLTpFQ/YGW7DWAdNYaILG9RqDFEmmp3KNiz hBIt1g3r+9pAPEmXjEkNp8U4qv+uZ2aL918zoRmWGy+nBOl8t+o8IY96STSS3JwYGm Ij7Js47FuHPGwxe0adNNiJ/pjZznHttgy9g/If1ORjkGycy0xi4YpK1c0Sscp86+VA FaY9WXcbMM98Q8gwX07MoFjDXMrfcky8br0PXuNy7vzTu0rmEgnBVTOr895SN6rY68 uPHq51RgqmtXr1GQGqFpUPW1pvG9sJrkLpXTPJEBPQZeU1OtphzuLWBxSxgomcJD+w IkUoBVzWk6CAg== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Wp1kX5vYgz6v0V; Tue, 20 Aug 2024 09:46:44 +0200 (CEST) In-Reply-To: <86a5h73d89.fsf@mail.linkov.net> (Juri Linkov's message of "Tue, 20 Aug 2024 09:44:22 +0300") Autocrypt: addr=philipk@posteo.net; keydata= mDMEZBBQQhYJKwYBBAHaRw8BAQdAHJuofBrfqFh12uQu0Yi7mrl525F28eTmwUDflFNmdui0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiWBBMWCAA+FiEEDg7HY17ghYlni8XN8xYDWXahwukFAmQQUEICGwMFCQHhM4AFCwkI BwIGFQoJCAsCBBYCAwECHgECF4AACgkQ8xYDWXahwulikAEA77hloUiSrXgFkUVJhlKBpLCHUjA0 mWZ9j9w5d08+jVwBAK6c4iGP7j+/PhbkxaEKa4V3MzIl7zJkcNNjHCXmvFcEuDgEZBBQQhIKKwYB BAGXVQEFAQEHQI5NLiLRjZy3OfSt1dhCmFyn+fN/QKELUYQetiaoe+MMAwEIB4h+BBgWCAAmFiEE Dg7HY17ghYlni8XN8xYDWXahwukFAmQQUEICGwwFCQHhM4AACgkQ8xYDWXahwukm+wEA8cml4JpK NeAu65rg+auKrPOP6TP/4YWRCTIvuYDm0joBALw98AMz7/qMHvSCeU/hw9PL6u6R2EScxtpKnWof z4oM OpenPGP: id=philipk@posteo.net; url="https://keys.openpgp.org/vks/v1/by-email/philipk@posteo.net"; preference=signencrypt 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:290428 Archived-At: --=-=-= Content-Type: text/plain Juri Linkov writes: >> I'm attaching the newest version of the patch here: > > Thanks, I tried it out, and it works nicely for the single hunk case. 1+ >> +*** New command 'diff-kill-ring-save' >> +This command copies out the modified contents out of a diff, without >> +having to apply it first. If the selected range extends a hunk, the >> +commands attempts to look up and copy the text between from the >> +referenced file. > > I'm not sure about usefulness of the last part in multi-hunk case. > Does someone really need to copy a huge part of the source file > between hunks at the top and bottom? I expected that multi-hunk case > would copy only concatenated text of hunks, not the source file. > But I have no strong opinion about this. The main scenario I had in mind was when someone modifies the beginning and the end of a function, and I want to copy all of it at once. I agree that there is usually not much of a use for copying huge parts of a source file, but we get that for free when the beginning-to-end of a function case is handled. > My main use case will be > to copy the text of the current hunk. Could you please support > this case where typing 'w' would copy the current hunk when the > region is not activated. Done, see below. >> @@ -630,6 +631,22 @@ diff-end-of-hunk >> +(defun diff-beginning-of-hunk-position (&optional try-harder) >> + "Call `diff-end-of-hunk' without moving. >> +The optional argument TRY-HARDER is passed on to >> +`diff-beginning-of-hunk'." >> + (save-excursion >> + (save-window-excursion >> + (diff-beginning-of-hunk try-harder)))) >> + >> +(defun diff-end-of-hunk-position (&optional style donttrustheader) >> + "Call `diff-end-of-hunk' without moving. >> +The optional arguments STYLE and DONTTRUSTHEADER are passed on to >> +`diff-end-of-hunk'." >> + (save-excursion >> + (save-window-excursion >> + (diff-end-of-hunk style donttrustheader)))) > > I don't understand why separate functions with 'save-window-excursion' > are needed here, since all other uses of 'diff-beginning-of-hunk' > just wrap the calls with 'save-excursion'. You are right, it isn't needed anymore, and I have removed it. Here is the updated patch: --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-command-to-copy-contents-in-a-diff-mode-buffer.patch >From c19e18e3a2fd8df3547ddebce0f63c1620e7d2f6 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Sat, 19 Aug 2023 11:47:54 +0200 Subject: [PATCH] Add command to copy contents in a diff-mode buffer * lisp/vc/diff-mode.el (diff-mode-shared-map): Bind 'diff-kill-ring-save'. (diff-mode-map): Ensure the "w" binding does not get prefixed. (diff-kill-ring-save): Add the command. * etc/NEWS: Mention 'diff-kill-ring-save'. (Bug#65380) --- etc/NEWS | 9 ++++++++ lisp/vc/diff-mode.el | 51 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index b89a80aa14d..e493c1bb035 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -210,6 +210,15 @@ The host name for Kubernetes connections can be of kind used. This overrides the setiing in 'tramp-kubernetes-namespace', if any. +** Diff + +--- +*** New command 'diff-kill-ring-save' +This command copies out the modified contents out of a diff, without +having to apply it first. If the selected range extends a hunk, the +commands attempts to look up and copy the text between from the +referenced file. + * New Modes and Packages in Emacs 31.1 diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 81e8b23ee33..5caf2cb5e5b 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -196,6 +196,7 @@ diff-mode-shared-map "RET" #'diff-goto-source "" #'diff-goto-source "W" #'widen + "w" #'diff-kill-ring-save "o" #'diff-goto-source ; other-window "A" #'diff-ediff-patch "r" #'diff-restrict-view @@ -208,7 +209,7 @@ diff-mode-map ;; We want to inherit most bindings from ;; `diff-mode-shared-map', but not all since they may hide ;; useful `M-' global bindings when editing. - (dolist (key '("A" "r" "R" "g" "q" "W" "z")) + (dolist (key '("A" "r" "R" "g" "q" "W" "w" "z")) (keymap-set map key nil)) map) ;; From compilation-minor-mode. @@ -2108,6 +2109,54 @@ diff-goto-source (goto-char (+ (car pos) (cdr src))) (when buffer (next-error-found buffer (current-buffer)))))) +(defun diff-kill-ring-save (beg end &optional reverse) + "Save contents of the region between BEG and END akin to `kill-ring-save'. +By default the command will copy the text that applying the diff would +produce, along with the text between hunks. If REVERSE is non-nil, or +the command was invoked with a prefix argument, copy the deleted text." + (interactive + (append (if (use-region-p) + (list (region-beginning) (region-end)) + (save-excursion + (list (diff-beginning-of-hunk) + (diff-end-of-hunk)))) + (list current-prefix-arg))) + (unless (derived-mode-p 'diff-mode) + (user-error "Command can only be invoked in a diff-buffer")) + (let ((parts '())) + (save-excursion + (goto-char beg) + (catch 'break + (while t + (let ((hunk (diff-hunk-text + (buffer-substring + (save-excursion (diff-beginning-of-hunk)) + (save-excursion (min (diff-end-of-hunk) end))) + (not reverse) + (save-excursion + (- (point) (diff-beginning-of-hunk)))))) + (push (substring (car hunk) (cdr hunk)) + parts)) + ;; check if we have copied everything + (diff-end-of-hunk) + (when (<= end (point)) (throw 'break t)) + ;; copy the text between hunks + (let ((inhibit-message t) start) + (save-window-excursion + (save-excursion + (forward-line -1) + ;; FIXME: Detect if the line we jump to doesn't match + ;; the line in the diff. + (diff-goto-source t) + (forward-line +1) + (setq start (point)))) + (save-window-excursion + (diff-goto-source t) + (push (buffer-substring start (point)) + parts)))))) + (kill-new (string-join (nreverse parts))) + (setq deactivate-mark t) + (message (if reverse "Copied original text" "Copied modified text")))) (defun diff-current-defun () "Find the name of function at point. -- 2.46.0 --=-=-= Content-Type: text/plain -- Philip Kaludercic on peregrine --=-=-=--