bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#25410: 26.0.50; Refine an unified diff hunk only if adds lines


From: Tino Calancha
Subject: bug#25410: 26.0.50; Refine an unified diff hunk only if adds lines
Date: Fri, 13 Jan 2017 21:11:20 +0900 (JST)
User-agent: Alpine 2.20 (DEB 67 2015-01-07)



On Fri, 13 Jan 2017, Tino Calancha wrote:

+         (while (re-search-forward "^-" end t)
+           (let ((beg-del (progn (beginning-of-line) (point)))
+                 (beg-add (diff--forward-while-leading-char ?- end 'no-lf-eol))
+                 (end-add (diff--forward-while-leading-char ?+ end 
'no-lf-eol)))
+             (when (and beg-add end-add)
+               (smerge-refine-subst beg-del beg-add beg-add end-add
+                                    nil 'diff-refine-preproc props-r 
props-a)))))

This is symmetrical respect beg-add/end-add but we could save a
`diff--forward-while-leading-char' call if beg-add is nil.
I think the following lazy version is better:

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 9dfcd944bb..d550a59d6d 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -2062,6 +2062,19 @@ diff-refine-preproc
 (declare-function smerge-refine-subst "smerge-mode"
                   (beg1 end1 beg2 end2 props-c &optional preproc props-r 
props-a))

+(defun diff--forward-while-leading-char (char bound &optional ok-no-lf-eol)
+  "Move point until reaching a line not starting with CHAR.
+Return new point, if it was moved.
+If optional arg OK-NO-LF-EOL is non-nil, then allow no newline at end of line."
+  (let ((orig (point)))
+    (cl-labels ((fn (ch limit)
+                    (while (and (< (point) limit) (eql (following-char) ch))
+                      (forward-line 1))))
+      (progn
+        (fn char bound)
+        (when ok-no-lf-eol (fn ?\\ bound))
+        (unless (= orig (point)) (point))))))
+
 (defun diff-refine-hunk ()
   "Highlight changes of hunk at point at a finer granularity."
   (interactive)
@@ -2081,16 +2094,14 @@ diff-refine-hunk
       (goto-char beg)
       (pcase style
         (`unified
-         (while (re-search-forward
-                 (eval-when-compile
-                   (let ((no-LF-at-eol-re "\\(?:\\\\.*\n\\)?"))
-                     (concat "^\\(?:-.*\n\\)+" no-LF-at-eol-re
-                             "\\(\\)"
-                             "\\(?:\\+.*\n\\)+" no-LF-at-eol-re)))
-                 end t)
-           (smerge-refine-subst (match-beginning 0) (match-end 1)
-                                (match-end 1) (match-end 0)
-                                nil 'diff-refine-preproc props-r props-a)))
+         (while (re-search-forward "^-" end t)
+           (let* ((beg-del (progn (beginning-of-line) (point)))
+                  (beg-add (diff--forward-while-leading-char ?- end t))
+                  (end-add (and beg-add
+                                (diff--forward-while-leading-char ?+ end t))))
+             (when end-add
+               (smerge-refine-subst beg-del beg-add beg-add end-add
+                                    nil 'diff-refine-preproc props-r 
props-a)))))
         (`context
          (let* ((middle (save-excursion (re-search-forward "^---")))
                 (other middle))






reply via email to

[Prev in Thread] Current Thread [Next in Thread]