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