org-latex-preview: Use org-persist for .fmt cache

* lisp/org-latex-preview.el (org-latex-preview-precompile,
org-latex-preview--create-tex-file): Cache compiled ".fmt" format dump
files with org-persist, instead of using predictably named tempfiles.
`org-latex-preview--precompile-preamble' is introduced to seperate out
the compilation process from the org-persist interaction.  As a result
of the new changes, the ".fmt" extension is also returned now, and so it
must be stripped in `org-latex-preview--create-tex-file'.
This commit is contained in:
TEC 2023-02-12 15:20:30 +08:00
parent 4e623d4593
commit 688427138d
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 77 additions and 60 deletions

View File

@ -1486,7 +1486,7 @@ The path of the created LaTeX file is returned."
(insert (if-let ((format-file (insert (if-let ((format-file
(and org-latex-preview-use-precompilation (and org-latex-preview-use-precompilation
(org-latex-preview-precompile processing-info header)))) (org-latex-preview-precompile processing-info header))))
(concat "%& " format-file) (concat "%& " (file-name-sans-extension format-file))
header)) header))
;; The \abovedisplayskip length must be set after \begin{document} because ;; The \abovedisplayskip length must be set after \begin{document} because
;; it is usually set during the font size intialisation that occurs at ;; it is usually set during the font size intialisation that occurs at
@ -2031,71 +2031,88 @@ the *entire* preview cache will be cleared, and `org-persist-gc' run."
(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")))))
(defun org-latex-preview-precompile (processing-info header) (defun org-latex-preview-precompile (processing-info preamble)
"Precompile/dump LaTeX HEADER (preamble) text. "Precompile/dump LaTeX PREAMBLE text.
This dump is named using its sha1 hash and placed in The path to the format file (.fmt) is returned. If the format
`temporary-file-directory', and the name is returned. If a dump file could not be found in the persist cache, it is generated
file with this name already exists, simply return the name. according to PROCESSING-INFO and stored.
This is intended to speed up Org's LaTeX preview generation This is intended to speed up Org's LaTeX preview generation
process." process."
(let* ((header-hash (let ((preamble-hash
(thread-first (thread-first
header preamble
(concat (concat
(prin1-to-string (prin1-to-string
(car (plist-get processing-info :programs))) (car (plist-get processing-info :programs)))
(plist-get processing-info :latex-processor) (plist-get processing-info :latex-processor)
(and (string-match-p "\\(?:\\\\input{\\|\\\\include{\\)" (and (string-match-p "\\(?:\\\\input{\\|\\\\include{\\)"
header) preamble)
default-directory)) default-directory))
(sha1) (sha1))))
(substring 0 12))) (or (cadr
(header-no-ext (org-persist-read "LaTeX format file cache"
(expand-file-name header-hash temporary-file-directory)) (list :key preamble-hash)
(dump-file (concat header-no-ext ".fmt")) nil nil :read-related t))
(header-file (concat header-no-ext ".tex")) (when-let ((dump-file
(precompile-buffer (org-latex-preview--precompile-preamble
(with-current-buffer processing-info preamble
(get-buffer-create org-latex-preview--precompile-log) (expand-file-name preamble-hash temporary-file-directory))))
(erase-buffer) (cadr
(current-buffer)))) (org-persist-register `(,"LaTeX format file cache"
(if (file-exists-p dump-file) (file ,dump-file))
header-no-ext (list :key preamble-hash)
(with-temp-file header-file :write-immediately t))))))
(insert header "\n\\endofdump\n"))
(message "Precompiling Org LaTeX Preview preamble...") (defun org-latex-preview--precompile-preamble (processing-info preamble basepath)
(condition-case file "Precompile PREAMBLE with \"mylatexformat\".
(org-compile-file The PREAMBLE string is placed in BASEPATH.tex and compiled
header-file (plist-get processing-info :latex-precompiler) according to PROCESSING-INFO. If compilation and dumping
"fmt" nil precompile-buffer succeeded, BASEPATH.fmt will be returned.
(let ((org-tex-compiler
(cdr (assoc (plist-get processing-info :latex-processor) Should any errors occur during compilation, nil will be returned,
org-latex-preview-compiler-command-map)))) and appropriate warnings may be emitted."
`((?l . ,org-tex-compiler) (let ((dump-file (concat basepath ".fmt"))
(?L . ,(car (split-string org-tex-compiler)))))) (preamble-file (concat basepath ".tex"))
(:success (precompile-buffer
(kill-buffer precompile-buffer) (with-current-buffer
(delete-file header-file) (get-buffer-create org-latex-preview--precompile-log)
header-no-ext) (erase-buffer)
(error (current-buffer))))
(unless (= 0 (call-process "kpsewhich" nil nil nil "mylatexformat.ltx")) (with-temp-file preamble-file
(display-warning (insert preamble "\n\\endofdump\n"))
'(org latex-preview preamble-precompilation) (message "Precompiling Org LaTeX Preview preamble...")
"The LaTeX package \"mylatexformat\" is required for precompilation, but could not be found") (condition-case nil
:warning) (org-compile-file
(unless (= 0 (call-process "kpsewhich" nil nil nil "preview.sty")) preamble-file (plist-get processing-info :latex-precompiler)
(display-warning "fmt" nil precompile-buffer
'(org latex-preview preamble-precompilation) (let ((org-tex-compiler
"The LaTeX package \"preview\" is required for precompilation, but could not be found") (cdr (assoc (plist-get processing-info :latex-processor)
:warning) org-latex-preview-compiler-command-map))))
`((?l . ,org-tex-compiler)
(?L . ,(car (split-string org-tex-compiler))))))
(:success
(kill-buffer precompile-buffer)
(delete-file preamble-file)
dump-file)
(error
(unless (= 0 (call-process "kpsewhich" nil nil nil "mylatexformat.ltx"))
(display-warning (display-warning
'(org latex-preview preamble-precompilation) '(org latex-preview preamble-precompilation)
(format "Failed to precompile preamble, see the \"%s\" buffer." "The LaTeX package \"mylatexformat\" is required for precompilation, but could not be found")
precompile-buffer) :warning)
:warning) (unless (= 0 (call-process "kpsewhich" nil nil nil "preview.sty"))
nil))))) (display-warning
'(org latex-preview preamble-precompilation)
"The LaTeX package \"preview\" is required for precompilation, but could not be found")
:warning)
(display-warning
'(org latex-preview preamble-precompilation)
(format "Failed to precompile preamble (%s), see the \"%s\" buffer."
preamble-file precompile-buffer)
:warning)
nil))))
(defun org-latex-preview--tex-styled (processing-type value options &optional html-p) (defun org-latex-preview--tex-styled (processing-type value options &optional html-p)
"Apply LaTeX style commands to VALUE based on OPTIONS. "Apply LaTeX style commands to VALUE based on OPTIONS.