如何用propertize增加属性而不是覆盖

我有一段代码

(setq ttt (propertize "1" 'face '(:background "#FF0000")))
(setq ttt (propertize ttt 'face '(:foreground "#00FF00" )))

这段代码执行完后ttt只有foreground的属性,原来的background的属性也被覆盖了。有没有办法增加属性而不是覆盖所有属性?

(let ((buf (get-buffer-create "*test-props*")))
  (with-current-buffer buf
    (let ((str (propertize "foobar" 'face '(:background "#FF0000"))))
      (put-text-property 0 3 'face '(:foreground "#00FF00") str)
      (insert str)))
  (switch-to-buffer-other-window buf))

你这两句都是添加 face 属性,所以会覆盖。不同的属性是不会覆盖的,如:

(propertize
 (propertize "Hello" 'prop1 123)
 'prop2 456)
;; => #("Hello" 0 5 (prop1 123 prop2 456))

如果你想在原有的基础上修改 face 属性,得把原来的值取出来,如:

(let ((s (propertize "black-on-while" 'face '(:background "white"))))
  (propertize
   s
   'face
   (append '(:foreground "black") (get-text-property 0 'face s))))
;; => #("black-on-while" 0 14 (face (:foreground "black" :background "white")))
2 个赞

UPDATE:其实这样的也行

(add-face-text-property 0 (length s)
                        '(:foreground "#0000FF")
                        nil
                        temp)
1 个赞