From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Dima Kogan Newsgroups: gmane.emacs.bugs Subject: bug#17544: 24.3; [PATCH] Improved diff-mode navigation/manipulation Date: Wed, 21 May 2014 05:21:28 -0700 Message-ID: <87ha4jgw53.fsf@secretsauce.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1400684431 11968 80.91.229.3 (21 May 2014 15:00:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 21 May 2014 15:00:31 +0000 (UTC) To: 17544@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed May 21 17:00:24 2014 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Wn80E-0003PH-Rg for geb-bug-gnu-emacs@m.gmane.org; Wed, 21 May 2014 17:00:23 +0200 Original-Received: from localhost ([::1]:60280 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn80E-0004UQ-Hz for geb-bug-gnu-emacs@m.gmane.org; Wed, 21 May 2014 11:00:22 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57631) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn804-0004U5-Qe for bug-gnu-emacs@gnu.org; Wed, 21 May 2014 11:00:19 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wn7zy-0003u2-5e for bug-gnu-emacs@gnu.org; Wed, 21 May 2014 11:00:12 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:56572) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn7zx-0003sm-VQ for bug-gnu-emacs@gnu.org; Wed, 21 May 2014 11:00:06 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1Wn7zx-00057x-LM for bug-gnu-emacs@gnu.org; Wed, 21 May 2014 11:00:05 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Dima Kogan Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 21 May 2014 15:00:05 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 17544 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.140068439619642 (code B ref -1); Wed, 21 May 2014 15:00:05 +0000 Original-Received: (at submit) by debbugs.gnu.org; 21 May 2014 14:59:56 +0000 Original-Received: from localhost ([127.0.0.1]:55447 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Wn7zm-00056c-UH for submit@debbugs.gnu.org; Wed, 21 May 2014 10:59:56 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:43869) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Wn5Wx-0008Uz-1y for submit@debbugs.gnu.org; Wed, 21 May 2014 08:21:59 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wn5Wl-0001G2-7Z for submit@debbugs.gnu.org; Wed, 21 May 2014 08:21:53 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:52388) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn5Wl-0001Fy-4e for submit@debbugs.gnu.org; Wed, 21 May 2014 08:21:47 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45563) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn5Wf-0002i1-6H for bug-gnu-emacs@gnu.org; Wed, 21 May 2014 08:21:47 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wn5WZ-00013Y-CV for bug-gnu-emacs@gnu.org; Wed, 21 May 2014 08:21:41 -0400 Original-Received: from out3-smtp.messagingengine.com ([66.111.4.27]:48905) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn5WZ-000138-70 for bug-gnu-emacs@gnu.org; Wed, 21 May 2014 08:21:35 -0400 Original-Received: from compute4.internal (compute4.nyi.mail.srv.osa [10.202.2.44]) by gateway1.nyi.mail.srv.osa (Postfix) with ESMTP id DAA90210F3 for ; Wed, 21 May 2014 08:21:31 -0400 (EDT) Original-Received: from frontend2 ([10.202.2.161]) by compute4.internal (MEProxy); Wed, 21 May 2014 08:21:32 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=from:to:subject:date:message-id :mime-version:content-type; s=smtpout; bh=vbWvuoLw1G7Lynp0lTKmkU iQTXA=; b=cVz7nEySwkPlEYizNbvojomsnVJdJK3mWE2nXfKIFXy7UFGgLxWEAE tQQOpsNfO8KvFVtaYfotZguYBnwqvf2DLXWE5jDJ/Dl/xt4HMvfizlP5ikHm/AbO ubKW4/xVAgBcMUstT4HMxcyhuzPLPBOn2ed6F80zQSalvm7STcjt4= X-Sasl-enc: 7oXcFONHGHto77jPxvrJ6gGlibQ4YovE4zkRlI9eY1Xl 1400674890 Original-Received: from shorty.local (unknown [23.243.199.75]) by mail.messagingengine.com (Postfix) with ESMTPA id BEB5F680118 for ; Wed, 21 May 2014 08:21:30 -0400 (EDT) Original-Received: from dima by shorty.local with local (Exim 4.82) (envelope-from ) id 1Wn5WT-0007KR-ER for bug-gnu-emacs@gnu.org; Wed, 21 May 2014 05:21:29 -0700 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Mailman-Approved-At: Wed, 21 May 2014 10:59:50 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:89318 Archived-At: --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Improved-diff-mode-navigation-manipulation.patch >From 27d926293402c74eb3d83124e0287dd49cb4543a Mon Sep 17 00:00:00 2001 From: Dima Kogan Date: Thu, 15 May 2014 00:24:00 -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 | 77 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 11 deletions(-) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 923de9a..d334307 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -593,6 +593,58 @@ next hunk if TRY-HARDER is non-nil; otherwise signal an error." (easy-mmode-define-navigation diff-file diff-file-header-re "file" diff-end-of-file) +(defun diff--wrap-hunk-navigation (isinteractive name orig count) + "I override the default diff-hunk-next/prev implementation by +skipping any lines that are associated with this hunk 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))) + (let ((hunk-bounds (diff-bounds-of-hunk))) + (goto-char (car hunk-bounds))) + + (funcall orig count) + + (when (not (looking-at diff-hunk-header-re)) + (goto-char start) + (user-error (format "No %s hunk" name)))))) + +(setq diff--hunk-prev-internal (symbol-function 'diff-hunk-prev)) +(defun diff-hunk-prev (&optional count) + "Go to the previous COUNT'th hunk" + (interactive "p") + (diff--wrap-hunk-navigation + (called-interactively-p 'interactive) + "prev" + diff--hunk-prev-internal + count)) + +(setq diff--hunk-next-internal (symbol-function 'diff-hunk-next)) +(defun diff-hunk-next (&optional count) + "Go to the next COUNT'th hunk" + (interactive "p") + (diff--wrap-hunk-navigation + (called-interactively-p 'interactive) + "next" + diff--hunk-next-internal + count)) + + + + (defun diff-bounds-of-hunk () "Return the bounds of the diff hunk at point. The return value is a list (BEG END), which are the hunk's start @@ -602,12 +654,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. @@ -1683,8 +1736,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), @@ -1693,7 +1747,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. @@ -1802,7 +1856,7 @@ 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 - (diff-hunk-next)))))) + (call-interactively 'diff-hunk-next)))))) (defun diff-test-hunk (&optional reverse) @@ -1873,14 +1927,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")) @@ -1967,8 +2022,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-change))) @@ -1976,7 +2031,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.0.0.rc0 --=-=-=--