unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dima Kogan <dima@secretsauce.net>
To: npostavs@users.sourceforge.net
Cc: Andreas Schwab <schwab@linux-m68k.org>, 17544@debbugs.gnu.org
Subject: bug#17544: 24.3; [PATCH] Improved diff-mode navigation/manipulation
Date: Wed, 14 Sep 2016 15:31:10 -0700	[thread overview]
Message-ID: <87twdi6rm9.fsf@secretsauce.net> (raw)
In-Reply-To: <87oa4018r3.fsf@secretsauce.net>

[-- Attachment #1: Type: text/plain, Size: 548 bytes --]

Here's a revised patch. I've been running with this for a few days, and
it feels delightful. Changes from the last time:

- Removed the unnecessary (call-interactively ...) pointed out by Eli

- Instead of using (easy-mmode-define-navigation) to create
  diff-{hunk,file}-{next,prev} and then overriding that definition, I
  now have (easy-mmode-define-navigation) create
  diff--internal-{hunk,file}-{next,prev}, and then my own
  (diff-{hunk,file}-{next,prev}) call those functions

- I now handle file navigation in addition to hunk navigation


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Improved-diff-mode-navigation-manipulation.patch --]
[-- Type: text/x-diff, Size: 9152 bytes --]

From 8070ca643f5467c3dcfeb8d45b76a897b347d518 Mon Sep 17 00:00:00 2001
From: Dima Kogan <Dmitriy.Kogan@jpl.nasa.gov>
Date: Wed, 14 Sep 2016 15:25:06 -0700
Subject: [PATCH] Improved diff-mode navigation/manipulation

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 this:

 --- aaa
 +++ bbb
 @@ -52,7 +52,7 @@
 hunk1
 @@ -74,7 +74,7 @@
 hunk2
 --- ccc
 +++ ddd
 @@ -608,6 +608,6 @@
 hunk3
 @@ -654,7 +654,7 @@
 hunk4

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 hunk3.

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 | 110 +++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 98 insertions(+), 12 deletions(-)

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 58498cb..6716f39 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -551,7 +551,7 @@ next hunk if TRY-HARDER is non-nil; otherwise signal an error."
 
 ;; Define diff-{hunk,file}-{prev,next}
 (easy-mmode-define-navigation
- 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,88 @@ next hunk if TRY-HARDER is non-nil; otherwise signal an error."
                                 (diff-refine-hunk))))))))))))
 
 (easy-mmode-define-navigation
- 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 (isinteractive
+                              what orig
+                              header-re goto-start-func count)
+  "I 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 contain
+
+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. Here I move the point to the @@... marker before
+executing the default diff-hunk-next/prev implementation to move
+to the NEXT marker"
+  (if (not isinteractive)
+      (funcall orig count)
+
+    (let ((start (point)))
+      (funcall goto-start-func)
+
+      ;; I 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))))))
+
+(defun diff-hunk-prev (&optional count)
+  "Go to the previous COUNT'th hunk"
+  (interactive "p")
+  (diff--wrap-navigation
+   (called-interactively-p 'interactive)
+   "prev hunk"
+   'diff--internal-hunk-prev
+   diff-hunk-header-re
+   (lambda () (goto-char (car (diff-bounds-of-hunk))))
+   count))
+
+(defun diff-hunk-next (&optional count)
+  "Go to the next COUNT'th hunk"
+  (interactive "p")
+  (diff--wrap-navigation
+   (called-interactively-p 'interactive)
+   "next hunk"
+   'diff--internal-hunk-next
+   diff-hunk-header-re
+   (lambda () (goto-char (car (diff-bounds-of-hunk))))
+   count))
+
+(defun diff-file-prev (&optional count)
+  "Go to the previous COUNT'th file"
+  (interactive "p")
+  (diff--wrap-navigation
+   (called-interactively-p 'interactive)
+   "prev file"
+   'diff--internal-file-prev
+   diff-file-header-re
+   (lambda () (goto-char (car (diff-bounds-of-file))) (diff--internal-hunk-next))
+   count))
+
+(defun diff-file-next (&optional count)
+  "Go to the next COUNT'th file"
+  (interactive "p")
+  (diff--wrap-navigation
+   (called-interactively-p 'interactive)
+   "next file"
+   'diff--internal-file-next
+   diff-file-header-re
+   (lambda () (goto-char (car (diff-bounds-of-file))) (diff--internal-hunk-next))
+   count))
+
+
+
 
 (defun diff-bounds-of-hunk ()
   "Return the bounds of the diff hunk at point.
@@ -581,12 +662,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 +1747,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."
   (save-excursion
-    (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 +1758,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.
@@ -1784,6 +1867,8 @@ With a prefix argument, REVERSE the hunk."
       (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
       (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
       (when diff-advance-after-apply-hunk
+        ;; old option:
+        ;; 	(call-interactively 'diff-hunk-next))))))
 	(diff-hunk-next))))))
 
 
@@ -1865,14 +1950,15 @@ For use in `add-log-current-defun-function'."
 (defun diff-ignore-whitespace-hunk ()
   "Re-diff the current hunk, ignoring whitespace differences."
   (interactive)
-  (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 +2045,8 @@ For use in `add-log-current-defun-function'."
   (interactive)
   (require 'smerge-mode)
   (save-excursion
-    (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 +2054,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)
 
-- 
2.8.1


  reply	other threads:[~2016-09-14 22:31 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-21 12:21 bug#17544: 24.3; [PATCH] Improved diff-mode navigation/manipulation Dima Kogan
2016-02-24  2:33 ` Lars Ingebrigtsen
2016-09-03  9:17   ` Dima Kogan
2016-09-03 10:14     ` Eli Zaretskii
2016-09-03 21:24       ` Dima Kogan
2016-09-04  3:27         ` npostavs
2016-09-07  7:14           ` Dima Kogan
2016-09-14 22:31             ` Dima Kogan [this message]
2016-09-23  7:22               ` Dima Kogan
2016-10-22 15:47                 ` npostavs
2016-10-23  1:44                   ` Dima Kogan
2016-10-23  2:49                     ` npostavs
2016-11-07  2:26                       ` Dima Kogan
2016-11-15  3:31                         ` npostavs
2016-11-17  4:15                           ` Dima Kogan
2016-11-17  4:33                             ` npostavs
2016-11-17  8:05                               ` Dima Kogan
2016-11-20  2:37                                 ` npostavs
2016-11-21  7:23                                   ` Dima Kogan
2016-11-23  0:42                                     ` npostavs
2016-11-23 21:11                                       ` Dima Kogan
2016-11-29  4:10                                         ` npostavs
2016-09-13  3:56           ` Dima Kogan
2016-09-03 11:05 ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87twdi6rm9.fsf@secretsauce.net \
    --to=dima@secretsauce.net \
    --cc=17544@debbugs.gnu.org \
    --cc=npostavs@users.sourceforge.net \
    --cc=schwab@linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).