From 5f082d1fe6ac28872c48095a77294a28b69deec2 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Tue, 19 Feb 2008 21:31:20 +0000 Subject: [PATCH] (diff-file-junk-re): New const. (diff-beginning-of-file-and-junk): Use it. (diff-file-kill): Make sure we were really inside a file diff. --- lisp/ChangeLog | 4 ++++ lisp/diff-mode.el | 26 +++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 5ea2b6be5d6..a6d46dc5434 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,9 @@ 2008-02-19 Stefan Monnier + * diff-mode.el (diff-file-junk-re): New const. + (diff-beginning-of-file-and-junk): Use it. + (diff-file-kill): Make sure we were really inside a file diff. + * diff-mode.el: Make it more robust in the presence of empty context lines in unified hunks. (diff-valid-unified-empty-line): New var. diff --git a/lisp/diff-mode.el b/lisp/diff-mode.el index e011ce17e1e..5a01793d06e 100644 --- a/lisp/diff-mode.el +++ b/lisp/diff-mode.el @@ -501,11 +501,19 @@ If the prefix ARG is given, restrict the view to the current file instead." (diff-end-of-hunk) (kill-region start (point))))) +(defconst diff-file-junk-re "diff \\|index ") ; "index " is output by git-diff. + (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)) + (let ((orig (point)) + ;; Skip forward over what might be "leading junk" so as to get + ;; closer to the actual diff. + (_ (progn (beginning-of-line) + (while (looking-at diff-file-junk-re) + (forward-line 1)))) + (start (point)) (file (condition-case err (progn (diff-beginning-of-file) (point)) (error err))) ;; prevhunk is one of the limits. @@ -521,20 +529,28 @@ data such as \"Index: ...\" and such." (re-search-backward "^Index: " prevhunk t)))) (when index (setq file index)) (if (<= file start) - (goto-char file) + (progn + (goto-char file) + ;; Now skip backward over the leading junk we may have before the + ;; diff itself. + (while (save-excursion + (and (zerop (forward-line -1)) + (looking-at diff-file-junk-re))) + (forward-line -1))) ;; File starts *after* the starting point: we really weren't in ;; a file diff but elsewhere. - (goto-char start) + (goto-char orig) (signal (car err) (cdr err)))))) (defun diff-file-kill () "Kill current file's hunks." (interactive) - (diff-beginning-of-file-and-junk) - (let* ((start (point)) + (let ((orig (point)) + (start (progn (diff-beginning-of-file-and-junk) (point))) (inhibit-read-only t)) (diff-end-of-file) (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs. + (if (> orig (point)) (error "Not inside a file diff")) (kill-region start (point)))) (defun diff-kill-junk () -- 2.39.2