From 9f15db7133824a2fdacc31b1cae46d5e32deb6b8 Mon Sep 17 00:00:00 2001 From: Nathaniel Nicandro Date: Tue, 11 Jun 2019 21:00:27 -0500 Subject: [PATCH] jupyter-org--append-to-example-block: Fix edge case Fix the case when appending to a line without inserting a newline and adding indentation. Also add tests for `jupyter-org--append-to-example-block` --- jupyter-org-client.el | 18 +++++++--- test/jupyter-test.el | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/jupyter-org-client.el b/jupyter-org-client.el index dc7ce9a..2f2c157 100644 --- a/jupyter-org-client.el +++ b/jupyter-org-client.el @@ -1253,11 +1253,19 @@ RESULT." (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))))) + (let ((ind (make-string org-edit-src-content-indentation ?\s)) + head tail) + (if keep-newline + (setq head "" + tail result) + (let ((first-newline (save-match-data + (string-match "\n" result)))) + (setq head (substring result 0 (1+ first-newline)) + tail (substring result (1+ first-newline))))) + (setq result (concat head (replace-regexp-in-string + "^[ \t]*\\S-" + (concat ind "\\&") + (org-remove-indentation tail)))))) (insert (concat (when keep-newline "\n") result))) (defun jupyter-org--append-stream-result (result) diff --git a/test/jupyter-test.el b/test/jupyter-test.el index eebc709..a2c1d29 100644 --- a/test/jupyter-test.el +++ b/test/jupyter-test.el @@ -2012,6 +2012,88 @@ file:foo : b ")))) +(defvar org-edit-src-preserve-indentation) +(defvar org-src-preserve-indentation) + +(ert-deftest jupyter-org--append-to-example-block () + :tags '(org) + (let ((org-src-preserve-indentation 0)) + (with-temp-buffer + (org-mode) + (insert "\ +#+begin_example +| +#+end_example +") + (search-backward "|") + (forward-char) + (jupyter-org--append-to-example-block "a\nb" nil) + (should (equal (buffer-string) "\ +#+begin_example +|a +b +#+end_example +")) + (backward-char) + (jupyter-org--append-to-example-block "a\nb" t) + (should (equal (buffer-string) "\ +#+begin_example +|a +b +a +b +#+end_example +")))) + (when (version<= "9.2" (org-version)) + (let ((org-src-preserve-indentation nil) + (org-edit-src-preserve-indentation 2)) + (ert-info ("Example block indentation") + (with-temp-buffer + (org-mode) + (insert "\ +#+begin_example + | +#+end_example +") + (search-backward "|") + (forward-char) + (jupyter-org--append-to-example-block "ab" nil) + (should (equal (buffer-string) "\ +#+begin_example + |ab +#+end_example +")) + (backward-char) + (jupyter-org--append-to-example-block "ab" t) + (should (equal (buffer-string) "\ +#+begin_example + |ab + ab +#+end_example +")) + (backward-char) + ;; TODO: What to about the case of removing the common indentation + ;; while appending to a line? + (jupyter-org--append-to-example-block " a\n b" nil) + (should (equal (buffer-string) "\ +#+begin_example + |ab + ab a + b +#+end_example +")) + (backward-char) + (jupyter-org--append-to-example-block " a\n b" t) + (should (equal (buffer-string) "\ +#+begin_example + |ab + ab a + b + a + b +#+end_example +"))))))) + (ert-deftest jupyter-org-indent-inserted-region () :tags '(org) (with-temp-buffer