org-latex-preview: Wait for dvisvgm to write

* lisp/org-latex-preview.el (org-latex-preview--dvisvgm-filter): As
mentioned in the code comment, dvisvgm seems to sometimes lie about when
file content has been written, so we need to work around that by waiting
a little bit.
(org-latex-preview--cleanup-callback): Now that the svg processing is on
a timer, we must be careful not to run the callback to soon.  This isn't
desperately urgent, so we can use a relatively generous timer for this.
This commit is contained in:
TEC 2023-01-07 00:25:34 +08:00
parent 8aabc04b08
commit 93799947c8
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 18 additions and 2 deletions

View File

@ -1275,6 +1275,14 @@ The path of the created LaTeX file is returned."
:failure "LaTeX preview image conversion failed! (error code %d)")))
(defun org-latex-preview--cleanup-callback (_exit-code _stdout extended-info)
"Schedule cleanup with EXTENDED-INFO."
(message "Scheduling cleanup")
(run-with-idle-timer
1.0 nil
#'org-latex-preview--do-cleanup
extended-info))
(defun org-latex-preview--do-cleanup (extended-info)
"Delete files after image creation, in accord with EXTENDED-INFO."
(let* ((basename (file-name-sans-extension (plist-get extended-info :texfile)))
(images
@ -1461,8 +1469,16 @@ EXTENDED-INFO, and displayed in the buffer."
(setq page-marks (cdr page-marks)))
(when fragments-to-show
(setq fragments-to-show (nreverse fragments-to-show))
(mapc #'org-latex-preview--svg-make-fg-currentColor fragments-to-show)
(org-latex-preview--place-images extended-info fragments-to-show))))
;; There seems to often be a slight delay between dvisvgm reporting
;; to have written a file, and all the content actually being there.
;; On my machine, an 0.002s delay is sufficient to eliminate this issue,
;; to be a bit safer this we use 5x that here.
(run-at-time
0.01 nil
(lambda (frags)
(mapc #'org-latex-preview--svg-make-fg-currentColor frags)
(org-latex-preview--place-images extended-info frags))
fragments-to-show))))
(defun org-latex-preview--svg-make-fg-currentColor (svg-fragment)
"Replace the foreground color in SVG-FRAGMENT's file with \"currentColor\".