From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Amlund Thomsen Subject: [Babel] Interpreting results as tables and (eval)'uation of them Date: Sun, 13 Feb 2011 21:37:45 +0100 Message-ID: <1297629465.4235.27.camel@eeevk> Reply-To: danamlund@gmail.com Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Return-path: Received: from [140.186.70.92] (port=50747 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Poihd-0002E1-CZ for emacs-orgmode@gnu.org; Sun, 13 Feb 2011 15:37:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Poihc-0005FM-5D for emacs-orgmode@gnu.org; Sun, 13 Feb 2011 15:37:53 -0500 Received: from mail-ey0-f169.google.com ([209.85.215.169]:63619) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Poihb-0005FH-VB for emacs-orgmode@gnu.org; Sun, 13 Feb 2011 15:37:52 -0500 Received: by eyh6 with SMTP id 6so1863264eyh.0 for ; Sun, 13 Feb 2011 12:37:50 -0800 (PST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org I've encountered some weird, possible buggy, behavior when interpreting results as tables (tested with python, scheme and lisp). * Item 1: Interpreting result as table With ":results value" the result is interpreted as a table if possible, but with ":results output" it isn't. This happens with python, lisp and scheme, but not with c. The documentation suggests both value and output results should be interpreted as a table if possible. "By default, results are inserted as either a table or scalar depending on their value." [http://orgmode.org/manual/results.html] #+begin_src python :results output print "'(1 2)" #+end_src #+results: : '(1 2) #+begin_src python :results value return "'(1 2)" #+end_src #+results: | 1 | 2 | * Item 2: Evaluating list results When a result is interpreted as a list, the list is (eval)'ed. This happens in non-lisp languages (c, python) but not in lisp languages (lisp, scheme). In my opinion the lists should not be evaluated, but 'org-babel-script-escape' and 'org-babel-read' suggests it is intended behavior. Is this a bug or a feature? #+begin_src c++ :includes printf("(1 2)"); #+end_src Returns the error "Invalid function: 1". The correct approach is: #+begin_src c++ :includes printf("(list 1 2)"); #+end_src #+results: | 1 | 2 | With lisp the list is not evaluated (note that "'(1 2)" results in "(1 2)"). #+begin_src lisp '(1 2) #+end_src #+results: | 1 | 2 | * Item 3: Checking if result is a list is not safe Mismatched parenthesis and bad characters causes errors. I suggest showing the raw result if the result is not a valid list. I'm not sure if this is a bug or not. These error messages could be helpful in debugging code when trying to output a list that needs to be evaluated. Although the final output of the (invalid) list could also be helpful with debugging. #+begin_src c++ :includes printf("("); #+end_src Returns the error: End of file during parsing #+begin_src python return "(list #)" #+end_src Returns the error: Invalid read syntax: "#" Here are some possible solutions: #+begin_src emacs-lisp (defun org-babel-safe-read-dont-eval (str) "Converts string into a list. Elements are converted into strings to prevent read errors from special characters." (let ((str (replace-regexp-in-string "\\([^() \f\t\n\r\v]+\\)" "\"\\1\""str))) (condition-case nil (read str) (error (concat "\"" str "\""))))) (org-babel-safe-read-dont-eval "(1 1#123 1)") #+end_src #+results: | 1 | 1#123 | 1 | #+begin_src emacs-lisp (defun org-babel-safe-read-do-eval (str) "Converts string into a evaluated list." (condition-case nil (eval (read str)) (error (concat "\"" str "\"")))) (org-babel-safe-read-do-eval "(1 1#123 1)") #+end_src #+results: : "(1 1#123 1)"