From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kyle Meyer Subject: ob-python session output Date: Wed, 20 Nov 2013 02:41:04 -0500 Message-ID: <87y54j34hb.fsf@kmlap.domain.org> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:35675) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vj2PT-000433-CA for emacs-orgmode@gnu.org; Wed, 20 Nov 2013 02:41:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vj2PM-0005mj-1s for emacs-orgmode@gnu.org; Wed, 20 Nov 2013 02:41:15 -0500 Received: from mail-qc0-f182.google.com ([209.85.216.182]:55505) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vj2PL-0005mc-TE for emacs-orgmode@gnu.org; Wed, 20 Nov 2013 02:41:07 -0500 Received: by mail-qc0-f182.google.com with SMTP id n7so5687097qcx.13 for ; Tue, 19 Nov 2013 23:41:07 -0800 (PST) Received: from localhost (c-71-233-154-22.hsd1.ct.comcast.net. [71.233.154.22]) by mx.google.com with ESMTPSA id b9sm61716212qas.7.2013.11.19.23.41.05 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Nov 2013 23:41:06 -0800 (PST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Hello, I've been having some issues with the output of babel python session blocks. They do not seem to be properly processing python shell characters and leading spaces in the output. For the examples that follow, the only user configuration is the loading of python. #+BEGIN_SRC elisp :results none (org-babel-do-load-languages 'org-babel-load-languages '((python . t))) #+END_SRC When the session is first started up, python shell character ('>>>' and '...') make it into the output. #+BEGIN_SRC python :session :results output for i in range(2): print(i) #+END_SRC #+RESULTS: : : >>> >>> >>> ... ... 0 : 1 When ran a second time, only '...' remains. #+BEGIN_SRC python :session :results output for i in range(2): print(i) #+END_SRC #+RESULTS: : : ... 0 : 1 The other issue I'm seeing is that leading spaces in the output are being discarded. #+BEGIN_SRC python :session :results output print(' one leading space') #+END_SRC #+RESULTS: : one leading space #+BEGIN_SRC python :session :results output print(' many leading spaces') #+END_SRC #+RESULTS: : many leading spaces I've made the following changes for a quick fix. It's ugly and certainly not a proper solution, but it seems to take care of all the above cases except for the one leading space. --- lisp/ob-python.el | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 3c3f664..5cda5d6 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -307,7 +307,8 @@ last statement in BODY, as elisp." (case result-type (output (mapconcat - #'org-babel-trim + #'(lambda (string) (org-babel-chomp + (org-babel-python-leading-chomp string))) (butlast (org-babel-comint-with-output (session org-babel-python-eoe-indicator t body) @@ -339,6 +340,25 @@ last statement in BODY, as elisp." (match-string 1 string) string)) +(defun org-babel-python-leading-chomp (string) + "Strip the leading python output characters from STRING + +This is different from `org-babel-chomp' (where '>' and '.' are +added to the regex) because it considers a bit more of the +context. If only single characters are matched against, then +meaningful spaces are often deleted from the output. However, if +spaces are not trimmed at all, extra spaces creep into the +output (particularly when the output source line is indented). To +get around this, only leading spaces that are followed by a +non-space character are deleted. This completely fails on cases +where the intended output acutally has one space before non-space +character." + (let ((regexp " *>+\\|\\( \\)*\\.+\\| \\w" )) + (while (and (/= (length string) 0) + (eq (string-match regexp string) 0)) + (setq string (substring string 1))) + string)) + (provide 'ob-python) -- 1.8.4.2 I'm using the following versions of emacs and org mode. #+BEGIN_SRC elisp (emacs-version) #+END_SRC #+RESULTS: : GNU Emacs 24.3.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.8.2) : of 2013-08-06 on -mnt-storage-buildroots-staging-x86_64-eric #+BEGIN_SRC elisp (org-version nil t) #+END_SRC #+RESULTS: : Org-mode version 8.2.3c (release_8.2.3c-251-gbb97f5 @ /home/kyle/src/emacs/org-mode/lisp/) Thanks -- Kyle Meyer