emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Bug] org-startup-with-inline-images
@ 2013-04-30 17:39 Rick Frankel
  2013-05-01 17:28 ` Daimrod
  0 siblings, 1 reply; 4+ messages in thread
From: Rick Frankel @ 2013-04-30 17:39 UTC (permalink / raw)
  To: Org-Mode

`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:

   (defun org-display-inline-images (&optional include-linked refresh 
beg end)
     "..."
    (interactive "P")
    (when (display-graphic-p)
    ^^^^^^^^^^^^^^^^^^^^^^^^
    [...])

rick

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Bug] org-startup-with-inline-images
  2013-04-30 17:39 [Bug] org-startup-with-inline-images Rick Frankel
@ 2013-05-01 17:28 ` Daimrod
  2013-05-01 17:42   ` Rick Frankel
  0 siblings, 1 reply; 4+ messages in thread
From: Daimrod @ 2013-05-01 17:28 UTC (permalink / raw)
  To: Rick Frankel; +Cc: Org-Mode


[-- Attachment #1.1: Type: text/plain, Size: 1409 bytes --]

Rick Frankel <rick@rickster.com> writes:
Hello Rick,

> `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:
>
>   (defun org-display-inline-images (&optional include-linked refresh
> beg end)
>     "..."
>    (interactive "P")
>    (when (display-graphic-p)
>    ^^^^^^^^^^^^^^^^^^^^^^^^
>    [...])

Thanks for the report, I've attached a patch that fixes this problem (in
both `org-display-inline-images' and `org-preview-latex-fragment').
However I don't know if it is the right approach or if I should try to
narrow this to lower-level functions.

I know that `clear-image-cache' raise this error but I haven't tried to
see if it the only one. Should I try to look more at it and add a
`org-clear-image-cache' which will check if a graphic display is
available or is the current solution fine?

Regards,


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-lisp-org.el-Do-not-inline-images-when-no-graphic-dis.patch --]
[-- Type: text/x-diff, Size: 8550 bytes --]

From db2c62c07b9e1a61db4930fc8252b96f3d6bc0b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9goire=20Jadi?= <gregoire.jadi@gmail.com>
Date: Wed, 1 May 2013 19:19:57 +0200
Subject: [PATCH] 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:
---
 lisp/org.el |  158 ++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 80 insertions(+), 78 deletions(-)

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


[-- Attachment #1.3: Type: text/plain, Size: 21 bytes --]


-- 
Daimrod/Greg

[-- Attachment #2: Type: application/pgp-signature, Size: 835 bytes --]

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Bug] org-startup-with-inline-images
  2013-05-01 17:28 ` Daimrod
@ 2013-05-01 17:42   ` Rick Frankel
  2013-05-02  8:09     ` Daimrod
  0 siblings, 1 reply; 4+ messages in thread
From: Rick Frankel @ 2013-05-01 17:42 UTC (permalink / raw)
  To: emacs-orgmode

On 01.05.2013 13:28, Daimrod wrote:
> Thanks for the report, I've attached a patch that fixes this problem 
> (in
> both `org-display-inline-images' and `org-preview-latex-fragment').
> However I don't know if it is the right approach or if I should try 
> to
> narrow this to lower-level functions.
>
> I know that `clear-image-cache' raise this error but I haven't tried 
> to
> see if it the only one. Should I try to look more at it and add a
> `org-clear-image-cache' which will check if a graphic display is
> available or is the current solution fine?

Looks good. No, I tried narrowing it down myself and it breaks in amy
other places in org-display-inline-images if you don't simply cond out
the entire function.

thanx,
rick

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Bug] org-startup-with-inline-images
  2013-05-01 17:42   ` Rick Frankel
@ 2013-05-02  8:09     ` Daimrod
  0 siblings, 0 replies; 4+ messages in thread
From: Daimrod @ 2013-05-02  8:09 UTC (permalink / raw)
  To: Rick Frankel; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 884 bytes --]

Rick Frankel <rick@rickster.com> writes:

> On 01.05.2013 13:28, Daimrod wrote:
>> Thanks for the report, I've attached a patch that fixes this problem
>> (in
>> both `org-display-inline-images' and `org-preview-latex-fragment').
>> However I don't know if it is the right approach or if I should try
>> to
>> narrow this to lower-level functions.
>>
>> I know that `clear-image-cache' raise this error but I haven't tried
>> to
>> see if it the only one. Should I try to look more at it and add a
>> `org-clear-image-cache' which will check if a graphic display is
>> available or is the current solution fine?
>
> Looks good. No, I tried narrowing it down myself and it breaks in amy
> other places in org-display-inline-images if you don't simply cond out
> the entire function.

Fixed in master, thanks again for your report!

Regards,

-- 
Daimrod/Greg

[-- Attachment #2: Type: application/pgp-signature, Size: 835 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-05-02  8:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-30 17:39 [Bug] org-startup-with-inline-images Rick Frankel
2013-05-01 17:28 ` Daimrod
2013-05-01 17:42   ` Rick Frankel
2013-05-02  8:09     ` Daimrod

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).