unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#49197: [PATCH] 28.0.50; Inability to find "diff" prevents `font-lock-add-keywords' from working in `diff-mode'
@ 2021-06-23 21:00 Jim Porter
  2021-06-24 16:07 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 2+ messages in thread
From: Jim Porter @ 2021-06-23 21:00 UTC (permalink / raw)
  To: 49197

[-- Attachment #1: Type: text/plain, Size: 1099 bytes --]

This is a very minor issue that I happened to stumble upon. On systems
without "diff", adding a new font-lock rule via
`font-lock-add-keywords' doesn't do anything. This is (if I'm
understanding things correctly) because the diff-refined code will
signal an error, terminating the font-lock code early. Since
`diff--font-lock-refined' is normally the last element in the list,
this only causes issues when a user has added more font-lock rules. To
see the problem in action, you can do the following:

  emacs -Q
  (setq diff-command "nonexist")
  (add-hook 'diff-mode-hook
            (lambda ()
              (font-lock-add-keywords
               nil
               '(("^..*?\\([[:blank:]]+\\)$" 1
                  'trailing-whitespace prepend))
               t)))
  C-x C-f diff-trailing-ws.patch ;; Attached
  ;; The added line should be highlighted with `trailing-whitespace',
  ;; but it isn't.

I've attached a patch for this which ignores `file-missing' errors
when using diff-refined during font-locking. With the patch, running
the above steps correctly highlights trailing whitespace.

[-- Attachment #2: 0001-Ignore-file-missing-errors-during-diff-refined-font-.patch --]
[-- Type: application/octet-stream, Size: 1978 bytes --]

From 5154533da7af1e07d1ce2630cbae5c4892eeb449 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Wed, 23 Jun 2021 13:52:54 -0700
Subject: [PATCH] Ignore file-missing errors during diff-refined font-locking

* lisp/vc/diff-mode.el (diff--font-lock-refined): Ignore file-missing
errors.
---
 lisp/vc/diff-mode.el | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index a0093391c6..4652afa1f9 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -2265,17 +2265,20 @@ diff--font-lock-refined
       ;; same hunk.
       (goto-char (next-single-char-property-change
                   (point) 'diff--font-lock-refined nil max)))
-    (diff--iterate-hunks
-     max
-     (lambda (beg end)
-       (unless (get-char-property beg 'diff--font-lock-refined)
-         (diff--refine-hunk beg end)
-         (let ((ol (make-overlay beg end)))
-           (overlay-put ol 'diff--font-lock-refined t)
-           (overlay-put ol 'diff-mode 'fine)
-           (overlay-put ol 'evaporate t)
-           (overlay-put ol 'modification-hooks
-                        '(diff--overlay-auto-delete))))))))
+    ;; Ignore errors that diff cannot be found so that custom font-lock
+    ;; keywords after `diff--font-lock-refined' can still be evaluated.
+    (ignore-error file-missing
+      (diff--iterate-hunks
+       max
+       (lambda (beg end)
+         (unless (get-char-property beg 'diff--font-lock-refined)
+           (diff--refine-hunk beg end)
+           (let ((ol (make-overlay beg end)))
+             (overlay-put ol 'diff--font-lock-refined t)
+             (overlay-put ol 'diff-mode 'fine)
+             (overlay-put ol 'evaporate t)
+             (overlay-put ol 'modification-hooks
+                          '(diff--overlay-auto-delete)))))))))
 
 (defun diff--overlay-auto-delete (ol _after _beg _end &optional _len)
   (delete-overlay ol))
-- 
2.25.1


[-- Attachment #3: diff-trailing-ws.patch --]
[-- Type: application/octet-stream, Size: 123 bytes --]

--- pre.txt	2021-06-23 13:40:48.850659087 -0700
+++ post.txt	2021-06-23 13:40:53.942533907 -0700
@@ -1 +1 @@
-text
+text  

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#49197: [PATCH] 28.0.50; Inability to find "diff" prevents `font-lock-add-keywords' from working in `diff-mode'
  2021-06-23 21:00 bug#49197: [PATCH] 28.0.50; Inability to find "diff" prevents `font-lock-add-keywords' from working in `diff-mode' Jim Porter
@ 2021-06-24 16:07 ` Lars Ingebrigtsen
  0 siblings, 0 replies; 2+ messages in thread
From: Lars Ingebrigtsen @ 2021-06-24 16:07 UTC (permalink / raw)
  To: Jim Porter; +Cc: 49197

Jim Porter <jporterbugs@gmail.com> writes:

> I've attached a patch for this which ignores `file-missing' errors
> when using diff-refined during font-locking. With the patch, running
> the above steps correctly highlights trailing whitespace.

Makes sense to me.  Pushed to Emacs 28 now.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-06-24 16:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-23 21:00 bug#49197: [PATCH] 28.0.50; Inability to find "diff" prevents `font-lock-add-keywords' from working in `diff-mode' Jim Porter
2021-06-24 16:07 ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).