From ad7b7efcdc2e0803cbf0257bb2588c8e8e1cac29 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Mon, 23 Feb 2015 18:37:37 +0100 Subject: [PATCH] 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 --- lisp/ob-core.el | 29 +++++++++++++++++++---------- testing/lisp/test-ob.el | 12 ++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index a00b5a310..f2062efd0 100644 --- a/lisp/ob-core.el +++ b/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) diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el index c5b5756b4..f52ff2419 100644 --- a/testing/lisp/test-ob.el +++ b/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 + " +#+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* -- 2.11.4.GIT