aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuri Linkov2018-12-19 00:55:15 +0200
committerJuri Linkov2018-12-19 00:55:15 +0200
commit8cbbf4ba25bef2c6c1b525aa380a1cd3f0b0d012 (patch)
treee08309ab9586f4798660b7e194a68966a63031b8
parentcdaaaf2e1bd1f8ad2784ffc8265aa642da2d1190 (diff)
downloademacs-8cbbf4ba25bef2c6c1b525aa380a1cd3f0b0d012.tar.gz
emacs-8cbbf4ba25bef2c6c1b525aa380a1cd3f0b0d012.zip
Fontify one-line diffs without the final newline (bug#33567)
* lisp/vc/diff-mode.el (diff-hunk-text, diff-syntax-fontify-hunk): Skip lines beginning with backslash like "\ No newline at end of file". (diff-syntax-fontify-hunk): Use string-trim-right. For one-line diffs use 1 explicitly in the list of line numbers.
-rw-r--r--lisp/vc/diff-mode.el42
1 files changed, 24 insertions, 18 deletions
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index ed953deb21a..549f49a8d13 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -1697,10 +1697,11 @@ char-offset in TEXT."
1697 (delete-region divider-pos (point-max))) 1697 (delete-region divider-pos (point-max)))
1698 (delete-region (point-min) keep)) 1698 (delete-region (point-min) keep))
1699 ;; Remove line-prefix characters, and unneeded lines (unified diffs). 1699 ;; Remove line-prefix characters, and unneeded lines (unified diffs).
1700 (let ((kill-char (if destp ?- ?+))) 1700 ;; Also skip lines like "\ No newline at end of file"
1701 (let ((kill-chars (list (if destp ?- ?+) ?\\)))
1701 (goto-char (point-min)) 1702 (goto-char (point-min))
1702 (while (not (eobp)) 1703 (while (not (eobp))
1703 (if (eq (char-after) kill-char) 1704 (if (memq (char-after) kill-chars)
1704 (delete-region (point) (progn (forward-line 1) (point))) 1705 (delete-region (point) (progn (forward-line 1) (point)))
1705 (delete-char num-pfx-chars) 1706 (delete-char num-pfx-chars)
1706 (forward-line 1))))) 1707 (forward-line 1)))))
@@ -2394,19 +2395,23 @@ and the position in MAX."
2394 2395
2395(defvar diff-syntax-fontify-revisions (make-hash-table :test 'equal)) 2396(defvar diff-syntax-fontify-revisions (make-hash-table :test 'equal))
2396 2397
2398(eval-when-compile (require 'subr-x)) ; for string-trim-right
2399
2397(defun diff-syntax-fontify-hunk (beg end old) 2400(defun diff-syntax-fontify-hunk (beg end old)
2398 "Highlight source language syntax in diff hunk between BEG and END. 2401 "Highlight source language syntax in diff hunk between BEG and END.
2399When OLD is non-nil, highlight the hunk from the old source." 2402When OLD is non-nil, highlight the hunk from the old source."
2400 (remove-overlays beg end 'diff-mode 'syntax) 2403 (remove-overlays beg end 'diff-mode 'syntax)
2401 (goto-char beg) 2404 (goto-char beg)
2402 (let* ((hunk (buffer-substring-no-properties beg end)) 2405 (let* ((hunk (buffer-substring-no-properties beg end))
2403 (text (or (ignore-errors (diff-hunk-text hunk (not old) nil)) "")) 2406 (text (string-trim-right (or (ignore-errors (diff-hunk-text hunk (not old) nil)) "")))
2404 (line (if (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?") 2407 (line (if (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")
2405 (if old (match-string 1) 2408 (if old (match-string 1)
2406 (if (match-end 3) (match-string 3) (match-string 1))))) 2409 (if (match-end 3) (match-string 3) (match-string 1)))))
2407 (line-nb (and line (string-match "\\([0-9]+\\),\\([0-9]+\\)" line) 2410 (line-nb (when line
2408 (list (string-to-number (match-string 1 line)) 2411 (if (string-match "\\([0-9]+\\),\\([0-9]+\\)" line)
2409 (string-to-number (match-string 2 line))))) 2412 (list (string-to-number (match-string 1 line))
2413 (string-to-number (match-string 2 line)))
2414 (list (string-to-number line) 1)))) ; One-line diffs
2410 props) 2415 props)
2411 (cond 2416 (cond
2412 ((and diff-vc-backend (not (eq diff-font-lock-syntax 'hunk-only))) 2417 ((and diff-vc-backend (not (eq diff-font-lock-syntax 'hunk-only)))
@@ -2470,18 +2475,19 @@ When OLD is non-nil, highlight the hunk from the old source."
2470 (while (< (progn (forward-line 1) (point)) end) 2475 (while (< (progn (forward-line 1) (point)) end)
2471 (when (or (and (not old) (not (looking-at-p "[-<]"))) 2476 (when (or (and (not old) (not (looking-at-p "[-<]")))
2472 (and old (not (looking-at-p "[+>]")))) 2477 (and old (not (looking-at-p "[+>]"))))
2473 (if (and old (not (looking-at-p "[-<]"))) 2478 (unless (looking-at-p "\\\\") ; skip "\ No newline at end of file"
2474 ;; Fontify context lines only from new source, 2479 (if (and old (not (looking-at-p "[-<]")))
2475 ;; don't refontify context lines from old source. 2480 ;; Fontify context lines only from new source,
2476 (pop props) 2481 ;; don't refontify context lines from old source.
2477 (let ((line-props (pop props)) 2482 (pop props)
2478 (bol (1+ (point)))) 2483 (let ((line-props (pop props))
2479 (dolist (prop line-props) 2484 (bol (1+ (point))))
2480 (let ((ol (make-overlay (+ bol (nth 0 prop)) 2485 (dolist (prop line-props)
2481 (+ bol (nth 1 prop)) 2486 (let ((ol (make-overlay (+ bol (nth 0 prop))
2482 nil 'front-advance nil))) 2487 (+ bol (nth 1 prop))
2483 (overlay-put ol 'evaporate t) 2488 nil 'front-advance nil)))
2484 (overlay-put ol 'face (nth 2 prop))))))))))) 2489 (overlay-put ol 'evaporate t)
2490 (overlay-put ol 'face (nth 2 prop))))))))))))
2485 2491
2486(defun diff-syntax-fontify-props (file text line-nb &optional no-init hunk-only) 2492(defun diff-syntax-fontify-props (file text line-nb &optional no-init hunk-only)
2487 "Get font-lock properties from the source code. 2493 "Get font-lock properties from the source code.