4

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

  • 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 Mar 18 at 19:05
6

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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