设置文本属性

时间:2009-11-24 07:24:34

标签: emacs elisp emacs-faces

我想使用文本属性将文本从一个缓冲区复制到另一个缓冲区。所以我有

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

如何将文本到副本插入到具有所有文本属性的另一个缓冲区?我特别感兴趣的是“面子”属性。

函数buffer-substring返回一个列表,例如("substring" 42 51 (face font-lock-keyword-face) 52 59 (face font-lock-function-name-face))

如果我将此列表传递给(insert text-to-copy),它似乎忽略了文本属性

3 个答案:

答案 0 :(得分:2)

如果在插入的目标缓冲区中启用了font-lock-mode,则一旦启用了fontification,就会重置face属性。我认为你需要关闭font-lock-mode,或者munge在插入之前用'font-lock-face'替换'face'的文本属性。

答案 1 :(得分:0)

'insert'函数应该按原样处理包含text-properties的字符串。由于默认情况下buffer-substring会返回带有text-properties(如果存在)的字符串,因此您应该只需要“(insert text-to-copy)”。

另一方面,如果你想提取字符串而不是文本属性,那么你想要使用buffer-substring-no-properties而不是

答案 2 :(得分:0)

那应该有用。这来自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.

您可以交互使用命令describe-text-properties来查看它实际上是什么:

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.