lisp/org.el: Do not inline images when no graphic display is available

* lisp/org.el (org-preview-latex-fragment)
(org-display-inline-images): Detect whether a graphic display is
available before inlining images to prevent an error.

Thanks to Rick Frankel for the report and the solution.

> `org-startup-with-inline-images' is a customizable variable. The
> problem is that if an org file is visited in a non-graphics buffer (or
> batch), `org-display-inline-images' is called an throws an error
> ("Non-X frame used").
>
> This problem also occurs when e.g., `org-babel-after-execute-hook' is
> set to 'org-display-inline-images (which can be mitigated by not
> setting the hook in a non-x frame).
>
> Since the startup variable is a customization, and causes problems if
> not set programatically, IMHO, the best solution would be to wrap the
> `org-display-inline-images' function in a test so that is is a no-op
> on non graphic displays:
This commit is contained in:
Grégoire Jadi 2013-05-01 19:19:57 +02:00
parent 73ce77f991
commit 168c5584f5
1 changed files with 80 additions and 78 deletions

View File

@ -18195,37 +18195,38 @@ The images can be removed again with \\[org-ctrl-c-ctrl-c]."
(interactive "P") (interactive "P")
(unless buffer-file-name (unless buffer-file-name
(user-error "Can't preview LaTeX fragment in a non-file buffer")) (user-error "Can't preview LaTeX fragment in a non-file buffer"))
(org-remove-latex-fragment-image-overlays) (when (display-graphic-p)
(save-excursion (org-remove-latex-fragment-image-overlays)
(save-restriction (save-excursion
(let (beg end at msg) (save-restriction
(cond (let (beg end at msg)
((or (equal subtree '(16)) (cond
(not (save-excursion ((or (equal subtree '(16))
(re-search-backward org-outline-regexp-bol nil t)))) (not (save-excursion
(setq beg (point-min) end (point-max) (re-search-backward org-outline-regexp-bol nil t))))
msg "Creating images for buffer...%s")) (setq beg (point-min) end (point-max)
((equal subtree '(4)) msg "Creating images for buffer...%s"))
(org-back-to-heading) ((equal subtree '(4))
(setq beg (point) end (org-end-of-subtree t) (org-back-to-heading)
msg "Creating images for subtree...%s")) (setq beg (point) end (org-end-of-subtree t)
(t msg "Creating images for subtree...%s"))
(if (setq at (org-inside-LaTeX-fragment-p)) (t
(goto-char (max (point-min) (- (cdr at) 2))) (if (setq at (org-inside-LaTeX-fragment-p))
(org-back-to-heading)) (goto-char (max (point-min) (- (cdr at) 2)))
(setq beg (point) end (progn (outline-next-heading) (point)) (org-back-to-heading))
msg (if at "Creating image...%s" (setq beg (point) end (progn (outline-next-heading) (point))
"Creating images for entry...%s")))) msg (if at "Creating image...%s"
(message msg "") "Creating images for entry...%s"))))
(narrow-to-region beg end) (message msg "")
(goto-char beg) (narrow-to-region beg end)
(org-format-latex (goto-char beg)
(concat org-latex-preview-ltxpng-directory (file-name-sans-extension (org-format-latex
(file-name-nondirectory (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
buffer-file-name))) (file-name-nondirectory
default-directory 'overlays msg at 'forbuffer buffer-file-name)))
org-latex-create-formula-image-program) default-directory 'overlays msg at 'forbuffer
(message msg "done. Use `C-c C-c' to remove images."))))) org-latex-create-formula-image-program)
(message msg "done. Use `C-c C-c' to remove images."))))))
(defun org-format-latex (prefix &optional dir overlays msg at (defun org-format-latex (prefix &optional dir overlays msg at
forbuffer processing-type) forbuffer processing-type)
@ -18747,53 +18748,54 @@ When REFRESH is set, refresh existing images between BEG and END.
This will create new image displays only if necessary. This will create new image displays only if necessary.
BEG and END default to the buffer boundaries." BEG and END default to the buffer boundaries."
(interactive "P") (interactive "P")
(unless refresh (when (display-graphic-p)
(org-remove-inline-images) (unless refresh
(if (fboundp 'clear-image-cache) (clear-image-cache))) (org-remove-inline-images)
(save-excursion (if (fboundp 'clear-image-cache) (clear-image-cache)))
(save-restriction (save-excursion
(widen) (save-restriction
(setq beg (or beg (point-min)) end (or end (point-max))) (widen)
(goto-char beg) (setq beg (or beg (point-min)) end (or end (point-max)))
(let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?" (goto-char beg)
(substring (org-image-file-name-regexp) 0 -2) (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
"\\)\\]" (if include-linked "" "\\]"))) (substring (org-image-file-name-regexp) 0 -2)
(case-fold-search t) "\\)\\]" (if include-linked "" "\\]")))
old file ov img type attrwidth width) (case-fold-search t)
(while (re-search-forward re end t) old file ov img type attrwidth width)
(setq old (get-char-property-and-overlay (match-beginning 1) (while (re-search-forward re end t)
'org-image-overlay) (setq old (get-char-property-and-overlay (match-beginning 1)
file (expand-file-name 'org-image-overlay)
(concat (or (match-string 3) "") (match-string 4)))) file (expand-file-name
(when (image-type-available-p 'imagemagick) (concat (or (match-string 3) "") (match-string 4))))
(setq attrwidth (if (or (listp org-image-actual-width) (when (image-type-available-p 'imagemagick)
(null org-image-actual-width)) (setq attrwidth (if (or (listp org-image-actual-width)
(save-excursion (null org-image-actual-width))
(save-match-data (save-excursion
(when (re-search-backward (save-match-data
"#\\+attr.*:width[ \t]+\\([^ ]+\\)" (when (re-search-backward
(save-excursion "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
(re-search-backward "^[ \t]*$\\|\\`" nil t)) t) (save-excursion
(string-to-number (match-string 1)))))) (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
width (cond ((eq org-image-actual-width t) nil) (string-to-number (match-string 1))))))
((null org-image-actual-width) attrwidth) width (cond ((eq org-image-actual-width t) nil)
((numberp org-image-actual-width) ((null org-image-actual-width) attrwidth)
org-image-actual-width) ((numberp org-image-actual-width)
((listp org-image-actual-width) org-image-actual-width)
(or attrwidth (car org-image-actual-width)))) ((listp org-image-actual-width)
type (if width 'imagemagick))) (or attrwidth (car org-image-actual-width))))
(when (file-exists-p file) type (if width 'imagemagick)))
(if (and (car-safe old) refresh) (when (file-exists-p file)
(image-refresh (overlay-get (cdr old) 'display)) (if (and (car-safe old) refresh)
(setq img (save-match-data (create-image file type nil :width width))) (image-refresh (overlay-get (cdr old) 'display))
(when img (setq img (save-match-data (create-image file type nil :width width)))
(setq ov (make-overlay (match-beginning 0) (match-end 0))) (when img
(overlay-put ov 'display img) (setq ov (make-overlay (match-beginning 0) (match-end 0)))
(overlay-put ov 'face 'default) (overlay-put ov 'display img)
(overlay-put ov 'org-image-overlay t) (overlay-put ov 'face 'default)
(overlay-put ov 'modification-hooks (overlay-put ov 'org-image-overlay t)
(list 'org-display-inline-remove-overlay)) (overlay-put ov 'modification-hooks
(push ov org-inline-image-overlays))))))))) (list 'org-display-inline-remove-overlay))
(push ov org-inline-image-overlays))))))))))
(define-obsolete-function-alias (define-obsolete-function-alias
'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3") 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")