From 70f4b1b9a3b7e303c4a6b560b12eb86a469e1cdc Mon Sep 17 00:00:00 2001 From: Nathaniel Nicandro Date: Thu, 21 Feb 2019 11:35:47 -0600 Subject: [PATCH] jupyter-org--append-stream-result: Consider `org-edit-src-content-indentation` --- jupyter-org-client.el | 13 +++- test/jupyter-test.el | 155 ++++++++++++++++++++++++------------------ test/test-helper.el | 4 +- 3 files changed, 99 insertions(+), 73 deletions(-) diff --git a/jupyter-org-client.el b/jupyter-org-client.el index c481ac1..d817aa2 100644 --- a/jupyter-org-client.el +++ b/jupyter-org-client.el @@ -1079,8 +1079,17 @@ insertion into the buffer." ;; Delete the newline that will be re-inserted by the call to ;; `org-element-normalize-string'. (delete-char 1) - (insert (concat (when keep-newline "\n") - (org-element-normalize-string result)))))) + (setq 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 diff --git a/test/jupyter-test.el b/test/jupyter-test.el index 74e2cae..9be3abd 100644 --- a/test/jupyter-test.el +++ b/test/jupyter-test.el @@ -1393,120 +1393,139 @@ file:foo (ert-deftest jupyter-org-coalesce-stream-results () :tags '(org) - (ert-info ("Synchronous") - (jupyter-org-test-src-block - "\ + (let ((org-edit-src-content-indentation 0)) + (ert-info ("Synchronous") + (jupyter-org-test-src-block + "\ print(\"foo\") print(\"foo\", flush=True) print(\"foo\")" - "\ + "\ : foo : foo : foo")) - (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 ("Asynchronous") (ert-info ("Newline after first stream message") (jupyter-org-test-src-block "\ print(\"foo\") print(\"foo\", flush=True) -print(\"bar\\nqux\")" +print(\"foo\")" "\ : foo : foo -: bar -: qux" - :async "yes")) +: 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\\nqux\")" +print(\"bar\")" "\ : 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 : qux" - :async "yes"))) - (ert-info ("fixed-width to example-block promotion") - (let ((org-babel-min-lines-for-block-output 2)) - (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") + :async "yes"))) + (ert-info ("fixed-width to example-block promotion") + (let ((org-babel-min-lines-for-block-output 2)) + (jupyter-org-test-src-block "print(\"z\")" ": z") (jupyter-org-test-src-block "\ print(\"z\", flush=True) -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 + "\ +print(\"z\", flush=True) +print(\"z\", flush=True) +print(\"z\")" + "\ +#+BEGIN_EXAMPLE +z +z z #+END_EXAMPLE" - :async "yes")) - (ert-info ("Append to block with newline after first stream message") - (jupyter-org-test-src-block - "\ + :async "yes")) + (ert-info ("Append to block with newline after first stream message") + (jupyter-org-test-src-block + "\ print(\"z\\nz\", flush=True) print(\"z\")" - "\ + "\ #+BEGIN_EXAMPLE z z z #+END_EXAMPLE" - :async "yes")) - (ert-info ("Append to block without newline after first stream message") - (jupyter-org-test-src-block - "\ + :async "yes")) + (ert-info ("Append to block without newline after first stream message") + (jupyter-org-test-src-block + "\ print(\"z\\nz\", end=\"\", flush=True) print(\"z\")" - "\ + "\ #+BEGIN_EXAMPLE z zz #+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 () :tags '(org) diff --git a/test/test-helper.el b/test/test-helper.el index d5291c2..946eef0 100644 --- a/test/test-helper.el +++ b/test/test-helper.el @@ -354,9 +354,7 @@ results instead of an equality match." (defun jupyter-org-test-src-block-1 (code test-result &optional regexp args) (insert (jupyter-org-test-make-block code args)) (let* ((info (org-babel-get-src-block-info)) - (end (point-marker)) - ;; Don't indent example-block results - (org-edit-src-content-indentation 0)) + (end (point-marker))) (set-marker-insertion-type end t) (save-window-excursion (org-babel-execute-src-block nil info))