jupyter-org--append-stream-result: Consider org-edit-src-content-indentation

This commit is contained in:
Nathaniel Nicandro 2019-02-21 11:35:47 -06:00
parent 7d72673604
commit 70f4b1b9a3
No known key found for this signature in database
GPG key ID: C34814B309DD06B8
3 changed files with 99 additions and 73 deletions

View file

@ -1079,8 +1079,17 @@ insertion into the buffer."
;; Delete the newline that will be re-inserted by the call to ;; Delete the newline that will be re-inserted by the call to
;; `org-element-normalize-string'. ;; `org-element-normalize-string'.
(delete-char 1) (delete-char 1)
(insert (concat (when keep-newline "\n") (setq result (org-element-normalize-string result))
(org-element-normalize-string result)))))) ;; From `org-element-example-block-interpreter'
(when (and (not org-src-preserve-indentation)
(/= 0 org-edit-src-content-indentation)
(version<= "9.2" (org-version)))
(let ((ind (make-string org-edit-src-content-indentation ?\s)))
(setq result (replace-regexp-in-string
"^[ \t]*\\S-"
(concat ind "\\&")
(org-remove-indentation result)))))
(insert (concat (when keep-newline "\n") result)))))
;;;; Value results ;;;; Value results

View file

@ -1393,120 +1393,139 @@ file:foo
(ert-deftest jupyter-org-coalesce-stream-results () (ert-deftest jupyter-org-coalesce-stream-results ()
:tags '(org) :tags '(org)
(ert-info ("Synchronous") (let ((org-edit-src-content-indentation 0))
(jupyter-org-test-src-block (ert-info ("Synchronous")
"\ (jupyter-org-test-src-block
"\
print(\"foo\") print(\"foo\")
print(\"foo\", flush=True) print(\"foo\", flush=True)
print(\"foo\")" print(\"foo\")"
"\ "\
: foo : foo
: foo : foo
: foo")) : foo"))
(ert-info ("Asynchronous") (ert-info ("Asynchronous")
(ert-info ("Newline after first stream message")
(jupyter-org-test-src-block
"\
print(\"foo\")
print(\"foo\", flush=True)
print(\"foo\")"
"\
: foo
: foo
: foo"
:async "yes")
(jupyter-org-test-src-block
"\
print(\"foo\", flush=True)
print(\"foo\", end=\"\", flush=True)
print(\"foo\")"
"\
: foo
: foofoo")
:async "yes")
(ert-info ("No newline after first stream message")
(jupyter-org-test-src-block
"\
print(\"foo\")
print(\"foo\", end=\"\", flush=True)
print(\"bar\")"
"\
: foo
: foobar"
:async "yes"))
(ert-info ("Multiple newlines in appended stream message")
(ert-info ("Newline after first stream message") (ert-info ("Newline after first stream message")
(jupyter-org-test-src-block (jupyter-org-test-src-block
"\ "\
print(\"foo\") print(\"foo\")
print(\"foo\", flush=True) print(\"foo\", flush=True)
print(\"bar\\nqux\")" print(\"foo\")"
"\ "\
: foo : foo
: foo : foo
: bar : foo"
: qux" :async "yes")
:async "yes")) (jupyter-org-test-src-block
"\
print(\"foo\", flush=True)
print(\"foo\", end=\"\", flush=True)
print(\"foo\")"
"\
: foo
: foofoo")
:async "yes")
(ert-info ("No newline after first stream message") (ert-info ("No newline after first stream message")
(jupyter-org-test-src-block (jupyter-org-test-src-block
"\ "\
print(\"foo\") print(\"foo\")
print(\"foo\", end=\"\", flush=True) print(\"foo\", end=\"\", flush=True)
print(\"bar\\nqux\")" print(\"bar\")"
"\ "\
: foo : foo
: foobar"
:async "yes"))
(ert-info ("Multiple newlines in appended stream message")
(ert-info ("Newline after first stream message")
(jupyter-org-test-src-block
"\
print(\"foo\")
print(\"foo\", flush=True)
print(\"bar\\nqux\")"
"\
: foo
: foo
: bar
: qux"
:async "yes"))
(ert-info ("No newline after first stream message")
(jupyter-org-test-src-block
"\
print(\"foo\")
print(\"foo\", end=\"\", flush=True)
print(\"bar\\nqux\")"
"\
: foo
: foobar : foobar
: qux" : qux"
:async "yes"))) :async "yes")))
(ert-info ("fixed-width to example-block promotion") (ert-info ("fixed-width to example-block promotion")
(let ((org-babel-min-lines-for-block-output 2)) (let ((org-babel-min-lines-for-block-output 2))
(jupyter-org-test-src-block "print(\"z\")" ": z") (jupyter-org-test-src-block "print(\"z\")" ": z")
(jupyter-org-test-src-block
"\
print(\"z\", flush=True)
print(\"z\")"
"\
#+BEGIN_EXAMPLE
z
z
#+END_EXAMPLE"
:async "yes")
(ert-info ("Appending after block promotion")
(jupyter-org-test-src-block (jupyter-org-test-src-block
"\ "\
print(\"z\", flush=True) print(\"z\", flush=True)
print(\"z\", flush=True)
print(\"z\")" print(\"z\")"
"\ "\
#+BEGIN_EXAMPLE #+BEGIN_EXAMPLE
z z
z z
#+END_EXAMPLE"
:async "yes")
(ert-info ("Appending after block promotion")
(jupyter-org-test-src-block
"\
print(\"z\", flush=True)
print(\"z\", flush=True)
print(\"z\")"
"\
#+BEGIN_EXAMPLE
z
z
z z
#+END_EXAMPLE" #+END_EXAMPLE"
:async "yes")) :async "yes"))
(ert-info ("Append to block with newline after first stream message") (ert-info ("Append to block with newline after first stream message")
(jupyter-org-test-src-block (jupyter-org-test-src-block
"\ "\
print(\"z\\nz\", flush=True) print(\"z\\nz\", flush=True)
print(\"z\")" print(\"z\")"
"\ "\
#+BEGIN_EXAMPLE #+BEGIN_EXAMPLE
z z
z z
z z
#+END_EXAMPLE" #+END_EXAMPLE"
:async "yes")) :async "yes"))
(ert-info ("Append to block without newline after first stream message") (ert-info ("Append to block without newline after first stream message")
(jupyter-org-test-src-block (jupyter-org-test-src-block
"\ "\
print(\"z\\nz\", end=\"\", flush=True) print(\"z\\nz\", end=\"\", flush=True)
print(\"z\")" print(\"z\")"
"\ "\
#+BEGIN_EXAMPLE #+BEGIN_EXAMPLE
z z
zz zz
#+END_EXAMPLE" #+END_EXAMPLE"
:async "yes")))))) :async "yes")))))))
(ert-deftest jupyter-org-example-block-indentation ()
:tags '(org)
(skip-unless (version<= "9.2" (org-version)))
(let ((org-babel-min-lines-for-block-output 2)
(org-edit-src-content-indentation 2))
(ert-info ("Appending obeys `org-edit-src-content-indentation'")
(jupyter-org-test-src-block
"\
print(\"z\", flush=True)
print(\"z\")"
"\
#+BEGIN_EXAMPLE
z
z
#+END_EXAMPLE"
:async "yes"))))
(ert-deftest org-babel-jupyter-:dir-header-arg () (ert-deftest org-babel-jupyter-:dir-header-arg ()
:tags '(org) :tags '(org)

View file

@ -354,9 +354,7 @@ results instead of an equality match."
(defun jupyter-org-test-src-block-1 (code test-result &optional regexp args) (defun jupyter-org-test-src-block-1 (code test-result &optional regexp args)
(insert (jupyter-org-test-make-block code args)) (insert (jupyter-org-test-make-block code args))
(let* ((info (org-babel-get-src-block-info)) (let* ((info (org-babel-get-src-block-info))
(end (point-marker)) (end (point-marker)))
;; Don't indent example-block results
(org-edit-src-content-indentation 0))
(set-marker-insertion-type end t) (set-marker-insertion-type end t)
(save-window-excursion (save-window-excursion
(org-babel-execute-src-block nil info)) (org-babel-execute-src-block nil info))