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: Wed, 21 Aug 2024 19:40:01 +0000 Message-ID: <87wmk9acmm.fsf@posteo.net> References: <87wmxrwe6h.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> <86le0rwhlx.fsf@gnu.org> <87y14rv1ie.fsf@posteo.net> <86bk1nwdcj.fsf@gnu.org> <87ttffupsi.fsf@posteo.net> <86v7zvujbf.fsf@gnu.org> <87plq2vpx1.fsf@posteo.net> <86bk1muh4f.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="14150"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 65380@debbugs.gnu.org, juri@linkov.net To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Aug 21 21:40: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 1sgrCN-0003Uj-O4 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 21 Aug 2024 21:40:52 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sgrC0-0001rc-ST; Wed, 21 Aug 2024 15:40:29 -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 1sgrBr-0001rG-Qs for bug-gnu-emacs@gnu.org; Wed, 21 Aug 2024 15:40:21 -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 1sgrBp-0006NJ-3X for bug-gnu-emacs@gnu.org; Wed, 21 Aug 2024 15:40:18 -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=kDyQu7d3/adDPy3zb140EpXa9dGtdo+EKrcwlyfPwLE=; b=oeW9xHWUOprQkzsN7GcsYmqWpoepV+WLIyW4zMpJZzxn3K7Y8AYnYp78a3OmyC9nk9E7JZXbO1gmgJHGb/O7QWqZG7zNuVO8a2ptfY+szr+blQArC4fJx6TpqRh+NQwjFy6K85Pb9Ls/b6rSsAMk6fvHXrouQa/YMM5S36QUJllua0szPOs5eLNiJj0dMCzYXhWctFDFrKcsBuqMtYyELWw1wEITi6xIDJFESqPtzeufDp/1aWIiJ9KogQCkPZupO5olbSqMWUD4B7tKrDSt00LUb4pAfZmKICJd+P0gOAkOmx8on1RudIygfgC5/UTxg4EJxJ+qicgbmVAmoxIsxw==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sgrCX-0005zV-IY for bug-gnu-emacs@gnu.org; Wed, 21 Aug 2024 15:41: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: Wed, 21 Aug 2024 19:41: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.172426925923014 (code B ref 65380); Wed, 21 Aug 2024 19:41:01 +0000 Original-Received: (at 65380) by debbugs.gnu.org; 21 Aug 2024 19:40:59 +0000 Original-Received: from localhost ([127.0.0.1]:36389 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sgrCV-0005z7-0q for submit@debbugs.gnu.org; Wed, 21 Aug 2024 15:40:59 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:45625) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sgrCQ-0005yp-BY for 65380@debbugs.gnu.org; Wed, 21 Aug 2024 15:40:57 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 8E167240106 for <65380@debbugs.gnu.org>; Wed, 21 Aug 2024 21:40:03 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1724269203; bh=x3Hrk0mSZIuwoawmASRMFfQ5+deGu8MQr7Byaklo2ok=; h=From:To:Cc:Subject:Autocrypt:OpenPGP:Date:Message-ID:MIME-Version: Content-Type:From; b=oz0ZiUgh3//hZS1BCIxmKb4CORXJh7qbag/YbLscZ2xWjebe3bY4yvqik0nGie2ox XYBaR3G6su3dvZRWGiff17Wb4/WWZkrGdTxKN2FfuocQoPkbRwSJ7elQCaEByGZHPm uyLDJptRj1JTrMw5nVcoQR9llxarMCgIJ07kdY8IDIM5+BaOaIYoPxyk/9cv3hU60C +k+yvO4a7y4lRjiRALzmQIsO+dZdDKGDv1jVXCIi67OIeGlgMS3K2SbZT76V4H24se F/Y2AdWhIhuvNr2dMAkWDuwiLQhAf5VKT4mQDeX/4O9oNBUXEyYpHcdx2ts4mLoZ9G uu4SIMmazpNMw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4WpxW648Cfz6twf; Wed, 21 Aug 2024 21:40:02 +0200 (CEST) In-Reply-To: <86bk1muh4f.fsf@gnu.org> (Eli Zaretskii's message of "Wed, 21 Aug 2024 16:42:40 +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:290501 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Philip Kaludercic >> Cc: juri@linkov.net, 65380@debbugs.gnu.org >> Date: Tue, 20 Aug 2024 21:35:06 +0000 >> >> > Yes, but the connection between that and what you mean by "deleted >> > text" might not be obvious to everyone. >> >> OK on both counts, here is the updated version: > > Thanks. > >> +*** New command 'diff-kill-ring-save' >> +This command copies out the modified contents out of a diff, without > ^^^ ^^^ > One of these two "outs"s should be removed, I think. > >> +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 think it is better to say "copies to the kill-ring the modified > contents..." Ugh, I forgot to amend by last patch, this is the current version: This command copies to the 'kill-ring' a region of text modified according to diffs in the current buffer, but without applying the diffs to the original text. If the selected range extends a hunk, the commands attempts to look up and copy the text in-between the hunks. I am thinking about splitting the first sentence up into This command copies text out of a diff to the 'kill-ring'. By default it will use the text the diff would modify, without having to apply a hunk. If the selected range extends a hunk, the commands attempts to look up and copy the text in-between the hunks. WDYT? Here is the patch as it is: --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Add-command-to-copy-contents-in-a-diff-mode-buffer.patch >From 494b9ea69a501f3420c8eee1080d7d45637e39d5 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 | 10 +++++++++ lisp/vc/diff-mode.el | 52 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index b89a80aa14d..25ceb408e2c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -210,6 +210,16 @@ 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 to the 'kill-ring' a region of text modified +according to diffs in the current buffer, but without applying the +diffs to the original text. If the selected range extends a hunk, the +commands attempts to look up and copy the text in-between the +hunks. + * New Modes and Packages in Emacs 31.1 diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 81e8b23ee33..4810b9ce01c 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,55 @@ 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 to `kill-ring' the result of applying diffs in region between BEG and END. +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 lines that the +diff would remove (beginning with \"+\" or \"<\")." + (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 --=-=-=--