Håkon Hægland
Håkon Hægland

Reputation: 40718

Setting text properties in Emacs when using font-lock-defaults

I am trying to set some background text properties in my Emacs buffer. For instance,

(set-text-properties pos1 pos2 '(face '(:background "cyan")))

But I cannot get it to work. I think I have a clue why it does not work: I am also using a major mode that uses (setq font-lock-defaults '((some-font-lock-keywords))) If I comment out that assignment to font-lock-defaults it works fine.

Upvotes: 4

Views: 1442

Answers (3)

Håkon Hægland
Håkon Hægland

Reputation: 40718

I found the following to also work: Replace 'font with 'font-lock-face :

(set-text-properties pos1 pos2 '(font-lock-face '(:background "cyan")))

or maybe better: (as suggested by @Drew and @abo-abo)

(put-text-property pos1 pos2 'font-lock-face '(:background "cyan")) 

then there seems to be no need to set (inhibit-modification-hooks t) first..

See http://www.gnu.org/software/emacs/manual/html_node/elisp/Precalculated-Fontification.html

Upvotes: 2

Drew
Drew

Reputation: 30701

Wrt your code: Drop the second quote mark. And be aware that set-text-properties replaces all existing text properties. You might want to only specify the face property, without erasing other properties. See (elisp) Changing Properties.

And yes, it is likely that font lock interferes here -- that is typically the case when you apply a face property. Look at the code in various highlighting libraries to see how you can work around this. But generally, if you want font locking then you should do your highlighting via font-lock also.

Upvotes: 1

abo-abo
abo-abo

Reputation: 20342

Here's a bit of code that you can paste into *scratch*:

(let ((inhibit-modification-hooks t))
  (make-face 'temp-face)
  (set-face-background 'temp-face "cyan")
  (put-text-property 1 50 'face 'temp-face))

Upvotes: 3

Related Questions