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: Mon, 19 Aug 2024 19:34:48 +0000 Message-ID: <87ed6kxq5j.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> 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="35336"; 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 Mon Aug 19 21: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 1sg8AP-00095Y-OE for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 19 Aug 2024 21:35:50 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sg8A3-0002PU-0u; Mon, 19 Aug 2024 15: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 1sg89z-0002PA-Rl for bug-gnu-emacs@gnu.org; Mon, 19 Aug 2024 15:35: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 1sg89x-0008OL-Gy for bug-gnu-emacs@gnu.org; Mon, 19 Aug 2024 15:35: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=lKvCwA6sTSqy99Ylh3s3GFvUpeVYZX0tyz5YHffeq2I=; b=I0u6aYOQZzAwPbv0cfQbNvSQPSr09MU73qUHRcbdm8/rDF0Qe5RApFcXwH0ciDhzlpZvEHZSNzJHYidx7OtRahBNPMKu3WGe+remSBLpK8bQyhlHse8f6q5Yf5oXEULBoN25PCthGBHn803GMF73uRH4eYspA4vVQYZW39Cuu0eUphz+o4R+3Bx4Lp9n8S8mE+UtRKyCPHe7m5xlu5V+fGzJEfnwTtR5KG9OXFQcM/ufxq4mr65+woddItqL2OW0dvoH9SUvZjxgGhq84d8jTCtHf+4K31nX24dwr0HuaJOqL7JRa7lr1XUhdHevZiQ2RXZcFI5/Rb96oF3KOKFF7w==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sg8Ac-0006TU-9F for bug-gnu-emacs@gnu.org; Mon, 19 Aug 2024 15: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: Mon, 19 Aug 2024 19: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.172409614224849 (code B ref 65380); Mon, 19 Aug 2024 19:36:02 +0000 Original-Received: (at 65380) by debbugs.gnu.org; 19 Aug 2024 19:35:42 +0000 Original-Received: from localhost ([127.0.0.1]:59271 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sg8AH-0006Sf-Qv for submit@debbugs.gnu.org; Mon, 19 Aug 2024 15:35:42 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:53431) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sg8AE-0006SM-TR for 65380@debbugs.gnu.org; Mon, 19 Aug 2024 15:35:40 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id A00E3240029 for <65380@debbugs.gnu.org>; Mon, 19 Aug 2024 21:34:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1724096090; bh=kxsrsFR3GkhJmpPhR1C8MRbcqCHIX2kXfjSQiU4P6bw=; h=From:To:Cc:Subject:Autocrypt:OpenPGP:Date:Message-ID:MIME-Version: Content-Type:From; b=M+wdiSFxWyciwwMY0kfvY2b8hUWJKDEjUyxzd4rNuIPhr70nPSyv93PjoE/e5dXNj c1mgLrJiJpPllZYKiTxQ2eOSSnN2tUNzarGIHL0nrHHOXr4g8IuaF9oMB58slDL4Lv 7ZcBzlM9x060/9Grs3poYWd57nbtN7auzbRSSPlnZP4vYD8OI8f3B2lSDzeeP4oGUQ v7uK2IuuekxBJQl6ksR/renI5TxrqJH7P41ipULjXgZRwUvJqYKVWpI4vcUkkvkIgv x3l+XqaSmZknhuPvpgPjFS5A9NWiEBeSM4gZOIl9Y16cnFzKLpRNxA3Nan+pt39q/X w37Dhgdu69cSQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4WnjV11rh4z9rxM; Mon, 19 Aug 2024 21:34:48 +0200 (CEST) In-Reply-To: <861q2lzp70.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 18 Aug 2024 21:00:19 +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:290413 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Philip Kaludercic >> Cc: juri@linkov.net, 65380@debbugs.gnu.org >> Date: Sun, 18 Aug 2024 16:20:52 +0000 >> >> >> To fix this I have written a general macro for subr.el to restore >> >> the scroll position: >> > >> > Let's first make sure we understand what happens here before we add >> > such a macro. (It also has some conceptual problems of its own, but >> > let's defer that until we actually sure it is needed.) >> >> It is not needed, but I would be interested in what the conceptual >> errors are. > > Forcing window-start has some side-effects that could look like bugs. > For example, the window's vscroll is reset, and if the original window > started its display with an overlay or display property, > set-window-start will not necessarily restore the original display, > due to boring technical issues. > > So my recommendation is to avoid calling set-window-start as a means > to return to some previous display, because it is not guaranteed that > you can do it like that. OK, I understood and will keep it in mind for the future. Thank you for the background! I'm attaching the newest version of the patch here: --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-command-to-copy-contents-in-a-diff-mode-buffer.patch >From b87daa72aa4f833001cd60be0d1f5cfe4064f717 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-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) --- etc/NEWS | 9 +++++++ lisp/vc/diff-mode.el | 58 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 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..0f3b4c6b4d1 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,45 @@ 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")) + (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 --=-=-=--