diff options
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/vc/diff-mode.el | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index b8a9484627c..c59c0954ae1 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el | |||
| @@ -218,6 +218,7 @@ The default \"-b\" means to ignore whitespace-only changes, | |||
| 218 | "C-x 4 A" #'diff-add-change-log-entries-other-window | 218 | "C-x 4 A" #'diff-add-change-log-entries-other-window |
| 219 | ;; Misc operations. | 219 | ;; Misc operations. |
| 220 | "C-c C-a" #'diff-apply-hunk | 220 | "C-c C-a" #'diff-apply-hunk |
| 221 | "C-c M-r" #'diff-revert-and-kill-hunk | ||
| 221 | "C-c C-m a" #'diff-apply-buffer | 222 | "C-c C-m a" #'diff-apply-buffer |
| 222 | "C-c C-e" #'diff-ediff-patch | 223 | "C-c C-e" #'diff-ediff-patch |
| 223 | "C-c C-n" #'diff-restrict-view | 224 | "C-c C-n" #'diff-restrict-view |
| @@ -242,6 +243,8 @@ The default \"-b\" means to ignore whitespace-only changes, | |||
| 242 | :help "Apply the current hunk to the source file and go to the next"] | 243 | :help "Apply the current hunk to the source file and go to the next"] |
| 243 | ["Test applying hunk" diff-test-hunk | 244 | ["Test applying hunk" diff-test-hunk |
| 244 | :help "See whether it's possible to apply the current hunk"] | 245 | :help "See whether it's possible to apply the current hunk"] |
| 246 | ["Revert and kill hunk" diff-revert-and-kill-hunk | ||
| 247 | :help "Reverse-apply and then kill the current hunk."] | ||
| 245 | ["Apply all hunks" diff-apply-buffer | 248 | ["Apply all hunks" diff-apply-buffer |
| 246 | :help "Apply all hunks in the current diff buffer"] | 249 | :help "Apply all hunks in the current diff buffer"] |
| 247 | ["Apply diff with Ediff" diff-ediff-patch | 250 | ["Apply diff with Ediff" diff-ediff-patch |
| @@ -2050,24 +2053,52 @@ With a prefix argument, try to REVERSE the hunk." | |||
| 2050 | (diff-hunk-kill) | 2053 | (diff-hunk-kill) |
| 2051 | (diff-hunk-next))))) | 2054 | (diff-hunk-next))))) |
| 2052 | 2055 | ||
| 2053 | (defun diff-apply-buffer () | 2056 | (defcustom diff-ask-before-revert-and-kill-hunk t |
| 2057 | "If non-nil, `diff-revert-and-kill-hunk' will ask for confirmation." | ||
| 2058 | :type 'boolean) | ||
| 2059 | |||
| 2060 | (defun diff-revert-and-kill-hunk () | ||
| 2061 | "Reverse-apply and then kill the hunk at point. Save changed buffer. | ||
| 2062 | |||
| 2063 | This command is useful in buffers generated by \\[vc-diff] and \\[vc-root-diff], | ||
| 2064 | especially when preparing to commit the patch with \\[vc-next-action]. | ||
| 2065 | You can use \\<diff-mode-map>\\[diff-hunk-kill] to temporarily remove changes that you intend to | ||
| 2066 | include in a separate commit or commits, and you can use this command | ||
| 2067 | to permanently drop changes you didn't intend, or no longer want. | ||
| 2068 | |||
| 2069 | This is a destructive operation, so by default, this command asks you to | ||
| 2070 | confirm you really want to reverse-apply and kill the hunk. You can | ||
| 2071 | customize `diff-ask-before-revert-and-kill-hunk' to change that." | ||
| 2072 | (interactive) | ||
| 2073 | (when (or (not diff-ask-before-revert-and-kill-hunk) | ||
| 2074 | (yes-or-no-p "Really reverse-apply and kill this hunk?")) | ||
| 2075 | (cl-destructuring-bind (beg end) (diff-bounds-of-hunk) | ||
| 2076 | (when (null (diff-apply-buffer beg end t)) | ||
| 2077 | (diff-hunk-kill))))) | ||
| 2078 | |||
| 2079 | (defun diff-apply-buffer (&optional beg end reverse) | ||
| 2054 | "Apply the diff in the entire diff buffer. | 2080 | "Apply the diff in the entire diff buffer. |
| 2055 | When applying all hunks was successful, then save the changed buffers." | 2081 | When applying all hunks was successful, then save the changed buffers. |
| 2082 | When called from Lisp with optional arguments, restrict the application | ||
| 2083 | to hunks lying between BEG and END, and reverse-apply when REVERSE is | ||
| 2084 | non-nil. Returns nil if buffers were saved, or the number of failed | ||
| 2085 | applications." | ||
| 2056 | (interactive) | 2086 | (interactive) |
| 2057 | (let ((buffer-edits nil) | 2087 | (let ((buffer-edits nil) |
| 2058 | (failures 0) | 2088 | (failures 0) |
| 2059 | (diff-refine nil)) | 2089 | (diff-refine nil)) |
| 2060 | (save-excursion | 2090 | (save-excursion |
| 2061 | (goto-char (point-min)) | 2091 | (goto-char (or beg (point-min))) |
| 2062 | (diff-beginning-of-hunk t) | 2092 | (diff-beginning-of-hunk t) |
| 2063 | (while (pcase-let ((`(,buf ,line-offset ,pos ,_src ,dst ,switched) | 2093 | (while (pcase-let ((`(,buf ,line-offset ,pos ,_src ,dst ,switched) |
| 2064 | (diff-find-source-location nil nil))) | 2094 | (diff-find-source-location nil reverse))) |
| 2065 | (cond ((and line-offset (not switched)) | 2095 | (cond ((and line-offset (not switched)) |
| 2066 | (push (cons pos dst) | 2096 | (push (cons pos dst) |
| 2067 | (alist-get buf buffer-edits))) | 2097 | (alist-get buf buffer-edits))) |
| 2068 | (t (setq failures (1+ failures)))) | 2098 | (t (setq failures (1+ failures)))) |
| 2069 | (and (not (eq (prog1 (point) (ignore-errors (diff-hunk-next))) | 2099 | (and (not (eq (prog1 (point) (ignore-errors (diff-hunk-next))) |
| 2070 | (point))) | 2100 | (point))) |
| 2101 | (or (not end) (< (point) end)) | ||
| 2071 | (looking-at-p diff-hunk-header-re))))) | 2102 | (looking-at-p diff-hunk-header-re))))) |
| 2072 | (cond ((zerop failures) | 2103 | (cond ((zerop failures) |
| 2073 | (dolist (buf-edits (reverse buffer-edits)) | 2104 | (dolist (buf-edits (reverse buffer-edits)) |
| @@ -2080,11 +2111,13 @@ When applying all hunks was successful, then save the changed buffers." | |||
| 2080 | (delete-region (car pos) (cdr pos)) | 2111 | (delete-region (car pos) (cdr pos)) |
| 2081 | (insert (car dst)))) | 2112 | (insert (car dst)))) |
| 2082 | (save-buffer))) | 2113 | (save-buffer))) |
| 2083 | (message "Saved %d buffers" (length buffer-edits))) | 2114 | (message "Saved %d buffers" (length buffer-edits)) |
| 2115 | nil) | ||
| 2084 | (t | 2116 | (t |
| 2085 | (message (ngettext "%d hunk failed; no buffers changed" | 2117 | (message (ngettext "%d hunk failed; no buffers changed" |
| 2086 | "%d hunks failed; no buffers changed" | 2118 | "%d hunks failed; no buffers changed" |
| 2087 | failures)))))) | 2119 | failures)) |
| 2120 | failures)))) | ||
| 2088 | 2121 | ||
| 2089 | (defalias 'diff-mouse-goto-source #'diff-goto-source) | 2122 | (defalias 'diff-mouse-goto-source #'diff-goto-source) |
| 2090 | 2123 | ||