From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#33567: Syntactic fontification of diff hunks Date: Tue, 18 Dec 2018 02:14:48 +0200 Organization: LINKOV.NET Message-ID: <87tvjblm0f.fsf@mail.linkov.net> References: <878t18j4is.fsf@mail.linkov.net> <83a7lobemr.fsf@gnu.org> <87a7lnv6ex.fsf@mail.linkov.net> <83pnuj9kb8.fsf@gnu.org> <875zwajh6c.fsf@mail.linkov.net> <837egpagko.fsf@gnu.org> <87k1koygvm.fsf@mail.linkov.net> <83r2ew8mfx.fsf@gnu.org> <87va475y1m.fsf@mail.linkov.net> <87mupafi73.fsf@mail.linkov.net> <834lbgh3n7.fsf@gnu.org> <87h8fduis0.fsf@mail.linkov.net> <83d0q0dtc0.fsf@gnu.org> <877eg7sq8a.fsf@mail.linkov.net> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1545093129 6526 195.159.176.226 (18 Dec 2018 00:32:09 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 18 Dec 2018 00:32:09 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu) To: 33567@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Dec 18 01:32:05 2018 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 1gZ3Ip-0001Yj-PY for geb-bug-gnu-emacs@m.gmane.org; Tue, 18 Dec 2018 01:32:04 +0100 Original-Received: from localhost ([::1]:50795 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZ3Kw-0007PT-Fc for geb-bug-gnu-emacs@m.gmane.org; Mon, 17 Dec 2018 19:34:14 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60088) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZ3Ko-0007PL-Nc for bug-gnu-emacs@gnu.org; Mon, 17 Dec 2018 19:34:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gZ3Kk-00037g-Us for bug-gnu-emacs@gnu.org; Mon, 17 Dec 2018 19:34:06 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:48130) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gZ3Kk-00037K-Co for bug-gnu-emacs@gnu.org; Mon, 17 Dec 2018 19:34:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gZ3Kk-00038f-5T for bug-gnu-emacs@gnu.org; Mon, 17 Dec 2018 19:34:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 18 Dec 2018 00:34:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33567 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 33567-submit@debbugs.gnu.org id=B33567.154509322712043 (code B ref 33567); Tue, 18 Dec 2018 00:34:02 +0000 Original-Received: (at 33567) by debbugs.gnu.org; 18 Dec 2018 00:33:47 +0000 Original-Received: from localhost ([127.0.0.1]:52388 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gZ3KU-00038B-UL for submit@debbugs.gnu.org; Mon, 17 Dec 2018 19:33:47 -0500 Original-Received: from ladybird.maple.relay.mailchannels.net ([23.83.214.98]:18403) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gZ3KR-000380-VR for 33567@debbugs.gnu.org; Mon, 17 Dec 2018 19:33:44 -0500 X-Sender-Id: dreamhost|x-authsender|jurta@jurta.org Original-Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 5E7833E3ECE for <33567@debbugs.gnu.org>; Tue, 18 Dec 2018 00:33:42 +0000 (UTC) Original-Received: from pdx1-sub0-mail-a37.g.dreamhost.com (unknown [100.96.19.74]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 160193E32EF for <33567@debbugs.gnu.org>; Tue, 18 Dec 2018 00:33:42 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|jurta@jurta.org Original-Received: from pdx1-sub0-mail-a37.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.16.2); Tue, 18 Dec 2018 00:33:42 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|jurta@jurta.org X-MailChannels-Auth-Id: dreamhost X-Stretch-Grain: 64eec2a0500d9cfb_1545093222203_2528278769 X-MC-Loop-Signature: 1545093222202:1547472118 X-MC-Ingress-Time: 1545093222202 Original-Received: from pdx1-sub0-mail-a37.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a37.g.dreamhost.com (Postfix) with ESMTP id B3F9D81B90 for <33567@debbugs.gnu.org>; Mon, 17 Dec 2018 16:33:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=linkov.net; h=from:to :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=linkov.net; bh=D/u4INvHrl0HG4xHE8Eo7FBqwzY=; b= WkwUhwZ9yxvWWUHOpWnFUPkfGMMsnvtRWeeWnIu6O9aza4BMpItGnqq/52Sz2qAP TjD/5Izs52JdXbqVczlBMni49mtXNf2cIOatf5XFm3IanA2yFM6RKkV0dFgWEM2g mctrX/D6DpvQRcsGblcRw7PFta00gBQWlRpBH/KsSe8= Original-Received: from mail.jurta.org (m91-129-96-177.cust.tele2.ee [91.129.96.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: jurta@jurta.org) by pdx1-sub0-mail-a37.g.dreamhost.com (Postfix) with ESMTPSA id E2B4481B8A for <33567@debbugs.gnu.org>; Mon, 17 Dec 2018 16:33:40 -0800 (PST) X-DH-BACKEND: pdx1-sub0-mail-a37 In-Reply-To: <877eg7sq8a.fsf@mail.linkov.net> (Juri Linkov's message of "Tue, 18 Dec 2018 01:11:49 +0200") X-VR-OUT-STATUS: OK X-VR-OUT-SCORE: 0 X-VR-OUT-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedtkedrudeigedgvdegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuggftfghnshhusghstghrihgsvgdpffftgfetoffjqffuvfenuceurghilhhouhhtmecufedttdenucenucfjughrpefhvffuohhfffgjkfgfgggtsehmtderredtredtnecuhfhrohhmpefluhhrihcunfhinhhkohhvuceojhhurhhisehlihhnkhhovhdrnhgvtheqnecukfhppeeluddruddvledrleeirddujeejnecurfgrrhgrmhepmhhouggvpehsmhhtphdphhgvlhhopehmrghilhdrjhhurhhtrgdrohhrghdpihhnvghtpeeluddruddvledrleeirddujeejpdhrvghtuhhrnhdqphgrthhhpefluhhrihcunfhinhhkohhvuceojhhurhhisehlihhnkhhovhdrnhgvtheqpdhmrghilhhfrhhomhepjhhurhhisehlihhnkhhovhdrnhgvthdpnhhrtghpthhtohepfeefheeijeesuggvsggsuhhgshdrghhnuhdrohhrghenucevlhhushhtvghrufhiiigvpedt 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:153541 Archived-At: --=-=-= Content-Type: text/plain >> Thanks, this LGTM. > > Pushed to master and closed. Here's an additional patch to support diffs with files that contain just one line as documented in (info "(diffutils) Detailed Unified") and also without the final newline. Such diffs have the text: \ No newline at end of file --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=diff-mode_no_newline.el.patch diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index ed953deb21..8c18f69e8c 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -1697,10 +1697,11 @@ diff-hunk-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 @@ diff-syntax-fontify (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) + (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))))) + (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,6 +2475,7 @@ diff-syntax-fontify-hunk (while (< (progn (forward-line 1) (point)) end) (when (or (and (not old) (not (looking-at-p "[-<]"))) (and old (not (looking-at-p "[+>]")))) + (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. @@ -2481,7 +2487,7 @@ diff-syntax-fontify-hunk (+ bol (nth 1 prop)) nil 'front-advance nil))) (overlay-put ol 'evaporate t) - (overlay-put ol 'face (nth 2 prop))))))))))) + (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. --=-=-=--