2

In this scenario, there exists one or more faces for a particular condition named "foo". When the condition changes to the name of "bar", the goal is to add an additional face attribute such as :strike-through t. How can this be done without explicitly defining separate faces for both conditions? E.g., eliminate the need to define my-face+strike-through.

(defface my-face
  '((t (:foreground "red")))
  "Doc-string"
  :group 'my-faces)

(defface my-face+strike-through
  '((t (:foreground "red" :strike-through t)))
  "Doc-string"
  :group 'my-faces)

(let ((txt "foo"))
  (if (equal txt "bar")
    (add-text-properties 0 (length txt) (list 'face 'my-face) txt)
    (add-text-properties 0 (length txt) (list 'face 'my-face+strike-through) txt))
  txt)

1 Answer 1

2

A face spec can be a face defined with defface, or it can be a list of face attributs and their values.

You can apply multiple face specs (multiple faces) to the same text (in a string or buffer). Those multiple faces are merged - their attributes are combined.

(defvar foo "foo")
(defvar bar "bar")

(add-text-properties 0 3 (list 'face 'my-face) foo)
(add-text-properties 0 3 (list 'face '(my-face (:strike-through t))) bar)

See the Elisp manual, nodes Defining Faces and Displaying Faces. See also node Face Remapping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.