1 ;;; diff-mode.el --- a mode for viewing/editing context diffs
3 ;; Copyright (C) 1998,1999,2000,01,02,03,2004 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: convenience patch diff
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Provides support for font-lock, outline, navigation
28 ;; commands, editing and various conversions as well as jumping
29 ;; to the corresponding source file.
31 ;; Inspired by Pavel Machek's patch-mode.el (<pavel@@atrey.karlin.mff.cuni.cz>)
32 ;; Some efforts were spent to have it somewhat compatible with XEmacs'
33 ;; diff-mode as well as with compilation-minor-mode
37 ;; - Reverse doesn't work with normal diffs.
41 ;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
42 ;; Also allow C-c C-a to delete already-applied hunks.
44 ;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location
45 ;; of a hunk. Show then the changes between <file> and <hunk> and make it
46 ;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>.
47 ;; Or maybe just make it into a ".rej to diff3-markers converter".
49 ;; - Refine hunk on a word-by-word basis.
51 ;; - Use the new next-error-function to allow C-x `.
52 ;; - Handle `diff -b' output in context->unified.
56 (eval-when-compile (require 'cl))
59 (defgroup diff-mode ()
60 "Major mode for viewing/editing diffs"
65 (defcustom diff-default-read-only nil
66 "If non-nil, `diff-mode' buffers default to being read-only."
70 (defcustom diff-jump-to-old-file nil
71 "*Non-nil means `diff-goto-source' jumps to the old file.
72 Else, it jumps to the new file."
75 (defcustom diff-update-on-the-fly t
76 "*Non-nil means hunk headers are kept up-to-date on-the-fly.
77 When editing a diff file, the line numbers in the hunk headers
78 need to be kept consistent with the actual diff. This can
79 either be done on the fly (but this sometimes interacts poorly with the
80 undo mechanism) or whenever the file is written (can be slow
81 when editing big diffs)."
84 (defcustom diff-advance-after-apply-hunk t
85 "*Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
89 (defcustom diff-mode-hook nil
90 "Run after setting up the `diff-mode' major mode."
92 :options '(diff-delete-empty-files diff-make-unified))
94 (defvar diff-outline-regexp
95 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
98 ;;;; keymap, menu, ...
101 (easy-mmode-defmap diff-mode-shared-map
102 '(;; From Pavel Machek's patch-mode.
103 ("n" . diff-hunk-next)
104 ("N" . diff-file-next)
105 ("p" . diff-hunk-prev)
106 ("P" . diff-file-prev)
107 ("k" . diff-hunk-kill)
108 ("K" . diff-file-kill)
109 ;; From compilation-minor-mode.
110 ("}" . diff-file-next)
111 ("{" . diff-file-prev)
112 ("\C-m" . diff-goto-source)
113 ([mouse-2] . diff-mouse-goto-source)
114 ;; From XEmacs' diff-mode.
116 ;;("." . diff-goto-source) ;display-buffer
117 ;;("f" . diff-goto-source) ;find-file
118 ("o" . diff-goto-source) ;other-window
119 ;;("w" . diff-goto-source) ;other-frame
120 ;;("N" . diff-narrow)
121 ;;("h" . diff-show-header)
122 ;;("j" . diff-show-difference) ;jump to Nth diff
125 ("\177" . scroll-down)
126 ;; Our very own bindings.
127 ("A" . diff-ediff-patch)
128 ("r" . diff-restrict-view)
129 ("R" . diff-reverse-direction)
130 ("U" . diff-context->unified)
131 ("C" . diff-unified->context)
133 "Basic keymap for `diff-mode', bound to various prefix keys.")
135 (easy-mmode-defmap diff-mode-map
136 `(("\e" . ,diff-mode-shared-map)
137 ;; From compilation-minor-mode.
138 ("\C-c\C-c" . diff-goto-source)
140 ("\C-c\C-r" . diff-refine-hunk)
141 ("\C-c\C-s" . diff-split-hunk)
142 ("\C-c\C-a" . diff-apply-hunk)
143 ("\C-c\C-t" . diff-test-hunk))
144 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
146 (easy-menu-define diff-mode-menu diff-mode-map
147 "Menu for `diff-mode'."
149 ["Jump to Source" diff-goto-source t]
150 ["Apply hunk" diff-apply-hunk t]
151 ["Apply diff with Ediff" diff-ediff-patch t]
153 ["Reverse direction" diff-reverse-direction t]
154 ["Context -> Unified" diff-context->unified t]
155 ["Unified -> Context" diff-unified->context t]
156 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
159 (defcustom diff-minor-mode-prefix "\C-c="
160 "Prefix key for `diff-minor-mode' commands."
161 :type '(choice (string "\e") (string "C-c=") string))
163 (easy-mmode-defmap diff-minor-mode-map
164 `((,diff-minor-mode-prefix . ,diff-mode-shared-map))
165 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
169 ;;;; font-lock support
172 (defface diff-header-face
173 '((((type tty pc) (class color) (background light))
174 (:foreground "blue1" :weight bold))
175 (((type tty pc) (class color) (background dark))
176 (:foreground "green" :weight bold))
177 (((class color) (background light))
178 (:background "grey85"))
179 (((class color) (background dark))
180 (:background "grey45"))
182 "`diff-mode' face inherited by hunk and index header faces.")
183 (defvar diff-header-face 'diff-header-face)
185 (defface diff-file-header-face
186 '((((type tty pc) (class color) (background light))
187 (:foreground "yellow" :weight bold))
188 (((type tty pc) (class color) (background dark))
189 (:foreground "cyan" :weight bold))
190 (((class color) (background light))
191 (:background "grey70" :weight bold))
192 (((class color) (background dark))
193 (:background "grey60" :weight bold))
194 (t (:weight bold))) ; :height 1.3
195 "`diff-mode' face used to highlight file header lines.")
196 (defvar diff-file-header-face 'diff-file-header-face)
198 (defface diff-index-face
199 '((t (:inherit diff-file-header-face)))
200 "`diff-mode' face used to highlight index header lines.")
201 (defvar diff-index-face 'diff-index-face)
203 (defface diff-hunk-header-face
204 '((t (:inherit diff-header-face)))
205 "`diff-mode' face used to highlight hunk header lines.")
206 (defvar diff-hunk-header-face 'diff-hunk-header-face)
208 (defface diff-removed-face
209 '((t (:inherit diff-changed-face)))
210 "`diff-mode' face used to highlight removed lines.")
211 (defvar diff-removed-face 'diff-removed-face)
213 (defface diff-added-face
214 '((t (:inherit diff-changed-face)))
215 "`diff-mode' face used to highlight added lines.")
216 (defvar diff-added-face 'diff-added-face)
218 (defface diff-changed-face
219 '((((type tty pc) (class color) (background light))
220 (:foreground "magenta" :weight bold :slant italic))
221 (((type tty pc) (class color) (background dark))
222 (:foreground "yellow" :weight bold :slant italic))
224 "`diff-mode' face used to highlight changed lines.")
225 (defvar diff-changed-face 'diff-changed-face)
227 (defface diff-function-face
228 '((t (:inherit diff-context-face)))
229 "`diff-mode' face used to highlight function names produced by \"diff -p\".")
230 (defvar diff-function-face 'diff-function-face)
232 (defface diff-context-face
233 '((((class color) (background light))
234 (:foreground "grey50"))
235 (((class color) (background dark))
236 (:foreground "grey70"))
238 "`diff-mode' face used to highlight context and other side-information.")
239 (defvar diff-context-face 'diff-context-face)
241 (defface diff-nonexistent-face
242 '((t (:inherit diff-file-header-face)))
243 "`diff-mode' face used to highlight nonexistent files in recursive diffs.")
244 (defvar diff-nonexistent-face 'diff-nonexistent-face)
246 (defconst diff-yank-handler '(diff-yank-function))
247 (defun diff-yank-function (text)
248 ;; FIXME: the yank-handler is now called separately on each piece of text
249 ;; with a yank-handler property, so the next-single-property-change call
250 ;; below will always return nil :-( --stef
251 (let ((mixed (next-single-property-change 0 'yank-handler text))
253 ;; First insert the text.
255 ;; If the text does not include any diff markers and if we're not
256 ;; yanking back into a diff-mode buffer, get rid of the prefixes.
257 (unless (or mixed (derived-mode-p 'diff-mode))
258 (undo-boundary) ; Just in case the user wanted the prefixes.
259 (let ((re (save-excursion
260 (if (re-search-backward "^[><!][ \t]" start t)
261 (if (eq (char-after) ?!)
262 "^[!+- ][ \t]" "^[<>][ \t]")
265 (while (re-search-backward re start t)
266 (replace-match "" t t)))))))
269 (defvar diff-font-lock-keywords
270 `(("^\\(@@ -[0-9,]+ \\+[0-9,]+ @@\\)\\(.*\\)$" ;unified
271 (1 diff-hunk-header-face)
272 (2 diff-function-face))
273 ("^--- .+ ----$" . diff-hunk-header-face) ;context
274 ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
275 (1 diff-hunk-header-face)
276 (2 diff-function-face))
277 ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context
278 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\(\\S-+\\)\\(.*[^*-]\\)?\n"
279 (0 diff-header-face) (2 diff-file-header-face prepend))
280 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face)
281 ("^!.*\n" (0 diff-changed-face))
282 ("^[+>].*\n" (0 diff-added-face))
283 ("^[-<].*\n" (0 diff-removed-face))
284 ("^Index: \\(.+\\).*\n" (0 diff-header-face) (1 diff-index-face prepend))
285 ("^Only in .*\n" . diff-nonexistent-face)
286 ("^#.*" . font-lock-string-face)
287 ("^[^-=+*!<>].*\n" (0 diff-context-face))))
289 (defconst diff-font-lock-defaults
290 '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
292 (defvar diff-imenu-generic-expression
293 ;; Prefer second name as first is most likely to be a backup or
294 ;; version-control name. The [\t\n] at the end of the unidiff pattern
295 ;; catches Debian source diff files (which lack the trailing date).
296 '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
297 (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
303 (defconst diff-hunk-header-re "^\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")
304 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+\\|\\*\\*\\* .+\n---\\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1)))
305 (defvar diff-narrowed-to nil)
307 (defun diff-end-of-hunk (&optional style)
308 (if (looking-at diff-hunk-header-re) (goto-char (match-end 0)))
309 (let ((end (and (re-search-forward (case style
310 ;; A `unified' header is ambiguous.
311 (unified (concat "^[^-+# \\]\\|"
312 diff-file-header-re))
313 (context "^[^-+#! \\]")
317 (match-beginning 0))))
318 ;; The return value is used by easy-mmode-define-navigation.
319 (goto-char (or end (point-max)))))
321 (defun diff-beginning-of-hunk ()
323 (unless (looking-at diff-hunk-header-re)
326 (re-search-backward diff-hunk-header-re)
327 (error (error "Can't find the beginning of the hunk")))))
329 (defun diff-beginning-of-file ()
331 (unless (looking-at diff-file-header-re)
334 (re-search-backward diff-file-header-re)
335 (error (error "Can't find the beginning of the file")))))
337 (defun diff-end-of-file ()
338 (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
339 (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re)
341 (if (match-beginning 1)
342 (goto-char (match-beginning 1))
343 (beginning-of-line)))
345 ;; Define diff-{hunk,file}-{prev,next}
346 (easy-mmode-define-navigation
347 diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view)
348 (easy-mmode-define-navigation
349 diff-file diff-file-header-re "file" diff-end-of-hunk)
351 (defun diff-restrict-view (&optional arg)
352 "Restrict the view to the current hunk.
353 If the prefix ARG is given, restrict the view to the current file instead."
356 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk))
357 (narrow-to-region (point)
358 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
360 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
363 (defun diff-hunk-kill ()
366 (diff-beginning-of-hunk)
367 (let* ((start (point))
368 (nexthunk (when (re-search-forward diff-hunk-header-re nil t)
369 (match-beginning 0)))
370 (firsthunk (ignore-errors
372 (diff-beginning-of-file) (diff-hunk-next) (point)))
373 (nextfile (ignore-errors (diff-file-next) (point))))
375 (if (and firsthunk (= firsthunk start)
377 (and nextfile (> nexthunk nextfile))))
378 ;; It's the only hunk for this file, so kill the file.
381 (kill-region start (point)))))
383 (defun diff-file-kill ()
384 "Kill current file's hunks."
386 (diff-beginning-of-file)
387 (let* ((start (point))
388 (prevhunk (save-excursion
390 (diff-hunk-prev) (point))))
391 (index (save-excursion
392 (re-search-backward "^Index: " prevhunk t))))
393 (when index (setq start index))
395 (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
396 (kill-region start (point))))
398 (defun diff-kill-junk ()
399 "Kill spurious empty diffs."
402 (let ((inhibit-read-only t))
403 (goto-char (point-min))
404 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
405 "\\([^-+!* <>].*\n\\)*?"
406 "\\(\\(Index:\\) \\|"
407 diff-file-header-re "\\)")
409 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
411 (beginning-of-line)))))
413 (defun diff-count-matches (re start end)
417 (while (re-search-forward re end t) (incf n))
420 (defun diff-split-hunk ()
421 "Split the current (unified diff) hunk at point into two hunks."
425 (start (progn (diff-beginning-of-hunk) (point))))
426 (unless (looking-at "@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@")
427 (error "diff-split-hunk only works on unified context diffs"))
429 (let* ((start1 (string-to-number (match-string 1)))
430 (start2 (string-to-number (match-string 2)))
431 (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
432 (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos))))
434 ;; Hopefully the after-change-function will not screw us over.
435 (insert "@@ -" (number-to-string newstart1) ",1 +"
436 (number-to-string newstart2) ",1 @@\n")
437 ;; Fix the original hunk-header.
438 (diff-fixup-modifs start pos))))
442 ;;;; jump to other buffers
445 (defvar diff-remembered-files-alist nil)
447 (defun diff-filename-drop-dir (file)
448 (when (string-match "/" file) (substring file (match-end 0))))
450 (defun diff-merge-strings (ancestor from to)
451 "Merge the diff between ANCESTOR and FROM into TO.
452 Returns the merged string if successful or nil otherwise.
453 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
454 If ANCESTOR = FROM, returns TO.
455 If ANCESTOR = TO, returns FROM.
456 The heuristic is simplistic and only really works for cases
457 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
459 ;; AMB ANB CMD -> CND
460 ;; but that's ambiguous if `foo' or `bar' is empty:
461 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
462 (let ((str (concat ancestor "\n" from "\n" to)))
463 (when (and (string-match (concat
464 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
466 "\\(.*\\(\\2\\).*\\)\\'") str)
467 (equal to (match-string 5 str)))
468 (concat (substring str (match-beginning 5) (match-beginning 6))
470 (substring str (match-end 6) (match-end 5))))))
472 (defun diff-tell-file-name (old name)
473 "Tell Emacs where the find the source file of the current hunk.
474 If the OLD prefix arg is passed, tell the file NAME of the old file."
476 (let* ((old current-prefix-arg)
477 (fs (diff-hunk-file-names current-prefix-arg)))
478 (unless fs (error "No file name to look for"))
479 (list old (read-file-name (format "File for %s: " (car fs))
480 nil (diff-find-file-name old) t))))
481 (let ((fs (diff-hunk-file-names old)))
482 (unless fs (error "No file name to look for"))
483 (push (cons fs name) diff-remembered-files-alist)))
485 (defun diff-hunk-file-names (&optional old)
486 "Give the list of file names textually mentioned for the current hunk."
488 (unless (looking-at diff-file-header-re)
489 (or (ignore-errors (diff-beginning-of-file))
490 (re-search-forward diff-file-header-re nil t)))
491 (let ((limit (save-excursion
493 (progn (diff-hunk-prev) (point))
494 (error (point-min)))))
496 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
497 (list (if old (match-string 1) (match-string 3))
498 (if old (match-string 3) (match-string 1)))
499 (forward-line 1) nil)))
504 (re-search-backward "^Index: \\(.+\\)" limit t)))
505 (list (match-string 1)))
507 (when (re-search-backward
508 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?"
510 (list (if old (match-string 2) (match-string 4))
511 (if old (match-string 4) (match-string 2)))))))))
513 (defun diff-find-file-name (&optional old)
514 "Return the file corresponding to the current patch.
515 Non-nil OLD means that we want the old file."
517 (unless (looking-at diff-file-header-re)
518 (or (ignore-errors (diff-beginning-of-file))
519 (re-search-forward diff-file-header-re nil t)))
520 (let ((fs (diff-hunk-file-names old)))
522 ;; use any previously used preference
523 (cdr (assoc fs diff-remembered-files-alist))
524 ;; try to be clever and use previous choices as an inspiration
525 (dolist (rf diff-remembered-files-alist)
526 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
527 (if (and newfile (file-exists-p newfile)) (return newfile))))
528 ;; look for each file in turn. If none found, try again but
529 ;; ignoring the first level of directory, ...
530 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
533 (setq file (do* ((files files (cdr files))
534 (file (car files) (car files)))
535 ((or (null file) (file-exists-p file))
538 ;; <foo>.rej patches implicitly apply to <foo>
539 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
540 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
541 (when (file-exists-p file) file)))
542 ;; if all else fails, ask the user
543 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
544 nil (first fs) t (first fs))))
545 (set (make-local-variable 'diff-remembered-files-alist)
546 (cons (cons fs file) diff-remembered-files-alist))
550 (defun diff-mouse-goto-source (event)
551 "Run `diff-goto-source' for the diff at a mouse click."
554 (mouse-set-point event)
558 (defun diff-ediff-patch ()
559 "Call `ediff-patch-file' on the current buffer."
562 (ediff-patch-file nil (current-buffer))
563 (wrong-number-of-arguments (ediff-patch-file))))
566 ;;;; Conversion functions
569 ;;(defvar diff-inhibit-after-change nil
570 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
572 (defun diff-unified->context (start end)
573 "Convert unified diffs to context diffs.
574 START and END are either taken from the region (if a prefix arg is given) or
575 else cover the whole bufer."
576 (interactive (if current-prefix-arg
577 (list (mark) (point))
578 (list (point-min) (point-max))))
579 (unless (markerp end) (setq end (copy-marker end)))
580 (let (;;(diff-inhibit-after-change t)
581 (inhibit-read-only t))
584 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*\\)$" nil t)
586 (combine-after-change-calls
587 (if (match-beginning 2)
588 ;; we matched a file header
590 ;; use reverse order to make sure the indices are kept valid
591 (replace-match "---" t t nil 3)
592 (replace-match "***" t t nil 2))
593 ;; we matched a hunk header
594 (let ((line1 (match-string 4))
595 (lines1 (match-string 5))
596 (line2 (match-string 6))
597 (lines2 (match-string 7)))
599 (concat "***************\n*** " line1 ","
600 (number-to-string (+ (string-to-number line1)
601 (string-to-number lines1)
605 (narrow-to-region (point)
606 (progn (diff-end-of-hunk 'unified) (point)))
607 (let ((hunk (buffer-string)))
608 (goto-char (point-min))
609 (if (not (save-excursion (re-search-forward "^-" nil t)))
610 (delete-region (point) (point-max))
611 (goto-char (point-max))
612 (let ((modif nil) last-pt)
613 (while (progn (setq last-pt (point))
614 (= (forward-line -1) 0))
616 (? (insert " ") (setq modif nil) (backward-char 1))
617 (?+ (delete-region (point) last-pt) (setq modif t))
619 (progn (forward-char 1)
624 (?\\ (when (save-excursion (forward-line -1)
626 (delete-region (point) last-pt) (setq modif t)))
627 (t (setq modif nil))))))
628 (goto-char (point-max))
630 (insert "--- " line2 ","
631 (number-to-string (+ (string-to-number line2)
632 (string-to-number lines2)
633 -1)) " ----\n" hunk))
634 ;;(goto-char (point-min))
636 (if (not (save-excursion (re-search-forward "^+" nil t)))
637 (delete-region (point) (point-max))
638 (let ((modif nil) (delete nil))
641 (? (insert " ") (setq modif nil) (backward-char 1))
642 (?- (setq delete t) (setq modif t))
644 (progn (forward-char 1)
649 (?\\ (when (save-excursion (forward-line 1)
651 (setq delete t) (setq modif t)))
652 (t (setq modif nil)))
653 (let ((last-pt (point)))
656 (delete-region last-pt (point))
657 (setq delete nil)))))))))))))))
659 (defun diff-context->unified (start end)
660 "Convert context diffs to unified diffs.
661 START and END are either taken from the region (if a prefix arg is given) or
662 else cover the whole bufer."
663 (interactive (if current-prefix-arg
664 (list (mark) (point))
665 (list (point-min) (point-max))))
666 (unless (markerp end) (setq end (copy-marker end)))
667 (let (;;(diff-inhibit-after-change t)
668 (inhibit-read-only t))
671 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
673 (combine-after-change-calls
674 (if (match-beginning 2)
675 ;; we matched a file header
677 ;; use reverse order to make sure the indices are kept valid
678 (replace-match "+++" t t nil 3)
679 (replace-match "---" t t nil 2))
680 ;; we matched a hunk header
681 (let ((line1s (match-string 4))
682 (line1e (match-string 5))
683 (pt1 (match-beginning 0)))
685 (unless (re-search-forward
686 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
687 (error "Can't find matching `--- n1,n2 ----' line"))
688 (let ((line2s (match-string 1))
689 (line2e (match-string 2))
691 (delete-region (progn (beginning-of-line) (point))
692 (progn (forward-line 1) (point)))
696 (while (< (point) pt2)
698 ((?! ?-) (delete-char 2) (insert "-") (forward-line 1))
699 (?\ ;merge with the other half of the chunk
702 (goto-char pt2) (forward-line 1) (point)))
703 (c (char-after pt2)))
707 (prog1 (buffer-substring (+ pt2 2) endline2)
708 (delete-region pt2 endline2))))
709 (?\ ;FIXME: check consistency
710 (delete-region pt2 endline2)
713 (?\\ (forward-line 1))
714 (t (delete-char 1) (forward-line 1)))))
715 (t (forward-line 1))))
716 (while (looking-at "[+! ] ")
717 (if (/= (char-after) ?!) (forward-char 1)
718 (delete-char 1) (insert "+"))
719 (delete-char 1) (forward-line 1))
722 (insert "@@ -" line1s ","
723 (number-to-string (- (string-to-number line1e)
724 (string-to-number line1s)
727 (number-to-string (- (string-to-number line2e)
728 (string-to-number line2s)
731 (defun diff-reverse-direction (start end)
732 "Reverse the direction of the diffs.
733 START and END are either taken from the region (if a prefix arg is given) or
734 else cover the whole bufer."
735 (interactive (if current-prefix-arg
736 (list (mark) (point))
737 (list (point-min) (point-max))))
738 (unless (markerp end) (setq end (copy-marker end)))
739 (let (;;(diff-inhibit-after-change t)
740 (inhibit-read-only t))
743 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
745 (combine-after-change-calls
748 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
749 ;; a context-diff hunk header
751 (let ((pt-lines1 (match-beginning 6))
752 (lines1 (match-string 6)))
753 (replace-match "" nil nil nil 6)
755 (let ((half1s (point)))
756 (while (looking-at "[-! \\][ \t]\\|#")
757 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
759 (let ((half1 (delete-and-extract-region half1s (point))))
760 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
762 (error "Can't find matching `--- n1,n2 ----' line"))
763 (let ((str1 (match-string 1)))
764 (replace-match lines1 nil nil nil 1)
766 (let ((half2s (point)))
767 (while (looking-at "[!+ \\][ \t]\\|#")
768 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
770 (let ((half2 (delete-and-extract-region half2s (point))))
771 (insert (or half1 ""))
773 (insert (or half2 ""))))
774 (goto-char pt-lines1)
776 ;; a unified-diff hunk header
778 (replace-match "@@ -\\8 +\\7 @@" nil)
780 (let ((c (char-after)) first last)
781 (while (case (setq c (char-after))
782 (?- (setq first (or first (point)))
783 (delete-char 1) (insert "+") t)
784 (?+ (setq last (or last (point)))
785 (delete-char 1) (insert "-") t)
787 (t (when (and first last (< first last))
788 (insert (delete-and-extract-region first last)))
789 (setq first nil last nil)
791 (forward-line 1))))))))))
793 (defun diff-fixup-modifs (start end)
794 "Fixup the hunk headers (in case the buffer was modified).
795 START and END are either taken from the region (if a prefix arg is given) or
796 else cover the whole bufer."
797 (interactive (if current-prefix-arg
798 (list (mark) (point))
799 (list (point-min) (point-max))))
800 (let ((inhibit-read-only t))
802 (goto-char end) (diff-end-of-hunk)
803 (let ((plus 0) (minus 0) (space 0) (bang 0))
804 (while (and (= (forward-line -1) 0) (<= start (point)))
805 (if (not (looking-at "\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|[-*][-*][-*] .+ [-*][-*][-*][-*]\\)$"))
812 (t (setq space 0 plus 0 minus 0 bang 0)))
814 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@.*$")
815 (let* ((old1 (match-string 1))
816 (old2 (match-string 2))
817 (new1 (number-to-string (+ space minus)))
818 (new2 (number-to-string (+ space plus))))
819 (unless (string= new2 old2) (replace-match new2 t t nil 2))
820 (unless (string= new1 old1) (replace-match new1 t t nil 1))))
821 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
822 (when (> (+ space bang plus) 0)
823 (let* ((old1 (match-string 1))
824 (old2 (match-string 2))
825 (new (number-to-string
826 (+ space bang plus -1 (string-to-number old1)))))
827 (unless (string= new old2) (replace-match new t t nil 2)))))
828 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
829 (when (> (+ space bang minus) 0)
830 (let* ((old (match-string 1))
832 (concat "%0" (number-to-string (length old)) "d")
833 (+ space bang minus -1 (string-to-number old)))))
834 (unless (string= new old) (replace-match new t t nil 2))))))
835 (setq space 0 plus 0 minus 0 bang 0)))))))
841 (defun diff-write-contents-hooks ()
842 "Fixup hunk headers if necessary."
843 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
846 ;; It turns out that making changes in the buffer from within an
847 ;; *-change-function is asking for trouble, whereas making them
848 ;; from a post-command-hook doesn't pose much problems
849 (defvar diff-unhandled-changes nil)
850 (defun diff-after-change-function (beg end len)
851 "Remember to fixup the hunk header.
852 See `after-change-functions' for the meaning of BEG, END and LEN."
853 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
854 ;; incorrect, but it turns out that inhibit-read-only is normally not set
855 ;; inside editing commands, while it tends to be set when the buffer gets
856 ;; updated by an async process or by a conversion function, both of which
857 ;; would rather not be uselessly slowed down by this hook.
858 (when (and (not undo-in-progress) (not inhibit-read-only))
859 (if diff-unhandled-changes
860 (setq diff-unhandled-changes
861 (cons (min beg (car diff-unhandled-changes))
862 (max end (cdr diff-unhandled-changes))))
863 (setq diff-unhandled-changes (cons beg end)))))
865 (defun diff-post-command-hook ()
866 "Fixup hunk headers if necessary."
867 (when (consp diff-unhandled-changes)
870 (goto-char (car diff-unhandled-changes))
871 ;; Maybe we've cut the end of the hunk before point.
872 (if (and (bolp) (not (bobp))) (backward-char 1))
873 ;; We used to fixup modifs on all the changes, but it turns out
874 ;; that it's safer not to do it on big changes, for example
875 ;; when yanking a big diff, since we might then screw up perfectly
876 ;; correct values. -stef
877 ;; (unless (ignore-errors
878 ;; (diff-beginning-of-hunk)
880 ;; (diff-end-of-hunk)
881 ;; (> (point) (car diff-unhandled-changes))))
882 ;; (goto-char (car diff-unhandled-changes))
883 ;; (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
884 ;; (diff-beginning-of-hunk))
885 ;; (diff-fixup-modifs (point) (cdr diff-unhandled-changes))
886 (diff-beginning-of-hunk)
887 (when (save-excursion
889 (>= (point) (cdr diff-unhandled-changes)))
890 (diff-fixup-modifs (point) (cdr diff-unhandled-changes)))))
891 (setq diff-unhandled-changes nil)))
894 ;;;; The main function
898 (define-derived-mode diff-mode fundamental-mode "Diff"
899 "Major mode for viewing/editing context diffs.
900 Supports unified and context diffs as well as (to a lesser extent)
902 When the buffer is read-only, the ESC prefix is not necessary.
903 IF you edit the buffer manually, diff-mode will try to update the hunk
904 headers for you on-the-fly.
906 You can also switch between context diff and unified diff with \\[diff-context->unified],
907 or vice versa with \\[diff-unified->context] and you can also revert the direction of
908 a diff with \\[diff-reverse-direction]."
909 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
910 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
911 (set (make-local-variable 'imenu-generic-expression)
912 diff-imenu-generic-expression)
913 ;; These are not perfect. They would be better done separately for
914 ;; context diffs and unidiffs.
915 ;; (set (make-local-variable 'paragraph-start)
916 ;; (concat "@@ " ; unidiff hunk
917 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
918 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
919 ;; ; start (first or second line)
920 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
921 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
924 (when (and (> (point-max) (point-min)) diff-default-read-only)
925 (toggle-read-only t))
926 ;; setup change hooks
927 (if (not diff-update-on-the-fly)
928 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
929 (make-local-variable 'diff-unhandled-changes)
930 (add-hook 'after-change-functions 'diff-after-change-function nil t)
931 (add-hook 'post-command-hook 'diff-post-command-hook nil t))
932 ;; Neat trick from Dave Love to add more bindings in read-only mode:
933 (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
934 (add-to-list 'minor-mode-overriding-map-alist ro-bind)
935 ;; Turn off this little trick in case the buffer is put in view-mode.
936 (add-hook 'view-mode-hook
938 (setq minor-mode-overriding-map-alist
939 (delq ,ro-bind minor-mode-overriding-map-alist)))
942 (set (make-local-variable 'add-log-current-defun-function)
944 (set (make-local-variable 'add-log-buffer-file-name-function)
945 'diff-find-file-name))
948 (define-minor-mode diff-minor-mode
949 "Minor mode for viewing/editing context diffs.
950 \\{diff-minor-mode-map}"
952 ;; FIXME: setup font-lock
953 ;; setup change hooks
954 (if (not diff-update-on-the-fly)
955 (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
956 (make-local-variable 'diff-unhandled-changes)
957 (add-hook 'after-change-functions 'diff-after-change-function nil t)
958 (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
960 ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
962 (defun diff-delete-if-empty ()
963 ;; An empty diff file means there's no more diffs to integrate, so we
964 ;; can just remove the file altogether. Very handy for .rej files if we
965 ;; remove hunks as we apply them.
966 (when (and buffer-file-name
967 (eq 0 (nth 7 (file-attributes buffer-file-name))))
968 (delete-file buffer-file-name)))
970 (defun diff-delete-empty-files ()
971 "Arrange for empty diff files to be removed."
972 (add-hook 'after-save-hook 'diff-delete-if-empty nil t))
974 (defun diff-make-unified ()
975 "Turn context diffs into unified diffs if applicable."
977 (goto-char (point-min))
978 (looking-at "\\*\\*\\* "))
979 (let ((mod (buffer-modified-p)))
981 (diff-context->unified (point-min) (point-max))
982 (restore-buffer-modified-p mod)))))
985 ;;; Misc operations that have proved useful at some point.
988 (defun diff-next-complex-hunk ()
989 "Jump to the next \"complex\" hunk.
990 \"Complex\" is approximated by \"the hunk changes the number of lines\".
991 Only works for unified diffs."
994 (and (re-search-forward "^@@ [-0-9]+,\\([0-9]+\\) [+0-9]+,\\([0-9]+\\) @@"
996 (equal (match-string 1) (match-string 2)))))
998 (defun diff-hunk-text (hunk destp char-offset)
999 "Return the literal source text from HUNK as (TEXT . OFFSET).
1000 if DESTP is nil TEXT is the source, otherwise the destination text.
1001 CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding
1002 char-offset in TEXT."
1005 (goto-char (point-min))
1010 ;; Set the following variables:
1011 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
1012 ;; DST-POS buffer pos of the destination part of the hunk or nil
1013 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
1014 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
1015 (cond ((looking-at "^@@")
1017 (setq num-pfx-chars 1)
1019 (setq src-pos (point) dst-pos (point)))
1020 ((looking-at "^\\*\\*")
1023 (setq src-pos (point))
1024 (re-search-forward "^--- " nil t)
1026 (setq divider-pos (point))
1028 (setq dst-pos (point)))
1029 ((looking-at "^[0-9]+a[0-9,]+$")
1030 ;; normal diff, insert
1032 (setq dst-pos (point)))
1033 ((looking-at "^[0-9,]+d[0-9]+$")
1034 ;; normal diff, delete
1036 (setq src-pos (point)))
1037 ((looking-at "^[0-9,]+c[0-9,]+$")
1038 ;; normal diff, change
1040 (setq src-pos (point))
1041 (re-search-forward "^---$" nil t)
1043 (setq divider-pos (point))
1045 (setq dst-pos (point)))
1047 (error "Unknown diff hunk type")))
1049 (if (if destp (null dst-pos) (null src-pos))
1050 ;; Implied empty text
1051 (if char-offset '("" . 0) "")
1053 ;; For context diffs, either side can be empty, (if there's only
1054 ;; added or only removed text). We should then use the other side.
1055 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
1056 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
1058 (when char-offset (goto-char (+ (point-min) char-offset)))
1060 ;; Get rid of anything except the desired text.
1062 ;; Delete unused text region
1063 (let ((keep (if destp dst-pos src-pos)))
1064 (when (and divider-pos (> divider-pos keep))
1065 (delete-region divider-pos (point-max)))
1066 (delete-region (point-min) keep))
1067 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1068 (let ((kill-char (if destp ?- ?+)))
1069 (goto-char (point-min))
1071 (if (eq (char-after) kill-char)
1072 (delete-region (point) (progn (forward-line 1) (point)))
1073 (delete-char num-pfx-chars)
1074 (forward-line 1)))))
1076 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1077 (if char-offset (cons text (- (point) (point-min))) text))))))
1080 (defun diff-find-text (text)
1081 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1082 If TEXT isn't found, nil is returned."
1083 (let* ((orig (point))
1084 (forw (and (search-forward text nil t)
1085 (cons (match-beginning 0) (match-end 0))))
1086 (back (and (goto-char (+ orig (length text)))
1087 (search-backward text nil t)
1088 (cons (match-beginning 0) (match-end 0)))))
1089 ;; Choose the closest match.
1091 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1094 (defun diff-find-approx-text (text)
1095 "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
1096 Whitespace differences are ignored."
1097 (let* ((orig (point))
1098 (re (concat "^[ \t\n
\f]*"
1099 (mapconcat 'regexp-quote (split-string text) "[ \t\n
\f]+")
1101 (forw (and (re-search-forward re nil t)
1102 (cons (match-beginning 0) (match-end 0))))
1103 (back (and (goto-char (+ orig (length text)))
1104 (re-search-backward re nil t)
1105 (cons (match-beginning 0) (match-end 0)))))
1106 ;; Choose the closest match.
1108 (if (> (- (car forw) orig) (- orig (car back))) back forw)
1111 (defsubst diff-xor (a b) (if a (not b) b))
1113 (defun diff-find-source-location (&optional other-file reverse)
1114 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
1115 BUF is the buffer corresponding to the source file.
1116 LINE-OFFSET is the offset between the expected and actual positions
1117 of the text of the hunk or nil if the text was not found.
1118 POS is a pair (BEG . END) indicating the position of the text in the buffer.
1119 SRC and DST are the two variants of text as returned by `diff-hunk-text'.
1120 SRC is the variant that was found in the buffer.
1121 SWITCHED is non-nil if the patch is already applied."
1123 (let* ((other (diff-xor other-file diff-jump-to-old-file))
1124 (char-offset (- (point) (progn (diff-beginning-of-hunk) (point))))
1125 (hunk (buffer-substring (point)
1126 (save-excursion (diff-end-of-hunk) (point))))
1127 (old (diff-hunk-text hunk reverse char-offset))
1128 (new (diff-hunk-text hunk (not reverse) char-offset))
1129 ;; Find the location specification.
1130 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
1131 (error "Can't find the hunk header")
1132 (if other (match-string 1)
1133 (if (match-end 3) (match-string 3)
1134 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
1135 (error "Can't find the hunk separator"))
1136 (match-string 1)))))
1137 (file (or (diff-find-file-name other) (error "Can't find the file")))
1138 (buf (find-file-noselect file)))
1139 ;; Update the user preference if he so wished.
1140 (when (> (prefix-numeric-value other-file) 8)
1141 (setq diff-jump-to-old-file other))
1142 (with-current-buffer buf
1143 (goto-line (string-to-number line))
1144 (let* ((orig-pos (point))
1146 ;; FIXME: Check for case where both OLD and NEW are found.
1147 (pos (or (diff-find-text (car old))
1148 (progn (setq switched t) (diff-find-text (car new)))
1149 (progn (setq switched nil)
1151 (diff-find-approx-text (car old))
1152 (invalid-regexp nil))) ;Regex too big.
1153 (progn (setq switched t)
1155 (diff-find-approx-text (car new))
1156 (invalid-regexp nil))) ;Regex too big.
1157 (progn (setq switched nil) nil))))
1161 (list (count-lines orig-pos (car pos)) pos)
1162 (list nil (cons orig-pos (+ orig-pos (length (car old))))))
1163 (if switched (list new old t) (list old new))))))))
1166 (defun diff-hunk-status-msg (line-offset reversed dry-run)
1167 (let ((msg (if dry-run
1168 (if reversed "already applied" "not yet applied")
1169 (if reversed "undone" "applied"))))
1170 (message (cond ((null line-offset) "Hunk text not found")
1171 ((= line-offset 0) "Hunk %s")
1172 ((= line-offset 1) "Hunk %s at offset %d line")
1173 (t "Hunk %s at offset %d lines"))
1177 (defun diff-apply-hunk (&optional reverse)
1178 "Apply the current hunk to the source file and go to the next.
1179 By default, the new source file is patched, but if the variable
1180 `diff-jump-to-old-file' is non-nil, then the old source file is
1181 patched instead (some commands, such as `diff-goto-source' can change
1182 the value of this variable when given an appropriate prefix argument).
1184 With a prefix argument, REVERSE the hunk."
1186 (destructuring-bind (buf line-offset pos old new &optional switched)
1187 ;; If REVERSE go to the new file, otherwise go to the old.
1188 (diff-find-source-location (not reverse) reverse)
1191 (error "Can't find the text to patch"))
1193 ;; A reversed patch was detected, perhaps apply it in reverse.
1194 (not (save-window-excursion
1196 (goto-char (+ (car pos) (cdr old)))
1199 "Hunk hasn't been applied yet; apply it now? "
1200 "Hunk has already been applied; undo it? ")))))
1201 (message "(Nothing done)"))
1204 (with-current-buffer buf
1205 (goto-char (car pos))
1206 (delete-region (car pos) (cdr pos))
1208 ;; Display BUF in a window
1209 (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
1210 (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
1211 (when diff-advance-after-apply-hunk
1212 (diff-hunk-next))))))
1215 (defun diff-test-hunk (&optional reverse)
1216 "See whether it's possible to apply the current hunk.
1217 With a prefix argument, try to REVERSE the hunk."
1219 (destructuring-bind (buf line-offset pos src dst &optional switched)
1220 ;; If REVERSE go to the new file, otherwise go to the old.
1221 (diff-find-source-location (not reverse) reverse)
1222 (set-window-point (display-buffer buf) (+ (car pos) (cdr src)))
1223 (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
1226 (defun diff-goto-source (&optional other-file)
1227 "Jump to the corresponding source line.
1228 `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
1229 is given) determines whether to jump to the old or the new file.
1230 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1231 then `diff-jump-to-old-file' is also set, for the next invocations."
1233 ;; When pointing at a removal line, we probably want to jump to
1234 ;; the old location, and else to the new (i.e. as if reverting).
1235 ;; This is a convenient detail when using smerge-diff.
1236 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1237 (destructuring-bind (buf line-offset pos src dst &optional switched)
1238 (diff-find-source-location other-file rev)
1240 (goto-char (+ (car pos) (cdr src)))
1241 (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
1244 (defun diff-current-defun ()
1245 "Find the name of function at point.
1246 For use in `add-log-current-defun-function'."
1247 (destructuring-bind (buf line-offset pos src dst &optional switched)
1248 (diff-find-source-location)
1251 (or (when (memq (char-after) '(?< ?-))
1252 ;; Cursor is pointing at removed text. This could be a removed
1253 ;; function, in which case, going to the source buffer will
1254 ;; not help since the function is now removed. Instead,
1255 ;; try to figure out the function name just from the code-fragment.
1256 (let ((old (if switched dst src)))
1259 (funcall (with-current-buffer buf major-mode))
1260 (goto-char (+ (point-min) (cdr old)))
1261 (add-log-current-defun))))
1262 (with-current-buffer buf
1263 (goto-char (+ (car pos) (cdr src)))
1264 (add-log-current-defun))))))
1266 (defun diff-refine-hunk ()
1267 "Refine the current hunk by ignoring space differences."
1269 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk) (point))))
1270 (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b")))
1271 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
1272 (error "Can't find line number"))
1273 (string-to-number (match-string 1))))
1274 (hunk (delete-and-extract-region
1275 (point) (save-excursion (diff-end-of-hunk) (point))))
1276 (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
1277 (file1 (make-temp-file "diff1"))
1278 (file2 (make-temp-file "diff2"))
1279 (coding-system-for-read buffer-file-coding-system)
1283 (setq old (diff-hunk-text hunk nil char-offset))
1284 (setq new (diff-hunk-text hunk t char-offset))
1285 (write-region (concat lead (car old)) nil file1 nil 'nomessage)
1286 (write-region (concat lead (car new)) nil file2 nil 'nomessage)
1289 (call-process diff-command nil t nil
1292 (0 nil) ;Nothing to reformat.
1293 (1 (goto-char (point-min))
1294 ;; Remove the file-header.
1295 (when (re-search-forward diff-hunk-header-re nil t)
1296 (delete-region (point-min) (match-beginning 0))))
1297 (t (goto-char (point-max))
1298 (unless (bolp) (insert "\n"))
1300 (setq hunk (buffer-string))
1301 (unless (memq status '(0 1))
1302 (error "Diff returned: %s" status)))))
1303 ;; Whatever happens, put back some equivalent text: either the new
1304 ;; one or the original one in case some error happened.
1307 (delete-file file2))))
1309 ;; provide the package
1310 (provide 'diff-mode)
1312 ;;; Old Change Log from when diff-mode wasn't part of Emacs:
1313 ;; Revision 1.11 1999/10/09 23:38:29 monnier
1314 ;; (diff-mode-load-hook): dropped.
1315 ;; (auto-mode-alist): also catch *.diffs.
1316 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
1317 ;; for *.rej files (that lack any file name indication).
1319 ;; Revision 1.10 1999/09/30 15:32:11 monnier
1320 ;; added support for "\ No newline at end of file".
1322 ;; Revision 1.9 1999/09/15 00:01:13 monnier
1323 ;; - added basic `compile' support.
1324 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
1325 ;; - diff-kill-file now tries to kill the leading garbage as well.
1327 ;; Revision 1.8 1999/09/13 21:10:09 monnier
1328 ;; - don't use CL in the autoloaded code
1329 ;; - accept diffs using -T
1331 ;; Revision 1.7 1999/09/05 20:53:03 monnier
1332 ;; interface to ediff-patch
1334 ;; Revision 1.6 1999/09/01 20:55:13 monnier
1335 ;; (ediff=patch-file): add bindings to call ediff-patch.
1336 ;; (diff-find-file-name): taken out of diff-goto-source.
1337 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
1338 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
1340 ;; Revision 1.5 1999/08/31 19:18:52 monnier
1341 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
1343 ;; Revision 1.4 1999/08/31 13:01:44 monnier
1344 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
1347 ;;; arch-tag: 2571d7ff-bc28-4cf9-8585-42e21890be66
1348 ;;; diff-mode.el ends here