From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Dima Kogan Newsgroups: gmane.emacs.bugs Subject: bug#17544: 24.3; [PATCH] Improved diff-mode navigation/manipulation Date: Wed, 23 Nov 2016 13:11:23 -0800 Message-ID: <87lgw96gw4.fsf@secretsauce.net> References: <87ha4jgw53.fsf@secretsauce.net> <87si0ion1c.fsf@gnus.org> <87k2et8hr4.fsf@secretsauce.net> <83a8fpe1dr.fsf@gnu.org> <87h99w8ynv.fsf@secretsauce.net> <87k2es9weh.fsf@users.sourceforge.net> <87oa4018r3.fsf@secretsauce.net> <87twdi6rm9.fsf@secretsauce.net> <87h997nkqp.fsf@secretsauce.net> <874m44tmge.fsf@users.sourceforge.net> <87h983rg9c.fsf@secretsauce.net> <87mvhvsrta.fsf@users.sourceforge.net> <87wpgge0kl.fsf@secretsauce.net> <87twb9l8qi.fsf@users.sourceforge.net> <878tsihhhl.fsf@secretsauce.net> <87lgwilo8c.fsf@users.sourceforge.net> <871syah6p9.fsf@secretsauce.net> <87zikukhb3.fsf@users.sourceforge.net> <87r365fg9z.fsf@secretsauce.net> <87d1hnjack.fsf@users.sourceforge.net> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1479935539 3471 195.159.176.226 (23 Nov 2016 21:12:19 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 23 Nov 2016 21:12:19 +0000 (UTC) User-Agent: mu4e 0.9.17; emacs 26.0.50.1 Cc: Andreas Schwab , 17544@debbugs.gnu.org To: npostavs@users.sourceforge.net Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Nov 23 22:12:13 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c9ept-0007pl-Ip for geb-bug-gnu-emacs@m.gmane.org; Wed, 23 Nov 2016 22:12:10 +0100 Original-Received: from localhost ([::1]:36407 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c9epx-0000gG-6H for geb-bug-gnu-emacs@m.gmane.org; Wed, 23 Nov 2016 16:12:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33045) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c9epp-0000fz-Ju for bug-gnu-emacs@gnu.org; Wed, 23 Nov 2016 16:12:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c9epm-00063f-Aw for bug-gnu-emacs@gnu.org; Wed, 23 Nov 2016 16:12:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:52470) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1c9epm-00063F-6e for bug-gnu-emacs@gnu.org; Wed, 23 Nov 2016 16:12:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1c9epl-0006Wv-Q1 for bug-gnu-emacs@gnu.org; Wed, 23 Nov 2016 16:12:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Dima Kogan Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 23 Nov 2016 21:12:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 17544 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 17544-submit@debbugs.gnu.org id=B17544.147993548825056 (code B ref 17544); Wed, 23 Nov 2016 21:12:01 +0000 Original-Received: (at 17544) by debbugs.gnu.org; 23 Nov 2016 21:11:28 +0000 Original-Received: from localhost ([127.0.0.1]:39636 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c9epD-0006W3-J0 for submit@debbugs.gnu.org; Wed, 23 Nov 2016 16:11:28 -0500 Original-Received: from out5-smtp.messagingengine.com ([66.111.4.29]:53979) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c9epC-0006Vw-6w for 17544@debbugs.gnu.org; Wed, 23 Nov 2016 16:11:26 -0500 Original-Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id B35E421B9C; Wed, 23 Nov 2016 16:11:25 -0500 (EST) Original-Received: from frontend2 ([10.202.2.161]) by compute1.internal (MEProxy); Wed, 23 Nov 2016 16:11:25 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=secretsauce.net; h=cc:content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=TPRpmUoogmwkiGH5Ad2SQeCzeyk=; b=Ea/SUp hAjlXf6vVvjhGsu9j4KuQo7xAMjH4U5vmNYQAIvDajPHUmEfGj+KVVPcaAu3hGIK DXimsaGC2cEgQYy5c3WZ5P69rvmMVkEMTy2S4iAFGohLZBCJCCCiRT9BgswoWo+b rDzlxO9mfWDpu7zz080ZuodSrA/EjLHqHrYJQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=smtpout; bh=TPRpmUoogmwkiG H5Ad2SQeCzeyk=; b=W9ntuHg7ehtM3tmx6DJasmgIjvsSxS3SXIGhuCbCB+ngqi PQUyvysCgjeofBAuuGy4OdILn35u1uQwj99sow/Uuxw5m1SNhAl7BRQEGziFIAc7 FklaHnvPDyaVf6HV9RGK7ENm3e65iakK1uilonNC1PT+RmM+UWN7EQ+AfNnTE= X-ME-Sender: X-Sasl-enc: zoUoODE1XNECUBVEDHcgo40SB4swNdiLys3u1wOdr2ng 1479935485 Original-Received: from scrawny (50-1-153-216.dsl.dynamic.fusionbroadband.com [50.1.153.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 38C1025056; Wed, 23 Nov 2016 16:11:25 -0500 (EST) Original-Received: from [::1] (helo=scrawny) by scrawny with esmtp (Exim 4.87) (envelope-from ) id 1c9ep9-0004RR-UI; Wed, 23 Nov 2016 13:11:23 -0800 In-reply-to: <87d1hnjack.fsf@users.sourceforge.net> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.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" Xref: news.gmane.org gmane.emacs.bugs:126027 Archived-At: --=-=-= Content-Type: text/plain npostavs@users.sourceforge.net writes: > The commit message should also have double spaced sentences and should > end with a ChangeLog style entry. Apart from that I think is ready to > push to master. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Improve-diff-mode-navigation-manipulation.patch >From d3950fca6cb1f3aa097cec0f524e38b9a7f05303 Mon Sep 17 00:00:00 2001 From: Dima Kogan Date: Wed, 14 Sep 2016 15:25:06 -0700 Subject: [PATCH] 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 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 (diff--wrap-navigation): New function to add better navigation logic to diff-{hunk,file}-{next,prev}. lisp/vc/diff-mode.el (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. lisp/vc/diff-mode.el (diff-bounds-of-hunk, diff-find-source-location, diff-apply-hunk, diff-current-defun, diff-refine-hunk): Small tweaks to improve hunk navigation. --- lisp/vc/diff-mode.el | 131 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 118 insertions(+), 13 deletions(-) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 58498cb..c9a5f89 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -551,7 +551,7 @@ diff--auto-refine-data ;; 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,102 @@ diff--auto-refine-data (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 (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 +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. 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)) + (diff--wrap-navigation + skip-hunk-start + "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 skip-hunk-start) + "Go to the next COUNT'th hunk." + (interactive (list (prefix-numeric-value current-prefix-arg) t)) + (diff--wrap-navigation + skip-hunk-start + "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 skip-hunk-start) + "Go to the previous COUNT'th file." + (interactive (list (prefix-numeric-value current-prefix-arg) t)) + (diff--wrap-navigation + skip-hunk-start + "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 skip-hunk-start) + "Go to the next COUNT'th file." + (interactive (list (prefix-numeric-value current-prefix-arg) t)) + (diff--wrap-navigation + skip-hunk-start + "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 +676,13 @@ diff-bounds-of-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 @@ diff-find-source-location 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 +1772,7 @@ diff-find-source-location ;; 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 @@ diff-apply-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 http://debbugs.gnu.org/17544). (when diff-advance-after-apply-hunk - (diff-hunk-next)))))) + (diff-hunk-next nil t)))))) (defun diff-test-hunk (&optional reverse) @@ -1865,14 +1969,15 @@ diff-current-defun (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 +2064,8 @@ diff-refine-hunk (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 +2073,7 @@ diff-refine-hunk (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.0.rc3 --=-=-=--