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: Sun, 18 Aug 2024 15:29:25 +0000 Message-ID: <87frr1zw6i.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> 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="38411"; 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 Sun Aug 18 17:30: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 1sfhrl-0009qq-6y for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 18 Aug 2024 17:30:49 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sfhrR-0004gv-4V; Sun, 18 Aug 2024 11:30: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 1sfhrK-0004gB-L6 for bug-gnu-emacs@gnu.org; Sun, 18 Aug 2024 11:30:24 -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 1sfhrK-0006IK-5f for bug-gnu-emacs@gnu.org; Sun, 18 Aug 2024 11:30:22 -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=BSJwrTcfCmGWaAO/UMgVW2fF6wENtTq5TrwE8GDz+/4=; b=ljc4VNRhpRXipdbEBQVcnMSidD+puLu0+JCr8NZRBBVtzMeGE/TPUHMMMGexhk9f88DfPi19CM3vdWL6UfS+VT0hMiLhMN0S7ueV7vl9TYDcjVtgKRTn8IO4dWdvWpPSealup5eyVRkfUC4RzTDbJg9pSLxKAmJhMFcGDRgwjdFwZZXvgQ7c140wzfYz60GhSUCGBuQuYk+2Yob76rOsqYcJK8ZR0jL75Ubyei0dRPW6aZfNIu4hfN+d9Rm2mHmPETbODIy7YlYCMO+M4KYfrGsE3GoJc7Ct3+pKSSjFYjdL4Rp2HwQMAJAEAMg3Af0nH7uJ5E4IcfRPe7rNgnBh5Q==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sfhry-0001cc-Cb for bug-gnu-emacs@gnu.org; Sun, 18 Aug 2024 11:31: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: Sun, 18 Aug 2024 15:31: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.17239950186167 (code B ref 65380); Sun, 18 Aug 2024 15:31:02 +0000 Original-Received: (at 65380) by debbugs.gnu.org; 18 Aug 2024 15:30:18 +0000 Original-Received: from localhost ([127.0.0.1]:56498 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sfhrF-0001bO-Tb for submit@debbugs.gnu.org; Sun, 18 Aug 2024 11:30:18 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:40325) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sfhrD-0001b8-8x for 65380@debbugs.gnu.org; Sun, 18 Aug 2024 11:30:16 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 93E25240028 for <65380@debbugs.gnu.org>; Sun, 18 Aug 2024 17:29:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1723994968; bh=MbclzdMuWINrgMI4zJWkywJL3WekMM2HuXNZj94PyYw=; h=From:To:Cc:Subject:Autocrypt:OpenPGP:Date:Message-ID:MIME-Version: Content-Type:From; b=aaOhDGk1fbLU3uzGDBRA9O2QISe4zyeE5lBm5FvNnL0g93UByZDUNph7SXPo+/RSP LtsxQSKZgcO2rkk0/CzEkRS6Pf5YwWhYMN86LjWsUq+QIb7hgkPeXz1jUhTwqFVuZB ctj1xUQTiuBkulJ0u1q1h3HE0X5M+3foMhj2d4OngNdrZRxc7FJl66UDJv2yAY56je Qh4ID04bNVI2Akridxb7PtM7jJkjBkSk+ypS9V3bLVlf4IotIohdhPaZ3en8SHqhjN cagCBYCV2OKrvg2GwL+Td0XAxWk7ZDjvFy7Ez/F1P4cd5zCeQ+v80Gby5x2+5gSbeD 2le+OxVc105nw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Wn05M2xpBz9rxG; Sun, 18 Aug 2024 17:29:26 +0200 (CEST) In-Reply-To: <871q2n152u.fsf@posteo.net> (Philip Kaludercic's message of "Sat, 17 Aug 2024 18:34:17 +0200") 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:290327 Archived-At: --=-=-= Content-Type: text/plain Philip Kaludercic writes: > + ;; copy the text between hunks > + (let (start) > + (save-window-excursion > + (save-excursion > + (forward-line -1) > + (diff-goto-source) > + (forward-line +1) > + (setq start (point)))) One issue I still have here is that the `forwarad-line' might scroll the window, which `save-window-excursion' doesn't restore. To fix this I have written a general macro for subr.el to restore the scroll position: --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-Add-new-macro-to-restore-the-scroll-position-of-a-wi.patch >From c289c114ac381105cb550b3805041ad5cf5e61a5 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Sun, 18 Aug 2024 17:26:01 +0200 Subject: [PATCH 2/2] Add new macro to restore the scroll position of a window * lisp/subr.el (save-window-start): Add macro. --- lisp/subr.el | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lisp/subr.el b/lisp/subr.el index 9ea18ca5bff..003e27f2ad7 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -4922,6 +4922,16 @@ save-window-excursion (unwind-protect (progn ,@body) (set-window-configuration ,c))))) +(defmacro save-window-start (&rest body) + "Execute BODY, then restore the starting position of the selected window." + (declare (indent 0) (debug t)) + (let ((window (make-symbol "window")) + (start (make-symbol "start"))) + `(let* ((,window (selected-window)) + (,start (window-start ,window))) + (unwind-protect ,(macroexp-progn body) + (set-window-start ,window ,start t))))) + (defun internal-temp-output-buffer-show (buffer) "Internal function for `with-output-to-temp-buffer'." (with-current-buffer buffer -- 2.46.0 --=-=-= Content-Type: text/plain and updated the previous patch accordingly (+ some other minor improvements): --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-command-to-copy-contents-in-a-diff-mode-buffer.patch >From 4565d12cdcbdb22c316f741e656d5388e880201c Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Sat, 19 Aug 2023 11:47:54 +0200 Subject: [PATCH 1/2] 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-beginning-of-hunk-position, diff-end-of-hunk-position): New utility functions. (diff-kill-ring-save): Add the command. * etc/NEWS: Mention 'diff-kill-ring-save'. (Bug#65380) --- lisp/vc/diff-mode.el | 59 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 81e8b23ee33..2a41862e363 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. @@ -630,6 +631,22 @@ diff-end-of-hunk ;; The return value is used by easy-mmode-define-navigation. (goto-char (or end (point-max))))) +(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)))) + ;; "index ", "old mode", "new mode", "new file mode" and ;; "deleted file mode" are output by git-diff. (defconst diff-file-junk-re @@ -2108,6 +2125,46 @@ 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 t t t) ;refresh the diff + (let ((parts '())) + (save-excursion + (goto-char beg) + (catch 'break + (while t + (let ((hunk (diff-hunk-text + (buffer-substring + (diff-beginning-of-hunk-position) + (min (diff-end-of-hunk-position) end)) + (not reverse) + (- (point) (diff-beginning-of-hunk-position))))) + (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) + (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 --=-=-=--