forked from mirrors/org-mode
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:
parent
73df97486f
commit
e1fd05fda5
|
@ -1810,25 +1810,15 @@ EXTENDED-INFO, and displayed in the buffer."
|
||||||
(setq page-marks (cdr page-marks)))
|
(setq page-marks (cdr page-marks)))
|
||||||
(when fragments-to-show
|
(when fragments-to-show
|
||||||
(setq fragments-to-show (nreverse fragments-to-show))
|
(setq fragments-to-show (nreverse fragments-to-show))
|
||||||
;; There seems to often be a slight delay between dvisvgm reporting
|
(mapc #'org-latex-preview--svg-make-fg-currentColor fragments-to-show)
|
||||||
;; to have written a file, and all the content actually being there.
|
(if (plist-get extended-info :place-preview-p)
|
||||||
;; On my machine, an 0.002s delay is sufficient to eliminate this issue,
|
(org-latex-preview--place-images extended-info fragments-to-show)
|
||||||
;; to be a bit safer this we use 5x that here.
|
(dolist (fragment-info fragments-to-show)
|
||||||
(run-at-time
|
(org-latex-preview--cache-image
|
||||||
0.01 nil
|
(plist-get fragment-info :key)
|
||||||
(if (plist-get extended-info :place-preview-p)
|
(plist-get fragment-info :path)
|
||||||
(lambda (fragments)
|
(org-latex-preview--display-info
|
||||||
(mapc #'org-latex-preview--svg-make-fg-currentColor fragments)
|
extended-info fragment-info)))))))
|
||||||
(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))))
|
|
||||||
|
|
||||||
(defun org-latex-preview--svg-make-fg-currentColor (svg-fragment)
|
(defun org-latex-preview--svg-make-fg-currentColor (svg-fragment)
|
||||||
"Replace the foreground color in SVG-FRAGMENT's file with \"currentColor\".
|
"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.
|
;; `image-file-handler') from being called.
|
||||||
(file-name-handler-alist nil)
|
(file-name-handler-alist nil)
|
||||||
(path (plist-get svg-fragment :path)))
|
(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
|
(when path
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(insert-file-contents path)
|
(insert-file-contents path)
|
||||||
(unless ; When the svg is incomplete, wait for it to be completed.
|
(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>")
|
"</svg>")
|
||||||
(catch 'svg-complete
|
(catch 'svg-complete
|
||||||
(dotimes (_ 1000) ; Check for complete svg over 1s.
|
(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>")
|
"</svg>")
|
||||||
(throw 'svg-complete t)
|
(throw 'svg-complete t)
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
(sit-for 0.001)
|
(sleep-for 0.001)
|
||||||
(insert-file-contents path)))
|
(insert-file-contents path)))
|
||||||
(erase-buffer)))
|
(erase-buffer)))
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
|
|
Loading…
Reference in New Issue