all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Samuel Wales <samologist@gmail.com>
Cc: 61396@debbugs.gnu.org
Subject: bug#61396: diff mode could distinguish changed from deleted lines
Date: Tue, 12 Sep 2023 18:11:37 -0400	[thread overview]
Message-ID: <jwv34zjkprk.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <jwv8r9loe93.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Mon, 04 Sep 2023 17:06:06 -0400")

> - 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))






  parent reply	other threads:[~2023-09-12 22:11 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-10  3:25 bug#61396: diff mode could distinguish changed from deleted lines Samuel Wales
2023-02-10  7:17 ` Juri Linkov
2023-02-10 23:49   ` Samuel Wales
2023-02-10 23:50     ` Samuel Wales
2023-02-10 13:58 ` Dmitry Gutov
2023-02-11  4:25 ` Richard Stallman
2023-02-11  5:07   ` Samuel Wales
2023-02-11 17:54     ` Juri Linkov
2023-02-12  0:52       ` Samuel Wales
2023-02-12  1:04         ` Dmitry Gutov
2023-02-12  1:07           ` Samuel Wales
2023-02-12  1:52             ` Dmitry Gutov
2023-02-12  2:12               ` Samuel Wales
2023-02-12  2:17                 ` Dmitry Gutov
2023-02-12  2:54                   ` Samuel Wales
2023-02-12  8:31         ` Juri Linkov
2023-02-12  9:03           ` Samuel Wales
2023-02-12 17:20             ` Juri Linkov
2023-02-12 22:16               ` Samuel Wales
2023-02-12 22:48                 ` Samuel Wales
2023-07-23  6:04                   ` Samuel Wales
2023-07-24 10:21                     ` Robert Pluim
2023-07-24 23:38                       ` Samuel Wales
2023-07-24 23:39                         ` Samuel Wales
2023-07-25  8:11                           ` Robert Pluim
2023-07-25 21:29                             ` Samuel Wales
2023-08-10 23:56                     ` Samuel Wales
2023-08-11  0:41                       ` Dmitry Gutov
2023-09-03 17:29                     ` Juri Linkov
2023-03-08 21:14 ` Samuel Wales
2023-09-04 21:06 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-04 22:38   ` Samuel Wales
2023-09-07  2:34     ` Samuel Wales
2023-09-12 22:11   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-09-12 22:31     ` Dmitry Gutov
2023-09-13 14:51       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-14  6:05         ` Samuel Wales
2023-09-14 22:42         ` Dmitry Gutov
2023-09-15  1:34           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-15  1:58             ` Samuel Wales
2023-09-15 10:20             ` Dmitry Gutov
2023-09-30 17:38         ` Juri Linkov
2023-09-30 18:18           ` Eli Zaretskii
2023-10-01  6:32             ` Juri Linkov
2023-10-01 15:54           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-01 18:53             ` Juri Linkov
2023-10-01 22:16               ` Samuel Wales
2023-10-02  6:48                 ` Juri Linkov
2023-10-02 16:56   ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwv34zjkprk.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=61396@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=samologist@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.