*** empty log message ***
[emacs.git] / lisp / diff-mode.el
blob8d836894f5d9d90f2f53e0f93527cca614af5bf9
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.34 2000/11/12 16:59:52 monnier 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 patterns, outline-regexps, 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 ;; - Improve the `compile' support (?).
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-flag 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-flag 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 (defvar diff-font-lock-keywords
253 '(("^\\(@@ -[0-9,]+ \\+[0-9,]+ @@\\)\\(.*\\)$" ;unified
254 (1 diff-hunk-header-face)
255 (2 diff-function-face))
256 ("^--- .+ ----$" ;context
257 . diff-hunk-header-face)
258 ("\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
259 (1 diff-hunk-header-face)
260 (2 diff-function-face))
261 ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context
262 ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\(\\S-+\\)\\(.*[^*-]\\)?\n"
263 (0 diff-header-face) (2 diff-file-header-face prepend))
264 ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face)
265 ("^!.*\n" . diff-changed-face) ;context
266 ("^[+>].*\n" . diff-added-face)
267 ("^[-<].*\n" . diff-removed-face)
268 ("^Index: \\(.+\\).*\n" (0 diff-header-face) (1 diff-index-face prepend))
269 ("^#.*" . font-lock-string-face)
270 ("^[^-=+*!<>].*\n" . diff-context-face)))
272 (defconst diff-font-lock-defaults
273 '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
275 (defvar diff-imenu-generic-expression
276 ;; Prefer second name as first is most likely to be a backup or
277 ;; version-control name. The [\t\n] at the end of the unidiff pattern
278 ;; catches Debian source diff files (which lack the trailing date).
279 '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
280 (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
282 ;;;;
283 ;;;; Compile support
284 ;;;;
286 (defvar diff-file-regexp-alist
287 '(("Index: \\(.+\\)" 1)))
289 (defvar diff-error-regexp-alist
290 '(("@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@" nil 2)
291 ("--- \\([0-9]+\\),[0-9]+ ----" nil 1)
292 ("\\([0-9]+\\)\\(,[0-9]+\\)?[adc]\\([0-9]+\\)" nil 3)))
294 ;;;;
295 ;;;; Movement
296 ;;;;
298 (defconst diff-hunk-header-re "^\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$")
299 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+\\|\\*\\*\\* .+\n---\\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1)))
300 (defvar diff-narrowed-to nil)
302 (defun diff-end-of-hunk (&optional style)
303 (if (looking-at diff-hunk-header-re) (goto-char (match-end 0)))
304 (let ((end (and (re-search-forward (case style
305 (unified "^[^-+# \\]")
306 (context "^[^-+#! \\]")
307 (normal "^[^<>#\\]")
308 (t "^[^-+#!<> \\]"))
309 nil t)
310 (match-beginning 0))))
311 ;; The return value is used by easy-mmode-define-navigation.
312 (goto-char (or end (point-max)))))
314 (defun diff-beginning-of-hunk ()
315 (beginning-of-line)
316 (unless (looking-at diff-hunk-header-re)
317 (forward-line 1)
318 (condition-case ()
319 (re-search-backward diff-hunk-header-re)
320 (error (error "Can't find the beginning of the hunk")))))
322 (defun diff-beginning-of-file ()
323 (beginning-of-line)
324 (unless (looking-at diff-file-header-re)
325 (forward-line 2)
326 (condition-case ()
327 (re-search-backward diff-file-header-re)
328 (error (error "Can't find the beginning of the file")))))
330 (defun diff-end-of-file ()
331 (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
332 (re-search-forward "^[^-+#!<>0-9@* \\]" nil 'move)
333 (beginning-of-line))
335 ;; Define diff-{hunk,file}-{prev,next}
336 (easy-mmode-define-navigation
337 diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk)
338 (easy-mmode-define-navigation
339 diff-file diff-file-header-re "file" diff-end-of-hunk)
341 (defun diff-restrict-view (&optional arg)
342 "Restrict the view to the current hunk.
343 If the prefix ARG is given, restrict the view to the current file instead."
344 (interactive "P")
345 (save-excursion
346 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk))
347 (narrow-to-region (point)
348 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
349 (point)))
350 (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
353 (defun diff-hunk-kill ()
354 "Kill current hunk."
355 (interactive)
356 (diff-beginning-of-hunk)
357 (let ((start (point))
358 (firsthunk (save-excursion
359 (ignore-errors
360 (diff-beginning-of-file) (diff-hunk-next) (point))))
361 (nexthunk (save-excursion
362 (ignore-errors
363 (diff-hunk-next) (point))))
364 (nextfile (save-excursion
365 (ignore-errors
366 (diff-file-next) (point)))))
367 (if (and firsthunk (= firsthunk start)
368 (or (null nexthunk)
369 (and nextfile (> nexthunk nextfile))))
370 ;; we're the only hunk for this file, so kill the file
371 (diff-file-kill)
372 (diff-end-of-hunk)
373 (kill-region start (point)))))
375 (defun diff-file-kill ()
376 "Kill current file's hunks."
377 (interactive)
378 (diff-beginning-of-file)
379 (let* ((start (point))
380 (prevhunk (save-excursion
381 (ignore-errors
382 (diff-hunk-prev) (point))))
383 (index (save-excursion
384 (re-search-backward "^Index: " prevhunk t))))
385 (when index (setq start index))
386 (diff-end-of-file)
387 (kill-region start (point))))
389 (defun diff-kill-junk ()
390 "Kill spurious empty diffs."
391 (interactive)
392 (save-excursion
393 (let ((inhibit-read-only t))
394 (goto-char (point-min))
395 (while (re-search-forward (concat "^\\(Index: .*\n\\)"
396 "\\([^-+!* <>].*\n\\)*?"
397 "\\(\\(Index:\\) \\|"
398 diff-file-header-re "\\)")
399 nil t)
400 (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
401 (match-beginning 3))
402 (beginning-of-line)))))
404 (defun diff-count-matches (re start end)
405 (save-excursion
406 (let ((n 0))
407 (goto-char start)
408 (while (re-search-forward re end t) (incf n))
409 n)))
411 (defun diff-split-hunk ()
412 "Split the current (unified diff) hunk at point into two hunks."
413 (interactive)
414 (beginning-of-line)
415 (let ((pos (point))
416 (start (progn (diff-beginning-of-hunk) (point))))
417 (unless (looking-at "@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@")
418 (error "diff-split-hunk only works on unified context diffs"))
419 (forward-line 1)
420 (let* ((start1 (string-to-number (match-string 1)))
421 (start2 (string-to-number (match-string 2)))
422 (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
423 (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos))))
424 (goto-char pos)
425 ;; Hopefully the after-change-function will not screw us over.
426 (insert "@@ -" (number-to-string newstart1) ",1 +"
427 (number-to-string newstart2) ",1 @@\n")
428 ;; Fix the original hunk-header.
429 (diff-fixup-modifs start pos))))
432 ;;;;
433 ;;;; jump to other buffers
434 ;;;;
436 (defvar diff-remembered-files-alist nil)
438 (defun diff-filename-drop-dir (file)
439 (when (string-match "/" file) (substring file (match-end 0))))
441 (defun diff-merge-strings (ancestor from to)
442 "Merge the diff between ANCESTOR and FROM into TO.
443 Returns the merged string if successful or nil otherwise.
444 The strings are assumed not to contain any \"\\n\" (i.e. end of line).
445 If ANCESTOR = FROM, returns TO.
446 If ANCESTOR = TO, returns FROM.
447 The heuristic is simplistic and only really works for cases
448 like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
449 ;; Ideally, we want:
450 ;; AMB ANB CMD -> CND
451 ;; but that's ambiguous if `foo' or `bar' is empty:
452 ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
453 (let ((str (concat ancestor "\n" from "\n" to)))
454 (when (and (string-match (concat
455 "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
456 "\\1\\(.*\\)\\3\n"
457 "\\(.*\\(\\2\\).*\\)\\'") str)
458 (equal to (match-string 5 str)))
459 (concat (substring str (match-beginning 5) (match-beginning 6))
460 (match-string 4 str)
461 (substring str (match-end 6) (match-end 5))))))
463 (defun diff-find-file-name (&optional old)
464 "Return the file corresponding to the current patch.
465 Non-nil OLD means that we want the old file."
466 (save-excursion
467 (unless (looking-at diff-file-header-re)
468 (or (ignore-errors (diff-beginning-of-file))
469 (re-search-forward diff-file-header-re nil t)))
470 (let* ((limit (save-excursion
471 (condition-case ()
472 (progn (diff-hunk-prev) (point))
473 (error (point-min)))))
474 (header-files
475 (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
476 (list (if old (match-string 1) (match-string 3))
477 (if old (match-string 3) (match-string 1)))
478 (forward-line 1) nil))
479 (fs (append
480 (when (save-excursion
481 (re-search-backward "^Index: \\(.+\\)" limit t))
482 (list (match-string 1)))
483 header-files
484 (when (re-search-backward "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" nil t)
485 (list (if old (match-string 2) (match-string 4))
486 (if old (match-string 4) (match-string 2))))))
487 (fs (delq nil fs)))
489 ;; use any previously used preference
490 (cdr (assoc fs diff-remembered-files-alist))
491 ;; try to be clever and use previous choices as an inspiration
492 (dolist (rf diff-remembered-files-alist)
493 (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
494 (if (and newfile (file-exists-p newfile)) (return newfile))))
495 ;; look for each file in turn. If none found, try again but
496 ;; ignoring the first level of directory, ...
497 (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
498 (file nil nil))
499 ((or (null files)
500 (setq file (do* ((files files (cdr files))
501 (file (car files) (car files)))
502 ((or (null file) (file-exists-p file))
503 file))))
504 file))
505 ;; <foo>.rej patches implicitly apply to <foo>
506 (and (string-match "\\.rej\\'" (or buffer-file-name ""))
507 (let ((file (substring buffer-file-name 0 (match-beginning 0))))
508 (when (file-exists-p file) file)))
509 ;; if all else fails, ask the user
510 (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
511 nil (first fs) t (first fs))))
512 (set (make-local-variable 'diff-remembered-files-alist)
513 (cons (cons fs file) diff-remembered-files-alist))
514 file)))))
517 (defun diff-mouse-goto-source (event)
518 "Run `diff-goto-source' for the diff at a mouse click."
519 (interactive "e")
520 (save-excursion
521 (mouse-set-point event)
522 (diff-goto-source)))
525 (defun diff-ediff-patch ()
526 "Call `ediff-patch-file' on the current buffer."
527 (interactive)
528 (condition-case err
529 (ediff-patch-file nil (current-buffer))
530 (wrong-number-of-arguments (ediff-patch-file))))
532 ;;;;
533 ;;;; Conversion functions
534 ;;;;
536 ;;(defvar diff-inhibit-after-change nil
537 ;; "Non-nil means inhibit `diff-mode's after-change functions.")
539 (defun diff-unified->context (start end)
540 "Convert unified diffs to context diffs.
541 START and END are either taken from the region (if a prefix arg is given) or
542 else cover the whole bufer."
543 (interactive (if current-prefix-arg
544 (list (mark) (point))
545 (list (point-min) (point-max))))
546 (unless (markerp end) (setq end (copy-marker end)))
547 (let (;;(diff-inhibit-after-change t)
548 (inhibit-read-only t))
549 (save-excursion
550 (goto-char start)
551 (while (and (re-search-forward "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*\\)$" nil t)
552 (< (point) end))
553 (combine-after-change-calls
554 (if (match-beginning 2)
555 ;; we matched a file header
556 (progn
557 ;; use reverse order to make sure the indices are kept valid
558 (replace-match "---" t t nil 3)
559 (replace-match "***" t t nil 2))
560 ;; we matched a hunk header
561 (let ((line1 (match-string 4))
562 (lines1 (match-string 5))
563 (line2 (match-string 6))
564 (lines2 (match-string 7)))
565 (replace-match
566 (concat "***************\n*** " line1 ","
567 (number-to-string (+ (string-to-number line1)
568 (string-to-number lines1)
569 -1)) " ****"))
570 (forward-line 1)
571 (save-restriction
572 (narrow-to-region (point)
573 (progn (diff-end-of-hunk 'unified) (point)))
574 (let ((hunk (buffer-string)))
575 (goto-char (point-min))
576 (if (not (save-excursion (re-search-forward "^-" nil t)))
577 (delete-region (point) (point-max))
578 (goto-char (point-max))
579 (let ((modif nil) last-pt)
580 (while (progn (setq last-pt (point))
581 (= (forward-line -1) 0))
582 (case (char-after)
583 (? (insert " ") (setq modif nil) (backward-char 1))
584 (?+ (delete-region (point) last-pt) (setq modif t))
585 (?- (if (not modif)
586 (progn (forward-char 1)
587 (insert " "))
588 (delete-char 1)
589 (insert "! "))
590 (backward-char 2))
591 (?\\ (when (save-excursion (forward-line -1)
592 (= (char-after) ?+))
593 (delete-region (point) last-pt) (setq modif t)))
594 (t (setq modif nil))))))
595 (goto-char (point-max))
596 (save-excursion
597 (insert "--- " line2 ","
598 (number-to-string (+ (string-to-number line2)
599 (string-to-number lines2)
600 -1)) " ----\n" hunk))
601 ;;(goto-char (point-min))
602 (forward-line 1)
603 (if (not (save-excursion (re-search-forward "^+" nil t)))
604 (delete-region (point) (point-max))
605 (let ((modif nil) (delete nil))
606 (while (not (eobp))
607 (case (char-after)
608 (? (insert " ") (setq modif nil) (backward-char 1))
609 (?- (setq delete t) (setq modif t))
610 (?+ (if (not modif)
611 (progn (forward-char 1)
612 (insert " "))
613 (delete-char 1)
614 (insert "! "))
615 (backward-char 2))
616 (?\\ (when (save-excursion (forward-line 1)
617 (not (eobp)))
618 (setq delete t) (setq modif t)))
619 (t (setq modif nil)))
620 (let ((last-pt (point)))
621 (forward-line 1)
622 (when delete
623 (delete-region last-pt (point))
624 (setq delete nil)))))))))))))))
626 (defun diff-context->unified (start end)
627 "Convert context diffs to unified diffs.
628 START and END are either taken from the region (if a prefix arg is given) or
629 else cover the whole bufer."
630 (interactive (if current-prefix-arg
631 (list (mark) (point))
632 (list (point-min) (point-max))))
633 (unless (markerp end) (setq end (copy-marker end)))
634 (let (;;(diff-inhibit-after-change t)
635 (inhibit-read-only t))
636 (save-excursion
637 (goto-char start)
638 (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
639 (< (point) end))
640 (combine-after-change-calls
641 (if (match-beginning 2)
642 ;; we matched a file header
643 (progn
644 ;; use reverse order to make sure the indices are kept valid
645 (replace-match "+++" t t nil 3)
646 (replace-match "---" t t nil 2))
647 ;; we matched a hunk header
648 (let ((line1s (match-string 4))
649 (line1e (match-string 5))
650 (pt1 (match-beginning 0)))
651 (replace-match "")
652 (unless (re-search-forward
653 "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
654 (error "Can't find matching `--- n1,n2 ----' line"))
655 (let ((line2s (match-string 1))
656 (line2e (match-string 2))
657 (pt2 (progn
658 (delete-region (progn (beginning-of-line) (point))
659 (progn (forward-line 1) (point)))
660 (point-marker))))
661 (goto-char pt1)
662 (forward-line 1)
663 (while (< (point) pt2)
664 (case (char-after)
665 ((?! ?-) (delete-char 2) (insert "-") (forward-line 1))
666 (?\ ;merge with the other half of the chunk
667 (let* ((endline2
668 (save-excursion
669 (goto-char pt2) (forward-line 1) (point)))
670 (c (char-after pt2)))
671 (case c
672 ((?! ?+)
673 (insert "+"
674 (prog1 (buffer-substring (+ pt2 2) endline2)
675 (delete-region pt2 endline2))))
676 (?\ ;FIXME: check consistency
677 (delete-region pt2 endline2)
678 (delete-char 1)
679 (forward-line 1))
680 (?\\ (forward-line 1))
681 (t (delete-char 1) (forward-line 1)))))
682 (t (forward-line 1))))
683 (while (looking-at "[+! ] ")
684 (if (/= (char-after) ?!) (forward-char 1)
685 (delete-char 1) (insert "+"))
686 (delete-char 1) (forward-line 1))
687 (save-excursion
688 (goto-char pt1)
689 (insert "@@ -" line1s ","
690 (number-to-string (- (string-to-number line1e)
691 (string-to-number line1s)
692 -1))
693 " +" line2s ","
694 (number-to-string (- (string-to-number line2e)
695 (string-to-number line2s)
696 -1)) " @@"))))))))))
698 (defun diff-reverse-direction (start end)
699 "Reverse the direction of the diffs.
700 START and END are either taken from the region (if a prefix arg is given) or
701 else cover the whole bufer."
702 (interactive (if current-prefix-arg
703 (list (mark) (point))
704 (list (point-min) (point-max))))
705 (unless (markerp end) (setq end (copy-marker end)))
706 (let (;;(diff-inhibit-after-change t)
707 (inhibit-read-only t))
708 (save-excursion
709 (goto-char start)
710 (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
711 (< (point) end))
712 (combine-after-change-calls
713 (cond
714 ;; a file header
715 ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
716 ;; a context-diff hunk header
717 ((match-beginning 6)
718 (let ((pt-lines1 (match-beginning 6))
719 (lines1 (match-string 6)))
720 (replace-match "" nil nil nil 6)
721 (forward-line 1)
722 (let ((half1s (point)))
723 (while (looking-at "[-! \\][ \t]\\|#")
724 (when (= (char-after) ?-) (delete-char 1) (insert "+"))
725 (forward-line 1))
726 (let ((half1 (delete-and-extract-region half1s (point))))
727 (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
728 (insert half1)
729 (error "Can't find matching `--- n1,n2 ----' line"))
730 (let ((str1 (match-string 1)))
731 (replace-match lines1 nil nil nil 1)
732 (forward-line 1)
733 (let ((half2s (point)))
734 (while (looking-at "[!+ \\][ \t]\\|#")
735 (when (= (char-after) ?+) (delete-char 1) (insert "-"))
736 (forward-line 1))
737 (let ((half2 (delete-and-extract-region half2s (point))))
738 (insert (or half1 ""))
739 (goto-char half1s)
740 (insert (or half2 ""))))
741 (goto-char pt-lines1)
742 (insert str1))))))
743 ;; a unified-diff hunk header
744 ((match-beginning 7)
745 (replace-match "@@ -\\8 +\\7 @@" nil)
746 (forward-line 1)
747 (let ((c (char-after)) first last)
748 (while (case (setq c (char-after))
749 (?- (setq first (or first (point)))
750 (delete-char 1) (insert "+") t)
751 (?+ (setq last (or last (point)))
752 (delete-char 1) (insert "-") t)
753 ((?\\ ?#) t)
754 (t (when (and first last (< first last))
755 (let ((str
756 (save-excursion
757 (delete-and-extract-region first last))))
758 (insert str)))
759 (setq first nil last nil)
760 (equal ?\ c)))
761 (forward-line 1))))))))))
763 (defun diff-fixup-modifs (start end)
764 "Fixup the hunk headers (in case the buffer was modified).
765 START and END are either taken from the region (if a prefix arg is given) or
766 else cover the whole bufer."
767 (interactive (if current-prefix-arg
768 (list (mark) (point))
769 (list (point-min) (point-max))))
770 (let ((inhibit-read-only t))
771 (save-excursion
772 (goto-char end) (diff-end-of-hunk)
773 (let ((plus 0) (minus 0) (space 0) (bang 0))
774 (while (and (= (forward-line -1) 0) (<= start (point)))
775 (if (not (looking-at "\\(@@ -[0-9,]+ \\+[0-9,]+ @@.*\\|[-*][-*][-*] .+ [-*][-*][-*][-*]\\)$"))
776 (case (char-after)
777 (?\ (incf space))
778 (?+ (incf plus))
779 (?- (incf minus))
780 (?! (incf bang))
781 ((?\\ ?#) nil)
782 (t (setq space 0 plus 0 minus 0 bang 0)))
783 (cond
784 ((looking-at "@@ -[0-9]+,\\([0-9]*\\) \\+[0-9]+,\\([0-9]*\\) @@.*$")
785 (let* ((old1 (match-string 1))
786 (old2 (match-string 2))
787 (new1 (number-to-string (+ space minus)))
788 (new2 (number-to-string (+ space plus))))
789 (unless (string= new2 old2) (replace-match new2 t t nil 2))
790 (unless (string= new1 old1) (replace-match new1 t t nil 1))))
791 ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
792 (when (> (+ space bang plus) 0)
793 (let* ((old1 (match-string 1))
794 (old2 (match-string 2))
795 (new (number-to-string
796 (+ space bang plus -1 (string-to-number old1)))))
797 (unless (string= new old2) (replace-match new t t nil 2)))))
798 ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
799 (when (> (+ space bang minus) 0)
800 (let* ((old (match-string 1))
801 (new (format
802 (concat "%0" (number-to-string (length old)) "d")
803 (+ space bang minus -1 (string-to-number old)))))
804 (unless (string= new old) (replace-match new t t nil 2))))))
805 (setq space 0 plus 0 minus 0 bang 0)))))))
807 ;;;;
808 ;;;; Hooks
809 ;;;;
811 (defun diff-write-contents-hooks ()
812 "Fixup hunk headers if necessary."
813 (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
814 nil)
816 ;; It turns out that making changes in the buffer from within an
817 ;; *-change-function is asking for trouble, whereas making them
818 ;; from a post-command-hook doesn't pose much problems
819 (defvar diff-unhandled-changes nil)
820 (defun diff-after-change-function (beg end len)
821 "Remember to fixup the hunk header.
822 See `after-change-functions' for the meaning of BEG, END and LEN."
823 ;; Ignoring changes when inhibit-read-only is set is strictly speaking
824 ;; incorrect, but it turns out that inhibit-read-only is normally not set
825 ;; inside editing commands, while it tends to be set when the buffer gets
826 ;; updated by an async process or by a conversion function, both of which
827 ;; would rather not be uselessly slowed down by this hook.
828 (when (and (not undo-in-progress) (not inhibit-read-only))
829 (if diff-unhandled-changes
830 (setq diff-unhandled-changes
831 (cons (min beg (car diff-unhandled-changes))
832 (max beg (cdr diff-unhandled-changes))))
833 (setq diff-unhandled-changes (cons beg end)))))
835 (defun diff-post-command-hook ()
836 "Fixup hunk headers if necessary."
837 (when (consp diff-unhandled-changes)
838 (ignore-errors
839 (save-excursion
840 (goto-char (car diff-unhandled-changes))
841 (unless (ignore-errors
842 (diff-beginning-of-hunk)
843 (save-excursion
844 (diff-end-of-hunk)
845 (> (point) (car diff-unhandled-changes))))
846 (goto-char (car diff-unhandled-changes))
847 (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
848 (diff-beginning-of-hunk))
849 (diff-fixup-modifs (point) (cdr diff-unhandled-changes))))
850 (setq diff-unhandled-changes nil)))
852 ;;;;
853 ;;;; The main function
854 ;;;;
856 ;;;###autoload
857 (define-derived-mode diff-mode fundamental-mode "Diff"
858 "Major mode for viewing/editing context diffs.
859 Supports unified and context diffs as well as (to a lesser extent) normal diffs.
860 When the buffer is read-only, the ESC prefix is not necessary.
861 This mode runs `diff-mode-hook'.
862 \\{diff-mode-map}"
863 (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
864 (set (make-local-variable 'outline-regexp) diff-outline-regexp)
865 (set (make-local-variable 'imenu-generic-expression)
866 diff-imenu-generic-expression)
867 ;; These are not perfect. They would be better done separately for
868 ;; context diffs and unidiffs.
869 ;; (set (make-local-variable 'paragraph-start)
870 ;; (concat "@@ " ; unidiff hunk
871 ;; "\\|\\*\\*\\* " ; context diff hunk or file start
872 ;; "\\|--- [^\t]+\t")) ; context or unidiff file
873 ;; ; start (first or second line)
874 ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
875 ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
876 ;; compile support
877 (set (make-local-variable 'compilation-file-regexp-alist)
878 diff-file-regexp-alist)
879 (set (make-local-variable 'compilation-error-regexp-alist)
880 diff-error-regexp-alist)
881 (when (string-match "\\.rej\\'" (or buffer-file-name ""))
882 (set (make-local-variable 'compilation-current-file)
883 (substring buffer-file-name 0 (match-beginning 0))))
884 (compilation-shell-minor-mode 1)
885 ;; setup change hooks
886 (toggle-read-only t)
887 (if (not diff-update-on-the-fly-flag)
888 (add-hook 'write-contents-hooks 'diff-write-contents-hooks)
889 (make-local-variable 'diff-unhandled-changes)
890 (add-hook 'after-change-functions 'diff-after-change-function nil t)
891 (add-hook 'post-command-hook 'diff-post-command-hook nil t))
892 ;; Neat trick from Dave Love to add more bindings in read-only mode:
893 (add-to-list (make-local-variable 'minor-mode-overriding-map-alist)
894 (cons 'buffer-read-only diff-mode-shared-map))
895 ;; add-log support
896 (set (make-local-variable 'add-log-current-defun-function)
897 'diff-current-defun)
898 (set (make-local-variable 'add-log-buffer-file-name-function)
899 'diff-find-file-name))
901 ;;;###autoload
902 (define-minor-mode diff-minor-mode
903 "Minor mode for viewing/editing context diffs.
904 \\{diff-minor-mode-map}"
905 nil " Diff" nil
906 ;; FIXME: setup font-lock
907 ;; setup change hooks
908 (if (not diff-update-on-the-fly-flag)
909 (add-hook 'write-contents-hooks 'diff-write-contents-hooks)
910 (make-local-variable 'diff-unhandled-changes)
911 (add-hook 'after-change-functions 'diff-after-change-function nil t)
912 (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
916 ;;; Misc operations that have proved useful at some point.
919 (defun diff-next-complex-hunk ()
920 "Jump to the next \"complex\" hunk.
921 \"Complex\" is approximated by \"the hunk changes the number of lines\".
922 Only works for unified diffs."
923 (interactive)
924 (while
925 (and (re-search-forward "^@@ [-0-9]+,\\([0-9]+\\) [+0-9]+,\\([0-9]+\\) @@"
926 nil t)
927 (equal (match-string 1) (match-string 2)))))
929 (defun diff-hunk-text (hunk destp &optional char-offset)
930 "Return the literal source text from HUNK.
931 if DESTP is nil return the source, otherwise the destination text.
932 If CHAR-OFFSET is non-nil, it should be a char-offset in
933 HUNK, and instead of a string, a cons cell is returned whose car is the
934 appropriate text, and whose cdr is the corresponding char-offset in that text."
935 (with-temp-buffer
936 (insert hunk)
937 (goto-char (point-min))
938 (let ((src-pos nil)
939 (dst-pos nil)
940 (divider-pos nil)
941 (num-pfx-chars 2))
942 ;; Set the following variables:
943 ;; SRC-POS buffer pos of the source part of the hunk or nil if none
944 ;; DST-POS buffer pos of the destination part of the hunk or nil
945 ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
946 ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
947 (cond ((looking-at "^@@")
948 ;; unified diff
949 (setq num-pfx-chars 1)
950 (forward-line 1)
951 (setq src-pos (point) dst-pos (point)))
952 ((looking-at "^\\*\\*")
953 ;; context diff
954 (forward-line 2)
955 (setq src-pos (point))
956 (re-search-forward "^--- " nil t)
957 (forward-line 0)
958 (setq divider-pos (point))
959 (forward-line 1)
960 (setq dst-pos (point)))
961 ((looking-at "^[0-9]+a[0-9,]+$")
962 ;; normal diff, insert
963 (forward-line 1)
964 (setq dst-pos (point)))
965 ((looking-at "^[0-9,]+d[0-9]+$")
966 ;; normal diff, delete
967 (forward-line 1)
968 (setq src-pos (point)))
969 ((looking-at "^[0-9,]+c[0-9,]+$")
970 ;; normal diff, change
971 (forward-line 1)
972 (setq src-pos (point))
973 (re-search-forward "^---$" nil t)
974 (forward-line 0)
975 (setq divider-pos (point))
976 (forward-line 1)
977 (setq dst-pos (point)))
979 (error "Unknown diff hunk type")))
981 (if (if destp (null dst-pos) (null src-pos))
982 ;; Implied empty text
983 (if char-offset '("" . 0) "")
985 ;; For context diffs, either side can be empty, (if there's only
986 ;; added or only removed text). We should then use the other side.
987 (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
988 ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
990 (when char-offset (goto-char (+ (point-min) char-offset)))
992 ;; Get rid of anything except the desired text.
993 (save-excursion
994 ;; Delete unused text region
995 (let ((keep (if destp dst-pos src-pos)))
996 (when (and divider-pos (> divider-pos keep))
997 (delete-region divider-pos (point-max)))
998 (delete-region (point-min) keep))
999 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1000 (let ((kill-char (if destp ?- ?+)))
1001 (goto-char (point-min))
1002 (while (not (eobp))
1003 (if (eq (char-after) kill-char)
1004 (delete-region (point) (progn (forward-line 1) (point)))
1005 (delete-char num-pfx-chars)
1006 (forward-line 1)))))
1008 (let ((text (buffer-substring-no-properties (point-min) (point-max))))
1009 (if char-offset (cons text (- (point) (point-min))) text))))))
1012 (defun diff-find-text (text)
1013 "Return the buffer position of the nearest occurrence of TEXT.
1014 If TEXT isn't found, nil is returned."
1015 (let* ((orig (point))
1016 (forw (and (search-forward text nil t)
1017 (match-beginning 0)))
1018 (back (and (goto-char (+ orig (length text)))
1019 (search-backward text nil t)
1020 (match-beginning 0))))
1021 ;; Choose the closest match.
1022 (if (and forw back)
1023 (if (> (- forw orig) (- orig back)) back forw)
1024 (or back forw))))
1026 (defsubst diff-xor (a b) (if a (not b) b))
1028 (defun diff-find-source-location (&optional other-file reverse)
1029 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED)."
1030 (save-excursion
1031 (let* ((other (diff-xor other-file diff-jump-to-old-file-flag))
1032 (char-offset (- (point) (progn (diff-beginning-of-hunk) (point))))
1033 (hunk (buffer-substring (point)
1034 (save-excursion (diff-end-of-hunk) (point))))
1035 (old (diff-hunk-text hunk reverse char-offset))
1036 (new (diff-hunk-text hunk (not reverse) char-offset))
1037 ;; Find the location specification.
1038 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
1039 (error "Can't find the hunk header")
1040 (if other (match-string 1)
1041 (if (match-end 3) (match-string 3)
1042 (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
1043 (error "Can't find the hunk separator"))
1044 (match-string 1)))))
1045 (file (or (diff-find-file-name other) (error "Can't find the file")))
1046 (buf (find-file-noselect file)))
1047 ;; Update the user preference if he so wished.
1048 (when (> (prefix-numeric-value other-file) 8)
1049 (setq diff-jump-to-old-file-flag other))
1050 (with-current-buffer buf
1051 (goto-line (string-to-number line))
1052 (let* ((orig-pos (point))
1053 (pos (diff-find-text (car old)))
1054 (switched nil))
1055 (when (null pos)
1056 (setq pos (diff-find-text (car new)) switched t))
1057 (nconc
1058 (list buf)
1059 (if pos (list (count-lines orig-pos pos) pos) (list nil orig-pos))
1060 (if switched (list new old t) (list old new))))))))
1063 (defun diff-hunk-status-msg (line-offset reversed dry-run)
1064 (let ((msg (if dry-run
1065 (if reversed "already applied" "not yet applied")
1066 (if reversed "undone" "applied"))))
1067 (message (cond ((null line-offset) "Hunk text not found")
1068 ((= line-offset 0) "Hunk %s")
1069 ((= line-offset 1) "Hunk %s at offset %d line")
1070 (t "Hunk %s at offset %d lines"))
1071 msg line-offset)))
1074 (defun diff-apply-hunk (&optional reverse)
1075 "Apply the current hunk to the source file and go to the next.
1076 By default, the new source file is patched, but if the variable
1077 `diff-jump-to-old-file-flag' is non-nil, then the old source file is
1078 patched instead (some commands, such as `diff-goto-source' can change
1079 the value of this variable when given an appropriate prefix argument).
1081 With a prefix argument, REVERSE the hunk."
1082 (interactive "P")
1083 (destructuring-bind (buf line-offset pos old new &optional switched)
1084 (diff-find-source-location nil reverse)
1085 (cond
1086 ((null line-offset)
1087 (error "Can't find the text to patch"))
1088 ((and switched
1089 ;; A reversed patch was detected, perhaps apply it in reverse
1090 (not (save-window-excursion
1091 (pop-to-buffer buf)
1092 (goto-char (+ pos (cdr old)))
1093 (y-or-n-p
1094 (if reverse
1095 "Hunk hasn't been applied yet; apply it now? "
1096 "Hunk has already been applied; undo it? ")))))
1097 (message "(Nothing done)"))
1099 ;; Apply the hunk
1100 (with-current-buffer buf
1101 (goto-char pos)
1102 (delete-char (length (car old)))
1103 (insert (car new)))
1104 ;; Display BUF in a window
1105 (set-window-point (display-buffer buf) (+ pos (cdr new)))
1106 (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
1107 (when diff-advance-after-apply-hunk
1108 (diff-hunk-next))))))
1111 (defun diff-test-hunk (&optional reverse)
1112 "See whether it's possible to apply the current hunk.
1113 With a prefix argument, try to REVERSE the hunk."
1114 (interactive "P")
1115 (destructuring-bind (buf line-offset pos src dst &optional switched)
1116 (diff-find-source-location nil reverse)
1117 (set-window-point (display-buffer buf) (+ pos (cdr src)))
1118 (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
1121 (defun diff-goto-source (&optional other-file)
1122 "Jump to the corresponding source line.
1123 `diff-jump-to-old-file-flag' (or its opposite if the OTHER-FILE prefix arg
1124 is given) determines whether to jump to the old or the new file.
1125 If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
1126 then `diff-jump-to-old-file-flag' is also set, for the next invocations."
1127 (interactive "P")
1128 ;; When pointing at a removal line, we probably want to jump to
1129 ;; the old location, and else to the new (i.e. as if reverting).
1130 ;; This is a convenient detail when using smerge-diff.
1131 (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
1132 (destructuring-bind (buf line-offset pos src dst &optional switched)
1133 (diff-find-source-location other-file rev)
1134 (pop-to-buffer buf)
1135 (goto-char (+ pos (cdr src)))
1136 (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
1139 (defun diff-current-defun ()
1140 "Find the name of function at point.
1141 For use in `add-log-current-defun-function'."
1142 (destructuring-bind (buf line-offset pos src dst &optional switched)
1143 (diff-find-source-location)
1144 (save-excursion
1145 (beginning-of-line)
1146 (or (when (memq (char-after) '(?< ?-))
1147 ;; Cursor is pointing at removed text. This could be a removed
1148 ;; function, in which case, going to the source buffer will
1149 ;; not help since the function is now removed. Instead,
1150 ;; try to figure out the function name just from the code-fragment.
1151 (let ((old (if switched dst src)))
1152 (with-temp-buffer
1153 (insert (car old))
1154 (goto-char (cdr old))
1155 (funcall (with-current-buffer buf major-mode))
1156 (add-log-current-defun))))
1157 (with-current-buffer buf
1158 (goto-char (+ pos (cdr src)))
1159 (add-log-current-defun))))))
1161 ;; provide the package
1162 (provide 'diff-mode)
1164 ;;; Old Change Log from when diff-mode wasn't part of Emacs:
1165 ;; Revision 1.11 1999/10/09 23:38:29 monnier
1166 ;; (diff-mode-load-hook): dropped.
1167 ;; (auto-mode-alist): also catch *.diffs.
1168 ;; (diff-find-file-name, diff-mode): add smarts to find the right file
1169 ;; for *.rej files (that lack any file name indication).
1171 ;; Revision 1.10 1999/09/30 15:32:11 monnier
1172 ;; added support for "\ No newline at end of file".
1174 ;; Revision 1.9 1999/09/15 00:01:13 monnier
1175 ;; - added basic `compile' support.
1176 ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
1177 ;; - diff-kill-file now tries to kill the leading garbage as well.
1179 ;; Revision 1.8 1999/09/13 21:10:09 monnier
1180 ;; - don't use CL in the autoloaded code
1181 ;; - accept diffs using -T
1183 ;; Revision 1.7 1999/09/05 20:53:03 monnier
1184 ;; interface to ediff-patch
1186 ;; Revision 1.6 1999/09/01 20:55:13 monnier
1187 ;; (ediff=patch-file): add bindings to call ediff-patch.
1188 ;; (diff-find-file-name): taken out of diff-goto-source.
1189 ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
1190 ;; diff-fixup-modifs): only use the region if a prefix arg is given.
1192 ;; Revision 1.5 1999/08/31 19:18:52 monnier
1193 ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
1195 ;; Revision 1.4 1999/08/31 13:01:44 monnier
1196 ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
1199 ;;; diff-mode.el ends here