(diff-sanity-check-hunk): Only accept an empty line if
[emacs.git] / lisp / diff-mode.el
blobc57613dff518b29097d2d475e80789e62b52fb0f
1 ;;; diff-mode.el --- a mode for viewing/editing context diffs
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: convenience patch diff
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Provides support for font-lock, outline, navigation
29 ;; commands, editing and various conversions as well as jumping
30 ;; to the corresponding source file.
32 ;; Inspired by Pavel Machek's patch-mode.el (<pavel@@atrey.karlin.mff.cuni.cz>)
33 ;; Some efforts were spent to have it somewhat compatible with XEmacs'
34 ;; diff-mode as well as with compilation-minor-mode
36 ;; Bugs:
38 ;; - Reverse doesn't work with normal diffs.
40 ;; Todo:
42 ;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
43 ;; Also allow C-c C-a to delete already-applied hunks.
45 ;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location
46 ;; of a hunk. Show then the changes between <file> and <hunk> and make it
47 ;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>.
48 ;; Or maybe just make it into a ".rej to diff3-markers converter".
49 ;; Maybe just use `wiggle' (by Neil Brown) to do it for us.
51 ;; - Refine hunk on a word-by-word basis.
52 ;;
53 ;; - in diff-apply-hunk, strip context in replace-match to better
54 ;; preserve markers and spacing.
55 ;; - Handle `diff -b' output in context->unified.
57 ;;; Code:
58 (eval-when-compile (require 'cl))
60 (defvar add-log-buffer-file-name-function)
63 (defgroup diff-mode ()
64 "Major mode for viewing/editing diffs."
65 :version "21.1"
66 :group 'tools
67 :group 'diff)
69 (defcustom diff-default-read-only nil
70 "If non-nil, `diff-mode' buffers default to being read-only."
71 :type 'boolean
72 :group 'diff-mode)
74 (defcustom diff-jump-to-old-file nil
75 "*Non-nil means `diff-goto-source' jumps to the old file.
76 Else, it jumps to the new file."
77 :type 'boolean
78 :group 'diff-mode)
80 (defcustom diff-update-on-the-fly t
81 "*Non-nil means hunk headers are kept up-to-date on-the-fly.
82 When editing a diff file, the line numbers in the hunk headers
83 need to be kept consistent with the actual diff. This can
84 either be done on the fly (but this sometimes interacts poorly with the
85 undo mechanism) or whenever the file is written (can be slow
86 when editing big diffs)."
87 :type 'boolean
88 :group 'diff-mode)
90 (defcustom diff-advance-after-apply-hunk t
91 "*Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
92 :type 'boolean
93 :group 'diff-mode)
96 (defcustom diff-mode-hook nil
97 "Run after setting up the `diff-mode' major mode."
98 :type 'hook
99 :options '(diff-delete-empty-files diff-make-unified)
100 :group 'diff-mode)
102 (defvar diff-outline-regexp
103 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
105 ;;;;
106 ;;;; keymap, menu, ...
107 ;;;;
109 (easy-mmode-defmap diff-mode-shared-map
110 '(;; From Pavel Machek's patch-mode.
111 ("n" . diff-hunk-next)
112 ("N" . diff-file-next)
113 ("p" . diff-hunk-prev)
114 ("P" . diff-file-prev)
115 ("\t" . diff-hunk-next)
116 ([backtab] . diff-hunk-prev)
117 ("k" . diff-hunk-kill)
118 ("K" . diff-file-kill)
119 ;; From compilation-minor-mode.
120 ("}" . diff-file-next)
121 ("{" . diff-file-prev)
122 ("\C-m" . diff-goto-source)
123 ([mouse-2] . diff-goto-source)
124 ;; From XEmacs' diff-mode.
125 ;; Standard M-w is useful, so don't change M-W.
126 ;;("W" . widen)
127 ;;("." . diff-goto-source) ;display-buffer
128 ;;("f" . diff-goto-source) ;find-file
129 ("o" . diff-goto-source) ;other-window
130 ;;("w" . diff-goto-source) ;other-frame
131 ;;("N" . diff-narrow)
132 ;;("h" . diff-show-header)
133 ;;("j" . diff-show-difference) ;jump to Nth diff
134 ;;("q" . diff-quit)
135 ;; Not useful if you have to metafy them.
136 ;;(" " . scroll-up)
137 ;;("\177" . scroll-down)
138 ;; Standard M-a is useful, so don't change M-A.
139 ;;("A" . diff-ediff-patch)
140 ;; Standard M-r is useful, so don't change M-r or M-R.
141 ;;("r" . diff-restrict-view)
142 ;;("R" . diff-reverse-direction)
143 ("q" . quit-window))
144 "Basic keymap for `diff-mode', bound to various prefix keys.")
146 (easy-mmode-defmap diff-mode-map
147 `(("\e" . ,diff-mode-shared-map)
148 ;; From compilation-minor-mode.
149 ("\C-c\C-c" . diff-goto-source)
150 ;; Misc operations.
151 ("\C-c\C-a" . diff-apply-hunk)
152 ("\C-c\C-e" . diff-ediff-patch)
153 ("\C-c\C-n" . diff-restrict-view)
154 ("\C-c\C-r" . diff-reverse-direction)
155 ("\C-c\C-s" . diff-split-hunk)
156 ("\C-c\C-t" . diff-test-hunk)
157 ("\C-c\C-u" . diff-context->unified)
158 ;; `d' because it duplicates the context :-( --Stef
159 ("\C-c\C-d" . diff-unified->context)
160 ("\C-c\C-w" . diff-refine-hunk)
161 ("\C-c\C-f" . next-error-follow-minor-mode))
162 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
164 (easy-menu-define diff-mode-menu diff-mode-map
165 "Menu for `diff-mode'."
166 '("Diff"
167 ["Jump to Source" diff-goto-source t]
168 ["Apply hunk" diff-apply-hunk t]
169 ["Test applying hunk" diff-test-hunk t]
170 ["Apply diff with Ediff" diff-ediff-patch t]
171 "-----"
172 ["Reverse direction" diff-reverse-direction t]
173 ["Context -> Unified" diff-context->unified t]
174 ["Unified -> Context" diff-unified->context t]
175 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
176 "-----"
177 ["Split hunk" diff-split-hunk t]
178 ["Refine hunk" diff-refine-hunk t]
179 ["Kill current hunk" diff-hunk-kill t]
180 ["Kill current file's hunks" diff-file-kill t]
181 "-----"
182 ["Previous Hunk" diff-hunk-prev t]
183 ["Next Hunk" diff-hunk-next t]
184 ["Previous File" diff-file-prev t]
185 ["Next File" diff-file-next t]
188 (defcustom diff-minor-mode-prefix "\C-c="
189 "Prefix key for `diff-minor-mode' commands."
190 :type '(choice (string "\e") (string "C-c=") string)
191 :group 'diff-mode)
193 (easy-mmode-defmap diff-minor-mode-map
194 `((,diff-minor-mode-prefix . ,diff-mode-shared-map))
195 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
198 ;;;;
199 ;;;; font-lock support
200 ;;;;
202 (defface diff-header
203 '((((class color) (min-colors 88) (background light))
204 :background "grey85")
205 (((class color) (min-colors 88) (background dark))
206 :background "grey45")
207 (((class color) (background light))
208 :foreground "blue1" :weight bold)
209 (((class color) (background dark))
210 :foreground "green" :weight bold)
211 (t :weight bold))
212 "`diff-mode' face inherited by hunk and index header faces."
213 :group 'diff-mode)
214 ;; backward-compatibility alias
215 (put 'diff-header-face 'face-alias 'diff-header)
216 (defvar diff-header-face 'diff-header)
218 (defface diff-file-header
219 '((((class color) (min-colors 88) (background light))
220 :background "grey70" :weight bold)
221 (((class color) (min-colors 88) (background dark))
222 :background "grey60" :weight bold)
223 (((class color) (background light))
224 :foreground "green" :weight bold)
225 (((class color) (background dark))
226 :foreground "cyan" :weight bold)
227 (t :weight bold)) ; :height 1.3
228 "`diff-mode' face used to highlight file header lines."
229 :group 'diff-mode)
230 ;; backward-compatibility alias
231 (put 'diff-file-header-face 'face-alias 'diff-file-header)
232 (defvar diff-file-header-face 'diff-file-header)
234 (defface diff-index
235 '((t :inherit diff-file-header))
236 "`diff-mode' face used to highlight index header lines."
237 :group 'diff-mode)
238 ;; backward-compatibility alias
239 (put 'diff-index-face 'face-alias 'diff-index)
240 (defvar diff-index-face 'diff-index)
242 (defface diff-hunk-header
243 '((t :inherit diff-header))
244 "`diff-mode' face used to highlight hunk header lines."
245 :group 'diff-mode)
246 ;; backward-compatibility alias
247 (put 'diff-hunk-header-face 'face-alias 'diff-hunk-header)
248 (defvar diff-hunk-header-face 'diff-hunk-header)
250 (defface diff-removed
251 '((t :inherit diff-changed))
252 "`diff-mode' face used to highlight removed lines."
253 :group 'diff-mode)
254 ;; backward-compatibility alias
255 (put 'diff-removed-face 'face-alias 'diff-removed)
256 (defvar diff-removed-face 'diff-removed)
258 (defface diff-added
259 '((t :inherit diff-changed))
260 "`diff-mode' face used to highlight added lines."
261 :group 'diff-mode)
262 ;; backward-compatibility alias
263 (put 'diff-added-face 'face-alias 'diff-added)
264 (defvar diff-added-face 'diff-added)
266 (defface diff-changed
267 '((((type tty pc) (class color) (background light))
268 :foreground "magenta" :weight bold :slant italic)
269 (((type tty pc) (class color) (background dark))
270 :foreground "yellow" :weight bold :slant italic))
271 "`diff-mode' face used to highlight changed lines."
272 :group 'diff-mode)
273 ;; backward-compatibility alias
274 (put 'diff-changed-face 'face-alias 'diff-changed)
275 (defvar diff-changed-face 'diff-changed)
277 (defface diff-indicator-removed
278 '((t :inherit diff-removed))
279 "`diff-mode' face used to highlight indicator of removed lines (-, <)."
280 :group 'diff-mode
281 :version "22.1")
282 (defvar diff-indicator-removed-face 'diff-indicator-removed)
284 (defface diff-indicator-added
285 '((t :inherit diff-added))
286 "`diff-mode' face used to highlight indicator of added lines (+, >)."
287 :group 'diff-mode
288 :version "22.1")
289 (defvar diff-indicator-added-face 'diff-indicator-added)
291 (defface diff-indicator-changed
292 '((t :inherit diff-changed))
293 "`diff-mode' face used to highlight indicator of changed lines."
294 :group 'diff-mode
295 :version "22.1")
296 (defvar diff-indicator-changed-face 'diff-indicator-changed)
298 (defface diff-function
299 '((t :inherit diff-header))
300 "`diff-mode' face used to highlight function names produced by \"diff -p\"."
301 :group 'diff-mode)
302 ;; backward-compatibility alias
303 (put 'diff-function-face 'face-alias 'diff-function)
304 (defvar diff-function-face 'diff-function)
306 (defface diff-context
307 '((((class color grayscale) (min-colors 88)) :inherit shadow))
308 "`diff-mode' face used to highlight context and other side-information."
309 :group 'diff-mode)
310 ;; backward-compatibility alias
311 (put 'diff-context-face 'face-alias 'diff-context)
312 (defvar diff-context-face 'diff-context)
314 (defface diff-nonexistent
315 '((t :inherit diff-file-header))
316 "`diff-mode' face used to highlight nonexistent files in recursive diffs."
317 :group 'diff-mode)
318 ;; backward-compatibility alias
319 (put 'diff-nonexistent-face 'face-alias 'diff-nonexistent)
320 (defvar diff-nonexistent-face 'diff-nonexistent)
322 (defconst diff-yank-handler '(diff-yank-function))
323 (defun diff-yank-function (text)
324 ;; FIXME: the yank-handler is now called separately on each piece of text
325 ;; with a yank-handler property, so the next-single-property-change call
326 ;; below will always return nil :-( --stef
327 (let ((mixed (next-single-property-change 0 'yank-handler text))
328 (start (point)))
329 ;; First insert the text.
330 (insert text)
331 ;; If the text does not include any diff markers and if we're not
332 ;; yanking back into a diff-mode buffer, get rid of the prefixes.
333 (unless (or mixed (derived-mode-p 'diff-mode))
334 (undo-boundary) ; Just in case the user wanted the prefixes.
335 (let ((re (save-excursion
336 (if (re-search-backward "^[><!][ \t]" start t)
337 (if (eq (char-after) ?!)
338 "^[!+- ][ \t]" "^[<>][ \t]")
339 "^[ <>!+-]"))))
340 (save-excursion
341 (while (re-search-backward re start t)
342 (replace-match "" t t)))))))
344 (defconst diff-hunk-header-re-unified
345 "^@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@")
347 (defvar diff-font-lock-keywords
348 `((,(concat "\\(" diff-hunk-header-re-unified "\\)\\(.*\\)$")
349 (1 diff-hunk-header-face) (6 diff-function-face))
350 ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
351 (1 diff-hunk-header-face) (2 diff-function-face))
352 ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context
353 ("^--- .+ ----$" . diff-hunk-header-face) ;context
354 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face) ;normal
355 ("^---$" . diff-hunk-header-face) ;normal
356 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\(\\S-+\\)\\(.*[^*-]\\)?\n"
357 (0 diff-header-face) (2 diff-file-header-face prepend))
358 ("^\\([-<]\\)\\(.*\n\\)"
359 (1 diff-indicator-removed-face) (2 diff-removed-face))
360 ("^\\([+>]\\)\\(.*\n\\)"
361 (1 diff-indicator-added-face) (2 diff-added-face))
362 ("^\\(!\\)\\(.*\n\\)"
363 (1 diff-indicator-changed-face) (2 diff-changed-face))
364 ("^Index: \\(.+\\).*\n"
365 (0 diff-header-face) (1 diff-index-face prepend))
366 ("^Only in .*\n" . diff-nonexistent-face)
367 ("^\\(#\\)\\(.*\\)"
368 (1 font-lock-comment-delimiter-face)
369 (2 font-lock-comment-face))
370 ("^[^-=+*!<>#].*\n" (0 diff-context-face))))
372 (defconst diff-font-lock-defaults
373 '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
375 (defvar diff-imenu-generic-expression
376 ;; Prefer second name as first is most likely to be a backup or
377 ;; version-control name. The [\t\n] at the end of the unidiff pattern
378 ;; catches Debian source diff files (which lack the trailing date).
379 '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
380 (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
382 ;;;;
383 ;;;; Movement
384 ;;;;
386 (defvar diff-valid-unified-empty-line t
387 "If non-nil, empty lines are valid in unified diffs.
388 Some versions of diff replace all-blank context lines in unified format with
389 empty lines. This makes the format less robust, but is tolerated.
390 See http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01990.html")
392 (defconst diff-hunk-header-re
393 (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$"))
394 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1)))
395 (defvar diff-narrowed-to nil)
397 (defun diff-end-of-hunk (&optional style donttrustheader)
398 (let (end)
399 (when (looking-at diff-hunk-header-re)
400 (unless style
401 ;; Especially important for unified (because headers are ambiguous).
402 (setq style (cdr (assq (char-after) '((?@ . unified) (?* . context))))))
403 (goto-char (match-end 0))
404 (when (and (not donttrustheader) (match-end 2))
405 (save-excursion
406 (re-search-forward (if diff-valid-unified-empty-line
407 "^[- \n]" "^[- ]")
408 nil t
409 (string-to-number (match-string 2)))
410 (setq end (line-beginning-position 2)))))
411 ;; We may have a first evaluation of `end' thanks to the hunk header.
412 (unless end
413 (setq end (and (re-search-forward
414 (case style
415 (unified (concat (if diff-valid-unified-empty-line
416 "^[^-+# \\\n]\\|" "^[^-+# \\]\\|")
417 ;; A `unified' header is ambiguous.
418 diff-file-header-re))
419 (context "^[^-+#! \\]")
420 (normal "^[^<>#\\]")
421 (t "^[^-+#!<> \\]"))
422 nil t)
423 (match-beginning 0)))
424 (when diff-valid-unified-empty-line
425 ;; While empty lines may be valid inside hunks, they are also likely
426 ;; to be unrelated to the hunk.
427 (goto-char (or end (point-max)))
428 (while (eq ?\n (char-before (1- (point))))
429 (forward-char -1)
430 (setq end (point)))))
431 ;; The return value is used by easy-mmode-define-navigation.
432 (goto-char (or end (point-max)))))
434 (defun diff-beginning-of-hunk (&optional try-harder)
435 "Move back to beginning of hunk.
436 If TRY-HARDER is non-nil, try to cater to the case where we're not in a hunk
437 but in the file header instead, in which case move forward to the first hunk."
438 (beginning-of-line)
439 (unless (looking-at diff-hunk-header-re)
440 (forward-line 1)
441 (condition-case ()
442 (re-search-backward diff-hunk-header-re)
443 (error
444 (if (not try-harder)
445 (error "Can't find the beginning of the hunk")
446 (diff-beginning-of-file-and-junk)
447 (diff-hunk-next))))))
449 (defun diff-beginning-of-file ()
450 (beginning-of-line)
451 (unless (looking-at diff-file-header-re)
452 (forward-line 2)
453 (condition-case ()
454 (re-search-backward diff-file-header-re)
455 (error (error "Can't find the beginning of the file")))))
457 (defun diff-end-of-file ()
458 (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
459 (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re)
460 nil 'move)
461 (if (match-beginning 1)
462 (goto-char (match-beginning 1))
463 (beginning-of-line)))
465 ;; Define diff-{hunk,file}-{prev,next}
466 (easy-mmode-define-navigation
467 diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view)
468 (easy-mmode-define-navigation
469 diff-file diff-file-header-re "file" diff-end-of-hunk)
471 (defun diff-restrict-view (&optional arg)
472 "Restrict the view to the current hunk.
473 If the prefix ARG is given, restrict the view to the current file instead."
474 (interactive "P")
475 (save-excursion
476 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk 'try-harder))
477 (narrow-to-region (point)
478 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
479 (point)))
480 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
483 (defun diff-hunk-kill ()
484 "Kill current hunk."
485 (interactive)
486 (diff-beginning-of-hunk)
487 (let* ((start (point))
488 (nexthunk (when (re-search-forward diff-hunk-header-re nil t)
489 (match-beginning 0)))
490 (firsthunk (ignore-errors
491 (goto-char start)
492 (diff-beginning-of-file) (diff-hunk-next) (point)))
493 (nextfile (ignore-errors (diff-file-next) (point)))
494 (inhibit-read-only t))
495 (goto-char start)
496 (if (and firsthunk (= firsthunk start)
497 (or (null nexthunk)
498 (and nextfile (> nexthunk nextfile))))
499 ;; It's the only hunk for this file, so kill the file.
500 (diff-file-kill)
501 (diff-end-of-hunk)
502 (kill-region start (point)))))
504 (defconst diff-file-junk-re "diff \\|index ") ; "index " is output by git-diff.
506 (defun diff-beginning-of-file-and-junk ()
507 "Go to the beginning of file-related diff-info.
508 This is like `diff-beginning-of-file' except it tries to skip back over leading
509 data such as \"Index: ...\" and such."
510 (let ((orig (point))
511 ;; Skip forward over what might be "leading junk" so as to get
512 ;; closer to the actual diff.
513 (_ (progn (beginning-of-line)
514 (while (looking-at diff-file-junk-re)
515 (forward-line 1))))
516 (start (point))
517 (file (condition-case err (progn (diff-beginning-of-file) (point))
518 (error err)))
519 ;; prevhunk is one of the limits.
520 (prevhunk (save-excursion (ignore-errors (diff-hunk-prev) (point))))
521 err)
522 (when (consp file)
523 ;; Presumably, we started before the file header, in the leading junk.
524 (setq err file)
525 (diff-file-next)
526 (setq file (point)))
527 (let ((index (save-excursion
528 (forward-line 1) ;In case we're looking at "Index:".
529 (re-search-backward "^Index: " prevhunk t))))
530 (when index (setq file index))
531 (if (<= file start)
532 (progn
533 (goto-char file)
534 ;; Now skip backward over the leading junk we may have before the
535 ;; diff itself.
536 (while (save-excursion
537 (and (zerop (forward-line -1))
538 (looking-at diff-file-junk-re)))
539 (forward-line -1)))
540 ;; File starts *after* the starting point: we really weren't in
541 ;; a file diff but elsewhere.
542 (goto-char orig)
543 (signal (car err) (cdr err))))))
545 (defun diff-file-kill ()
546 "Kill current file's hunks."
547 (interactive)
548 (let ((orig (point))
549 (start (progn (diff-beginning-of-file-and-junk) (point)))
550 (inhibit-read-only t))
551 (diff-end-of-file)
552 (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
553 (if (> orig (point)) (error "Not inside a file diff"))
554 (kill-region start (point))))
556 (defun diff-kill-junk ()
557 "Kill spurious empty diffs."
558 (interactive)
559 (save-excursion
560 (let ((inhibit-read-only t))
561 (goto-char (point-min))
562 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
563 "\\([^-+!* <>].*\n\\)*?"
564 "\\(\\(Index:\\) \\|"
565 diff-file-header-re "\\)")
566 nil t)
567 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
568 (match-beginning 3))
569 (beginning-of-line)))))
571 (defun diff-count-matches (re start end)
572 (save-excursion
573 (let ((n 0))
574 (goto-char start)
575 (while (re-search-forward re end t) (incf n))
576 n)))
578 (defun diff-split-hunk ()
579 "Split the current (unified diff) hunk at point into two hunks."
580 (interactive)
581 (beginning-of-line)
582 (let ((pos (point))
583 (start (progn (diff-beginning-of-hunk) (point))))
584 (unless (looking-at diff-hunk-header-re-unified)
585 (error "diff-split-hunk only works on unified context diffs"))
586 (forward-line 1)
587 (let* ((start1 (string-to-number (match-string 1)))
588 (start2 (string-to-number (match-string 3)))
589 (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
590 (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos)))
591 (inhibit-read-only t))
592 (goto-char pos)
593 ;; Hopefully the after-change-function will not screw us over.
594 (insert "@@ -" (number-to-string newstart1) ",1 +"
595 (number-to-string newstart2) ",1 @@\n")
596 ;; Fix the original hunk-header.
597 (diff-fixup-modifs start pos))))
600 ;;;;
601 ;;;; jump to other buffers
602 ;;;;
604 (defvar diff-remembered-files-alist nil)
606 (defun diff-filename-drop-dir (file)
607 (when (string-match "/" file) (substring file (match-end 0))))
609 (defun diff-merge-strings (ancestor from to)
610 "Merge the diff between ANCESTOR and FROM into TO.
611 Returns the merged string if successful or nil otherwise.
612 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
613 If ANCESTOR = FROM, returns TO.
614 If ANCESTOR = TO, returns FROM.
615 The heuristic is simplistic and only really works for cases
616 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
617 ;; Ideally, we want:
618 ;; AMB ANB CMD -> CND
619 ;; but that's ambiguous if `foo' or `bar' is empty:
620 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
621 (let ((str (concat ancestor "\n" from "\n" to)))
622 (when (and (string-match (concat
623 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
624 "\\1\\(.*\\)\\3\n"
625 "\\(.*\\(\\2\\).*\\)\\'") str)
626 (equal to (match-string 5 str)))
627 (concat (substring str (match-beginning 5) (match-beginning 6))
628 (match-string 4 str)
629 (substring str (match-end 6) (match-end 5))))))
631 (defun diff-tell-file-name (old name)
632 "Tell Emacs where the find the source file of the current hunk.
633 If the OLD prefix arg is passed, tell the file NAME of the old file."
634 (interactive
635 (let* ((old current-prefix-arg)
636 (fs (diff-hunk-file-names current-prefix-arg)))
637 (unless fs (error "No file name to look for"))
638 (list old (read-file-name (format "File for %s: " (car fs))
639 nil (diff-find-file-name old) t))))
640 (let ((fs (diff-hunk-file-names old)))
641 (unless fs (error "No file name to look for"))
642 (push (cons fs name) diff-remembered-files-alist)))
644 (defun diff-hunk-file-names (&optional old)
645 "Give the list of file names textually mentioned for the current hunk."
646 (save-excursion
647 (unless (looking-at diff-file-header-re)
648 (or (ignore-errors (diff-beginning-of-file))
649 (re-search-forward diff-file-header-re nil t)))
650 (let ((limit (save-excursion
651 (condition-case ()
652 (progn (diff-hunk-prev) (point))
653 (error (point-min)))))
654 (header-files
655 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
656 (list (if old (match-string 1) (match-string 3))
657 (if old (match-string 3) (match-string 1)))
658 (forward-line 1) nil)))
659 (delq nil
660 (append
661 (when (and (not old)
662 (save-excursion
663 (re-search-backward "^Index: \\(.+\\)" limit t)))
664 (list (match-string 1)))
665 header-files
666 (when (re-search-backward
667 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?"
668 nil t)
669 (list (if old (match-string 2) (match-string 4))
670 (if old (match-string 4) (match-string 2)))))))))
672 (defun diff-find-file-name (&optional old prefix)
673 "Return the file corresponding to the current patch.
674 Non-nil OLD means that we want the old file.
675 PREFIX is only used internally: don't use it."
676 (save-excursion
677 (unless (looking-at diff-file-header-re)
678 (or (ignore-errors (diff-beginning-of-file))
679 (re-search-forward diff-file-header-re nil t)))
680 (let ((fs (diff-hunk-file-names old)))
681 (if prefix (setq fs (mapcar (lambda (f) (concat prefix f)) fs)))
683 ;; use any previously used preference
684 (cdr (assoc fs diff-remembered-files-alist))
685 ;; try to be clever and use previous choices as an inspiration
686 (dolist (rf diff-remembered-files-alist)
687 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
688 (if (and newfile (file-exists-p newfile)) (return newfile))))
689 ;; look for each file in turn. If none found, try again but
690 ;; ignoring the first level of directory, ...
691 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
692 (file nil nil))
693 ((or (null files)
694 (setq file (do* ((files files (cdr files))
695 (file (car files) (car files)))
696 ;; Use file-regular-p to avoid
697 ;; /dev/null, directories, etc.
698 ((or (null file) (file-regular-p file))
699 file))))
700 file))
701 ;; <foo>.rej patches implicitly apply to <foo>
702 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
703 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
704 (when (file-exists-p file) file)))
705 ;; If we haven't found the file, maybe it's because we haven't paid
706 ;; attention to the PCL-CVS hint.
707 (and (not prefix)
708 (boundp 'cvs-pcl-cvs-dirchange-re)
709 (save-excursion
710 (re-search-backward cvs-pcl-cvs-dirchange-re nil t))
711 (diff-find-file-name old (match-string 1)))
712 ;; if all else fails, ask the user
713 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
714 nil (first fs) t (first fs))))
715 (set (make-local-variable 'diff-remembered-files-alist)
716 (cons (cons fs file) diff-remembered-files-alist))
717 file)))))
720 (defun diff-ediff-patch ()
721 "Call `ediff-patch-file' on the current buffer."
722 (interactive)
723 (condition-case err
724 (ediff-patch-file nil (current-buffer))
725 (wrong-number-of-arguments (ediff-patch-file))))
727 ;;;;
728 ;;;; Conversion functions
729 ;;;;
731 ;;(defvar diff-inhibit-after-change nil
732 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
734 (defun diff-unified->context (start end)
735 "Convert unified diffs to context diffs.
736 START and END are either taken from the region (if a prefix arg is given) or
737 else cover the whole buffer."
738 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
739 (list (region-beginning) (region-end))
740 (list (point-min) (point-max))))
741 (unless (markerp end) (setq end (copy-marker end t)))
742 (let (;;(diff-inhibit-after-change t)
743 (inhibit-read-only t))
744 (save-excursion
745 (goto-char start)
746 (while (and (re-search-forward
747 (concat "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|"
748 diff-hunk-header-re-unified ".*\\)$")
749 nil t)
750 (< (point) end))
751 (combine-after-change-calls
752 (if (match-beginning 2)
753 ;; we matched a file header
754 (progn
755 ;; use reverse order to make sure the indices are kept valid
756 (replace-match "---" t t nil 3)
757 (replace-match "***" t t nil 2))
758 ;; we matched a hunk header
759 (let ((line1 (match-string 4))
760 (lines1 (match-string 5))
761 (line2 (match-string 6))
762 (lines2 (match-string 7)))
763 (replace-match
764 (concat "***************\n*** " line1 ","
765 (number-to-string (+ (string-to-number line1)
766 (string-to-number lines1)
767 -1)) " ****"))
768 (save-restriction
769 (narrow-to-region (line-beginning-position 2)
770 ;; Call diff-end-of-hunk from just before
771 ;; the hunk header so it can use the hunk
772 ;; header info.
773 (progn (diff-end-of-hunk 'unified) (point)))
774 (let ((hunk (buffer-string)))
775 (goto-char (point-min))
776 (if (not (save-excursion (re-search-forward "^-" nil t)))
777 (delete-region (point) (point-max))
778 (goto-char (point-max))
779 (let ((modif nil) last-pt)
780 (while (progn (setq last-pt (point))
781 (= (forward-line -1) 0))
782 (case (char-after)
783 (?\s (insert " ") (setq modif nil) (backward-char 1))
784 (?+ (delete-region (point) last-pt) (setq modif t))
785 (?- (if (not modif)
786 (progn (forward-char 1)
787 (insert " "))
788 (delete-char 1)
789 (insert "! "))
790 (backward-char 2))
791 (?\\ (when (save-excursion (forward-line -1)
792 (= (char-after) ?+))
793 (delete-region (point) last-pt) (setq modif t)))
794 ;; diff-valid-unified-empty-line.
795 (?\n (insert " ") (setq modif nil) (backward-char 2))
796 (t (setq modif nil))))))
797 (goto-char (point-max))
798 (save-excursion
799 (insert "--- " line2 ","
800 (number-to-string (+ (string-to-number line2)
801 (string-to-number lines2)
802 -1)) " ----\n" hunk))
803 ;;(goto-char (point-min))
804 (forward-line 1)
805 (if (not (save-excursion (re-search-forward "^+" nil t)))
806 (delete-region (point) (point-max))
807 (let ((modif nil) (delete nil))
808 (while (not (eobp))
809 (case (char-after)
810 (?\s (insert " ") (setq modif nil) (backward-char 1))
811 (?- (setq delete t) (setq modif t))
812 (?+ (if (not modif)
813 (progn (forward-char 1)
814 (insert " "))
815 (delete-char 1)
816 (insert "! "))
817 (backward-char 2))
818 (?\\ (when (save-excursion (forward-line 1)
819 (not (eobp)))
820 (setq delete t) (setq modif t)))
821 ;; diff-valid-unified-empty-line.
822 (?\n (insert " ") (setq modif nil) (backward-char 2))
823 (t (setq modif nil)))
824 (let ((last-pt (point)))
825 (forward-line 1)
826 (when delete
827 (delete-region last-pt (point))
828 (setq delete nil)))))))))))))))
830 (defun diff-context->unified (start end &optional to-context)
831 "Convert context diffs to unified diffs.
832 START and END are either taken from the region
833 \(when it is highlighted) or else cover the whole buffer.
834 With a prefix argument, convert unified format to context format."
835 (interactive (if (and transient-mark-mode mark-active)
836 (list (region-beginning) (region-end) current-prefix-arg)
837 (list (point-min) (point-max) current-prefix-arg)))
838 (if to-context
839 (diff-unified->context start end)
840 (unless (markerp end) (setq end (copy-marker end t)))
841 (let ( ;;(diff-inhibit-after-change t)
842 (inhibit-read-only t))
843 (save-excursion
844 (goto-char start)
845 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
846 (< (point) end))
847 (combine-after-change-calls
848 (if (match-beginning 2)
849 ;; we matched a file header
850 (progn
851 ;; use reverse order to make sure the indices are kept valid
852 (replace-match "+++" t t nil 3)
853 (replace-match "---" t t nil 2))
854 ;; we matched a hunk header
855 (let ((line1s (match-string 4))
856 (line1e (match-string 5))
857 (pt1 (match-beginning 0)))
858 (replace-match "")
859 (unless (re-search-forward
860 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
861 (error "Can't find matching `--- n1,n2 ----' line"))
862 (let ((line2s (match-string 1))
863 (line2e (match-string 2))
864 (pt2 (progn
865 (delete-region (progn (beginning-of-line) (point))
866 (progn (forward-line 1) (point)))
867 (point-marker))))
868 (goto-char pt1)
869 (forward-line 1)
870 (while (< (point) pt2)
871 (case (char-after)
872 ((?! ?-) (delete-char 2) (insert "-") (forward-line 1))
873 (?\s ;merge with the other half of the chunk
874 (let* ((endline2
875 (save-excursion
876 (goto-char pt2) (forward-line 1) (point)))
877 (c (char-after pt2)))
878 (case c
879 ((?! ?+)
880 (insert "+"
881 (prog1 (buffer-substring (+ pt2 2) endline2)
882 (delete-region pt2 endline2))))
883 (?\s ;FIXME: check consistency
884 (delete-region pt2 endline2)
885 (delete-char 1)
886 (forward-line 1))
887 (?\\ (forward-line 1))
888 (t (delete-char 1) (forward-line 1)))))
889 (t (forward-line 1))))
890 (while (looking-at "[+! ] ")
891 (if (/= (char-after) ?!) (forward-char 1)
892 (delete-char 1) (insert "+"))
893 (delete-char 1) (forward-line 1))
894 (save-excursion
895 (goto-char pt1)
896 (insert "@@ -" line1s ","
897 (number-to-string (- (string-to-number line1e)
898 (string-to-number line1s)
899 -1))
900 " +" line2s ","
901 (number-to-string (- (string-to-number line2e)
902 (string-to-number line2s)
903 -1)) " @@")))))))))))
905 (defun diff-reverse-direction (start end)
906 "Reverse the direction of the diffs.
907 START and END are either taken from the region (if a prefix arg is given) or
908 else cover the whole buffer."
909 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
910 (list (region-beginning) (region-end))
911 (list (point-min) (point-max))))
912 (unless (markerp end) (setq end (copy-marker end t)))
913 (let (;;(diff-inhibit-after-change t)
914 (inhibit-read-only t))
915 (save-excursion
916 (goto-char start)
917 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
918 (< (point) end))
919 (combine-after-change-calls
920 (cond
921 ;; a file header
922 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
923 ;; a context-diff hunk header
924 ((match-beginning 6)
925 (let ((pt-lines1 (match-beginning 6))
926 (lines1 (match-string 6)))
927 (replace-match "" nil nil nil 6)
928 (forward-line 1)
929 (let ((half1s (point)))
930 (while (looking-at "[-! \\][ \t]\\|#")
931 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
932 (forward-line 1))
933 (let ((half1 (delete-and-extract-region half1s (point))))
934 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
935 (insert half1)
936 (error "Can't find matching `--- n1,n2 ----' line"))
937 (let ((str1 (match-string 1)))
938 (replace-match lines1 nil nil nil 1)
939 (forward-line 1)
940 (let ((half2s (point)))
941 (while (looking-at "[!+ \\][ \t]\\|#")
942 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
943 (forward-line 1))
944 (let ((half2 (delete-and-extract-region half2s (point))))
945 (insert (or half1 ""))
946 (goto-char half1s)
947 (insert (or half2 ""))))
948 (goto-char pt-lines1)
949 (insert str1))))))
950 ;; a unified-diff hunk header
951 ((match-beginning 7)
952 (replace-match "@@ -\\8 +\\7 @@" nil)
953 (forward-line 1)
954 (let ((c (char-after)) first last)
955 (while (case (setq c (char-after))
956 (?- (setq first (or first (point)))
957 (delete-char 1) (insert "+") t)
958 (?+ (setq last (or last (point)))
959 (delete-char 1) (insert "-") t)
960 ((?\\ ?#) t)
961 (t (when (and first last (< first last))
962 (insert (delete-and-extract-region first last)))
963 (setq first nil last nil)
964 (memq c (if diff-valid-unified-empty-line
965 '(?\s ?\n) '(?\s)))))
966 (forward-line 1))))))))))
968 (defun diff-fixup-modifs (start end)
969 "Fixup the hunk headers (in case the buffer was modified).
970 START and END are either taken from the region (if a prefix arg is given) or
971 else cover the whole buffer."
972 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
973 (list (region-beginning) (region-end))
974 (list (point-min) (point-max))))
975 (let ((inhibit-read-only t))
976 (save-excursion
977 (goto-char end) (diff-end-of-hunk nil 'donttrustheader)
978 (let ((plus 0) (minus 0) (space 0) (bang 0))
979 (while (and (= (forward-line -1) 0) (<= start (point)))
980 (if (not (looking-at
981 (concat diff-hunk-header-re-unified
982 "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$"
983 "\\|--- .+\n\\+\\+\\+ ")))
984 (case (char-after)
985 (?\s (incf space))
986 (?+ (incf plus))
987 (?- (incf minus))
988 (?! (incf bang))
989 ((?\\ ?#) nil)
990 (t (setq space 0 plus 0 minus 0 bang 0)))
991 (cond
992 ((looking-at diff-hunk-header-re-unified)
993 (let* ((old1 (match-string 2))
994 (old2 (match-string 4))
995 (new1 (number-to-string (+ space minus)))
996 (new2 (number-to-string (+ space plus))))
997 (unless (string= new2 old2) (replace-match new2 t t nil 4))
998 (unless (string= new1 old1) (replace-match new1 t t nil 2))))
999 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
1000 (when (> (+ space bang plus) 0)
1001 (let* ((old1 (match-string 1))
1002 (old2 (match-string 2))
1003 (new (number-to-string
1004 (+ space bang plus -1 (string-to-number old1)))))
1005 (unless (string= new old2) (replace-match new t t nil 2)))))
1006 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
1007 (when (> (+ space bang minus) 0)
1008 (let* ((old (match-string 1))
1009 (new (format
1010 (concat "%0" (number-to-string (length old)) "d")
1011 (+ space bang minus -1 (string-to-number old)))))
1012 (unless (string= new old) (replace-match new t t nil 2))))))
1013 (setq space 0 plus 0 minus 0 bang 0)))))))
1015 ;;;;
1016 ;;;; Hooks
1017 ;;;;
1019 (defun diff-write-contents-hooks ()
1020 "Fixup hunk headers if necessary."
1021 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
1022 nil)
1024 ;; It turns out that making changes in the buffer from within an
1025 ;; *-change-function is asking for trouble, whereas making them
1026 ;; from a post-command-hook doesn't pose much problems
1027 (defvar diff-unhandled-changes nil)
1028 (defun diff-after-change-function (beg end len)
1029 "Remember to fixup the hunk header.
1030 See `after-change-functions' for the meaning of BEG, END and LEN."
1031 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
1032 ;; incorrect, but it turns out that inhibit-read-only is normally not set
1033 ;; inside editing commands, while it tends to be set when the buffer gets
1034 ;; updated by an async process or by a conversion function, both of which
1035 ;; would rather not be uselessly slowed down by this hook.
1036 (when (and (not undo-in-progress) (not inhibit-read-only))
1037 (if diff-unhandled-changes
1038 (setq diff-unhandled-changes
1039 (cons (min beg (car diff-unhandled-changes))
1040 (max end (cdr diff-unhandled-changes))))
1041 (setq diff-unhandled-changes (cons beg end)))))
1043 (defun diff-post-command-hook ()
1044 "Fixup hunk headers if necessary."
1045 (when (consp diff-unhandled-changes)
1046 (ignore-errors
1047 (save-excursion
1048 (goto-char (car diff-unhandled-changes))
1049 ;; Maybe we've cut the end of the hunk before point.
1050 (if (and (bolp) (not (bobp))) (backward-char 1))
1051 ;; We used to fixup modifs on all the changes, but it turns out
1052 ;; that it's safer not to do it on big changes, for example
1053 ;; when yanking a big diff, since we might then screw up perfectly
1054 ;; correct values. -stef
1055 ;; (unless (ignore-errors
1056 ;; (diff-beginning-of-hunk)
1057 ;; (save-excursion
1058 ;; (diff-end-of-hunk)
1059 ;; (> (point) (car diff-unhandled-changes))))
1060 ;; (goto-char (car diff-unhandled-changes))
1061 ;; (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
1062 ;; (diff-beginning-of-hunk))
1063 ;; (diff-fixup-modifs (point) (cdr diff-unhandled-changes))
1064 (diff-beginning-of-hunk)
1065 (when (save-excursion
1066 (diff-end-of-hunk nil 'donttrustheader)
1067 (>= (point) (cdr diff-unhandled-changes)))
1068 (diff-fixup-modifs (point) (cdr diff-unhandled-changes)))))
1069 (setq diff-unhandled-changes nil)))
1071 (defun diff-next-error (arg reset)
1072 ;; Select a window that displays the current buffer so that point
1073 ;; movements are reflected in that window. Otherwise, the user might
1074 ;; never see the hunk corresponding to the source she's jumping to.
1075 (pop-to-buffer (current-buffer))
1076 (if reset (goto-char (point-min)))
1077 (diff-hunk-next arg)
1078 (diff-goto-source))
1080 ;;;###autoload
1081 (define-derived-mode diff-mode fundamental-mode "Diff"
1082 "Major mode for viewing/editing context diffs.
1083 Supports unified and context diffs as well as (to a lesser extent)
1084 normal diffs.
1086 When the buffer is read-only, the ESC prefix is not necessary.
1087 If you edit the buffer manually, diff-mode will try to update the hunk
1088 headers for you on-the-fly.
1090 You can also switch between context diff and unified diff with \\[diff-context->unified],
1091 or vice versa with \\[diff-unified->context] and you can also reverse the direction of
1092 a diff with \\[diff-reverse-direction].
1094 \\{diff-mode-map}"
1096 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
1097 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
1098 (set (make-local-variable 'imenu-generic-expression)
1099 diff-imenu-generic-expression)
1100 ;; These are not perfect. They would be better done separately for
1101 ;; context diffs and unidiffs.
1102 ;; (set (make-local-variable 'paragraph-start)
1103 ;; (concat "@@ " ; unidiff hunk
1104 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
1105 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
1106 ;; ; start (first or second line)
1107 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
1108 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
1109 ;; compile support
1110 (set (make-local-variable 'next-error-function) 'diff-next-error)
1112 (setq buffer-read-only diff-default-read-only)
1113 ;; setup change hooks
1114 (if (not diff-update-on-the-fly)
1115 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
1116 (make-local-variable 'diff-unhandled-changes)
1117 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1118 (add-hook 'post-command-hook 'diff-post-command-hook nil t))
1119 ;; Neat trick from Dave Love to add more bindings in read-only mode:
1120 (lexical-let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
1121 (add-to-list 'minor-mode-overriding-map-alist ro-bind)
1122 ;; Turn off this little trick in case the buffer is put in view-mode.
1123 (add-hook 'view-mode-hook
1124 (lambda ()
1125 (setq minor-mode-overriding-map-alist
1126 (delq ro-bind minor-mode-overriding-map-alist)))
1127 nil t))
1128 ;; add-log support
1129 (set (make-local-variable 'add-log-current-defun-function)
1130 'diff-current-defun)
1131 (set (make-local-variable 'add-log-buffer-file-name-function)
1132 'diff-find-file-name))
1134 ;;;###autoload
1135 (define-minor-mode diff-minor-mode
1136 "Minor mode for viewing/editing context diffs.
1137 \\{diff-minor-mode-map}"
1138 :group 'diff-mode :lighter " Diff"
1139 ;; FIXME: setup font-lock
1140 ;; setup change hooks
1141 (if (not diff-update-on-the-fly)
1142 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
1143 (make-local-variable 'diff-unhandled-changes)
1144 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1145 (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
1147 ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1149 (defun diff-delete-if-empty ()
1150 ;; An empty diff file means there's no more diffs to integrate, so we
1151 ;; can just remove the file altogether. Very handy for .rej files if we
1152 ;; remove hunks as we apply them.
1153 (when (and buffer-file-name
1154 (eq 0 (nth 7 (file-attributes buffer-file-name))))
1155 (delete-file buffer-file-name)))
1157 (defun diff-delete-empty-files ()
1158 "Arrange for empty diff files to be removed."
1159 (add-hook 'after-save-hook 'diff-delete-if-empty nil t))
1161 (defun diff-make-unified ()
1162 "Turn context diffs into unified diffs if applicable."
1163 (if (save-excursion
1164 (goto-char (point-min))
1165 (and (looking-at diff-hunk-header-re) (eq (char-after) ?*)))
1166 (let ((mod (buffer-modified-p)))
1167 (unwind-protect
1168 (diff-context->unified (point-min) (point-max))
1169 (restore-buffer-modified-p mod)))))
1172 ;;; Misc operations that have proved useful at some point.
1175 (defun diff-next-complex-hunk ()
1176 "Jump to the next \"complex\" hunk.
1177 \"Complex\" is approximated by \"the hunk changes the number of lines\".
1178 Only works for unified diffs."
1179 (interactive)
1180 (while
1181 (and (re-search-forward diff-hunk-header-re-unified nil t)
1182 (equal (match-string 2) (match-string 4)))))
1184 (defun diff-sanity-check-context-hunk-half (lines)
1185 (let ((count lines))
1186 (while
1187 (cond
1188 ((and (memq (char-after) '(?\s ?! ?+ ?-))
1189 (memq (char-after (1+ (point))) '(?\s ?\t)))
1190 (decf count) t)
1191 ((or (zerop count) (= count lines)) nil)
1192 ((memq (char-after) '(?! ?+ ?-))
1193 (if (not (and (eq (char-after (1+ (point))) ?\n)
1194 (y-or-n-p "Try to auto-fix whitespace loss damage? ")))
1195 (error "End of hunk ambiguously marked")
1196 (forward-char 1) (insert " ") (forward-line -1) t))
1197 ((< lines 0)
1198 (error "End of hunk ambiguously marked"))
1199 ((not (y-or-n-p "Try to auto-fix whitespace loss and word-wrap damage? "))
1200 (error "Abort!"))
1201 ((eolp) (insert " ") (forward-line -1) t)
1202 (t (insert " ") (delete-region (- (point) 2) (- (point) 1)) t))
1203 (forward-line))))
1205 (defun diff-sanity-check-hunk ()
1206 (let (;; Every modification is protected by a y-or-n-p, so it's probably
1207 ;; OK to override a read-only setting.
1208 (inhibit-read-only t))
1209 (save-excursion
1210 (cond
1211 ((not (looking-at diff-hunk-header-re))
1212 (error "Not recognizable hunk header"))
1214 ;; A context diff.
1215 ((eq (char-after) ?*)
1216 (if (not (looking-at "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\([0-9]+\\),\\([0-9]+\\) \\*\\*\\*\\*"))
1217 (error "Unrecognized context diff first hunk header format")
1218 (forward-line 2)
1219 (diff-sanity-check-context-hunk-half
1220 (1+ (- (string-to-number (match-string 2))
1221 (string-to-number (match-string 1)))))
1222 (if (not (looking-at "--- \\([0-9]+\\),\\([0-9]+\\) ----$"))
1223 (error "Unrecognized context diff second hunk header format")
1224 (forward-line)
1225 (diff-sanity-check-context-hunk-half
1226 (1+ (- (string-to-number (match-string 2))
1227 (string-to-number (match-string 1))))))))
1229 ;; A unified diff.
1230 ((eq (char-after) ?@)
1231 (if (not (looking-at diff-hunk-header-re-unified))
1232 (error "Unrecognized unified diff hunk header format")
1233 (let ((before (string-to-number (match-string 2)))
1234 (after (string-to-number (match-string 4))))
1235 (forward-line)
1236 (while
1237 (case (char-after)
1238 (?\s (decf before) (decf after) t)
1240 (if (and (looking-at diff-file-header-re)
1241 (zerop before) (zerop after))
1242 ;; No need to query: this is a case where two patches
1243 ;; are concatenated and only counting the lines will
1244 ;; give the right result. Let's just add an empty
1245 ;; line so that our code which doesn't count lines
1246 ;; will not get confused.
1247 (progn (save-excursion (insert "\n")) nil)
1248 (decf before) t))
1249 (?+ (decf after) t)
1251 (cond
1252 ((and diff-valid-unified-empty-line
1253 ;; Not just (eolp) so we don't infloop at eob.
1254 (eq (char-after) ?\n)
1255 (> before 0) (> after 0))
1256 (decf before) (decf after) t)
1257 ((and (zerop before) (zerop after)) nil)
1258 ((or (< before 0) (< after 0))
1259 (error (if (or (zerop before) (zerop after))
1260 "End of hunk ambiguously marked"
1261 "Hunk seriously messed up")))
1262 ((not (y-or-n-p (concat "Try to auto-fix " (if (eolp) "whitespace loss" "word-wrap damage") "? ")))
1263 (error "Abort!"))
1264 ((eolp) (insert " ") (forward-line -1) t)
1265 (t (insert " ")
1266 (delete-region (- (point) 2) (- (point) 1)) t))))
1267 (forward-line)))))
1269 ;; A plain diff.
1271 ;; TODO.
1272 )))))
1274 (defun diff-hunk-text (hunk destp char-offset)
1275 "Return the literal source text from HUNK as (TEXT . OFFSET).
1276 If DESTP is nil, TEXT is the source, otherwise the destination text.
1277 CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding
1278 char-offset in TEXT."
1279 (with-temp-buffer
1280 (insert hunk)
1281 (goto-char (point-min))
1282 (let ((src-pos nil)
1283 (dst-pos nil)
1284 (divider-pos nil)
1285 (num-pfx-chars 2))
1286 ;; Set the following variables:
1287 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
1288 ;; DST-POS buffer pos of the destination part of the hunk or nil
1289 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
1290 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
1291 (cond ((looking-at "^@@")
1292 ;; unified diff
1293 (setq num-pfx-chars 1)
1294 (forward-line 1)
1295 (setq src-pos (point) dst-pos (point)))
1296 ((looking-at "^\\*\\*")
1297 ;; context diff
1298 (forward-line 2)
1299 (setq src-pos (point))
1300 (re-search-forward "^--- " nil t)
1301 (forward-line 0)
1302 (setq divider-pos (point))
1303 (forward-line 1)
1304 (setq dst-pos (point)))
1305 ((looking-at "^[0-9]+a[0-9,]+$")
1306 ;; normal diff, insert
1307 (forward-line 1)
1308 (setq dst-pos (point)))
1309 ((looking-at "^[0-9,]+d[0-9]+$")
1310 ;; normal diff, delete
1311 (forward-line 1)
1312 (setq src-pos (point)))
1313 ((looking-at "^[0-9,]+c[0-9,]+$")
1314 ;; normal diff, change
1315 (forward-line 1)
1316 (setq src-pos (point))
1317 (re-search-forward "^---$" nil t)
1318 (forward-line 0)
1319 (setq divider-pos (point))
1320 (forward-line 1)
1321 (setq dst-pos (point)))
1323 (error "Unknown diff hunk type")))
1325 (if (if destp (null dst-pos) (null src-pos))
1326 ;; Implied empty text
1327 (if char-offset '("" . 0) "")
1329 ;; For context diffs, either side can be empty, (if there's only
1330 ;; added or only removed text). We should then use the other side.
1331 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
1332 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
1334 (when char-offset (goto-char (+ (point-min) char-offset)))
1336 ;; Get rid of anything except the desired text.
1337 (save-excursion
1338 ;; Delete unused text region
1339 (let ((keep (if destp dst-pos src-pos)))
1340 (when (and divider-pos (> divider-pos keep))
1341 (delete-region divider-pos (point-max)))
1342 (delete-region (point-min) keep))
1343 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1344 (let ((kill-char (if destp ?- ?+)))
1345 (goto-char (point-min))
1346 (while (not (eobp))
1347 (if (eq (char-after) kill-char)
1348 (delete-region (point) (progn (forward-line 1) (point)))
1349 (delete-char num-pfx-chars)
1350 (forward-line 1)))))
1352 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1353 (if char-offset (cons text (- (point) (point-min))) text))))))
1356 (defun diff-find-text (text)
1357 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1358 If TEXT isn't found, nil is returned."
1359 (let* ((orig (point))
1360 (forw (and (search-forward text nil t)
1361 (cons (match-beginning 0) (match-end 0))))
1362 (back (and (goto-char (+ orig (length text)))
1363 (search-backward text nil t)
1364 (cons (match-beginning 0) (match-end 0)))))
1365 ;; Choose the closest match.
1366 (if (and forw back)
1367 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1368 (or back forw))))
1370 (defun diff-find-approx-text (text)
1371 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1372 Whitespace differences are ignored."
1373 (let* ((orig (point))
1374 (re (concat "^[ \t\n\f]*"
1375 (mapconcat 'regexp-quote (split-string text) "[ \t\n\f]+")
1376 "[ \t\n\f]*\n"))
1377 (forw (and (re-search-forward re nil t)
1378 (cons (match-beginning 0) (match-end 0))))
1379 (back (and (goto-char (+ orig (length text)))
1380 (re-search-backward re nil t)
1381 (cons (match-beginning 0) (match-end 0)))))
1382 ;; Choose the closest match.
1383 (if (and forw back)
1384 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1385 (or back forw))))
1387 (defsubst diff-xor (a b) (if a (not b) b))
1389 (defun diff-find-source-location (&optional other-file reverse)
1390 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
1391 BUF is the buffer corresponding to the source file.
1392 LINE-OFFSET is the offset between the expected and actual positions
1393 of the text of the hunk or nil if the text was not found.
1394 POS is a pair (BEG . END) indicating the position of the text in the buffer.
1395 SRC and DST are the two variants of text as returned by `diff-hunk-text'.
1396 SRC is the variant that was found in the buffer.
1397 SWITCHED is non-nil if the patch is already applied."
1398 (save-excursion
1399 (let* ((other (diff-xor other-file diff-jump-to-old-file))
1400 (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1401 (point))))
1402 ;; Check that the hunk is well-formed. Otherwise diff-mode and
1403 ;; the user may disagree on what constitutes the hunk
1404 ;; (e.g. because an empty line truncates the hunk mid-course),
1405 ;; leading to potentially nasty surprises for the user.
1406 (_ (diff-sanity-check-hunk))
1407 (hunk (buffer-substring (point)
1408 (save-excursion (diff-end-of-hunk) (point))))
1409 (old (diff-hunk-text hunk reverse char-offset))
1410 (new (diff-hunk-text hunk (not reverse) char-offset))
1411 ;; Find the location specification.
1412 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
1413 (error "Can't find the hunk header")
1414 (if other (match-string 1)
1415 (if (match-end 3) (match-string 3)
1416 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
1417 (error "Can't find the hunk separator"))
1418 (match-string 1)))))
1419 (file (or (diff-find-file-name other) (error "Can't find the file")))
1420 (buf (find-file-noselect file)))
1421 ;; Update the user preference if he so wished.
1422 (when (> (prefix-numeric-value other-file) 8)
1423 (setq diff-jump-to-old-file other))
1424 (with-current-buffer buf
1425 (goto-line (string-to-number line))
1426 (let* ((orig-pos (point))
1427 (switched nil)
1428 ;; FIXME: Check for case where both OLD and NEW are found.
1429 (pos (or (diff-find-text (car old))
1430 (progn (setq switched t) (diff-find-text (car new)))
1431 (progn (setq switched nil)
1432 (condition-case nil
1433 (diff-find-approx-text (car old))
1434 (invalid-regexp nil))) ;Regex too big.
1435 (progn (setq switched t)
1436 (condition-case nil
1437 (diff-find-approx-text (car new))
1438 (invalid-regexp nil))) ;Regex too big.
1439 (progn (setq switched nil) nil))))
1440 (nconc
1441 (list buf)
1442 (if pos
1443 (list (count-lines orig-pos (car pos)) pos)
1444 (list nil (cons orig-pos (+ orig-pos (length (car old))))))
1445 (if switched (list new old t) (list old new))))))))
1448 (defun diff-hunk-status-msg (line-offset reversed dry-run)
1449 (let ((msg (if dry-run
1450 (if reversed "already applied" "not yet applied")
1451 (if reversed "undone" "applied"))))
1452 (message (cond ((null line-offset) "Hunk text not found")
1453 ((= line-offset 0) "Hunk %s")
1454 ((= line-offset 1) "Hunk %s at offset %d line")
1455 (t "Hunk %s at offset %d lines"))
1456 msg line-offset)))
1458 (defvar diff-apply-hunk-to-backup-file nil)
1460 (defun diff-apply-hunk (&optional reverse)
1461 "Apply the current hunk to the source file and go to the next.
1462 By default, the new source file is patched, but if the variable
1463 `diff-jump-to-old-file' is non-nil, then the old source file is
1464 patched instead (some commands, such as `diff-goto-source' can change
1465 the value of this variable when given an appropriate prefix argument).
1467 With a prefix argument, REVERSE the hunk."
1468 (interactive "P")
1469 (destructuring-bind (buf line-offset pos old new &optional switched)
1470 ;; If REVERSE go to the new file, otherwise go to the old.
1471 (diff-find-source-location (not reverse) reverse)
1472 (cond
1473 ((null line-offset)
1474 (error "Can't find the text to patch"))
1475 ((with-current-buffer buf
1476 (and buffer-file-name
1477 (backup-file-name-p buffer-file-name)
1478 (not diff-apply-hunk-to-backup-file)
1479 (not (set (make-local-variable 'diff-apply-hunk-to-backup-file)
1480 (yes-or-no-p (format "Really apply this hunk to %s? "
1481 (file-name-nondirectory
1482 buffer-file-name)))))))
1483 (error "%s"
1484 (substitute-command-keys
1485 (format "Use %s\\[diff-apply-hunk] to apply it to the other file"
1486 (if (not reverse) "\\[universal-argument] ")))))
1487 ((and switched
1488 ;; A reversed patch was detected, perhaps apply it in reverse.
1489 (not (save-window-excursion
1490 (pop-to-buffer buf)
1491 (goto-char (+ (car pos) (cdr old)))
1492 (y-or-n-p
1493 (if reverse
1494 "Hunk hasn't been applied yet; apply it now? "
1495 "Hunk has already been applied; undo it? ")))))
1496 (message "(Nothing done)"))
1498 ;; Apply the hunk
1499 (with-current-buffer buf
1500 (goto-char (car pos))
1501 (delete-region (car pos) (cdr pos))
1502 (insert (car new)))
1503 ;; Display BUF in a window
1504 (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
1505 (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
1506 (when diff-advance-after-apply-hunk
1507 (diff-hunk-next))))))
1510 (defun diff-test-hunk (&optional reverse)
1511 "See whether it's possible to apply the current hunk.
1512 With a prefix argument, try to REVERSE the hunk."
1513 (interactive "P")
1514 (destructuring-bind (buf line-offset pos src dst &optional switched)
1515 ;; If REVERSE go to the new file, otherwise go to the old.
1516 (diff-find-source-location (not reverse) reverse)
1517 (set-window-point (display-buffer buf) (+ (car pos) (cdr src)))
1518 (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
1521 (defalias 'diff-mouse-goto-source 'diff-goto-source)
1523 (defun diff-goto-source (&optional other-file event)
1524 "Jump to the corresponding source line.
1525 `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
1526 is given) determines whether to jump to the old or the new file.
1527 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1528 then `diff-jump-to-old-file' is also set, for the next invocations."
1529 (interactive (list current-prefix-arg last-input-event))
1530 ;; When pointing at a removal line, we probably want to jump to
1531 ;; the old location, and else to the new (i.e. as if reverting).
1532 ;; This is a convenient detail when using smerge-diff.
1533 (if event (posn-set-point (event-end event)))
1534 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1535 (destructuring-bind (buf line-offset pos src dst &optional switched)
1536 (diff-find-source-location other-file rev)
1537 (pop-to-buffer buf)
1538 (goto-char (+ (car pos) (cdr src)))
1539 (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
1542 (defun diff-current-defun ()
1543 "Find the name of function at point.
1544 For use in `add-log-current-defun-function'."
1545 (save-excursion
1546 (when (looking-at diff-hunk-header-re)
1547 (forward-line 1)
1548 (re-search-forward "^[^ ]" nil t))
1549 (destructuring-bind (buf line-offset pos src dst &optional switched)
1550 (diff-find-source-location)
1551 (beginning-of-line)
1552 (or (when (memq (char-after) '(?< ?-))
1553 ;; Cursor is pointing at removed text. This could be a removed
1554 ;; function, in which case, going to the source buffer will
1555 ;; not help since the function is now removed. Instead,
1556 ;; try to figure out the function name just from the code-fragment.
1557 (let ((old (if switched dst src)))
1558 (with-temp-buffer
1559 (insert (car old))
1560 (funcall (with-current-buffer buf major-mode))
1561 (goto-char (+ (point-min) (cdr old)))
1562 (add-log-current-defun))))
1563 (with-current-buffer buf
1564 (goto-char (+ (car pos) (cdr src)))
1565 (add-log-current-defun))))))
1567 (defun diff-refine-hunk ()
1568 "Refine the current hunk by ignoring space differences."
1569 (interactive)
1570 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1571 (point))))
1572 (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b")))
1573 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
1574 (error "Can't find line number"))
1575 (string-to-number (match-string 1))))
1576 (hunk (delete-and-extract-region
1577 (point) (save-excursion (diff-end-of-hunk) (point))))
1578 (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
1579 (file1 (make-temp-file "diff1"))
1580 (file2 (make-temp-file "diff2"))
1581 (coding-system-for-read buffer-file-coding-system)
1582 (inhibit-read-only t)
1583 old new)
1584 (unwind-protect
1585 (save-excursion
1586 (setq old (diff-hunk-text hunk nil char-offset))
1587 (setq new (diff-hunk-text hunk t char-offset))
1588 (write-region (concat lead (car old)) nil file1 nil 'nomessage)
1589 (write-region (concat lead (car new)) nil file2 nil 'nomessage)
1590 (with-temp-buffer
1591 (let ((status
1592 (call-process diff-command nil t nil
1593 opts file1 file2)))
1594 (case status
1595 (0 nil) ;Nothing to reformat.
1596 (1 (goto-char (point-min))
1597 ;; Remove the file-header.
1598 (when (re-search-forward diff-hunk-header-re nil t)
1599 (delete-region (point-min) (match-beginning 0))))
1600 (t (goto-char (point-max))
1601 (unless (bolp) (insert "\n"))
1602 (insert hunk)))
1603 (setq hunk (buffer-string))
1604 (unless (memq status '(0 1))
1605 (error "Diff returned: %s" status)))))
1606 ;; Whatever happens, put back some equivalent text: either the new
1607 ;; one or the original one in case some error happened.
1608 (insert hunk)
1609 (delete-file file1)
1610 (delete-file file2))))
1612 ;; provide the package
1613 (provide 'diff-mode)
1615 ;;; Old Change Log from when diff-mode wasn't part of Emacs:
1616 ;; Revision 1.11 1999/10/09 23:38:29 monnier
1617 ;; (diff-mode-load-hook): dropped.
1618 ;; (auto-mode-alist): also catch *.diffs.
1619 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
1620 ;; for *.rej files (that lack any file name indication).
1622 ;; Revision 1.10 1999/09/30 15:32:11 monnier
1623 ;; added support for "\ No newline at end of file".
1625 ;; Revision 1.9 1999/09/15 00:01:13 monnier
1626 ;; - added basic `compile' support.
1627 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
1628 ;; - diff-kill-file now tries to kill the leading garbage as well.
1630 ;; Revision 1.8 1999/09/13 21:10:09 monnier
1631 ;; - don't use CL in the autoloaded code
1632 ;; - accept diffs using -T
1634 ;; Revision 1.7 1999/09/05 20:53:03 monnier
1635 ;; interface to ediff-patch
1637 ;; Revision 1.6 1999/09/01 20:55:13 monnier
1638 ;; (ediff=patch-file): add bindings to call ediff-patch.
1639 ;; (diff-find-file-name): taken out of diff-goto-source.
1640 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
1641 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
1643 ;; Revision 1.5 1999/08/31 19:18:52 monnier
1644 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
1646 ;; Revision 1.4 1999/08/31 13:01:44 monnier
1647 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
1650 ;; arch-tag: 2571d7ff-bc28-4cf9-8585-42e21890be66
1651 ;;; diff-mode.el ends here