From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#61396: diff mode could distinguish changed from deleted lines Date: Tue, 12 Sep 2023 18:11:37 -0400 Message-ID: References: Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8653"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 61396@debbugs.gnu.org To: Samuel Wales Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 13 00:13:26 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qgBdN-00023Z-JE for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 13 Sep 2023 00:13:25 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qgBcx-0005pw-Kb; Tue, 12 Sep 2023 18:12:59 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qgBcw-0005pi-2u for bug-gnu-emacs@gnu.org; Tue, 12 Sep 2023 18:12:58 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qgBcv-0007tN-Qb for bug-gnu-emacs@gnu.org; Tue, 12 Sep 2023 18:12:57 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qgBcz-0007bk-LC for bug-gnu-emacs@gnu.org; Tue, 12 Sep 2023 18:13:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 12 Sep 2023 22:13:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61396 X-GNU-PR-Package: emacs Original-Received: via spool by 61396-submit@debbugs.gnu.org id=B61396.169455678029237 (code B ref 61396); Tue, 12 Sep 2023 22:13:01 +0000 Original-Received: (at 61396) by debbugs.gnu.org; 12 Sep 2023 22:13:00 +0000 Original-Received: from localhost ([127.0.0.1]:60231 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qgBcy-0007bV-9A for submit@debbugs.gnu.org; Tue, 12 Sep 2023 18:13:00 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:8904) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qgBcv-0007bH-LC for 61396@debbugs.gnu.org; Tue, 12 Sep 2023 18:12:59 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id DF5C5100106; Tue, 12 Sep 2023 18:12:46 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1694556765; bh=J4IMZKzXcIWuWPxp3hvpze5GLg6H1j4Q+EwXJcKAYRI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=MDHA+4QpAJ7jl2jHwlga7KlGqs5Qy6WnS0hAWIa3bPKamG0R13tgu/NBr6FFxAxP4 5rvrjhRxaU2yjf3ykaNjmttEJ2f+R/kqvoymLkUwF+8tAYTpOGwk2PkuzIe5pANQXf 4sKleYpe4wRyRec5d7lgqdvaCrPKUu9/T2oYZDmYnNYK7PautK2A7MIq1ZgfF2kag+ sylu53n9PugBkZZdPmLz+XX8bkPfAd7in2KjRLuj4TT+QNMexR1SFMcaIBwZoDjV0i MRRGEzf3ywp3cfDVomZSQoRbq3zFX0s1xzvLgl44zLG6mAG0l/r7pgZSTb4qeM+DWk /Kc8NPVksqXFw== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id D01A31000A3; Tue, 12 Sep 2023 18:12:45 -0400 (EDT) Original-Received: from lechazo (lechon.iro.umontreal.ca [132.204.27.242]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id BF479120442; Tue, 12 Sep 2023 18:12:45 -0400 (EDT) In-Reply-To: (Stefan Monnier's message of "Mon, 04 Sep 2023 17:06:06 -0400") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:270227 Archived-At: > - First, `diff -u` (contrary to `diff -c`) does not distinguish between > `removed/added` and `modified` lines. And `diff-mode` currently inherits > this weakness. I think there's a good case to be made for > highlighting the "truly added" and "truly removed" lines differently > from those that are modified. > I'd argue that a "logical" choice would be to highlight them the same > way as those parts highlighted by `diff-refine-hunk` > (i.e. `diff-refine-removed` and `diff-refine-added`) since that's how > refinement would highlight them if we were to ask it to. The patch below does that for the case of unified diffs. I kind of like the result. It's not quite ready for prime time, but I'd be interested to hear what other people think about it. Stefan diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index e8f22622935..d503652a9cf 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -2314,6 +2308,17 @@ diff-refine-hunk (end (progn (diff-end-of-hunk) (point)))) (diff--refine-hunk beg end))))) +(defun diff--refine-propertize (beg end face) + (let ((ol (make-overlay beg end))) + (overlay-put ol 'diff-mode 'fine) + (overlay-put ol 'evaporate t) + (overlay-put ol 'face face))) + +(defcustom diff-refine-nonmodified t + "If non-nil also highlight the added/removed lines. +This is currently only implemented for `unified' diffs." + :type 'boolean) + (defun diff--refine-hunk (start end) (require 'smerge-mode) (goto-char start) @@ -2328,18 +2333,26 @@ diff--refine-hunk (goto-char beg) (pcase style ('unified - (while (re-search-forward "^-" end t) + (while (re-search-forward "^[-+]" end t) (let ((beg-del (progn (beginning-of-line) (point))) beg-add end-add) - (when (and (diff--forward-while-leading-char ?- end) - ;; Allow for "\ No newline at end of file". - (progn (diff--forward-while-leading-char ?\\ end) - (setq beg-add (point))) - (diff--forward-while-leading-char ?+ end) - (progn (diff--forward-while-leading-char ?\\ end) - (setq end-add (point)))) + (cond + ((eq (char-after) ?+) + (diff--forward-while-leading-char ?+ end) + (diff--refine-propertize beg-del (point) 'diff-refine-added)) + ((and (diff--forward-while-leading-char ?- end) + ;; Allow for "\ No newline at end of file". + (progn (diff--forward-while-leading-char ?\\ end) + (setq beg-add (point))) + (diff--forward-while-leading-char ?+ end) + (progn (diff--forward-while-leading-char ?\\ end) + (setq end-add (point)))) (smerge-refine-regions beg-del beg-add beg-add end-add - nil #'diff-refine-preproc props-r props-a))))) + nil #'diff-refine-preproc props-r props-a)) + (t ;; If we're here, it's because + ;; (diff--forward-while-leading-char ?+ end) failed. + (diff--refine-propertize beg-del (point) 'diff-refine-removed) + ))))) ('context (let* ((middle (save-excursion (re-search-forward "^---" end t))) (other middle))