Browse Source

ob-core: Fix inserting improper lists

* lisp/ob-core.el (org-babel-insert-result): Fix output when result is
  an improper list, which cannot be turned into a table.

* testing/lisp/test-ob.el (test-ob/org-babel-insert-result--improper-lists):
  New test.

Reported-by: Daniele Pizzolli <dan@toel.it>
<http://permalink.gmane.org/gmane.emacs.orgmode/95348>
Nicolas Goaziou 2 years ago
parent
commit
ad7b7efcdc
2 changed files with 31 additions and 10 deletions
  1. 19 10
      lisp/ob-core.el
  2. 12 0
      testing/lisp/test-ob.el

+ 19 - 10
lisp/ob-core.el

@@ -2231,18 +2231,28 @@ INFO may provide the values of these header arguments (in the
 			    (if (listp result) result (split-string result "\n" t))))
 		     '(:splicep nil :istart "- " :iend "\n")))
 		   "\n"))
-		 ;; assume the result is a table if it's not a string
-		 ((funcall proper-list-p result)
+		 ;; Try hard to print RESULT as a table.  Give up if
+		 ;; it contains an improper list.
+		 ((and (funcall proper-list-p result)
+		       (org-every (lambda (e)
+				    (or (atom e) (funcall proper-list-p e)))
+				  result))
 		  (goto-char beg)
 		  (insert (concat (orgtbl-to-orgtbl
 				   (if (org-every
-					(lambda (el) (or (listp el) (eq el 'hline)))
+					(lambda (e)
+					  (or (eq e 'hline) (listp e)))
 					result)
-				       result (list result))
-				   '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
-		  (goto-char beg) (when (org-at-table-p) (org-table-align)))
-		 ((and (listp result) (not (funcall proper-list-p result)))
-		  (insert (format "%s\n" result)))
+				       result
+				     (list result))
+				   nil)
+				  "\n"))
+		  (goto-char beg)
+		  (when (org-at-table-p) (org-table-align))
+		  (goto-char (org-table-end)))
+		 ;; Print verbatim a list that cannot be turned into
+		 ;; a table.
+		 ((listp result) (insert (format "%s\n" result)))
 		 ((member "file" result-params)
 		  (when inlinep
 		    (goto-char inlinep)
@@ -2254,11 +2264,10 @@ INFO may provide the values of these header arguments (in the
 		  (insert (org-macro-escape-arguments
 			   (org-babel-chomp result "\n"))))
 		 (t (goto-char beg) (insert result)))
-		(when (funcall proper-list-p result) (goto-char (org-table-end)))
 		(setq end (point-marker))
 		;; possibly wrap result
 		(cond
-		 (bad-inline-p) ; Do nothing.
+		 (bad-inline-p)		; Do nothing.
 		 ((assoc :wrap (nth 2 info))
 		  (let ((name (or (cdr (assoc :wrap (nth 2 info))) "RESULTS")))
 		    (funcall wrap (concat "#+BEGIN_" name)

+ 12 - 0
testing/lisp/test-ob.el

@@ -758,6 +758,18 @@ on two lines
 	     ": 2"
 	     (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
 
+(ert-deftest test-ob/org-babel-insert-result--improper-lists ()
+  "Test `org-babel-insert-result' with improper lists."
+  ;; Do not error when output is an improper list.
+  (should
+   (org-test-with-temp-text
+       "
+<point>#+BEGIN_SRC emacs-lisp
+'((1 . nil) (2 . 3))
+#+END_SRC
+"
+     (org-babel-execute-maybe) t)))
+
 (ert-deftest test-ob/remove-inline-result ()
   "Test `org-babel-remove-inline-result' honors whitespace."
   (let*