changeset 82033:6df2ca7b3d53

(diff-beginning-of-file-and-junk): New function. (diff-file-kill): Use it. (diff-beginning-of-hunk): Add arg `try-harder' using it. (diff-restrict-view, diff-find-source-location, diff-refine-hunk): Use it so they find the hunk even when we're in the file header.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sun, 22 Jul 2007 21:26:34 +0000
parents 7d7ee463e4e9
children 11eee91b817d
files lisp/ChangeLog lisp/diff-mode.el
diffstat 2 files changed, 49 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/ChangeLog	Sun Jul 22 21:16:34 2007 +0000
+++ b/lisp/ChangeLog	Sun Jul 22 21:26:34 2007 +0000
@@ -1,3 +1,11 @@
+2007-07-22  Stefan Monnier  <monnier@iro.umontreal.ca>
+
+	* diff-mode.el (diff-beginning-of-file-and-junk): New function.
+	(diff-file-kill): Use it.
+	(diff-beginning-of-hunk): Add arg `try-harder' using it.
+	(diff-restrict-view, diff-find-source-location, diff-refine-hunk):
+	Use it so they find the hunk even when we're in the file header.
+
 2007-07-22  Dan Nicolaescu  <dann@ics.uci.edu>
 
 	* vc-git.el (vc-git-revision-granularity,vc-git-root),
@@ -28,7 +36,7 @@
 
 2007-07-22  Stefan Monnier  <monnier@iro.umontreal.ca>
 
-	* textmodes/tex-mode.el (tex-font-script-display): Change default to 0.2.
+	* textmodes/tex-mode.el (tex-font-script-display): Change default.
 
 2007-07-22  Dan Nicolaescu  <dann@ics.uci.edu>
 
--- a/lisp/diff-mode.el	Sun Jul 22 21:16:34 2007 +0000
+++ b/lisp/diff-mode.el	Sun Jul 22 21:26:34 2007 +0000
@@ -390,13 +390,20 @@
     ;; 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)
@@ -425,7 +432,7 @@
 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)))
@@ -453,18 +460,37 @@
       (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))))
@@ -1289,7 +1315,8 @@
 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),
@@ -1464,7 +1491,8 @@
 (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"))