unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: 53155@debbugs.gnu.org
Subject: bug#53155: Use overlays in hi-lock
Date: Mon, 10 Jan 2022 10:17:15 +0200	[thread overview]
Message-ID: <86zgo4ovmw.fsf@mail.linkov.net> (raw)

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

When diff-refine overlays take precedence over hi-lock text properties,
then hi-lock can't be used to review diffs.  Hi-lock provides two modes:
font-lock highlighting and highlighting with overlays.  Using overlays
solves the problem.  But how to decide when overlays should be used?
Detecting whether the buffer is read-only, so no more text is expected
to be added to the buffer?  Then add overlays instead of using font-lock?
But this heuristics doesn't work in read-only *vc-change-log* buffers
where expanding commits inserts new text that should be highlighted
with font-lock.

Thus the following patch adds a new variable hi-lock-use-overlays
that can be used like:

#+begin_src emacs-lisp
(add-hook 'diff-mode-hook
            (lambda ()
              (setq-local hi-lock-use-overlays t)))
#+end_src


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: hi-lock-use-overlays.patch --]
[-- Type: text/x-diff, Size: 2035 bytes --]

diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el
index fbd698e234..99e908ad07 100644
--- a/lisp/hi-lock.el
+++ b/lisp/hi-lock.el
@@ -738,6 +738,17 @@ hi-lock-read-face-name
       (add-to-list 'hi-lock-face-defaults face t))
     (intern face)))
 
+(defvar hi-lock-use-overlays nil
+  "Whether to always use overlays instead of font-lock rules.
+When font-lock-mode is enabled and the buffer specifies font-lock rules,
+highlighting is performed by adding new font-lock rules to the existing ones,
+so when new matching strings are added, they are highlighted by font-lock.
+Otherwise, overlays are used, but new highlighting overlays are not added
+when new matching strings are inserted to the buffer.
+However, sometimes overlays are still preferable even in buffers
+where font-lock is enabled, because hi-lock overlays take precedence
+over other overlays in the same buffer, such as diff-refine overlays, etc.")
+
 (defun hi-lock-set-pattern (regexp face &optional subexp lighter case-fold spaces-regexp)
   "Highlight SUBEXP of REGEXP with face FACE.
 If omitted or nil, SUBEXP defaults to zero, i.e. the entire
@@ -759,7 +770,8 @@ hi-lock-set-pattern
         (add-to-list 'hi-lock--unused-faces (face-name face))
       (push pattern hi-lock-interactive-patterns)
       (push (cons (or lighter regexp) pattern) hi-lock-interactive-lighters)
-      (if (and font-lock-mode (font-lock-specified-p major-mode))
+      (if (and font-lock-mode (font-lock-specified-p major-mode)
+               (not hi-lock-use-overlays))
 	  (progn
 	    (font-lock-add-keywords nil (list pattern) t)
 	    (font-lock-flush))
@@ -781,6 +793,7 @@ hi-lock-set-pattern
                                            (match-end subexp))))
                 (overlay-put overlay 'hi-lock-overlay t)
                 (overlay-put overlay 'hi-lock-overlay-regexp (or lighter regexp))
+                (overlay-put overlay 'priority 1)
                 (overlay-put overlay 'face face))
               (goto-char (match-end 0)))
             (when no-matches

             reply	other threads:[~2022-01-10  8:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-10  8:17 Juri Linkov [this message]
2022-01-13  9:08 ` bug#53155: Use overlays in hi-lock Lars Ingebrigtsen
2022-01-24 19:17   ` Juri Linkov
2022-01-25 11:59     ` Lars Ingebrigtsen

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=86zgo4ovmw.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=53155@debbugs.gnu.org \
    /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 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).