0

Given e.g. (with-temp-buffer (clojure-mode) (insert ":hello") (buffer-string)), I'd expected the result to be propertized, however it isn't.

How to get a propertized string out of a temporary buffer?

2

You're mistaken. with-temp-buffer does not remove text properties.

(with-temp-buffer
  (insert (propertize ":hello" 'face 'error))
  (buffer-string))

=> #(":hello" 0 6 (face error))

I think you're expecting the call to clojure-mode to cause the subsequent non-propertized text to instantaneously acquire text properties as it is inserted into the buffer, which is not the case. (It may seem instant when you are entering text interactively, but that's just because humans are very slow.)

Assuming you want to force font-lock-mode to process the buffer before obtaining the buffer string, use font-lock-ensure:

(with-temp-buffer
  (clojure-mode)
  (insert ":hello")
  (font-lock-ensure)
  (buffer-string))

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.