Commit 2c8a7e50 authored by Dima Kogan's avatar Dima Kogan Committed by Noam Postavsky
Browse files

Improve diff-mode navigation/manipulation

This is Bug #17544.

Navigation and use of diff buffers had several annoying corner cases
that this patch fixes.  These corner cases were largely due to
inconsistent treatment of file headers.  Say you have a diff such as

 --- aaa
 +++ bbb
 @@ -52,7 +52,7 @@
 @@ -74,7 +74,7 @@
 --- ccc
 +++ ddd
 @@ -608,6 +608,6 @@
 @@ -654,7 +654,7 @@

The file headers here are the '---' and '+++' lines.  With the point on
such a line, hunk operations would sometimes refer to the next hunk and
sometimes to the previous hunk.  Most of the time it would be the
previous hunk, which is not what the user would expect.  This patch
consistently treats such headers as the next hunk.  So with this patch,
if the point is on the '--- ccc' line, the point is seen as referring to

Specific behaviors this fixes are:

1. It should be possible to place the point in the middle of a diff
buffer, and press M-k repeatedly to kill hunks in the order they appear
in the buffer.  With the point on hunk1, M-k M-k would kill hunk1 then
hunk2.  With the point on hunk3, it would kill hunk3 then hunk4; this is
fine.  However, with the point on hunk2, it'd kill hunk2 then hunk1.
This is fixed by this patch.

2. Similarly, it should be possible to apply hunks in order.  Previously
with the point at the start, C-c C-a would apply the hunk1, then move
the point to the first @@ header, and thus C-c C-a would try to apply
the same hunk again.

