仅打印文本丢弃文本属性

时间:2011-12-04 02:11:00

标签: emacs elisp

我有以下函数来打印指向* scratch *缓冲区的行,

(defun print-line ()
  (print (thing-at-point 'line) (get-buffer "*scratch*")))

但它甚至打印出像这样的已知信息

#(" OFFICE
" 0 2 (fontified t org ...

如何放弃已完成信息的打印。

3 个答案:

答案 0 :(得分:16)

扩展Daimrod提到的buffer-substring-no-properties ......

M-x apropos RET no-properties RET

buffer-substring-no-properties
  Function: Return the characters of part of the buffer, without the
            text properties.
field-string-no-properties
  Function: Return the contents of the field around POS, without text
            properties.
insert-buffer-substring-no-properties
  Function: Insert before point a substring of BUFFER, without text
            properties.
match-string-no-properties
  Function: Return string of text matched by last search, without text
            properties.
minibuffer-contents-no-properties
  Function: Return the user input in a minibuffer as a string, without
            text-properties.
substring-no-properties
  Function: Return a substring of STRING, without text properties.

您可以阅读手册中的文字属性:

M - :(info“(elisp)Text Properties”) RET

答案 1 :(得分:8)

在操作org-table中的字符串时,我需要eredis类似的东西。您可以在显示字符串时使用`set-text-properties'删除它们。

(defun strip-text-properties(txt)
  (set-text-properties 0 (length txt) nil txt)
      txt)

(defun print-line ()
 (print (strip-text-properties 
         (thing-at-point 'line))
    (get-buffer "*scratch*")))

答案 2 :(得分:1)

我尝试过一些东西,但这很奇怪,我真的不明白文字属性是如何运作的。

例如:

(type-of (thing-at-point 'line)) => string

正如你所说,如果有人试图打印它,那么也会打印属性,但是如果有人试图插入它:

(insert (format "%s" (thing-at-point 'line)))

仅打印字符串,而不是属性。

所以在我看来,这些属性只是绑定到字符串,但你可以照常操作字符串:

(lenght (thing-at-point 'line))
(substring (thing-at-point 'line) 0 2)

但是,如果您想要的只是行,而只有行,您可以使用buffer-substring-no-properties

(defun print-line ()     
  (print (buffer-substring-no-properties (point-at-bol) (point-at-eol))))