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