all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 65380@debbugs.gnu.org, juri@linkov.net
Subject: bug#65380: [PATCH] Add command to copy contents in a diff-mode buffer
Date: Sun, 18 Aug 2024 15:29:25 +0000	[thread overview]
Message-ID: <87frr1zw6i.fsf@posteo.net> (raw)
In-Reply-To: <871q2n152u.fsf@posteo.net> (Philip Kaludercic's message of "Sat,  17 Aug 2024 18:34:17 +0200")

[-- Attachment #1: Type: text/plain, Size: 561 bytes --]

Philip Kaludercic <philipk@posteo.net> 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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0002-Add-new-macro-to-restore-the-scroll-position-of-a-wi.patch --]
[-- Type: text/x-patch, Size: 1167 bytes --]

From c289c114ac381105cb550b3805041ad5cf5e61a5 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
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


[-- Attachment #3: Type: text/plain, Size: 80 bytes --]


and updated the previous patch accordingly (+ some other minor
improvements):


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0001-Add-command-to-copy-contents-in-a-diff-mode-buffer.patch --]
[-- Type: text/x-patch, Size: 4433 bytes --]

From 4565d12cdcbdb22c316f741e656d5388e880201c Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
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
   "<mouse-2>" #'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-<foo>' 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


[-- Attachment #5: Type: text/plain, Size: 37 bytes --]


-- 
	Philip Kaludercic on peregrine

  reply	other threads:[~2024-08-18 15:29 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-19  9:53 bug#65380: [PATCH] Add command to copy contents in a diff-mode buffer Philip Kaludercic
2023-08-19 10:00 ` Philip Kaludercic
2023-08-20  0:59   ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-20  7:52     ` Philip Kaludercic
2023-08-19 10:46 ` Eli Zaretskii
2023-08-19 10:48   ` Philip Kaludercic
2023-08-19 11:06     ` Eli Zaretskii
2023-08-19 15:45       ` Philip Kaludercic
2023-08-19 19:09         ` Eli Zaretskii
2023-08-19 19:30           ` Philip Kaludercic
2023-08-19 21:01           ` Sean Whitton
2023-08-19 22:49           ` Rudolf Adamkovič via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-20  0:41           ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-20 16:30           ` Juri Linkov
2023-08-20 18:17             ` Eli Zaretskii
2023-08-20 18:24               ` Philip Kaludercic
2023-08-20 18:29                 ` Eli Zaretskii
2023-08-22 11:06                   ` Philip Kaludercic
2023-08-22 11:29                     ` Eli Zaretskii
2024-08-17 16:34                       ` Philip Kaludercic
2024-08-18 15:29                         ` Philip Kaludercic [this message]
2024-08-18 15:43                           ` Eli Zaretskii
2024-08-18 16:20                             ` Philip Kaludercic
2024-08-18 18:00                               ` Eli Zaretskii
2024-08-19 19:34                                 ` Philip Kaludercic
2024-08-20  6:44                                   ` Juri Linkov
2024-08-20  7:46                                     ` Philip Kaludercic
2024-08-20 16:53                                       ` Juri Linkov
2024-08-20 11:36                                   ` Eli Zaretskii
2024-08-20 12:10                                     ` Philip Kaludercic
2024-08-20 13:09                                       ` Eli Zaretskii
2024-08-20 16:23                                         ` Philip Kaludercic
2024-08-20 18:43                                           ` Eli Zaretskii
2024-08-20 21:35                                             ` Philip Kaludercic
2024-08-21 13:42                                               ` Eli Zaretskii
2024-08-21 19:40                                                 ` Philip Kaludercic
2024-08-22  3:25                                                   ` Eli Zaretskii
2024-08-22  6:41                                                     ` Philip Kaludercic
2024-08-22 10:22                                                       ` Eli Zaretskii
2024-08-22 18:59                                                         ` Philip Kaludercic
2023-08-20 19:47           ` Jim Porter
2023-08-20 20:13             ` Gregory Heytings
2023-08-20 20:45               ` Jim Porter
2023-08-20 21:29                 ` Gregory Heytings
2023-08-20 22:21                   ` Jim Porter
2023-08-20 22:31                     ` Gregory Heytings
2023-08-20 23:39                       ` Gregory Heytings
2023-08-21  0:34                         ` Jim Porter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87frr1zw6i.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=65380@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=juri@linkov.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.