Specifying only the new background color for the keyword in font-lock-keywords

I would like to highlight some parts of the code with a specific background color, but I would like the rest of the code highlighting to stay in place. Ie, keywords should still be highlighted using font lock as keywords, only their background should change.

I am currently doing this with font-lock-add-keywords, where regexp matches what I want to highlight, and to each match it adds a face defined like this:

(:background "#d1eaff")

But when he adds this face, he does not preserve previous attributes, such as the foreground color, and simply uses the default face with this background color.

Is there a way to make it use the current faces and replace only their background colors with a new one? How to add a new attribute to existing faces?

+3
source share
1 answer

What you are looking for is this overlays.

The following code fragment will add a face compilation-errorto characters 1 through 3 in the current buffer:

(overlay-put (make-overlay 1 4)
             'face 'compilation-error)

Instead of numbers, you can put the search results in a regular expression or whatever you used to determine the boundaries of your overlay.

+2
source

All Articles