1 ;;; diff-mode.el --- a mode for viewing/editing context diffs
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: convenience patch diff
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Provides support for font-lock, outline, navigation
29 ;; commands, editing and various conversions as well as jumping
30 ;; to the corresponding source file.
32 ;; Inspired by Pavel Machek's patch-mode.el (<pavel@@atrey.karlin.mff.cuni.cz>)
33 ;; Some efforts were spent to have it somewhat compatible with XEmacs'
34 ;; diff-mode as well as with compilation-minor-mode
38 ;; - Reverse doesn't work with normal diffs.
42 ;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
43 ;; Also allow C-c C-a to delete already-applied hunks.
45 ;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location
46 ;; of a hunk. Show then the changes between <file> and <hunk> and make it
47 ;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>.
48 ;; Or maybe just make it into a ".rej to diff3-markers converter".
49 ;; Maybe just use `wiggle' (by Neil Brown) to do it for us.
51 ;; - Refine hunk on a word-by-word basis.
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.
58 (eval-when-compile (require 'cl
))
60 (defvar add-log-buffer-file-name-function
)
63 (defgroup diff-mode
()
64 "Major mode for viewing/editing diffs."
69 (defcustom diff-default-read-only nil
70 "If non-nil, `diff-mode' buffers default to being read-only."
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."
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)."
90 (defcustom diff-advance-after-apply-hunk t
91 "*Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
96 (defcustom diff-mode-hook nil
97 "Run after setting up the `diff-mode' major mode."
99 :options
'(diff-delete-empty-files diff-make-unified
)
102 (defvar diff-outline-regexp
103 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
106 ;;;; keymap, menu, ...
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.
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
133 ;; Not useful if you have to metafy them.
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)
142 "Basic keymap for `diff-mode', bound to various prefix keys.")
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
)
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-hunk
)
159 ("\C-c\C-f" . next-error-follow-minor-mode
))
160 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
162 (easy-menu-define diff-mode-menu diff-mode-map
163 "Menu for `diff-mode'."
165 ["Jump to Source" diff-goto-source t
]
166 ["Apply hunk" diff-apply-hunk t
]
167 ["Apply diff with Ediff" diff-ediff-patch t
]
169 ["Reverse direction" diff-reverse-direction t
]
170 ["Context -> Unified" diff-context-
>unified t
]
171 ["Unified -> Context" diff-unified-
>context t
]
172 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
175 (defcustom diff-minor-mode-prefix
"\C-c="
176 "Prefix key for `diff-minor-mode' commands."
177 :type
'(choice (string "\e") (string "C-c=") string
)
180 (easy-mmode-defmap diff-minor-mode-map
181 `((,diff-minor-mode-prefix .
,diff-mode-shared-map
))
182 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
186 ;;;; font-lock support
190 '((((class color
) (min-colors 88) (background light
))
191 :background
"grey85")
192 (((class color
) (min-colors 88) (background dark
))
193 :background
"grey45")
194 (((class color
) (background light
))
195 :foreground
"blue1" :weight bold
)
196 (((class color
) (background dark
))
197 :foreground
"green" :weight bold
)
199 "`diff-mode' face inherited by hunk and index header faces."
201 ;; backward-compatibility alias
202 (put 'diff-header-face
'face-alias
'diff-header
)
203 (defvar diff-header-face
'diff-header
)
205 (defface diff-file-header
206 '((((class color
) (min-colors 88) (background light
))
207 :background
"grey70" :weight bold
)
208 (((class color
) (min-colors 88) (background dark
))
209 :background
"grey60" :weight bold
)
210 (((class color
) (background light
))
211 :foreground
"green" :weight bold
)
212 (((class color
) (background dark
))
213 :foreground
"cyan" :weight bold
)
214 (t :weight bold
)) ; :height 1.3
215 "`diff-mode' face used to highlight file header lines."
217 ;; backward-compatibility alias
218 (put 'diff-file-header-face
'face-alias
'diff-file-header
)
219 (defvar diff-file-header-face
'diff-file-header
)
222 '((t :inherit diff-file-header
))
223 "`diff-mode' face used to highlight index header lines."
225 ;; backward-compatibility alias
226 (put 'diff-index-face
'face-alias
'diff-index
)
227 (defvar diff-index-face
'diff-index
)
229 (defface diff-hunk-header
230 '((t :inherit diff-header
))
231 "`diff-mode' face used to highlight hunk header lines."
233 ;; backward-compatibility alias
234 (put 'diff-hunk-header-face
'face-alias
'diff-hunk-header
)
235 (defvar diff-hunk-header-face
'diff-hunk-header
)
237 (defface diff-removed
238 '((t :inherit diff-changed
))
239 "`diff-mode' face used to highlight removed lines."
241 ;; backward-compatibility alias
242 (put 'diff-removed-face
'face-alias
'diff-removed
)
243 (defvar diff-removed-face
'diff-removed
)
246 '((t :inherit diff-changed
))
247 "`diff-mode' face used to highlight added lines."
249 ;; backward-compatibility alias
250 (put 'diff-added-face
'face-alias
'diff-added
)
251 (defvar diff-added-face
'diff-added
)
253 (defface diff-changed
254 '((((type tty pc
) (class color
) (background light
))
255 :foreground
"magenta" :weight bold
:slant italic
)
256 (((type tty pc
) (class color
) (background dark
))
257 :foreground
"yellow" :weight bold
:slant italic
))
258 "`diff-mode' face used to highlight changed lines."
260 ;; backward-compatibility alias
261 (put 'diff-changed-face
'face-alias
'diff-changed
)
262 (defvar diff-changed-face
'diff-changed
)
264 (defface diff-indicator-removed
265 '((t :inherit diff-removed
))
266 "`diff-mode' face used to highlight indicator of removed lines (-, <)."
269 (defvar diff-indicator-removed-face
'diff-indicator-removed
)
271 (defface diff-indicator-added
272 '((t :inherit diff-added
))
273 "`diff-mode' face used to highlight indicator of added lines (+, >)."
276 (defvar diff-indicator-added-face
'diff-indicator-added
)
278 (defface diff-indicator-changed
279 '((t :inherit diff-changed
))
280 "`diff-mode' face used to highlight indicator of changed lines."
283 (defvar diff-indicator-changed-face
'diff-indicator-changed
)
285 (defface diff-function
286 '((t :inherit diff-header
))
287 "`diff-mode' face used to highlight function names produced by \"diff -p\"."
289 ;; backward-compatibility alias
290 (put 'diff-function-face
'face-alias
'diff-function
)
291 (defvar diff-function-face
'diff-function
)
293 (defface diff-context
294 '((((class color grayscale
) (min-colors 88)) :inherit shadow
))
295 "`diff-mode' face used to highlight context and other side-information."
297 ;; backward-compatibility alias
298 (put 'diff-context-face
'face-alias
'diff-context
)
299 (defvar diff-context-face
'diff-context
)
301 (defface diff-nonexistent
302 '((t :inherit diff-file-header
))
303 "`diff-mode' face used to highlight nonexistent files in recursive diffs."
305 ;; backward-compatibility alias
306 (put 'diff-nonexistent-face
'face-alias
'diff-nonexistent
)
307 (defvar diff-nonexistent-face
'diff-nonexistent
)
309 (defconst diff-yank-handler
'(diff-yank-function))
310 (defun diff-yank-function (text)
311 ;; FIXME: the yank-handler is now called separately on each piece of text
312 ;; with a yank-handler property, so the next-single-property-change call
313 ;; below will always return nil :-( --stef
314 (let ((mixed (next-single-property-change 0 'yank-handler text
))
316 ;; First insert the text.
318 ;; If the text does not include any diff markers and if we're not
319 ;; yanking back into a diff-mode buffer, get rid of the prefixes.
320 (unless (or mixed
(derived-mode-p 'diff-mode
))
321 (undo-boundary) ; Just in case the user wanted the prefixes.
322 (let ((re (save-excursion
323 (if (re-search-backward "^[><!][ \t]" start t
)
324 (if (eq (char-after) ?
!)
325 "^[!+- ][ \t]" "^[<>][ \t]")
328 (while (re-search-backward re start t
)
329 (replace-match "" t t
)))))))
332 (defvar diff-font-lock-keywords
333 `(("^\\(@@ -[0-9,]+ \\+[0-9,]+ @@\\)\\(.*\\)$" ;unified
334 (1 diff-hunk-header-face
) (2 diff-function-face
))
335 ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
336 (1 diff-hunk-header-face
) (2 diff-function-face
))
337 ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face
) ;context
338 ("^--- .+ ----$" . diff-hunk-header-face
) ;context
339 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face
) ;normal
340 ("^---$" . diff-hunk-header-face
) ;normal
341 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\(\\S-+\\)\\(.*[^*-]\\)?\n"
342 (0 diff-header-face
) (2 diff-file-header-face prepend
))
343 ("^\\([-<]\\)\\(.*\n\\)"
344 (1 diff-indicator-removed-face
) (2 diff-removed-face
))
345 ("^\\([+>]\\)\\(.*\n\\)"
346 (1 diff-indicator-added-face
) (2 diff-added-face
))
347 ("^\\(!\\)\\(.*\n\\)"
348 (1 diff-indicator-changed-face
) (2 diff-changed-face
))
349 ("^Index: \\(.+\\).*\n"
350 (0 diff-header-face
) (1 diff-index-face prepend
))
351 ("^Only in .*\n" . diff-nonexistent-face
)
353 (1 font-lock-comment-delimiter-face
)
354 (2 font-lock-comment-face
))
355 ("^[^-=+*!<>#].*\n" (0 diff-context-face
))))
357 (defconst diff-font-lock-defaults
358 '(diff-font-lock-keywords t nil nil nil
(font-lock-multiline . nil
)))
360 (defvar diff-imenu-generic-expression
361 ;; Prefer second name as first is most likely to be a backup or
362 ;; version-control name. The [\t\n] at the end of the unidiff pattern
363 ;; catches Debian source diff files (which lack the trailing date).
364 '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
365 (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
371 (defconst diff-hunk-header-re
"^\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")
372 (defconst diff-file-header-re
(concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re
1)))
373 (defvar diff-narrowed-to nil
)
375 (defun diff-end-of-hunk (&optional style
)
376 (when (looking-at diff-hunk-header-re
)
378 ;; Especially important for unified (because headers are ambiguous).
379 (setq style
(cdr (assq (char-after) '((?
@ . unified
) (?
* . context
))))))
380 (goto-char (match-end 0)))
381 (let ((end (and (re-search-forward (case style
382 ;; A `unified' header is ambiguous.
383 (unified (concat "^[^-+# \\]\\|"
384 diff-file-header-re
))
385 (context "^[^-+#! \\]")
389 (match-beginning 0))))
390 ;; The return value is used by easy-mmode-define-navigation.
391 (goto-char (or end
(point-max)))))
393 (defun diff-beginning-of-hunk ()
395 (unless (looking-at diff-hunk-header-re
)
398 (re-search-backward diff-hunk-header-re
)
399 (error (error "Can't find the beginning of the hunk")))))
401 (defun diff-beginning-of-file ()
403 (unless (looking-at diff-file-header-re
)
406 (re-search-backward diff-file-header-re
)
407 (error (error "Can't find the beginning of the file")))))
409 (defun diff-end-of-file ()
410 (re-search-forward "^[-+#!<>0-9@* \\]" nil t
)
411 (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re
)
413 (if (match-beginning 1)
414 (goto-char (match-beginning 1))
415 (beginning-of-line)))
417 ;; Define diff-{hunk,file}-{prev,next}
418 (easy-mmode-define-navigation
419 diff-hunk diff-hunk-header-re
"hunk" diff-end-of-hunk diff-restrict-view
)
420 (easy-mmode-define-navigation
421 diff-file diff-file-header-re
"file" diff-end-of-hunk
)
423 (defun diff-restrict-view (&optional arg
)
424 "Restrict the view to the current hunk.
425 If the prefix ARG is given, restrict the view to the current file instead."
428 (if arg
(diff-beginning-of-file) (diff-beginning-of-hunk))
429 (narrow-to-region (point)
430 (progn (if arg
(diff-end-of-file) (diff-end-of-hunk))
432 (set (make-local-variable 'diff-narrowed-to
) (if arg
'file
'hunk
))))
435 (defun diff-hunk-kill ()
438 (diff-beginning-of-hunk)
439 (let* ((start (point))
440 (nexthunk (when (re-search-forward diff-hunk-header-re nil t
)
441 (match-beginning 0)))
442 (firsthunk (ignore-errors
444 (diff-beginning-of-file) (diff-hunk-next) (point)))
445 (nextfile (ignore-errors (diff-file-next) (point)))
446 (inhibit-read-only t
))
448 (if (and firsthunk
(= firsthunk start
)
450 (and nextfile
(> nexthunk nextfile
))))
451 ;; It's the only hunk for this file, so kill the file.
454 (kill-region start
(point)))))
456 (defun diff-file-kill ()
457 "Kill current file's hunks."
459 (diff-beginning-of-file)
460 (let* ((start (point))
461 (prevhunk (save-excursion
463 (diff-hunk-prev) (point))))
464 (index (save-excursion
465 (re-search-backward "^Index: " prevhunk t
)))
466 (inhibit-read-only t
))
467 (when index
(setq start index
))
469 (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
470 (kill-region start
(point))))
472 (defun diff-kill-junk ()
473 "Kill spurious empty diffs."
476 (let ((inhibit-read-only t
))
477 (goto-char (point-min))
478 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
479 "\\([^-+!* <>].*\n\\)*?"
480 "\\(\\(Index:\\) \\|"
481 diff-file-header-re
"\\)")
483 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
485 (beginning-of-line)))))
487 (defun diff-count-matches (re start end
)
491 (while (re-search-forward re end t
) (incf n
))
494 (defun diff-split-hunk ()
495 "Split the current (unified diff) hunk at point into two hunks."
499 (start (progn (diff-beginning-of-hunk) (point))))
500 (unless (looking-at "@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@")
501 (error "diff-split-hunk only works on unified context diffs"))
503 (let* ((start1 (string-to-number (match-string 1)))
504 (start2 (string-to-number (match-string 2)))
505 (newstart1 (+ start1
(diff-count-matches "^[- \t]" (point) pos
)))
506 (newstart2 (+ start2
(diff-count-matches "^[+ \t]" (point) pos
)))
507 (inhibit-read-only t
))
509 ;; Hopefully the after-change-function will not screw us over.
510 (insert "@@ -" (number-to-string newstart1
) ",1 +"
511 (number-to-string newstart2
) ",1 @@\n")
512 ;; Fix the original hunk-header.
513 (diff-fixup-modifs start pos
))))
517 ;;;; jump to other buffers
520 (defvar diff-remembered-files-alist nil
)
522 (defun diff-filename-drop-dir (file)
523 (when (string-match "/" file
) (substring file
(match-end 0))))
525 (defun diff-merge-strings (ancestor from to
)
526 "Merge the diff between ANCESTOR and FROM into TO.
527 Returns the merged string if successful or nil otherwise.
528 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
529 If ANCESTOR = FROM, returns TO.
530 If ANCESTOR = TO, returns FROM.
531 The heuristic is simplistic and only really works for cases
532 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
534 ;; AMB ANB CMD -> CND
535 ;; but that's ambiguous if `foo' or `bar' is empty:
536 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
537 (let ((str (concat ancestor
"\n" from
"\n" to
)))
538 (when (and (string-match (concat
539 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
541 "\\(.*\\(\\2\\).*\\)\\'") str
)
542 (equal to
(match-string 5 str
)))
543 (concat (substring str
(match-beginning 5) (match-beginning 6))
545 (substring str
(match-end 6) (match-end 5))))))
547 (defun diff-tell-file-name (old name
)
548 "Tell Emacs where the find the source file of the current hunk.
549 If the OLD prefix arg is passed, tell the file NAME of the old file."
551 (let* ((old current-prefix-arg
)
552 (fs (diff-hunk-file-names current-prefix-arg
)))
553 (unless fs
(error "No file name to look for"))
554 (list old
(read-file-name (format "File for %s: " (car fs
))
555 nil
(diff-find-file-name old
) t
))))
556 (let ((fs (diff-hunk-file-names old
)))
557 (unless fs
(error "No file name to look for"))
558 (push (cons fs name
) diff-remembered-files-alist
)))
560 (defun diff-hunk-file-names (&optional old
)
561 "Give the list of file names textually mentioned for the current hunk."
563 (unless (looking-at diff-file-header-re
)
564 (or (ignore-errors (diff-beginning-of-file))
565 (re-search-forward diff-file-header-re nil t
)))
566 (let ((limit (save-excursion
568 (progn (diff-hunk-prev) (point))
569 (error (point-min)))))
571 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
572 (list (if old
(match-string 1) (match-string 3))
573 (if old
(match-string 3) (match-string 1)))
574 (forward-line 1) nil
)))
579 (re-search-backward "^Index: \\(.+\\)" limit t
)))
580 (list (match-string 1)))
582 (when (re-search-backward
583 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?"
585 (list (if old
(match-string 2) (match-string 4))
586 (if old
(match-string 4) (match-string 2)))))))))
588 (defun diff-find-file-name (&optional old prefix
)
589 "Return the file corresponding to the current patch.
590 Non-nil OLD means that we want the old file.
591 PREFIX is only used internally: don't use it."
593 (unless (looking-at diff-file-header-re
)
594 (or (ignore-errors (diff-beginning-of-file))
595 (re-search-forward diff-file-header-re nil t
)))
596 (let ((fs (diff-hunk-file-names old
)))
597 (if prefix
(setq fs
(mapcar (lambda (f) (concat prefix f
)) fs
)))
599 ;; use any previously used preference
600 (cdr (assoc fs diff-remembered-files-alist
))
601 ;; try to be clever and use previous choices as an inspiration
602 (dolist (rf diff-remembered-files-alist
)
603 (let ((newfile (diff-merge-strings (caar rf
) (car fs
) (cdr rf
))))
604 (if (and newfile
(file-exists-p newfile
)) (return newfile
))))
605 ;; look for each file in turn. If none found, try again but
606 ;; ignoring the first level of directory, ...
607 (do* ((files fs
(delq nil
(mapcar 'diff-filename-drop-dir files
)))
610 (setq file
(do* ((files files
(cdr files
))
611 (file (car files
) (car files
)))
612 ((or (null file
) (file-exists-p file
))
615 ;; <foo>.rej patches implicitly apply to <foo>
616 (and (string-match "\\.rej\\'" (or buffer-file-name
""))
617 (let ((file (substring buffer-file-name
0 (match-beginning 0))))
618 (when (file-exists-p file
) file
)))
619 ;; If we haven't found the file, maybe it's because we haven't paid
620 ;; attention to the PCL-CVS hint.
622 (boundp 'cvs-pcl-cvs-dirchange-re
)
624 (re-search-backward cvs-pcl-cvs-dirchange-re nil t
))
625 (diff-find-file-name old
(match-string 1)))
626 ;; if all else fails, ask the user
627 (let ((file (read-file-name (format "Use file %s: " (or (first fs
) ""))
628 nil
(first fs
) t
(first fs
))))
629 (set (make-local-variable 'diff-remembered-files-alist
)
630 (cons (cons fs file
) diff-remembered-files-alist
))
634 (defun diff-ediff-patch ()
635 "Call `ediff-patch-file' on the current buffer."
638 (ediff-patch-file nil
(current-buffer))
639 (wrong-number-of-arguments (ediff-patch-file))))
642 ;;;; Conversion functions
645 ;;(defvar diff-inhibit-after-change nil
646 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
648 (defun diff-unified->context
(start end
)
649 "Convert unified diffs to context diffs.
650 START and END are either taken from the region (if a prefix arg is given) or
651 else cover the whole bufer."
652 (interactive (if (or current-prefix-arg
(and transient-mark-mode mark-active
))
653 (list (region-beginning) (region-end))
654 (list (point-min) (point-max))))
655 (unless (markerp end
) (setq end
(copy-marker end t
)))
656 (let (;;(diff-inhibit-after-change t)
657 (inhibit-read-only t
))
660 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*\\)$" nil t
)
662 (combine-after-change-calls
663 (if (match-beginning 2)
664 ;; we matched a file header
666 ;; use reverse order to make sure the indices are kept valid
667 (replace-match "---" t t nil
3)
668 (replace-match "***" t t nil
2))
669 ;; we matched a hunk header
670 (let ((line1 (match-string 4))
671 (lines1 (match-string 5))
672 (line2 (match-string 6))
673 (lines2 (match-string 7)))
675 (concat "***************\n*** " line1
","
676 (number-to-string (+ (string-to-number line1
)
677 (string-to-number lines1
)
681 (narrow-to-region (point)
682 (progn (diff-end-of-hunk 'unified
) (point)))
683 (let ((hunk (buffer-string)))
684 (goto-char (point-min))
685 (if (not (save-excursion (re-search-forward "^-" nil t
)))
686 (delete-region (point) (point-max))
687 (goto-char (point-max))
688 (let ((modif nil
) last-pt
)
689 (while (progn (setq last-pt
(point))
690 (= (forward-line -
1) 0))
692 (?\s
(insert " ") (setq modif nil
) (backward-char 1))
693 (?
+ (delete-region (point) last-pt
) (setq modif t
))
695 (progn (forward-char 1)
700 (?
\\ (when (save-excursion (forward-line -
1)
702 (delete-region (point) last-pt
) (setq modif t
)))
703 (t (setq modif nil
))))))
704 (goto-char (point-max))
706 (insert "--- " line2
","
707 (number-to-string (+ (string-to-number line2
)
708 (string-to-number lines2
)
709 -
1)) " ----\n" hunk
))
710 ;;(goto-char (point-min))
712 (if (not (save-excursion (re-search-forward "^+" nil t
)))
713 (delete-region (point) (point-max))
714 (let ((modif nil
) (delete nil
))
717 (?\s
(insert " ") (setq modif nil
) (backward-char 1))
718 (?-
(setq delete t
) (setq modif t
))
720 (progn (forward-char 1)
725 (?
\\ (when (save-excursion (forward-line 1)
727 (setq delete t
) (setq modif t
)))
728 (t (setq modif nil
)))
729 (let ((last-pt (point)))
732 (delete-region last-pt
(point))
733 (setq delete nil
)))))))))))))))
735 (defun diff-context->unified
(start end
&optional to-context
)
736 "Convert context diffs to unified diffs.
737 START and END are either taken from the region
738 \(when it is highlighted) or else cover the whole buffer.
739 With a prefix argument, convert unified format to context format."
740 (interactive (if (and transient-mark-mode mark-active
)
741 (list (region-beginning) (region-end) current-prefix-arg
)
742 (list (point-min) (point-max) current-prefix-arg
)))
744 (diff-unified->context start end
)
745 (unless (markerp end
) (setq end
(copy-marker end t
)))
746 (let ( ;;(diff-inhibit-after-change t)
747 (inhibit-read-only t
))
750 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t
)
752 (combine-after-change-calls
753 (if (match-beginning 2)
754 ;; we matched a file header
756 ;; use reverse order to make sure the indices are kept valid
757 (replace-match "+++" t t nil
3)
758 (replace-match "---" t t nil
2))
759 ;; we matched a hunk header
760 (let ((line1s (match-string 4))
761 (line1e (match-string 5))
762 (pt1 (match-beginning 0)))
764 (unless (re-search-forward
765 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t
)
766 (error "Can't find matching `--- n1,n2 ----' line"))
767 (let ((line2s (match-string 1))
768 (line2e (match-string 2))
770 (delete-region (progn (beginning-of-line) (point))
771 (progn (forward-line 1) (point)))
775 (while (< (point) pt2
)
777 ((?
! ?-
) (delete-char 2) (insert "-") (forward-line 1))
778 (?\s
;merge with the other half of the chunk
781 (goto-char pt2
) (forward-line 1) (point)))
782 (c (char-after pt2
)))
786 (prog1 (buffer-substring (+ pt2
2) endline2
)
787 (delete-region pt2 endline2
))))
788 (?\s
;FIXME: check consistency
789 (delete-region pt2 endline2
)
792 (?
\\ (forward-line 1))
793 (t (delete-char 1) (forward-line 1)))))
794 (t (forward-line 1))))
795 (while (looking-at "[+! ] ")
796 (if (/= (char-after) ?
!) (forward-char 1)
797 (delete-char 1) (insert "+"))
798 (delete-char 1) (forward-line 1))
801 (insert "@@ -" line1s
","
802 (number-to-string (- (string-to-number line1e
)
803 (string-to-number line1s
)
806 (number-to-string (- (string-to-number line2e
)
807 (string-to-number line2s
)
808 -
1)) " @@")))))))))))
810 (defun diff-reverse-direction (start end
)
811 "Reverse the direction of the diffs.
812 START and END are either taken from the region (if a prefix arg is given) or
813 else cover the whole bufer."
814 (interactive (if (or current-prefix-arg
(and transient-mark-mode mark-active
))
815 (list (region-beginning) (region-end))
816 (list (point-min) (point-max))))
817 (unless (markerp end
) (setq end
(copy-marker end t
)))
818 (let (;;(diff-inhibit-after-change t)
819 (inhibit-read-only t
))
822 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t
)
824 (combine-after-change-calls
827 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil
))
828 ;; a context-diff hunk header
830 (let ((pt-lines1 (match-beginning 6))
831 (lines1 (match-string 6)))
832 (replace-match "" nil nil nil
6)
834 (let ((half1s (point)))
835 (while (looking-at "[-! \\][ \t]\\|#")
836 (when (= (char-after) ?-
) (delete-char 1) (insert "+"))
838 (let ((half1 (delete-and-extract-region half1s
(point))))
839 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
841 (error "Can't find matching `--- n1,n2 ----' line"))
842 (let ((str1 (match-string 1)))
843 (replace-match lines1 nil nil nil
1)
845 (let ((half2s (point)))
846 (while (looking-at "[!+ \\][ \t]\\|#")
847 (when (= (char-after) ?
+) (delete-char 1) (insert "-"))
849 (let ((half2 (delete-and-extract-region half2s
(point))))
850 (insert (or half1
""))
852 (insert (or half2
""))))
853 (goto-char pt-lines1
)
855 ;; a unified-diff hunk header
857 (replace-match "@@ -\\8 +\\7 @@" nil
)
859 (let ((c (char-after)) first last
)
860 (while (case (setq c
(char-after))
861 (?-
(setq first
(or first
(point)))
862 (delete-char 1) (insert "+") t
)
863 (?
+ (setq last
(or last
(point)))
864 (delete-char 1) (insert "-") t
)
866 (t (when (and first last
(< first last
))
867 (insert (delete-and-extract-region first last
)))
868 (setq first nil last nil
)
870 (forward-line 1))))))))))
872 (defun diff-fixup-modifs (start end
)
873 "Fixup the hunk headers (in case the buffer was modified).
874 START and END are either taken from the region (if a prefix arg is given) or
875 else cover the whole bufer."
876 (interactive (if (or current-prefix-arg
(and transient-mark-mode mark-active
))
877 (list (region-beginning) (region-end))
878 (list (point-min) (point-max))))
879 (let ((inhibit-read-only t
))
881 (goto-char end
) (diff-end-of-hunk)
882 (let ((plus 0) (minus 0) (space 0) (bang 0))
883 (while (and (= (forward-line -
1) 0) (<= start
(point)))
885 (concat "@@ -[0-9,]+ \\+[0-9,]+ @@"
886 "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$"
887 "\\|--- .+\n\\+\\+\\+ ")))
894 (t (setq space
0 plus
0 minus
0 bang
0)))
896 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@.*$")
897 (let* ((old1 (match-string 1))
898 (old2 (match-string 2))
899 (new1 (number-to-string (+ space minus
)))
900 (new2 (number-to-string (+ space plus
))))
901 (unless (string= new2 old2
) (replace-match new2 t t nil
2))
902 (unless (string= new1 old1
) (replace-match new1 t t nil
1))))
903 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
904 (when (> (+ space bang plus
) 0)
905 (let* ((old1 (match-string 1))
906 (old2 (match-string 2))
907 (new (number-to-string
908 (+ space bang plus -
1 (string-to-number old1
)))))
909 (unless (string= new old2
) (replace-match new t t nil
2)))))
910 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
911 (when (> (+ space bang minus
) 0)
912 (let* ((old (match-string 1))
914 (concat "%0" (number-to-string (length old
)) "d")
915 (+ space bang minus -
1 (string-to-number old
)))))
916 (unless (string= new old
) (replace-match new t t nil
2))))))
917 (setq space
0 plus
0 minus
0 bang
0)))))))
923 (defun diff-write-contents-hooks ()
924 "Fixup hunk headers if necessary."
925 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
928 ;; It turns out that making changes in the buffer from within an
929 ;; *-change-function is asking for trouble, whereas making them
930 ;; from a post-command-hook doesn't pose much problems
931 (defvar diff-unhandled-changes nil
)
932 (defun diff-after-change-function (beg end len
)
933 "Remember to fixup the hunk header.
934 See `after-change-functions' for the meaning of BEG, END and LEN."
935 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
936 ;; incorrect, but it turns out that inhibit-read-only is normally not set
937 ;; inside editing commands, while it tends to be set when the buffer gets
938 ;; updated by an async process or by a conversion function, both of which
939 ;; would rather not be uselessly slowed down by this hook.
940 (when (and (not undo-in-progress
) (not inhibit-read-only
))
941 (if diff-unhandled-changes
942 (setq diff-unhandled-changes
943 (cons (min beg
(car diff-unhandled-changes
))
944 (max end
(cdr diff-unhandled-changes
))))
945 (setq diff-unhandled-changes
(cons beg end
)))))
947 (defun diff-post-command-hook ()
948 "Fixup hunk headers if necessary."
949 (when (consp diff-unhandled-changes
)
952 (goto-char (car diff-unhandled-changes
))
953 ;; Maybe we've cut the end of the hunk before point.
954 (if (and (bolp) (not (bobp))) (backward-char 1))
955 ;; We used to fixup modifs on all the changes, but it turns out
956 ;; that it's safer not to do it on big changes, for example
957 ;; when yanking a big diff, since we might then screw up perfectly
958 ;; correct values. -stef
959 ;; (unless (ignore-errors
960 ;; (diff-beginning-of-hunk)
962 ;; (diff-end-of-hunk)
963 ;; (> (point) (car diff-unhandled-changes))))
964 ;; (goto-char (car diff-unhandled-changes))
965 ;; (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
966 ;; (diff-beginning-of-hunk))
967 ;; (diff-fixup-modifs (point) (cdr diff-unhandled-changes))
968 (diff-beginning-of-hunk)
969 (when (save-excursion
971 (>= (point) (cdr diff-unhandled-changes
)))
972 (diff-fixup-modifs (point) (cdr diff-unhandled-changes
)))))
973 (setq diff-unhandled-changes nil
)))
975 (defun diff-next-error (arg reset
)
976 ;; Select a window that displays the current buffer so that point
977 ;; movements are reflected in that window. Otherwise, the user might
978 ;; never see the hunk corresponding to the source she's jumping to.
979 (pop-to-buffer (current-buffer))
980 (if reset
(goto-char (point-min)))
985 (define-derived-mode diff-mode fundamental-mode
"Diff"
986 "Major mode for viewing/editing context diffs.
987 Supports unified and context diffs as well as (to a lesser extent)
989 When the buffer is read-only, the ESC prefix is not necessary.
990 If you edit the buffer manually, diff-mode will try to update the hunk
991 headers for you on-the-fly.
993 You can also switch between context diff and unified diff with \\[diff-context->unified],
994 or vice versa with \\[diff-unified->context] and you can also reverse the direction of
995 a diff with \\[diff-reverse-direction].
997 (set (make-local-variable 'font-lock-defaults
) diff-font-lock-defaults
)
998 (set (make-local-variable 'outline-regexp
) diff-outline-regexp
)
999 (set (make-local-variable 'imenu-generic-expression
)
1000 diff-imenu-generic-expression
)
1001 ;; These are not perfect. They would be better done separately for
1002 ;; context diffs and unidiffs.
1003 ;; (set (make-local-variable 'paragraph-start)
1004 ;; (concat "@@ " ; unidiff hunk
1005 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
1006 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
1007 ;; ; start (first or second line)
1008 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
1009 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
1011 (set (make-local-variable 'next-error-function
) 'diff-next-error
)
1013 (setq buffer-read-only diff-default-read-only
)
1014 ;; setup change hooks
1015 (if (not diff-update-on-the-fly
)
1016 (add-hook 'write-contents-functions
'diff-write-contents-hooks nil t
)
1017 (make-local-variable 'diff-unhandled-changes
)
1018 (add-hook 'after-change-functions
'diff-after-change-function nil t
)
1019 (add-hook 'post-command-hook
'diff-post-command-hook nil t
))
1020 ;; Neat trick from Dave Love to add more bindings in read-only mode:
1021 (lexical-let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map
)))
1022 (add-to-list 'minor-mode-overriding-map-alist ro-bind
)
1023 ;; Turn off this little trick in case the buffer is put in view-mode.
1024 (add-hook 'view-mode-hook
1026 (setq minor-mode-overriding-map-alist
1027 (delq ro-bind minor-mode-overriding-map-alist
)))
1030 (set (make-local-variable 'add-log-current-defun-function
)
1031 'diff-current-defun
)
1032 (set (make-local-variable 'add-log-buffer-file-name-function
)
1033 'diff-find-file-name
))
1036 (define-minor-mode diff-minor-mode
1037 "Minor mode for viewing/editing context diffs.
1038 \\{diff-minor-mode-map}"
1039 :group
'diff-mode
:lighter
" Diff"
1040 ;; FIXME: setup font-lock
1041 ;; setup change hooks
1042 (if (not diff-update-on-the-fly
)
1043 (add-hook 'write-contents-functions
'diff-write-contents-hooks nil t
)
1044 (make-local-variable 'diff-unhandled-changes
)
1045 (add-hook 'after-change-functions
'diff-after-change-function nil t
)
1046 (add-hook 'post-command-hook
'diff-post-command-hook nil t
)))
1048 ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1050 (defun diff-delete-if-empty ()
1051 ;; An empty diff file means there's no more diffs to integrate, so we
1052 ;; can just remove the file altogether. Very handy for .rej files if we
1053 ;; remove hunks as we apply them.
1054 (when (and buffer-file-name
1055 (eq 0 (nth 7 (file-attributes buffer-file-name
))))
1056 (delete-file buffer-file-name
)))
1058 (defun diff-delete-empty-files ()
1059 "Arrange for empty diff files to be removed."
1060 (add-hook 'after-save-hook
'diff-delete-if-empty nil t
))
1062 (defun diff-make-unified ()
1063 "Turn context diffs into unified diffs if applicable."
1065 (goto-char (point-min))
1066 (and (looking-at diff-hunk-header-re
) (eq (char-after) ?
*)))
1067 (let ((mod (buffer-modified-p)))
1069 (diff-context->unified
(point-min) (point-max))
1070 (restore-buffer-modified-p mod
)))))
1073 ;;; Misc operations that have proved useful at some point.
1076 (defun diff-next-complex-hunk ()
1077 "Jump to the next \"complex\" hunk.
1078 \"Complex\" is approximated by \"the hunk changes the number of lines\".
1079 Only works for unified diffs."
1082 (and (re-search-forward "^@@ [-0-9]+,\\([0-9]+\\) [+0-9]+,\\([0-9]+\\) @@"
1084 (equal (match-string 1) (match-string 2)))))
1086 (defun diff-hunk-text (hunk destp char-offset
)
1087 "Return the literal source text from HUNK as (TEXT . OFFSET).
1088 If DESTP is nil, TEXT is the source, otherwise the destination text.
1089 CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding
1090 char-offset in TEXT."
1093 (goto-char (point-min))
1098 ;; Set the following variables:
1099 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
1100 ;; DST-POS buffer pos of the destination part of the hunk or nil
1101 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
1102 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
1103 (cond ((looking-at "^@@")
1105 (setq num-pfx-chars
1)
1107 (setq src-pos
(point) dst-pos
(point)))
1108 ((looking-at "^\\*\\*")
1111 (setq src-pos
(point))
1112 (re-search-forward "^--- " nil t
)
1114 (setq divider-pos
(point))
1116 (setq dst-pos
(point)))
1117 ((looking-at "^[0-9]+a[0-9,]+$")
1118 ;; normal diff, insert
1120 (setq dst-pos
(point)))
1121 ((looking-at "^[0-9,]+d[0-9]+$")
1122 ;; normal diff, delete
1124 (setq src-pos
(point)))
1125 ((looking-at "^[0-9,]+c[0-9,]+$")
1126 ;; normal diff, change
1128 (setq src-pos
(point))
1129 (re-search-forward "^---$" nil t
)
1131 (setq divider-pos
(point))
1133 (setq dst-pos
(point)))
1135 (error "Unknown diff hunk type")))
1137 (if (if destp
(null dst-pos
) (null src-pos
))
1138 ;; Implied empty text
1139 (if char-offset
'("" .
0) "")
1141 ;; For context diffs, either side can be empty, (if there's only
1142 ;; added or only removed text). We should then use the other side.
1143 (cond ((equal src-pos divider-pos
) (setq src-pos dst-pos
))
1144 ((equal dst-pos
(point-max)) (setq dst-pos src-pos
)))
1146 (when char-offset
(goto-char (+ (point-min) char-offset
)))
1148 ;; Get rid of anything except the desired text.
1150 ;; Delete unused text region
1151 (let ((keep (if destp dst-pos src-pos
)))
1152 (when (and divider-pos
(> divider-pos keep
))
1153 (delete-region divider-pos
(point-max)))
1154 (delete-region (point-min) keep
))
1155 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1156 (let ((kill-char (if destp ?- ?
+)))
1157 (goto-char (point-min))
1159 (if (eq (char-after) kill-char
)
1160 (delete-region (point) (progn (forward-line 1) (point)))
1161 (delete-char num-pfx-chars
)
1162 (forward-line 1)))))
1164 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1165 (if char-offset
(cons text
(- (point) (point-min))) text
))))))
1168 (defun diff-find-text (text)
1169 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1170 If TEXT isn't found, nil is returned."
1171 (let* ((orig (point))
1172 (forw (and (search-forward text nil t
)
1173 (cons (match-beginning 0) (match-end 0))))
1174 (back (and (goto-char (+ orig
(length text
)))
1175 (search-backward text nil t
)
1176 (cons (match-beginning 0) (match-end 0)))))
1177 ;; Choose the closest match.
1179 (if (> (- (car forw
) orig
) (- orig
(car back
))) back forw
)
1182 (defun diff-find-approx-text (text)
1183 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1184 Whitespace differences are ignored."
1185 (let* ((orig (point))
1186 (re (concat "^[ \t\n\f]*"
1187 (mapconcat 'regexp-quote
(split-string text
) "[ \t\n\f]+")
1189 (forw (and (re-search-forward re nil t
)
1190 (cons (match-beginning 0) (match-end 0))))
1191 (back (and (goto-char (+ orig
(length text
)))
1192 (re-search-backward re nil t
)
1193 (cons (match-beginning 0) (match-end 0)))))
1194 ;; Choose the closest match.
1196 (if (> (- (car forw
) orig
) (- orig
(car back
))) back forw
)
1199 (defsubst diff-xor
(a b
) (if a
(not b
) b
))
1201 (defun diff-find-source-location (&optional other-file reverse
)
1202 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
1203 BUF is the buffer corresponding to the source file.
1204 LINE-OFFSET is the offset between the expected and actual positions
1205 of the text of the hunk or nil if the text was not found.
1206 POS is a pair (BEG . END) indicating the position of the text in the buffer.
1207 SRC and DST are the two variants of text as returned by `diff-hunk-text'.
1208 SRC is the variant that was found in the buffer.
1209 SWITCHED is non-nil if the patch is already applied."
1211 (let* ((other (diff-xor other-file diff-jump-to-old-file
))
1212 (char-offset (- (point) (progn (diff-beginning-of-hunk) (point))))
1213 (hunk (buffer-substring (point)
1214 (save-excursion (diff-end-of-hunk) (point))))
1215 (old (diff-hunk-text hunk reverse char-offset
))
1216 (new (diff-hunk-text hunk
(not reverse
) char-offset
))
1217 ;; Find the location specification.
1218 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
1219 (error "Can't find the hunk header")
1220 (if other
(match-string 1)
1221 (if (match-end 3) (match-string 3)
1222 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t
)
1223 (error "Can't find the hunk separator"))
1224 (match-string 1)))))
1225 (file (or (diff-find-file-name other
) (error "Can't find the file")))
1226 (buf (find-file-noselect file
)))
1227 ;; Update the user preference if he so wished.
1228 (when (> (prefix-numeric-value other-file
) 8)
1229 (setq diff-jump-to-old-file other
))
1230 (with-current-buffer buf
1231 (goto-line (string-to-number line
))
1232 (let* ((orig-pos (point))
1234 ;; FIXME: Check for case where both OLD and NEW are found.
1235 (pos (or (diff-find-text (car old
))
1236 (progn (setq switched t
) (diff-find-text (car new
)))
1237 (progn (setq switched nil
)
1239 (diff-find-approx-text (car old
))
1240 (invalid-regexp nil
))) ;Regex too big.
1241 (progn (setq switched t
)
1243 (diff-find-approx-text (car new
))
1244 (invalid-regexp nil
))) ;Regex too big.
1245 (progn (setq switched nil
) nil
))))
1249 (list (count-lines orig-pos
(car pos
)) pos
)
1250 (list nil
(cons orig-pos
(+ orig-pos
(length (car old
))))))
1251 (if switched
(list new old t
) (list old new
))))))))
1254 (defun diff-hunk-status-msg (line-offset reversed dry-run
)
1255 (let ((msg (if dry-run
1256 (if reversed
"already applied" "not yet applied")
1257 (if reversed
"undone" "applied"))))
1258 (message (cond ((null line-offset
) "Hunk text not found")
1259 ((= line-offset
0) "Hunk %s")
1260 ((= line-offset
1) "Hunk %s at offset %d line")
1261 (t "Hunk %s at offset %d lines"))
1264 (defvar diff-apply-hunk-to-backup-file nil
)
1266 (defun diff-apply-hunk (&optional reverse
)
1267 "Apply the current hunk to the source file and go to the next.
1268 By default, the new source file is patched, but if the variable
1269 `diff-jump-to-old-file' is non-nil, then the old source file is
1270 patched instead (some commands, such as `diff-goto-source' can change
1271 the value of this variable when given an appropriate prefix argument).
1273 With a prefix argument, REVERSE the hunk."
1275 (destructuring-bind (buf line-offset pos old new
&optional switched
)
1276 ;; If REVERSE go to the new file, otherwise go to the old.
1277 (diff-find-source-location (not reverse
) reverse
)
1280 (error "Can't find the text to patch"))
1281 ((with-current-buffer buf
1282 (and buffer-file-name
1283 (backup-file-name-p buffer-file-name
)
1284 (not diff-apply-hunk-to-backup-file
)
1285 (not (set (make-local-variable 'diff-apply-hunk-to-backup-file
)
1286 (yes-or-no-p (format "Really apply this hunk to %s? "
1287 (file-name-nondirectory
1288 buffer-file-name
)))))))
1289 (error (substitute-command-keys
1290 (format "Use %s\\[diff-apply-hunk] to apply it to the other file"
1291 (if (not reverse
) "\\[universal-argument] ")))))
1293 ;; A reversed patch was detected, perhaps apply it in reverse.
1294 (not (save-window-excursion
1296 (goto-char (+ (car pos
) (cdr old
)))
1299 "Hunk hasn't been applied yet; apply it now? "
1300 "Hunk has already been applied; undo it? ")))))
1301 (message "(Nothing done)"))
1304 (with-current-buffer buf
1305 (goto-char (car pos
))
1306 (delete-region (car pos
) (cdr pos
))
1308 ;; Display BUF in a window
1309 (set-window-point (display-buffer buf
) (+ (car pos
) (cdr new
)))
1310 (diff-hunk-status-msg line-offset
(diff-xor switched reverse
) nil
)
1311 (when diff-advance-after-apply-hunk
1312 (diff-hunk-next))))))
1315 (defun diff-test-hunk (&optional reverse
)
1316 "See whether it's possible to apply the current hunk.
1317 With a prefix argument, try to REVERSE the hunk."
1319 (destructuring-bind (buf line-offset pos src dst
&optional switched
)
1320 ;; If REVERSE go to the new file, otherwise go to the old.
1321 (diff-find-source-location (not reverse
) reverse
)
1322 (set-window-point (display-buffer buf
) (+ (car pos
) (cdr src
)))
1323 (diff-hunk-status-msg line-offset
(diff-xor reverse switched
) t
)))
1326 (defalias 'diff-mouse-goto-source
'diff-goto-source
)
1328 (defun diff-goto-source (&optional other-file event
)
1329 "Jump to the corresponding source line.
1330 `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
1331 is given) determines whether to jump to the old or the new file.
1332 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1333 then `diff-jump-to-old-file' is also set, for the next invocations."
1334 (interactive (list current-prefix-arg last-input-event
))
1335 ;; When pointing at a removal line, we probably want to jump to
1336 ;; the old location, and else to the new (i.e. as if reverting).
1337 ;; This is a convenient detail when using smerge-diff.
1338 (if event
(posn-set-point (event-end event
)))
1339 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1340 (destructuring-bind (buf line-offset pos src dst
&optional switched
)
1341 (diff-find-source-location other-file rev
)
1343 (goto-char (+ (car pos
) (cdr src
)))
1344 (diff-hunk-status-msg line-offset
(diff-xor rev switched
) t
))))
1347 (defun diff-current-defun ()
1348 "Find the name of function at point.
1349 For use in `add-log-current-defun-function'."
1351 (when (looking-at diff-hunk-header-re
)
1353 (re-search-forward "^[^ ]" nil t
))
1354 (destructuring-bind (buf line-offset pos src dst
&optional switched
)
1355 (diff-find-source-location)
1357 (or (when (memq (char-after) '(?
< ?-
))
1358 ;; Cursor is pointing at removed text. This could be a removed
1359 ;; function, in which case, going to the source buffer will
1360 ;; not help since the function is now removed. Instead,
1361 ;; try to figure out the function name just from the code-fragment.
1362 (let ((old (if switched dst src
)))
1365 (funcall (with-current-buffer buf major-mode
))
1366 (goto-char (+ (point-min) (cdr old
)))
1367 (add-log-current-defun))))
1368 (with-current-buffer buf
1369 (goto-char (+ (car pos
) (cdr src
)))
1370 (add-log-current-defun))))))
1372 (defun diff-refine-hunk ()
1373 "Refine the current hunk by ignoring space differences."
1375 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk) (point))))
1376 (opts (case (char-after) (?
@ "-bu") (?
* "-bc") (t "-b")))
1377 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
1378 (error "Can't find line number"))
1379 (string-to-number (match-string 1))))
1380 (hunk (delete-and-extract-region
1381 (point) (save-excursion (diff-end-of-hunk) (point))))
1382 (lead (make-string (1- line-nb
) ?
\n)) ;Line nums start at 1.
1383 (file1 (make-temp-file "diff1"))
1384 (file2 (make-temp-file "diff2"))
1385 (coding-system-for-read buffer-file-coding-system
)
1386 (inhibit-read-only t
)
1390 (setq old
(diff-hunk-text hunk nil char-offset
))
1391 (setq new
(diff-hunk-text hunk t char-offset
))
1392 (write-region (concat lead
(car old
)) nil file1 nil
'nomessage
)
1393 (write-region (concat lead
(car new
)) nil file2 nil
'nomessage
)
1396 (call-process diff-command nil t nil
1399 (0 nil
) ;Nothing to reformat.
1400 (1 (goto-char (point-min))
1401 ;; Remove the file-header.
1402 (when (re-search-forward diff-hunk-header-re nil t
)
1403 (delete-region (point-min) (match-beginning 0))))
1404 (t (goto-char (point-max))
1405 (unless (bolp) (insert "\n"))
1407 (setq hunk
(buffer-string))
1408 (unless (memq status
'(0 1))
1409 (error "Diff returned: %s" status
)))))
1410 ;; Whatever happens, put back some equivalent text: either the new
1411 ;; one or the original one in case some error happened.
1414 (delete-file file2
))))
1416 ;; provide the package
1417 (provide 'diff-mode
)
1419 ;;; Old Change Log from when diff-mode wasn't part of Emacs:
1420 ;; Revision 1.11 1999/10/09 23:38:29 monnier
1421 ;; (diff-mode-load-hook): dropped.
1422 ;; (auto-mode-alist): also catch *.diffs.
1423 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
1424 ;; for *.rej files (that lack any file name indication).
1426 ;; Revision 1.10 1999/09/30 15:32:11 monnier
1427 ;; added support for "\ No newline at end of file".
1429 ;; Revision 1.9 1999/09/15 00:01:13 monnier
1430 ;; - added basic `compile' support.
1431 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
1432 ;; - diff-kill-file now tries to kill the leading garbage as well.
1434 ;; Revision 1.8 1999/09/13 21:10:09 monnier
1435 ;; - don't use CL in the autoloaded code
1436 ;; - accept diffs using -T
1438 ;; Revision 1.7 1999/09/05 20:53:03 monnier
1439 ;; interface to ediff-patch
1441 ;; Revision 1.6 1999/09/01 20:55:13 monnier
1442 ;; (ediff=patch-file): add bindings to call ediff-patch.
1443 ;; (diff-find-file-name): taken out of diff-goto-source.
1444 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
1445 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
1447 ;; Revision 1.5 1999/08/31 19:18:52 monnier
1448 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
1450 ;; Revision 1.4 1999/08/31 13:01:44 monnier
1451 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
1454 ;; arch-tag: 2571d7ff-bc28-4cf9-8585-42e21890be66
1455 ;;; diff-mode.el ends here