【问题标题】:set text properties设置文本属性
【发布时间】:2009-11-24 07:24:34
【问题描述】:

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

(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),它似乎会忽略文本属性

【问题讨论】:

    标签: emacs elisp emacs-faces


    【解决方案1】:

    如果在插入的目标缓冲区中打开font-lock-mode,一旦字体化启动,face 属性将被重置。我认为您需要关闭font-lock-mode,或者修改要替换的文本属性'face' 和 'font-lock-face' 在插入之前。

    【讨论】:

      【解决方案2】:

      insert”函数应按原样处理包含文本属性的字符串。由于buffer-substring 默认情况下将返回一个带有文本属性的字符串(如果存在),因此您需要做的就是“(insert text-to-copy)”。

      另一方面,如果您想提取字符串没有文本属性,您可能想改用buffer-substring-no-properties

      【讨论】:

      • 感谢您的回答。似乎 (insert) 没有考虑到文本属性。他们在(插入)的文档中也这么说
      • 我刚刚检查过,你是对的......但我的答案是基于 elisp 手册中关于文本属性的评论(我昨天读完后记得,以处理我正在做的事情中的文本属性):“在字符串和缓冲区之间复制文本会保留属性以及字符;这包括诸如子字符串、插入和缓冲区子字符串等多种功能。”所以,奇怪的、相互矛盾的信息......我想剩下的唯一选择是用 get-text-propertyset-text-property 迭代结果 - 虽然看起来并不优雅
      【解决方案3】:

      应该可以。这是来自 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.
      

      【讨论】:

      • 我需要将从(缓冲区子字符串)返回的文本属性应用到插入的文本
      猜你喜欢
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多