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: Sat, 17 Aug 2024 16:34:17 +0000 Message-ID: <871q2n152u.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> 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="35582"; 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 Sat Aug 17 18:35:50 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 1sfMP6-00098E-Uc for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 17 Aug 2024 18:35:49 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sfMOl-0001HA-1n; Sat, 17 Aug 2024 12:35:27 -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 1sfMOj-0001Gv-9p for bug-gnu-emacs@gnu.org; Sat, 17 Aug 2024 12:35:25 -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 1sfMOj-0002eN-0t for bug-gnu-emacs@gnu.org; Sat, 17 Aug 2024 12:35:25 -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=WKCRss7xQNkUJZQubePOVeFRokg4K4CDQccj0bbg18U=; b=INcR0Nq7nuYY5eqso7ocToNl5IE/x1WbkMhSxXt1Fy43TT+I7nDRYeyGz4gzV2mKJ+/BJubduIjA8uQ55n8n8rm4oKzvotTLPDzgT33NLGSeUkn7j98jqawJuSmIm1z9fbD8nDMBIXDI4IS6ztn/Ue6wbuItfj2KUCkmuzR5uymOcg/vcX//9Sm68rh61Zjqs9IUOtr7KB6YczizzCUJwqgso5Rtfv85jsisL7OUj13uyg7ooXKmLYfPko8ewARRxcAEL1D0m+m775+GdPUCQc1DXb5B0peLWgrUmZnAX71UZhsUWcSllWAfK/AWnleTfMK9ft1IUjfCnhOD0o09Aw==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sfMPK-0006Bk-2o for bug-gnu-emacs@gnu.org; Sat, 17 Aug 2024 12:36:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 17 Aug 2024 16:36:02 +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.172391251123721 (code B ref 65380); Sat, 17 Aug 2024 16:36:02 +0000 Original-Received: (at 65380) by debbugs.gnu.org; 17 Aug 2024 16:35:11 +0000 Original-Received: from localhost ([127.0.0.1]:54877 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sfMOV-0006AW-9W for submit@debbugs.gnu.org; Sat, 17 Aug 2024 12:35:11 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:55541) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sfMOR-00069t-8o for 65380@debbugs.gnu.org; Sat, 17 Aug 2024 12:35:09 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 4831C240104 for <65380@debbugs.gnu.org>; Sat, 17 Aug 2024 18:34:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1723912461; bh=OBjX0CpRDsSD0drVeBDe90xNCDNIEiXfr+MiBmrwbIk=; h=From:To:Cc:Subject:Autocrypt:OpenPGP:Date:Message-ID:MIME-Version: Content-Type:From; b=hNeedueEpKxsQEz4O1hOkM4KabeHBmzWN9J2HzqUW7dCqWkF8NaXa/BZujwERxZh7 hUWylCW0Czt7O4LC/GURSoOz2EqUlWo4WHYRI0SzoFhvCWAIsvtAam8SI+28j6cqOf SL4CvlXnN7mxjXevMVGJTqONpbAUx4Ycfr0jtzmzw7MkuhqT7VS3Nc12lKu15h7ZoR mIQqFPdua4fPBuJsGKCcIbVa8Uq9xENjTW81QglFtuLMrxEoMwx1dRlU1sUAQ6xvpy YM8ZrnxBlvWFSVRKH5P7pZ7HkuKHUdOiTJDTCL9UoVf95BFiQN7s2BjLCGttICEfS+ RVUH8TeCskGuQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4WmPZf4Zmvz6tsf; Sat, 17 Aug 2024 18:34:18 +0200 (CEST) In-Reply-To: <83fs4btiw1.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 22 Aug 2023 14:29:34 +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:290273 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Philip Kaludercic >> Cc: juri@linkov.net, 65380@debbugs.gnu.org >> Date: Tue, 22 Aug 2023 11:06:28 +0000 >> >> Eli Zaretskii writes: >> >> >> From: Philip Kaludercic >> >> Cc: Juri Linkov , 65380@debbugs.gnu.org >> >> Date: Sun, 20 Aug 2023 18:24:53 +0000 >> >> >> >> Eli Zaretskii writes: >> >> >> >> >> From: Juri Linkov >> >> >> Cc: Philip Kaludercic , 65380@debbugs.gnu.org >> >> >> Date: Sun, 20 Aug 2023 19:30:03 +0300 >> >> >> >> >> >> Regarding the implementation, there is the function >> >> >> 'diff-hunk-text', but it's limited to one hunk only. >> >> > >> >> > So is the proposed new function, isn't it? >> >> >> >> No, the current proposal doesn't have any special handling for text >> >> between hunks. >> > >> > AFAIU, the function you proposed removes the first character from each >> > line in the region, so how will it handle multiple hunks? >> > >> > Or maybe I misunderstood what you meant by "No"? >> >> The command does not error when the region selects more than just a >> sub-region of a hunk, but it doesn't do anything useful either. > > Then how about making a command that is basically a wrapper around > diff-hunk-text, and allows to copy bodies of N hunks, given an > argument of N? Wouldn't that be more useful? Sorry for the unreasonably delay in responding to this message. I've finally come up with something acceptable: --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-command-to-copy-contents-in-a-diff-mode-buffer.patch >From 463e555d2bd4837f02163ef3e93df85c7dc092da 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 new command * etc/NEWS: Mention 'diff-kill-ring-save'. --- lisp/vc/diff-mode.el | 46 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 81e8b23ee33..94e40f5900b 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,49 @@ 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 (list (region-beginning) (region-end) + current-prefix-arg)) + (unless (derived-mode-p 'diff-mode) + (user-error "Command can only be invoked in a diff-buffer")) + (revert-buffer) ;refresh the diff + (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 (start) + (save-window-excursion + (save-excursion + (forward-line -1) + (diff-goto-source) + (forward-line +1) + (setq start (point)))) + (save-window-excursion + (diff-goto-source) + (push (buffer-substring start (point)) + parts)))))) + (kill-new (string-join (nreverse parts))) + (setq deactivate-mark t) + (message "Copied text"))) (defun diff-current-defun () "Find the name of function at point. -- 2.46.0 --=-=-= Content-Type: text/plain This will also copy text between hunks. I've tested it a bit, but I am sure someone else here might find some edge-cases. -- Philip Kaludercic on peregrine --=-=-=--