* lisp/vc/diff-mode.el (diff--wrap-navigation): New function to add better
navigation logic to diff-{hunk,file}-{next,prev}.
(diff-hunk-next, diff-hunk-prev):
(diff-file-next, diff-file-prev): Better navigation logic if
skip-hunk-start is true, which happens when called interactively.
(diff-bounds-of-hunk, diff-find-source-location):
(diff-apply-hunk, diff-current-defun, diff-refine-hunk): Small tweaks to
improve hunk navigation.
parent 753c565d
......@@ -551,7 +551,7 @@ next hunk if TRY-HARDER is non-nil; otherwise signal an error."
;; Define diff-{hunk,file}-{prev,next}
diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view
diff--internal-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view
(when diff-auto-refine-mode
(unless (prog1 diff--auto-refine-data
(setq diff--auto-refine-data
......@@ -570,7 +570,102 @@ next hunk if TRY-HARDER is non-nil; otherwise signal an error."
diff-file diff-file-header-re "file" diff-end-of-file)
diff--internal-file diff-file-header-re "file" diff-end-of-file)
(defun diff--wrap-navigation (skip-hunk-start
what orig
header-re goto-start-func count)
"Wrap diff-{hunk,file}-{next,prev} for more intuitive behavior.
Override the default diff-{hunk,file}-{next,prev} implementation
by skipping any lines that are associated with this hunk/file but
precede the hunk-start marker. For instance, a diff file could
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 923de9a..6b1c24f 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -590,6 +590,22 @@
If a point is on 'index', then the point is considered to be in
this first hunk. Move the point to the @@... marker before
executing the default diff-hunk-next/prev implementation to move
to the NEXT marker."
(if (not skip-hunk-start)
(funcall orig count)
(let ((start (point)))
(funcall goto-start-func)
;; Trap the error.
(condition-case nil
(funcall orig count)
(error nil))
(when (not (looking-at header-re))
(goto-char start)
(user-error (format "No %s" what))))))
;; These functions all take a skip-hunk-start argument which controls
;; whether we skip pre-hunk-start text or not. In interactive uses we
;; always want to do this, but the simple behavior is still necessary
;; to, for example, avoid an infinite loop:
;; diff-hunk-next calls
;; diff--wrap-navigation calls
;; diff-bounds-of-hunk calls
;; diff-beginning-of-hunk calls
;; diff-hunk-next
;; Here the outer diff-hunk-next has skip-hunk-start set to t, but the
;; inner one does not, which breaks the loop.
(defun diff-hunk-prev (&optional count skip-hunk-start)
"Go to the previous COUNT'th hunk."
(interactive (list (prefix-numeric-value current-prefix-arg) t))
"prev hunk"
(lambda () (goto-char (car (diff-bounds-of-hunk))))
(defun diff-hunk-next (&optional count skip-hunk-start)
"Go to the next COUNT'th hunk."
(interactive (list (prefix-numeric-value current-prefix-arg) t))
"next hunk"
(lambda () (goto-char (car (diff-bounds-of-hunk))))
(defun diff-file-prev (&optional count skip-hunk-start)
"Go to the previous COUNT'th file."
(interactive (list (prefix-numeric-value current-prefix-arg) t))
"prev file"
(lambda () (goto-char (car (diff-bounds-of-file))) (diff--internal-hunk-next))
(defun diff-file-next (&optional count skip-hunk-start)
"Go to the next COUNT'th file."
(interactive (list (prefix-numeric-value current-prefix-arg) t))
"next file"
(lambda () (goto-char (car (diff-bounds-of-file))) (diff--internal-hunk-next))
(defun diff-bounds-of-hunk ()
"Return the bounds of the diff hunk at point.
......@@ -581,12 +676,13 @@ point is in a file header, return the bounds of the next hunk."
(let ((pos (point))
(beg (diff-beginning-of-hunk t))
(end (diff-end-of-hunk)))
(cond ((>= end pos)
(cond ((> end pos)
(list beg end))
;; If this hunk ends above POS, consider the next hunk.
((re-search-forward diff-hunk-header-re nil t)
(list (match-beginning 0) (diff-end-of-hunk)))
(t (error "No hunk found"))))))
;; There's no next hunk, so just take the one we have.
(t (list beg end))))))
(defun diff-bounds-of-file ()
"Return the bounds of the file segment at point.
......@@ -1665,8 +1761,9 @@ SRC and DST are the two variants of text as returned by `diff-hunk-text'.
SWITCHED is non-nil if the patch is already applied.
NOPROMPT, if non-nil, means not to prompt the user."
(let* ((other (diff-xor other-file diff-jump-to-old-file))
(char-offset (- (point) (diff-beginning-of-hunk t)))
(let* ((hunk-bounds (diff-bounds-of-hunk))
(other (diff-xor other-file diff-jump-to-old-file))
(char-offset (- (point) (goto-char (car hunk-bounds))))
;; 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),
......@@ -1675,7 +1772,7 @@ NOPROMPT, if non-nil, means not to prompt the user."
;; 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))))
(point) (cadr hunk-bounds)))
(old (diff-hunk-text hunk reverse char-offset))
(new (diff-hunk-text hunk (not reverse) char-offset))
;; Find the location specification.
......@@ -1783,8 +1880,15 @@ With a prefix argument, REVERSE the hunk."
;; Display BUF in a window
(set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
(diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
;; Advance to the next hunk with skip-hunk-start set to t
;; because we want the behavior of moving to the next logical
;; hunk, not the original behavior where were would sometimes
;; stay on the curent hunk. This is the behavior we get when
;; navigating through hunks interactively, and we want it when
;; applying hunks too (see
(when diff-advance-after-apply-hunk
(diff-hunk-next nil t))))))
(defun diff-test-hunk (&optional reverse)
......@@ -1865,14 +1969,15 @@ For use in `add-log-current-defun-function'."
(defun diff-ignore-whitespace-hunk ()
"Re-diff the current hunk, ignoring whitespace differences."
(let* ((char-offset (- (point) (diff-beginning-of-hunk t)))
(let* ((hunk-bounds (diff-bounds-of-hunk))
(char-offset (- (point) (goto-char (car hunk-bounds))))
(opts (pcase (char-after) (?@ "-bu") (?* "-bc") (_ "-b")))
(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))))
(point) (cadr hunk-bounds)))
(lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
(file1 (make-temp-file "diff1"))
(file2 (make-temp-file "diff2"))
......@@ -1959,8 +2064,8 @@ For use in `add-log-current-defun-function'."
(require 'smerge-mode)
(diff-beginning-of-hunk t)
(let* ((start (point))
(let* ((hunk-bounds (diff-bounds-of-hunk))
(start (goto-char (car hunk-bounds)))
(style (diff-hunk-style)) ;Skips the hunk header as well.
(beg (point))
(props-c '((diff-mode . fine) (face diff-refine-changed)))
......@@ -1968,7 +2073,7 @@ For use in `add-log-current-defun-function'."
(props-a '((diff-mode . fine) (face diff-refine-added)))
;; Be careful to go back to `start' so diff-end-of-hunk gets
;; to read the hunk header's line info.
(end (progn (goto-char start) (diff-end-of-hunk) (point))))
(end (goto-char (cadr hunk-bounds))))
(remove-overlays beg end 'diff-mode 'fine)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment