aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/diff-mode.el
diff options
context:
space:
mode:
authorStefan Monnier2007-07-22 21:26:34 +0000
committerStefan Monnier2007-07-22 21:26:34 +0000
commitfd69179929b3a9210ce9f4a49dfcacfe1b6d552e (patch)
tree5c97bce0b1f66548b27b5d523716b890f9530e5b /lisp/diff-mode.el
parent53cc90abac468db36ec45f7f67c4389f8b134165 (diff)
downloademacs-fd69179929b3a9210ce9f4a49dfcacfe1b6d552e.tar.gz
emacs-fd69179929b3a9210ce9f4a49dfcacfe1b6d552e.zip
(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.
Diffstat (limited to 'lisp/diff-mode.el')
-rw-r--r--lisp/diff-mode.el52
1 files changed, 40 insertions, 12 deletions
diff --git a/lisp/diff-mode.el b/lisp/diff-mode.el
index 64199147c21..358df76ba2f 100644
--- a/lisp/diff-mode.el
+++ b/lisp/diff-mode.el
@@ -390,13 +390,20 @@ when editing big diffs)."
390 ;; The return value is used by easy-mmode-define-navigation. 390 ;; The return value is used by easy-mmode-define-navigation.
391 (goto-char (or end (point-max))))) 391 (goto-char (or end (point-max)))))
392 392
393(defun diff-beginning-of-hunk () 393(defun diff-beginning-of-hunk (&optional try-harder)
394 "Move back to beginning of hunk.
395If TRY-HARDER is non-nil, try to cater to the case where we're not in a hunk
396but in the file header instead, in which case move forward to the first hunk."
394 (beginning-of-line) 397 (beginning-of-line)
395 (unless (looking-at diff-hunk-header-re) 398 (unless (looking-at diff-hunk-header-re)
396 (forward-line 1) 399 (forward-line 1)
397 (condition-case () 400 (condition-case ()
398 (re-search-backward diff-hunk-header-re) 401 (re-search-backward diff-hunk-header-re)
399 (error (error "Can't find the beginning of the hunk"))))) 402 (error
403 (if (not try-harder)
404 (error "Can't find the beginning of the hunk")
405 (diff-beginning-of-file-and-junk)
406 (diff-hunk-next))))))
400 407
401(defun diff-beginning-of-file () 408(defun diff-beginning-of-file ()
402 (beginning-of-line) 409 (beginning-of-line)
@@ -425,7 +432,7 @@ when editing big diffs)."
425If the prefix ARG is given, restrict the view to the current file instead." 432If the prefix ARG is given, restrict the view to the current file instead."
426 (interactive "P") 433 (interactive "P")
427 (save-excursion 434 (save-excursion
428 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk)) 435 (if arg (diff-beginning-of-file) (diff-beginning-of-hunk 'try-harder))
429 (narrow-to-region (point) 436 (narrow-to-region (point)
430 (progn (if arg (diff-end-of-file) (diff-end-of-hunk)) 437 (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
431 (point))) 438 (point)))
@@ -453,18 +460,37 @@ If the prefix ARG is given, restrict the view to the current file instead."
453 (diff-end-of-hunk) 460 (diff-end-of-hunk)
454 (kill-region start (point))))) 461 (kill-region start (point)))))
455 462
463(defun diff-beginning-of-file-and-junk ()
464 "Go to the beginning of file-related diff-info.
465This is like `diff-beginning-of-file' except it tries to skip back over leading
466data such as \"Index: ...\" and such."
467 (let ((start (point))
468 (file (condition-case err (progn (diff-beginning-of-file) (point))
469 (error err)))
470 ;; prevhunk is one of the limits.
471 (prevhunk (save-excursion (ignore-errors (diff-hunk-prev) (point))))
472 err)
473 (when (consp file)
474 ;; Presumably, we started before the file header, in the leading junk.
475 (setq err file)
476 (diff-file-next)
477 (setq file (point)))
478 (let ((index (save-excursion
479 (re-search-backward "^Index: " prevhunk t))))
480 (when index (setq file index))
481 (if (<= file start)
482 (goto-char file)
483 ;; File starts *after* the starting point: we really weren't in
484 ;; a file diff but elsewhere.
485 (goto-char start)
486 (signal (car err) (cdr err))))))
487
456(defun diff-file-kill () 488(defun diff-file-kill ()
457 "Kill current file's hunks." 489 "Kill current file's hunks."
458 (interactive) 490 (interactive)
459 (diff-beginning-of-file) 491 (diff-beginning-of-file-and-junk)
460 (let* ((start (point)) 492 (let* ((start (point))
461 (prevhunk (save-excursion
462 (ignore-errors
463 (diff-hunk-prev) (point))))
464 (index (save-excursion
465 (re-search-backward "^Index: " prevhunk t)))
466 (inhibit-read-only t)) 493 (inhibit-read-only t))
467 (when index (setq start index))
468 (diff-end-of-file) 494 (diff-end-of-file)
469 (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs. 495 (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
470 (kill-region start (point)))) 496 (kill-region start (point))))
@@ -1289,7 +1315,8 @@ SRC and DST are the two variants of text as returned by `diff-hunk-text'.
1289SWITCHED is non-nil if the patch is already applied." 1315SWITCHED is non-nil if the patch is already applied."
1290 (save-excursion 1316 (save-excursion
1291 (let* ((other (diff-xor other-file diff-jump-to-old-file)) 1317 (let* ((other (diff-xor other-file diff-jump-to-old-file))
1292 (char-offset (- (point) (progn (diff-beginning-of-hunk) (point)))) 1318 (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1319 (point))))
1293 ;; Check that the hunk is well-formed. Otherwise diff-mode and 1320 ;; Check that the hunk is well-formed. Otherwise diff-mode and
1294 ;; the user may disagree on what constitutes the hunk 1321 ;; the user may disagree on what constitutes the hunk
1295 ;; (e.g. because an empty line truncates the hunk mid-course), 1322 ;; (e.g. because an empty line truncates the hunk mid-course),
@@ -1464,7 +1491,8 @@ For use in `add-log-current-defun-function'."
1464(defun diff-refine-hunk () 1491(defun diff-refine-hunk ()
1465 "Refine the current hunk by ignoring space differences." 1492 "Refine the current hunk by ignoring space differences."
1466 (interactive) 1493 (interactive)
1467 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk) (point)))) 1494 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
1495 (point))))
1468 (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b"))) 1496 (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b")))
1469 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)") 1497 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
1470 (error "Can't find line number")) 1498 (error "Can't find line number"))