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)
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.
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
38 ;; - Reverse doesn't work with normal diffs.
42 ;; - Improve `diff-add-change-log-entries-other-window',
43 ;; it is very simplistic now.
45 ;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
46 ;; Also allow C-c C-a to delete already-applied hunks.
48 ;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location
49 ;; of a hunk. Show then the changes between <file> and <hunk> and make it
50 ;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>.
51 ;; Or maybe just make it into a ".rej to diff3-markers converter".
52 ;; Maybe just use `wiggle' (by Neil Brown) to do it for us.
54 ;; - in diff-apply-hunk, strip context in replace-match to better
55 ;; preserve markers and spacing.
56 ;; - Handle `diff -b' output in context->unified.
59 (eval-when-compile (require 'cl
))
61 (defvar add-log-buffer-file-name-function
)
64 (defgroup diff-mode
()
65 "Major mode for viewing/editing diffs."
70 (defcustom diff-default-read-only nil
71 "If non-nil, `diff-mode' buffers default to being read-only."
75 (defcustom diff-jump-to-old-file nil
76 "Non-nil means `diff-goto-source' jumps to the old file.
77 Else, it jumps to the new file."
81 (defcustom diff-update-on-the-fly t
82 "*Non-nil means hunk headers are kept up-to-date on-the-fly.
83 When editing a diff file, the line numbers in the hunk headers
84 need to be kept consistent with the actual diff. This can
85 either be done on the fly (but this sometimes interacts poorly with the
86 undo mechanism) or whenever the file is written (can be slow
87 when editing big diffs)."
91 (defcustom diff-advance-after-apply-hunk t
92 "*Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
96 (defcustom diff-auto-refine t
97 "Automatically highlight changes in detail as the user visits hunks."
101 (defcustom diff-mode-hook nil
102 "Run after setting up the `diff-mode' major mode."
104 :options
'(diff-delete-empty-files diff-make-unified
)
107 (defvar diff-outline-regexp
108 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
111 ;;;; keymap, menu, ...
114 (easy-mmode-defmap diff-mode-shared-map
115 '(;; From Pavel Machek's patch-mode.
116 ("n" . diff-hunk-next
)
117 ("N" . diff-file-next
)
118 ("p" . diff-hunk-prev
)
119 ("P" . diff-file-prev
)
120 ("\t" . diff-hunk-next
)
121 ([backtab] . diff-hunk-prev)
122 ("k" . diff-hunk-kill)
123 ("K" . diff-file-kill)
124 ;; From compilation-minor-mode.
125 ("}" . diff-file-next)
126 ("{" . diff-file-prev)
127 ("\C-m" . diff-goto-source)
128 ([mouse-2] . diff-goto-source)
129 ;; From XEmacs' diff-mode.
130 ;; Standard M-w is useful, so don't change M-W.
132 ;;("." . diff-goto-source) ;display-buffer
133 ;;("f" . diff-goto-source) ;find-file
134 ("o" . diff-goto-source) ;other-window
135 ;;("w" . diff-goto-source) ;other-frame
136 ;;("N" . diff-narrow)
137 ;;("h" . diff-show-header)
138 ;;("j" . diff-show-difference) ;jump to Nth diff
140 ;; Not useful if you have to metafy them.
142 ;;("\177" . scroll-down)
143 ;; Standard M-a is useful, so don't change M-A.
144 ;;("A" . diff-ediff-patch)
145 ;; Standard M-r is useful, so don't change M-r or M-R.
146 ;;("r" . diff-restrict-view)
147 ;;("R" . diff-reverse-direction)
149 "Basic keymap for `diff-mode', bound to various prefix keys.")
151 (easy-mmode-defmap diff-mode-map
152 `(("\e" . ,diff-mode-shared-map)
153 ;; From compilation-minor-mode.
154 ("\C-c\C-c" . diff-goto-source)
155 ;; By analogy with the global C-x 4 a binding.
156 ("\C-x4A" . diff-add-change-log-entries-other-window)
158 ("\C-c\C-a" . diff-apply-hunk)
159 ("\C-c\C-e" . diff-ediff-patch)
160 ("\C-c\C-n" . diff-restrict-view)
161 ("\C-c\C-s" . diff-split-hunk)
162 ("\C-c\C-t" . diff-test-hunk)
163 ("\C-c\C-r" . diff-reverse-direction)
164 ("\C-c\C-u" . diff-context->unified)
165 ;; `d' because it duplicates the context :-( --Stef
166 ("\C-c\C-d" . diff-unified->context)
167 ("\C-c\C-w" . diff-ignore-whitespace-hunk)
168 ("\C-c\C-b" . diff-refine-hunk) ;No reason for `b' :-(
169 ("\C-c\C-f" . next-error-follow-minor-mode))
170 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
172 (easy-menu-define diff-mode-menu diff-mode-map
173 "Menu for `diff-mode'."
175 ["Jump to Source" diff-goto-source
176 :help "Jump to the corresponding source line"]
177 ["Apply hunk" diff-apply-hunk
178 :help "Apply the current hunk to the source file and go to the next"]
179 ["Test applying hunk" diff-test-hunk
180 :help "See whether it's possible to apply the current hunk"]
181 ["Apply diff with Ediff" diff-ediff-patch
182 :help "Call `ediff-patch-file' on the current buffer"]
183 ["Create Change Log entries" diff-add-change-log-entries-other-window
184 :help "Create ChangeLog entries for the changes in the diff buffer"]
186 ["Reverse direction" diff-reverse-direction
187 :help "Reverse the direction of the diffs"]
188 ["Context -> Unified" diff-context->unified
189 :help "Convert context diffs to unified diffs"]
190 ["Unified -> Context" diff-unified->context
191 :help "Convert unified diffs to context diffs"]
192 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
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"]
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"]
216 (defcustom diff-minor-mode-prefix "\C-c="
217 "Prefix key for `diff-minor-mode' commands."
218 :type '(choice (string "\e") (string "C-c=") string)
221 (easy-mmode-defmap diff-minor-mode-map
222 `((,diff-minor-mode-prefix . ,diff-mode-shared-map))
223 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
227 ;;;; font-lock support
231 '((((class color) (min-colors 88) (background light))
232 :background "grey80")
233 (((class color) (min-colors 88) (background dark))
234 :background "grey45")
235 (((class color) (background light))
236 :foreground "blue1" :weight bold)
237 (((class color) (background dark))
238 :foreground "green" :weight bold)
240 "`diff-mode' face inherited by hunk and index header faces."
242 ;; backward-compatibility alias
243 (put 'diff-header-face 'face-alias 'diff-header)
244 (defvar diff-header-face 'diff-header)
246 (defface diff-file-header
247 '((((class color) (min-colors 88) (background light))
248 :background "grey70" :weight bold)
249 (((class color) (min-colors 88) (background dark))
250 :background "grey60" :weight bold)
251 (((class color) (background light))
252 :foreground "green" :weight bold)
253 (((class color) (background dark))
254 :foreground "cyan" :weight bold)
255 (t :weight bold)) ; :height 1.3
256 "`diff-mode' face used to highlight file header lines."
258 ;; backward-compatibility alias
259 (put 'diff-file-header-face 'face-alias 'diff-file-header)
260 (defvar diff-file-header-face 'diff-file-header)
263 '((t :inherit diff-file-header))
264 "`diff-mode' face used to highlight index header lines."
266 ;; backward-compatibility alias
267 (put 'diff-index-face 'face-alias 'diff-index)
268 (defvar diff-index-face 'diff-index)
270 (defface diff-hunk-header
271 '((t :inherit diff-header))
272 "`diff-mode' face used to highlight hunk header lines."
274 ;; backward-compatibility alias
275 (put 'diff-hunk-header-face 'face-alias 'diff-hunk-header)
276 (defvar diff-hunk-header-face 'diff-hunk-header)
278 (defface diff-removed
279 '((t :inherit diff-changed))
280 "`diff-mode' face used to highlight removed lines."
282 ;; backward-compatibility alias
283 (put 'diff-removed-face 'face-alias 'diff-removed)
284 (defvar diff-removed-face 'diff-removed)
287 '((t :inherit diff-changed))
288 "`diff-mode' face used to highlight added lines."
290 ;; backward-compatibility alias
291 (put 'diff-added-face 'face-alias 'diff-added)
292 (defvar diff-added-face 'diff-added)
294 (defface diff-changed
295 '((((type tty pc) (class color) (background light))
296 :foreground "magenta" :weight bold :slant italic)
297 (((type tty pc) (class color) (background dark))
298 :foreground "yellow" :weight bold :slant italic))
299 "`diff-mode' face used to highlight changed lines."
301 ;; backward-compatibility alias
302 (put 'diff-changed-face 'face-alias 'diff-changed)
303 (defvar diff-changed-face 'diff-changed)
305 (defface diff-indicator-removed
306 '((t :inherit diff-removed))
307 "`diff-mode' face used to highlight indicator of removed lines (-, <)."
310 (defvar diff-indicator-removed-face 'diff-indicator-removed)
312 (defface diff-indicator-added
313 '((t :inherit diff-added))
314 "`diff-mode' face used to highlight indicator of added lines (+, >)."
317 (defvar diff-indicator-added-face 'diff-indicator-added)
319 (defface diff-indicator-changed
320 '((t :inherit diff-changed))
321 "`diff-mode' face used to highlight indicator of changed lines."
324 (defvar diff-indicator-changed-face 'diff-indicator-changed)
326 (defface diff-function
327 '((t :inherit diff-header))
328 "`diff-mode' face used to highlight function names produced by \"diff -p\"."
330 ;; backward-compatibility alias
331 (put 'diff-function-face 'face-alias 'diff-function)
332 (defvar diff-function-face 'diff-function)
334 (defface diff-context
335 '((((class color grayscale) (min-colors 88)) :inherit shadow))
336 "`diff-mode' face used to highlight context and other side-information."
338 ;; backward-compatibility alias
339 (put 'diff-context-face 'face-alias 'diff-context)
340 (defvar diff-context-face 'diff-context)
342 (defface diff-nonexistent
343 '((t :inherit diff-file-header))
344 "`diff-mode' face used to highlight nonexistent files in recursive diffs."
346 ;; backward-compatibility alias
347 (put 'diff-nonexistent-face 'face-alias 'diff-nonexistent)
348 (defvar diff-nonexistent-face 'diff-nonexistent)
350 (defconst diff-yank-handler '(diff-yank-function))
351 (defun diff-yank-function (text)
352 ;; FIXME: the yank-handler is now called separately on each piece of text
353 ;; with a yank-handler property, so the next-single-property-change call
354 ;; below will always return nil :-( --stef
355 (let ((mixed (next-single-property-change 0 'yank-handler text))
357 ;; First insert the text.
359 ;; If the text does not include any diff markers and if we're not
360 ;; yanking back into a diff-mode buffer, get rid of the prefixes.
361 (unless (or mixed (derived-mode-p 'diff-mode))
362 (undo-boundary) ; Just in case the user wanted the prefixes.
363 (let ((re (save-excursion
364 (if (re-search-backward "^[><!][ \t]" start t)
365 (if (eq (char-after) ?!)
366 "^[!+- ][ \t]" "^[<>][ \t]")
369 (while (re-search-backward re start t)
370 (replace-match "" t t)))))))
372 (defconst diff-hunk-header-re-unified
373 "^@@ -\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\) @@")
375 (defvar diff-font-lock-keywords
376 `((,(concat "\\(" diff-hunk-header-re-unified "\\)\\(.*\\)$")
377 (1 diff-hunk-header-face) (6 diff-function-face))
378 ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
379 (1 diff-hunk-header-face) (2 diff-function-face))
380 ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context
381 ("^--- .+ ----$" . diff-hunk-header-face) ;context
382 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face) ;normal
383 ("^---$" . diff-hunk-header-face) ;normal
384 ;; For file headers, accept files with spaces, but be careful to rule
385 ;; out false-positives when matching hunk headers.
386 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\([^\t\n]+?\\)\\(?:\t.*\\| \\(\\*\\*\\*\\*\\|----\\)\\)?\n"
388 (2 (if (not (match-end 3)) diff-file-header-face) prepend))
389 ("^\\([-<]\\)\\(.*\n\\)"
390 (1 diff-indicator-removed-face) (2 diff-removed-face))
391 ("^\\([+>]\\)\\(.*\n\\)"
392 (1 diff-indicator-added-face) (2 diff-added-face))
393 ("^\\(!\\)\\(.*\n\\)"
394 (1 diff-indicator-changed-face) (2 diff-changed-face))
395 ("^Index: \\(.+\\).*\n"
396 (0 diff-header-face) (1 diff-index-face prepend))
397 ("^Only in .*\n" . diff-nonexistent-face)
399 (1 font-lock-comment-delimiter-face)
400 (2 font-lock-comment-face))
401 ("^[^-=+*!<>#].*\n" (0 diff-context-face))))
403 (defconst diff-font-lock-defaults
404 '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
406 (defvar diff-imenu-generic-expression
407 ;; Prefer second name as first is most likely to be a backup or
408 ;; version-control name. The [\t\n] at the end of the unidiff pattern
409 ;; catches Debian source diff files (which lack the trailing date).
410 '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
411 (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
417 (defvar diff-valid-unified-empty-line t
418 "If non-nil, empty lines are valid in unified diffs.
419 Some versions of diff replace all-blank context lines in unified format with
420 empty lines. This makes the format less robust, but is tolerated.
421 See http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01990.html")
423 (defconst diff-hunk-header-re
424 (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$"))
425 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1)))
426 (defvar diff-narrowed-to nil)
428 (defun diff-hunk-style (&optional style)
429 (when (looking-at diff-hunk-header-re)
430 (setq style (cdr (assq (char-after) '((?@ . unified) (?* . context)))))
431 (goto-char (match-end 0)))
434 (defun diff-end-of-hunk (&optional style donttrustheader)
436 (when (looking-at diff-hunk-header-re)
437 ;; Especially important for unified (because headers are ambiguous).
438 (setq style (diff-hunk-style style))
439 (goto-char (match-end 0))
440 (when (and (not donttrustheader) (match-end 2))
442 (re-search-forward (if diff-valid-unified-empty-line
445 (string-to-number (match-string 2)))
446 (setq end (line-beginning-position 2)))))
447 ;; We may have a first evaluation of `end' thanks to the hunk header.
449 (setq end (and (re-search-forward
451 (unified (concat (if diff-valid-unified-empty-line
452 "^[^-+# \\\n]\\|" "^[^-+# \\]\\|")
453 ;; A `unified' header is ambiguous.
454 diff-file-header-re))
455 (context "^[^-+#! \\]")
459 (match-beginning 0)))
460 (when diff-valid-unified-empty-line
461 ;; While empty lines may be valid inside hunks, they are also likely
462 ;; to be unrelated to the hunk.
463 (goto-char (or end (point-max)))
464 (while (eq ?\n (char-before (1- (point))))
466 (setq end (point)))))
467 ;; The return value is used by easy-mmode-define-navigation.
468 (goto-char (or end (point-max)))))
470 (defun diff-beginning-of-hunk (&optional try-harder)
471 "Move back to beginning of hunk.
472 If TRY-HARDER is non-nil, try to cater to the case where we're not in a hunk
473 but in the file header instead, in which case move forward to the first hunk."
475 (unless (looking-at diff-hunk-header-re)
478 (re-search-backward diff-hunk-header-re)
481 (error "Can't find the beginning of the hunk")
482 (diff-beginning-of-file-and-junk)
483 (diff-hunk-next))))))
485 (defun diff-unified-hunk-p ()
488 (diff-beginning-of-hunk)
489 (looking-at "^@@"))))
491 (defun diff-beginning-of-file ()
493 (unless (looking-at diff-file-header-re)
494 (let ((start (point))
496 ;; diff-file-header-re may need to match up to 4 lines, so in case
497 ;; we're inside the header, we need to move up to 3 lines forward.
499 (if (and (setq res (re-search-backward diff-file-header-re nil t))
500 ;; Maybe the 3 lines forward were too much and we matched
501 ;; a file header after our starting point :-(
502 (or (<= (point) start)
503 (setq res (re-search-backward diff-file-header-re nil t))))
506 (error "Can't find the beginning of the file")))))
509 (defun diff-end-of-file ()
510 (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
511 (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re)
513 (if (match-beginning 1)
514 (goto-char (match-beginning 1))
515 (beginning-of-line)))
517 ;; Define diff-{hunk,file}-{prev,next}
518 (easy-mmode-define-navigation
519 diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view
521 (condition-case-no-debug nil (diff-refine-hunk) (error nil))))
523 (easy-mmode-define-navigation
524 diff-file diff-file-header-re "file" diff-end-of-hunk)
526 (defun diff-restrict-view (&optional arg)
527 "Restrict the view to the current hunk.
528 If the prefix ARG is given, restrict the view to the current file instead."
531 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk 'try-harder))
532 (narrow-to-region (point)
533 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
535 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
538 (defun diff-hunk-kill ()
541 (diff-beginning-of-hunk)
542 (let* ((start (point))
543 (nexthunk (when (re-search-forward diff-hunk-header-re nil t)
544 (match-beginning 0)))
545 (firsthunk (ignore-errors
547 (diff-beginning-of-file) (diff-hunk-next) (point)))
548 (nextfile (ignore-errors (diff-file-next) (point)))
549 (inhibit-read-only t))
551 (if (and firsthunk (= firsthunk start)
553 (and nextfile (> nexthunk nextfile))))
554 ;; It's the only hunk for this file, so kill the file.
557 (kill-region start (point)))))
559 (defconst diff-file-junk-re "diff \\|index ") ; "index " is output by git-diff.
561 (defun diff-beginning-of-file-and-junk ()
562 "Go to the beginning of file-related diff-info.
563 This is like `diff-beginning-of-file' except it tries to skip back over leading
564 data such as \"Index: ...\" and such."
565 (let* ((orig (point))
566 ;; Skip forward over what might be "leading junk" so as to get
567 ;; closer to the actual diff.
568 (_ (progn (beginning-of-line)
569 (while (looking-at diff-file-junk-re)
572 (prevfile (condition-case err
573 (save-excursion (diff-beginning-of-file) (point))
575 (err (if (consp prevfile) prevfile))
576 (nextfile (ignore-errors
578 (goto-char start) (diff-file-next) (point))))
579 ;; prevhunk is one of the limits.
580 (prevhunk (save-excursion
582 (if (numberp prevfile) (goto-char prevfile))
583 (diff-hunk-prev) (point))))
584 (previndex (save-excursion
585 (forward-line 1) ;In case we're looking at "Index:".
586 (re-search-backward "^Index: " prevhunk t))))
587 ;; If we're in the junk, we should use nextfile instead of prevfile.
588 (if (and (numberp nextfile)
589 (or (not (numberp prevfile))
590 (and previndex (> previndex prevfile))))
591 (setq prevfile nextfile))
592 (if (and previndex (numberp prevfile) (< previndex prevfile))
593 (setq prevfile previndex))
594 (if (and (numberp prevfile) (<= prevfile start))
597 ;; Now skip backward over the leading junk we may have before the
599 (while (save-excursion
600 (and (zerop (forward-line -1))
601 (looking-at diff-file-junk-re)))
603 ;; File starts *after* the starting point: we really weren't in
604 ;; a file diff but elsewhere.
606 (signal (car err) (cdr err)))))
608 (defun diff-file-kill ()
609 "Kill current file's hunks."
612 (start (progn (diff-beginning-of-file-and-junk) (point)))
613 (inhibit-read-only t))
615 (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
616 (if (> orig (point)) (error "Not inside a file diff"))
617 (kill-region start (point))))
619 (defun diff-kill-junk ()
620 "Kill spurious empty diffs."
623 (let ((inhibit-read-only t))
624 (goto-char (point-min))
625 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
626 "\\([^-+!* <>].*\n\\)*?"
627 "\\(\\(Index:\\) \\|"
628 diff-file-header-re "\\)")
630 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
632 (beginning-of-line)))))
634 (defun diff-count-matches (re start end)
638 (while (re-search-forward re end t) (incf n))
641 (defun diff-splittable-p ()
644 (and (looking-at "^[-+ ]")
645 (progn (forward-line -1) (looking-at "^[-+ ]"))
646 (diff-unified-hunk-p))))
648 (defun diff-split-hunk ()
649 "Split the current (unified diff) hunk at point into two hunks."
653 (start (progn (diff-beginning-of-hunk) (point))))
654 (unless (looking-at diff-hunk-header-re-unified)
655 (error "diff-split-hunk only works on unified context diffs"))
657 (let* ((start1 (string-to-number (match-string 1)))
658 (start2 (string-to-number (match-string 3)))
659 (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
660 (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos)))
661 (inhibit-read-only t))
663 ;; Hopefully the after-change-function will not screw us over.
664 (insert "@@ -" (number-to-string newstart1) ",1 +"
665 (number-to-string newstart2) ",1 @@\n")
666 ;; Fix the original hunk-header.
667 (diff-fixup-modifs start pos))))
671 ;;;; jump to other buffers
674 (defvar diff-remembered-files-alist nil)
675 (defvar diff-remembered-defdir nil)
677 (defun diff-filename-drop-dir (file)
678 (when (string-match "/" file) (substring file (match-end 0))))
680 (defun diff-merge-strings (ancestor from to)
681 "Merge the diff between ANCESTOR and FROM into TO.
682 Returns the merged string if successful or nil otherwise.
683 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
684 If ANCESTOR = FROM, returns TO.
685 If ANCESTOR = TO, returns FROM.
686 The heuristic is simplistic and only really works for cases
687 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
689 ;; AMB ANB CMD -> CND
690 ;; but that's ambiguous if `foo' or `bar' is empty:
691 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
692 (let ((str (concat ancestor "\n" from "\n" to)))
693 (when (and (string-match (concat
694 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
696 "\\(.*\\(\\2\\).*\\)\\'") str)
697 (equal to (match-string 5 str)))
698 (concat (substring str (match-beginning 5) (match-beginning 6))
700 (substring str (match-end 6) (match-end 5))))))
702 (defun diff-tell-file-name (old name)
703 "Tell Emacs where the find the source file of the current hunk.
704 If the OLD prefix arg is passed, tell the file NAME of the old file."
706 (let* ((old current-prefix-arg)
707 (fs (diff-hunk-file-names current-prefix-arg)))
708 (unless fs (error "No file name to look for"))
709 (list old (read-file-name (format "File for %s: " (car fs))
710 nil (diff-find-file-name old) t))))
711 (let ((fs (diff-hunk-file-names old)))
712 (unless fs (error "No file name to look for"))
713 (push (cons fs name) diff-remembered-files-alist)))
715 (defun diff-hunk-file-names (&optional old)
716 "Give the list of file names textually mentioned for the current hunk."
718 (unless (looking-at diff-file-header-re)
719 (or (ignore-errors (diff-beginning-of-file))
720 (re-search-forward diff-file-header-re nil t)))
721 (let ((limit (save-excursion
723 (progn (diff-hunk-prev) (point))
724 (error (point-min)))))
726 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
727 (list (if old (match-string 1) (match-string 3))
728 (if old (match-string 3) (match-string 1)))
729 (forward-line 1) nil)))
734 (re-search-backward "^Index: \\(.+\\)" limit t)))
735 (list (match-string 1)))
737 (when (re-search-backward
738 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?"
740 (list (if old (match-string 2) (match-string 4))
741 (if old (match-string 4) (match-string 2)))))))))
743 (defun diff-find-file-name (&optional old batch prefix)
744 "Return the file corresponding to the current patch.
745 Non-nil OLD means that we want the old file.
746 Non-nil BATCH means to prefer returning an incorrect answer than to prompt
748 PREFIX is only used internally: don't use it."
749 (unless (equal diff-remembered-defdir default-directory)
750 ;; Flush diff-remembered-files-alist if the default-directory is changed.
751 (set (make-local-variable 'diff-remembered-defdir) default-directory)
752 (set (make-local-variable 'diff-remembered-files-alist) nil))
754 (unless (looking-at diff-file-header-re)
755 (or (ignore-errors (diff-beginning-of-file))
756 (re-search-forward diff-file-header-re nil t)))
757 (let ((fs (diff-hunk-file-names old)))
758 (if prefix (setq fs (mapcar (lambda (f) (concat prefix f)) fs)))
760 ;; use any previously used preference
761 (cdr (assoc fs diff-remembered-files-alist))
762 ;; try to be clever and use previous choices as an inspiration
763 (dolist (rf diff-remembered-files-alist)
764 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
765 (if (and newfile (file-exists-p newfile)) (return newfile))))
766 ;; look for each file in turn. If none found, try again but
767 ;; ignoring the first level of directory, ...
768 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
771 (setq file (do* ((files files (cdr files))
772 (file (car files) (car files)))
773 ;; Use file-regular-p to avoid
774 ;; /dev/null, directories, etc.
775 ((or (null file) (file-regular-p file))
778 ;; <foo>.rej patches implicitly apply to <foo>
779 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
780 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
781 (when (file-exists-p file) file)))
782 ;; If we haven't found the file, maybe it's because we haven't paid
783 ;; attention to the PCL-CVS hint.
785 (boundp 'cvs-pcl-cvs-dirchange-re)
787 (re-search-backward cvs-pcl-cvs-dirchange-re nil t))
788 (diff-find-file-name old batch (match-string 1)))
789 ;; Invent something, if necessary.
791 (or (car fs) default-directory))
792 ;; if all else fails, ask the user
793 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
794 nil (first fs) t (first fs))))
795 (set (make-local-variable 'diff-remembered-files-alist)
796 (cons (cons fs file) diff-remembered-files-alist))
800 (defun diff-ediff-patch ()
801 "Call `ediff-patch-file' on the current buffer."
804 (ediff-patch-file nil (current-buffer))
805 (wrong-number-of-arguments (ediff-patch-file))))
808 ;;;; Conversion functions
811 ;;(defvar diff-inhibit-after-change nil
812 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
814 (defun diff-unified->context (start end)
815 "Convert unified diffs to context diffs.
816 START and END are either taken from the region (if a prefix arg is given) or
817 else cover the whole buffer."
818 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
819 (list (region-beginning) (region-end))
820 (list (point-min) (point-max))))
821 (unless (markerp end) (setq end (copy-marker end t)))
822 (let (;;(diff-inhibit-after-change t)
823 (inhibit-read-only t))
826 (while (and (re-search-forward
827 (concat "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|"
828 diff-hunk-header-re-unified ".*\\)$")
831 (combine-after-change-calls
832 (if (match-beginning 2)
833 ;; we matched a file header
835 ;; use reverse order to make sure the indices are kept valid
836 (replace-match "---" t t nil 3)
837 (replace-match "***" t t nil 2))
838 ;; we matched a hunk header
839 (let ((line1 (match-string 4))
840 (lines1 (if (match-end 5)
841 (string-to-number (match-string 5)) 1))
842 (line2 (match-string 6))
843 (lines2 (if (match-end 7)
844 (string-to-number (match-string 7)) 1))
845 ;; Variables to use the special undo function.
846 (old-undo buffer-undo-list)
847 (old-end (marker-position end))
848 (start (match-beginning 0))
851 (concat "***************\n*** " line1 ","
852 (number-to-string (+ (string-to-number line1)
853 lines1 -1)) " ****"))
855 (narrow-to-region (line-beginning-position 2)
856 ;; Call diff-end-of-hunk from just before
857 ;; the hunk header so it can use the hunk
859 (progn (diff-end-of-hunk 'unified) (point)))
860 (let ((hunk (buffer-string)))
861 (goto-char (point-min))
862 (if (not (save-excursion (re-search-forward "^-" nil t)))
863 (delete-region (point) (point-max))
864 (goto-char (point-max))
865 (let ((modif nil) last-pt)
866 (while (progn (setq last-pt (point))
867 (= (forward-line -1) 0))
869 (?\s (insert " ") (setq modif nil) (backward-char 1))
870 (?+ (delete-region (point) last-pt) (setq modif t))
872 (progn (forward-char 1)
877 (?\\ (when (save-excursion (forward-line -1)
879 (delete-region (point) last-pt) (setq modif t)))
880 ;; diff-valid-unified-empty-line.
881 (?\n (insert " ") (setq modif nil) (backward-char 2))
882 (t (setq modif nil))))))
883 (goto-char (point-max))
885 (insert "--- " line2 ","
886 (number-to-string (+ (string-to-number line2)
889 ;;(goto-char (point-min))
891 (if (not (save-excursion (re-search-forward "^+" nil t)))
892 (delete-region (point) (point-max))
893 (let ((modif nil) (delete nil))
894 (if (save-excursion (re-search-forward "^\\+.*\n-" nil t))
895 ;; Normally, lines in a substitution come with
896 ;; first the removals and then the additions, and
897 ;; the context->unified function follows this
898 ;; convention, of course. Yet, other alternatives
899 ;; are valid as well, but they preclude the use of
900 ;; context->unified as an undo command.
901 (setq reversible nil))
904 (?\s (insert " ") (setq modif nil) (backward-char 1))
905 (?- (setq delete t) (setq modif t))
907 (progn (forward-char 1)
912 (?\\ (when (save-excursion (forward-line 1)
914 (setq delete t) (setq modif t)))
915 ;; diff-valid-unified-empty-line.
916 (?\n (insert " ") (setq modif nil) (backward-char 2)
917 (setq reversible nil))
918 (t (setq modif nil)))
919 (let ((last-pt (point)))
922 (delete-region last-pt (point))
923 (setq delete nil)))))))
924 (unless (or (not reversible) (eq buffer-undo-list t))
925 ;; Drop the many undo entries and replace them with
926 ;; a single entry that uses diff-context->unified to do
928 (setq buffer-undo-list
929 (cons (list 'apply (- old-end end) start (point-max)
930 'diff-context->unified start (point-max))
933 (defun diff-context->unified (start end &optional to-context)
934 "Convert context diffs to unified diffs.
935 START and END are either taken from the region
936 \(when it is highlighted) or else cover the whole buffer.
937 With a prefix argument, convert unified format to context format."
938 (interactive (if (and transient-mark-mode mark-active)
939 (list (region-beginning) (region-end) current-prefix-arg)
940 (list (point-min) (point-max) current-prefix-arg)))
942 (diff-unified->context start end)
943 (unless (markerp end) (setq end (copy-marker end t)))
944 (let ( ;;(diff-inhibit-after-change t)
945 (inhibit-read-only t))
948 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
950 (combine-after-change-calls
951 (if (match-beginning 2)
952 ;; we matched a file header
954 ;; use reverse order to make sure the indices are kept valid
955 (replace-match "+++" t t nil 3)
956 (replace-match "---" t t nil 2))
957 ;; we matched a hunk header
958 (let ((line1s (match-string 4))
959 (line1e (match-string 5))
960 (pt1 (match-beginning 0))
961 ;; Variables to use the special undo function.
962 (old-undo buffer-undo-list)
963 (old-end (marker-position end))
966 (unless (re-search-forward
967 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
968 (error "Can't find matching `--- n1,n2 ----' line"))
969 (let ((line2s (match-string 1))
970 (line2e (match-string 2))
972 (delete-region (progn (beginning-of-line) (point))
973 (progn (forward-line 1) (point)))
977 (while (< (point) pt2)
979 (?! (delete-char 2) (insert "-") (forward-line 1))
980 (?- (forward-char 1) (delete-char 1) (forward-line 1))
981 (?\s ;merge with the other half of the chunk
984 (goto-char pt2) (forward-line 1) (point))))
985 (case (char-after pt2)
988 (prog1 (buffer-substring (+ pt2 2) endline2)
989 (delete-region pt2 endline2))))
991 (unless (= (- endline2 pt2)
992 (- (line-beginning-position 2) (point)))
993 ;; If the two lines we're merging don't have the
994 ;; same length (can happen with "diff -b"), then
995 ;; diff-unified->context will not properly undo
997 (setq reversible nil))
998 (delete-region pt2 endline2)
1001 (?\\ (forward-line 1))
1002 (t (setq reversible nil)
1003 (delete-char 1) (forward-line 1)))))
1004 (t (setq reversible nil) (forward-line 1))))
1005 (while (looking-at "[+! ] ")
1006 (if (/= (char-after) ?!) (forward-char 1)
1007 (delete-char 1) (insert "+"))
1008 (delete-char 1) (forward-line 1))
1011 (insert "@@ -" line1s ","
1012 (number-to-string (- (string-to-number line1e)
1013 (string-to-number line1s)
1016 (number-to-string (- (string-to-number line2e)
1017 (string-to-number line2s)
1019 (set-marker pt2 nil)
1020 ;; The whole procedure succeeded, let's replace the myriad
1021 ;; of undo elements with just a single special one.
1022 (unless (or (not reversible) (eq buffer-undo-list t))
1023 (setq buffer-undo-list
1024 (cons (list 'apply (- old-end end) pt1 (point)
1025 'diff-unified->context pt1 (point))
1029 (defun diff-reverse-direction (start end)
1030 "Reverse the direction of the diffs.
1031 START and END are either taken from the region (if a prefix arg is given) or
1032 else cover the whole buffer."
1033 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
1034 (list (region-beginning) (region-end))
1035 (list (point-min) (point-max))))
1036 (unless (markerp end) (setq end (copy-marker end t)))
1037 (let (;;(diff-inhibit-after-change t)
1038 (inhibit-read-only t))
1041 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
1043 (combine-after-change-calls
1046 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
1047 ;; a context-diff hunk header
1048 ((match-beginning 6)
1049 (let ((pt-lines1 (match-beginning 6))
1050 (lines1 (match-string 6)))
1051 (replace-match "" nil nil nil 6)
1053 (let ((half1s (point)))
1054 (while (looking-at "[-! \\][ \t]\\|#")
1055 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
1057 (let ((half1 (delete-and-extract-region half1s (point))))
1058 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
1060 (error "Can't find matching `--- n1,n2 ----' line"))
1061 (let ((str1 (match-string 1)))
1062 (replace-match lines1 nil nil nil 1)
1064 (let ((half2s (point)))
1065 (while (looking-at "[!+ \\][ \t]\\|#")
1066 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
1068 (let ((half2 (delete-and-extract-region half2s (point))))
1069 (insert (or half1 ""))
1071 (insert (or half2 ""))))
1072 (goto-char pt-lines1)
1074 ;; a unified-diff hunk header
1075 ((match-beginning 7)
1076 (replace-match "@@ -\\8 +\\7 @@" nil)
1078 (let ((c (char-after)) first last)
1079 (while (case (setq c (char-after))
1080 (?- (setq first (or first (point)))
1081 (delete-char 1) (insert "+") t)
1082 (?+ (setq last (or last (point)))
1083 (delete-char 1) (insert "-") t)
1085 (t (when (and first last (< first last))
1086 (insert (delete-and-extract-region first last)))
1087 (setq first nil last nil)
1088 (memq c (if diff-valid-unified-empty-line
1089 '(?\s ?\n) '(?\s)))))
1090 (forward-line 1))))))))))
1092 (defun diff-fixup-modifs (start end)
1093 "Fixup the hunk headers (in case the buffer was modified).
1094 START and END are either taken from the region (if a prefix arg is given) or
1095 else cover the whole buffer."
1096 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
1097 (list (region-beginning) (region-end))
1098 (list (point-min) (point-max))))
1099 (let ((inhibit-read-only t))
1101 (goto-char end) (diff-end-of-hunk nil 'donttrustheader)
1102 (let ((plus 0) (minus 0) (space 0) (bang 0))
1103 (while (and (= (forward-line -1) 0) (<= start (point)))
1104 (if (not (looking-at
1105 (concat diff-hunk-header-re-unified
1106 "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$"
1107 "\\|--- .+\n\\+\\+\\+ ")))
1114 (t (setq space 0 plus 0 minus 0 bang 0)))
1116 ((looking-at diff-hunk-header-re-unified)
1117 (let* ((old1 (match-string 2))
1118 (old2 (match-string 4))
1119 (new1 (number-to-string (+ space minus)))
1120 (new2 (number-to-string (+ space plus))))
1122 (unless (string= new2 old2) (replace-match new2 t t nil 4))
1123 (goto-char (match-end 4)) (insert "," new2))
1125 (unless (string= new1 old1) (replace-match new1 t t nil 2))
1126 (goto-char (match-end 2)) (insert "," new1))))
1127 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
1128 (when (> (+ space bang plus) 0)
1129 (let* ((old1 (match-string 1))
1130 (old2 (match-string 2))
1131 (new (number-to-string
1132 (+ space bang plus -1 (string-to-number old1)))))
1133 (unless (string= new old2) (replace-match new t t nil 2)))))
1134 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
1135 (when (> (+ space bang minus) 0)
1136 (let* ((old (match-string 1))
1138 (concat "%0" (number-to-string (length old)) "d")
1139 (+ space bang minus -1 (string-to-number old)))))
1140 (unless (string= new old) (replace-match new t t nil 2))))))
1141 (setq space 0 plus 0 minus 0 bang 0)))))))
1147 (defun diff-write-contents-hooks ()
1148 "Fixup hunk headers if necessary."
1149 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
1152 ;; It turns out that making changes in the buffer from within an
1153 ;; *-change-function is asking for trouble, whereas making them
1154 ;; from a post-command-hook doesn't pose much problems
1155 (defvar diff-unhandled-changes nil)
1156 (defun diff-after-change-function (beg end len)
1157 "Remember to fixup the hunk header.
1158 See `after-change-functions' for the meaning of BEG, END and LEN."
1159 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
1160 ;; incorrect, but it turns out that inhibit-read-only is normally not set
1161 ;; inside editing commands, while it tends to be set when the buffer gets
1162 ;; updated by an async process or by a conversion function, both of which
1163 ;; would rather not be uselessly slowed down by this hook.
1164 (when (and (not undo-in-progress) (not inhibit-read-only))
1165 (if diff-unhandled-changes
1166 (setq diff-unhandled-changes
1167 (cons (min beg (car diff-unhandled-changes))
1168 (max end (cdr diff-unhandled-changes))))
1169 (setq diff-unhandled-changes (cons beg end)))))
1171 (defun diff-post-command-hook ()
1172 "Fixup hunk headers if necessary."
1173 (when (consp diff-unhandled-changes)
1176 (goto-char (car diff-unhandled-changes))
1177 ;; Maybe we've cut the end of the hunk before point.
1178 (if (and (bolp) (not (bobp))) (backward-char 1))
1179 ;; We used to fixup modifs on all the changes, but it turns out
1180 ;; that it's safer not to do it on big changes, for example
1181 ;; when yanking a big diff, since we might then screw up perfectly
1182 ;; correct values. -stef
1183 ;; (unless (ignore-errors
1184 ;; (diff-beginning-of-hunk)
1186 ;; (diff-end-of-hunk)
1187 ;; (> (point) (car diff-unhandled-changes))))
1188 ;; (goto-char (car diff-unhandled-changes))
1189 ;; (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
1190 ;; (diff-beginning-of-hunk))
1191 ;; (diff-fixup-modifs (point) (cdr diff-unhandled-changes))
1192 (diff-beginning-of-hunk)
1193 (when (save-excursion
1194 (diff-end-of-hunk nil 'donttrustheader)
1195 (>= (point) (cdr diff-unhandled-changes)))
1196 (diff-fixup-modifs (point) (cdr diff-unhandled-changes)))))
1197 (setq diff-unhandled-changes nil)))
1199 (defun diff-next-error (arg reset)
1200 ;; Select a window that displays the current buffer so that point
1201 ;; movements are reflected in that window. Otherwise, the user might
1202 ;; never see the hunk corresponding to the source she's jumping to.
1203 (pop-to-buffer (current-buffer))
1204 (if reset (goto-char (point-min)))
1205 (diff-hunk-next arg)
1209 (define-derived-mode diff-mode fundamental-mode "Diff"
1210 "Major mode for viewing/editing context diffs.
1211 Supports unified and context diffs as well as (to a lesser extent)
1214 When the buffer is read-only, the ESC prefix is not necessary.
1215 If you edit the buffer manually, diff-mode will try to update the hunk
1216 headers for you on-the-fly.
1218 You can also switch between context diff and unified diff with \\[diff-context->unified],
1219 or vice versa with \\[diff-unified->context] and you can also reverse the direction of
1220 a diff with \\[diff-reverse-direction].
1224 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
1225 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
1226 (set (make-local-variable 'imenu-generic-expression)
1227 diff-imenu-generic-expression)
1228 ;; These are not perfect. They would be better done separately for
1229 ;; context diffs and unidiffs.
1230 ;; (set (make-local-variable 'paragraph-start)
1231 ;; (concat "@@ " ; unidiff hunk
1232 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
1233 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
1234 ;; ; start (first or second line)
1235 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
1236 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
1238 (set (make-local-variable 'next-error-function) 'diff-next-error)
1240 (setq buffer-read-only diff-default-read-only)
1241 ;; setup change hooks
1242 (if (not diff-update-on-the-fly)
1243 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
1244 (make-local-variable 'diff-unhandled-changes)
1245 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1246 (add-hook 'post-command-hook 'diff-post-command-hook nil t))
1247 ;; Neat trick from Dave Love to add more bindings in read-only mode:
1248 (lexical-let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
1249 (add-to-list 'minor-mode-overriding-map-alist ro-bind)
1250 ;; Turn off this little trick in case the buffer is put in view-mode.
1251 (add-hook 'view-mode-hook
1253 (setq minor-mode-overriding-map-alist
1254 (delq ro-bind minor-mode-overriding-map-alist)))
1257 (set (make-local-variable 'add-log-current-defun-function)
1258 'diff-current-defun)
1259 (set (make-local-variable 'add-log-buffer-file-name-function)
1260 'diff-find-file-name))
1263 (define-minor-mode diff-minor-mode
1264 "Minor mode for viewing/editing context diffs.
1265 \\{diff-minor-mode-map}"
1266 :group 'diff-mode :lighter " Diff"
1267 ;; FIXME: setup font-lock
1268 ;; setup change hooks
1269 (if (not diff-update-on-the-fly)
1270 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
1271 (make-local-variable 'diff-unhandled-changes)
1272 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1273 (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
1275 ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1277 (defun diff-delete-if-empty ()
1278 ;; An empty diff file means there's no more diffs to integrate, so we
1279 ;; can just remove the file altogether. Very handy for .rej files if we
1280 ;; remove hunks as we apply them.
1281 (when (and buffer-file-name
1282 (eq 0 (nth 7 (file-attributes buffer-file-name))))
1283 (delete-file buffer-file-name)))
1285 (defun diff-delete-empty-files ()
1286 "Arrange for empty diff files to be removed."
1287 (add-hook 'after-save-hook 'diff-delete-if-empty nil t))
1289 (defun diff-make-unified ()
1290 "Turn context diffs into unified diffs if applicable."
1292 (goto-char (point-min))
1293 (and (looking-at diff-hunk-header-re) (eq (char-after) ?*)))
1294 (let ((mod (buffer-modified-p)))
1296 (diff-context->unified (point-min) (point-max))
1297 (restore-buffer-modified-p mod)))))
1300 ;;; Misc operations that have proved useful at some point.
1303 (defun diff-next-complex-hunk ()
1304 "Jump to the next \"complex\" hunk.
1305 \"Complex\" is approximated by \"the hunk changes the number of lines\".
1306 Only works for unified diffs."
1309 (and (re-search-forward diff-hunk-header-re-unified nil t)
1310 (equal (match-string 2) (match-string 4)))))
1312 (defun diff-sanity-check-context-hunk-half (lines)
1313 (let ((count lines))
1316 ((and (memq (char-after) '(?\s ?! ?+ ?-))
1317 (memq (char-after (1+ (point))) '(?\s ?\t)))
1319 ((or (zerop count) (= count lines)) nil)
1320 ((memq (char-after) '(?! ?+ ?-))
1321 (if (not (and (eq (char-after (1+ (point))) ?\n)
1322 (y-or-n-p "Try to auto-fix whitespace loss damage? ")))
1323 (error "End of hunk ambiguously marked")
1324 (forward-char 1) (insert " ") (forward-line -1) t))
1326 (error "End of hunk ambiguously marked"))
1327 ((not (y-or-n-p "Try to auto-fix whitespace loss and word-wrap damage? "))
1329 ((eolp) (insert " ") (forward-line -1) t)
1330 (t (insert " ") (delete-region (- (point) 2) (- (point) 1)) t))
1333 (defun diff-sanity-check-hunk ()
1334 (let (;; Every modification is protected by a y-or-n-p, so it's probably
1335 ;; OK to override a read-only setting.
1336 (inhibit-read-only t))
1339 ((not (looking-at diff-hunk-header-re))
1340 (error "Not recognizable hunk header"))
1343 ((eq (char-after) ?*)
1344 (if (not (looking-at "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\*\\*\\*\\*"))
1345 (error "Unrecognized context diff first hunk header format")
1347 (diff-sanity-check-context-hunk-half
1349 (1+ (- (string-to-number (match-string 2))
1350 (string-to-number (match-string 1))))
1352 (if (not (looking-at "--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$"))
1353 (error "Unrecognized context diff second hunk header format")
1355 (diff-sanity-check-context-hunk-half
1357 (1+ (- (string-to-number (match-string 2))
1358 (string-to-number (match-string 1))))
1362 ((eq (char-after) ?@)
1363 (if (not (looking-at diff-hunk-header-re-unified))
1364 (error "Unrecognized unified diff hunk header format")
1365 (let ((before (if (match-end 2)
1366 (string-to-number (match-string 2)) 1))
1367 (after (if (match-end 4)
1368 (string-to-number (match-string 4)) 1)))
1372 (?\s (decf before) (decf after) t)
1374 (if (and (looking-at diff-file-header-re)
1375 (zerop before) (zerop after))
1376 ;; No need to query: this is a case where two patches
1377 ;; are concatenated and only counting the lines will
1378 ;; give the right result. Let's just add an empty
1379 ;; line so that our code which doesn't count lines
1380 ;; will not get confused.
1381 (progn (save-excursion (insert "\n")) nil)
1386 ((and diff-valid-unified-empty-line
1387 ;; Not just (eolp) so we don't infloop at eob.
1388 (eq (char-after) ?\n)
1389 (> before 0) (> after 0))
1390 (decf before) (decf after) t)
1391 ((and (zerop before) (zerop after)) nil)
1392 ((or (< before 0) (< after 0))
1393 (error (if (or (zerop before) (zerop after))
1394 "End of hunk ambiguously marked"
1395 "Hunk seriously messed up")))
1396 ((not (y-or-n-p (concat "Try to auto-fix " (if (eolp) "whitespace loss" "word-wrap damage") "? ")))
1398 ((eolp) (insert " ") (forward-line -1) t)
1400 (delete-region (- (point) 2) (- (point) 1)) t))))
1408 (defun diff-hunk-text (hunk destp char-offset)
1409 "Return the literal source text from HUNK as (TEXT . OFFSET).
1410 If DESTP is nil, TEXT is the source, otherwise the destination text.
1411 CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding
1412 char-offset in TEXT."
1415 (goto-char (point-min))
1420 ;; Set the following variables:
1421 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
1422 ;; DST-POS buffer pos of the destination part of the hunk or nil
1423 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
1424 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
1425 (cond ((looking-at "^@@")
1427 (setq num-pfx-chars 1)
1429 (setq src-pos (point) dst-pos (point)))
1430 ((looking-at "^\\*\\*")
1433 (setq src-pos (point))
1434 (re-search-forward "^--- " nil t)
1436 (setq divider-pos (point))
1438 (setq dst-pos (point)))
1439 ((looking-at "^[0-9]+a[0-9,]+$")
1440 ;; normal diff, insert
1442 (setq dst-pos (point)))
1443 ((looking-at "^[0-9,]+d[0-9]+$")
1444 ;; normal diff, delete
1446 (setq src-pos (point)))
1447 ((looking-at "^[0-9,]+c[0-9,]+$")
1448 ;; normal diff, change
1450 (setq src-pos (point))
1451 (re-search-forward "^---$" nil t)
1453 (setq divider-pos (point))
1455 (setq dst-pos (point)))
1457 (error "Unknown diff hunk type")))
1459 (if (if destp (null dst-pos) (null src-pos))
1460 ;; Implied empty text
1461 (if char-offset '("" . 0) "")
1463 ;; For context diffs, either side can be empty, (if there's only
1464 ;; added or only removed text). We should then use the other side.
1465 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
1466 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
1468 (when char-offset (goto-char (+ (point-min) char-offset)))
1470 ;; Get rid of anything except the desired text.
1472 ;; Delete unused text region
1473 (let ((keep (if destp dst-pos src-pos)))
1474 (when (and divider-pos (> divider-pos keep))
1475 (delete-region divider-pos (point-max)))
1476 (delete-region (point-min) keep))
1477 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1478 (let ((kill-char (if destp ?- ?+)))
1479 (goto-char (point-min))
1481 (if (eq (char-after) kill-char)
1482 (delete-region (point) (progn (forward-line 1) (point)))
1483 (delete-char num-pfx-chars)
1484 (forward-line 1)))))
1486 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1487 (if char-offset (cons text (- (point) (point-min))) text))))))
1490 (defun diff-find-text (text)
1491 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1492 If TEXT isn't found, nil is returned."
1493 (let* ((orig (point))
1494 (forw (and (search-forward text nil t)
1495 (cons (match-beginning 0) (match-end 0))))
1496 (back (and (goto-char (+ orig (length text)))
1497 (search-backward text nil t)
1498 (cons (match-beginning 0) (match-end 0)))))
1499 ;; Choose the closest match.
1501 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1504 (defun diff-find-approx-text (text)
1505 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1506 Whitespace differences are ignored."
1507 (let* ((orig (point))
1508 (re (concat "^[ \t\n\f]*"
1509 (mapconcat 'regexp-quote (split-string text) "[ \t\n\f]+")
1511 (forw (and (re-search-forward re nil t)
1512 (cons (match-beginning 0) (match-end 0))))
1513 (back (and (goto-char (+ orig (length text)))
1514 (re-search-backward re nil t)
1515 (cons (match-beginning 0) (match-end 0)))))
1516 ;; Choose the closest match.
1518 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1521 (defsubst diff-xor (a b) (if a (if (not b) a) b))
1523 (defun diff-find-source-location (&optional other-file reverse)
1524 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
1525 BUF is the buffer corresponding to the source file.
1526 LINE-OFFSET is the offset between the expected and actual positions
1527 of the text of the hunk or nil if the text was not found.
1528 POS is a pair (BEG . END) indicating the position of the text in the buffer.
1529 SRC and DST are the two variants of text as returned by `diff-hunk-text'.
1530 SRC is the variant that was found in the buffer.
1531 SWITCHED is non-nil if the patch is already applied."
1533 (let* ((other (diff-xor other-file diff-jump-to-old-file))
1534 (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1536 ;; Check that the hunk is well-formed. Otherwise diff-mode and
1537 ;; the user may disagree on what constitutes the hunk
1538 ;; (e.g. because an empty line truncates the hunk mid-course),
1539 ;; leading to potentially nasty surprises for the user.
1540 (_ (diff-sanity-check-hunk))
1541 (hunk (buffer-substring (point)
1542 (save-excursion (diff-end-of-hunk) (point))))
1543 (old (diff-hunk-text hunk reverse char-offset))
1544 (new (diff-hunk-text hunk (not reverse) char-offset))
1545 ;; Find the location specification.
1546 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
1547 (error "Can't find the hunk header")
1548 (if other (match-string 1)
1549 (if (match-end 3) (match-string 3)
1550 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
1551 (error "Can't find the hunk separator"))
1552 (match-string 1)))))
1553 (file (or (diff-find-file-name other) (error "Can't find the file")))
1554 (buf (find-file-noselect file)))
1555 ;; Update the user preference if he so wished.
1556 (when (> (prefix-numeric-value other-file) 8)
1557 (setq diff-jump-to-old-file other))
1558 (with-current-buffer buf
1559 (goto-line (string-to-number line))
1560 (let* ((orig-pos (point))
1562 ;; FIXME: Check for case where both OLD and NEW are found.
1563 (pos (or (diff-find-text (car old))
1564 (progn (setq switched t) (diff-find-text (car new)))
1565 (progn (setq switched nil)
1567 (diff-find-approx-text (car old))
1568 (invalid-regexp nil))) ;Regex too big.
1569 (progn (setq switched t)
1571 (diff-find-approx-text (car new))
1572 (invalid-regexp nil))) ;Regex too big.
1573 (progn (setq switched nil) nil))))
1577 (list (count-lines orig-pos (car pos)) pos)
1578 (list nil (cons orig-pos (+ orig-pos (length (car old))))))
1579 (if switched (list new old t) (list old new))))))))
1582 (defun diff-hunk-status-msg (line-offset reversed dry-run)
1583 (let ((msg (if dry-run
1584 (if reversed "already applied" "not yet applied")
1585 (if reversed "undone" "applied"))))
1586 (message (cond ((null line-offset) "Hunk text not found")
1587 ((= line-offset 0) "Hunk %s")
1588 ((= line-offset 1) "Hunk %s at offset %d line")
1589 (t "Hunk %s at offset %d lines"))
1592 (defvar diff-apply-hunk-to-backup-file nil)
1594 (defun diff-apply-hunk (&optional reverse)
1595 "Apply the current hunk to the source file and go to the next.
1596 By default, the new source file is patched, but if the variable
1597 `diff-jump-to-old-file' is non-nil, then the old source file is
1598 patched instead (some commands, such as `diff-goto-source' can change
1599 the value of this variable when given an appropriate prefix argument).
1601 With a prefix argument, REVERSE the hunk."
1603 (destructuring-bind (buf line-offset pos old new &optional switched)
1604 ;; Sometimes we'd like to have the following behavior: if REVERSE go
1605 ;; to the new file, otherwise go to the old. But that means that by
1606 ;; default we use the old file, which is the opposite of the default
1607 ;; for diff-goto-source, and is thus confusing. Also when you don't
1608 ;; know about it it's pretty surprising.
1609 ;; TODO: make it possible to ask explicitly for this behavior.
1611 ;; This is duplicated in diff-test-hunk.
1612 (diff-find-source-location nil reverse)
1615 (error "Can't find the text to patch"))
1616 ((with-current-buffer buf
1617 (and buffer-file-name
1618 (backup-file-name-p buffer-file-name)
1619 (not diff-apply-hunk-to-backup-file)
1620 (not (set (make-local-variable 'diff-apply-hunk-to-backup-file)
1621 (yes-or-no-p (format "Really apply this hunk to %s? "
1622 (file-name-nondirectory
1623 buffer-file-name)))))))
1625 (substitute-command-keys
1626 (format "Use %s\\[diff-apply-hunk] to apply it to the other file"
1627 (if (not reverse) "\\[universal-argument] ")))))
1629 ;; A reversed patch was detected, perhaps apply it in reverse.
1630 (not (save-window-excursion
1632 (goto-char (+ (car pos) (cdr old)))
1635 "Hunk hasn't been applied yet; apply it now? "
1636 "Hunk has already been applied; undo it? ")))))
1637 (message "(Nothing done)"))
1640 (with-current-buffer buf
1641 (goto-char (car pos))
1642 (delete-region (car pos) (cdr pos))
1644 ;; Display BUF in a window
1645 (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
1646 (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
1647 (when diff-advance-after-apply-hunk
1648 (diff-hunk-next))))))
1651 (defun diff-test-hunk (&optional reverse)
1652 "See whether it's possible to apply the current hunk.
1653 With a prefix argument, try to REVERSE the hunk."
1655 (destructuring-bind (buf line-offset pos src dst &optional switched)
1656 (diff-find-source-location nil reverse)
1657 (set-window-point (display-buffer buf) (+ (car pos) (cdr src)))
1658 (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
1661 (defalias 'diff-mouse-goto-source 'diff-goto-source)
1663 (defun diff-goto-source (&optional other-file event)
1664 "Jump to the corresponding source line.
1665 `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
1666 is given) determines whether to jump to the old or the new file.
1667 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1668 then `diff-jump-to-old-file' is also set, for the next invocations."
1669 (interactive (list current-prefix-arg last-input-event))
1670 ;; When pointing at a removal line, we probably want to jump to
1671 ;; the old location, and else to the new (i.e. as if reverting).
1672 ;; This is a convenient detail when using smerge-diff.
1673 (if event (posn-set-point (event-end event)))
1674 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1675 (destructuring-bind (buf line-offset pos src dst &optional switched)
1676 (diff-find-source-location other-file rev)
1678 (goto-char (+ (car pos) (cdr src)))
1679 (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
1682 (defun diff-current-defun ()
1683 "Find the name of function at point.
1684 For use in `add-log-current-defun-function'."
1685 ;; Kill change-log-default-name so it gets recomputed each time, since
1686 ;; each hunk may belong to another file which may belong to another
1687 ;; directory and hence have a different ChangeLog file.
1688 (kill-local-variable 'change-log-default-name)
1690 (when (looking-at diff-hunk-header-re)
1692 (re-search-forward "^[^ ]" nil t))
1693 (destructuring-bind (buf line-offset pos src dst &optional switched)
1694 (diff-find-source-location)
1696 (or (when (memq (char-after) '(?< ?-))
1697 ;; Cursor is pointing at removed text. This could be a removed
1698 ;; function, in which case, going to the source buffer will
1699 ;; not help since the function is now removed. Instead,
1700 ;; try to figure out the function name just from the code-fragment.
1701 (let ((old (if switched dst src)))
1704 (funcall (with-current-buffer buf major-mode))
1705 (goto-char (+ (point-min) (cdr old)))
1706 (add-log-current-defun))))
1707 (with-current-buffer buf
1708 (goto-char (+ (car pos) (cdr src)))
1709 (add-log-current-defun))))))
1711 (defun diff-ignore-whitespace-hunk ()
1712 "Re-diff the current hunk, ignoring whitespace differences."
1714 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1716 (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b")))
1717 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
1718 (error "Can't find line number"))
1719 (string-to-number (match-string 1))))
1720 (inhibit-read-only t)
1721 (hunk (delete-and-extract-region
1722 (point) (save-excursion (diff-end-of-hunk) (point))))
1723 (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
1724 (file1 (make-temp-file "diff1"))
1725 (file2 (make-temp-file "diff2"))
1726 (coding-system-for-read buffer-file-coding-system)
1730 (setq old (diff-hunk-text hunk nil char-offset))
1731 (setq new (diff-hunk-text hunk t char-offset))
1732 (write-region (concat lead (car old)) nil file1 nil 'nomessage)
1733 (write-region (concat lead (car new)) nil file2 nil 'nomessage)
1736 (call-process diff-command nil t nil
1739 (0 nil) ;Nothing to reformat.
1740 (1 (goto-char (point-min))
1741 ;; Remove the file-header.
1742 (when (re-search-forward diff-hunk-header-re nil t)
1743 (delete-region (point-min) (match-beginning 0))))
1744 (t (goto-char (point-max))
1745 (unless (bolp) (insert "\n"))
1747 (setq hunk (buffer-string))
1748 (unless (memq status '(0 1))
1749 (error "Diff returned: %s" status)))))
1750 ;; Whatever happens, put back some equivalent text: either the new
1751 ;; one or the original one in case some error happened.
1754 (delete-file file2))))
1756 ;;; Fine change highlighting.
1758 (defface diff-refine-change
1759 '((((class color) (min-colors 88) (background light))
1760 :background "grey85")
1761 (((class color) (min-colors 88) (background dark))
1762 :background "grey60")
1763 (((class color) (background light))
1764 :background "yellow")
1765 (((class color) (background dark))
1766 :background "green")
1768 "Face used for char-based changes shown by `diff-refine-hunk'."
1771 (defun diff-refine-preproc ()
1772 (while (re-search-forward "^[+>]" nil t)
1773 ;; Remove spurious changes due to the fact that one side of the hunk is
1774 ;; marked with leading + or > and the other with leading - or <.
1775 ;; We used to replace all the prefix chars with " " but this only worked
1776 ;; when we did char-based refinement (or when using
1777 ;; smerge-refine-weight-hack) since otherwise, the `forward' motion done
1778 ;; in chopup do not necessarily do the same as the ones in highlight
1779 ;; since the "_" is not treated the same as " ".
1780 (replace-match (cdr (assq (char-before) '((?+ . "-") (?> . "<"))))))
1783 (defun diff-refine-hunk ()
1784 "Highlight changes of hunk at point at a finer granularity."
1786 (eval-and-compile (require 'smerge-mode))
1788 (diff-beginning-of-hunk 'try-harder)
1789 (let* ((style (diff-hunk-style)) ;Skips the hunk header as well.
1791 (props '((diff-mode . fine) (face diff-refine-change)))
1792 (end (progn (diff-end-of-hunk) (point))))
1794 (remove-overlays beg end 'diff-mode 'fine)
1799 (while (re-search-forward "^\\(?:-.*\n\\)+\\(\\)\\(?:\\+.*\n\\)+"
1801 (smerge-refine-subst (match-beginning 0) (match-end 1)
1802 (match-end 1) (match-end 0)
1803 props 'diff-refine-preproc)))
1805 (let* ((middle (save-excursion (re-search-forward "^---")))
1807 (while (re-search-forward "^\\(?:!.*\n\\)+" middle t)
1808 (smerge-refine-subst (match-beginning 0) (match-end 0)
1811 (re-search-forward "^\\(?:!.*\n\\)+" end)
1812 (setq other (match-end 0))
1813 (match-beginning 0))
1815 props 'diff-refine-preproc))))
1817 (let ((beg1 (1+ (point))))
1818 (when (re-search-forward "^---.*\n" end t)
1819 ;; It's a combined add&remove, so there's something to do.
1820 (smerge-refine-subst beg1 (match-beginning 0)
1822 props 'diff-refine-preproc))))))))
1825 (defun diff-add-change-log-entries-other-window ()
1826 "Iterate through the current diff and create ChangeLog entries.
1827 I.e. like `add-change-log-entry-other-window' but applied to all hunks."
1829 ;; XXX: Currently add-change-log-entry-other-window is only called
1830 ;; once per hunk. Some hunks have multiple changes, it would be
1831 ;; good to call it for each change.
1833 (goto-char (point-min))
1834 (let ((orig-buffer (current-buffer)))
1836 ;; Call add-change-log-entry-other-window for each hunk in
1840 ;; Move to where the changes are,
1841 ;; `add-change-log-entry-other-window' works better in
1845 ;; If the hunk is a context hunk with an empty first
1846 ;; half, recognize the "--- NNN,MMM ----" line
1847 "\\(-- [0-9]+\\(,[0-9]+\\)? ----\n"
1848 ;; and skip to the next non-context line.
1849 "\\( .*\n\\)*[+]\\)?")
1852 (add-change-log-entry nil nil t t)))
1853 ;; When there's no more hunks, diff-hunk-next signals an error.
1856 ;; provide the package
1857 (provide 'diff-mode)
1859 ;;; Old Change Log from when diff-mode wasn't part of Emacs:
1860 ;; Revision 1.11 1999/10/09 23:38:29 monnier
1861 ;; (diff-mode-load-hook): dropped.
1862 ;; (auto-mode-alist): also catch *.diffs.
1863 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
1864 ;; for *.rej files (that lack any file name indication).
1866 ;; Revision 1.10 1999/09/30 15:32:11 monnier
1867 ;; added support for "\ No newline at end of file".
1869 ;; Revision 1.9 1999/09/15 00:01:13 monnier
1870 ;; - added basic `compile' support.
1871 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
1872 ;; - diff-kill-file now tries to kill the leading garbage as well.
1874 ;; Revision 1.8 1999/09/13 21:10:09 monnier
1875 ;; - don't use CL in the autoloaded code
1876 ;; - accept diffs using -T
1878 ;; Revision 1.7 1999/09/05 20:53:03 monnier
1879 ;; interface to ediff-patch
1881 ;; Revision 1.6 1999/09/01 20:55:13 monnier
1882 ;; (ediff=patch-file): add bindings to call ediff-patch.
1883 ;; (diff-find-file-name): taken out of diff-goto-source.
1884 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
1885 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
1887 ;; Revision 1.5 1999/08/31 19:18:52 monnier
1888 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
1890 ;; Revision 1.4 1999/08/31 13:01:44 monnier
1891 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
1894 ;; arch-tag: 2571d7ff-bc28-4cf9-8585-42e21890be66
1895 ;;; diff-mode.el ends here