diff options
| author | Eli Zaretskii | 2012-11-13 16:17:18 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2012-11-13 16:17:18 +0200 |
| commit | 3c4ca7155293ffc2d04708007131bcbc882d8913 (patch) | |
| tree | 61787be8cd43b6fb3d5159852fbd186eea404de7 /lisp/vc/diff-mode.el | |
| parent | 5ade42a5114255c43117065494b96d480c1e1588 (diff) | |
| parent | c708524567662c8911c5ab2695acc7bda0383705 (diff) | |
| download | emacs-3c4ca7155293ffc2d04708007131bcbc882d8913.tar.gz emacs-3c4ca7155293ffc2d04708007131bcbc882d8913.zip | |
Merge from trunk.
Diffstat (limited to 'lisp/vc/diff-mode.el')
| -rw-r--r-- | lisp/vc/diff-mode.el | 96 |
1 files changed, 66 insertions, 30 deletions
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 49b76a8e3bc..26c64ce2ad3 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el | |||
| @@ -178,7 +178,7 @@ when editing big diffs)." | |||
| 178 | ["Unified -> Context" diff-unified->context | 178 | ["Unified -> Context" diff-unified->context |
| 179 | :help "Convert unified diffs to context diffs"] | 179 | :help "Convert unified diffs to context diffs"] |
| 180 | ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)] | 180 | ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)] |
| 181 | ["Remove trailing whitespace" diff-remove-trailing-whitespace | 181 | ["Remove trailing whitespace" diff-delete-trailing-whitespace |
| 182 | :help "Remove trailing whitespace problems introduced by the diff"] | 182 | :help "Remove trailing whitespace problems introduced by the diff"] |
| 183 | ["Show trailing whitespace" whitespace-mode | 183 | ["Show trailing whitespace" whitespace-mode |
| 184 | :style toggle :selected (bound-and-true-p whitespace-mode) | 184 | :style toggle :selected (bound-and-true-p whitespace-mode) |
| @@ -2048,35 +2048,71 @@ I.e. like `add-change-log-entry-other-window' but applied to all hunks." | |||
| 2048 | ;; When there's no more hunks, diff-hunk-next signals an error. | 2048 | ;; When there's no more hunks, diff-hunk-next signals an error. |
| 2049 | (error nil)))) | 2049 | (error nil)))) |
| 2050 | 2050 | ||
| 2051 | (defun diff-remove-trailing-whitespace () | 2051 | (defun diff-delete-trailing-whitespace (&optional other-file) |
| 2052 | "When on a buffer that contains a diff, inspects the | 2052 | "Remove trailing whitespace from lines modified in this diff. |
| 2053 | differences and removes trailing whitespace (spaces, tabs) from | 2053 | This edits both the current Diff mode buffer and the patched |
| 2054 | the lines modified or introduced by this diff. Shows a message | 2054 | source file(s). If `diff-jump-to-old-file' is non-nil, edit the |
| 2055 | with the name of the altered buffers, which are unsaved. If a | 2055 | original (unpatched) source file instead. With a prefix argument |
| 2056 | file referenced on the diff has no buffer and needs to be fixed, | 2056 | OTHER-FILE, flip the choice of which source file to edit. |
| 2057 | a buffer visiting that file is created." | 2057 | |
| 2058 | (interactive) | 2058 | If a file referenced in the diff has no buffer and needs to be |
| 2059 | ;; We assume that the diff header has no trailing whitespace. | 2059 | fixed, visit it in a buffer." |
| 2060 | (let ((modified-buffers nil)) | 2060 | (interactive "P") |
| 2061 | (save-excursion | 2061 | (save-excursion |
| 2062 | (goto-char (point-min)) | 2062 | (goto-char (point-min)) |
| 2063 | (while (re-search-forward "^[+!>].*[ \t]+$" (point-max) t) | 2063 | (let* ((other (diff-xor other-file diff-jump-to-old-file)) |
| 2064 | (pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,_switched) | 2064 | (modified-buffers nil) |
| 2065 | (diff-find-source-location t t))) | 2065 | (style (save-excursion |
| 2066 | (when line-offset | 2066 | (when (re-search-forward diff-hunk-header-re nil t) |
| 2067 | (with-current-buffer buf | 2067 | (goto-char (match-beginning 0)) |
| 2068 | (save-excursion | 2068 | (diff-hunk-style)))) |
| 2069 | (goto-char (+ (car pos) (cdr src))) | 2069 | (regexp (concat "^[" (if other "-<" "+>") "!]" |
| 2070 | (beginning-of-line) | 2070 | (if (eq style 'context) " " "") |
| 2071 | (when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t) | 2071 | ".*?\\([ \t]+\\)$")) |
| 2072 | (unless (memq buf modified-buffers) | 2072 | (inhibit-read-only t) |
| 2073 | (push buf modified-buffers)) | 2073 | (end-marker (make-marker)) |
| 2074 | (replace-match "")))))))) | 2074 | hunk-end) |
| 2075 | (if modified-buffers | 2075 | ;; Move to the first hunk. |
| 2076 | (message "Deleted new trailing whitespace from: %s" | 2076 | (re-search-forward diff-hunk-header-re nil 1) |
| 2077 | (mapconcat (lambda (buf) (concat "`" (buffer-name buf) "'")) | 2077 | (while (progn (save-excursion |
| 2078 | modified-buffers " ")) | 2078 | (re-search-forward diff-hunk-header-re nil 1) |
| 2079 | (message "No trailing whitespace fixes needed.")))) | 2079 | (setq hunk-end (point))) |
| 2080 | (< (point) hunk-end)) | ||
| 2081 | ;; For context diffs, search only in the appropriate half of | ||
| 2082 | ;; the hunk. For other diffs, search within the entire hunk. | ||
| 2083 | (if (not (eq style 'context)) | ||
| 2084 | (set-marker end-marker hunk-end) | ||
| 2085 | (let ((mid-hunk | ||
| 2086 | (save-excursion | ||
| 2087 | (re-search-forward diff-context-mid-hunk-header-re hunk-end) | ||
| 2088 | (point)))) | ||
| 2089 | (if other | ||
| 2090 | (set-marker end-marker mid-hunk) | ||
| 2091 | (goto-char mid-hunk) | ||
| 2092 | (set-marker end-marker hunk-end)))) | ||
| 2093 | (while (re-search-forward regexp end-marker t) | ||
| 2094 | (let ((match-data (match-data))) | ||
| 2095 | (pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,_switched) | ||
| 2096 | (diff-find-source-location other-file))) | ||
| 2097 | (when line-offset | ||
| 2098 | ;; Remove the whitespace in the Diff mode buffer. | ||
| 2099 | (set-match-data match-data) | ||
| 2100 | (replace-match "" t t nil 1) | ||
| 2101 | ;; Remove the whitespace in the source buffer. | ||
| 2102 | (with-current-buffer buf | ||
| 2103 | (save-excursion | ||
| 2104 | (goto-char (+ (car pos) (cdr src))) | ||
| 2105 | (beginning-of-line) | ||
| 2106 | (when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t) | ||
| 2107 | (unless (memq buf modified-buffers) | ||
| 2108 | (push buf modified-buffers)) | ||
| 2109 | (replace-match "")))))))) | ||
| 2110 | (goto-char hunk-end)) | ||
| 2111 | (if modified-buffers | ||
| 2112 | (message "Deleted trailing whitespace from %s." | ||
| 2113 | (mapconcat (lambda (buf) (concat "`" (buffer-name buf) "'")) | ||
| 2114 | modified-buffers ", ")) | ||
| 2115 | (message "No trailing whitespace to delete."))))) | ||
| 2080 | 2116 | ||
| 2081 | ;; provide the package | 2117 | ;; provide the package |
| 2082 | (provide 'diff-mode) | 2118 | (provide 'diff-mode) |