From a3087aba81a82d2657e66086bb1be18c05fd6d41 Mon Sep 17 00:00:00 2001 From: TEC Date: Wed, 4 Jan 2023 01:18:34 +0800 Subject: [PATCH] org-latex-preview: Do not show empty SVG fragments * lisp/org-latex-preview.el (org-latex-preview--svg-make-fg-currentColor): When an SVG is empty, delete it. This will cause `org-latex-preview--cache-image' to give nil as the path. When the provided path is nil, do nothing. (org-latex-preview--update-overlay): If the image path is nil, do attempt to place an image. If an error is recorded and there is no image this causes the text to be shown with the error face. (org-latex-preview--cleanup-callback): If the image path is nil, do not try to delete it. --- lisp/org-latex-preview.el | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/lisp/org-latex-preview.el b/lisp/org-latex-preview.el index 6284cb5be..a1520d05e 100644 --- a/lisp/org-latex-preview.el +++ b/lisp/org-latex-preview.el @@ -320,7 +320,7 @@ indeed LaTeX fragments/environments.") (height (plist-get (cdr path-info) :height)) (depth (plist-get (cdr path-info) :depth)) (image-display - (and path-info + (and (car path-info) (list 'image :type (plist-get (cdr path-info) :image-type) :file (car path-info) @@ -341,8 +341,9 @@ indeed LaTeX fragments/environments.") (round (* 100 (- 1 (/ (max 0.0 (- depth 0.02)) height)))) 'center))))) - (overlay-put ov 'display image-display) - (overlay-put ov 'preview-image image-display) + (when image-display + (overlay-put ov 'display image-display) + (overlay-put ov 'preview-image image-display)) (overlay-put ov 'face (cond @@ -1041,7 +1042,7 @@ The path of the created LaTeX file is returned." '(".dvi" ".xdv" ".pdf" ".tex" ".aux" ".log" ".svg" ".png" ".jpg" ".jpeg" ".out")))) (dolist (img images) - (delete-file img)) + (and img (delete-file img))) (dolist (ext clean-exts) (when (file-exists-p (concat basename ext)) (delete-file (concat basename ext)))))) @@ -1196,16 +1197,24 @@ tests with the output of dvisvgm." (coding-system-for-write 'utf-8) ;; Prevent any file handlers (specifically ;; `image-file-handler') from being called. - (file-name-handler-alist nil)) - (with-temp-buffer - (insert-file-contents (plist-get svg-fragment :path)) - (goto-char (point-min)) - (when (re-search-forward "]*>\n]*>\n" nil t) + ;; We never want to show an empty SVG, instead it is better to delete + ;; it and leave the LaTeX fragment without an image overlay. + ;; This also works better with other parts of the system, such as + ;; the display of errors. + (delete-file path) + (when (re-search-forward "