Commit 8c0fcaf6 authored by Noam Postavsky's avatar Noam Postavsky
Browse files

Avoid inefficient regex in diff-refine-hunk (Bug#25410)

* lisp/vc/diff-mode.el (diff--forward-while-leading-char): New function.
(diff-refine-hunk): Use it instead of trying to match multiple lines
with a single lines.
parent 5304cb98
...@@ -2062,6 +2062,15 @@ For use in `add-log-current-defun-function'." ...@@ -2062,6 +2062,15 @@ For use in `add-log-current-defun-function'."
(declare-function smerge-refine-subst "smerge-mode" (declare-function smerge-refine-subst "smerge-mode"
(beg1 end1 beg2 end2 props-c &optional preproc props-r props-a)) (beg1 end1 beg2 end2 props-c &optional preproc props-r props-a))
(defun diff--forward-while-leading-char (char bound)
"Move point until reaching a line not starting with CHAR.
Return new point, if it was moved."
(let ((pt nil))
(while (and (< (point) bound) (eql (following-char) char))
(forward-line 1)
(setq pt (point)))
pt))
(defun diff-refine-hunk () (defun diff-refine-hunk ()
"Highlight changes of hunk at point at a finer granularity." "Highlight changes of hunk at point at a finer granularity."
(interactive) (interactive)
...@@ -2081,16 +2090,18 @@ For use in `add-log-current-defun-function'." ...@@ -2081,16 +2090,18 @@ For use in `add-log-current-defun-function'."
(goto-char beg) (goto-char beg)
(pcase style (pcase style
(`unified (`unified
(while (re-search-forward (while (re-search-forward "^-" end t)
(eval-when-compile (let ((beg-del (progn (beginning-of-line) (point)))
(let ((no-LF-at-eol-re "\\(?:\\\\.*\n\\)?")) beg-add end-add)
(concat "^\\(?:-.*\n\\)+" no-LF-at-eol-re (when (and (diff--forward-while-leading-char ?- end)
"\\(\\)" ;; Allow for "\ No newline at end of file".
"\\(?:\\+.*\n\\)+" no-LF-at-eol-re))) (progn (diff--forward-while-leading-char ?\\ end)
end t) (setq beg-add (point)))
(smerge-refine-subst (match-beginning 0) (match-end 1) (diff--forward-while-leading-char ?+ end)
(match-end 1) (match-end 0) (progn (diff--forward-while-leading-char ?\\ end)
nil 'diff-refine-preproc props-r props-a))) (setq end-add (point))))
(smerge-refine-subst beg-del beg-add beg-add end-add
nil 'diff-refine-preproc props-r props-a)))))
(`context (`context
(let* ((middle (save-excursion (re-search-forward "^---"))) (let* ((middle (save-excursion (re-search-forward "^---")))
(other middle)) (other middle))
......
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