tags:

views:

91

answers:

3

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

A: 

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

NikkiA
Thanks for your answer. It seems that (insert) does not take into account text properties. They say this also in the documentation for (insert)
Oleg Pavliv
I've just checked, and you're right... but what I was basing my answer on was this comment from the elisp manual on text-properties (which I remembered after reading it just yesterday, to deal with text-properties in something I was doing):"Copying text between strings and buffers preserves the properties along with the characters; this includes such diverse functions as substring, insert, and buffer-substring."So, odd, conflicting information...I imagine the only option left is to iterate over the results with `get-text-property` and `set-text-property` - doesn't seem elegant though
NikkiA
A: 

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.
Joe Casadonte
I need to apply text properties returned from (buffer-substring) to inserted text
Oleg Pavliv
+2  A: 

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.

huaiyuan
yes, it was font-lock-mode. Thanks a lot!
Oleg Pavliv