org-latex-preview: More robust face resolving

* lisp/org-latex-preview.el (org-latex-preview--colors-at,
org-latex-preview--resolved-faces-attr): Just using `face-attribute' on
the first face spec given when the face text property is a list is too
naive.  To do this correctly, a new function
`org-latex-preview--resolved-faces-attr' is introduced which takes care
of the annoying semantics, and used in `org-latex-preview--colors-at' to
better resolve the face attribute.
This commit is contained in:
TEC 2023-01-06 23:56:04 +08:00
parent 3e54ddd45c
commit 330d5a49ab
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 22 additions and 5 deletions

View File

@ -851,19 +851,36 @@ Some of the options can be changed using the variable
(let* ((face (or (and (> pos 1)
(get-text-property (1- pos) 'face))
'default))
(faces (if (consp face)
(append face '(default))
(list face 'default)))
(fg (pcase (plist-get org-latex-preview-options :foreground)
('auto (face-attribute (car faces) :foreground nil (cdr faces)))
('auto
(org-latex-preview--resolved-faces-attr face :foreground))
('default (face-attribute 'default :foreground nil))
(color color)))
(bg (pcase (plist-get org-latex-preview-options :background)
('auto (face-attribute (car faces) :background nil (cdr faces)))
('auto
(org-latex-preview--resolved-faces-attr face :background))
('default (face-attribute 'default :background nil))
(color color))))
(list fg bg)))
(defun org-latex-preview--resolved-faces-attr (face attr)
"Find ATTR of the FACE text property.
This is surprisingly complicated given the various forms of output
\\=(get-text-property pos \\='face) can produce."
(if (consp face) ; Spec is a list of face-specs.
(cond
((keywordp (car face)) ; Spec like (:inherit org-block :extend t).
(or (plist-get face attr)
(face-attribute 'default attr)))
((consp (car face)) ; Spec like ((:inherit default :extend t) org-block).
(or (plist-get (car face) attr)
(face-attribute (cadr face) attr nil
(append (cddr face) '(default)))))
((symbolp (car face)) ; Spec like (org-level-1 default).
(face-attribute (car face) attr nil (append (cdr face) '(default)))))
;; A single-face spec, like org-level-1.
(face-attribute face attr nil 'default)))
(defun org-latex-preview--hash (processing-type string imagetype fg bg)
"Return a SHA1 hash for referencing LaTeX fragments when previewing them.