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