;;; diff-mode.el --- a mode for viewing/editing context diffs
-;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,2005, 2006,
+;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
;; Keywords: convenience patch diff
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; 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 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; - Improve `diff-add-change-log-entries-other-window',
;; it is very simplistic now.
-;;
+;;
;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
;; Also allow C-c C-a to delete already-applied hunks.
;;
:group 'diff-mode)
(defcustom diff-update-on-the-fly t
- "*Non-nil means hunk headers are kept up-to-date on-the-fly.
+ "Non-nil means hunk headers are kept up-to-date on-the-fly.
When editing a diff file, the line numbers in the hunk headers
need to be kept consistent with the actual diff. This can
either be done on the fly (but this sometimes interacts poorly with the
:group 'diff-mode)
(defcustom diff-advance-after-apply-hunk t
- "*Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
- :type 'boolean
- :group 'diff-mode)
-
-(defcustom diff-auto-refine t
- "Automatically highlight changes in detail as the user visits hunks."
+ "Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
:type 'boolean
:group 'diff-mode)
(easy-menu-define diff-mode-menu diff-mode-map
"Menu for `diff-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]
+ ["Jump to Source" diff-goto-source
+ :help "Jump to the corresponding source line"]
+ ["Apply hunk" diff-apply-hunk
+ :help "Apply the current hunk to the source file and go to the next"]
+ ["Test applying hunk" diff-test-hunk
+ :help "See whether it's possible to apply the current hunk"]
+ ["Apply diff with Ediff" diff-ediff-patch
+ :help "Call `ediff-patch-file' on the current buffer"]
["Create Change Log entries" diff-add-change-log-entries-other-window
:help "Create ChangeLog entries for the changes in the diff buffer"]
"-----"
- ["Reverse direction" diff-reverse-direction t]
- ["Context -> Unified" diff-context->unified t]
- ["Unified -> Context" diff-unified->context t]
+ ["Reverse direction" diff-reverse-direction
+ :help "Reverse the direction of the diffs"]
+ ["Context -> Unified" diff-context->unified
+ :help "Convert context diffs to unified diffs"]
+ ["Unified -> Context" diff-unified->context
+ :help "Convert unified diffs to context diffs"]
;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
+ ["Show trailing whitespace" whitespace-mode
+ :style toggle :selected (bound-and-true-p whitespace-mode)
+ :help "Show trailing whitespace in modified lines"]
"-----"
- ["Split hunk" diff-split-hunk (diff-splittable-p)]
- ["Ignore whitespace changes" diff-ignore-whitespace-hunk t]
- ["Highlight fine changes" diff-refine-hunk t]
- ["Kill current hunk" diff-hunk-kill t]
- ["Kill current file's hunks" diff-file-kill t]
+ ["Split hunk" diff-split-hunk
+ :active (diff-splittable-p)
+ :help "Split the current (unified diff) hunk at point into two hunks"]
+ ["Ignore whitespace changes" diff-ignore-whitespace-hunk
+ :help "Re-diff the current hunk, ignoring whitespace differences"]
+ ["Highlight fine changes" diff-refine-hunk
+ :help "Highlight changes of hunk at point at a finer granularity"]
+ ["Kill current hunk" diff-hunk-kill
+ :help "Kill current hunk"]
+ ["Kill current file's hunks" diff-file-kill
+ :help "Kill all current file's hunks"]
"-----"
- ["Previous Hunk" diff-hunk-prev t]
- ["Next Hunk" diff-hunk-next t]
- ["Previous File" diff-file-prev t]
- ["Next File" diff-file-next t]
+ ["Previous Hunk" diff-hunk-prev
+ :help "Go to the previous count'th hunk"]
+ ["Next Hunk" diff-hunk-next
+ :help "Go to the next count'th hunk"]
+ ["Previous File" diff-file-prev
+ :help "Go to the previous count'th file"]
+ ["Next File" diff-file-next
+ :help "Go to the next count'th file"]
))
(defcustom diff-minor-mode-prefix "\C-c="
`((,diff-minor-mode-prefix . ,diff-mode-shared-map))
"Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
+(define-minor-mode diff-auto-refine-mode
+ "Automatically highlight changes in detail as the user visits hunks.
+When transitioning from disabled to enabled,
+try to refine the current hunk, as well."
+ :group 'diff-mode :init-value t :lighter nil ;; " Auto-Refine"
+ (when diff-auto-refine-mode
+ (condition-case-no-debug nil (diff-refine-hunk) (error nil))))
;;;;
;;;; font-lock support
(defface diff-header
'((((class color) (min-colors 88) (background light))
- :background "grey85")
+ :background "grey80")
(((class color) (min-colors 88) (background dark))
:background "grey45")
(((class color) (background light))
(t :weight bold))
"`diff-mode' face inherited by hunk and index header faces."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-header-face 'face-alias 'diff-header)
+(define-obsolete-face-alias 'diff-header-face 'diff-header "22.1")
(defvar diff-header-face 'diff-header)
(defface diff-file-header
(t :weight bold)) ; :height 1.3
"`diff-mode' face used to highlight file header lines."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-file-header-face 'face-alias 'diff-file-header)
+(define-obsolete-face-alias 'diff-file-header-face 'diff-file-header "22.1")
(defvar diff-file-header-face 'diff-file-header)
(defface diff-index
'((t :inherit diff-file-header))
"`diff-mode' face used to highlight index header lines."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-index-face 'face-alias 'diff-index)
+(define-obsolete-face-alias 'diff-index-face 'diff-index "22.1")
(defvar diff-index-face 'diff-index)
(defface diff-hunk-header
'((t :inherit diff-header))
"`diff-mode' face used to highlight hunk header lines."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-hunk-header-face 'face-alias 'diff-hunk-header)
+(define-obsolete-face-alias 'diff-hunk-header-face 'diff-hunk-header "22.1")
(defvar diff-hunk-header-face 'diff-hunk-header)
(defface diff-removed
'((t :inherit diff-changed))
"`diff-mode' face used to highlight removed lines."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-removed-face 'face-alias 'diff-removed)
+(define-obsolete-face-alias 'diff-removed-face 'diff-removed "22.1")
(defvar diff-removed-face 'diff-removed)
(defface diff-added
'((t :inherit diff-changed))
"`diff-mode' face used to highlight added lines."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-added-face 'face-alias 'diff-added)
+(define-obsolete-face-alias 'diff-added-face 'diff-added "22.1")
(defvar diff-added-face 'diff-added)
(defface diff-changed
:foreground "yellow" :weight bold :slant italic))
"`diff-mode' face used to highlight changed lines."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-changed-face 'face-alias 'diff-changed)
+(define-obsolete-face-alias 'diff-changed-face 'diff-changed "22.1")
(defvar diff-changed-face 'diff-changed)
(defface diff-indicator-removed
'((t :inherit diff-header))
"`diff-mode' face used to highlight function names produced by \"diff -p\"."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-function-face 'face-alias 'diff-function)
+(define-obsolete-face-alias 'diff-function-face 'diff-function "22.1")
(defvar diff-function-face 'diff-function)
(defface diff-context
'((((class color grayscale) (min-colors 88)) :inherit shadow))
"`diff-mode' face used to highlight context and other side-information."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-context-face 'face-alias 'diff-context)
+(define-obsolete-face-alias 'diff-context-face 'diff-context "22.1")
(defvar diff-context-face 'diff-context)
(defface diff-nonexistent
'((t :inherit diff-file-header))
"`diff-mode' face used to highlight nonexistent files in recursive diffs."
:group 'diff-mode)
-;; backward-compatibility alias
-(put 'diff-nonexistent-face 'face-alias 'diff-nonexistent)
+(define-obsolete-face-alias 'diff-nonexistent-face 'diff-nonexistent "22.1")
(defvar diff-nonexistent-face 'diff-nonexistent)
(defconst diff-yank-handler '(diff-yank-function))
(replace-match "" t t)))))))
(defconst diff-hunk-header-re-unified
- "^@@ -\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\) @@")
+ "^@@ -\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? @@")
+(defconst diff-context-mid-hunk-header-re
+ "--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$")
(defvar diff-font-lock-keywords
`((,(concat "\\(" diff-hunk-header-re-unified "\\)\\(.*\\)$")
("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
(1 diff-hunk-header-face) (2 diff-function-face))
("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context
- ("^--- .+ ----$" . diff-hunk-header-face) ;context
+ (,diff-context-mid-hunk-header-re . diff-hunk-header-face) ;context
("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face) ;normal
("^---$" . diff-hunk-header-face) ;normal
;; For file headers, accept files with spaces, but be careful to rule
(defconst diff-hunk-header-re
(concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$"))
-(defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* ]\\).+\n" (substring diff-hunk-header-re 1)))
+(defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* \n]\\).+\n" (substring diff-hunk-header-re 1)))
(defvar diff-narrowed-to nil)
(defun diff-hunk-style (&optional style)
(setq style (diff-hunk-style style))
(goto-char (match-end 0))
(when (and (not donttrustheader) (match-end 2))
+ (let* ((nold (string-to-number (or (match-string 2) "1")))
+ (nnew (string-to-number (or (match-string 4) "1")))
+ (endold
(save-excursion
(re-search-forward (if diff-valid-unified-empty-line
"^[- \n]" "^[- ]")
- nil t
- (string-to-number (match-string 2)))
- (setq end (line-beginning-position 2)))))
+ nil t nold)
+ (line-beginning-position 2)))
+ (endnew
+ ;; The hunk may end with a bunch of "+" lines, so the `end' is
+ ;; then further than computed above.
+ (save-excursion
+ (re-search-forward (if diff-valid-unified-empty-line
+ "^[+ \n]" "^[+ ]")
+ nil t nnew)
+ (line-beginning-position 2))))
+ (setq end (max endold endnew)))))
;; We may have a first evaluation of `end' thanks to the hunk header.
(unless end
(setq end (and (re-search-forward
res
(goto-char start)
(error "Can't find the beginning of the file")))))
-
+
(defun diff-end-of-file ()
(re-search-forward "^[-+#!<>0-9@* \\]" nil t)
;; Define diff-{hunk,file}-{prev,next}
(easy-mmode-define-navigation
diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view
- (if diff-auto-refine
+ (if diff-auto-refine-mode
(condition-case-no-debug nil (diff-refine-hunk) (error nil))))
(easy-mmode-define-navigation
(interactive)
(diff-beginning-of-hunk)
(let* ((start (point))
- (nexthunk (when (re-search-forward diff-hunk-header-re nil t)
+ ;; Search the second match, since we're looking at the first.
+ (nexthunk (when (re-search-forward diff-hunk-header-re nil t 2)
(match-beginning 0)))
(firsthunk (ignore-errors
(goto-char start)
(diff-end-of-hunk)
(kill-region start (point)))))
-(defconst diff-file-junk-re "diff \\|index ") ; "index " is output by git-diff.
+;; "index ", "old mode", "new mode", "new file mode" and
+;; "deleted file mode" are output by git-diff.
+(defconst diff-file-junk-re
+ "diff \\|index \\|\\(?:deleted file\\|new\\(?: file\\)?\\|old\\) mode")
(defun diff-beginning-of-file-and-junk ()
"Go to the beginning of file-related diff-info.
;; a file diff but elsewhere.
(goto-char orig)
(signal (car err) (cdr err)))))
-
+
(defun diff-file-kill ()
"Kill current file's hunks."
(interactive)
;;;;
(defvar diff-remembered-files-alist nil)
+(defvar diff-remembered-defdir nil)
(defun diff-filename-drop-dir (file)
(when (string-match "/" file) (substring file (match-end 0))))
(fs (diff-hunk-file-names current-prefix-arg)))
(unless fs (error "No file name to look for"))
(list old (read-file-name (format "File for %s: " (car fs))
- nil (diff-find-file-name old) t))))
+ nil (diff-find-file-name old 'noprompt) t))))
(let ((fs (diff-hunk-file-names old)))
(unless fs (error "No file name to look for"))
(push (cons fs name) diff-remembered-files-alist)))
(list (if old (match-string 2) (match-string 4))
(if old (match-string 4) (match-string 2)))))))))
-(defun diff-find-file-name (&optional old batch prefix)
+(defun diff-find-file-name (&optional old noprompt prefix)
"Return the file corresponding to the current patch.
Non-nil OLD means that we want the old file.
-Non-nil BATCH means to prefer returning an incorrect answer than to prompt
-the user.
+Non-nil NOPROMPT means to prefer returning nil than to prompt the user.
PREFIX is only used internally: don't use it."
+ (unless (equal diff-remembered-defdir default-directory)
+ ;; Flush diff-remembered-files-alist if the default-directory is changed.
+ (set (make-local-variable 'diff-remembered-defdir) default-directory)
+ (set (make-local-variable 'diff-remembered-files-alist) nil))
(save-excursion
(unless (looking-at diff-file-header-re)
(or (ignore-errors (diff-beginning-of-file))
(boundp 'cvs-pcl-cvs-dirchange-re)
(save-excursion
(re-search-backward cvs-pcl-cvs-dirchange-re nil t))
- (diff-find-file-name old batch (match-string 1)))
- ;; Invent something, if necessary.
- (when batch
- (or (car fs) default-directory))
+ (diff-find-file-name old noprompt (match-string 1)))
;; if all else fails, ask the user
- (let ((file (read-file-name (format "Use file %s: " (or (first fs) ""))
- nil (first fs) t (first fs))))
- (set (make-local-variable 'diff-remembered-files-alist)
- (cons (cons fs file) diff-remembered-files-alist))
- file)))))
+ (unless noprompt
+ (let ((file (read-file-name (format "Use file %s: "
+ (or (first fs) ""))
+ nil (first fs) t (first fs))))
+ (set (make-local-variable 'diff-remembered-files-alist)
+ (cons (cons fs file) diff-remembered-files-alist))
+ file))))))
(defun diff-ediff-patch ()
(replace-match "***" t t nil 2))
;; we matched a hunk header
(let ((line1 (match-string 4))
- (lines1 (if (match-end 5)
- (string-to-number (match-string 5)) 1))
+ (lines1 (or (match-string 5) "1"))
(line2 (match-string 6))
- (lines2 (if (match-end 7)
- (string-to-number (match-string 7)) 1))
+ (lines2 (or (match-string 7) "1"))
;; Variables to use the special undo function.
(old-undo buffer-undo-list)
(old-end (marker-position end))
(replace-match
(concat "***************\n*** " line1 ","
(number-to-string (+ (string-to-number line1)
- lines1 -1)) " ****"))
+ (string-to-number lines1)
+ -1))
+ " ****"))
(save-restriction
(narrow-to-region (line-beginning-position 2)
;; Call diff-end-of-hunk from just before
(save-excursion
(insert "--- " line2 ","
(number-to-string (+ (string-to-number line2)
- lines2 -1))
+ (string-to-number lines2)
+ -1))
" ----\n" hunk))
;;(goto-char (point-min))
(forward-line 1)
(reversible t))
(replace-match "")
(unless (re-search-forward
- "^--- \\([0-9]+\\),\\(-?[0-9]+\\) ----$" nil t)
+ diff-context-mid-hunk-header-re nil t)
(error "Can't find matching `--- n1,n2 ----' line"))
(let ((line2s (match-string 1))
(line2e (match-string 2))
(when (= (char-after) ?-) (delete-char 1) (insert "+"))
(forward-line 1))
(let ((half1 (delete-and-extract-region half1s (point))))
- (unless (looking-at "^--- \\([0-9]+,-?[0-9]+\\) ----$")
+ (unless (looking-at diff-context-mid-hunk-header-re)
(insert half1)
(error "Can't find matching `--- n1,n2 ----' line"))
- (let ((str1 (match-string 1)))
- (replace-match lines1 nil nil nil 1)
+ (let* ((str1end (or (match-end 2) (match-end 1)))
+ (str1 (buffer-substring (match-beginning 1) str1end)))
+ (goto-char str1end)
+ (insert lines1)
+ (delete-region (match-beginning 1) str1end)
(forward-line 1)
(let ((half2s (point)))
(while (looking-at "[!+ \\][ \t]\\|#")
(if old1
(unless (string= new1 old1) (replace-match new1 t t nil 2))
(goto-char (match-end 2)) (insert "," new1))))
- ((looking-at "--- \\([0-9]+\\),\\([0-9]*\\) ----$")
+ ((looking-at diff-context-mid-hunk-header-re)
(when (> (+ space bang plus) 0)
(let* ((old1 (match-string 1))
(old2 (match-string 2))
(goto-char (car diff-unhandled-changes))
;; Maybe we've cut the end of the hunk before point.
(if (and (bolp) (not (bobp))) (backward-char 1))
- ;; We used to fixup modifs on all the changes, but it turns out
- ;; that it's safer not to do it on big changes, for example
- ;; when yanking a big diff, since we might then screw up perfectly
- ;; correct values. -stef
- ;; (unless (ignore-errors
- ;; (diff-beginning-of-hunk)
- ;; (save-excursion
- ;; (diff-end-of-hunk)
- ;; (> (point) (car diff-unhandled-changes))))
- ;; (goto-char (car diff-unhandled-changes))
- ;; (re-search-forward diff-hunk-header-re (cdr diff-unhandled-changes))
- ;; (diff-beginning-of-hunk))
- ;; (diff-fixup-modifs (point) (cdr diff-unhandled-changes))
+ ;; We used to fixup modifs on all the changes, but it turns out that
+ ;; it's safer not to do it on big changes, e.g. when yanking a big
+ ;; diff, or when the user edits the header, since we might then
+ ;; screw up perfectly correct values. --Stef
(diff-beginning-of-hunk)
- (when (save-excursion
+ (let* ((style (if (looking-at "\\*\\*\\*") 'context))
+ (start (line-beginning-position (if (eq style 'context) 3 2)))
+ (mid (if (eq style 'context)
+ (save-excursion
+ (re-search-forward diff-context-mid-hunk-header-re
+ nil t)))))
+ (when (and ;; Don't try to fixup changes in the hunk header.
+ (> (car diff-unhandled-changes) start)
+ ;; Don't try to fixup changes in the mid-hunk header either.
+ (or (not mid)
+ (< (cdr diff-unhandled-changes) (match-beginning 0))
+ (> (car diff-unhandled-changes) (match-end 0)))
+ (save-excursion
(diff-end-of-hunk nil 'donttrustheader)
- (>= (point) (cdr diff-unhandled-changes)))
+ ;; Don't try to fixup changes past the end of the hunk.
+ (>= (point) (cdr diff-unhandled-changes))))
(diff-fixup-modifs (point) (cdr diff-unhandled-changes)))))
- (setq diff-unhandled-changes nil)))
+ (setq diff-unhandled-changes nil))))
(defun diff-next-error (arg reset)
;; Select a window that displays the current buffer so that point
(diff-hunk-next arg)
(diff-goto-source))
+(defvar whitespace-style)
+(defvar whitespace-trailing-regexp)
+
;;;###autoload
(define-derived-mode diff-mode fundamental-mode "Diff"
"Major mode for viewing/editing context diffs.
;; compile support
(set (make-local-variable 'next-error-function) 'diff-next-error)
+ (set (make-local-variable 'beginning-of-defun-function)
+ 'diff-beginning-of-file-and-junk)
+ (set (make-local-variable 'end-of-defun-function)
+ 'diff-end-of-file)
+
+ ;; Set up `whitespace-mode' so that turning it on will show trailing
+ ;; whitespace problems on the modified lines of the diff.
+ (set (make-local-variable 'whitespace-style) '(trailing))
+ (set (make-local-variable 'whitespace-trailing-regexp)
+ "^[-\+!<>].*?\\([\t ]+\\)$")
+
(setq buffer-read-only diff-default-read-only)
;; setup change hooks
(if (not diff-update-on-the-fly)
(set (make-local-variable 'add-log-current-defun-function)
'diff-current-defun)
(set (make-local-variable 'add-log-buffer-file-name-function)
- 'diff-find-file-name))
+ (lambda () (diff-find-file-name nil 'noprompt))))
;;;###autoload
(define-minor-mode diff-minor-mode
(1+ (- (string-to-number (match-string 2))
(string-to-number (match-string 1))))
1))
- (if (not (looking-at "--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$"))
+ (if (not (looking-at diff-context-mid-hunk-header-re))
(error "Unrecognized context diff second hunk header format")
(forward-line)
(diff-sanity-check-context-hunk-half
((eq (char-after) ?@)
(if (not (looking-at diff-hunk-header-re-unified))
(error "Unrecognized unified diff hunk header format")
- (let ((before (if (match-end 2)
- (string-to-number (match-string 2)) 1))
- (after (if (match-end 4)
- (string-to-number (match-string 4)) 1)))
+ (let ((before (string-to-number (or (match-string 2) "1")))
+ (after (string-to-number (or (match-string 4) "1"))))
(forward-line)
(while
(case (char-after)
(cond
((and diff-valid-unified-empty-line
;; Not just (eolp) so we don't infloop at eob.
- (eq (char-after) ?\n))
+ (eq (char-after) ?\n)
+ (> before 0) (> after 0))
(decf before) (decf after) t)
((and (zerop before) (zerop after)) nil)
((or (< before 0) (< after 0))
;; context diff
(forward-line 2)
(setq src-pos (point))
- (re-search-forward "^--- " nil t)
+ (re-search-forward diff-context-mid-hunk-header-re nil t)
(forward-line 0)
(setq divider-pos (point))
(forward-line 1)
(defsubst diff-xor (a b) (if a (if (not b) a) b))
-(defun diff-find-source-location (&optional other-file reverse)
+(defun diff-find-source-location (&optional other-file reverse noprompt)
"Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
BUF is the buffer corresponding to the source file.
LINE-OFFSET is the offset between the expected and actual positions
POS is a pair (BEG . END) indicating the position of the text in the buffer.
SRC and DST are the two variants of text as returned by `diff-hunk-text'.
SRC is the variant that was found in the buffer.
-SWITCHED is non-nil if the patch is already applied."
+SWITCHED is non-nil if the patch is already applied.
+NOPROMPT, if non-nil, means not to prompt the user."
(save-excursion
(let* ((other (diff-xor other-file diff-jump-to-old-file))
(char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
;; the user may disagree on what constitutes the hunk
;; (e.g. because an empty line truncates the hunk mid-course),
;; leading to potentially nasty surprises for the user.
- (_ (diff-sanity-check-hunk))
- (hunk (buffer-substring (point)
- (save-excursion (diff-end-of-hunk) (point))))
+ ;;
+ ;; Suppress check when NOPROMPT is non-nil (Bug#3033).
+ (_ (unless noprompt (diff-sanity-check-hunk)))
+ (hunk (buffer-substring
+ (point) (save-excursion (diff-end-of-hunk) (point))))
(old (diff-hunk-text hunk reverse char-offset))
(new (diff-hunk-text hunk (not reverse) char-offset))
;; Find the location specification.
(error "Can't find the hunk header")
(if other (match-string 1)
(if (match-end 3) (match-string 3)
- (unless (re-search-forward "^--- \\([0-9,]+\\)" nil t)
+ (unless (re-search-forward
+ diff-context-mid-hunk-header-re nil t)
(error "Can't find the hunk separator"))
(match-string 1)))))
- (file (or (diff-find-file-name other) (error "Can't find the file")))
+ (file (or (diff-find-file-name other noprompt)
+ (error "Can't find the file")))
(buf (find-file-noselect file)))
;; Update the user preference if he so wished.
(when (> (prefix-numeric-value other-file) 8)
(setq diff-jump-to-old-file other))
(with-current-buffer buf
- (goto-line (string-to-number line))
+ (goto-char (point-min)) (forward-line (1- (string-to-number line)))
(let* ((orig-pos (point))
(switched nil)
;; FIXME: Check for case where both OLD and NEW are found.
;; for diff-goto-source, and is thus confusing. Also when you don't
;; know about it it's pretty surprising.
;; TODO: make it possible to ask explicitly for this behavior.
- ;;
+ ;;
;; This is duplicated in diff-test-hunk.
(diff-find-source-location nil reverse)
(cond
(when (looking-at diff-hunk-header-re)
(forward-line 1)
(re-search-forward "^[^ ]" nil t))
- (destructuring-bind (buf line-offset pos src dst &optional switched)
- (diff-find-source-location)
- (beginning-of-line)
- (or (when (memq (char-after) '(?< ?-))
- ;; Cursor is pointing at removed text. This could be a removed
- ;; function, in which case, going to the source buffer will
- ;; not help since the function is now removed. Instead,
- ;; try to figure out the function name just from the code-fragment.
- (let ((old (if switched dst src)))
- (with-temp-buffer
- (insert (car old))
- (funcall (with-current-buffer buf major-mode))
- (goto-char (+ (point-min) (cdr old)))
- (add-log-current-defun))))
- (with-current-buffer buf
- (goto-char (+ (car pos) (cdr src)))
- (add-log-current-defun))))))
+ (destructuring-bind (&optional buf line-offset pos src dst switched)
+ ;; Use `noprompt' since this is used in which-func-mode and such.
+ (ignore-errors ;Signals errors in place of prompting.
+ (diff-find-source-location nil nil 'noprompt))
+ (when buf
+ (beginning-of-line)
+ (or (when (memq (char-after) '(?< ?-))
+ ;; Cursor is pointing at removed text. This could be a removed
+ ;; function, in which case, going to the source buffer will
+ ;; not help since the function is now removed. Instead,
+ ;; try to figure out the function name just from the
+ ;; code-fragment.
+ (let ((old (if switched dst src)))
+ (with-temp-buffer
+ (insert (car old))
+ (funcall (buffer-local-value 'major-mode buf))
+ (goto-char (+ (point-min) (cdr old)))
+ (add-log-current-defun))))
+ (with-current-buffer buf
+ (goto-char (+ (car pos) (cdr src)))
+ (add-log-current-defun)))))))
(defun diff-ignore-whitespace-hunk ()
"Re-diff the current hunk, ignoring whitespace differences."
(line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
(error "Can't find line number"))
(string-to-number (match-string 1))))
+ (inhibit-read-only t)
(hunk (delete-and-extract-region
(point) (save-excursion (diff-end-of-hunk) (point))))
(lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
(file1 (make-temp-file "diff1"))
(file2 (make-temp-file "diff2"))
(coding-system-for-read buffer-file-coding-system)
- (inhibit-read-only t)
old new)
(unwind-protect
(save-excursion
(defface diff-refine-change
'((((class color) (min-colors 88) (background light))
- :background "grey90")
+ :background "grey85")
(((class color) (min-colors 88) (background dark))
- :background "grey40")
+ :background "grey60")
(((class color) (background light))
:background "yellow")
(((class color) (background dark))
"\\( .*\n\\)*[+]\\)?")
nil t))
(save-excursion
- (add-change-log-entry nil nil t t)))
+ ;; FIXME: this pops up windows of all the buffers.
+ (add-change-log-entry nil nil t nil t)))
;; When there's no more hunks, diff-hunk-next signals an error.
(error nil)))))