(display-time-string-forms): Use
[emacs.git] / lisp / diff-mode.el
blobb01ea535ac08b8a83dd7d12aa4c4a26ead86ff32
1 ;;; diff-mode.el --- A mode for viewing/editing context diffs
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: 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)
13 ;; any later version.
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.
25 ;;; Commentary:
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
35 ;; to use it, simply add to your .emacs the following lines:
36 ;;
37 ;; (autoload 'diff-mode "diff-mode" "Diff major mode" t)
38 ;; (add-to-list 'auto-mode-alist '("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode))
40 ;; Bugs:
42 ;; - Reverse doesn't work with normal diffs.
44 ;; Todo:
46 ;; - Improve narrowed-view support.
47 ;; - re-enable (conditionally) the `compile' support after improving it to use
48 ;; the same code as diff-goto-source.
49 ;; - Support for # comments in context->unified.
50 ;; - Do a fuzzy search in diff-goto-source.
51 ;; - Allow diff.el to use diff-mode.
52 ;; This mostly means ability to jump from half-hunk to half-hunk
53 ;; in context (and normal) diffs and to jump to the corresponding
54 ;; (i.e. new or old) file.
55 ;; - Handle `diff -b' output in context->unified.
57 ;; Low priority:
58 ;; - Spice up the minor-mode with font-lock support.
59 ;; - Recognize pcl-cvs' special string for `cvs-execute-single'.
61 ;;; Code:
63 (eval-when-compile (require 'cl))
66 (defgroup diff-mode ()
67 "Major mode for viewing/editing diffs"
68 :version "21.1"
69 :group 'tools
70 :group 'diff)
72 (defcustom diff-jump-to-old-file nil
73 "*Non-nil means `diff-goto-source' jumps to the old file.
74 Else, it jumps to the new file."
75 :group 'diff-mode
76 :type '(boolean))
78 (defcustom diff-update-on-the-fly t
79 "*Non-nil means hunk headers are kept up-to-date on-the-fly.
80 When editing a diff file, the line numbers in the hunk headers
81 need to be kept consistent with the actual diff. This can
82 either be done on the fly (but this sometimes interacts poorly with the
83 undo mechanism) or whenever the file is written (can be slow
84 when editing big diffs)."
85 :group 'diff-mode
86 :type '(boolean))
88 (defcustom diff-advance-after-apply-hunk t
89 "*Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
90 :group 'diff-mode
91 :type 'boolean)
94 (defvar diff-mode-hook nil
95 "Run after setting up the `diff-mode' major mode.")
97 (defvar diff-outline-regexp
98 "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
100 ;;;;
101 ;;;; keymap, menu, ...
102 ;;;;
104 (easy-mmode-defmap diff-mode-shared-map
105 '(;; From Pavel Machek's patch-mode.
106 ("n" . diff-hunk-next)
107 ("N" . diff-file-next)
108 ("p" . diff-hunk-prev)
109 ("P" . diff-file-prev)
110 ("k" . diff-hunk-kill)
111 ("K" . diff-file-kill)
112 ;; From compilation-minor-mode.
113 ("}" . diff-file-next)
114 ("{" . diff-file-prev)
115 ("\C-m" . diff-goto-source)
116 ([mouse-2] . diff-mouse-goto-source)
117 ;; From XEmacs' diff-mode.
118 ("W" . widen)
119 ;;("." . diff-goto-source) ;display-buffer
120 ;;("f" . diff-goto-source) ;find-file
121 ("o" . diff-goto-source) ;other-window
122 ;;("w" . diff-goto-source) ;other-frame
123 ;;("N" . diff-narrow)
124 ;;("h" . diff-show-header)
125 ;;("j" . diff-show-difference) ;jump to Nth diff
126 ;;("q" . diff-quit)
127 (" " . scroll-up)
128 ("\177" . scroll-down)
129 ;; Our very own bindings.
130 ("A" . diff-ediff-patch)
131 ("r" . diff-restrict-view)
132 ("R" . diff-reverse-direction)
133 ("U" . diff-context->unified)
134 ("C" . diff-unified->context))
135 "Basic keymap for `diff-mode', bound to various prefix keys.")
137 (easy-mmode-defmap diff-mode-map
138 `(("\e" . ,diff-mode-shared-map)
139 ;; From compilation-minor-mode.
140 ("\C-c\C-c" . diff-goto-source)
141 ;; Misc operations.
142 ("\C-c\C-s" . diff-split-hunk)
143 ("\C-c\C-a" . diff-apply-hunk)
144 ("\C-c\C-t" . diff-test-hunk))
145 "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
147 (easy-menu-define diff-mode-menu diff-mode-map
148 "Menu for `diff-mode'."
149 '("Diff"
150 ["Jump to Source" diff-goto-source t]
151 ["Apply hunk" diff-apply-hunk t]
152 ["Apply diff with Ediff" diff-ediff-patch t]
153 ["-----" nil nil]
154 ["Reverse direction" diff-reverse-direction t]
155 ["Context -> Unified" diff-context->unified t]
156 ["Unified -> Context" diff-unified->context t]
157 ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
160 (defcustom diff-minor-mode-prefix "\C-c="
161 "Prefix key for `diff-minor-mode' commands."
162 :group 'diff-mode
163 :type '(choice (string "\e") (string "C-c=") string))
165 (easy-mmode-defmap diff-minor-mode-map
166 `((,diff-minor-mode-prefix . ,diff-mode-shared-map))
167 "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
170 ;;;;
171 ;;;; font-lock support
172 ;;;;
174 (defface diff-header-face
175 '((((type tty pc) (class color) (background light))
176 (:foreground "blue1" :bold t))
177 (((type tty pc) (class color) (background dark))
178 (:foreground "green" :bold t))
179 (((class color) (background light))
180 (:background "grey85"))
181 (((class color) (background dark))
182 (:background "grey45"))
183 (t (:bold t)))
184 "`diff-mode' face inherited by hunk and index header faces."
185 :group 'diff-mode)
186 (defvar diff-header-face 'diff-header-face)
188 (defface diff-file-header-face
189 '((((type tty pc) (class color) (background light))
190 (:foreground "yellow" :bold t))
191 (((type tty pc) (class color) (background dark))
192 (:foreground "cyan" :bold t))
193 (((class color) (background light))
194 (:background "grey70" :bold t))
195 (((class color) (background dark))
196 (:background "grey60" :bold t))
197 (t (:bold t))) ; :height 1.3
198 "`diff-mode' face used to highlight file header lines."
199 :group 'diff-mode)
200 (defvar diff-file-header-face 'diff-file-header-face)
202 (defface diff-index-face
203 '((t (:inherit diff-file-header-face)))
204 "`diff-mode' face used to highlight index header lines."
205 :group 'diff-mode)
206 (defvar diff-index-face 'diff-index-face)
208 (defface diff-hunk-header-face
209 '((t (:inherit diff-header-face)))
210 "`diff-mode' face used to highlight hunk header lines."
211 :group 'diff-mode)
212 (defvar diff-hunk-header-face 'diff-hunk-header-face)
214 (defface diff-removed-face
215 '((t (:inherit diff-changed-face)))
216 "`diff-mode' face used to highlight removed lines."
217 :group 'diff-mode)
218 (defvar diff-removed-face 'diff-removed-face)
220 (defface diff-added-face
221 '((t (:inherit diff-changed-face)))
222 "`diff-mode' face used to highlight added lines."
223 :group 'diff-mode)
224 (defvar diff-added-face 'diff-added-face)
226 (defface diff-changed-face
227 '((((type tty pc) (class color) (background light))
228 (:foreground "magenta" :bold t :italic t))
229 (((type tty pc) (class color) (background dark))
230 (:foreground "yellow" :bold t :italic t))
231 (t ()))
232 "`diff-mode' face used to highlight changed lines."
233 :group 'diff-mode)
234 (defvar diff-changed-face 'diff-changed-face)
236 (defface diff-function-face
237 '((t (:inherit diff-context-face)))
238 "`diff-mode' face used to highlight function names produced by \"diff -p\"."
239 :group 'diff-mode)
240 (defvar diff-function-face 'diff-function-face)
242 (defface diff-context-face
243 '((((class color) (background light))
244 (:foreground "grey50"))
245 (((class color) (background dark))
246 (:foreground "grey70"))
247 (t ))
248 "`diff-mode' face used to highlight context and other side-information."
249 :group 'diff-mode)
250 (defvar diff-context-face 'diff-context-face)
252 (defface diff-nonexistent-face
253 '((t (:inherit diff-file-header-face)))
254 "`diff-mode' face used to highlight nonexistent files in recursive diffs."
255 :group 'diff-mode)
256 (defvar diff-nonexistent-face 'diff-nonexistent-face)
258 (defvar diff-font-lock-keywords
259 '(("^\\(@@ -[0-9,]+ \\+[0-9,]+ @@\\)\\(.*\\)$" ;unified
260 (1 diff-hunk-header-face)
261 (2 diff-function-face))
262 ("^--- .+ ----$" . diff-hunk-header-face) ;context
263 ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
264 (1 diff-hunk-header-face)
265 (2 diff-function-face))
266 ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context
267 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\(\\S-+\\)\\(.*[^*-]\\)?\n"
268 (0 diff-header-face) (2 diff-file-header-face prepend))
269 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face)
270 ("^!.*\n" . diff-changed-face) ;context
271 ("^[+>].*\n" . diff-added-face)
272 ("^[-<].*\n" . diff-removed-face)
273 ("^Index: \\(.+\\).*\n" (0 diff-header-face) (1 diff-index-face prepend))
274 ("^Only in .*\n" . diff-nonexistent-face)
275 ("^#.*" . font-lock-string-face)
276 ("^[^-=+*!<>].*\n" . diff-context-face)))
278 (defconst diff-font-lock-defaults
279 '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
281 (defvar diff-imenu-generic-expression
282 ;; Prefer second name as first is most likely to be a backup or
283 ;; version-control name. The [\t\n] at the end of the unidiff pattern
284 ;; catches Debian source diff files (which lack the trailing date).
285 '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
286 (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
288 ;;;;
289 ;;;; Compile support
290 ;;;;
292 (defvar diff-file-regexp-alist
293 '(("Index: \\(.+\\)" 1)))
295 (defvar diff-error-regexp-alist
296 '(("@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@" nil 2)
297 ("--- \\([0-9]+\\),[0-9]+ ----" nil 1)
298 ("\\([0-9]+\\)\\(,[0-9]+\\)?[adc]\\([0-9]+\\)" nil 3)))
300 ;;;;
301 ;;;; Movement
302 ;;;;
304 (defconst diff-hunk-header-re "^\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")
305 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+\\|\\*\\*\\* .+\n---\\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1)))
306 (defvar diff-narrowed-to nil)
308 (defun diff-end-of-hunk (&optional style)
309 (if (looking-at diff-hunk-header-re) (goto-char (match-end 0)))
310 (let ((end (and (re-search-forward (case style
311 (unified "^[^-+# \\]")
312 (context "^[^-+#! \\]")
313 (normal "^[^<>#\\]")
314 (t "^[^-+#!<> \\]"))
315 nil t)
316 (match-beginning 0))))
317 ;; The return value is used by easy-mmode-define-navigation.
318 (goto-char (or end (point-max)))))
320 (defun diff-beginning-of-hunk ()
321 (beginning-of-line)
322 (unless (looking-at diff-hunk-header-re)
323 (forward-line 1)
324 (condition-case ()
325 (re-search-backward diff-hunk-header-re)
326 (error (error "Can't find the beginning of the hunk")))))
328 (defun diff-beginning-of-file ()
329 (beginning-of-line)
330 (unless (looking-at diff-file-header-re)
331 (forward-line 2)
332 (condition-case ()
333 (re-search-backward diff-file-header-re)
334 (error (error "Can't find the beginning of the file")))))
336 (defun diff-end-of-file ()
337 (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
338 (re-search-forward "^[^-+#!<>0-9@* \\]" nil 'move)
339 (beginning-of-line))
341 ;; Define diff-{hunk,file}-{prev,next}
342 (easy-mmode-define-navigation
343 diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk)
344 (easy-mmode-define-navigation
345 diff-file diff-file-header-re "file" diff-end-of-hunk)
347 (defun diff-restrict-view (&optional arg)
348 "Restrict the view to the current hunk.
349 If the prefix ARG is given, restrict the view to the current file instead."
350 (interactive "P")
351 (save-excursion
352 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk))
353 (narrow-to-region (point)
354 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
355 (point)))
356 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
359 (defun diff-hunk-kill ()
360 "Kill current hunk."
361 (interactive)
362 (diff-beginning-of-hunk)
363 (let ((start (point))
364 (firsthunk (save-excursion
365 (ignore-errors
366 (diff-beginning-of-file) (diff-hunk-next) (point))))
367 (nexthunk (save-excursion
368 (ignore-errors
369 (diff-hunk-next) (point))))
370 (nextfile (save-excursion
371 (ignore-errors
372 (diff-file-next) (point)))))
373 (if (and firsthunk (= firsthunk start)
374 (or (null nexthunk)
375 (and nextfile (> nexthunk nextfile))))
376 ;; we're the only hunk for this file, so kill the file
377 (diff-file-kill)
378 (diff-end-of-hunk)
379 (kill-region start (point)))))
381 (defun diff-file-kill ()
382 "Kill current file's hunks."
383 (interactive)
384 (diff-beginning-of-file)
385 (let* ((start (point))
386 (prevhunk (save-excursion
387 (ignore-errors
388 (diff-hunk-prev) (point))))
389 (index (save-excursion
390 (re-search-backward "^Index: " prevhunk t))))
391 (when index (setq start index))
392 (diff-end-of-file)
393 (kill-region start (point))))
395 (defun diff-kill-junk ()
396 "Kill spurious empty diffs."
397 (interactive)
398 (save-excursion
399 (let ((inhibit-read-only t))
400 (goto-char (point-min))
401 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
402 "\\([^-+!* <>].*\n\\)*?"
403 "\\(\\(Index:\\) \\|"
404 diff-file-header-re "\\)")
405 nil t)
406 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
407 (match-beginning 3))
408 (beginning-of-line)))))
410 (defun diff-count-matches (re start end)
411 (save-excursion
412 (let ((n 0))
413 (goto-char start)
414 (while (re-search-forward re end t) (incf n))
415 n)))
417 (defun diff-split-hunk ()
418 "Split the current (unified diff) hunk at point into two hunks."
419 (interactive)
420 (beginning-of-line)
421 (let ((pos (point))
422 (start (progn (diff-beginning-of-hunk) (point))))
423 (unless (looking-at "@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@")
424 (error "diff-split-hunk only works on unified context diffs"))
425 (forward-line 1)
426 (let* ((start1 (string-to-number (match-string 1)))
427 (start2 (string-to-number (match-string 2)))
428 (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
429 (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos))))
430 (goto-char pos)
431 ;; Hopefully the after-change-function will not screw us over.
432 (insert "@@ -" (number-to-string newstart1) ",1 +"
433 (number-to-string newstart2) ",1 @@\n")
434 ;; Fix the original hunk-header.
435 (diff-fixup-modifs start pos))))
438 ;;;;
439 ;;;; jump to other buffers
440 ;;;;
442 (defvar diff-remembered-files-alist nil)
444 (defun diff-filename-drop-dir (file)
445 (when (string-match "/" file) (substring file (match-end 0))))
447 (defun diff-merge-strings (ancestor from to)
448 "Merge the diff between ANCESTOR and FROM into TO.
449 Returns the merged string if successful or nil otherwise.
450 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
451 If ANCESTOR = FROM, returns TO.
452 If ANCESTOR = TO, returns FROM.
453 The heuristic is simplistic and only really works for cases
454 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
455 ;; Ideally, we want:
456 ;; AMB ANB CMD -> CND
457 ;; but that's ambiguous if `foo' or `bar' is empty:
458 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
459 (let ((str (concat ancestor "\n" from "\n" to)))
460 (when (and (string-match (concat
461 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
462 "\\1\\(.*\\)\\3\n"
463 "\\(.*\\(\\2\\).*\\)\\'") str)
464 (equal to (match-string 5 str)))
465 (concat (substring str (match-beginning 5) (match-beginning 6))
466 (match-string 4 str)
467 (substring str (match-end 6) (match-end 5))))))
469 (defun diff-find-file-name (&optional old)
470 "Return the file corresponding to the current patch.
471 Non-nil OLD means that we want the old file."
472 (save-excursion
473 (unless (looking-at diff-file-header-re)
474 (or (ignore-errors (diff-beginning-of-file))
475 (re-search-forward diff-file-header-re nil t)))
476 (let* ((limit (save-excursion
477 (condition-case ()
478 (progn (diff-hunk-prev) (point))
479 (error (point-min)))))
480 (header-files
481 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
482 (list (if old (match-string 1) (match-string 3))
483 (if old (match-string 3) (match-string 1)))
484 (forward-line 1) nil))
485 (fs (append
486 (when (save-excursion
487 (re-search-backward "^Index: \\(.+\\)" limit t))
488 (list (match-string 1)))
489 header-files
490 (when (re-search-backward "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" nil t)
491 (list (if old (match-string 2) (match-string 4))
492 (if old (match-string 4) (match-string 2))))))
493 (fs (delq nil fs)))
495 ;; use any previously used preference
496 (cdr (assoc fs diff-remembered-files-alist))
497 ;; try to be clever and use previous choices as an inspiration
498 (dolist (rf diff-remembered-files-alist)
499 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
500 (if (and newfile (file-exists-p newfile)) (return newfile))))
501 ;; look for each file in turn. If none found, try again but
502 ;; ignoring the first level of directory, ...
503 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
504 (file nil nil))
505 ((or (null files)
506 (setq file (do* ((files files (cdr files))
507 (file (car files) (car files)))
508 ((or (null file) (file-exists-p file))
509 file))))
510 file))
511 ;; <foo>.rej patches implicitly apply to <foo>
512 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
513 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
514 (when (file-exists-p file) file)))
515 ;; if all else fails, ask the user
516 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
517 nil (first fs) t (first fs))))
518 (set (make-local-variable 'diff-remembered-files-alist)
519 (cons (cons fs file) diff-remembered-files-alist))
520 file)))))
523 (defun diff-mouse-goto-source (event)
524 "Run `diff-goto-source' for the diff at a mouse click."
525 (interactive "e")
526 (save-excursion
527 (mouse-set-point event)
528 (diff-goto-source)))
531 (defun diff-ediff-patch ()
532 "Call `ediff-patch-file' on the current buffer."
533 (interactive)
534 (condition-case err
535 (ediff-patch-file nil (current-buffer))
536 (wrong-number-of-arguments (ediff-patch-file))))
538 ;;;;
539 ;;;; Conversion functions
540 ;;;;
542 ;;(defvar diff-inhibit-after-change nil
543 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
545 (defun diff-unified->context (start end)
546 "Convert unified diffs to context diffs.
547 START and END are either taken from the region (if a prefix arg is given) or
548 else cover the whole bufer."
549 (interactive (if current-prefix-arg
550 (list (mark) (point))
551 (list (point-min) (point-max))))
552 (unless (markerp end) (setq end (copy-marker end)))
553 (let (;;(diff-inhibit-after-change t)
554 (inhibit-read-only t))
555 (save-excursion
556 (goto-char start)
557 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*\\)$" nil t)
558 (< (point) end))
559 (combine-after-change-calls
560 (if (match-beginning 2)
561 ;; we matched a file header
562 (progn
563 ;; use reverse order to make sure the indices are kept valid
564 (replace-match "---" t t nil 3)
565 (replace-match "***" t t nil 2))
566 ;; we matched a hunk header
567 (let ((line1 (match-string 4))
568 (lines1 (match-string 5))
569 (line2 (match-string 6))
570 (lines2 (match-string 7)))
571 (replace-match
572 (concat "***************\n*** " line1 ","
573 (number-to-string (+ (string-to-number line1)
574 (string-to-number lines1)
575 -1)) " ****"))
576 (forward-line 1)
577 (save-restriction
578 (narrow-to-region (point)
579 (progn (diff-end-of-hunk 'unified) (point)))
580 (let ((hunk (buffer-string)))
581 (goto-char (point-min))
582 (if (not (save-excursion (re-search-forward "^-" nil t)))
583 (delete-region (point) (point-max))
584 (goto-char (point-max))
585 (let ((modif nil) last-pt)
586 (while (progn (setq last-pt (point))
587 (= (forward-line -1) 0))
588 (case (char-after)
589 (? (insert " ") (setq modif nil) (backward-char 1))
590 (?+ (delete-region (point) last-pt) (setq modif t))
591 (?- (if (not modif)
592 (progn (forward-char 1)
593 (insert " "))
594 (delete-char 1)
595 (insert "! "))
596 (backward-char 2))
597 (?\\ (when (save-excursion (forward-line -1)
598 (= (char-after) ?+))
599 (delete-region (point) last-pt) (setq modif t)))
600 (t (setq modif nil))))))
601 (goto-char (point-max))
602 (save-excursion
603 (insert "--- " line2 ","
604 (number-to-string (+ (string-to-number line2)
605 (string-to-number lines2)
606 -1)) " ----\n" hunk))
607 ;;(goto-char (point-min))
608 (forward-line 1)
609 (if (not (save-excursion (re-search-forward "^+" nil t)))
610 (delete-region (point) (point-max))
611 (let ((modif nil) (delete nil))
612 (while (not (eobp))
613 (case (char-after)
614 (? (insert " ") (setq modif nil) (backward-char 1))
615 (?- (setq delete t) (setq modif t))
616 (?+ (if (not modif)
617 (progn (forward-char 1)
618 (insert " "))
619 (delete-char 1)
620 (insert "! "))
621 (backward-char 2))
622 (?\\ (when (save-excursion (forward-line 1)
623 (not (eobp)))
624 (setq delete t) (setq modif t)))
625 (t (setq modif nil)))
626 (let ((last-pt (point)))
627 (forward-line 1)
628 (when delete
629 (delete-region last-pt (point))
630 (setq delete nil)))))))))))))))
632 (defun diff-context->unified (start end)
633 "Convert context diffs to unified diffs.
634 START and END are either taken from the region (if a prefix arg is given) or
635 else cover the whole bufer."
636 (interactive (if current-prefix-arg
637 (list (mark) (point))
638 (list (point-min) (point-max))))
639 (unless (markerp end) (setq end (copy-marker end)))
640 (let (;;(diff-inhibit-after-change t)
641 (inhibit-read-only t))
642 (save-excursion
643 (goto-char start)
644 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
645 (< (point) end))
646 (combine-after-change-calls
647 (if (match-beginning 2)
648 ;; we matched a file header
649 (progn
650 ;; use reverse order to make sure the indices are kept valid
651 (replace-match "+++" t t nil 3)
652 (replace-match "---" t t nil 2))
653 ;; we matched a hunk header
654 (let ((line1s (match-string 4))
655 (line1e (match-string 5))
656 (pt1 (match-beginning 0)))
657 (replace-match "")
658 (unless (re-search-forward
659 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
660 (error "Can't find matching `--- n1,n2 ----' line"))
661 (let ((line2s (match-string 1))
662 (line2e (match-string 2))
663 (pt2 (progn
664 (delete-region (progn (beginning-of-line) (point))
665 (progn (forward-line 1) (point)))
666 (point-marker))))
667 (goto-char pt1)
668 (forward-line 1)
669 (while (< (point) pt2)
670 (case (char-after)
671 ((?! ?-) (delete-char 2) (insert "-") (forward-line 1))
672 (?\ ;merge with the other half of the chunk
673 (let* ((endline2
674 (save-excursion
675 (goto-char pt2) (forward-line 1) (point)))
676 (c (char-after pt2)))
677 (case c
678 ((?! ?+)
679 (insert "+"
680 (prog1 (buffer-substring (+ pt2 2) endline2)
681 (delete-region pt2 endline2))))
682 (?\ ;FIXME: check consistency
683 (delete-region pt2 endline2)
684 (delete-char 1)
685 (forward-line 1))
686 (?\\ (forward-line 1))
687 (t (delete-char 1) (forward-line 1)))))
688 (t (forward-line 1))))
689 (while (looking-at "[+! ] ")
690 (if (/= (char-after) ?!) (forward-char 1)
691 (delete-char 1) (insert "+"))
692 (delete-char 1) (forward-line 1))
693 (save-excursion
694 (goto-char pt1)
695 (insert "@@ -" line1s ","
696 (number-to-string (- (string-to-number line1e)
697 (string-to-number line1s)
698 -1))
699 " +" line2s ","
700 (number-to-string (- (string-to-number line2e)
701 (string-to-number line2s)
702 -1)) " @@"))))))))))
704 (defun diff-reverse-direction (start end)
705 "Reverse the direction of the diffs.
706 START and END are either taken from the region (if a prefix arg is given) or
707 else cover the whole bufer."
708 (interactive (if current-prefix-arg
709 (list (mark) (point))
710 (list (point-min) (point-max))))
711 (unless (markerp end) (setq end (copy-marker end)))
712 (let (;;(diff-inhibit-after-change t)
713 (inhibit-read-only t))
714 (save-excursion
715 (goto-char start)
716 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
717 (< (point) end))
718 (combine-after-change-calls
719 (cond
720 ;; a file header
721 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
722 ;; a context-diff hunk header
723 ((match-beginning 6)
724 (let ((pt-lines1 (match-beginning 6))
725 (lines1 (match-string 6)))
726 (replace-match "" nil nil nil 6)
727 (forward-line 1)
728 (let ((half1s (point)))
729 (while (looking-at "[-! \\][ \t]\\|#")
730 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
731 (forward-line 1))
732 (let ((half1 (delete-and-extract-region half1s (point))))
733 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
734 (insert half1)
735 (error "Can't find matching `--- n1,n2 ----' line"))
736 (let ((str1 (match-string 1)))
737 (replace-match lines1 nil nil nil 1)
738 (forward-line 1)
739 (let ((half2s (point)))
740 (while (looking-at "[!+ \\][ \t]\\|#")
741 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
742 (forward-line 1))
743 (let ((half2 (delete-and-extract-region half2s (point))))
744 (insert (or half1 ""))
745 (goto-char half1s)
746 (insert (or half2 ""))))
747 (goto-char pt-lines1)
748 (insert str1))))))
749 ;; a unified-diff hunk header
750 ((match-beginning 7)
751 (replace-match "@@ -\\8 +\\7 @@" nil)
752 (forward-line 1)
753 (let ((c (char-after)) first last)
754 (while (case (setq c (char-after))
755 (?- (setq first (or first (point)))
756 (delete-char 1) (insert "+") t)
757 (?+ (setq last (or last (point)))
758 (delete-char 1) (insert "-") t)
759 ((?\\ ?#) t)
760 (t (when (and first last (< first last))
761 (let ((str
762 (save-excursion
763 (delete-and-extract-region first last))))
764 (insert str)))
765 (setq first nil last nil)
766 (equal ?\ c)))
767 (forward-line 1))))))))))
769 (defun diff-fixup-modifs (start end)
770 "Fixup the hunk headers (in case the buffer was modified).
771 START and END are either taken from the region (if a prefix arg is given) or
772 else cover the whole bufer."
773 (interactive (if current-prefix-arg
774 (list (mark) (point))
775 (list (point-min) (point-max))))
776 (let ((inhibit-read-only t))
777 (save-excursion
778 (goto-char end) (diff-end-of-hunk)
779 (let ((plus 0) (minus 0) (space 0) (bang 0))
780 (while (and (= (forward-line -1) 0) (<= start (point)))
781 (if (not (looking-at "\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|[-*][-*][-*] .+ [-*][-*][-*][-*]\\)$"))
782 (case (char-after)
783 (?\ (incf space))
784 (?+ (incf plus))
785 (?- (incf minus))
786 (?! (incf bang))
787 ((?\\ ?#) nil)
788 (t (setq space 0 plus 0 minus 0 bang 0)))
789 (cond
790 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@.*$")
791 (let* ((old1 (match-string 1))
792 (old2 (match-string 2))
793 (new1 (number-to-string (+ space minus)))
794 (new2 (number-to-string (+ space plus))))
795 (unless (string= new2 old2) (replace-match new2 t t nil 2))
796 (unless (string= new1 old1) (replace-match new1 t t nil 1))))
797 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
798 (when (> (+ space bang plus) 0)
799 (let* ((old1 (match-string 1))
800 (old2 (match-string 2))
801 (new (number-to-string
802 (+ space bang plus -1 (string-to-number old1)))))
803 (unless (string= new old2) (replace-match new t t nil 2)))))
804 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
805 (when (> (+ space bang minus) 0)
806 (let* ((old (match-string 1))
807 (new (format
808 (concat "%0" (number-to-string (length old)) "d")
809 (+ space bang minus -1 (string-to-number old)))))
810 (unless (string= new old) (replace-match new t t nil 2))))))
811 (setq space 0 plus 0 minus 0 bang 0)))))))
813 ;;;;
814 ;;;; Hooks
815 ;;;;
817 (defun diff-write-contents-hooks ()
818 "Fixup hunk headers if necessary."
819 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
820 nil)
822 ;; It turns out that making changes in the buffer from within an
823 ;; *-change-function is asking for trouble, whereas making them
824 ;; from a post-command-hook doesn't pose much problems
825 (defvar diff-unhandled-changes nil)
826 (defun diff-after-change-function (beg end len)
827 "Remember to fixup the hunk header.
828 See `after-change-functions' for the meaning of BEG, END and LEN."
829 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
830 ;; incorrect, but it turns out that inhibit-read-only is normally not set
831 ;; inside editing commands, while it tends to be set when the buffer gets
832 ;; updated by an async process or by a conversion function, both of which
833 ;; would rather not be uselessly slowed down by this hook.
834 (when (and (not undo-in-progress) (not inhibit-read-only))
835 (if diff-unhandled-changes
836 (setq diff-unhandled-changes
837 (cons (min beg (car diff-unhandled-changes))
838 (max beg (cdr diff-unhandled-changes))))
839 (setq diff-unhandled-changes (cons beg end)))))
841 (defun diff-post-command-hook ()
842 "Fixup hunk headers if necessary."
843 (when (consp diff-unhandled-changes)
844 (ignore-errors
845 (save-excursion
846 (goto-char (car diff-unhandled-changes))
847 (unless (ignore-errors
848 (diff-beginning-of-hunk)
849 (save-excursion
850 (diff-end-of-hunk)
851 (> (point) (car diff-unhandled-changes))))
852 (goto-char (car diff-unhandled-changes))
853 (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
854 (diff-beginning-of-hunk))
855 (diff-fixup-modifs (point) (cdr diff-unhandled-changes))))
856 (setq diff-unhandled-changes nil)))
858 ;;;;
859 ;;;; The main function
860 ;;;;
862 ;;;###autoload
863 (define-derived-mode diff-mode fundamental-mode "Diff"
864 "Major mode for viewing/editing context diffs.
865 Supports unified and context diffs as well as (to a lesser extent) normal diffs.
866 When the buffer is read-only, the ESC prefix is not necessary.
867 This mode runs `diff-mode-hook'.
868 \\{diff-mode-map}"
869 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
870 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
871 (set (make-local-variable 'imenu-generic-expression)
872 diff-imenu-generic-expression)
873 ;; These are not perfect. They would be better done separately for
874 ;; context diffs and unidiffs.
875 ;; (set (make-local-variable 'paragraph-start)
876 ;; (concat "@@ " ; unidiff hunk
877 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
878 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
879 ;; ; start (first or second line)
880 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
881 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
882 ;; compile support
884 ;;;; compile support is not good enough yet. Also it can be annoying
885 ;; and should thus only be enabled conditionally.
886 ;; (set (make-local-variable 'compilation-file-regexp-alist)
887 ;; diff-file-regexp-alist)
888 ;; (set (make-local-variable 'compilation-error-regexp-alist)
889 ;; diff-error-regexp-alist)
890 ;; (when (string-match "\\.rej\\'" (or buffer-file-name ""))
891 ;; (set (make-local-variable 'compilation-current-file)
892 ;; (substring buffer-file-name 0 (match-beginning 0))))
893 ;; (compilation-shell-minor-mode 1)
895 ;; setup change hooks
896 (toggle-read-only t)
897 (if (not diff-update-on-the-fly)
898 (add-hook 'write-contents-hooks 'diff-write-contents-hooks)
899 (make-local-variable 'diff-unhandled-changes)
900 (add-hook 'after-change-functions 'diff-after-change-function nil t)
901 (add-hook 'post-command-hook 'diff-post-command-hook nil t))
902 ;; Neat trick from Dave Love to add more bindings in read-only mode:
903 (add-to-list (make-local-variable 'minor-mode-overriding-map-alist)
904 (cons 'buffer-read-only diff-mode-shared-map))
905 ;; add-log support
906 (set (make-local-variable 'add-log-current-defun-function)
907 'diff-current-defun)
908 (set (make-local-variable 'add-log-buffer-file-name-function)
909 'diff-find-file-name))
911 ;;;###autoload
912 (define-minor-mode diff-minor-mode
913 "Minor mode for viewing/editing context diffs.
914 \\{diff-minor-mode-map}"
915 nil " Diff" nil
916 ;; FIXME: setup font-lock
917 ;; setup change hooks
918 (if (not diff-update-on-the-fly)
919 (add-hook 'write-contents-hooks 'diff-write-contents-hooks)
920 (make-local-variable 'diff-unhandled-changes)
921 (add-hook 'after-change-functions 'diff-after-change-function nil t)
922 (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
926 ;;; Misc operations that have proved useful at some point.
929 (defun diff-next-complex-hunk ()
930 "Jump to the next \"complex\" hunk.
931 \"Complex\" is approximated by \"the hunk changes the number of lines\".
932 Only works for unified diffs."
933 (interactive)
934 (while
935 (and (re-search-forward "^@@ [-0-9]+,\\([0-9]+\\) [+0-9]+,\\([0-9]+\\) @@"
936 nil t)
937 (equal (match-string 1) (match-string 2)))))
939 (defun diff-hunk-text (hunk destp &optional char-offset)
940 "Return the literal source text from HUNK.
941 if DESTP is nil return the source, otherwise the destination text.
942 If CHAR-OFFSET is non-nil, it should be a char-offset in
943 HUNK, and instead of a string, a cons cell is returned whose car is the
944 appropriate text, and whose cdr is the corresponding char-offset in that text."
945 (with-temp-buffer
946 (insert hunk)
947 (goto-char (point-min))
948 (let ((src-pos nil)
949 (dst-pos nil)
950 (divider-pos nil)
951 (num-pfx-chars 2))
952 ;; Set the following variables:
953 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
954 ;; DST-POS buffer pos of the destination part of the hunk or nil
955 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
956 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
957 (cond ((looking-at "^@@")
958 ;; unified diff
959 (setq num-pfx-chars 1)
960 (forward-line 1)
961 (setq src-pos (point) dst-pos (point)))
962 ((looking-at "^\\*\\*")
963 ;; context diff
964 (forward-line 2)
965 (setq src-pos (point))
966 (re-search-forward "^--- " nil t)
967 (forward-line 0)
968 (setq divider-pos (point))
969 (forward-line 1)
970 (setq dst-pos (point)))
971 ((looking-at "^[0-9]+a[0-9,]+$")
972 ;; normal diff, insert
973 (forward-line 1)
974 (setq dst-pos (point)))
975 ((looking-at "^[0-9,]+d[0-9]+$")
976 ;; normal diff, delete
977 (forward-line 1)
978 (setq src-pos (point)))
979 ((looking-at "^[0-9,]+c[0-9,]+$")
980 ;; normal diff, change
981 (forward-line 1)
982 (setq src-pos (point))
983 (re-search-forward "^---$" nil t)
984 (forward-line 0)
985 (setq divider-pos (point))
986 (forward-line 1)
987 (setq dst-pos (point)))
989 (error "Unknown diff hunk type")))
991 (if (if destp (null dst-pos) (null src-pos))
992 ;; Implied empty text
993 (if char-offset '("" . 0) "")
995 ;; For context diffs, either side can be empty, (if there's only
996 ;; added or only removed text). We should then use the other side.
997 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
998 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
1000 (when char-offset (goto-char (+ (point-min) char-offset)))
1002 ;; Get rid of anything except the desired text.
1003 (save-excursion
1004 ;; Delete unused text region
1005 (let ((keep (if destp dst-pos src-pos)))
1006 (when (and divider-pos (> divider-pos keep))
1007 (delete-region divider-pos (point-max)))
1008 (delete-region (point-min) keep))
1009 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1010 (let ((kill-char (if destp ?- ?+)))
1011 (goto-char (point-min))
1012 (while (not (eobp))
1013 (if (eq (char-after) kill-char)
1014 (delete-region (point) (progn (forward-line 1) (point)))
1015 (delete-char num-pfx-chars)
1016 (forward-line 1)))))
1018 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1019 (if char-offset (cons text (- (point) (point-min))) text))))))
1022 (defun diff-find-text (text)
1023 "Return the buffer position of the nearest occurrence of TEXT.
1024 If TEXT isn't found, nil is returned."
1025 (let* ((orig (point))
1026 (forw (and (search-forward text nil t)
1027 (match-beginning 0)))
1028 (back (and (goto-char (+ orig (length text)))
1029 (search-backward text nil t)
1030 (match-beginning 0))))
1031 ;; Choose the closest match.
1032 (if (and forw back)
1033 (if (> (- forw orig) (- orig back)) back forw)
1034 (or back forw))))
1036 (defsubst diff-xor (a b) (if a (not b) b))
1038 (defun diff-find-source-location (&optional other-file reverse)
1039 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED)."
1040 (save-excursion
1041 (let* ((other (diff-xor other-file diff-jump-to-old-file))
1042 (char-offset (- (point) (progn (diff-beginning-of-hunk) (point))))
1043 (hunk (buffer-substring (point)
1044 (save-excursion (diff-end-of-hunk) (point))))
1045 (old (diff-hunk-text hunk reverse char-offset))
1046 (new (diff-hunk-text hunk (not reverse) char-offset))
1047 ;; Find the location specification.
1048 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
1049 (error "Can't find the hunk header")
1050 (if other (match-string 1)
1051 (if (match-end 3) (match-string 3)
1052 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
1053 (error "Can't find the hunk separator"))
1054 (match-string 1)))))
1055 (file (or (diff-find-file-name other) (error "Can't find the file")))
1056 (buf (find-file-noselect file)))
1057 ;; Update the user preference if he so wished.
1058 (when (> (prefix-numeric-value other-file) 8)
1059 (setq diff-jump-to-old-file other))
1060 (with-current-buffer buf
1061 (goto-line (string-to-number line))
1062 (let* ((orig-pos (point))
1063 (pos (diff-find-text (car old)))
1064 (switched nil))
1065 (when (null pos)
1066 (setq pos (diff-find-text (car new)) switched t))
1067 (nconc
1068 (list buf)
1069 (if pos (list (count-lines orig-pos pos) pos) (list nil orig-pos))
1070 (if switched (list new old t) (list old new))))))))
1073 (defun diff-hunk-status-msg (line-offset reversed dry-run)
1074 (let ((msg (if dry-run
1075 (if reversed "already applied" "not yet applied")
1076 (if reversed "undone" "applied"))))
1077 (message (cond ((null line-offset) "Hunk text not found")
1078 ((= line-offset 0) "Hunk %s")
1079 ((= line-offset 1) "Hunk %s at offset %d line")
1080 (t "Hunk %s at offset %d lines"))
1081 msg line-offset)))
1084 (defun diff-apply-hunk (&optional reverse)
1085 "Apply the current hunk to the source file and go to the next.
1086 By default, the new source file is patched, but if the variable
1087 `diff-jump-to-old-file' is non-nil, then the old source file is
1088 patched instead (some commands, such as `diff-goto-source' can change
1089 the value of this variable when given an appropriate prefix argument).
1091 With a prefix argument, REVERSE the hunk."
1092 (interactive "P")
1093 (destructuring-bind (buf line-offset pos old new &optional switched)
1094 (diff-find-source-location nil reverse)
1095 (cond
1096 ((null line-offset)
1097 (error "Can't find the text to patch"))
1098 ((and switched
1099 ;; A reversed patch was detected, perhaps apply it in reverse.
1100 (not (save-window-excursion
1101 (pop-to-buffer buf)
1102 (goto-char (+ pos (cdr old)))
1103 (y-or-n-p
1104 (if reverse
1105 "Hunk hasn't been applied yet; apply it now? "
1106 "Hunk has already been applied; undo it? ")))))
1107 (message "(Nothing done)"))
1109 ;; Apply the hunk
1110 (with-current-buffer buf
1111 (goto-char pos)
1112 (delete-char (length (car old)))
1113 (insert (car new)))
1114 ;; Display BUF in a window
1115 (set-window-point (display-buffer buf) (+ pos (cdr new)))
1116 (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
1117 (when diff-advance-after-apply-hunk
1118 (diff-hunk-next))))))
1121 (defun diff-test-hunk (&optional reverse)
1122 "See whether it's possible to apply the current hunk.
1123 With a prefix argument, try to REVERSE the hunk."
1124 (interactive "P")
1125 (destructuring-bind (buf line-offset pos src dst &optional switched)
1126 (diff-find-source-location nil reverse)
1127 (set-window-point (display-buffer buf) (+ pos (cdr src)))
1128 (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
1131 (defun diff-goto-source (&optional other-file)
1132 "Jump to the corresponding source line.
1133 `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
1134 is given) determines whether to jump to the old or the new file.
1135 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1136 then `diff-jump-to-old-file' is also set, for the next invocations."
1137 (interactive "P")
1138 ;; When pointing at a removal line, we probably want to jump to
1139 ;; the old location, and else to the new (i.e. as if reverting).
1140 ;; This is a convenient detail when using smerge-diff.
1141 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1142 (destructuring-bind (buf line-offset pos src dst &optional switched)
1143 (diff-find-source-location other-file rev)
1144 (pop-to-buffer buf)
1145 (goto-char (+ pos (cdr src)))
1146 (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
1149 (defun diff-current-defun ()
1150 "Find the name of function at point.
1151 For use in `add-log-current-defun-function'."
1152 (destructuring-bind (buf line-offset pos src dst &optional switched)
1153 (diff-find-source-location)
1154 (save-excursion
1155 (beginning-of-line)
1156 (or (when (memq (char-after) '(?< ?-))
1157 ;; Cursor is pointing at removed text. This could be a removed
1158 ;; function, in which case, going to the source buffer will
1159 ;; not help since the function is now removed. Instead,
1160 ;; try to figure out the function name just from the code-fragment.
1161 (let ((old (if switched dst src)))
1162 (with-temp-buffer
1163 (insert (car old))
1164 (goto-char (cdr old))
1165 (funcall (with-current-buffer buf major-mode))
1166 (add-log-current-defun))))
1167 (with-current-buffer buf
1168 (goto-char (+ pos (cdr src)))
1169 (add-log-current-defun))))))
1171 ;; provide the package
1172 (provide 'diff-mode)
1174 ;;; Old Change Log from when diff-mode wasn't part of Emacs:
1175 ;; Revision 1.11 1999/10/09 23:38:29 monnier
1176 ;; (diff-mode-load-hook): dropped.
1177 ;; (auto-mode-alist): also catch *.diffs.
1178 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
1179 ;; for *.rej files (that lack any file name indication).
1181 ;; Revision 1.10 1999/09/30 15:32:11 monnier
1182 ;; added support for "\ No newline at end of file".
1184 ;; Revision 1.9 1999/09/15 00:01:13 monnier
1185 ;; - added basic `compile' support.
1186 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
1187 ;; - diff-kill-file now tries to kill the leading garbage as well.
1189 ;; Revision 1.8 1999/09/13 21:10:09 monnier
1190 ;; - don't use CL in the autoloaded code
1191 ;; - accept diffs using -T
1193 ;; Revision 1.7 1999/09/05 20:53:03 monnier
1194 ;; interface to ediff-patch
1196 ;; Revision 1.6 1999/09/01 20:55:13 monnier
1197 ;; (ediff=patch-file): add bindings to call ediff-patch.
1198 ;; (diff-find-file-name): taken out of diff-goto-source.
1199 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
1200 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
1202 ;; Revision 1.5 1999/08/31 19:18:52 monnier
1203 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
1205 ;; Revision 1.4 1999/08/31 13:01:44 monnier
1206 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
1209 ;;; diff-mode.el ends here