5

For instance, why does (insert #("abc" 0 3 (face (:foreground "red")))) not insert red text?

1
  • I'll let another forum participant explain the "why" ... But, how about using?: (insert (propertize "abc" 'face '(:foreground "red"))) See also add-text-properties and put-text-property as alternative methods to colorize text ...; and, this can also be done with overlays if so desired ...
    – lawlist
    Commented Mar 18, 2020 at 19:05

1 Answer 1

7

You probably try that in the *scratch* buffer or any other buffer with active font-lock-mode.

In such a buffer the faces are immediately adapted to the rules prescribed by the variable font-lock-keywords.

Use the property font-lock-face instead of face in those buffers.

The modified version of your example would be:

(insert #("abc" 0 3 (font-lock-face (:foreground "red"))))

font-lock-face properties are directly translated into face properties by font-lock-mode. No fontification rules are applied to those stretches of text.

Side-note: lisp-interaction-mode is derived from emacs-lisp-mode that activates font-lock-mode.

2
  • 1
    It's really easy to forget this.
    – Drew
    Commented Mar 18, 2020 at 23:11
  • Indeed, I tried it in the *scratch* buffer. Commented Mar 21, 2020 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.