我想做相当于的elisp

 <font color="red">normal<b>bold</b></font>

我试过了
 (propertize (concat "normal"
               (propertize "bold" 'font-lock-face
                                  '(:weight bold)))
  'font-lock-face '(:foreground "red"))

然而,“红色”属性覆盖了“粗体”属性,我最终得到
#("normalbold" 0 6
  (font-lock-face
   (:foreground "red"))
  6 10
  (font-lock-face
   (:foreground "red")))

可行吗?

谢谢!

最佳答案

我不认为嵌套可以用 elisp 提供的功能来完成。 The docs 建议单独对字符串的每个部分进行属性化,然后将它们连接起来:


要将不同的属性放在字符串的各个部分,您可以使用 propertyize 构造每个部分,然后将它们与 concat 组合:

      (concat
       (propertize "foo" 'face 'italic
                   'mouse-face 'bold-italic)
       " and "
       (propertize "bar" 'face 'italic
                   'mouse-face 'bold-italic))
           ⇒ #("foo and bar"
                       0 3 (face italic mouse-face bold-italic)
                       3 8 nil
                       8 11 (face italic mouse-face bold-italic))



在你的情况下,它看起来像:
(concat (propertize "normal" 'font-lock-face '(:foreground "red" ))
        (propertize "bold" 'font-lock-face '(:foreground "red" :weight bold)))

在不了解您的用例的情况下,我无法确定这对您有用。如果没有,您可以尝试使用 add-text-properties (也在文档中进行了描述),您可以使用它来有状态地修改字符串的文本属性。

关于emacs - 如何嵌套各种 `propertize` ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18168023/

10-14 13:33
查看更多