org-latex-preview: Wait for SVGs more robustly

* lisp/org-latex-preview.el (org-latex-preview--svg-make-fg-currentColor,
org-latex-preview--dvisvgm-filter): Instead of waiting 0.01s and hoping
that an SVG exists by that point, we can wait and check for the
existance of an SVG within
`org-latex-preview--svg-make-fg-currentColor', and that should reduce
the chance of edge cases where the delay is insufficient.
(org-latex-preview--svg-make-fg-currentColor): Adjust the
`buffer-substring' call to not error when the buffer is empty.
This commit is contained in:
TEC 2023-02-26 19:27:12 +08:00
parent 73df97486f
commit e1fd05fda5
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 17 additions and 22 deletions

View File

@ -1810,25 +1810,15 @@ 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))
;; 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
(if (plist-get extended-info :place-preview-p)
(lambda (fragments)
(mapc #'org-latex-preview--svg-make-fg-currentColor fragments)
(org-latex-preview--place-images extended-info fragments))
(lambda (fragments)
(mapc #'org-latex-preview--svg-make-fg-currentColor fragments)
(dolist (fragment-info fragments)
(org-latex-preview--cache-image
(plist-get fragment-info :key)
(plist-get fragment-info :path)
(org-latex-preview--display-info
extended-info fragment-info)))))
fragments-to-show))))
(mapc #'org-latex-preview--svg-make-fg-currentColor fragments-to-show)
(if (plist-get extended-info :place-preview-p)
(org-latex-preview--place-images extended-info fragments-to-show)
(dolist (fragment-info fragments-to-show)
(org-latex-preview--cache-image
(plist-get fragment-info :key)
(plist-get fragment-info :path)
(org-latex-preview--display-info
extended-info fragment-info)))))))
(defun org-latex-preview--svg-make-fg-currentColor (svg-fragment)
"Replace the foreground color in SVG-FRAGMENT's file with \"currentColor\".
@ -1844,19 +1834,24 @@ tests with the output of dvisvgm."
;; `image-file-handler') from being called.
(file-name-handler-alist nil)
(path (plist-get svg-fragment :path)))
(catch 'svg-exists
(dotimes (_ 1000) ; Check for svg existance over 1s.
(when (file-exists-p path)
(throw 'svg-exists t))
(sleep-for 0.001)))
(when path
(with-temp-buffer
(insert-file-contents path)
(unless ; When the svg is incomplete, wait for it to be completed.
(string= (buffer-substring (- (point-max) 6) (point-max))
(string= (buffer-substring (max 1 (- (point-max) 6)) (point-max))
"</svg>")
(catch 'svg-complete
(dotimes (_ 1000) ; Check for complete svg over 1s.
(if (string= (buffer-substring (- (point-max) 6) (point-max))
(if (string= (buffer-substring (max 1 (- (point-max) 6)) (point-max))
"</svg>")
(throw 'svg-complete t)
(erase-buffer)
(sit-for 0.001)
(sleep-for 0.001)
(insert-file-contents path)))
(erase-buffer)))
(goto-char (point-min))