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

StackOverflow https://stackoverflow.com/questions/10201408

  •  01-06-2021
  •  | 
  •  

سؤال

I would like to highlight some portions of code with a specific background color, but I would like the rest of code highlighting to remain in place. I.e., keywords should still be highlighted by font-lock as keywords, only their background should change.

At the moment, I am doing it with font-lock-add-keywords, where a regexp matches whatever I want to highlight and to every match it prepends a face defined like so:

(:background "#d1eaff")

But when it adds this face, it doesn't preserve the previous attributes like foreground color, and just uses the default face with this background color.

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

هل كانت مفيدة؟

المحلول

What you are looking for are overlays.

The following code snippet will add the face compilation-error on characters 1 through 3 in the current buffer:

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

Instead of the numbers you can put the results of a regexp search or anything that you used to determine the boundaries of your overlay.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top