]> code.delx.au - gnu-emacs/blob - lisp/diff-mode.el
(compile_pattern_1): Treat non-nil and non-string of
[gnu-emacs] / lisp / diff-mode.el
1 ;;; diff-mode.el --- a mode for viewing/editing context diffs
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: convenience patch diff
8
9 ;; This file is part of GNU Emacs.
10
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.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
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.
25
26 ;;; Commentary:
27
28 ;; Provides support for font-lock, outline, navigation
29 ;; commands, editing and various conversions as well as jumping
30 ;; to the corresponding source file.
31
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
35
36 ;; Bugs:
37
38 ;; - Reverse doesn't work with normal diffs.
39
40 ;; Todo:
41
42 ;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
43 ;; Also allow C-c C-a to delete already-applied hunks.
44 ;;
45 ;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location
46 ;; of a hunk. Show then the changes between <file> and <hunk> and make it
47 ;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>.
48 ;; Or maybe just make it into a ".rej to diff3-markers converter".
49 ;; Maybe just use `wiggle' (by Neil Brown) to do it for us.
50 ;;
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.
56
57 ;;; Code:
58 (eval-when-compile (require 'cl))
59
60 (defvar add-log-buffer-file-name-function)
61
62
63 (defgroup diff-mode ()
64 "Major mode for viewing/editing diffs."
65 :version "21.1"
66 :group 'tools
67 :group 'diff)
68
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)
73
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)
79
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)
89
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)
94
95
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)
101
102 (defvar diff-outline-regexp
103 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
104
105 ;;;;
106 ;;;; keymap, menu, ...
107 ;;;;
108
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.")
145
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'.")
163
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]
186 ))
187
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)
192
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'.")
196
197
198 ;;;;
199 ;;;; font-lock support
200 ;;;;
201
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)
217
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)
233
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)
241
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)
249
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)
257
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)
265
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)
276
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)
283
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)
290
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)
297
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)
305
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)
313
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)
321
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)))))))
343
344 (defconst diff-hunk-header-re-unified
345 "^@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@")
346
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))))
371
372 (defconst diff-font-lock-defaults
373 '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
374
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
381
382 ;;;;
383 ;;;; Movement
384 ;;;;
385
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")
391
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)
396
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 (let* ((nold (string-to-number (match-string 2)))
406 (nnew (string-to-number (match-string 4)))
407 (endold
408 (save-excursion
409 (re-search-forward (if diff-valid-unified-empty-line
410 "^[- \n]" "^[- ]")
411 nil t nold)
412 (line-beginning-position 2)))
413 (endnew
414 ;; The hunk may end with a bunch of "+" lines, so the `end' is
415 ;; then further than computed above.
416 (save-excursion
417 (re-search-forward (if diff-valid-unified-empty-line
418 "^[+ \n]" "^[+ ]")
419 nil t nnew)
420 (line-beginning-position 2))))
421 (setq end (max endold endnew)))))
422 ;; We may have a first evaluation of `end' thanks to the hunk header.
423 (unless end
424 (setq end (and (re-search-forward
425 (case style
426 (unified (concat (if diff-valid-unified-empty-line
427 "^[^-+# \\\n]\\|" "^[^-+# \\]\\|")
428 ;; A `unified' header is ambiguous.
429 diff-file-header-re))
430 (context "^[^-+#! \\]")
431 (normal "^[^<>#\\]")
432 (t "^[^-+#!<> \\]"))
433 nil t)
434 (match-beginning 0)))
435 (when diff-valid-unified-empty-line
436 ;; While empty lines may be valid inside hunks, they are also likely
437 ;; to be unrelated to the hunk.
438 (goto-char (or end (point-max)))
439 (while (eq ?\n (char-before (1- (point))))
440 (forward-char -1)
441 (setq end (point)))))
442 ;; The return value is used by easy-mmode-define-navigation.
443 (goto-char (or end (point-max)))))
444
445 (defun diff-beginning-of-hunk (&optional try-harder)
446 "Move back to beginning of hunk.
447 If TRY-HARDER is non-nil, try to cater to the case where we're not in a hunk
448 but in the file header instead, in which case move forward to the first hunk."
449 (beginning-of-line)
450 (unless (looking-at diff-hunk-header-re)
451 (forward-line 1)
452 (condition-case ()
453 (re-search-backward diff-hunk-header-re)
454 (error
455 (if (not try-harder)
456 (error "Can't find the beginning of the hunk")
457 (diff-beginning-of-file-and-junk)
458 (diff-hunk-next))))))
459
460 (defun diff-beginning-of-file ()
461 (beginning-of-line)
462 (unless (looking-at diff-file-header-re)
463 (forward-line 2)
464 (condition-case ()
465 (re-search-backward diff-file-header-re)
466 (error (error "Can't find the beginning of the file")))))
467
468 (defun diff-end-of-file ()
469 (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
470 (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re)
471 nil 'move)
472 (if (match-beginning 1)
473 (goto-char (match-beginning 1))
474 (beginning-of-line)))
475
476 ;; Define diff-{hunk,file}-{prev,next}
477 (easy-mmode-define-navigation
478 diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view)
479 (easy-mmode-define-navigation
480 diff-file diff-file-header-re "file" diff-end-of-hunk)
481
482 (defun diff-restrict-view (&optional arg)
483 "Restrict the view to the current hunk.
484 If the prefix ARG is given, restrict the view to the current file instead."
485 (interactive "P")
486 (save-excursion
487 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk 'try-harder))
488 (narrow-to-region (point)
489 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
490 (point)))
491 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
492
493
494 (defun diff-hunk-kill ()
495 "Kill current hunk."
496 (interactive)
497 (diff-beginning-of-hunk)
498 (let* ((start (point))
499 (nexthunk (when (re-search-forward diff-hunk-header-re nil t)
500 (match-beginning 0)))
501 (firsthunk (ignore-errors
502 (goto-char start)
503 (diff-beginning-of-file) (diff-hunk-next) (point)))
504 (nextfile (ignore-errors (diff-file-next) (point)))
505 (inhibit-read-only t))
506 (goto-char start)
507 (if (and firsthunk (= firsthunk start)
508 (or (null nexthunk)
509 (and nextfile (> nexthunk nextfile))))
510 ;; It's the only hunk for this file, so kill the file.
511 (diff-file-kill)
512 (diff-end-of-hunk)
513 (kill-region start (point)))))
514
515 (defconst diff-file-junk-re "diff \\|index ") ; "index " is output by git-diff.
516
517 (defun diff-beginning-of-file-and-junk ()
518 "Go to the beginning of file-related diff-info.
519 This is like `diff-beginning-of-file' except it tries to skip back over leading
520 data such as \"Index: ...\" and such."
521 (let ((orig (point))
522 ;; Skip forward over what might be "leading junk" so as to get
523 ;; closer to the actual diff.
524 (_ (progn (beginning-of-line)
525 (while (looking-at diff-file-junk-re)
526 (forward-line 1))))
527 (start (point))
528 (file (condition-case err (progn (diff-beginning-of-file) (point))
529 (error err)))
530 ;; prevhunk is one of the limits.
531 (prevhunk (save-excursion (ignore-errors (diff-hunk-prev) (point))))
532 err)
533 (when (consp file)
534 ;; Presumably, we started before the file header, in the leading junk.
535 (setq err file)
536 (diff-file-next)
537 (setq file (point)))
538 (let ((index (save-excursion
539 (forward-line 1) ;In case we're looking at "Index:".
540 (re-search-backward "^Index: " prevhunk t))))
541 (when index (setq file index))
542 (if (<= file start)
543 (progn
544 (goto-char file)
545 ;; Now skip backward over the leading junk we may have before the
546 ;; diff itself.
547 (while (save-excursion
548 (and (zerop (forward-line -1))
549 (looking-at diff-file-junk-re)))
550 (forward-line -1)))
551 ;; File starts *after* the starting point: we really weren't in
552 ;; a file diff but elsewhere.
553 (goto-char orig)
554 (signal (car err) (cdr err))))))
555
556 (defun diff-file-kill ()
557 "Kill current file's hunks."
558 (interactive)
559 (let ((orig (point))
560 (start (progn (diff-beginning-of-file-and-junk) (point)))
561 (inhibit-read-only t))
562 (diff-end-of-file)
563 (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
564 (if (> orig (point)) (error "Not inside a file diff"))
565 (kill-region start (point))))
566
567 (defun diff-kill-junk ()
568 "Kill spurious empty diffs."
569 (interactive)
570 (save-excursion
571 (let ((inhibit-read-only t))
572 (goto-char (point-min))
573 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
574 "\\([^-+!* <>].*\n\\)*?"
575 "\\(\\(Index:\\) \\|"
576 diff-file-header-re "\\)")
577 nil t)
578 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
579 (match-beginning 3))
580 (beginning-of-line)))))
581
582 (defun diff-count-matches (re start end)
583 (save-excursion
584 (let ((n 0))
585 (goto-char start)
586 (while (re-search-forward re end t) (incf n))
587 n)))
588
589 (defun diff-split-hunk ()
590 "Split the current (unified diff) hunk at point into two hunks."
591 (interactive)
592 (beginning-of-line)
593 (let ((pos (point))
594 (start (progn (diff-beginning-of-hunk) (point))))
595 (unless (looking-at diff-hunk-header-re-unified)
596 (error "diff-split-hunk only works on unified context diffs"))
597 (forward-line 1)
598 (let* ((start1 (string-to-number (match-string 1)))
599 (start2 (string-to-number (match-string 3)))
600 (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
601 (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos)))
602 (inhibit-read-only t))
603 (goto-char pos)
604 ;; Hopefully the after-change-function will not screw us over.
605 (insert "@@ -" (number-to-string newstart1) ",1 +"
606 (number-to-string newstart2) ",1 @@\n")
607 ;; Fix the original hunk-header.
608 (diff-fixup-modifs start pos))))
609
610
611 ;;;;
612 ;;;; jump to other buffers
613 ;;;;
614
615 (defvar diff-remembered-files-alist nil)
616
617 (defun diff-filename-drop-dir (file)
618 (when (string-match "/" file) (substring file (match-end 0))))
619
620 (defun diff-merge-strings (ancestor from to)
621 "Merge the diff between ANCESTOR and FROM into TO.
622 Returns the merged string if successful or nil otherwise.
623 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
624 If ANCESTOR = FROM, returns TO.
625 If ANCESTOR = TO, returns FROM.
626 The heuristic is simplistic and only really works for cases
627 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
628 ;; Ideally, we want:
629 ;; AMB ANB CMD -> CND
630 ;; but that's ambiguous if `foo' or `bar' is empty:
631 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
632 (let ((str (concat ancestor "\n" from "\n" to)))
633 (when (and (string-match (concat
634 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
635 "\\1\\(.*\\)\\3\n"
636 "\\(.*\\(\\2\\).*\\)\\'") str)
637 (equal to (match-string 5 str)))
638 (concat (substring str (match-beginning 5) (match-beginning 6))
639 (match-string 4 str)
640 (substring str (match-end 6) (match-end 5))))))
641
642 (defun diff-tell-file-name (old name)
643 "Tell Emacs where the find the source file of the current hunk.
644 If the OLD prefix arg is passed, tell the file NAME of the old file."
645 (interactive
646 (let* ((old current-prefix-arg)
647 (fs (diff-hunk-file-names current-prefix-arg)))
648 (unless fs (error "No file name to look for"))
649 (list old (read-file-name (format "File for %s: " (car fs))
650 nil (diff-find-file-name old) t))))
651 (let ((fs (diff-hunk-file-names old)))
652 (unless fs (error "No file name to look for"))
653 (push (cons fs name) diff-remembered-files-alist)))
654
655 (defun diff-hunk-file-names (&optional old)
656 "Give the list of file names textually mentioned for the current hunk."
657 (save-excursion
658 (unless (looking-at diff-file-header-re)
659 (or (ignore-errors (diff-beginning-of-file))
660 (re-search-forward diff-file-header-re nil t)))
661 (let ((limit (save-excursion
662 (condition-case ()
663 (progn (diff-hunk-prev) (point))
664 (error (point-min)))))
665 (header-files
666 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
667 (list (if old (match-string 1) (match-string 3))
668 (if old (match-string 3) (match-string 1)))
669 (forward-line 1) nil)))
670 (delq nil
671 (append
672 (when (and (not old)
673 (save-excursion
674 (re-search-backward "^Index: \\(.+\\)" limit t)))
675 (list (match-string 1)))
676 header-files
677 (when (re-search-backward
678 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?"
679 nil t)
680 (list (if old (match-string 2) (match-string 4))
681 (if old (match-string 4) (match-string 2)))))))))
682
683 (defun diff-find-file-name (&optional old prefix)
684 "Return the file corresponding to the current patch.
685 Non-nil OLD means that we want the old file.
686 PREFIX is only used internally: don't use it."
687 (save-excursion
688 (unless (looking-at diff-file-header-re)
689 (or (ignore-errors (diff-beginning-of-file))
690 (re-search-forward diff-file-header-re nil t)))
691 (let ((fs (diff-hunk-file-names old)))
692 (if prefix (setq fs (mapcar (lambda (f) (concat prefix f)) fs)))
693 (or
694 ;; use any previously used preference
695 (cdr (assoc fs diff-remembered-files-alist))
696 ;; try to be clever and use previous choices as an inspiration
697 (dolist (rf diff-remembered-files-alist)
698 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
699 (if (and newfile (file-exists-p newfile)) (return newfile))))
700 ;; look for each file in turn. If none found, try again but
701 ;; ignoring the first level of directory, ...
702 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
703 (file nil nil))
704 ((or (null files)
705 (setq file (do* ((files files (cdr files))
706 (file (car files) (car files)))
707 ;; Use file-regular-p to avoid
708 ;; /dev/null, directories, etc.
709 ((or (null file) (file-regular-p file))
710 file))))
711 file))
712 ;; <foo>.rej patches implicitly apply to <foo>
713 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
714 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
715 (when (file-exists-p file) file)))
716 ;; If we haven't found the file, maybe it's because we haven't paid
717 ;; attention to the PCL-CVS hint.
718 (and (not prefix)
719 (boundp 'cvs-pcl-cvs-dirchange-re)
720 (save-excursion
721 (re-search-backward cvs-pcl-cvs-dirchange-re nil t))
722 (diff-find-file-name old (match-string 1)))
723 ;; if all else fails, ask the user
724 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
725 nil (first fs) t (first fs))))
726 (set (make-local-variable 'diff-remembered-files-alist)
727 (cons (cons fs file) diff-remembered-files-alist))
728 file)))))
729
730
731 (defun diff-ediff-patch ()
732 "Call `ediff-patch-file' on the current buffer."
733 (interactive)
734 (condition-case err
735 (ediff-patch-file nil (current-buffer))
736 (wrong-number-of-arguments (ediff-patch-file))))
737
738 ;;;;
739 ;;;; Conversion functions
740 ;;;;
741
742 ;;(defvar diff-inhibit-after-change nil
743 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
744
745 (defun diff-unified->context (start end)
746 "Convert unified diffs to context diffs.
747 START and END are either taken from the region (if a prefix arg is given) or
748 else cover the whole buffer."
749 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
750 (list (region-beginning) (region-end))
751 (list (point-min) (point-max))))
752 (unless (markerp end) (setq end (copy-marker end t)))
753 (let (;;(diff-inhibit-after-change t)
754 (inhibit-read-only t))
755 (save-excursion
756 (goto-char start)
757 (while (and (re-search-forward
758 (concat "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|"
759 diff-hunk-header-re-unified ".*\\)$")
760 nil t)
761 (< (point) end))
762 (combine-after-change-calls
763 (if (match-beginning 2)
764 ;; we matched a file header
765 (progn
766 ;; use reverse order to make sure the indices are kept valid
767 (replace-match "---" t t nil 3)
768 (replace-match "***" t t nil 2))
769 ;; we matched a hunk header
770 (let ((line1 (match-string 4))
771 (lines1 (match-string 5))
772 (line2 (match-string 6))
773 (lines2 (match-string 7)))
774 (replace-match
775 (concat "***************\n*** " line1 ","
776 (number-to-string (+ (string-to-number line1)
777 (string-to-number lines1)
778 -1)) " ****"))
779 (save-restriction
780 (narrow-to-region (line-beginning-position 2)
781 ;; Call diff-end-of-hunk from just before
782 ;; the hunk header so it can use the hunk
783 ;; header info.
784 (progn (diff-end-of-hunk 'unified) (point)))
785 (let ((hunk (buffer-string)))
786 (goto-char (point-min))
787 (if (not (save-excursion (re-search-forward "^-" nil t)))
788 (delete-region (point) (point-max))
789 (goto-char (point-max))
790 (let ((modif nil) last-pt)
791 (while (progn (setq last-pt (point))
792 (= (forward-line -1) 0))
793 (case (char-after)
794 (?\s (insert " ") (setq modif nil) (backward-char 1))
795 (?+ (delete-region (point) last-pt) (setq modif t))
796 (?- (if (not modif)
797 (progn (forward-char 1)
798 (insert " "))
799 (delete-char 1)
800 (insert "! "))
801 (backward-char 2))
802 (?\\ (when (save-excursion (forward-line -1)
803 (= (char-after) ?+))
804 (delete-region (point) last-pt) (setq modif t)))
805 ;; diff-valid-unified-empty-line.
806 (?\n (insert " ") (setq modif nil) (backward-char 2))
807 (t (setq modif nil))))))
808 (goto-char (point-max))
809 (save-excursion
810 (insert "--- " line2 ","
811 (number-to-string (+ (string-to-number line2)
812 (string-to-number lines2)
813 -1)) " ----\n" hunk))
814 ;;(goto-char (point-min))
815 (forward-line 1)
816 (if (not (save-excursion (re-search-forward "^+" nil t)))
817 (delete-region (point) (point-max))
818 (let ((modif nil) (delete nil))
819 (while (not (eobp))
820 (case (char-after)
821 (?\s (insert " ") (setq modif nil) (backward-char 1))
822 (?- (setq delete t) (setq modif t))
823 (?+ (if (not modif)
824 (progn (forward-char 1)
825 (insert " "))
826 (delete-char 1)
827 (insert "! "))
828 (backward-char 2))
829 (?\\ (when (save-excursion (forward-line 1)
830 (not (eobp)))
831 (setq delete t) (setq modif t)))
832 ;; diff-valid-unified-empty-line.
833 (?\n (insert " ") (setq modif nil) (backward-char 2))
834 (t (setq modif nil)))
835 (let ((last-pt (point)))
836 (forward-line 1)
837 (when delete
838 (delete-region last-pt (point))
839 (setq delete nil)))))))))))))))
840
841 (defun diff-context->unified (start end &optional to-context)
842 "Convert context diffs to unified diffs.
843 START and END are either taken from the region
844 \(when it is highlighted) or else cover the whole buffer.
845 With a prefix argument, convert unified format to context format."
846 (interactive (if (and transient-mark-mode mark-active)
847 (list (region-beginning) (region-end) current-prefix-arg)
848 (list (point-min) (point-max) current-prefix-arg)))
849 (if to-context
850 (diff-unified->context start end)
851 (unless (markerp end) (setq end (copy-marker end t)))
852 (let ( ;;(diff-inhibit-after-change t)
853 (inhibit-read-only t))
854 (save-excursion
855 (goto-char start)
856 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
857 (< (point) end))
858 (combine-after-change-calls
859 (if (match-beginning 2)
860 ;; we matched a file header
861 (progn
862 ;; use reverse order to make sure the indices are kept valid
863 (replace-match "+++" t t nil 3)
864 (replace-match "---" t t nil 2))
865 ;; we matched a hunk header
866 (let ((line1s (match-string 4))
867 (line1e (match-string 5))
868 (pt1 (match-beginning 0)))
869 (replace-match "")
870 (unless (re-search-forward
871 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
872 (error "Can't find matching `--- n1,n2 ----' line"))
873 (let ((line2s (match-string 1))
874 (line2e (match-string 2))
875 (pt2 (progn
876 (delete-region (progn (beginning-of-line) (point))
877 (progn (forward-line 1) (point)))
878 (point-marker))))
879 (goto-char pt1)
880 (forward-line 1)
881 (while (< (point) pt2)
882 (case (char-after)
883 ((?! ?-) (delete-char 2) (insert "-") (forward-line 1))
884 (?\s ;merge with the other half of the chunk
885 (let* ((endline2
886 (save-excursion
887 (goto-char pt2) (forward-line 1) (point)))
888 (c (char-after pt2)))
889 (case c
890 ((?! ?+)
891 (insert "+"
892 (prog1 (buffer-substring (+ pt2 2) endline2)
893 (delete-region pt2 endline2))))
894 (?\s ;FIXME: check consistency
895 (delete-region pt2 endline2)
896 (delete-char 1)
897 (forward-line 1))
898 (?\\ (forward-line 1))
899 (t (delete-char 1) (forward-line 1)))))
900 (t (forward-line 1))))
901 (while (looking-at "[+! ] ")
902 (if (/= (char-after) ?!) (forward-char 1)
903 (delete-char 1) (insert "+"))
904 (delete-char 1) (forward-line 1))
905 (save-excursion
906 (goto-char pt1)
907 (insert "@@ -" line1s ","
908 (number-to-string (- (string-to-number line1e)
909 (string-to-number line1s)
910 -1))
911 " +" line2s ","
912 (number-to-string (- (string-to-number line2e)
913 (string-to-number line2s)
914 -1)) " @@")))))))))))
915
916 (defun diff-reverse-direction (start end)
917 "Reverse the direction of the diffs.
918 START and END are either taken from the region (if a prefix arg is given) or
919 else cover the whole buffer."
920 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
921 (list (region-beginning) (region-end))
922 (list (point-min) (point-max))))
923 (unless (markerp end) (setq end (copy-marker end t)))
924 (let (;;(diff-inhibit-after-change t)
925 (inhibit-read-only t))
926 (save-excursion
927 (goto-char start)
928 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
929 (< (point) end))
930 (combine-after-change-calls
931 (cond
932 ;; a file header
933 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
934 ;; a context-diff hunk header
935 ((match-beginning 6)
936 (let ((pt-lines1 (match-beginning 6))
937 (lines1 (match-string 6)))
938 (replace-match "" nil nil nil 6)
939 (forward-line 1)
940 (let ((half1s (point)))
941 (while (looking-at "[-! \\][ \t]\\|#")
942 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
943 (forward-line 1))
944 (let ((half1 (delete-and-extract-region half1s (point))))
945 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
946 (insert half1)
947 (error "Can't find matching `--- n1,n2 ----' line"))
948 (let ((str1 (match-string 1)))
949 (replace-match lines1 nil nil nil 1)
950 (forward-line 1)
951 (let ((half2s (point)))
952 (while (looking-at "[!+ \\][ \t]\\|#")
953 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
954 (forward-line 1))
955 (let ((half2 (delete-and-extract-region half2s (point))))
956 (insert (or half1 ""))
957 (goto-char half1s)
958 (insert (or half2 ""))))
959 (goto-char pt-lines1)
960 (insert str1))))))
961 ;; a unified-diff hunk header
962 ((match-beginning 7)
963 (replace-match "@@ -\\8 +\\7 @@" nil)
964 (forward-line 1)
965 (let ((c (char-after)) first last)
966 (while (case (setq c (char-after))
967 (?- (setq first (or first (point)))
968 (delete-char 1) (insert "+") t)
969 (?+ (setq last (or last (point)))
970 (delete-char 1) (insert "-") t)
971 ((?\\ ?#) t)
972 (t (when (and first last (< first last))
973 (insert (delete-and-extract-region first last)))
974 (setq first nil last nil)
975 (memq c (if diff-valid-unified-empty-line
976 '(?\s ?\n) '(?\s)))))
977 (forward-line 1))))))))))
978
979 (defun diff-fixup-modifs (start end)
980 "Fixup the hunk headers (in case the buffer was modified).
981 START and END are either taken from the region (if a prefix arg is given) or
982 else cover the whole buffer."
983 (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
984 (list (region-beginning) (region-end))
985 (list (point-min) (point-max))))
986 (let ((inhibit-read-only t))
987 (save-excursion
988 (goto-char end) (diff-end-of-hunk nil 'donttrustheader)
989 (let ((plus 0) (minus 0) (space 0) (bang 0))
990 (while (and (= (forward-line -1) 0) (<= start (point)))
991 (if (not (looking-at
992 (concat diff-hunk-header-re-unified
993 "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$"
994 "\\|--- .+\n\\+\\+\\+ ")))
995 (case (char-after)
996 (?\s (incf space))
997 (?+ (incf plus))
998 (?- (incf minus))
999 (?! (incf bang))
1000 ((?\\ ?#) nil)
1001 (t (setq space 0 plus 0 minus 0 bang 0)))
1002 (cond
1003 ((looking-at diff-hunk-header-re-unified)
1004 (let* ((old1 (match-string 2))
1005 (old2 (match-string 4))
1006 (new1 (number-to-string (+ space minus)))
1007 (new2 (number-to-string (+ space plus))))
1008 (unless (string= new2 old2) (replace-match new2 t t nil 4))
1009 (unless (string= new1 old1) (replace-match new1 t t nil 2))))
1010 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
1011 (when (> (+ space bang plus) 0)
1012 (let* ((old1 (match-string 1))
1013 (old2 (match-string 2))
1014 (new (number-to-string
1015 (+ space bang plus -1 (string-to-number old1)))))
1016 (unless (string= new old2) (replace-match new t t nil 2)))))
1017 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
1018 (when (> (+ space bang minus) 0)
1019 (let* ((old (match-string 1))
1020 (new (format
1021 (concat "%0" (number-to-string (length old)) "d")
1022 (+ space bang minus -1 (string-to-number old)))))
1023 (unless (string= new old) (replace-match new t t nil 2))))))
1024 (setq space 0 plus 0 minus 0 bang 0)))))))
1025
1026 ;;;;
1027 ;;;; Hooks
1028 ;;;;
1029
1030 (defun diff-write-contents-hooks ()
1031 "Fixup hunk headers if necessary."
1032 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
1033 nil)
1034
1035 ;; It turns out that making changes in the buffer from within an
1036 ;; *-change-function is asking for trouble, whereas making them
1037 ;; from a post-command-hook doesn't pose much problems
1038 (defvar diff-unhandled-changes nil)
1039 (defun diff-after-change-function (beg end len)
1040 "Remember to fixup the hunk header.
1041 See `after-change-functions' for the meaning of BEG, END and LEN."
1042 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
1043 ;; incorrect, but it turns out that inhibit-read-only is normally not set
1044 ;; inside editing commands, while it tends to be set when the buffer gets
1045 ;; updated by an async process or by a conversion function, both of which
1046 ;; would rather not be uselessly slowed down by this hook.
1047 (when (and (not undo-in-progress) (not inhibit-read-only))
1048 (if diff-unhandled-changes
1049 (setq diff-unhandled-changes
1050 (cons (min beg (car diff-unhandled-changes))
1051 (max end (cdr diff-unhandled-changes))))
1052 (setq diff-unhandled-changes (cons beg end)))))
1053
1054 (defun diff-post-command-hook ()
1055 "Fixup hunk headers if necessary."
1056 (when (consp diff-unhandled-changes)
1057 (ignore-errors
1058 (save-excursion
1059 (goto-char (car diff-unhandled-changes))
1060 ;; Maybe we've cut the end of the hunk before point.
1061 (if (and (bolp) (not (bobp))) (backward-char 1))
1062 ;; We used to fixup modifs on all the changes, but it turns out
1063 ;; that it's safer not to do it on big changes, for example
1064 ;; when yanking a big diff, since we might then screw up perfectly
1065 ;; correct values. -stef
1066 ;; (unless (ignore-errors
1067 ;; (diff-beginning-of-hunk)
1068 ;; (save-excursion
1069 ;; (diff-end-of-hunk)
1070 ;; (> (point) (car diff-unhandled-changes))))
1071 ;; (goto-char (car diff-unhandled-changes))
1072 ;; (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
1073 ;; (diff-beginning-of-hunk))
1074 ;; (diff-fixup-modifs (point) (cdr diff-unhandled-changes))
1075 (diff-beginning-of-hunk)
1076 (when (save-excursion
1077 (diff-end-of-hunk nil 'donttrustheader)
1078 (>= (point) (cdr diff-unhandled-changes)))
1079 (diff-fixup-modifs (point) (cdr diff-unhandled-changes)))))
1080 (setq diff-unhandled-changes nil)))
1081
1082 (defun diff-next-error (arg reset)
1083 ;; Select a window that displays the current buffer so that point
1084 ;; movements are reflected in that window. Otherwise, the user might
1085 ;; never see the hunk corresponding to the source she's jumping to.
1086 (pop-to-buffer (current-buffer))
1087 (if reset (goto-char (point-min)))
1088 (diff-hunk-next arg)
1089 (diff-goto-source))
1090
1091 ;;;###autoload
1092 (define-derived-mode diff-mode fundamental-mode "Diff"
1093 "Major mode for viewing/editing context diffs.
1094 Supports unified and context diffs as well as (to a lesser extent)
1095 normal diffs.
1096
1097 When the buffer is read-only, the ESC prefix is not necessary.
1098 If you edit the buffer manually, diff-mode will try to update the hunk
1099 headers for you on-the-fly.
1100
1101 You can also switch between context diff and unified diff with \\[diff-context->unified],
1102 or vice versa with \\[diff-unified->context] and you can also reverse the direction of
1103 a diff with \\[diff-reverse-direction].
1104
1105 \\{diff-mode-map}"
1106
1107 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
1108 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
1109 (set (make-local-variable 'imenu-generic-expression)
1110 diff-imenu-generic-expression)
1111 ;; These are not perfect. They would be better done separately for
1112 ;; context diffs and unidiffs.
1113 ;; (set (make-local-variable 'paragraph-start)
1114 ;; (concat "@@ " ; unidiff hunk
1115 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
1116 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
1117 ;; ; start (first or second line)
1118 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
1119 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
1120 ;; compile support
1121 (set (make-local-variable 'next-error-function) 'diff-next-error)
1122
1123 (setq buffer-read-only diff-default-read-only)
1124 ;; setup change hooks
1125 (if (not diff-update-on-the-fly)
1126 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
1127 (make-local-variable 'diff-unhandled-changes)
1128 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1129 (add-hook 'post-command-hook 'diff-post-command-hook nil t))
1130 ;; Neat trick from Dave Love to add more bindings in read-only mode:
1131 (lexical-let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
1132 (add-to-list 'minor-mode-overriding-map-alist ro-bind)
1133 ;; Turn off this little trick in case the buffer is put in view-mode.
1134 (add-hook 'view-mode-hook
1135 (lambda ()
1136 (setq minor-mode-overriding-map-alist
1137 (delq ro-bind minor-mode-overriding-map-alist)))
1138 nil t))
1139 ;; add-log support
1140 (set (make-local-variable 'add-log-current-defun-function)
1141 'diff-current-defun)
1142 (set (make-local-variable 'add-log-buffer-file-name-function)
1143 'diff-find-file-name))
1144
1145 ;;;###autoload
1146 (define-minor-mode diff-minor-mode
1147 "Minor mode for viewing/editing context diffs.
1148 \\{diff-minor-mode-map}"
1149 :group 'diff-mode :lighter " Diff"
1150 ;; FIXME: setup font-lock
1151 ;; setup change hooks
1152 (if (not diff-update-on-the-fly)
1153 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
1154 (make-local-variable 'diff-unhandled-changes)
1155 (add-hook 'after-change-functions 'diff-after-change-function nil t)
1156 (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
1157
1158 ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1159
1160 (defun diff-delete-if-empty ()
1161 ;; An empty diff file means there's no more diffs to integrate, so we
1162 ;; can just remove the file altogether. Very handy for .rej files if we
1163 ;; remove hunks as we apply them.
1164 (when (and buffer-file-name
1165 (eq 0 (nth 7 (file-attributes buffer-file-name))))
1166 (delete-file buffer-file-name)))
1167
1168 (defun diff-delete-empty-files ()
1169 "Arrange for empty diff files to be removed."
1170 (add-hook 'after-save-hook 'diff-delete-if-empty nil t))
1171
1172 (defun diff-make-unified ()
1173 "Turn context diffs into unified diffs if applicable."
1174 (if (save-excursion
1175 (goto-char (point-min))
1176 (and (looking-at diff-hunk-header-re) (eq (char-after) ?*)))
1177 (let ((mod (buffer-modified-p)))
1178 (unwind-protect
1179 (diff-context->unified (point-min) (point-max))
1180 (restore-buffer-modified-p mod)))))
1181
1182 ;;;
1183 ;;; Misc operations that have proved useful at some point.
1184 ;;;
1185
1186 (defun diff-next-complex-hunk ()
1187 "Jump to the next \"complex\" hunk.
1188 \"Complex\" is approximated by \"the hunk changes the number of lines\".
1189 Only works for unified diffs."
1190 (interactive)
1191 (while
1192 (and (re-search-forward diff-hunk-header-re-unified nil t)
1193 (equal (match-string 2) (match-string 4)))))
1194
1195 (defun diff-sanity-check-context-hunk-half (lines)
1196 (let ((count lines))
1197 (while
1198 (cond
1199 ((and (memq (char-after) '(?\s ?! ?+ ?-))
1200 (memq (char-after (1+ (point))) '(?\s ?\t)))
1201 (decf count) t)
1202 ((or (zerop count) (= count lines)) nil)
1203 ((memq (char-after) '(?! ?+ ?-))
1204 (if (not (and (eq (char-after (1+ (point))) ?\n)
1205 (y-or-n-p "Try to auto-fix whitespace loss damage? ")))
1206 (error "End of hunk ambiguously marked")
1207 (forward-char 1) (insert " ") (forward-line -1) t))
1208 ((< lines 0)
1209 (error "End of hunk ambiguously marked"))
1210 ((not (y-or-n-p "Try to auto-fix whitespace loss and word-wrap damage? "))
1211 (error "Abort!"))
1212 ((eolp) (insert " ") (forward-line -1) t)
1213 (t (insert " ") (delete-region (- (point) 2) (- (point) 1)) t))
1214 (forward-line))))
1215
1216 (defun diff-sanity-check-hunk ()
1217 (let (;; Every modification is protected by a y-or-n-p, so it's probably
1218 ;; OK to override a read-only setting.
1219 (inhibit-read-only t))
1220 (save-excursion
1221 (cond
1222 ((not (looking-at diff-hunk-header-re))
1223 (error "Not recognizable hunk header"))
1224
1225 ;; A context diff.
1226 ((eq (char-after) ?*)
1227 (if (not (looking-at "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\([0-9]+\\),\\([0-9]+\\) \\*\\*\\*\\*"))
1228 (error "Unrecognized context diff first hunk header format")
1229 (forward-line 2)
1230 (diff-sanity-check-context-hunk-half
1231 (1+ (- (string-to-number (match-string 2))
1232 (string-to-number (match-string 1)))))
1233 (if (not (looking-at "--- \\([0-9]+\\),\\([0-9]+\\) ----$"))
1234 (error "Unrecognized context diff second hunk header format")
1235 (forward-line)
1236 (diff-sanity-check-context-hunk-half
1237 (1+ (- (string-to-number (match-string 2))
1238 (string-to-number (match-string 1))))))))
1239
1240 ;; A unified diff.
1241 ((eq (char-after) ?@)
1242 (if (not (looking-at diff-hunk-header-re-unified))
1243 (error "Unrecognized unified diff hunk header format")
1244 (let ((before (string-to-number (match-string 2)))
1245 (after (string-to-number (match-string 4))))
1246 (forward-line)
1247 (while
1248 (case (char-after)
1249 (?\s (decf before) (decf after) t)
1250 (?-
1251 (if (and (looking-at diff-file-header-re)
1252 (zerop before) (zerop after))
1253 ;; No need to query: this is a case where two patches
1254 ;; are concatenated and only counting the lines will
1255 ;; give the right result. Let's just add an empty
1256 ;; line so that our code which doesn't count lines
1257 ;; will not get confused.
1258 (progn (save-excursion (insert "\n")) nil)
1259 (decf before) t))
1260 (?+ (decf after) t)
1261 (t
1262 (cond
1263 ((and diff-valid-unified-empty-line
1264 ;; Not just (eolp) so we don't infloop at eob.
1265 (eq (char-after) ?\n)
1266 (> before 0) (> after 0))
1267 (decf before) (decf after) t)
1268 ((and (zerop before) (zerop after)) nil)
1269 ((or (< before 0) (< after 0))
1270 (error (if (or (zerop before) (zerop after))
1271 "End of hunk ambiguously marked"
1272 "Hunk seriously messed up")))
1273 ((not (y-or-n-p (concat "Try to auto-fix " (if (eolp) "whitespace loss" "word-wrap damage") "? ")))
1274 (error "Abort!"))
1275 ((eolp) (insert " ") (forward-line -1) t)
1276 (t (insert " ")
1277 (delete-region (- (point) 2) (- (point) 1)) t))))
1278 (forward-line)))))
1279
1280 ;; A plain diff.
1281 (t
1282 ;; TODO.
1283 )))))
1284
1285 (defun diff-hunk-text (hunk destp char-offset)
1286 "Return the literal source text from HUNK as (TEXT . OFFSET).
1287 If DESTP is nil, TEXT is the source, otherwise the destination text.
1288 CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding
1289 char-offset in TEXT."
1290 (with-temp-buffer
1291 (insert hunk)
1292 (goto-char (point-min))
1293 (let ((src-pos nil)
1294 (dst-pos nil)
1295 (divider-pos nil)
1296 (num-pfx-chars 2))
1297 ;; Set the following variables:
1298 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
1299 ;; DST-POS buffer pos of the destination part of the hunk or nil
1300 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
1301 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
1302 (cond ((looking-at "^@@")
1303 ;; unified diff
1304 (setq num-pfx-chars 1)
1305 (forward-line 1)
1306 (setq src-pos (point) dst-pos (point)))
1307 ((looking-at "^\\*\\*")
1308 ;; context diff
1309 (forward-line 2)
1310 (setq src-pos (point))
1311 (re-search-forward "^--- " nil t)
1312 (forward-line 0)
1313 (setq divider-pos (point))
1314 (forward-line 1)
1315 (setq dst-pos (point)))
1316 ((looking-at "^[0-9]+a[0-9,]+$")
1317 ;; normal diff, insert
1318 (forward-line 1)
1319 (setq dst-pos (point)))
1320 ((looking-at "^[0-9,]+d[0-9]+$")
1321 ;; normal diff, delete
1322 (forward-line 1)
1323 (setq src-pos (point)))
1324 ((looking-at "^[0-9,]+c[0-9,]+$")
1325 ;; normal diff, change
1326 (forward-line 1)
1327 (setq src-pos (point))
1328 (re-search-forward "^---$" nil t)
1329 (forward-line 0)
1330 (setq divider-pos (point))
1331 (forward-line 1)
1332 (setq dst-pos (point)))
1333 (t
1334 (error "Unknown diff hunk type")))
1335
1336 (if (if destp (null dst-pos) (null src-pos))
1337 ;; Implied empty text
1338 (if char-offset '("" . 0) "")
1339
1340 ;; For context diffs, either side can be empty, (if there's only
1341 ;; added or only removed text). We should then use the other side.
1342 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
1343 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
1344
1345 (when char-offset (goto-char (+ (point-min) char-offset)))
1346
1347 ;; Get rid of anything except the desired text.
1348 (save-excursion
1349 ;; Delete unused text region
1350 (let ((keep (if destp dst-pos src-pos)))
1351 (when (and divider-pos (> divider-pos keep))
1352 (delete-region divider-pos (point-max)))
1353 (delete-region (point-min) keep))
1354 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1355 (let ((kill-char (if destp ?- ?+)))
1356 (goto-char (point-min))
1357 (while (not (eobp))
1358 (if (eq (char-after) kill-char)
1359 (delete-region (point) (progn (forward-line 1) (point)))
1360 (delete-char num-pfx-chars)
1361 (forward-line 1)))))
1362
1363 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1364 (if char-offset (cons text (- (point) (point-min))) text))))))
1365
1366
1367 (defun diff-find-text (text)
1368 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1369 If TEXT isn't found, nil is returned."
1370 (let* ((orig (point))
1371 (forw (and (search-forward text nil t)
1372 (cons (match-beginning 0) (match-end 0))))
1373 (back (and (goto-char (+ orig (length text)))
1374 (search-backward text nil t)
1375 (cons (match-beginning 0) (match-end 0)))))
1376 ;; Choose the closest match.
1377 (if (and forw back)
1378 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1379 (or back forw))))
1380
1381 (defun diff-find-approx-text (text)
1382 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1383 Whitespace differences are ignored."
1384 (let* ((orig (point))
1385 (re (concat "^[ \t\n\f]*"
1386 (mapconcat 'regexp-quote (split-string text) "[ \t\n\f]+")
1387 "[ \t\n\f]*\n"))
1388 (forw (and (re-search-forward re nil t)
1389 (cons (match-beginning 0) (match-end 0))))
1390 (back (and (goto-char (+ orig (length text)))
1391 (re-search-backward re nil t)
1392 (cons (match-beginning 0) (match-end 0)))))
1393 ;; Choose the closest match.
1394 (if (and forw back)
1395 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1396 (or back forw))))
1397
1398 (defsubst diff-xor (a b) (if a (not b) b))
1399
1400 (defun diff-find-source-location (&optional other-file reverse)
1401 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
1402 BUF is the buffer corresponding to the source file.
1403 LINE-OFFSET is the offset between the expected and actual positions
1404 of the text of the hunk or nil if the text was not found.
1405 POS is a pair (BEG . END) indicating the position of the text in the buffer.
1406 SRC and DST are the two variants of text as returned by `diff-hunk-text'.
1407 SRC is the variant that was found in the buffer.
1408 SWITCHED is non-nil if the patch is already applied."
1409 (save-excursion
1410 (let* ((other (diff-xor other-file diff-jump-to-old-file))
1411 (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1412 (point))))
1413 ;; Check that the hunk is well-formed. Otherwise diff-mode and
1414 ;; the user may disagree on what constitutes the hunk
1415 ;; (e.g. because an empty line truncates the hunk mid-course),
1416 ;; leading to potentially nasty surprises for the user.
1417 (_ (diff-sanity-check-hunk))
1418 (hunk (buffer-substring (point)
1419 (save-excursion (diff-end-of-hunk) (point))))
1420 (old (diff-hunk-text hunk reverse char-offset))
1421 (new (diff-hunk-text hunk (not reverse) char-offset))
1422 ;; Find the location specification.
1423 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
1424 (error "Can't find the hunk header")
1425 (if other (match-string 1)
1426 (if (match-end 3) (match-string 3)
1427 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
1428 (error "Can't find the hunk separator"))
1429 (match-string 1)))))
1430 (file (or (diff-find-file-name other) (error "Can't find the file")))
1431 (buf (find-file-noselect file)))
1432 ;; Update the user preference if he so wished.
1433 (when (> (prefix-numeric-value other-file) 8)
1434 (setq diff-jump-to-old-file other))
1435 (with-current-buffer buf
1436 (goto-line (string-to-number line))
1437 (let* ((orig-pos (point))
1438 (switched nil)
1439 ;; FIXME: Check for case where both OLD and NEW are found.
1440 (pos (or (diff-find-text (car old))
1441 (progn (setq switched t) (diff-find-text (car new)))
1442 (progn (setq switched nil)
1443 (condition-case nil
1444 (diff-find-approx-text (car old))
1445 (invalid-regexp nil))) ;Regex too big.
1446 (progn (setq switched t)
1447 (condition-case nil
1448 (diff-find-approx-text (car new))
1449 (invalid-regexp nil))) ;Regex too big.
1450 (progn (setq switched nil) nil))))
1451 (nconc
1452 (list buf)
1453 (if pos
1454 (list (count-lines orig-pos (car pos)) pos)
1455 (list nil (cons orig-pos (+ orig-pos (length (car old))))))
1456 (if switched (list new old t) (list old new))))))))
1457
1458
1459 (defun diff-hunk-status-msg (line-offset reversed dry-run)
1460 (let ((msg (if dry-run
1461 (if reversed "already applied" "not yet applied")
1462 (if reversed "undone" "applied"))))
1463 (message (cond ((null line-offset) "Hunk text not found")
1464 ((= line-offset 0) "Hunk %s")
1465 ((= line-offset 1) "Hunk %s at offset %d line")
1466 (t "Hunk %s at offset %d lines"))
1467 msg line-offset)))
1468
1469 (defvar diff-apply-hunk-to-backup-file nil)
1470
1471 (defun diff-apply-hunk (&optional reverse)
1472 "Apply the current hunk to the source file and go to the next.
1473 By default, the new source file is patched, but if the variable
1474 `diff-jump-to-old-file' is non-nil, then the old source file is
1475 patched instead (some commands, such as `diff-goto-source' can change
1476 the value of this variable when given an appropriate prefix argument).
1477
1478 With a prefix argument, REVERSE the hunk."
1479 (interactive "P")
1480 (destructuring-bind (buf line-offset pos old new &optional switched)
1481 ;; If REVERSE go to the new file, otherwise go to the old.
1482 (diff-find-source-location (not reverse) reverse)
1483 (cond
1484 ((null line-offset)
1485 (error "Can't find the text to patch"))
1486 ((with-current-buffer buf
1487 (and buffer-file-name
1488 (backup-file-name-p buffer-file-name)
1489 (not diff-apply-hunk-to-backup-file)
1490 (not (set (make-local-variable 'diff-apply-hunk-to-backup-file)
1491 (yes-or-no-p (format "Really apply this hunk to %s? "
1492 (file-name-nondirectory
1493 buffer-file-name)))))))
1494 (error "%s"
1495 (substitute-command-keys
1496 (format "Use %s\\[diff-apply-hunk] to apply it to the other file"
1497 (if (not reverse) "\\[universal-argument] ")))))
1498 ((and switched
1499 ;; A reversed patch was detected, perhaps apply it in reverse.
1500 (not (save-window-excursion
1501 (pop-to-buffer buf)
1502 (goto-char (+ (car pos) (cdr old)))
1503 (y-or-n-p
1504 (if reverse
1505 "Hunk hasn't been applied yet; apply it now? "
1506 "Hunk has already been applied; undo it? ")))))
1507 (message "(Nothing done)"))
1508 (t
1509 ;; Apply the hunk
1510 (with-current-buffer buf
1511 (goto-char (car pos))
1512 (delete-region (car pos) (cdr pos))
1513 (insert (car new)))
1514 ;; Display BUF in a window
1515 (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
1516 (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
1517 (when diff-advance-after-apply-hunk
1518 (diff-hunk-next))))))
1519
1520
1521 (defun diff-test-hunk (&optional reverse)
1522 "See whether it's possible to apply the current hunk.
1523 With a prefix argument, try to REVERSE the hunk."
1524 (interactive "P")
1525 (destructuring-bind (buf line-offset pos src dst &optional switched)
1526 ;; If REVERSE go to the new file, otherwise go to the old.
1527 (diff-find-source-location (not reverse) reverse)
1528 (set-window-point (display-buffer buf) (+ (car pos) (cdr src)))
1529 (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
1530
1531
1532 (defalias 'diff-mouse-goto-source 'diff-goto-source)
1533
1534 (defun diff-goto-source (&optional other-file event)
1535 "Jump to the corresponding source line.
1536 `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
1537 is given) determines whether to jump to the old or the new file.
1538 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1539 then `diff-jump-to-old-file' is also set, for the next invocations."
1540 (interactive (list current-prefix-arg last-input-event))
1541 ;; When pointing at a removal line, we probably want to jump to
1542 ;; the old location, and else to the new (i.e. as if reverting).
1543 ;; This is a convenient detail when using smerge-diff.
1544 (if event (posn-set-point (event-end event)))
1545 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1546 (destructuring-bind (buf line-offset pos src dst &optional switched)
1547 (diff-find-source-location other-file rev)
1548 (pop-to-buffer buf)
1549 (goto-char (+ (car pos) (cdr src)))
1550 (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
1551
1552
1553 (defun diff-current-defun ()
1554 "Find the name of function at point.
1555 For use in `add-log-current-defun-function'."
1556 (save-excursion
1557 (when (looking-at diff-hunk-header-re)
1558 (forward-line 1)
1559 (re-search-forward "^[^ ]" nil t))
1560 (destructuring-bind (buf line-offset pos src dst &optional switched)
1561 (diff-find-source-location)
1562 (beginning-of-line)
1563 (or (when (memq (char-after) '(?< ?-))
1564 ;; Cursor is pointing at removed text. This could be a removed
1565 ;; function, in which case, going to the source buffer will
1566 ;; not help since the function is now removed. Instead,
1567 ;; try to figure out the function name just from the code-fragment.
1568 (let ((old (if switched dst src)))
1569 (with-temp-buffer
1570 (insert (car old))
1571 (funcall (with-current-buffer buf major-mode))
1572 (goto-char (+ (point-min) (cdr old)))
1573 (add-log-current-defun))))
1574 (with-current-buffer buf
1575 (goto-char (+ (car pos) (cdr src)))
1576 (add-log-current-defun))))))
1577
1578 (defun diff-refine-hunk ()
1579 "Refine the current hunk by ignoring space differences."
1580 (interactive)
1581 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1582 (point))))
1583 (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b")))
1584 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
1585 (error "Can't find line number"))
1586 (string-to-number (match-string 1))))
1587 (hunk (delete-and-extract-region
1588 (point) (save-excursion (diff-end-of-hunk) (point))))
1589 (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
1590 (file1 (make-temp-file "diff1"))
1591 (file2 (make-temp-file "diff2"))
1592 (coding-system-for-read buffer-file-coding-system)
1593 (inhibit-read-only t)
1594 old new)
1595 (unwind-protect
1596 (save-excursion
1597 (setq old (diff-hunk-text hunk nil char-offset))
1598 (setq new (diff-hunk-text hunk t char-offset))
1599 (write-region (concat lead (car old)) nil file1 nil 'nomessage)
1600 (write-region (concat lead (car new)) nil file2 nil 'nomessage)
1601 (with-temp-buffer
1602 (let ((status
1603 (call-process diff-command nil t nil
1604 opts file1 file2)))
1605 (case status
1606 (0 nil) ;Nothing to reformat.
1607 (1 (goto-char (point-min))
1608 ;; Remove the file-header.
1609 (when (re-search-forward diff-hunk-header-re nil t)
1610 (delete-region (point-min) (match-beginning 0))))
1611 (t (goto-char (point-max))
1612 (unless (bolp) (insert "\n"))
1613 (insert hunk)))
1614 (setq hunk (buffer-string))
1615 (unless (memq status '(0 1))
1616 (error "Diff returned: %s" status)))))
1617 ;; Whatever happens, put back some equivalent text: either the new
1618 ;; one or the original one in case some error happened.
1619 (insert hunk)
1620 (delete-file file1)
1621 (delete-file file2))))
1622
1623 ;; provide the package
1624 (provide 'diff-mode)
1625
1626 ;;; Old Change Log from when diff-mode wasn't part of Emacs:
1627 ;; Revision 1.11 1999/10/09 23:38:29 monnier
1628 ;; (diff-mode-load-hook): dropped.
1629 ;; (auto-mode-alist): also catch *.diffs.
1630 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
1631 ;; for *.rej files (that lack any file name indication).
1632 ;;
1633 ;; Revision 1.10 1999/09/30 15:32:11 monnier
1634 ;; added support for "\ No newline at end of file".
1635 ;;
1636 ;; Revision 1.9 1999/09/15 00:01:13 monnier
1637 ;; - added basic `compile' support.
1638 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
1639 ;; - diff-kill-file now tries to kill the leading garbage as well.
1640 ;;
1641 ;; Revision 1.8 1999/09/13 21:10:09 monnier
1642 ;; - don't use CL in the autoloaded code
1643 ;; - accept diffs using -T
1644 ;;
1645 ;; Revision 1.7 1999/09/05 20:53:03 monnier
1646 ;; interface to ediff-patch
1647 ;;
1648 ;; Revision 1.6 1999/09/01 20:55:13 monnier
1649 ;; (ediff=patch-file): add bindings to call ediff-patch.
1650 ;; (diff-find-file-name): taken out of diff-goto-source.
1651 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
1652 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
1653 ;;
1654 ;; Revision 1.5 1999/08/31 19:18:52 monnier
1655 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
1656 ;;
1657 ;; Revision 1.4 1999/08/31 13:01:44 monnier
1658 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
1659 ;;
1660
1661 ;; arch-tag: 2571d7ff-bc28-4cf9-8585-42e21890be66
1662 ;;; diff-mode.el ends here