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