Oleg Pavliv
Oleg Pavliv

Reputation: 21162

set text properties

I want to copy a text from one buffer to another with text properties. So I have

(with-current-buffer from-buffer
  (setq text-to-copy (buffer-substring beg end)))

How can I insert the text-to-copy to another buffer with all text properties? I'm interested especially in 'face' properties.

The function buffer-substring returns a list, for example ("substring" 42 51 (face font-lock-keyword-face) 52 59 (face font-lock-function-name-face))

If I pass this list to (insert text-to-copy) it seems that it ignores text properties

Upvotes: 1

Views: 1437

Answers (3)

huaiyuan
huaiyuan

Reputation: 26539

If font-lock-mode is turned on in the target buffer of insertion, the face property will be reset once the fontification kicks in. I think you'll need to either turn off font-lock-mode, or munge the text properties to replace 'face' with 'font-lock-face' before insertion.

Upvotes: 2

Joe Casadonte
Joe Casadonte

Reputation: 16859

That should work. This is from Emacs 23.1.1:

buffer-substring is a built-in function in `C source code'.

(buffer-substring start end)

Return the contents of part of the current buffer as a string.
The two arguments start and end are character positions;
they can be in either order.
The string returned is multibyte if the buffer is multibyte.

This function copies the text properties of that part of the buffer
into the result string; if you don't want the text properties,
use `buffer-substring-no-properties' instead.

You can use the command describe-text-properties interactively to see what it is you actually got:

describe-text-properties is an interactive compiled Lisp function in
`descr-text.el'.

It is bound to <C-down-mouse-2> <dp>, <menu-bar> <edit> <props> <dp>.
(describe-text-properties pos &optional output-buffer)

Describe widgets, buttons, overlays and text properties at pos.
Interactively, describe them for the character after point.
If optional second argument output-buffer is non-nil,
insert the output into that buffer, and don't initialize or clear it
otherwise.

Upvotes: 0

NikkiA
NikkiA

Reputation: 1364

The 'insert' function should handle strings that include text-properties, as-is. Since buffer-substring by default will return a string with text-properties if present, '(insert text-to-copy)' should be all you need to do.

If on the other hand you wanted to extract the string without the text-properties, you'd want to be using buffer-substring-no-properties instead

Upvotes: 0

Related Questions