emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jack Kamm <jackkamm@gmail.com>
To: Kyle Meyer <kyle@kyleam.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: ob-python newline & indentation behavior
Date: Sat, 18 Nov 2017 22:15:57 +0000	[thread overview]
Message-ID: <CADRALHztrRFK4ovvBHk6Mevm7jnKhtMYxOzKfJ_PB-kCf5oAww@mail.gmail.com> (raw)
In-Reply-To: <87h8trtyoa.fsf@kyleam.com>

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

Thanks Kyle, and sorry for missing that recent related thread.

I adapted your old patch to the current master branch. I also extended it
to work for ":results value" (the original patch only worked for ":results
output"). I did this by not writing the last line of the code block to the
tmpfile, unless it is indented.

I've never contributed before so would appreciate any comments on this
patch, and how to get it accepted. Cheers, Jack.

From a5d553ece9f6ee35cd1e273e554a21a19e80ec3c Mon Sep 17 00:00:00 2001

From: Jack Kamm <jackkamm@gmail.com>
Date: Sat, 18 Nov 2017 21:47:09 +0000
Subject: [PATCH] fix newline/indentation issues in ob-python :session

---
 lisp/ob-python.el | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 60ec5fa47..6623ef5fc 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -303,6 +303,10 @@ last statement in BODY, as elisp."
                       (mapc (lambda (line) (insert line) (funcall
send-wait))
                             (split-string body "[\r\n]"))
                       (funcall send-wait)))
+        (indented-p (org-babel-python--indented-p body))
+        (body (if indented-p
+                  (org-babel-python--replace-body-tmpfile body)
+                body))
          (results
           (pcase result-type
             (`output
@@ -340,6 +344,35 @@ last statement in BODY, as elisp."
       (substring string 1 -1)
     string))

+
+(defun org-babel-python--indented-p (input)
+     "Non-nil if any line in INPUT is indented."
+     (string-match-p "^[ \t]" input))
+
+(defun org-babel-python--replace-body-tmpfile (body)
+  "Place body in tmpfile, and return string to exec the tmpfile.
+If last line of body is not indented, place it at end of exec string
+instead of tmpfile, so shell can see the result"
+  (let* ((tmp-file (org-babel-temp-file "python-"))
+        (lines (split-string body "\n" t))
+        (lastline (car (last lines)))
+        (newbody (concat
+                  (format "__pyfilename = '%s'; " tmp-file)
+                  "__pyfile = open(__pyfilename); "
+                  "exec(compile("
+                  "__pyfile.read(), __pyfilename, 'exec'"
+                  ")); "
+                  "__pyfile.close()")))
+    (if (string-match-p "^[ \t]" lastline)
+       (progn
+         (with-temp-file tmp-file (insert body))
+         newbody)
+      (with-temp-file tmp-file
+       (insert (mapconcat 'identity
+                          (butlast lines) "\n")))
+      (concat newbody "\n" lastline)))
+  )
+
 (provide 'ob-python)


--
2.15.0


On Sat, Nov 18, 2017 at 3:05 PM, Kyle Meyer <kyle@kyleam.com> wrote:

> Hello,
>
> Jack Kamm <jackkamm@gmail.com> writes:
>
> > ob-python newline & indentation behavior in :session is very ugly and
> > possibly broken. For example, consider the following code block:
>
> [...]
>
> > There is a 2 year old patch that fixes this behavior but has not yet
> > been incorporated:
> > https://lists.gnu.org/archive/html/emacs-orgmode/2015-03/msg00505.html
>
> [...]
>
> > The patch follows the python.el behavior of using a temporary file and
> > executing that from the shell.
> >
> > Could this patch, or something similar, be incorporated into org-mode,
> > to fix this behavior?
>
> Here's what I said in a recent post about ob-python sessions:
>
>     https://lists.gnu.org/archive/html/emacs-orgmode/2017-10/msg00198.html
>
>     I dropped my attempt to fix it because 1) I was still having trouble
>     getting a complete understanding of what the issue was and 2) I didn't
>     have the motivation to spend time digging deeper because I don't use
>     ob-python (and in general am not a heavy Org-Babel user).  Perhaps you
>     or some other ob-python user could help make ob-python sessions more
>     robust?
>
> Perhaps you are the "you"?
>
> --
> Kyle
>

[-- Attachment #2: Type: text/html, Size: 6041 bytes --]

  reply	other threads:[~2017-11-18 22:16 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-18 12:02 ob-python newline & indentation behavior Jack Kamm
2017-11-18 15:05 ` Kyle Meyer
2017-11-18 22:15   ` Jack Kamm [this message]
2017-11-19  3:27     ` Martin Alsinet
2017-11-19  3:34       ` Martin Alsinet
2017-11-19  7:41         ` Jack Kamm
2017-11-20  6:25           ` Kyle Meyer
2017-11-20 21:31             ` Jack Kamm
2017-11-21  4:04               ` Kyle Meyer
2017-11-21  8:28                 ` Jack Kamm
2017-11-26  2:45                   ` Ista Zahn
2017-11-26  8:25                     ` Jack Kamm
2017-11-26  8:15                   ` Jack Kamm
2017-11-27  4:00                     ` Kyle Meyer
2017-12-09 12:12                       ` John Kamm
2017-12-18  8:22                         ` Jack Kamm
2017-12-18 11:44                           ` Kyle Meyer
2017-12-19  5:11                             ` Kyle Meyer
2017-11-19 17:17     ` Kyle Meyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADRALHztrRFK4ovvBHk6Mevm7jnKhtMYxOzKfJ_PB-kCf5oAww@mail.gmail.com \
    --to=jackkamm@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=kyle@kyleam.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).