;;; diff-mode.el --- a mode for viewing/editing context diffs
;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-;; 2005, 2006, 2007 Free Software Foundation, Inc.
+;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
;; Keywords: convenience patch diff
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
("N" . diff-file-next)
("p" . diff-hunk-prev)
("P" . diff-file-prev)
+ ("\t" . diff-hunk-next)
+ ([backtab] . diff-hunk-prev)
("k" . diff-hunk-kill)
("K" . diff-file-kill)
;; From compilation-minor-mode.
'("Diff"
["Jump to Source" diff-goto-source t]
["Apply hunk" diff-apply-hunk t]
+ ["Test applying hunk" diff-test-hunk t]
["Apply diff with Ediff" diff-ediff-patch t]
- ["-----" nil nil]
+ "-----"
["Reverse direction" diff-reverse-direction t]
["Context -> Unified" diff-context->unified t]
["Unified -> Context" diff-unified->context t]
;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
+ "-----"
+ ["Split hunk" diff-split-hunk t]
+ ["Refine hunk" diff-refine-hunk t]
+ ["Kill current hunk" diff-hunk-kill t]
+ ["Kill current file's hunks" diff-file-kill t]
+ "-----"
+ ["Previous Hunk" diff-hunk-prev t]
+ ["Next Hunk" diff-hunk-next t]
+ ["Previous File" diff-file-prev t]
+ ["Next File" diff-file-next t]
))
(defcustom diff-minor-mode-prefix "\C-c="
;; The return value is used by easy-mmode-define-navigation.
(goto-char (or end (point-max)))))
-(defun diff-beginning-of-hunk ()
+(defun diff-beginning-of-hunk (&optional try-harder)
+ "Move back to beginning of hunk.
+If TRY-HARDER is non-nil, try to cater to the case where we're not in a hunk
+but in the file header instead, in which case move forward to the first hunk."
(beginning-of-line)
(unless (looking-at diff-hunk-header-re)
(forward-line 1)
(condition-case ()
(re-search-backward diff-hunk-header-re)
- (error (error "Can't find the beginning of the hunk")))))
+ (error
+ (if (not try-harder)
+ (error "Can't find the beginning of the hunk")
+ (diff-beginning-of-file-and-junk)
+ (diff-hunk-next))))))
(defun diff-beginning-of-file ()
(beginning-of-line)
If the prefix ARG is given, restrict the view to the current file instead."
(interactive "P")
(save-excursion
- (if arg (diff-beginning-of-file) (diff-beginning-of-hunk))
+ (if arg (diff-beginning-of-file) (diff-beginning-of-hunk 'try-harder))
(narrow-to-region (point)
(progn (if arg (diff-end-of-file) (diff-end-of-hunk))
(point)))
(diff-end-of-hunk)
(kill-region start (point)))))
+(defun diff-beginning-of-file-and-junk ()
+ "Go to the beginning of file-related diff-info.
+This is like `diff-beginning-of-file' except it tries to skip back over leading
+data such as \"Index: ...\" and such."
+ (let ((start (point))
+ (file (condition-case err (progn (diff-beginning-of-file) (point))
+ (error err)))
+ ;; prevhunk is one of the limits.
+ (prevhunk (save-excursion (ignore-errors (diff-hunk-prev) (point))))
+ err)
+ (when (consp file)
+ ;; Presumably, we started before the file header, in the leading junk.
+ (setq err file)
+ (diff-file-next)
+ (setq file (point)))
+ (let ((index (save-excursion
+ (re-search-backward "^Index: " prevhunk t))))
+ (when index (setq file index))
+ (if (<= file start)
+ (goto-char file)
+ ;; File starts *after* the starting point: we really weren't in
+ ;; a file diff but elsewhere.
+ (goto-char start)
+ (signal (car err) (cdr err))))))
+
(defun diff-file-kill ()
"Kill current file's hunks."
(interactive)
- (diff-beginning-of-file)
+ (diff-beginning-of-file-and-junk)
(let* ((start (point))
- (prevhunk (save-excursion
- (ignore-errors
- (diff-hunk-prev) (point))))
- (index (save-excursion
- (re-search-backward "^Index: " prevhunk t)))
(inhibit-read-only t))
- (when index (setq start index))
(diff-end-of-file)
(if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
(kill-region start (point))))
((or (null files)
(setq file (do* ((files files (cdr files))
(file (car files) (car files)))
- ((or (null file) (file-exists-p file))
+ ;; Use file-regular-p to avoid
+ ;; /dev/null, directories, etc.
+ ((or (null file) (file-regular-p file))
file))))
file))
;; <foo>.rej patches implicitly apply to <foo>
(defun diff-unified->context (start end)
"Convert unified diffs to context diffs.
START and END are either taken from the region (if a prefix arg is given) or
-else cover the whole bufer."
+else cover the whole buffer."
(interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(defun diff-reverse-direction (start end)
"Reverse the direction of the diffs.
START and END are either taken from the region (if a prefix arg is given) or
-else cover the whole bufer."
+else cover the whole buffer."
(interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(defun diff-fixup-modifs (start end)
"Fixup the hunk headers (in case the buffer was modified).
START and END are either taken from the region (if a prefix arg is given) or
-else cover the whole bufer."
+else cover the whole buffer."
(interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(define-derived-mode diff-mode fundamental-mode "Diff"
"Major mode for viewing/editing context diffs.
Supports unified and context diffs as well as (to a lesser extent)
-normal diffs. If you edit the buffer manually, diff-mode will try
-to update the hunk headers for you on-the-fly.
+normal diffs.
When the buffer is read-only, the ESC prefix is not necessary.
If you edit the buffer manually, diff-mode will try to update the hunk
or vice versa with \\[diff-unified->context] and you can also reverse the direction of
a diff with \\[diff-reverse-direction].
-When the buffer is read-only, the Meta- modifier is not necessary
-to run the Diff mode commands:
-
\\{diff-mode-map}"
(set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
(while
(case (char-after)
(?\s (decf before) (decf after) t)
- (?- (decf before) t)
+ (?-
+ (if (and (looking-at diff-file-header-re)
+ (zerop before) (zerop after))
+ ;; No need to query: this is a case where two patches
+ ;; are concatenated and only counting the lines will
+ ;; give the right result. Let's just add an empty
+ ;; line so that our code which doesn't count lines
+ ;; will not get confused.
+ (progn (save-excursion (insert "\n")) nil)
+ (decf before) t))
(?+ (decf after) t)
(t
(cond
SWITCHED is non-nil if the patch is already applied."
(save-excursion
(let* ((other (diff-xor other-file diff-jump-to-old-file))
- (char-offset (- (point) (progn (diff-beginning-of-hunk) (point))))
+ (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
+ (point))))
;; Check that the hunk is well-formed. Otherwise diff-mode and
;; the user may disagree on what constitutes the hunk
;; (e.g. because an empty line truncates the hunk mid-course),
(defun diff-refine-hunk ()
"Refine the current hunk by ignoring space differences."
(interactive)
- (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk) (point))))
+ (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
+ (point))))
(opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b")))
(line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
(error "Can't find line number"))