Changing text appearance in Emacs: overlays work, text properties don't
Asked Answered
C

2

11

I've been experimenting today with text properties in Emacs. If I position the cursor on a line with some text on it and then execute the following code with M-:, the line is redisplayed in bold.

(overlay-put
 (make-overlay
  (line-beginning-position)
  (line-end-position))
 'face 'bold)

If, however, I wipe out the overlay with (remove-overlays) and execute the following code, nothing happens (except that the word "nil" appears in the minibuffer).

(put-text-property
 (line-beginning-position)
 (line-end-position)
 'face 'bold)

From what I've gleaned so far, I'd expect that these two snippets should produce the same visual results. Why don't they?

Cusack answered 11/2, 2010 at 4:28 Comment(3)
Works for me with Emacs 23.1, with text in a buffer in both fundamental-mode and text-mode.Overweary
22.1.50 isn't an official build, I'd try grabbing an official release from homepage.mac.com/zenitani/emacs-e.htmlOverweary
I downloaded and installed the official release, and the same problem occurred. But I chanced upon the answer regardless. I opened a new empty buffer and typed some random gibberish, and setting text properties worked there. The buffer I tried all my experiments in originally was in xml-mode (or sgml-mode, whatever). So at least I can apply text properties now, but it's still a mystery to me how (and why) certain modes can countermand put-text-property.Cusack
P
6

When font-lock-mode is on, the face attribute will be overridden. Try font-lock-face instead:

(put-text-property
 (line-beginning-position)
 (line-end-position)
 'font-lock-face 'bold)
Paste answered 11/2, 2010 at 10:7 Comment(0)
E
2

ansi-color.el -- "In Emacs, however, things are a bit different: When font-lock is active in a buffer, you cannot simply add face text-properties to the buffer. Font-lock will remove the face text-property using 'font-lock-unfontify-region-function'. If you want to insert the strings returned by 'ansi-color-apply' into such buffers, you must set 'font-lock-unfontify-region-function' to `ansi-color-unfontify-region'. This function will not remove all face text-properties unconditionally. It will keep the face text-properties if the property 'ansi-color' is set.

Eternal answered 7/3, 2011 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.