org-latex-preview: Rework general cache variable

* lisp/org-latex-preview.el (org-latex-preview--get-cached,
org-latex-preview--table, org-latex-preview-persist,
org-latex-preview-cache): Rename and tweak `org-latex-preview-persist'
to also allow for caching to a custom directory, and (in the near
future) unify behaviour with HTML export.

* testing/lisp/test-org-latex-preview.el: Accommodate for the cache variable
change.

* etc/ORG-NEWS: Accommodate for the cache variable change.
This commit is contained in:
TEC 2024-01-08 17:16:48 +08:00
parent b415f5ef2c
commit e54cc82172
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
3 changed files with 72 additions and 64 deletions

View File

@ -536,16 +536,17 @@ If the option ~org-latex-preview-auto-track-inserts~ is non-nil (which see), pre
This alist maps compilers in ~org-latex-compilers~ to command strings This alist maps compilers in ~org-latex-compilers~ to command strings
used for LaTeX precompilation when creating previews or LaTeX exports. used for LaTeX precompilation when creating previews or LaTeX exports.
***** New option ~org-latex-preview-persist~ to enable preview image caching ***** New option ~org-latex-preview-cache~ to enable preview image caching
When non-nil, images produced using ~org-latex-preview~ will be cached and When set to =persist=, images produced using ~org-latex-preview~ will
persisted across Emacs sessions using ~org-persist~. be cached and persisted across Emacs sessions using
~org-persist~. Temporary or custom directories can also be used.
***** New option ~org-latex-preview-persist-expiry~ to set persistence period ***** New option ~org-latex-preview-persist-expiry~ to set persistence period
This is the number of days for which LaTeX preview images will be This is the number of days for which LaTeX preview images will be
cached, assuming persistence is turned on with cached, assuming persistence is turned on with
~org-latex-preview-persist~. ~org-latex-preview-cache~.
***** New option ~org-latex-preview-process-active-indicator~ to indicate preview processing for LaTeX fragments ***** New option ~org-latex-preview-process-active-indicator~ to indicate preview processing for LaTeX fragments

View File

@ -192,7 +192,7 @@ is required."
:type '(alist :tag "Compiler" :type '(alist :tag "Compiler"
:value-type (string :type "command"))) :value-type (string :type "command")))
(defcustom org-latex-preview-persist t (defcustom org-latex-preview-cache 'persist
"Persist produced LaTeX previews across Emacs sessions. "Persist produced LaTeX previews across Emacs sessions.
When non-nil, org-persist is used to cache the fragments and When non-nil, org-persist is used to cache the fragments and
@ -201,11 +201,14 @@ the data is stored in `org-latex-preview--table' for the duration
of the Emacs session." of the Emacs session."
:group 'org-latex :group 'org-latex
:package-version '(Org . "9.7") :package-version '(Org . "9.7")
:type 'boolean) :type '(choice (const :tag "Use org-mode's persistent cache system" persist)
(const :tag "Use the system temporary directory" temp)
(string :tag "Path to cache directory")))
(defcustom org-latex-preview-persist-expiry 7 (defcustom org-latex-preview-persist-expiry 7
"A homologue of `org-persist-default-expiry' for preview data. "A homologue of `org-persist-default-expiry' for preview data.
This is only relevant when `org-latex-preview-persist' is non-nil." This is only relevant when `org-latex-preview-cache' is set to
persist."
:group 'org-latex :group 'org-latex
:package-version '(Org . "9.7") :package-version '(Org . "9.7")
:type '(choice (const :tag "Never" never) :type '(choice (const :tag "Never" never)
@ -2662,44 +2665,46 @@ If this is an export run, images will only be cached, not placed."
(defvar org-latex-preview--table nil (defvar org-latex-preview--table nil
"Hash table to hold LaTeX preview image metadata. "Hash table to hold LaTeX preview image metadata.
This is only used if image caching is disabled by setting This is only used for non-persist image caching, used when
`org-latex-preview-persist' to nil.") `org-latex-preview-cache' is not set to persist.")
(defun org-latex-preview--cache-image (key path info) (defun org-latex-preview--cache-image (key path info)
"Save the image at PATH with associated INFO in the cache indexed by KEY. "Save the image at PATH with associated INFO in the cache indexed by KEY.
Return (path . info). Return (path . info).
The caching location depends on whether preview persistence is The caching location is set by CACHE, which defaults to
enabled, see `org-latex-preview-persist'." `org-latex-preview-cache'. It should be the symbol \"persist\",
(cond \"temp\", or an existing directory path as a string."
((not path) (if (not path)
(ignore (ignore
(display-warning (display-warning
'(org latex-preview put-cache) '(org latex-preview put-cache)
(format "Tried to cache %S without a path, skipping. This should not happen, please report it as a bug to the Org mailing list (M-x org-submit-bug-report)." key) (format "Tried to cache %S without a path, skipping. This should not happen, please report it as a bug to the Org mailing list (M-x org-submit-bug-report)." key)
:warning))) :warning))
(org-latex-preview-persist (pcase org-latex-preview-cache
(let ((label-path-info ('persist
(or (org-persist-read org-latex-preview--cache-name (let ((label-path-info
(list :key key) (or (org-persist-read org-latex-preview--cache-name
nil nil :read-related t) (list :key key)
(org-persist-register `(,org-latex-preview--cache-name nil nil :read-related t)
(file ,path) (org-persist-register `(,org-latex-preview--cache-name
(elisp-data ,info)) (file ,path)
(list :key key) (elisp-data ,info))
:expiry org-latex-preview-persist-expiry (list :key key)
:write-immediately t)))) :expiry org-latex-preview-persist-expiry
(cons (cadr label-path-info) info))) :write-immediately t))))
(t (cons (cadr label-path-info) info)))
(unless org-latex-preview--table ((and dir (or 'temp (pred stringp)))
(setq org-latex-preview--table (make-hash-table :test 'equal :size 240))) (unless org-latex-preview--table
(when-let ((path) (setq org-latex-preview--table (make-hash-table :test 'equal :size 240)))
(new-path (expand-file-name (when-let ((path)
(concat "org-tex-" key "." (file-name-extension path)) (new-path (expand-file-name
temporary-file-directory))) (concat "org-tex-" key "." (file-name-extension path))
(copy-file path new-path 'replace) (if (eq dir 'temp) temporary-file-directory dir))))
(puthash key (cons new-path info) (copy-file path new-path 'replace)
org-latex-preview--table))))) (puthash key (cons new-path info)
org-latex-preview--table)))
(bad (error "Invalid cache location: %S (must be persist, temp, or a string)" bad)))))
(defun org-latex-preview--get-cached (key) (defun org-latex-preview--get-cached (key)
"Retrieve the image path and info associated with KEY. "Retrieve the image path and info associated with KEY.
@ -2712,31 +2717,33 @@ Example result:
:width 7.6 :width 7.6
:depth 0.2 :depth 0.2
:errors nil)" :errors nil)"
(if org-latex-preview-persist (cond
(when-let ((label-path-info ((eq org-latex-preview-cache 'persist)
(org-persist-read org-latex-preview--cache-name (when-let ((label-path-info
(list :key key) (org-persist-read org-latex-preview--cache-name
nil nil :read-related t))) (list :key key)
(cons (cadr label-path-info) nil nil :read-related t)))
(caddr label-path-info))) (cons (cadr label-path-info)
(when org-latex-preview--table (caddr label-path-info))))
(gethash key org-latex-preview--table)))) (org-latex-preview--table
(gethash key org-latex-preview--table))))
(defun org-latex-preview--remove-cached (key) (defun org-latex-preview--remove-cached (key)
"Remove the fragment cache associated with KEY." "Remove the fragment cache associated with KEY."
(if org-latex-preview-persist (cond
(org-persist-unregister org-latex-preview--cache-name ((eq org-latex-preview-cache 'persist)
(list :key key) (org-persist-unregister org-latex-preview--cache-name
:remove-related t) (list :key key)
(when org-latex-preview--table :remove-related t))
(remhash key org-latex-preview--table) (org-latex-preview--table
(dolist (ext '("svg" "png")) (remhash key org-latex-preview--table)
(when-let ((image-file (dolist (ext '("svg" "png"))
(expand-file-name (when-let ((image-file
(concat "org-tex-" key "." ext) (expand-file-name
temporary-file-directory)) (concat "org-tex-" key "." ext)
((file-exists-p image-file))) temporary-file-directory))
(delete-file image-file)))))) ((file-exists-p image-file)))
(delete-file image-file))))))
(defun org-latex-preview-clear-cache (&optional beg end clear-entire-cache) (defun org-latex-preview-clear-cache (&optional beg end clear-entire-cache)
"Clear LaTeX preview cache for fragments between BEG and END. "Clear LaTeX preview cache for fragments between BEG and END.

View File

@ -233,7 +233,7 @@
(ert-deftest test-org-latex-preview/place-previews-1 () (ert-deftest test-org-latex-preview/place-previews-1 ()
(org-test-at-id "0b3807b3-69af-40cb-a27a-b380d54879cc" (org-test-at-id "0b3807b3-69af-40cb-a27a-b380d54879cc"
(let ((org-latex-preview-process-precompiled nil) (let ((org-latex-preview-process-precompiled nil)
(org-latex-preview-persist nil) (org-latex-preview-cache 'temp)
(org-latex-preview-process-default 'dvisvgm)) (org-latex-preview-process-default 'dvisvgm))
(org-latex-preview-auto-mode -1) (org-latex-preview-auto-mode -1)
(goto-char 255) (goto-char 255)
@ -268,7 +268,7 @@
(ert-deftest test-org-latex-preview/place-previews-all () (ert-deftest test-org-latex-preview/place-previews-all ()
(org-test-at-id "0b3807b3-69af-40cb-a27a-b380d54879cc" (org-test-at-id "0b3807b3-69af-40cb-a27a-b380d54879cc"
(let ((org-latex-preview-process-precompiled nil) (let ((org-latex-preview-process-precompiled nil)
(org-latex-preview-persist nil) (org-latex-preview-cache 'temp)
(org-latex-preview-process-default 'dvisvgm)) (org-latex-preview-process-default 'dvisvgm))
(org-latex-preview-auto-mode -1) (org-latex-preview-auto-mode -1)
(org-latex-preview-clear-cache (point-min) (point-max)) (org-latex-preview-clear-cache (point-min) (point-max))