Commit | Line | Data |
---|---|---|
a9de04fa | 1 | ;;; diff-mode.el --- a mode for viewing/editing context diffs -*- lexical-binding: t -*- |
610a6418 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1998-2014 Free Software Foundation, Inc. |
610a6418 | 4 | |
cc1eecfd | 5 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> |
9766adfb | 6 | ;; Keywords: convenience patch diff vc |
610a6418 SM |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
610a6418 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
610a6418 SM |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
610a6418 SM |
22 | |
23 | ;;; Commentary: | |
24 | ||
cdbb79c1 | 25 | ;; Provides support for font-lock, outline, navigation |
610a6418 SM |
26 | ;; commands, editing and various conversions as well as jumping |
27 | ;; to the corresponding source file. | |
28 | ||
3ccdb14e | 29 | ;; Inspired by Pavel Machek's patch-mode.el (<pavel@@atrey.karlin.mff.cuni.cz>) |
44e97401 | 30 | ;; Some efforts were spent to have it somewhat compatible with XEmacs's |
610a6418 SM |
31 | ;; diff-mode as well as with compilation-minor-mode |
32 | ||
610a6418 SM |
33 | ;; Bugs: |
34 | ||
027ac3f8 | 35 | ;; - Reverse doesn't work with normal diffs. |
610a6418 SM |
36 | |
37 | ;; Todo: | |
38 | ||
8330c175 SM |
39 | ;; - Improve `diff-add-change-log-entries-other-window', |
40 | ;; it is very simplistic now. | |
9201cc28 | 41 | ;; |
22cd1973 SM |
42 | ;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks. |
43 | ;; Also allow C-c C-a to delete already-applied hunks. | |
44 | ;; | |
45 | ;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location | |
46 | ;; of a hunk. Show then the changes between <file> and <hunk> and make it | |
47 | ;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>. | |
48 | ;; Or maybe just make it into a ".rej to diff3-markers converter". | |
e78cf8e5 | 49 | ;; Maybe just use `wiggle' (by Neil Brown) to do it for us. |
22cd1973 | 50 | ;; |
e78cf8e5 SM |
51 | ;; - in diff-apply-hunk, strip context in replace-match to better |
52 | ;; preserve markers and spacing. | |
027ac3f8 | 53 | ;; - Handle `diff -b' output in context->unified. |
610a6418 SM |
54 | |
55 | ;;; Code: | |
f58e0fd5 | 56 | (eval-when-compile (require 'cl-lib)) |
610a6418 | 57 | |
6e0f362c JB |
58 | (defvar add-log-buffer-file-name-function) |
59 | ||
610a6418 SM |
60 | |
61 | (defgroup diff-mode () | |
aa753196 | 62 | "Major mode for viewing/editing diffs." |
ccce6558 | 63 | :version "21.1" |
610a6418 SM |
64 | :group 'tools |
65 | :group 'diff) | |
66 | ||
22cd1973 | 67 | (defcustom diff-default-read-only nil |
769dd0f1 SM |
68 | "If non-nil, `diff-mode' buffers default to being read-only." |
69 | :type 'boolean | |
70 | :group 'diff-mode) | |
71 | ||
e8a1ed31 | 72 | (defcustom diff-jump-to-old-file nil |
44a07c5a | 73 | "Non-nil means `diff-goto-source' jumps to the old file. |
610a6418 | 74 | Else, it jumps to the new file." |
f5307782 JB |
75 | :type 'boolean |
76 | :group 'diff-mode) | |
610a6418 | 77 | |
e8a1ed31 | 78 | (defcustom diff-update-on-the-fly t |
9201cc28 | 79 | "Non-nil means hunk headers are kept up-to-date on-the-fly. |
610a6418 SM |
80 | When editing a diff file, the line numbers in the hunk headers |
81 | need to be kept consistent with the actual diff. This can | |
82 | either be done on the fly (but this sometimes interacts poorly with the | |
83 | undo mechanism) or whenever the file is written (can be slow | |
84 | when editing big diffs)." | |
f5307782 JB |
85 | :type 'boolean |
86 | :group 'diff-mode) | |
610a6418 | 87 | |
00df919e | 88 | (defcustom diff-advance-after-apply-hunk t |
9201cc28 | 89 | "Non-nil means `diff-apply-hunk' will move to the next hunk after applying." |
f5307782 JB |
90 | :type 'boolean |
91 | :group 'diff-mode) | |
00df919e | 92 | |
22cd1973 SM |
93 | (defcustom diff-mode-hook nil |
94 | "Run after setting up the `diff-mode' major mode." | |
95 | :type 'hook | |
f5307782 JB |
96 | :options '(diff-delete-empty-files diff-make-unified) |
97 | :group 'diff-mode) | |
610a6418 | 98 | |
a2f043d3 CY |
99 | (defvar diff-vc-backend nil |
100 | "The VC backend that created the current Diff buffer, if any.") | |
101 | ||
610a6418 SM |
102 | (defvar diff-outline-regexp |
103 | "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)") | |
104 | ||
cd632e57 | 105 | ;;;; |
610a6418 | 106 | ;;;; keymap, menu, ... |
cd632e57 | 107 | ;;;; |
610a6418 | 108 | |
d87e5627 | 109 | (easy-mmode-defmap diff-mode-shared-map |
8b71081d | 110 | '(("n" . diff-hunk-next) |
d87e5627 SM |
111 | ("N" . diff-file-next) |
112 | ("p" . diff-hunk-prev) | |
113 | ("P" . diff-file-prev) | |
25edda53 DN |
114 | ("\t" . diff-hunk-next) |
115 | ([backtab] . diff-hunk-prev) | |
d87e5627 SM |
116 | ("k" . diff-hunk-kill) |
117 | ("K" . diff-file-kill) | |
8b71081d | 118 | ("}" . diff-file-next) ; From compilation-minor-mode. |
d87e5627 | 119 | ("{" . diff-file-prev) |
610a6418 | 120 | ("\C-m" . diff-goto-source) |
a1e2d7f7 | 121 | ([mouse-2] . diff-goto-source) |
a904a09a | 122 | ("W" . widen) |
8b71081d | 123 | ("o" . diff-goto-source) ; other-window |
a904a09a SM |
124 | ("A" . diff-ediff-patch) |
125 | ("r" . diff-restrict-view) | |
8b71081d | 126 | ("R" . diff-reverse-direction) |
8b71081d | 127 | ([remap undo] . diff-undo)) |
abef340a SS |
128 | "Basic keymap for `diff-mode', bound to various prefix keys." |
129 | :inherit special-mode-map) | |
610a6418 | 130 | |
d87e5627 | 131 | (easy-mmode-defmap diff-mode-map |
a904a09a SM |
132 | `(("\e" . ,(let ((map (make-sparse-keymap))) |
133 | ;; We want to inherit most bindings from diff-mode-shared-map, | |
134 | ;; but not all since they may hide useful M-<foo> global | |
135 | ;; bindings when editing. | |
136 | (set-keymap-parent map diff-mode-shared-map) | |
c60c3703 | 137 | (dolist (key '("A" "r" "R" "g" "q" "W" "z")) |
a904a09a SM |
138 | (define-key map key nil)) |
139 | map)) | |
027ac3f8 SM |
140 | ;; From compilation-minor-mode. |
141 | ("\C-c\C-c" . diff-goto-source) | |
8330c175 SM |
142 | ;; By analogy with the global C-x 4 a binding. |
143 | ("\C-x4A" . diff-add-change-log-entries-other-window) | |
027ac3f8 | 144 | ;; Misc operations. |
3cec9c57 | 145 | ("\C-c\C-a" . diff-apply-hunk) |
28408bfd RS |
146 | ("\C-c\C-e" . diff-ediff-patch) |
147 | ("\C-c\C-n" . diff-restrict-view) | |
28408bfd | 148 | ("\C-c\C-s" . diff-split-hunk) |
b711788a | 149 | ("\C-c\C-t" . diff-test-hunk) |
2659df68 | 150 | ("\C-c\C-r" . diff-reverse-direction) |
28408bfd | 151 | ("\C-c\C-u" . diff-context->unified) |
fe39af99 SM |
152 | ;; `d' because it duplicates the context :-( --Stef |
153 | ("\C-c\C-d" . diff-unified->context) | |
2659df68 SM |
154 | ("\C-c\C-w" . diff-ignore-whitespace-hunk) |
155 | ("\C-c\C-b" . diff-refine-hunk) ;No reason for `b' :-( | |
677c0382 | 156 | ("\C-c\C-f" . next-error-follow-minor-mode)) |
610a6418 SM |
157 | "Keymap for `diff-mode'. See also `diff-mode-shared-map'.") |
158 | ||
159 | (easy-menu-define diff-mode-menu diff-mode-map | |
160 | "Menu for `diff-mode'." | |
161 | '("Diff" | |
fb4dfdd2 DN |
162 | ["Jump to Source" diff-goto-source |
163 | :help "Jump to the corresponding source line"] | |
164 | ["Apply hunk" diff-apply-hunk | |
165 | :help "Apply the current hunk to the source file and go to the next"] | |
166 | ["Test applying hunk" diff-test-hunk | |
167 | :help "See whether it's possible to apply the current hunk"] | |
168 | ["Apply diff with Ediff" diff-ediff-patch | |
169 | :help "Call `ediff-patch-file' on the current buffer"] | |
8330c175 | 170 | ["Create Change Log entries" diff-add-change-log-entries-other-window |
8a72c7f8 | 171 | :help "Create ChangeLog entries for the changes in the diff buffer"] |
8f2d38de | 172 | "-----" |
fb4dfdd2 DN |
173 | ["Reverse direction" diff-reverse-direction |
174 | :help "Reverse the direction of the diffs"] | |
175 | ["Context -> Unified" diff-context->unified | |
176 | :help "Convert context diffs to unified diffs"] | |
177 | ["Unified -> Context" diff-unified->context | |
178 | :help "Convert unified diffs to context diffs"] | |
610a6418 | 179 | ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)] |
9d9e48d9 | 180 | ["Remove trailing whitespace" diff-delete-trailing-whitespace |
0780c517 | 181 | :help "Remove trailing whitespace problems introduced by the diff"] |
ac7020b3 | 182 | ["Show trailing whitespace" whitespace-mode |
4c6de5a5 | 183 | :style toggle :selected (bound-and-true-p whitespace-mode) |
ac7020b3 | 184 | :help "Show trailing whitespace in modified lines"] |
8f2d38de | 185 | "-----" |
fb4dfdd2 DN |
186 | ["Split hunk" diff-split-hunk |
187 | :active (diff-splittable-p) | |
188 | :help "Split the current (unified diff) hunk at point into two hunks"] | |
189 | ["Ignore whitespace changes" diff-ignore-whitespace-hunk | |
190 | :help "Re-diff the current hunk, ignoring whitespace differences"] | |
191 | ["Highlight fine changes" diff-refine-hunk | |
192 | :help "Highlight changes of hunk at point at a finer granularity"] | |
193 | ["Kill current hunk" diff-hunk-kill | |
194 | :help "Kill current hunk"] | |
195 | ["Kill current file's hunks" diff-file-kill | |
196 | :help "Kill all current file's hunks"] | |
8f2d38de | 197 | "-----" |
fb4dfdd2 DN |
198 | ["Previous Hunk" diff-hunk-prev |
199 | :help "Go to the previous count'th hunk"] | |
200 | ["Next Hunk" diff-hunk-next | |
201 | :help "Go to the next count'th hunk"] | |
202 | ["Previous File" diff-file-prev | |
203 | :help "Go to the previous count'th file"] | |
204 | ["Next File" diff-file-next | |
205 | :help "Go to the next count'th file"] | |
610a6418 SM |
206 | )) |
207 | ||
3cec9c57 | 208 | (defcustom diff-minor-mode-prefix "\C-c=" |
0b82e382 | 209 | "Prefix key for `diff-minor-mode' commands." |
f5307782 JB |
210 | :type '(choice (string "\e") (string "C-c=") string) |
211 | :group 'diff-mode) | |
0b82e382 | 212 | |
d87e5627 SM |
213 | (easy-mmode-defmap diff-minor-mode-map |
214 | `((,diff-minor-mode-prefix . ,diff-mode-shared-map)) | |
0b82e382 SM |
215 | "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.") |
216 | ||
7381be9d | 217 | (define-minor-mode diff-auto-refine-mode |
ac6c8639 CY |
218 | "Toggle automatic diff hunk highlighting (Diff Auto Refine mode). |
219 | With a prefix argument ARG, enable Diff Auto Refine mode if ARG | |
220 | is positive, and disable it otherwise. If called from Lisp, | |
221 | enable the mode if ARG is omitted or nil. | |
222 | ||
223 | Diff Auto Refine mode is a buffer-local minor mode used with | |
224 | `diff-mode'. When enabled, Emacs automatically highlights | |
225 | changes in detail as the user visits hunks. When transitioning | |
226 | from disabled to enabled, it tries to refine the current hunk, as | |
227 | well." | |
4542adfb | 228 | :group 'diff-mode :init-value t :lighter nil ;; " Auto-Refine" |
7381be9d | 229 | (when diff-auto-refine-mode |
1be3ca5a | 230 | (condition-case-unless-debug nil (diff-refine-hunk) (error nil)))) |
610a6418 | 231 | |
cd632e57 | 232 | ;;;; |
610a6418 | 233 | ;;;; font-lock support |
cd632e57 | 234 | ;;;; |
610a6418 | 235 | |
221711eb | 236 | (defface diff-header |
55f2eb7e | 237 | '((((class color) (min-colors 88) (background light)) |
9f4e4f5b | 238 | :background "grey80") |
55f2eb7e | 239 | (((class color) (min-colors 88) (background dark)) |
4f815065 | 240 | :background "grey45") |
bc1b21bb | 241 | (((class color)) |
4f815065 | 242 | :foreground "blue1" :weight bold) |
4f815065 | 243 | (t :weight bold)) |
d676819e LK |
244 | "`diff-mode' face inherited by hunk and index header faces." |
245 | :group 'diff-mode) | |
c4f6e489 | 246 | (define-obsolete-face-alias 'diff-header-face 'diff-header "22.1") |
221711eb | 247 | (defvar diff-header-face 'diff-header) |
c0aa75f7 | 248 | |
221711eb | 249 | (defface diff-file-header |
55f2eb7e | 250 | '((((class color) (min-colors 88) (background light)) |
4f815065 | 251 | :background "grey70" :weight bold) |
55f2eb7e | 252 | (((class color) (min-colors 88) (background dark)) |
4f815065 | 253 | :background "grey60" :weight bold) |
bc1b21bb | 254 | (((class color)) |
4f815065 SM |
255 | :foreground "cyan" :weight bold) |
256 | (t :weight bold)) ; :height 1.3 | |
d676819e LK |
257 | "`diff-mode' face used to highlight file header lines." |
258 | :group 'diff-mode) | |
c4f6e489 | 259 | (define-obsolete-face-alias 'diff-file-header-face 'diff-file-header "22.1") |
221711eb | 260 | (defvar diff-file-header-face 'diff-file-header) |
610a6418 | 261 | |
221711eb MB |
262 | (defface diff-index |
263 | '((t :inherit diff-file-header)) | |
d676819e LK |
264 | "`diff-mode' face used to highlight index header lines." |
265 | :group 'diff-mode) | |
c4f6e489 | 266 | (define-obsolete-face-alias 'diff-index-face 'diff-index "22.1") |
221711eb | 267 | (defvar diff-index-face 'diff-index) |
610a6418 | 268 | |
221711eb MB |
269 | (defface diff-hunk-header |
270 | '((t :inherit diff-header)) | |
d676819e LK |
271 | "`diff-mode' face used to highlight hunk header lines." |
272 | :group 'diff-mode) | |
c4f6e489 | 273 | (define-obsolete-face-alias 'diff-hunk-header-face 'diff-hunk-header "22.1") |
221711eb | 274 | (defvar diff-hunk-header-face 'diff-hunk-header) |
610a6418 | 275 | |
221711eb | 276 | (defface diff-removed |
bc1b21bb JL |
277 | '((default |
278 | :inherit diff-changed) | |
279 | (((class color) (min-colors 88) (background light)) | |
280 | :background "#ffdddd") | |
281 | (((class color) (min-colors 88) (background dark)) | |
282 | :background "#553333") | |
283 | (((class color)) | |
284 | :foreground "red")) | |
d676819e LK |
285 | "`diff-mode' face used to highlight removed lines." |
286 | :group 'diff-mode) | |
c4f6e489 | 287 | (define-obsolete-face-alias 'diff-removed-face 'diff-removed "22.1") |
221711eb | 288 | (defvar diff-removed-face 'diff-removed) |
610a6418 | 289 | |
221711eb | 290 | (defface diff-added |
bc1b21bb JL |
291 | '((default |
292 | :inherit diff-changed) | |
293 | (((class color) (min-colors 88) (background light)) | |
294 | :background "#ddffdd") | |
295 | (((class color) (min-colors 88) (background dark)) | |
296 | :background "#335533") | |
297 | (((class color)) | |
298 | :foreground "green")) | |
d676819e LK |
299 | "`diff-mode' face used to highlight added lines." |
300 | :group 'diff-mode) | |
c4f6e489 | 301 | (define-obsolete-face-alias 'diff-added-face 'diff-added "22.1") |
221711eb | 302 | (defvar diff-added-face 'diff-added) |
610a6418 | 303 | |
221711eb | 304 | (defface diff-changed |
d5b44c93 CY |
305 | ;; We normally apply a `shadow'-based face on the `diff-context' |
306 | ;; face, and keep `diff-changed' the default. | |
307 | '((((class color grayscale) (min-colors 88))) | |
308 | ;; If the terminal lacks sufficient colors for shadowing, | |
309 | ;; highlight changed lines explicitly. | |
bc1b21bb JL |
310 | (((class color)) |
311 | :foreground "yellow")) | |
d676819e LK |
312 | "`diff-mode' face used to highlight changed lines." |
313 | :group 'diff-mode) | |
c4f6e489 | 314 | (define-obsolete-face-alias 'diff-changed-face 'diff-changed "22.1") |
221711eb | 315 | (defvar diff-changed-face 'diff-changed) |
610a6418 | 316 | |
f3abba99 JL |
317 | (defface diff-indicator-removed |
318 | '((t :inherit diff-removed)) | |
319 | "`diff-mode' face used to highlight indicator of removed lines (-, <)." | |
320 | :group 'diff-mode | |
321 | :version "22.1") | |
322 | (defvar diff-indicator-removed-face 'diff-indicator-removed) | |
323 | ||
324 | (defface diff-indicator-added | |
325 | '((t :inherit diff-added)) | |
326 | "`diff-mode' face used to highlight indicator of added lines (+, >)." | |
327 | :group 'diff-mode | |
328 | :version "22.1") | |
329 | (defvar diff-indicator-added-face 'diff-indicator-added) | |
330 | ||
331 | (defface diff-indicator-changed | |
332 | '((t :inherit diff-changed)) | |
333 | "`diff-mode' face used to highlight indicator of changed lines." | |
334 | :group 'diff-mode | |
335 | :version "22.1") | |
336 | (defvar diff-indicator-changed-face 'diff-indicator-changed) | |
337 | ||
221711eb | 338 | (defface diff-function |
ea5d66db | 339 | '((t :inherit diff-header)) |
d676819e LK |
340 | "`diff-mode' face used to highlight function names produced by \"diff -p\"." |
341 | :group 'diff-mode) | |
c4f6e489 | 342 | (define-obsolete-face-alias 'diff-function-face 'diff-function "22.1") |
221711eb | 343 | (defvar diff-function-face 'diff-function) |
34460354 | 344 | |
221711eb | 345 | (defface diff-context |
f3abba99 | 346 | '((((class color grayscale) (min-colors 88)) :inherit shadow)) |
d676819e LK |
347 | "`diff-mode' face used to highlight context and other side-information." |
348 | :group 'diff-mode) | |
c4f6e489 | 349 | (define-obsolete-face-alias 'diff-context-face 'diff-context "22.1") |
221711eb | 350 | (defvar diff-context-face 'diff-context) |
c0aa75f7 | 351 | |
221711eb MB |
352 | (defface diff-nonexistent |
353 | '((t :inherit diff-file-header)) | |
d676819e LK |
354 | "`diff-mode' face used to highlight nonexistent files in recursive diffs." |
355 | :group 'diff-mode) | |
c4f6e489 | 356 | (define-obsolete-face-alias 'diff-nonexistent-face 'diff-nonexistent "22.1") |
221711eb | 357 | (defvar diff-nonexistent-face 'diff-nonexistent) |
469fc0a2 | 358 | |
22cd1973 SM |
359 | (defconst diff-yank-handler '(diff-yank-function)) |
360 | (defun diff-yank-function (text) | |
1ec7bd14 SM |
361 | ;; FIXME: the yank-handler is now called separately on each piece of text |
362 | ;; with a yank-handler property, so the next-single-property-change call | |
363 | ;; below will always return nil :-( --stef | |
22cd1973 SM |
364 | (let ((mixed (next-single-property-change 0 'yank-handler text)) |
365 | (start (point))) | |
366 | ;; First insert the text. | |
367 | (insert text) | |
368 | ;; If the text does not include any diff markers and if we're not | |
369 | ;; yanking back into a diff-mode buffer, get rid of the prefixes. | |
370 | (unless (or mixed (derived-mode-p 'diff-mode)) | |
371 | (undo-boundary) ; Just in case the user wanted the prefixes. | |
372 | (let ((re (save-excursion | |
373 | (if (re-search-backward "^[><!][ \t]" start t) | |
374 | (if (eq (char-after) ?!) | |
375 | "^[!+- ][ \t]" "^[<>][ \t]") | |
376 | "^[ <>!+-]")))) | |
377 | (save-excursion | |
378 | (while (re-search-backward re start t) | |
379 | (replace-match "" t t))))))) | |
cfc80227 | 380 | |
f52d2f9c | 381 | (defconst diff-hunk-header-re-unified |
43392d12 | 382 | "^@@ -\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? @@") |
bf6970a5 SM |
383 | (defconst diff-context-mid-hunk-header-re |
384 | "--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$") | |
22cd1973 | 385 | |
bc1b21bb JL |
386 | (defvar diff-use-changed-face (and (face-differs-from-default-p diff-changed-face) |
387 | (not (face-equal diff-changed-face diff-added-face)) | |
388 | (not (face-equal diff-changed-face diff-removed-face))) | |
389 | "If non-nil, use the face `diff-changed' for changed lines in context diffs. | |
390 | Otherwise, use the face `diff-removed' for removed lines, | |
391 | and the face `diff-added' for added lines.") | |
392 | ||
610a6418 | 393 | (defvar diff-font-lock-keywords |
f52d2f9c SM |
394 | `((,(concat "\\(" diff-hunk-header-re-unified "\\)\\(.*\\)$") |
395 | (1 diff-hunk-header-face) (6 diff-function-face)) | |
f3abba99 JL |
396 | ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context |
397 | (1 diff-hunk-header-face) (2 diff-function-face)) | |
370d860c | 398 | ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context |
bf6970a5 | 399 | (,diff-context-mid-hunk-header-re . diff-hunk-header-face) ;context |
f3abba99 JL |
400 | ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face) ;normal |
401 | ("^---$" . diff-hunk-header-face) ;normal | |
4c969f97 SM |
402 | ;; For file headers, accept files with spaces, but be careful to rule |
403 | ;; out false-positives when matching hunk headers. | |
404 | ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\([^\t\n]+?\\)\\(?:\t.*\\| \\(\\*\\*\\*\\*\\|----\\)\\)?\n" | |
405 | (0 diff-header-face) | |
406 | (2 (if (not (match-end 3)) diff-file-header-face) prepend)) | |
f3abba99 JL |
407 | ("^\\([-<]\\)\\(.*\n\\)" |
408 | (1 diff-indicator-removed-face) (2 diff-removed-face)) | |
409 | ("^\\([+>]\\)\\(.*\n\\)" | |
410 | (1 diff-indicator-added-face) (2 diff-added-face)) | |
411 | ("^\\(!\\)\\(.*\n\\)" | |
bc1b21bb JL |
412 | (1 (if diff-use-changed-face |
413 | diff-indicator-changed-face | |
414 | ;; Otherwise, search for `diff-context-mid-hunk-header-re' and | |
415 | ;; if the line of context diff is above, use `diff-removed-face'; | |
416 | ;; if below, use `diff-added-face'. | |
417 | (save-match-data | |
418 | (let ((limit (save-excursion (diff-beginning-of-hunk)))) | |
419 | (if (save-excursion (re-search-backward diff-context-mid-hunk-header-re limit t)) | |
420 | diff-indicator-added-face | |
421 | diff-indicator-removed-face))))) | |
422 | (2 (if diff-use-changed-face | |
423 | diff-changed-face | |
424 | ;; Otherwise, use the same method as above. | |
425 | (save-match-data | |
426 | (let ((limit (save-excursion (diff-beginning-of-hunk)))) | |
427 | (if (save-excursion (re-search-backward diff-context-mid-hunk-header-re limit t)) | |
428 | diff-added-face | |
429 | diff-removed-face)))))) | |
430 | ("^\\(?:Index\\|revno\\): \\(.+\\).*\n" | |
f3abba99 | 431 | (0 diff-header-face) (1 diff-index-face prepend)) |
7dfb000f | 432 | ("^Only in .*\n" . diff-nonexistent-face) |
f3abba99 | 433 | ("^\\(#\\)\\(.*\\)" |
050dcc13 JL |
434 | (1 font-lock-comment-delimiter-face) |
435 | (2 font-lock-comment-face)) | |
f3abba99 | 436 | ("^[^-=+*!<>#].*\n" (0 diff-context-face)))) |
610a6418 SM |
437 | |
438 | (defconst diff-font-lock-defaults | |
0e104800 | 439 | '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil))) |
610a6418 | 440 | |
ccce6558 DL |
441 | (defvar diff-imenu-generic-expression |
442 | ;; Prefer second name as first is most likely to be a backup or | |
824693e7 DL |
443 | ;; version-control name. The [\t\n] at the end of the unidiff pattern |
444 | ;; catches Debian source diff files (which lack the trailing date). | |
445 | '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs | |
ccce6558 DL |
446 | (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs |
447 | ||
cd632e57 | 448 | ;;;; |
610a6418 | 449 | ;;;; Movement |
cd632e57 | 450 | ;;;; |
610a6418 | 451 | |
f52d2f9c SM |
452 | (defvar diff-valid-unified-empty-line t |
453 | "If non-nil, empty lines are valid in unified diffs. | |
454 | Some versions of diff replace all-blank context lines in unified format with | |
455 | empty lines. This makes the format less robust, but is tolerated. | |
456 | See http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01990.html") | |
457 | ||
458 | (defconst diff-hunk-header-re | |
459 | (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")) | |
365bdf63 | 460 | (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* \n]\\).+\n" (substring diff-hunk-header-re 1))) |
610a6418 SM |
461 | (defvar diff-narrowed-to nil) |
462 | ||
be36f934 | 463 | (defun diff-hunk-style (&optional style) |
996884b2 | 464 | (when (looking-at diff-hunk-header-re) |
be36f934 | 465 | (setq style (cdr (assq (char-after) '((?@ . unified) (?* . context))))) |
996884b2 | 466 | (goto-char (match-end 0))) |
be36f934 SM |
467 | style) |
468 | ||
f52d2f9c | 469 | (defun diff-end-of-hunk (&optional style donttrustheader) |
cb3e7ae0 | 470 | "Advance to the end of the current hunk, and return its position." |
f52d2f9c SM |
471 | (let (end) |
472 | (when (looking-at diff-hunk-header-re) | |
473 | ;; Especially important for unified (because headers are ambiguous). | |
474 | (setq style (diff-hunk-style style)) | |
475 | (goto-char (match-end 0)) | |
476 | (when (and (not donttrustheader) (match-end 2)) | |
43392d12 SM |
477 | (let* ((nold (string-to-number (or (match-string 2) "1"))) |
478 | (nnew (string-to-number (or (match-string 4) "1"))) | |
aec1ef07 | 479 | (endold |
a2be0357 SM |
480 | (save-excursion |
481 | (re-search-forward (if diff-valid-unified-empty-line | |
482 | "^[- \n]" "^[- ]") | |
aec1ef07 | 483 | nil t nold) |
a2be0357 SM |
484 | (line-beginning-position |
485 | ;; Skip potential "\ No newline at end of file". | |
486 | (if (looking-at ".*\n\\\\") 3 2)))) | |
aec1ef07 SM |
487 | (endnew |
488 | ;; The hunk may end with a bunch of "+" lines, so the `end' is | |
489 | ;; then further than computed above. | |
490 | (save-excursion | |
491 | (re-search-forward (if diff-valid-unified-empty-line | |
492 | "^[+ \n]" "^[+ ]") | |
493 | nil t nnew) | |
a2be0357 SM |
494 | (line-beginning-position |
495 | ;; Skip potential "\ No newline at end of file". | |
496 | (if (looking-at ".*\n\\\\") 3 2))))) | |
aec1ef07 | 497 | (setq end (max endold endnew))))) |
f52d2f9c SM |
498 | ;; We may have a first evaluation of `end' thanks to the hunk header. |
499 | (unless end | |
500 | (setq end (and (re-search-forward | |
f58e0fd5 SM |
501 | (pcase style |
502 | (`unified | |
503 | (concat (if diff-valid-unified-empty-line | |
504 | "^[^-+# \\\n]\\|" "^[^-+# \\]\\|") | |
505 | ;; A `unified' header is ambiguous. | |
506 | diff-file-header-re)) | |
507 | (`context "^[^-+#! \\]") | |
508 | (`normal "^[^<>#\\]") | |
509 | (_ "^[^-+#!<> \\]")) | |
f52d2f9c SM |
510 | nil t) |
511 | (match-beginning 0))) | |
512 | (when diff-valid-unified-empty-line | |
513 | ;; While empty lines may be valid inside hunks, they are also likely | |
514 | ;; to be unrelated to the hunk. | |
515 | (goto-char (or end (point-max))) | |
516 | (while (eq ?\n (char-before (1- (point)))) | |
517 | (forward-char -1) | |
518 | (setq end (point))))) | |
0e104800 SM |
519 | ;; The return value is used by easy-mmode-define-navigation. |
520 | (goto-char (or end (point-max))))) | |
610a6418 | 521 | |
fd691799 | 522 | (defun diff-beginning-of-hunk (&optional try-harder) |
cb3e7ae0 CY |
523 | "Move back to the previous hunk beginning, and return its position. |
524 | If point is in a file header rather than a hunk, advance to the | |
525 | next hunk if TRY-HARDER is non-nil; otherwise signal an error." | |
610a6418 | 526 | (beginning-of-line) |
cb3e7ae0 CY |
527 | (if (looking-at diff-hunk-header-re) |
528 | (point) | |
610a6418 SM |
529 | (forward-line 1) |
530 | (condition-case () | |
531 | (re-search-backward diff-hunk-header-re) | |
fd691799 | 532 | (error |
cb3e7ae0 CY |
533 | (unless try-harder |
534 | (error "Can't find the beginning of the hunk")) | |
535 | (diff-beginning-of-file-and-junk) | |
536 | (diff-hunk-next) | |
537 | (point))))) | |
610a6418 | 538 | |
f151b310 SM |
539 | (defun diff-unified-hunk-p () |
540 | (save-excursion | |
541 | (ignore-errors | |
542 | (diff-beginning-of-hunk) | |
543 | (looking-at "^@@")))) | |
544 | ||
610a6418 SM |
545 | (defun diff-beginning-of-file () |
546 | (beginning-of-line) | |
547 | (unless (looking-at diff-file-header-re) | |
4c969f97 SM |
548 | (let ((start (point)) |
549 | res) | |
550 | ;; diff-file-header-re may need to match up to 4 lines, so in case | |
551 | ;; we're inside the header, we need to move up to 3 lines forward. | |
552 | (forward-line 3) | |
553 | (if (and (setq res (re-search-backward diff-file-header-re nil t)) | |
554 | ;; Maybe the 3 lines forward were too much and we matched | |
555 | ;; a file header after our starting point :-( | |
556 | (or (<= (point) start) | |
557 | (setq res (re-search-backward diff-file-header-re nil t)))) | |
558 | res | |
559 | (goto-char start) | |
560 | (error "Can't find the beginning of the file"))))) | |
9201cc28 | 561 | |
610a6418 SM |
562 | |
563 | (defun diff-end-of-file () | |
0e104800 | 564 | (re-search-forward "^[-+#!<>0-9@* \\]" nil t) |
22cd1973 SM |
565 | (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re) |
566 | nil 'move) | |
567 | (if (match-beginning 1) | |
568 | (goto-char (match-beginning 1)) | |
569 | (beginning-of-line))) | |
610a6418 | 570 | |
6be9197e SM |
571 | (defvar diff--auto-refine-data nil) |
572 | ||
d87e5627 SM |
573 | ;; Define diff-{hunk,file}-{prev,next} |
574 | (easy-mmode-define-navigation | |
2659df68 | 575 | diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view |
6be9197e | 576 | (when diff-auto-refine-mode |
1000d895 SM |
577 | (unless (prog1 diff--auto-refine-data |
578 | (setq diff--auto-refine-data | |
579 | (cons (current-buffer) (point-marker)))) | |
580 | (run-at-time 0.0 nil | |
581 | (lambda () | |
582 | (when diff--auto-refine-data | |
583 | (let ((buffer (car diff--auto-refine-data)) | |
584 | (point (cdr diff--auto-refine-data))) | |
585 | (setq diff--auto-refine-data nil) | |
586 | (with-local-quit | |
587 | (when (buffer-live-p buffer) | |
588 | (with-current-buffer buffer | |
589 | (save-excursion | |
590 | (goto-char point) | |
591 | (diff-refine-hunk)))))))))))) | |
2659df68 | 592 | |
d87e5627 | 593 | (easy-mmode-define-navigation |
d65c9521 | 594 | diff-file diff-file-header-re "file" diff-end-of-file) |
610a6418 | 595 | |
cb3e7ae0 CY |
596 | (defun diff-bounds-of-hunk () |
597 | "Return the bounds of the diff hunk at point. | |
598 | The return value is a list (BEG END), which are the hunk's start | |
599 | and end positions. Signal an error if no hunk is found. If | |
600 | point is in a file header, return the bounds of the next hunk." | |
601 | (save-excursion | |
602 | (let ((pos (point)) | |
603 | (beg (diff-beginning-of-hunk t)) | |
604 | (end (diff-end-of-hunk))) | |
605 | (cond ((>= end pos) | |
606 | (list beg end)) | |
607 | ;; If this hunk ends above POS, consider the next hunk. | |
608 | ((re-search-forward diff-hunk-header-re nil t) | |
609 | (list (match-beginning 0) (diff-end-of-hunk))) | |
610 | (t (error "No hunk found")))))) | |
611 | ||
612 | (defun diff-bounds-of-file () | |
613 | "Return the bounds of the file segment at point. | |
614 | The return value is a list (BEG END), which are the segment's | |
615 | start and end positions." | |
616 | (save-excursion | |
617 | (let ((pos (point)) | |
618 | (beg (progn (diff-beginning-of-file-and-junk) | |
619 | (point)))) | |
620 | (diff-end-of-file) | |
621 | ;; bzr puts a newline after the last hunk. | |
622 | (while (looking-at "^\n") | |
623 | (forward-char 1)) | |
624 | (if (> pos (point)) | |
625 | (error "Not inside a file diff")) | |
626 | (list beg (point))))) | |
627 | ||
610a6418 SM |
628 | (defun diff-restrict-view (&optional arg) |
629 | "Restrict the view to the current hunk. | |
630 | If the prefix ARG is given, restrict the view to the current file instead." | |
631 | (interactive "P") | |
cb3e7ae0 CY |
632 | (apply 'narrow-to-region |
633 | (if arg (diff-bounds-of-file) (diff-bounds-of-hunk))) | |
634 | (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))) | |
610a6418 | 635 | |
d87e5627 | 636 | (defun diff-hunk-kill () |
cb3e7ae0 | 637 | "Kill the hunk at point." |
610a6418 | 638 | (interactive) |
cb3e7ae0 CY |
639 | (let* ((hunk-bounds (diff-bounds-of-hunk)) |
640 | (file-bounds (ignore-errors (diff-bounds-of-file))) | |
641 | ;; If the current hunk is the only one for its file, kill the | |
642 | ;; file header too. | |
643 | (bounds (if (and file-bounds | |
644 | (progn (goto-char (car file-bounds)) | |
645 | (= (progn (diff-hunk-next) (point)) | |
646 | (car hunk-bounds))) | |
647 | (progn (goto-char (cadr hunk-bounds)) | |
648 | ;; bzr puts a newline after the last hunk. | |
649 | (while (looking-at "^\n") | |
650 | (forward-char 1)) | |
651 | (= (point) (cadr file-bounds)))) | |
652 | file-bounds | |
653 | hunk-bounds)) | |
dd24cb37 | 654 | (inhibit-read-only t)) |
cb3e7ae0 CY |
655 | (apply 'kill-region bounds) |
656 | (goto-char (car bounds)))) | |
610a6418 | 657 | |
8d73b84e DN |
658 | ;; "index ", "old mode", "new mode", "new file mode" and |
659 | ;; "deleted file mode" are output by git-diff. | |
9201cc28 | 660 | (defconst diff-file-junk-re |
cb3e7ae0 | 661 | "diff \\|index \\|\\(?:deleted file\\|new\\(?: file\\)?\\|old\\) mode\\|=== modified file") |
f52d2f9c | 662 | |
fd691799 SM |
663 | (defun diff-beginning-of-file-and-junk () |
664 | "Go to the beginning of file-related diff-info. | |
665 | This is like `diff-beginning-of-file' except it tries to skip back over leading | |
666 | data such as \"Index: ...\" and such." | |
f52d2f9c SM |
667 | (let* ((orig (point)) |
668 | ;; Skip forward over what might be "leading junk" so as to get | |
669 | ;; closer to the actual diff. | |
670 | (_ (progn (beginning-of-line) | |
671 | (while (looking-at diff-file-junk-re) | |
672 | (forward-line 1)))) | |
673 | (start (point)) | |
4c969f97 SM |
674 | (prevfile (condition-case err |
675 | (save-excursion (diff-beginning-of-file) (point)) | |
676 | (error err))) | |
677 | (err (if (consp prevfile) prevfile)) | |
678 | (nextfile (ignore-errors | |
679 | (save-excursion | |
680 | (goto-char start) (diff-file-next) (point)))) | |
681 | ;; prevhunk is one of the limits. | |
682 | (prevhunk (save-excursion | |
683 | (ignore-errors | |
684 | (if (numberp prevfile) (goto-char prevfile)) | |
685 | (diff-hunk-prev) (point)))) | |
686 | (previndex (save-excursion | |
f52d2f9c | 687 | (forward-line 1) ;In case we're looking at "Index:". |
4c969f97 SM |
688 | (re-search-backward "^Index: " prevhunk t)))) |
689 | ;; If we're in the junk, we should use nextfile instead of prevfile. | |
690 | (if (and (numberp nextfile) | |
691 | (or (not (numberp prevfile)) | |
692 | (and previndex (> previndex prevfile)))) | |
693 | (setq prevfile nextfile)) | |
694 | (if (and previndex (numberp prevfile) (< previndex prevfile)) | |
695 | (setq prevfile previndex)) | |
696 | (if (and (numberp prevfile) (<= prevfile start)) | |
f52d2f9c SM |
697 | (progn |
698 | (goto-char prevfile) | |
699 | ;; Now skip backward over the leading junk we may have before the | |
700 | ;; diff itself. | |
701 | (while (save-excursion | |
702 | (and (zerop (forward-line -1)) | |
703 | (looking-at diff-file-junk-re))) | |
704 | (forward-line -1))) | |
4c969f97 SM |
705 | ;; File starts *after* the starting point: we really weren't in |
706 | ;; a file diff but elsewhere. | |
f52d2f9c | 707 | (goto-char orig) |
4c969f97 | 708 | (signal (car err) (cdr err))))) |
9201cc28 | 709 | |
d87e5627 | 710 | (defun diff-file-kill () |
610a6418 SM |
711 | "Kill current file's hunks." |
712 | (interactive) | |
cb3e7ae0 CY |
713 | (let ((inhibit-read-only t)) |
714 | (apply 'kill-region (diff-bounds-of-file)))) | |
610a6418 | 715 | |
fef8c55b SM |
716 | (defun diff-kill-junk () |
717 | "Kill spurious empty diffs." | |
718 | (interactive) | |
719 | (save-excursion | |
720 | (let ((inhibit-read-only t)) | |
721 | (goto-char (point-min)) | |
722 | (while (re-search-forward (concat "^\\(Index: .*\n\\)" | |
723 | "\\([^-+!* <>].*\n\\)*?" | |
724 | "\\(\\(Index:\\) \\|" | |
725 | diff-file-header-re "\\)") | |
726 | nil t) | |
727 | (delete-region (if (match-end 4) (match-beginning 0) (match-end 1)) | |
728 | (match-beginning 3)) | |
729 | (beginning-of-line))))) | |
730 | ||
cd632e57 SM |
731 | (defun diff-count-matches (re start end) |
732 | (save-excursion | |
733 | (let ((n 0)) | |
734 | (goto-char start) | |
f58e0fd5 | 735 | (while (re-search-forward re end t) (cl-incf n)) |
cd632e57 SM |
736 | n))) |
737 | ||
f151b310 SM |
738 | (defun diff-splittable-p () |
739 | (save-excursion | |
740 | (beginning-of-line) | |
741 | (and (looking-at "^[-+ ]") | |
742 | (progn (forward-line -1) (looking-at "^[-+ ]")) | |
743 | (diff-unified-hunk-p)))) | |
744 | ||
cd632e57 SM |
745 | (defun diff-split-hunk () |
746 | "Split the current (unified diff) hunk at point into two hunks." | |
747 | (interactive) | |
748 | (beginning-of-line) | |
749 | (let ((pos (point)) | |
cb3e7ae0 | 750 | (start (diff-beginning-of-hunk))) |
f52d2f9c | 751 | (unless (looking-at diff-hunk-header-re-unified) |
cd632e57 SM |
752 | (error "diff-split-hunk only works on unified context diffs")) |
753 | (forward-line 1) | |
754 | (let* ((start1 (string-to-number (match-string 1))) | |
f52d2f9c | 755 | (start2 (string-to-number (match-string 3))) |
cd632e57 | 756 | (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos))) |
dd24cb37 CY |
757 | (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos))) |
758 | (inhibit-read-only t)) | |
cd632e57 SM |
759 | (goto-char pos) |
760 | ;; Hopefully the after-change-function will not screw us over. | |
761 | (insert "@@ -" (number-to-string newstart1) ",1 +" | |
762 | (number-to-string newstart2) ",1 @@\n") | |
763 | ;; Fix the original hunk-header. | |
764 | (diff-fixup-modifs start pos)))) | |
71296446 | 765 | |
cd632e57 | 766 | |
610a6418 SM |
767 | ;;;; |
768 | ;;;; jump to other buffers | |
769 | ;;;; | |
770 | ||
0b82e382 | 771 | (defvar diff-remembered-files-alist nil) |
adf4cc7e | 772 | (defvar diff-remembered-defdir nil) |
0b82e382 | 773 | |
610a6418 SM |
774 | (defun diff-filename-drop-dir (file) |
775 | (when (string-match "/" file) (substring file (match-end 0)))) | |
776 | ||
0b82e382 SM |
777 | (defun diff-merge-strings (ancestor from to) |
778 | "Merge the diff between ANCESTOR and FROM into TO. | |
779 | Returns the merged string if successful or nil otherwise. | |
d87e5627 | 780 | The strings are assumed not to contain any \"\\n\" (i.e. end of line). |
0b82e382 SM |
781 | If ANCESTOR = FROM, returns TO. |
782 | If ANCESTOR = TO, returns FROM. | |
783 | The heuristic is simplistic and only really works for cases | |
784 | like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")." | |
785 | ;; Ideally, we want: | |
786 | ;; AMB ANB CMD -> CND | |
787 | ;; but that's ambiguous if `foo' or `bar' is empty: | |
788 | ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1 | |
d87e5627 | 789 | (let ((str (concat ancestor "\n" from "\n" to))) |
0b82e382 | 790 | (when (and (string-match (concat |
d87e5627 SM |
791 | "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n" |
792 | "\\1\\(.*\\)\\3\n" | |
0b82e382 SM |
793 | "\\(.*\\(\\2\\).*\\)\\'") str) |
794 | (equal to (match-string 5 str))) | |
795 | (concat (substring str (match-beginning 5) (match-beginning 6)) | |
796 | (match-string 4 str) | |
797 | (substring str (match-end 6) (match-end 5)))))) | |
798 | ||
22cd1973 SM |
799 | (defun diff-tell-file-name (old name) |
800 | "Tell Emacs where the find the source file of the current hunk. | |
801 | If the OLD prefix arg is passed, tell the file NAME of the old file." | |
802 | (interactive | |
803 | (let* ((old current-prefix-arg) | |
804 | (fs (diff-hunk-file-names current-prefix-arg))) | |
805 | (unless fs (error "No file name to look for")) | |
806 | (list old (read-file-name (format "File for %s: " (car fs)) | |
68035b97 | 807 | nil (diff-find-file-name old 'noprompt) t)))) |
22cd1973 SM |
808 | (let ((fs (diff-hunk-file-names old))) |
809 | (unless fs (error "No file name to look for")) | |
810 | (push (cons fs name) diff-remembered-files-alist))) | |
cfc80227 | 811 | |
22cd1973 SM |
812 | (defun diff-hunk-file-names (&optional old) |
813 | "Give the list of file names textually mentioned for the current hunk." | |
610a6418 SM |
814 | (save-excursion |
815 | (unless (looking-at diff-file-header-re) | |
816 | (or (ignore-errors (diff-beginning-of-file)) | |
817 | (re-search-forward diff-file-header-re nil t))) | |
22cd1973 | 818 | (let ((limit (save-excursion |
610a6418 | 819 | (condition-case () |
d87e5627 | 820 | (progn (diff-hunk-prev) (point)) |
610a6418 | 821 | (error (point-min))))) |
22cd1973 | 822 | (header-files |
3602cceb SS |
823 | ;; handle filenames with spaces; |
824 | ;; cf. diff-font-lock-keywords / diff-file-header-face | |
825 | (if (looking-at "[-*][-*][-*] \\([^\t]+\\)\t.*\n[-+][-+][-+] \\([^\t]+\\)") | |
826 | (list (if old (match-string 1) (match-string 2)) | |
827 | (if old (match-string 2) (match-string 1))) | |
22cd1973 SM |
828 | (forward-line 1) nil))) |
829 | (delq nil | |
830 | (append | |
831 | (when (and (not old) | |
832 | (save-excursion | |
833 | (re-search-backward "^Index: \\(.+\\)" limit t))) | |
834 | (list (match-string 1))) | |
835 | header-files | |
3602cceb | 836 | ;; this assumes that there are no spaces in filenames |
22cd1973 SM |
837 | (when (re-search-backward |
838 | "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" | |
839 | nil t) | |
840 | (list (if old (match-string 2) (match-string 4)) | |
841 | (if old (match-string 4) (match-string 2))))))))) | |
842 | ||
68035b97 | 843 | (defun diff-find-file-name (&optional old noprompt prefix) |
22cd1973 | 844 | "Return the file corresponding to the current patch. |
e78cf8e5 | 845 | Non-nil OLD means that we want the old file. |
68035b97 | 846 | Non-nil NOPROMPT means to prefer returning nil than to prompt the user. |
e78cf8e5 | 847 | PREFIX is only used internally: don't use it." |
adf4cc7e SM |
848 | (unless (equal diff-remembered-defdir default-directory) |
849 | ;; Flush diff-remembered-files-alist if the default-directory is changed. | |
850 | (set (make-local-variable 'diff-remembered-defdir) default-directory) | |
851 | (set (make-local-variable 'diff-remembered-files-alist) nil)) | |
22cd1973 SM |
852 | (save-excursion |
853 | (unless (looking-at diff-file-header-re) | |
854 | (or (ignore-errors (diff-beginning-of-file)) | |
855 | (re-search-forward diff-file-header-re nil t))) | |
856 | (let ((fs (diff-hunk-file-names old))) | |
e78cf8e5 | 857 | (if prefix (setq fs (mapcar (lambda (f) (concat prefix f)) fs))) |
610a6418 | 858 | (or |
0b82e382 SM |
859 | ;; use any previously used preference |
860 | (cdr (assoc fs diff-remembered-files-alist)) | |
861 | ;; try to be clever and use previous choices as an inspiration | |
f58e0fd5 | 862 | (cl-dolist (rf diff-remembered-files-alist) |
0b82e382 | 863 | (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf)))) |
f58e0fd5 | 864 | (if (and newfile (file-exists-p newfile)) (cl-return newfile)))) |
0b82e382 SM |
865 | ;; look for each file in turn. If none found, try again but |
866 | ;; ignoring the first level of directory, ... | |
f58e0fd5 SM |
867 | (cl-do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files))) |
868 | (file nil nil)) | |
0b82e382 | 869 | ((or (null files) |
f58e0fd5 SM |
870 | (setq file (cl-do* ((files files (cdr files)) |
871 | (file (car files) (car files))) | |
f9274544 GM |
872 | ;; Use file-regular-p to avoid |
873 | ;; /dev/null, directories, etc. | |
874 | ((or (null file) (file-regular-p file)) | |
0b82e382 SM |
875 | file)))) |
876 | file)) | |
877 | ;; <foo>.rej patches implicitly apply to <foo> | |
610a6418 SM |
878 | (and (string-match "\\.rej\\'" (or buffer-file-name "")) |
879 | (let ((file (substring buffer-file-name 0 (match-beginning 0)))) | |
880 | (when (file-exists-p file) file))) | |
e78cf8e5 SM |
881 | ;; If we haven't found the file, maybe it's because we haven't paid |
882 | ;; attention to the PCL-CVS hint. | |
883 | (and (not prefix) | |
884 | (boundp 'cvs-pcl-cvs-dirchange-re) | |
885 | (save-excursion | |
886 | (re-search-backward cvs-pcl-cvs-dirchange-re nil t)) | |
68035b97 | 887 | (diff-find-file-name old noprompt (match-string 1))) |
0b82e382 | 888 | ;; if all else fails, ask the user |
68035b97 | 889 | (unless noprompt |
f58e0fd5 | 890 | (let ((file (expand-file-name (or (car fs) "")))) |
f3af92b7 MA |
891 | (setq file |
892 | (read-file-name (format "Use file %s: " file) | |
893 | (file-name-directory file) file t | |
894 | (file-name-nondirectory file))) | |
68035b97 SM |
895 | (set (make-local-variable 'diff-remembered-files-alist) |
896 | (cons (cons fs file) diff-remembered-files-alist)) | |
897 | file)))))) | |
610a6418 | 898 | |
1b1b5dae | 899 | |
610a6418 SM |
900 | (defun diff-ediff-patch () |
901 | "Call `ediff-patch-file' on the current buffer." | |
902 | (interactive) | |
ba83908c | 903 | (condition-case nil |
0e104800 | 904 | (ediff-patch-file nil (current-buffer)) |
610a6418 SM |
905 | (wrong-number-of-arguments (ediff-patch-file)))) |
906 | ||
71296446 | 907 | ;;;; |
610a6418 | 908 | ;;;; Conversion functions |
71296446 | 909 | ;;;; |
610a6418 SM |
910 | |
911 | ;;(defvar diff-inhibit-after-change nil | |
912 | ;; "Non-nil means inhibit `diff-mode's after-change functions.") | |
913 | ||
914 | (defun diff-unified->context (start end) | |
915 | "Convert unified diffs to context diffs. | |
916 | START and END are either taken from the region (if a prefix arg is given) or | |
dfbd373d | 917 | else cover the whole buffer." |
d7f9cc85 | 918 | (interactive (if (or current-prefix-arg (use-region-p)) |
a70b0ff8 | 919 | (list (region-beginning) (region-end)) |
610a6418 | 920 | (list (point-min) (point-max)))) |
e78cf8e5 | 921 | (unless (markerp end) (setq end (copy-marker end t))) |
610a6418 SM |
922 | (let (;;(diff-inhibit-after-change t) |
923 | (inhibit-read-only t)) | |
924 | (save-excursion | |
925 | (goto-char start) | |
f52d2f9c SM |
926 | (while (and (re-search-forward |
927 | (concat "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|" | |
928 | diff-hunk-header-re-unified ".*\\)$") | |
929 | nil t) | |
610a6418 SM |
930 | (< (point) end)) |
931 | (combine-after-change-calls | |
932 | (if (match-beginning 2) | |
0e104800 | 933 | ;; we matched a file header |
610a6418 SM |
934 | (progn |
935 | ;; use reverse order to make sure the indices are kept valid | |
936 | (replace-match "---" t t nil 3) | |
937 | (replace-match "***" t t nil 2)) | |
938 | ;; we matched a hunk header | |
939 | (let ((line1 (match-string 4)) | |
43392d12 | 940 | (lines1 (or (match-string 5) "1")) |
610a6418 | 941 | (line2 (match-string 6)) |
341dd15a | 942 | (lines2 (or (match-string 7) "1")) |
3dc04e83 SM |
943 | ;; Variables to use the special undo function. |
944 | (old-undo buffer-undo-list) | |
945 | (old-end (marker-position end)) | |
946 | (start (match-beginning 0)) | |
947 | (reversible t)) | |
610a6418 SM |
948 | (replace-match |
949 | (concat "***************\n*** " line1 "," | |
950 | (number-to-string (+ (string-to-number line1) | |
951 | (string-to-number lines1) | |
341dd15a MB |
952 | -1)) |
953 | " ****")) | |
610a6418 | 954 | (save-restriction |
f52d2f9c SM |
955 | (narrow-to-region (line-beginning-position 2) |
956 | ;; Call diff-end-of-hunk from just before | |
957 | ;; the hunk header so it can use the hunk | |
958 | ;; header info. | |
610a6418 SM |
959 | (progn (diff-end-of-hunk 'unified) (point))) |
960 | (let ((hunk (buffer-string))) | |
961 | (goto-char (point-min)) | |
962 | (if (not (save-excursion (re-search-forward "^-" nil t))) | |
963 | (delete-region (point) (point-max)) | |
964 | (goto-char (point-max)) | |
965 | (let ((modif nil) last-pt) | |
966 | (while (progn (setq last-pt (point)) | |
967 | (= (forward-line -1) 0)) | |
f58e0fd5 | 968 | (pcase (char-after) |
bdad2e31 | 969 | (?\s (insert " ") (setq modif nil) (backward-char 1)) |
610a6418 SM |
970 | (?+ (delete-region (point) last-pt) (setq modif t)) |
971 | (?- (if (not modif) | |
f58e0fd5 SM |
972 | (progn (forward-char 1) |
973 | (insert " ")) | |
974 | (delete-char 1) | |
975 | (insert "! ")) | |
976 | (backward-char 2)) | |
610a6418 | 977 | (?\\ (when (save-excursion (forward-line -1) |
f58e0fd5 SM |
978 | (= (char-after) ?+)) |
979 | (delete-region (point) last-pt) | |
980 | (setq modif t))) | |
f52d2f9c | 981 | ;; diff-valid-unified-empty-line. |
f58e0fd5 SM |
982 | (?\n (insert " ") (setq modif nil) |
983 | (backward-char 2)) | |
984 | (_ (setq modif nil)))))) | |
610a6418 SM |
985 | (goto-char (point-max)) |
986 | (save-excursion | |
987 | (insert "--- " line2 "," | |
988 | (number-to-string (+ (string-to-number line2) | |
989 | (string-to-number lines2) | |
341dd15a | 990 | -1)) |
f52d2f9c | 991 | " ----\n" hunk)) |
610a6418 SM |
992 | ;;(goto-char (point-min)) |
993 | (forward-line 1) | |
994 | (if (not (save-excursion (re-search-forward "^+" nil t))) | |
995 | (delete-region (point) (point-max)) | |
996 | (let ((modif nil) (delete nil)) | |
f58e0fd5 SM |
997 | (if (save-excursion (re-search-forward "^\\+.*\n-" |
998 | nil t)) | |
3dc04e83 SM |
999 | ;; Normally, lines in a substitution come with |
1000 | ;; first the removals and then the additions, and | |
1001 | ;; the context->unified function follows this | |
1002 | ;; convention, of course. Yet, other alternatives | |
1003 | ;; are valid as well, but they preclude the use of | |
1004 | ;; context->unified as an undo command. | |
1005 | (setq reversible nil)) | |
610a6418 | 1006 | (while (not (eobp)) |
f58e0fd5 | 1007 | (pcase (char-after) |
bdad2e31 | 1008 | (?\s (insert " ") (setq modif nil) (backward-char 1)) |
610a6418 SM |
1009 | (?- (setq delete t) (setq modif t)) |
1010 | (?+ (if (not modif) | |
f58e0fd5 SM |
1011 | (progn (forward-char 1) |
1012 | (insert " ")) | |
1013 | (delete-char 1) | |
1014 | (insert "! ")) | |
1015 | (backward-char 2)) | |
610a6418 | 1016 | (?\\ (when (save-excursion (forward-line 1) |
f58e0fd5 SM |
1017 | (not (eobp))) |
1018 | (setq delete t) (setq modif t))) | |
f52d2f9c SM |
1019 | ;; diff-valid-unified-empty-line. |
1020 | (?\n (insert " ") (setq modif nil) (backward-char 2) | |
1021 | (setq reversible nil)) | |
f58e0fd5 | 1022 | (_ (setq modif nil))) |
610a6418 SM |
1023 | (let ((last-pt (point))) |
1024 | (forward-line 1) | |
1025 | (when delete | |
1026 | (delete-region last-pt (point)) | |
3dc04e83 SM |
1027 | (setq delete nil))))))) |
1028 | (unless (or (not reversible) (eq buffer-undo-list t)) | |
1029 | ;; Drop the many undo entries and replace them with | |
1030 | ;; a single entry that uses diff-context->unified to do | |
1031 | ;; the work. | |
1032 | (setq buffer-undo-list | |
1033 | (cons (list 'apply (- old-end end) start (point-max) | |
1034 | 'diff-context->unified start (point-max)) | |
1035 | old-undo))))))))))) | |
610a6418 | 1036 | |
28408bfd | 1037 | (defun diff-context->unified (start end &optional to-context) |
610a6418 | 1038 | "Convert context diffs to unified diffs. |
28408bfd RS |
1039 | START and END are either taken from the region |
1040 | \(when it is highlighted) or else cover the whole buffer. | |
1041 | With a prefix argument, convert unified format to context format." | |
d7f9cc85 | 1042 | (interactive (if (use-region-p) |
a70b0ff8 | 1043 | (list (region-beginning) (region-end) current-prefix-arg) |
28408bfd RS |
1044 | (list (point-min) (point-max) current-prefix-arg))) |
1045 | (if to-context | |
1046 | (diff-unified->context start end) | |
e78cf8e5 | 1047 | (unless (markerp end) (setq end (copy-marker end t))) |
28408bfd | 1048 | (let ( ;;(diff-inhibit-after-change t) |
be36f934 | 1049 | (inhibit-read-only t)) |
28408bfd | 1050 | (save-excursion |
be36f934 | 1051 | (goto-char start) |
d7f9cc85 | 1052 | (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)\\(?: \\(.*\\)\\|$\\)" nil t) |
be36f934 SM |
1053 | (< (point) end)) |
1054 | (combine-after-change-calls | |
1055 | (if (match-beginning 2) | |
1056 | ;; we matched a file header | |
1057 | (progn | |
1058 | ;; use reverse order to make sure the indices are kept valid | |
1059 | (replace-match "+++" t t nil 3) | |
1060 | (replace-match "---" t t nil 2)) | |
1061 | ;; we matched a hunk header | |
1062 | (let ((line1s (match-string 4)) | |
1063 | (line1e (match-string 5)) | |
1064 | (pt1 (match-beginning 0)) | |
1065 | ;; Variables to use the special undo function. | |
1066 | (old-undo buffer-undo-list) | |
1067 | (old-end (marker-position end)) | |
d7f9cc85 SM |
1068 | ;; We currently throw away the comment that can follow |
1069 | ;; the hunk header. FIXME: Preserve it instead! | |
1070 | (reversible (not (match-end 6)))) | |
be36f934 SM |
1071 | (replace-match "") |
1072 | (unless (re-search-forward | |
bf6970a5 | 1073 | diff-context-mid-hunk-header-re nil t) |
be36f934 SM |
1074 | (error "Can't find matching `--- n1,n2 ----' line")) |
1075 | (let ((line2s (match-string 1)) | |
1076 | (line2e (match-string 2)) | |
1077 | (pt2 (progn | |
1078 | (delete-region (progn (beginning-of-line) (point)) | |
1079 | (progn (forward-line 1) (point))) | |
1080 | (point-marker)))) | |
1081 | (goto-char pt1) | |
1082 | (forward-line 1) | |
1083 | (while (< (point) pt2) | |
f58e0fd5 | 1084 | (pcase (char-after) |
be36f934 SM |
1085 | (?! (delete-char 2) (insert "-") (forward-line 1)) |
1086 | (?- (forward-char 1) (delete-char 1) (forward-line 1)) | |
f58e0fd5 | 1087 | (?\s ;merge with the other half of the chunk |
be36f934 SM |
1088 | (let* ((endline2 |
1089 | (save-excursion | |
1090 | (goto-char pt2) (forward-line 1) (point)))) | |
f58e0fd5 SM |
1091 | (pcase (char-after pt2) |
1092 | ((or ?! ?+) | |
be36f934 | 1093 | (insert "+" |
f58e0fd5 SM |
1094 | (prog1 |
1095 | (buffer-substring (+ pt2 2) endline2) | |
be36f934 SM |
1096 | (delete-region pt2 endline2)))) |
1097 | (?\s | |
1098 | (unless (= (- endline2 pt2) | |
1099 | (- (line-beginning-position 2) (point))) | |
1100 | ;; If the two lines we're merging don't have the | |
1101 | ;; same length (can happen with "diff -b"), then | |
1102 | ;; diff-unified->context will not properly undo | |
1103 | ;; this operation. | |
1104 | (setq reversible nil)) | |
1105 | (delete-region pt2 endline2) | |
1106 | (delete-char 1) | |
1107 | (forward-line 1)) | |
1108 | (?\\ (forward-line 1)) | |
f58e0fd5 | 1109 | (_ (setq reversible nil) |
be36f934 | 1110 | (delete-char 1) (forward-line 1))))) |
f58e0fd5 | 1111 | (_ (setq reversible nil) (forward-line 1)))) |
be36f934 SM |
1112 | (while (looking-at "[+! ] ") |
1113 | (if (/= (char-after) ?!) (forward-char 1) | |
1114 | (delete-char 1) (insert "+")) | |
1115 | (delete-char 1) (forward-line 1)) | |
1116 | (save-excursion | |
1117 | (goto-char pt1) | |
1118 | (insert "@@ -" line1s "," | |
1119 | (number-to-string (- (string-to-number line1e) | |
1120 | (string-to-number line1s) | |
1121 | -1)) | |
1122 | " +" line2s "," | |
1123 | (number-to-string (- (string-to-number line2e) | |
1124 | (string-to-number line2s) | |
1125 | -1)) " @@")) | |
1126 | (set-marker pt2 nil) | |
1127 | ;; The whole procedure succeeded, let's replace the myriad | |
1128 | ;; of undo elements with just a single special one. | |
1129 | (unless (or (not reversible) (eq buffer-undo-list t)) | |
1130 | (setq buffer-undo-list | |
1131 | (cons (list 'apply (- old-end end) pt1 (point) | |
1132 | 'diff-unified->context pt1 (point)) | |
1133 | old-undo))) | |
1134 | ))))))))) | |
610a6418 SM |
1135 | |
1136 | (defun diff-reverse-direction (start end) | |
1137 | "Reverse the direction of the diffs. | |
1138 | START and END are either taken from the region (if a prefix arg is given) or | |
dfbd373d | 1139 | else cover the whole buffer." |
d7f9cc85 | 1140 | (interactive (if (or current-prefix-arg (use-region-p)) |
a70b0ff8 | 1141 | (list (region-beginning) (region-end)) |
610a6418 | 1142 | (list (point-min) (point-max)))) |
e78cf8e5 | 1143 | (unless (markerp end) (setq end (copy-marker end t))) |
610a6418 SM |
1144 | (let (;;(diff-inhibit-after-change t) |
1145 | (inhibit-read-only t)) | |
1146 | (save-excursion | |
1147 | (goto-char start) | |
c0078a04 | 1148 | (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t) |
610a6418 SM |
1149 | (< (point) end)) |
1150 | (combine-after-change-calls | |
1151 | (cond | |
1152 | ;; a file header | |
1153 | ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil)) | |
1154 | ;; a context-diff hunk header | |
1155 | ((match-beginning 6) | |
1156 | (let ((pt-lines1 (match-beginning 6)) | |
1157 | (lines1 (match-string 6))) | |
1158 | (replace-match "" nil nil nil 6) | |
1159 | (forward-line 1) | |
1160 | (let ((half1s (point))) | |
0e104800 | 1161 | (while (looking-at "[-! \\][ \t]\\|#") |
610a6418 SM |
1162 | (when (= (char-after) ?-) (delete-char 1) (insert "+")) |
1163 | (forward-line 1)) | |
fef8c55b | 1164 | (let ((half1 (delete-and-extract-region half1s (point)))) |
bf6970a5 | 1165 | (unless (looking-at diff-context-mid-hunk-header-re) |
610a6418 SM |
1166 | (insert half1) |
1167 | (error "Can't find matching `--- n1,n2 ----' line")) | |
bf6970a5 SM |
1168 | (let* ((str1end (or (match-end 2) (match-end 1))) |
1169 | (str1 (buffer-substring (match-beginning 1) str1end))) | |
1170 | (goto-char str1end) | |
1171 | (insert lines1) | |
1172 | (delete-region (match-beginning 1) str1end) | |
610a6418 SM |
1173 | (forward-line 1) |
1174 | (let ((half2s (point))) | |
0e104800 | 1175 | (while (looking-at "[!+ \\][ \t]\\|#") |
610a6418 SM |
1176 | (when (= (char-after) ?+) (delete-char 1) (insert "-")) |
1177 | (forward-line 1)) | |
fef8c55b | 1178 | (let ((half2 (delete-and-extract-region half2s (point)))) |
0e104800 | 1179 | (insert (or half1 "")) |
610a6418 | 1180 | (goto-char half1s) |
0e104800 | 1181 | (insert (or half2 "")))) |
610a6418 SM |
1182 | (goto-char pt-lines1) |
1183 | (insert str1)))))) | |
1184 | ;; a unified-diff hunk header | |
1185 | ((match-beginning 7) | |
1186 | (replace-match "@@ -\\8 +\\7 @@" nil) | |
1187 | (forward-line 1) | |
1188 | (let ((c (char-after)) first last) | |
f58e0fd5 | 1189 | (while (pcase (setq c (char-after)) |
610a6418 | 1190 | (?- (setq first (or first (point))) |
f58e0fd5 | 1191 | (delete-char 1) (insert "+") t) |
610a6418 | 1192 | (?+ (setq last (or last (point))) |
f58e0fd5 SM |
1193 | (delete-char 1) (insert "-") t) |
1194 | ((or ?\\ ?#) t) | |
1195 | (_ (when (and first last (< first last)) | |
cb4e8176 | 1196 | (insert (delete-and-extract-region first last))) |
610a6418 | 1197 | (setq first nil last nil) |
f52d2f9c SM |
1198 | (memq c (if diff-valid-unified-empty-line |
1199 | '(?\s ?\n) '(?\s))))) | |
610a6418 SM |
1200 | (forward-line 1)))))))))) |
1201 | ||
1202 | (defun diff-fixup-modifs (start end) | |
1203 | "Fixup the hunk headers (in case the buffer was modified). | |
1204 | START and END are either taken from the region (if a prefix arg is given) or | |
dfbd373d | 1205 | else cover the whole buffer." |
d7f9cc85 | 1206 | (interactive (if (or current-prefix-arg (use-region-p)) |
a70b0ff8 | 1207 | (list (region-beginning) (region-end)) |
610a6418 SM |
1208 | (list (point-min) (point-max)))) |
1209 | (let ((inhibit-read-only t)) | |
1210 | (save-excursion | |
f52d2f9c | 1211 | (goto-char end) (diff-end-of-hunk nil 'donttrustheader) |
610a6418 | 1212 | (let ((plus 0) (minus 0) (space 0) (bang 0)) |
1aba75c2 | 1213 | (while (and (= (forward-line -1) 0) (<= start (point))) |
165fd2df AS |
1214 | (if (not (looking-at |
1215 | (concat diff-hunk-header-re-unified | |
1216 | "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$" | |
1217 | "\\|--- .+\n\\+\\+\\+ "))) | |
f58e0fd5 SM |
1218 | (pcase (char-after) |
1219 | (?\s (cl-incf space)) | |
1220 | (?+ (cl-incf plus)) | |
1221 | (?- (cl-incf minus)) | |
1222 | (?! (cl-incf bang)) | |
1223 | ((or ?\\ ?#) nil) | |
1224 | (_ (setq space 0 plus 0 minus 0 bang 0))) | |
165fd2df AS |
1225 | (cond |
1226 | ((looking-at diff-hunk-header-re-unified) | |
1227 | (let* ((old1 (match-string 2)) | |
1228 | (old2 (match-string 4)) | |
1229 | (new1 (number-to-string (+ space minus))) | |
1230 | (new2 (number-to-string (+ space plus)))) | |
1231 | (if old2 | |
1232 | (unless (string= new2 old2) (replace-match new2 t t nil 4)) | |
1233 | (goto-char (match-end 3)) | |
1234 | (insert "," new2)) | |
1235 | (if old1 | |
1236 | (unless (string= new1 old1) (replace-match new1 t t nil 2)) | |
1237 | (goto-char (match-end 1)) | |
1238 | (insert "," new1)))) | |
1239 | ((looking-at diff-context-mid-hunk-header-re) | |
1240 | (when (> (+ space bang plus) 0) | |
1241 | (let* ((old1 (match-string 1)) | |
1242 | (old2 (match-string 2)) | |
1243 | (new (number-to-string | |
1244 | (+ space bang plus -1 (string-to-number old1))))) | |
1245 | (unless (string= new old2) (replace-match new t t nil 2))))) | |
1246 | ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$") | |
1247 | (when (> (+ space bang minus) 0) | |
1248 | (let* ((old (match-string 1)) | |
1249 | (new (format | |
1250 | (concat "%0" (number-to-string (length old)) "d") | |
1251 | (+ space bang minus -1 (string-to-number old))))) | |
1252 | (unless (string= new old) (replace-match new t t nil 2)))))) | |
1253 | (setq space 0 plus 0 minus 0 bang 0))))))) | |
610a6418 | 1254 | |
71296446 | 1255 | ;;;; |
610a6418 | 1256 | ;;;; Hooks |
71296446 | 1257 | ;;;; |
610a6418 SM |
1258 | |
1259 | (defun diff-write-contents-hooks () | |
1260 | "Fixup hunk headers if necessary." | |
1261 | (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max))) | |
1262 | nil) | |
1263 | ||
610a6418 SM |
1264 | ;; It turns out that making changes in the buffer from within an |
1265 | ;; *-change-function is asking for trouble, whereas making them | |
1266 | ;; from a post-command-hook doesn't pose much problems | |
1267 | (defvar diff-unhandled-changes nil) | |
ba83908c | 1268 | (defun diff-after-change-function (beg end _len) |
610a6418 SM |
1269 | "Remember to fixup the hunk header. |
1270 | See `after-change-functions' for the meaning of BEG, END and LEN." | |
c0078a04 SM |
1271 | ;; Ignoring changes when inhibit-read-only is set is strictly speaking |
1272 | ;; incorrect, but it turns out that inhibit-read-only is normally not set | |
1273 | ;; inside editing commands, while it tends to be set when the buffer gets | |
1274 | ;; updated by an async process or by a conversion function, both of which | |
1275 | ;; would rather not be uselessly slowed down by this hook. | |
610a6418 SM |
1276 | (when (and (not undo-in-progress) (not inhibit-read-only)) |
1277 | (if diff-unhandled-changes | |
1278 | (setq diff-unhandled-changes | |
1279 | (cons (min beg (car diff-unhandled-changes)) | |
cb4e8176 | 1280 | (max end (cdr diff-unhandled-changes)))) |
610a6418 SM |
1281 | (setq diff-unhandled-changes (cons beg end))))) |
1282 | ||
1283 | (defun diff-post-command-hook () | |
1284 | "Fixup hunk headers if necessary." | |
1285 | (when (consp diff-unhandled-changes) | |
1286 | (ignore-errors | |
1287 | (save-excursion | |
fef8c55b | 1288 | (goto-char (car diff-unhandled-changes)) |
cb4e8176 SM |
1289 | ;; Maybe we've cut the end of the hunk before point. |
1290 | (if (and (bolp) (not (bobp))) (backward-char 1)) | |
bf6970a5 SM |
1291 | ;; We used to fixup modifs on all the changes, but it turns out that |
1292 | ;; it's safer not to do it on big changes, e.g. when yanking a big | |
1293 | ;; diff, or when the user edits the header, since we might then | |
1294 | ;; screw up perfectly correct values. --Stef | |
1eabc5e6 | 1295 | (diff-beginning-of-hunk) |
bf6970a5 SM |
1296 | (let* ((style (if (looking-at "\\*\\*\\*") 'context)) |
1297 | (start (line-beginning-position (if (eq style 'context) 3 2))) | |
1298 | (mid (if (eq style 'context) | |
1299 | (save-excursion | |
1300 | (re-search-forward diff-context-mid-hunk-header-re | |
1301 | nil t))))) | |
1302 | (when (and ;; Don't try to fixup changes in the hunk header. | |
f4d79bd0 | 1303 | (>= (car diff-unhandled-changes) start) |
bf6970a5 SM |
1304 | ;; Don't try to fixup changes in the mid-hunk header either. |
1305 | (or (not mid) | |
1306 | (< (cdr diff-unhandled-changes) (match-beginning 0)) | |
1307 | (> (car diff-unhandled-changes) (match-end 0))) | |
1308 | (save-excursion | |
f52d2f9c | 1309 | (diff-end-of-hunk nil 'donttrustheader) |
bf6970a5 SM |
1310 | ;; Don't try to fixup changes past the end of the hunk. |
1311 | (>= (point) (cdr diff-unhandled-changes)))) | |
1eabc5e6 | 1312 | (diff-fixup-modifs (point) (cdr diff-unhandled-changes))))) |
bf6970a5 | 1313 | (setq diff-unhandled-changes nil)))) |
610a6418 | 1314 | |
6fc049f6 SM |
1315 | (defun diff-next-error (arg reset) |
1316 | ;; Select a window that displays the current buffer so that point | |
1317 | ;; movements are reflected in that window. Otherwise, the user might | |
1318 | ;; never see the hunk corresponding to the source she's jumping to. | |
1319 | (pop-to-buffer (current-buffer)) | |
1320 | (if reset (goto-char (point-min))) | |
1321 | (diff-hunk-next arg) | |
1322 | (diff-goto-source)) | |
610a6418 | 1323 | |
e8f642e7 GM |
1324 | (defvar whitespace-style) |
1325 | (defvar whitespace-trailing-regexp) | |
1326 | ||
610a6418 | 1327 | ;;;###autoload |
d87e5627 | 1328 | (define-derived-mode diff-mode fundamental-mode "Diff" |
0b82e382 | 1329 | "Major mode for viewing/editing context diffs. |
769dd0f1 | 1330 | Supports unified and context diffs as well as (to a lesser extent) |
6035b97e | 1331 | normal diffs. |
b64d66b5 | 1332 | |
df52e0e7 | 1333 | When the buffer is read-only, the ESC prefix is not necessary. |
3b696504 | 1334 | If you edit the buffer manually, diff-mode will try to update the hunk |
df52e0e7 SM |
1335 | headers for you on-the-fly. |
1336 | ||
1337 | You can also switch between context diff and unified diff with \\[diff-context->unified], | |
72147c53 | 1338 | or vice versa with \\[diff-unified->context] and you can also reverse the direction of |
e78cf8e5 | 1339 | a diff with \\[diff-reverse-direction]. |
b64d66b5 | 1340 | |
b64d66b5 RS |
1341 | \\{diff-mode-map}" |
1342 | ||
610a6418 | 1343 | (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults) |
6be9197e SM |
1344 | (add-hook 'font-lock-mode-hook |
1345 | (lambda () (remove-overlays nil nil 'diff-mode 'fine)) | |
1346 | nil 'local) | |
610a6418 | 1347 | (set (make-local-variable 'outline-regexp) diff-outline-regexp) |
ccce6558 DL |
1348 | (set (make-local-variable 'imenu-generic-expression) |
1349 | diff-imenu-generic-expression) | |
19e713d8 DL |
1350 | ;; These are not perfect. They would be better done separately for |
1351 | ;; context diffs and unidiffs. | |
1352 | ;; (set (make-local-variable 'paragraph-start) | |
1353 | ;; (concat "@@ " ; unidiff hunk | |
1354 | ;; "\\|\\*\\*\\* " ; context diff hunk or file start | |
1355 | ;; "\\|--- [^\t]+\t")) ; context or unidiff file | |
1356 | ;; ; start (first or second line) | |
1357 | ;; (set (make-local-variable 'paragraph-separate) paragraph-start) | |
1358 | ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t") | |
1359 | ;; compile support | |
6fc049f6 | 1360 | (set (make-local-variable 'next-error-function) 'diff-next-error) |
cdbb79c1 | 1361 | |
19446c41 DN |
1362 | (set (make-local-variable 'beginning-of-defun-function) |
1363 | 'diff-beginning-of-file-and-junk) | |
1364 | (set (make-local-variable 'end-of-defun-function) | |
1365 | 'diff-end-of-file) | |
1366 | ||
07875ee7 | 1367 | (diff-setup-whitespace) |
ac7020b3 | 1368 | |
e1b01c7f IS |
1369 | (if diff-default-read-only |
1370 | (setq buffer-read-only t)) | |
769dd0f1 | 1371 | ;; setup change hooks |
e8a1ed31 | 1372 | (if (not diff-update-on-the-fly) |
3ccdb14e | 1373 | (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t) |
610a6418 | 1374 | (make-local-variable 'diff-unhandled-changes) |
3cec9c57 SM |
1375 | (add-hook 'after-change-functions 'diff-after-change-function nil t) |
1376 | (add-hook 'post-command-hook 'diff-post-command-hook nil t)) | |
610a6418 | 1377 | ;; Neat trick from Dave Love to add more bindings in read-only mode: |
a9de04fa | 1378 | (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map))) |
22cd1973 SM |
1379 | (add-to-list 'minor-mode-overriding-map-alist ro-bind) |
1380 | ;; Turn off this little trick in case the buffer is put in view-mode. | |
1381 | (add-hook 'view-mode-hook | |
e78cf8e5 SM |
1382 | (lambda () |
1383 | (setq minor-mode-overriding-map-alist | |
1384 | (delq ro-bind minor-mode-overriding-map-alist))) | |
22cd1973 | 1385 | nil t)) |
281096ed SM |
1386 | ;; add-log support |
1387 | (set (make-local-variable 'add-log-current-defun-function) | |
1388 | 'diff-current-defun) | |
1b1b5dae | 1389 | (set (make-local-variable 'add-log-buffer-file-name-function) |
8117868f DN |
1390 | (lambda () (diff-find-file-name nil 'noprompt))) |
1391 | (unless (buffer-file-name) | |
1392 | (hack-dir-local-variables-non-file-buffer))) | |
610a6418 SM |
1393 | |
1394 | ;;;###autoload | |
0b82e382 | 1395 | (define-minor-mode diff-minor-mode |
ac6c8639 CY |
1396 | "Toggle Diff minor mode. |
1397 | With a prefix argument ARG, enable Diff minor mode if ARG is | |
1398 | positive, and disable it otherwise. If called from Lisp, enable | |
1399 | the mode if ARG is omitted or nil. | |
1400 | ||
0b82e382 | 1401 | \\{diff-minor-mode-map}" |
cfc80227 | 1402 | :group 'diff-mode :lighter " Diff" |
0b82e382 | 1403 | ;; FIXME: setup font-lock |
fef8c55b | 1404 | ;; setup change hooks |
e8a1ed31 | 1405 | (if (not diff-update-on-the-fly) |
3ccdb14e | 1406 | (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t) |
fef8c55b | 1407 | (make-local-variable 'diff-unhandled-changes) |
3cec9c57 SM |
1408 | (add-hook 'after-change-functions 'diff-after-change-function nil t) |
1409 | (add-hook 'post-command-hook 'diff-post-command-hook nil t))) | |
0b82e382 | 1410 | |
e78cf8e5 | 1411 | ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
22cd1973 | 1412 | |
07875ee7 CY |
1413 | (defun diff-setup-whitespace () |
1414 | "Set up Whitespace mode variables for the current Diff mode buffer. | |
1415 | This sets `whitespace-style' and `whitespace-trailing-regexp' so | |
1416 | that Whitespace mode shows trailing whitespace problems on the | |
1417 | modified lines of the diff." | |
1418 | (set (make-local-variable 'whitespace-style) '(face trailing)) | |
1419 | (let ((style (save-excursion | |
1420 | (goto-char (point-min)) | |
6be9197e SM |
1421 | ;; FIXME: For buffers filled from async processes, this search |
1422 | ;; will simply fail because the buffer is still empty :-( | |
07875ee7 CY |
1423 | (when (re-search-forward diff-hunk-header-re nil t) |
1424 | (goto-char (match-beginning 0)) | |
1425 | (diff-hunk-style))))) | |
1426 | (set (make-local-variable 'whitespace-trailing-regexp) | |
1427 | (if (eq style 'context) | |
1428 | "^[-\+!] .*?\\([\t ]+\\)$" | |
1429 | "^[-\+!<>].*?\\([\t ]+\\)$")))) | |
1430 | ||
22cd1973 SM |
1431 | (defun diff-delete-if-empty () |
1432 | ;; An empty diff file means there's no more diffs to integrate, so we | |
1433 | ;; can just remove the file altogether. Very handy for .rej files if we | |
1434 | ;; remove hunks as we apply them. | |
1435 | (when (and buffer-file-name | |
1436 | (eq 0 (nth 7 (file-attributes buffer-file-name)))) | |
1437 | (delete-file buffer-file-name))) | |
1438 | ||
1439 | (defun diff-delete-empty-files () | |
1440 | "Arrange for empty diff files to be removed." | |
1441 | (add-hook 'after-save-hook 'diff-delete-if-empty nil t)) | |
1442 | ||
1443 | (defun diff-make-unified () | |
1444 | "Turn context diffs into unified diffs if applicable." | |
1445 | (if (save-excursion | |
1446 | (goto-char (point-min)) | |
4c174fb4 | 1447 | (and (looking-at diff-hunk-header-re) (eq (char-after) ?*))) |
22cd1973 SM |
1448 | (let ((mod (buffer-modified-p))) |
1449 | (unwind-protect | |
1450 | (diff-context->unified (point-min) (point-max)) | |
1451 | (restore-buffer-modified-p mod))))) | |
610a6418 | 1452 | |
027ac3f8 SM |
1453 | ;;; |
1454 | ;;; Misc operations that have proved useful at some point. | |
1455 | ;;; | |
1456 | ||
1457 | (defun diff-next-complex-hunk () | |
1458 | "Jump to the next \"complex\" hunk. | |
1459 | \"Complex\" is approximated by \"the hunk changes the number of lines\". | |
1460 | Only works for unified diffs." | |
1461 | (interactive) | |
1462 | (while | |
f52d2f9c SM |
1463 | (and (re-search-forward diff-hunk-header-re-unified nil t) |
1464 | (equal (match-string 2) (match-string 4))))) | |
027ac3f8 | 1465 | |
3a349573 SM |
1466 | (defun diff-sanity-check-context-hunk-half (lines) |
1467 | (let ((count lines)) | |
1468 | (while | |
1469 | (cond | |
1470 | ((and (memq (char-after) '(?\s ?! ?+ ?-)) | |
1471 | (memq (char-after (1+ (point))) '(?\s ?\t))) | |
f58e0fd5 | 1472 | (cl-decf count) t) |
3a349573 SM |
1473 | ((or (zerop count) (= count lines)) nil) |
1474 | ((memq (char-after) '(?! ?+ ?-)) | |
1475 | (if (not (and (eq (char-after (1+ (point))) ?\n) | |
1476 | (y-or-n-p "Try to auto-fix whitespace loss damage? "))) | |
1477 | (error "End of hunk ambiguously marked") | |
1478 | (forward-char 1) (insert " ") (forward-line -1) t)) | |
1479 | ((< lines 0) | |
1480 | (error "End of hunk ambiguously marked")) | |
1481 | ((not (y-or-n-p "Try to auto-fix whitespace loss and word-wrap damage? ")) | |
1482 | (error "Abort!")) | |
1483 | ((eolp) (insert " ") (forward-line -1) t) | |
1484 | (t (insert " ") (delete-region (- (point) 2) (- (point) 1)) t)) | |
1485 | (forward-line)))) | |
1486 | ||
1487 | (defun diff-sanity-check-hunk () | |
1488 | (let (;; Every modification is protected by a y-or-n-p, so it's probably | |
1489 | ;; OK to override a read-only setting. | |
1490 | (inhibit-read-only t)) | |
1491 | (save-excursion | |
1492 | (cond | |
1493 | ((not (looking-at diff-hunk-header-re)) | |
1494 | (error "Not recognizable hunk header")) | |
1495 | ||
1496 | ;; A context diff. | |
1497 | ((eq (char-after) ?*) | |
7fb6ce6e | 1498 | (if (not (looking-at "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\*\\*\\*\\*")) |
3a349573 SM |
1499 | (error "Unrecognized context diff first hunk header format") |
1500 | (forward-line 2) | |
1501 | (diff-sanity-check-context-hunk-half | |
f52d2f9c | 1502 | (if (match-end 2) |
7fb6ce6e TTN |
1503 | (1+ (- (string-to-number (match-string 2)) |
1504 | (string-to-number (match-string 1)))) | |
1505 | 1)) | |
bf6970a5 | 1506 | (if (not (looking-at diff-context-mid-hunk-header-re)) |
3a349573 SM |
1507 | (error "Unrecognized context diff second hunk header format") |
1508 | (forward-line) | |
1509 | (diff-sanity-check-context-hunk-half | |
f52d2f9c | 1510 | (if (match-end 2) |
7fb6ce6e TTN |
1511 | (1+ (- (string-to-number (match-string 2)) |
1512 | (string-to-number (match-string 1)))) | |
1513 | 1))))) | |
3a349573 SM |
1514 | |
1515 | ;; A unified diff. | |
1516 | ((eq (char-after) ?@) | |
f52d2f9c | 1517 | (if (not (looking-at diff-hunk-header-re-unified)) |
3a349573 | 1518 | (error "Unrecognized unified diff hunk header format") |
43392d12 SM |
1519 | (let ((before (string-to-number (or (match-string 2) "1"))) |
1520 | (after (string-to-number (or (match-string 4) "1")))) | |
3a349573 SM |
1521 | (forward-line) |
1522 | (while | |
f58e0fd5 SM |
1523 | (pcase (char-after) |
1524 | (?\s (cl-decf before) (cl-decf after) t) | |
80a01d97 SM |
1525 | (?- |
1526 | (if (and (looking-at diff-file-header-re) | |
1527 | (zerop before) (zerop after)) | |
1528 | ;; No need to query: this is a case where two patches | |
1529 | ;; are concatenated and only counting the lines will | |
1530 | ;; give the right result. Let's just add an empty | |
1531 | ;; line so that our code which doesn't count lines | |
1532 | ;; will not get confused. | |
1533 | (progn (save-excursion (insert "\n")) nil) | |
f58e0fd5 SM |
1534 | (cl-decf before) t)) |
1535 | (?+ (cl-decf after) t) | |
1536 | (_ | |
3a349573 | 1537 | (cond |
f52d2f9c SM |
1538 | ((and diff-valid-unified-empty-line |
1539 | ;; Not just (eolp) so we don't infloop at eob. | |
79fd6168 SM |
1540 | (eq (char-after) ?\n) |
1541 | (> before 0) (> after 0)) | |
f58e0fd5 | 1542 | (cl-decf before) (cl-decf after) t) |
3a349573 SM |
1543 | ((and (zerop before) (zerop after)) nil) |
1544 | ((or (< before 0) (< after 0)) | |
1545 | (error (if (or (zerop before) (zerop after)) | |
1546 | "End of hunk ambiguously marked" | |
1547 | "Hunk seriously messed up"))) | |
f52d2f9c | 1548 | ((not (y-or-n-p (concat "Try to auto-fix " (if (eolp) "whitespace loss" "word-wrap damage") "? "))) |
3a349573 SM |
1549 | (error "Abort!")) |
1550 | ((eolp) (insert " ") (forward-line -1) t) | |
1551 | (t (insert " ") | |
1552 | (delete-region (- (point) 2) (- (point) 1)) t)))) | |
1553 | (forward-line))))) | |
1554 | ||
1555 | ;; A plain diff. | |
1556 | (t | |
1557 | ;; TODO. | |
1558 | ))))) | |
1559 | ||
1eabc5e6 SM |
1560 | (defun diff-hunk-text (hunk destp char-offset) |
1561 | "Return the literal source text from HUNK as (TEXT . OFFSET). | |
a70b0ff8 | 1562 | If DESTP is nil, TEXT is the source, otherwise the destination text. |
1eabc5e6 SM |
1563 | CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding |
1564 | char-offset in TEXT." | |
3017133f | 1565 | (with-temp-buffer |
6e4e8a3b SM |
1566 | (insert hunk) |
1567 | (goto-char (point-min)) | |
1568 | (let ((src-pos nil) | |
1569 | (dst-pos nil) | |
1570 | (divider-pos nil) | |
1571 | (num-pfx-chars 2)) | |
1572 | ;; Set the following variables: | |
1573 | ;; SRC-POS buffer pos of the source part of the hunk or nil if none | |
1574 | ;; DST-POS buffer pos of the destination part of the hunk or nil | |
1575 | ;; DIVIDER-POS buffer pos of any divider line separating the src & dst | |
1576 | ;; NUM-PFX-CHARS number of line-prefix characters used by this format" | |
1577 | (cond ((looking-at "^@@") | |
1578 | ;; unified diff | |
1579 | (setq num-pfx-chars 1) | |
1580 | (forward-line 1) | |
1581 | (setq src-pos (point) dst-pos (point))) | |
1582 | ((looking-at "^\\*\\*") | |
1583 | ;; context diff | |
1584 | (forward-line 2) | |
1585 | (setq src-pos (point)) | |
bf6970a5 | 1586 | (re-search-forward diff-context-mid-hunk-header-re nil t) |
6e4e8a3b SM |
1587 | (forward-line 0) |
1588 | (setq divider-pos (point)) | |
1589 | (forward-line 1) | |
1590 | (setq dst-pos (point))) | |
1591 | ((looking-at "^[0-9]+a[0-9,]+$") | |
1592 | ;; normal diff, insert | |
1593 | (forward-line 1) | |
1594 | (setq dst-pos (point))) | |
1595 | ((looking-at "^[0-9,]+d[0-9]+$") | |
1596 | ;; normal diff, delete | |
1597 | (forward-line 1) | |
1598 | (setq src-pos (point))) | |
1599 | ((looking-at "^[0-9,]+c[0-9,]+$") | |
1600 | ;; normal diff, change | |
1601 | (forward-line 1) | |
1602 | (setq src-pos (point)) | |
1603 | (re-search-forward "^---$" nil t) | |
1604 | (forward-line 0) | |
1605 | (setq divider-pos (point)) | |
1606 | (forward-line 1) | |
1607 | (setq dst-pos (point))) | |
1608 | (t | |
1609 | (error "Unknown diff hunk type"))) | |
1610 | ||
1611 | (if (if destp (null dst-pos) (null src-pos)) | |
1612 | ;; Implied empty text | |
1613 | (if char-offset '("" . 0) "") | |
1614 | ||
1615 | ;; For context diffs, either side can be empty, (if there's only | |
1616 | ;; added or only removed text). We should then use the other side. | |
1617 | (cond ((equal src-pos divider-pos) (setq src-pos dst-pos)) | |
1618 | ((equal dst-pos (point-max)) (setq dst-pos src-pos))) | |
1619 | ||
1620 | (when char-offset (goto-char (+ (point-min) char-offset))) | |
1621 | ||
1622 | ;; Get rid of anything except the desired text. | |
1623 | (save-excursion | |
1624 | ;; Delete unused text region | |
1625 | (let ((keep (if destp dst-pos src-pos))) | |
1626 | (when (and divider-pos (> divider-pos keep)) | |
1627 | (delete-region divider-pos (point-max))) | |
1628 | (delete-region (point-min) keep)) | |
1629 | ;; Remove line-prefix characters, and unneeded lines (unified diffs). | |
1630 | (let ((kill-char (if destp ?- ?+))) | |
1631 | (goto-char (point-min)) | |
1632 | (while (not (eobp)) | |
1633 | (if (eq (char-after) kill-char) | |
1634 | (delete-region (point) (progn (forward-line 1) (point))) | |
1635 | (delete-char num-pfx-chars) | |
1636 | (forward-line 1))))) | |
1637 | ||
1638 | (let ((text (buffer-substring-no-properties (point-min) (point-max)))) | |
1639 | (if char-offset (cons text (- (point) (point-min))) text)))))) | |
370d860c | 1640 | |
7530b6da | 1641 | |
c0aa75f7 | 1642 | (defun diff-find-text (text) |
1eabc5e6 | 1643 | "Return the buffer position (BEG . END) of the nearest occurrence of TEXT. |
7530b6da | 1644 | If TEXT isn't found, nil is returned." |
7530b6da MB |
1645 | (let* ((orig (point)) |
1646 | (forw (and (search-forward text nil t) | |
1eabc5e6 | 1647 | (cons (match-beginning 0) (match-end 0)))) |
7530b6da MB |
1648 | (back (and (goto-char (+ orig (length text))) |
1649 | (search-backward text nil t) | |
1eabc5e6 SM |
1650 | (cons (match-beginning 0) (match-end 0))))) |
1651 | ;; Choose the closest match. | |
1652 | (if (and forw back) | |
1653 | (if (> (- (car forw) orig) (- orig (car back))) back forw) | |
1654 | (or back forw)))) | |
1655 | ||
1656 | (defun diff-find-approx-text (text) | |
1657 | "Return the buffer position (BEG . END) of the nearest occurrence of TEXT. | |
1658 | Whitespace differences are ignored." | |
1659 | (let* ((orig (point)) | |
1660 | (re (concat "^[ \t\n\f]*" | |
1661 | (mapconcat 'regexp-quote (split-string text) "[ \t\n\f]+") | |
1662 | "[ \t\n\f]*\n")) | |
1663 | (forw (and (re-search-forward re nil t) | |
1664 | (cons (match-beginning 0) (match-end 0)))) | |
1665 | (back (and (goto-char (+ orig (length text))) | |
1666 | (re-search-backward re nil t) | |
1667 | (cons (match-beginning 0) (match-end 0))))) | |
1668 | ;; Choose the closest match. | |
7530b6da | 1669 | (if (and forw back) |
1eabc5e6 | 1670 | (if (> (- (car forw) orig) (- orig (car back))) back forw) |
7530b6da MB |
1671 | (or back forw)))) |
1672 | ||
44a07c5a | 1673 | (defsubst diff-xor (a b) (if a (if (not b) a) b)) |
370d860c | 1674 | |
68035b97 | 1675 | (defun diff-find-source-location (&optional other-file reverse noprompt) |
1eabc5e6 SM |
1676 | "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED). |
1677 | BUF is the buffer corresponding to the source file. | |
1678 | LINE-OFFSET is the offset between the expected and actual positions | |
1679 | of the text of the hunk or nil if the text was not found. | |
1680 | POS is a pair (BEG . END) indicating the position of the text in the buffer. | |
1681 | SRC and DST are the two variants of text as returned by `diff-hunk-text'. | |
1682 | SRC is the variant that was found in the buffer. | |
68035b97 SM |
1683 | SWITCHED is non-nil if the patch is already applied. |
1684 | NOPROMPT, if non-nil, means not to prompt the user." | |
7b91e0f2 | 1685 | (save-excursion |
e8a1ed31 | 1686 | (let* ((other (diff-xor other-file diff-jump-to-old-file)) |
cb3e7ae0 | 1687 | (char-offset (- (point) (diff-beginning-of-hunk t))) |
3a349573 SM |
1688 | ;; Check that the hunk is well-formed. Otherwise diff-mode and |
1689 | ;; the user may disagree on what constitutes the hunk | |
1690 | ;; (e.g. because an empty line truncates the hunk mid-course), | |
1691 | ;; leading to potentially nasty surprises for the user. | |
5dadb083 | 1692 | ;; |
19a4c504 | 1693 | ;; Suppress check when NOPROMPT is non-nil (Bug#3033). |
5dadb083 | 1694 | (_ (unless noprompt (diff-sanity-check-hunk))) |
68035b97 SM |
1695 | (hunk (buffer-substring |
1696 | (point) (save-excursion (diff-end-of-hunk) (point)))) | |
370d860c SM |
1697 | (old (diff-hunk-text hunk reverse char-offset)) |
1698 | (new (diff-hunk-text hunk (not reverse) char-offset)) | |
7b91e0f2 | 1699 | ;; Find the location specification. |
d868b3bd | 1700 | (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")) |
370d860c SM |
1701 | (error "Can't find the hunk header") |
1702 | (if other (match-string 1) | |
1703 | (if (match-end 3) (match-string 3) | |
bf6970a5 SM |
1704 | (unless (re-search-forward |
1705 | diff-context-mid-hunk-header-re nil t) | |
370d860c SM |
1706 | (error "Can't find the hunk separator")) |
1707 | (match-string 1))))) | |
68035b97 SM |
1708 | (file (or (diff-find-file-name other noprompt) |
1709 | (error "Can't find the file"))) | |
370d860c | 1710 | (buf (find-file-noselect file))) |
7b91e0f2 SM |
1711 | ;; Update the user preference if he so wished. |
1712 | (when (> (prefix-numeric-value other-file) 8) | |
e8a1ed31 | 1713 | (setq diff-jump-to-old-file other)) |
d868b3bd | 1714 | (with-current-buffer buf |
88421f3e | 1715 | (goto-char (point-min)) (forward-line (1- (string-to-number line))) |
d868b3bd | 1716 | (let* ((orig-pos (point)) |
1eabc5e6 | 1717 | (switched nil) |
22cd1973 | 1718 | ;; FIXME: Check for case where both OLD and NEW are found. |
1eabc5e6 SM |
1719 | (pos (or (diff-find-text (car old)) |
1720 | (progn (setq switched t) (diff-find-text (car new))) | |
1721 | (progn (setq switched nil) | |
22cd1973 SM |
1722 | (condition-case nil |
1723 | (diff-find-approx-text (car old)) | |
1724 | (invalid-regexp nil))) ;Regex too big. | |
1eabc5e6 | 1725 | (progn (setq switched t) |
22cd1973 SM |
1726 | (condition-case nil |
1727 | (diff-find-approx-text (car new)) | |
1728 | (invalid-regexp nil))) ;Regex too big. | |
1eabc5e6 | 1729 | (progn (setq switched nil) nil)))) |
370d860c SM |
1730 | (nconc |
1731 | (list buf) | |
1eabc5e6 SM |
1732 | (if pos |
1733 | (list (count-lines orig-pos (car pos)) pos) | |
1734 | (list nil (cons orig-pos (+ orig-pos (length (car old)))))) | |
370d860c SM |
1735 | (if switched (list new old t) (list old new)))))))) |
1736 | ||
7b91e0f2 | 1737 | |
370d860c SM |
1738 | (defun diff-hunk-status-msg (line-offset reversed dry-run) |
1739 | (let ((msg (if dry-run | |
1740 | (if reversed "already applied" "not yet applied") | |
1741 | (if reversed "undone" "applied")))) | |
1742 | (message (cond ((null line-offset) "Hunk text not found") | |
1743 | ((= line-offset 0) "Hunk %s") | |
1744 | ((= line-offset 1) "Hunk %s at offset %d line") | |
1745 | (t "Hunk %s at offset %d lines")) | |
1746 | msg line-offset))) | |
1747 | ||
cb9a30c8 | 1748 | (defvar diff-apply-hunk-to-backup-file nil) |
370d860c SM |
1749 | |
1750 | (defun diff-apply-hunk (&optional reverse) | |
6e4e8a3b | 1751 | "Apply the current hunk to the source file and go to the next. |
7530b6da | 1752 | By default, the new source file is patched, but if the variable |
e8a1ed31 | 1753 | `diff-jump-to-old-file' is non-nil, then the old source file is |
7530b6da MB |
1754 | patched instead (some commands, such as `diff-goto-source' can change |
1755 | the value of this variable when given an appropriate prefix argument). | |
1756 | ||
4eaa6852 | 1757 | With a prefix argument, REVERSE the hunk." |
370d860c | 1758 | (interactive "P") |
f58e0fd5 SM |
1759 | (pcase-let ((`(,buf ,line-offset ,pos ,old ,new ,switched) |
1760 | ;; Sometimes we'd like to have the following behavior: if | |
1761 | ;; REVERSE go to the new file, otherwise go to the old. | |
1762 | ;; But that means that by default we use the old file, which is | |
1763 | ;; the opposite of the default for diff-goto-source, and is thus | |
1764 | ;; confusing. Also when you don't know about it it's | |
1765 | ;; pretty surprising. | |
1766 | ;; TODO: make it possible to ask explicitly for this behavior. | |
1767 | ;; | |
1768 | ;; This is duplicated in diff-test-hunk. | |
1769 | (diff-find-source-location nil reverse))) | |
370d860c | 1770 | (cond |
4eaa6852 MB |
1771 | ((null line-offset) |
1772 | (error "Can't find the text to patch")) | |
cb9a30c8 SM |
1773 | ((with-current-buffer buf |
1774 | (and buffer-file-name | |
1775 | (backup-file-name-p buffer-file-name) | |
1776 | (not diff-apply-hunk-to-backup-file) | |
1777 | (not (set (make-local-variable 'diff-apply-hunk-to-backup-file) | |
1778 | (yes-or-no-p (format "Really apply this hunk to %s? " | |
1779 | (file-name-nondirectory | |
1780 | buffer-file-name))))))) | |
593b4fa8 AS |
1781 | (error "%s" |
1782 | (substitute-command-keys | |
cb9a30c8 SM |
1783 | (format "Use %s\\[diff-apply-hunk] to apply it to the other file" |
1784 | (if (not reverse) "\\[universal-argument] "))))) | |
370d860c | 1785 | ((and switched |
cdbb79c1 | 1786 | ;; A reversed patch was detected, perhaps apply it in reverse. |
370d860c SM |
1787 | (not (save-window-excursion |
1788 | (pop-to-buffer buf) | |
1eabc5e6 | 1789 | (goto-char (+ (car pos) (cdr old))) |
370d860c SM |
1790 | (y-or-n-p |
1791 | (if reverse | |
1792 | "Hunk hasn't been applied yet; apply it now? " | |
1793 | "Hunk has already been applied; undo it? "))))) | |
4eaa6852 | 1794 | (message "(Nothing done)")) |
370d860c | 1795 | (t |
4eaa6852 MB |
1796 | ;; Apply the hunk |
1797 | (with-current-buffer buf | |
1eabc5e6 SM |
1798 | (goto-char (car pos)) |
1799 | (delete-region (car pos) (cdr pos)) | |
4eaa6852 MB |
1800 | (insert (car new))) |
1801 | ;; Display BUF in a window | |
1eabc5e6 | 1802 | (set-window-point (display-buffer buf) (+ (car pos) (cdr new))) |
4eaa6852 MB |
1803 | (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil) |
1804 | (when diff-advance-after-apply-hunk | |
1805 | (diff-hunk-next)))))) | |
370d860c | 1806 | |
7530b6da | 1807 | |
7530b6da MB |
1808 | (defun diff-test-hunk (&optional reverse) |
1809 | "See whether it's possible to apply the current hunk. | |
4eaa6852 | 1810 | With a prefix argument, try to REVERSE the hunk." |
7530b6da | 1811 | (interactive "P") |
f58e0fd5 SM |
1812 | (pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,switched) |
1813 | (diff-find-source-location nil reverse))) | |
1eabc5e6 | 1814 | (set-window-point (display-buffer buf) (+ (car pos) (cdr src))) |
4eaa6852 | 1815 | (diff-hunk-status-msg line-offset (diff-xor reverse switched) t))) |
370d860c | 1816 | |
7530b6da | 1817 | |
a1e2d7f7 SM |
1818 | (defalias 'diff-mouse-goto-source 'diff-goto-source) |
1819 | ||
1820 | (defun diff-goto-source (&optional other-file event) | |
7530b6da | 1821 | "Jump to the corresponding source line. |
e8a1ed31 | 1822 | `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg |
281096ed | 1823 | is given) determines whether to jump to the old or the new file. |
7530b6da | 1824 | If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument]) |
a70b0ff8 | 1825 | then `diff-jump-to-old-file' is also set, for the next invocations." |
a1e2d7f7 | 1826 | (interactive (list current-prefix-arg last-input-event)) |
a6373340 SM |
1827 | ;; When pointing at a removal line, we probably want to jump to |
1828 | ;; the old location, and else to the new (i.e. as if reverting). | |
1829 | ;; This is a convenient detail when using smerge-diff. | |
a1e2d7f7 | 1830 | (if event (posn-set-point (event-end event))) |
a6373340 | 1831 | (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]"))))) |
f58e0fd5 SM |
1832 | (pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,switched) |
1833 | (diff-find-source-location other-file rev))) | |
a6373340 | 1834 | (pop-to-buffer buf) |
1eabc5e6 | 1835 | (goto-char (+ (car pos) (cdr src))) |
55d5d717 | 1836 | (diff-hunk-status-msg line-offset (diff-xor rev switched) t)))) |
370d860c | 1837 | |
7530b6da | 1838 | |
281096ed | 1839 | (defun diff-current-defun () |
370d860c SM |
1840 | "Find the name of function at point. |
1841 | For use in `add-log-current-defun-function'." | |
ef63ea1c SM |
1842 | ;; Kill change-log-default-name so it gets recomputed each time, since |
1843 | ;; each hunk may belong to another file which may belong to another | |
1844 | ;; directory and hence have a different ChangeLog file. | |
1845 | (kill-local-variable 'change-log-default-name) | |
2f9edc8b KG |
1846 | (save-excursion |
1847 | (when (looking-at diff-hunk-header-re) | |
1848 | (forward-line 1) | |
4f815065 | 1849 | (re-search-forward "^[^ ]" nil t)) |
f58e0fd5 SM |
1850 | (pcase-let ((`(,buf ,_line-offset ,pos ,src ,dst ,switched) |
1851 | (ignore-errors ;Signals errors in place of prompting. | |
1852 | ;; Use `noprompt' since this is used in which-func-mode | |
1853 | ;; and such. | |
1854 | (diff-find-source-location nil nil 'noprompt)))) | |
68035b97 SM |
1855 | (when buf |
1856 | (beginning-of-line) | |
1857 | (or (when (memq (char-after) '(?< ?-)) | |
1858 | ;; Cursor is pointing at removed text. This could be a removed | |
1859 | ;; function, in which case, going to the source buffer will | |
1860 | ;; not help since the function is now removed. Instead, | |
1861 | ;; try to figure out the function name just from the | |
1862 | ;; code-fragment. | |
1863 | (let ((old (if switched dst src))) | |
1864 | (with-temp-buffer | |
1865 | (insert (car old)) | |
1866 | (funcall (buffer-local-value 'major-mode buf)) | |
1867 | (goto-char (+ (point-min) (cdr old))) | |
1868 | (add-log-current-defun)))) | |
1869 | (with-current-buffer buf | |
1870 | (goto-char (+ (car pos) (cdr src))) | |
1871 | (add-log-current-defun))))))) | |
027ac3f8 | 1872 | |
2659df68 SM |
1873 | (defun diff-ignore-whitespace-hunk () |
1874 | "Re-diff the current hunk, ignoring whitespace differences." | |
22cd1973 | 1875 | (interactive) |
cb3e7ae0 | 1876 | (let* ((char-offset (- (point) (diff-beginning-of-hunk t))) |
f58e0fd5 | 1877 | (opts (pcase (char-after) (?@ "-bu") (?* "-bc") (_ "-b"))) |
22cd1973 SM |
1878 | (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)") |
1879 | (error "Can't find line number")) | |
1880 | (string-to-number (match-string 1)))) | |
14cf0430 | 1881 | (inhibit-read-only t) |
22cd1973 SM |
1882 | (hunk (delete-and-extract-region |
1883 | (point) (save-excursion (diff-end-of-hunk) (point)))) | |
1884 | (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1. | |
1885 | (file1 (make-temp-file "diff1")) | |
1886 | (file2 (make-temp-file "diff2")) | |
1887 | (coding-system-for-read buffer-file-coding-system) | |
1888 | old new) | |
1889 | (unwind-protect | |
1890 | (save-excursion | |
1891 | (setq old (diff-hunk-text hunk nil char-offset)) | |
1892 | (setq new (diff-hunk-text hunk t char-offset)) | |
1893 | (write-region (concat lead (car old)) nil file1 nil 'nomessage) | |
1894 | (write-region (concat lead (car new)) nil file2 nil 'nomessage) | |
1895 | (with-temp-buffer | |
1896 | (let ((status | |
1897 | (call-process diff-command nil t nil | |
1898 | opts file1 file2))) | |
f58e0fd5 SM |
1899 | (pcase status |
1900 | (0 nil) ;Nothing to reformat. | |
22cd1973 | 1901 | (1 (goto-char (point-min)) |
f58e0fd5 SM |
1902 | ;; Remove the file-header. |
1903 | (when (re-search-forward diff-hunk-header-re nil t) | |
1904 | (delete-region (point-min) (match-beginning 0)))) | |
1905 | (_ (goto-char (point-max)) | |
22cd1973 SM |
1906 | (unless (bolp) (insert "\n")) |
1907 | (insert hunk))) | |
1908 | (setq hunk (buffer-string)) | |
1909 | (unless (memq status '(0 1)) | |
1910 | (error "Diff returned: %s" status))))) | |
1911 | ;; Whatever happens, put back some equivalent text: either the new | |
1912 | ;; one or the original one in case some error happened. | |
1913 | (insert hunk) | |
1914 | (delete-file file1) | |
1915 | (delete-file file2)))) | |
1916 | ||
be36f934 SM |
1917 | ;;; Fine change highlighting. |
1918 | ||
2659df68 SM |
1919 | (defface diff-refine-change |
1920 | '((((class color) (min-colors 88) (background light)) | |
bc1b21bb | 1921 | :background "#ffff55") |
2659df68 | 1922 | (((class color) (min-colors 88) (background dark)) |
bc1b21bb JL |
1923 | :background "#aaaa22") |
1924 | (t :inverse-video t)) | |
2659df68 | 1925 | "Face used for char-based changes shown by `diff-refine-hunk'." |
5eee3c95 | 1926 | :group 'diff-mode) |
be36f934 | 1927 | |
bc1b21bb JL |
1928 | (defface diff-refine-removed |
1929 | '((default | |
1930 | :inherit diff-refine-change) | |
1931 | (((class color) (min-colors 88) (background light)) | |
d80d54b2 | 1932 | :background "#ffbbbb") |
bc1b21bb JL |
1933 | (((class color) (min-colors 88) (background dark)) |
1934 | :background "#aa2222")) | |
1935 | "Face used for removed characters shown by `diff-refine-hunk'." | |
1936 | :group 'diff-mode | |
2a1e2476 | 1937 | :version "24.3") |
bc1b21bb JL |
1938 | |
1939 | (defface diff-refine-added | |
1940 | '((default | |
1941 | :inherit diff-refine-change) | |
1942 | (((class color) (min-colors 88) (background light)) | |
1943 | :background "#aaffaa") | |
1944 | (((class color) (min-colors 88) (background dark)) | |
1945 | :background "#22aa22")) | |
1946 | "Face used for added characters shown by `diff-refine-hunk'." | |
1947 | :group 'diff-mode | |
2a1e2476 | 1948 | :version "24.3") |
bc1b21bb | 1949 | |
2659df68 | 1950 | (defun diff-refine-preproc () |
8872469d SM |
1951 | (while (re-search-forward "^[+>]" nil t) |
1952 | ;; Remove spurious changes due to the fact that one side of the hunk is | |
1953 | ;; marked with leading + or > and the other with leading - or <. | |
1954 | ;; We used to replace all the prefix chars with " " but this only worked | |
1955 | ;; when we did char-based refinement (or when using | |
1956 | ;; smerge-refine-weight-hack) since otherwise, the `forward' motion done | |
1957 | ;; in chopup do not necessarily do the same as the ones in highlight | |
1958 | ;; since the "_" is not treated the same as " ". | |
1959 | (replace-match (cdr (assq (char-before) '((?+ . "-") (?> . "<")))))) | |
1960 | ) | |
be36f934 | 1961 | |
60f884b2 | 1962 | (declare-function smerge-refine-subst "smerge-mode" |
bc1b21bb | 1963 | (beg1 end1 beg2 end2 props-c &optional preproc props-r props-a)) |
60f884b2 | 1964 | |
2659df68 | 1965 | (defun diff-refine-hunk () |
be36f934 SM |
1966 | "Highlight changes of hunk at point at a finer granularity." |
1967 | (interactive) | |
60f884b2 | 1968 | (require 'smerge-mode) |
8872469d | 1969 | (save-excursion |
cb3e7ae0 | 1970 | (diff-beginning-of-hunk t) |
b1816a74 SM |
1971 | (let* ((start (point)) |
1972 | (style (diff-hunk-style)) ;Skips the hunk header as well. | |
8872469d | 1973 | (beg (point)) |
bc1b21bb JL |
1974 | (props-c '((diff-mode . fine) (face diff-refine-change))) |
1975 | (props-r '((diff-mode . fine) (face diff-refine-removed))) | |
1976 | (props-a '((diff-mode . fine) (face diff-refine-added))) | |
b1816a74 SM |
1977 | ;; Be careful to go back to `start' so diff-end-of-hunk gets |
1978 | ;; to read the hunk header's line info. | |
1979 | (end (progn (goto-char start) (diff-end-of-hunk) (point)))) | |
8872469d SM |
1980 | |
1981 | (remove-overlays beg end 'diff-mode 'fine) | |
1982 | ||
1983 | (goto-char beg) | |
f58e0fd5 SM |
1984 | (pcase style |
1985 | (`unified | |
a2be0357 SM |
1986 | (while (re-search-forward |
1987 | (eval-when-compile | |
1988 | (let ((no-LF-at-eol-re "\\(?:\\\\.*\n\\)?")) | |
1989 | (concat "^\\(?:-.*\n\\)+" no-LF-at-eol-re | |
1990 | "\\(\\)" | |
1991 | "\\(?:\\+.*\n\\)+" no-LF-at-eol-re))) | |
1992 | end t) | |
8872469d SM |
1993 | (smerge-refine-subst (match-beginning 0) (match-end 1) |
1994 | (match-end 1) (match-end 0) | |
bc1b21bb | 1995 | nil 'diff-refine-preproc props-r props-a))) |
f58e0fd5 | 1996 | (`context |
8872469d SM |
1997 | (let* ((middle (save-excursion (re-search-forward "^---"))) |
1998 | (other middle)) | |
1999 | (while (re-search-forward "^\\(?:!.*\n\\)+" middle t) | |
2000 | (smerge-refine-subst (match-beginning 0) (match-end 0) | |
2001 | (save-excursion | |
2002 | (goto-char other) | |
2003 | (re-search-forward "^\\(?:!.*\n\\)+" end) | |
2004 | (setq other (match-end 0)) | |
2005 | (match-beginning 0)) | |
2006 | other | |
bc1b21bb JL |
2007 | (if diff-use-changed-face props-c) |
2008 | 'diff-refine-preproc | |
2009 | (unless diff-use-changed-face props-r) | |
2010 | (unless diff-use-changed-face props-a))))) | |
f58e0fd5 | 2011 | (_ ;; Normal diffs. |
8872469d SM |
2012 | (let ((beg1 (1+ (point)))) |
2013 | (when (re-search-forward "^---.*\n" end t) | |
2014 | ;; It's a combined add&remove, so there's something to do. | |
2015 | (smerge-refine-subst beg1 (match-beginning 0) | |
2016 | (match-end 0) end | |
bc1b21bb | 2017 | nil 'diff-refine-preproc props-r props-a)))))))) |
be36f934 | 2018 | |
8b71081d CY |
2019 | (defun diff-undo (&optional arg) |
2020 | "Perform `undo', ignoring the buffer's read-only status." | |
2021 | (interactive "P") | |
2022 | (let ((inhibit-read-only t)) | |
2023 | (undo arg))) | |
be36f934 | 2024 | |
8330c175 SM |
2025 | (defun diff-add-change-log-entries-other-window () |
2026 | "Iterate through the current diff and create ChangeLog entries. | |
2027 | I.e. like `add-change-log-entry-other-window' but applied to all hunks." | |
8a72c7f8 DN |
2028 | (interactive) |
2029 | ;; XXX: Currently add-change-log-entry-other-window is only called | |
2030 | ;; once per hunk. Some hunks have multiple changes, it would be | |
2031 | ;; good to call it for each change. | |
2032 | (save-excursion | |
2033 | (goto-char (point-min)) | |
ba83908c SM |
2034 | (condition-case nil |
2035 | ;; Call add-change-log-entry-other-window for each hunk in | |
2036 | ;; the diff buffer. | |
2037 | (while (progn | |
2038 | (diff-hunk-next) | |
2039 | ;; Move to where the changes are, | |
2040 | ;; `add-change-log-entry-other-window' works better in | |
2041 | ;; that case. | |
2042 | (re-search-forward | |
2043 | (concat "\n[!+-<>]" | |
2044 | ;; If the hunk is a context hunk with an empty first | |
2045 | ;; half, recognize the "--- NNN,MMM ----" line | |
2046 | "\\(-- [0-9]+\\(,[0-9]+\\)? ----\n" | |
2047 | ;; and skip to the next non-context line. | |
2048 | "\\( .*\n\\)*[+]\\)?") | |
2049 | nil t)) | |
2050 | (save-excursion | |
2051 | ;; FIXME: this pops up windows of all the buffers. | |
2052 | (add-change-log-entry nil nil t nil t))) | |
2053 | ;; When there's no more hunks, diff-hunk-next signals an error. | |
2054 | (error nil)))) | |
8a72c7f8 | 2055 | |
9d9e48d9 CY |
2056 | (defun diff-delete-trailing-whitespace (&optional other-file) |
2057 | "Remove trailing whitespace from lines modified in this diff. | |
2058 | This edits both the current Diff mode buffer and the patched | |
2059 | source file(s). If `diff-jump-to-old-file' is non-nil, edit the | |
2060 | original (unpatched) source file instead. With a prefix argument | |
2061 | OTHER-FILE, flip the choice of which source file to edit. | |
2062 | ||
2063 | If a file referenced in the diff has no buffer and needs to be | |
2064 | fixed, visit it in a buffer." | |
2065 | (interactive "P") | |
2066 | (save-excursion | |
2067 | (goto-char (point-min)) | |
2068 | (let* ((other (diff-xor other-file diff-jump-to-old-file)) | |
2069 | (modified-buffers nil) | |
2070 | (style (save-excursion | |
2071 | (when (re-search-forward diff-hunk-header-re nil t) | |
2072 | (goto-char (match-beginning 0)) | |
2073 | (diff-hunk-style)))) | |
2074 | (regexp (concat "^[" (if other "-<" "+>") "!]" | |
2075 | (if (eq style 'context) " " "") | |
2076 | ".*?\\([ \t]+\\)$")) | |
2077 | (inhibit-read-only t) | |
2078 | (end-marker (make-marker)) | |
2079 | hunk-end) | |
2080 | ;; Move to the first hunk. | |
2081 | (re-search-forward diff-hunk-header-re nil 1) | |
2082 | (while (progn (save-excursion | |
2083 | (re-search-forward diff-hunk-header-re nil 1) | |
2084 | (setq hunk-end (point))) | |
2085 | (< (point) hunk-end)) | |
2086 | ;; For context diffs, search only in the appropriate half of | |
2087 | ;; the hunk. For other diffs, search within the entire hunk. | |
2088 | (if (not (eq style 'context)) | |
2089 | (set-marker end-marker hunk-end) | |
2090 | (let ((mid-hunk | |
2091 | (save-excursion | |
2092 | (re-search-forward diff-context-mid-hunk-header-re hunk-end) | |
2093 | (point)))) | |
2094 | (if other | |
2095 | (set-marker end-marker mid-hunk) | |
2096 | (goto-char mid-hunk) | |
2097 | (set-marker end-marker hunk-end)))) | |
2098 | (while (re-search-forward regexp end-marker t) | |
2099 | (let ((match-data (match-data))) | |
2100 | (pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,_switched) | |
2101 | (diff-find-source-location other-file))) | |
2102 | (when line-offset | |
2103 | ;; Remove the whitespace in the Diff mode buffer. | |
2104 | (set-match-data match-data) | |
2105 | (replace-match "" t t nil 1) | |
2106 | ;; Remove the whitespace in the source buffer. | |
2107 | (with-current-buffer buf | |
2108 | (save-excursion | |
2109 | (goto-char (+ (car pos) (cdr src))) | |
2110 | (beginning-of-line) | |
2111 | (when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t) | |
2112 | (unless (memq buf modified-buffers) | |
2113 | (push buf modified-buffers)) | |
2114 | (replace-match "")))))))) | |
2115 | (goto-char hunk-end)) | |
2116 | (if modified-buffers | |
2117 | (message "Deleted trailing whitespace from %s." | |
2118 | (mapconcat (lambda (buf) (concat "`" (buffer-name buf) "'")) | |
2119 | modified-buffers ", ")) | |
2120 | (message "No trailing whitespace to delete."))))) | |
2c2d9c9c | 2121 | |
610a6418 SM |
2122 | ;; provide the package |
2123 | (provide 'diff-mode) | |
2124 | ||
027ac3f8 | 2125 | ;;; Old Change Log from when diff-mode wasn't part of Emacs: |
610a6418 SM |
2126 | ;; Revision 1.11 1999/10/09 23:38:29 monnier |
2127 | ;; (diff-mode-load-hook): dropped. | |
2128 | ;; (auto-mode-alist): also catch *.diffs. | |
2129 | ;; (diff-find-file-name, diff-mode): add smarts to find the right file | |
2130 | ;; for *.rej files (that lack any file name indication). | |
2131 | ;; | |
2132 | ;; Revision 1.10 1999/09/30 15:32:11 monnier | |
2133 | ;; added support for "\ No newline at end of file". | |
2134 | ;; | |
2135 | ;; Revision 1.9 1999/09/15 00:01:13 monnier | |
2136 | ;; - added basic `compile' support. | |
2137 | ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk. | |
2138 | ;; - diff-kill-file now tries to kill the leading garbage as well. | |
2139 | ;; | |
2140 | ;; Revision 1.8 1999/09/13 21:10:09 monnier | |
2141 | ;; - don't use CL in the autoloaded code | |
2142 | ;; - accept diffs using -T | |
2143 | ;; | |
2144 | ;; Revision 1.7 1999/09/05 20:53:03 monnier | |
2145 | ;; interface to ediff-patch | |
2146 | ;; | |
2147 | ;; Revision 1.6 1999/09/01 20:55:13 monnier | |
2148 | ;; (ediff=patch-file): add bindings to call ediff-patch. | |
2149 | ;; (diff-find-file-name): taken out of diff-goto-source. | |
2150 | ;; (diff-unified->context, diff-context->unified, diff-reverse-direction, | |
2151 | ;; diff-fixup-modifs): only use the region if a prefix arg is given. | |
2152 | ;; | |
2153 | ;; Revision 1.5 1999/08/31 19:18:52 monnier | |
2154 | ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis. | |
2155 | ;; | |
2156 | ;; Revision 1.4 1999/08/31 13:01:44 monnier | |
2157 | ;; use `combine-after-change-calls' to minimize the slowdown of font-lock. | |
2158 | ;; | |
2159 | ||
2160 | ;;; diff-mode.el ends here |