From 8cbbf4ba25bef2c6c1b525aa380a1cd3f0b0d012 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Wed, 19 Dec 2018 00:55:15 +0200 Subject: [PATCH] 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. --- lisp/vc/diff-mode.el | 42 ++++++++++++++++++++++++------------------ 1 file 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." (delete-region divider-pos (point-max))) (delete-region (point-min) keep)) ;; Remove line-prefix characters, and unneeded lines (unified diffs). - (let ((kill-char (if destp ?- ?+))) + ;; Also skip lines like "\ No newline at end of file" + (let ((kill-chars (list (if destp ?- ?+) ?\\))) (goto-char (point-min)) (while (not (eobp)) - (if (eq (char-after) kill-char) + (if (memq (char-after) kill-chars) (delete-region (point) (progn (forward-line 1) (point))) (delete-char num-pfx-chars) (forward-line 1))))) @@ -2394,19 +2395,23 @@ and the position in MAX." (defvar diff-syntax-fontify-revisions (make-hash-table :test 'equal)) +(eval-when-compile (require 'subr-x)) ; for string-trim-right + (defun diff-syntax-fontify-hunk (beg end old) "Highlight source language syntax in diff hunk between BEG and END. When OLD is non-nil, highlight the hunk from the old source." (remove-overlays beg end 'diff-mode 'syntax) (goto-char beg) (let* ((hunk (buffer-substring-no-properties beg end)) - (text (or (ignore-errors (diff-hunk-text hunk (not old) nil)) "")) + (text (string-trim-right (or (ignore-errors (diff-hunk-text hunk (not old) nil)) ""))) (line (if (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?") (if old (match-string 1) (if (match-end 3) (match-string 3) (match-string 1))))) - (line-nb (and line (string-match "\\([0-9]+\\),\\([0-9]+\\)" line) - (list (string-to-number (match-string 1 line)) - (string-to-number (match-string 2 line))))) + (line-nb (when line + (if (string-match "\\([0-9]+\\),\\([0-9]+\\)" line) + (list (string-to-number (match-string 1 line)) + (string-to-number (match-string 2 line))) + (list (string-to-number line) 1)))) ; One-line diffs props) (cond ((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." (while (< (progn (forward-line 1) (point)) end) (when (or (and (not old) (not (looking-at-p "[-<]"))) (and old (not (looking-at-p "[+>]")))) - (if (and old (not (looking-at-p "[-<]"))) - ;; Fontify context lines only from new source, - ;; don't refontify context lines from old source. - (pop props) - (let ((line-props (pop props)) - (bol (1+ (point)))) - (dolist (prop line-props) - (let ((ol (make-overlay (+ bol (nth 0 prop)) - (+ bol (nth 1 prop)) - nil 'front-advance nil))) - (overlay-put ol 'evaporate t) - (overlay-put ol 'face (nth 2 prop))))))))))) + (unless (looking-at-p "\\\\") ; skip "\ No newline at end of file" + (if (and old (not (looking-at-p "[-<]"))) + ;; Fontify context lines only from new source, + ;; don't refontify context lines from old source. + (pop props) + (let ((line-props (pop props)) + (bol (1+ (point)))) + (dolist (prop line-props) + (let ((ol (make-overlay (+ bol (nth 0 prop)) + (+ bol (nth 1 prop)) + nil 'front-advance nil))) + (overlay-put ol 'evaporate t) + (overlay-put ol 'face (nth 2 prop)))))))))))) (defun diff-syntax-fontify-props (file text line-nb &optional no-init hunk-only) "Get font-lock properties from the source code. -- 2.39.5