aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--etc/NEWS9
-rw-r--r--lisp/vc/diff-mode.el52
2 files changed, 60 insertions, 1 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 9a9b6ead241..f9ef15f9a40 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -244,6 +244,15 @@ The host name for Kubernetes connections can be of kind
244used. This overrides the setiing in 'tramp-kubernetes-namespace', if 244used. This overrides the setiing in 'tramp-kubernetes-namespace', if
245any. 245any.
246 246
247** Diff
248
249---
250*** New command 'diff-kill-ring-save'
251This command copies to the 'kill-ring' a region of text modified
252according to diffs in the current buffer, but without applying the diffs
253to the original text. If the selected range extends a hunk, the
254commands attempts to look up and copy the text in-between the hunks.
255
247 256
248* New Modes and Packages in Emacs 31.1 257* New Modes and Packages in Emacs 31.1
249 258
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 @@ The default \"-b\" means to ignore whitespace-only changes,
196 "RET" #'diff-goto-source 196 "RET" #'diff-goto-source
197 "<mouse-2>" #'diff-goto-source 197 "<mouse-2>" #'diff-goto-source
198 "W" #'widen 198 "W" #'widen
199 "w" #'diff-kill-ring-save
199 "o" #'diff-goto-source ; other-window 200 "o" #'diff-goto-source ; other-window
200 "A" #'diff-ediff-patch 201 "A" #'diff-ediff-patch
201 "r" #'diff-restrict-view 202 "r" #'diff-restrict-view
@@ -208,7 +209,7 @@ The default \"-b\" means to ignore whitespace-only changes,
208 ;; We want to inherit most bindings from 209 ;; We want to inherit most bindings from
209 ;; `diff-mode-shared-map', but not all since they may hide 210 ;; `diff-mode-shared-map', but not all since they may hide
210 ;; useful `M-<foo>' global bindings when editing. 211 ;; useful `M-<foo>' global bindings when editing.
211 (dolist (key '("A" "r" "R" "g" "q" "W" "z")) 212 (dolist (key '("A" "r" "R" "g" "q" "W" "w" "z"))
212 (keymap-set map key nil)) 213 (keymap-set map key nil))
213 map) 214 map)
214 ;; From compilation-minor-mode. 215 ;; From compilation-minor-mode.
@@ -2108,6 +2109,55 @@ revision of the file otherwise."
2108 (goto-char (+ (car pos) (cdr src))) 2109 (goto-char (+ (car pos) (cdr src)))
2109 (when buffer (next-error-found buffer (current-buffer)))))) 2110 (when buffer (next-error-found buffer (current-buffer))))))
2110 2111
2112(defun diff-kill-ring-save (beg end &optional reverse)
2113 "Save to `kill-ring' the result of applying diffs in region between BEG and END.
2114By default the command will copy the text that applying the diff would
2115produce, along with the text between hunks. If REVERSE is non-nil, or
2116the command was invoked with a prefix argument, copy the lines that the
2117diff would remove (beginning with \"+\" or \"<\")."
2118 (interactive
2119 (append (if (use-region-p)
2120 (list (region-beginning) (region-end))
2121 (save-excursion
2122 (list (diff-beginning-of-hunk)
2123 (diff-end-of-hunk))))
2124 (list current-prefix-arg)))
2125 (unless (derived-mode-p 'diff-mode)
2126 (user-error "Command can only be invoked in a diff-buffer"))
2127 (let ((parts '()))
2128 (save-excursion
2129 (goto-char beg)
2130 (catch 'break
2131 (while t
2132 (let ((hunk (diff-hunk-text
2133 (buffer-substring
2134 (save-excursion (diff-beginning-of-hunk))
2135 (save-excursion (min (diff-end-of-hunk) end)))
2136 (not reverse)
2137 (save-excursion
2138 (- (point) (diff-beginning-of-hunk))))))
2139 (push (substring (car hunk) (cdr hunk))
2140 parts))
2141 ;; check if we have copied everything
2142 (diff-end-of-hunk)
2143 (when (<= end (point)) (throw 'break t))
2144 ;; copy the text between hunks
2145 (let ((inhibit-message t) start)
2146 (save-window-excursion
2147 (save-excursion
2148 (forward-line -1)
2149 ;; FIXME: Detect if the line we jump to doesn't match
2150 ;; the line in the diff.
2151 (diff-goto-source t)
2152 (forward-line +1)
2153 (setq start (point))))
2154 (save-window-excursion
2155 (diff-goto-source t)
2156 (push (buffer-substring start (point))
2157 parts))))))
2158 (kill-new (string-join (nreverse parts)))
2159 (setq deactivate-mark t)
2160 (message (if reverse "Copied original text" "Copied modified text"))))
2111 2161
2112(defun diff-current-defun () 2162(defun diff-current-defun ()
2113 "Find the name of function at point. 2163 "Find the name of function at point.