org-latex-preview: Fix snippet preamble hash

* lisp/org-latex-preview.el (org-latex-preview--create-tex-file,
org-latex-preview--hash, org-latex-preview-place,
org-latex-preview-clear-cache): The only the default preamble was being
hashed, which led to incorrect results across documents.  To correctly
hash the preamble, it is now determined at the start of
`org-latex-preview-place', and used in `org-latex-preview--hash',
`org-latex-preview--create-tex-file', and
`org-latex-preview-clear-cache'.
This commit is contained in:
TEC 2023-03-14 09:42:22 +08:00
parent 4ec83eb21e
commit fed2ebc16a
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
2 changed files with 18 additions and 12 deletions

View File

@ -947,6 +947,11 @@ Each entry of ENTRIES should be a list of 2-3 items, either
(BEG END VALUE) (BEG END VALUE)
Where BEG and END are the positions in the buffer, and the LaTeX previewed Where BEG and END are the positions in the buffer, and the LaTeX previewed
is either the substring between BEG and END or (when provided) VALUE." is either the substring between BEG and END or (when provided) VALUE."
(unless latex-preamble
(setq latex-preamble
(or org-latex-preview--preamble-content
(setq org-latex-preview--preamble-content
(org-latex-preview--get-preamble)))))
(let* ((processing-info (let* ((processing-info
(cdr (assq processing-type org-latex-preview-process-alist))) (cdr (assq processing-type org-latex-preview-process-alist)))
(imagetype (or (plist-get processing-info :image-output-type) "png")) (imagetype (or (plist-get processing-info :image-output-type) "png"))
@ -960,7 +965,7 @@ is either the substring between BEG and END or (when provided) VALUE."
(`(,fg ,bg) (org-latex-preview--colors-around beg end)) (`(,fg ,bg) (org-latex-preview--colors-around beg end))
(number (car (setq numbering-offsets (cdr numbering-offsets)))) (number (car (setq numbering-offsets (cdr numbering-offsets))))
(hash (org-latex-preview--hash (hash (org-latex-preview--hash
processing-type value imagetype fg bg number)) processing-type latex-preamble value imagetype fg bg number))
(options (org-combine-plists (options (org-combine-plists
org-latex-preview-options org-latex-preview-options
(list :foreground fg (list :foreground fg
@ -1029,7 +1034,7 @@ Faces in `org-latex-preview--ignored-faces' are ignored."
((symbolp (car face)) ; Spec like (org-level-1 default). ((symbolp (car face)) ; Spec like (org-level-1 default).
(face-attribute (car face) attr nil (append (cdr face) '(default)))))) (face-attribute (car face) attr nil (append (cdr face) '(default))))))
(defun org-latex-preview--hash (processing-type string imagetype fg bg &optional number) (defun org-latex-preview--hash (processing-type preamble string imagetype fg bg &optional number)
"Return a SHA1 hash for referencing LaTeX fragments when previewing them. "Return a SHA1 hash for referencing LaTeX fragments when previewing them.
PROCESSING-TYPE is the type of process used to create the PROCESSING-TYPE is the type of process used to create the
@ -1049,9 +1054,7 @@ image.
NUMBER is the equation number that should be used, if applicable." NUMBER is the equation number that should be used, if applicable."
(sha1 (prin1-to-string (sha1 (prin1-to-string
(list processing-type (list processing-type
org-latex-preview-preamble preamble
org-latex-default-packages-alist
org-latex-packages-alist
org-latex-preview-options org-latex-preview-options
string string
(if (equal imagetype "svg") (if (equal imagetype "svg")
@ -1365,10 +1368,7 @@ FRAGMENTS will be placed in order, wrapped within a
The path of the created LaTeX file is returned." The path of the created LaTeX file is returned."
(let* ((header (let* ((header
(concat (concat
(or (plist-get processing-info :latex-header) (plist-get processing-info :latex-header)
org-latex-preview--preamble-content
(setq org-latex-preview--preamble-content
(org-latex-preview--get-preamble)))
(let ((w org-latex-preview-width)) (let ((w org-latex-preview-width))
(cond (cond
((stringp w) ((stringp w)
@ -2030,6 +2030,7 @@ the *entire* preview cache will be cleared, and `org-persist-gc' run."
(org-latex-preview--remove-cached (org-latex-preview--remove-cached
(org-latex-preview--hash (org-latex-preview--hash
org-latex-preview-default-process org-latex-preview-default-process
org-latex-preview--preamble-content
value imagetype fg bg number)))) value imagetype fg bg number))))
(message "Cleared LaTeX preview cache for %s." (message "Cleared LaTeX preview cache for %s."
(if (or beg end) "region" "buffer"))))) (if (or beg end) "region" "buffer")))))

View File

@ -3042,7 +3042,11 @@ CONTENTS is nil. INFO is a plist holding contextual information."
"Make sure that appropriate preview images exist for all LaTeX "Make sure that appropriate preview images exist for all LaTeX
TODO." TODO."
(when (assq (plist-get info :with-latex) org-latex-preview-process-alist) (when (assq (plist-get info :with-latex) org-latex-preview-process-alist)
(let* ((elements (let* ((latex-preamble
(or org-latex-preview--preamble-content
(setq org-latex-preview--preamble-content
(org-latex-preview--get-preamble))))
(elements
(org-element-map parse-tree (org-element-map parse-tree
'(latex-fragment latex-environment) '(latex-fragment latex-environment)
#'identity #'identity
@ -3069,7 +3073,7 @@ TODO."
(bg (plist-get html-options :background)) (bg (plist-get html-options :background))
(number (car (setq numbering-offsets (cdr numbering-offsets)))) (number (car (setq numbering-offsets (cdr numbering-offsets))))
(hash (org-latex-preview--hash (hash (org-latex-preview--hash
processing-type value imagetype fg bg number)) processing-type latex-preamble value imagetype fg bg number))
(options (org-combine-plists (options (org-combine-plists
org-latex-preview-options org-latex-preview-options
html-options html-options
@ -3091,7 +3095,8 @@ TODO."
(apply #'org-async-wait-for (apply #'org-async-wait-for
(org-latex-preview--create-image-async (org-latex-preview--create-image-async
processing-type processing-type
(nreverse fragment-info))))) (nreverse fragment-info)
:latex-preamble latex-preamble))))
(plist-put info :html-latex-preview-hash-table element-hash-table) (plist-put info :html-latex-preview-hash-table element-hash-table)
nil))) nil)))