From 2116162fe68165cc9cdd4cb778e41075f9f8e9e7 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Fri, 7 Oct 2016 23:45:08 +0200 Subject: [PATCH] ob-core: Prevent spurious blanks lines when executing code * lisp/ob-core.el (org-babel--insert-results-keyword): (org-babel-where-is-src-block-result): Control better the number of empty lines around results of a source block evaluation. * testing/lisp/test-ob.el (test-ob/blocks-with-spaces): Add tests. (test-ob/specific-colnames): Fix test. --- lisp/ob-core.el | 19 +++++++++------ testing/lisp/test-ob.el | 54 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 4d87ebc99..eac927109 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -1872,8 +1872,16 @@ the results hash, or nil. Leave point before the keyword." (t (format "[%s]" hash))) ":" (when name (concat " " name)) - "\n\n")) - (beginning-of-line -1) + "\n")) + ;; Make sure results are going to be followed by at least one blank + ;; line so they do not get merged with the next element, e.g., + ;; + ;; #+results: + ;; : 1 + ;; + ;; : fixed-width area, unrelated to the above. + (unless (looking-at "^[ \t]*$") (save-excursion (insert "\n"))) + (beginning-of-line 0) (when hash (org-babel-hide-hash))) (defun org-babel--clear-results-maybe (hash) @@ -1988,11 +1996,8 @@ to HASH." (goto-char (min (org-element-property :end context) (point-max))) (skip-chars-backward " \t\n") (forward-line) - (cond ((not (bolp)) (insert "\n\n")) - ((or (eobp) - (= (org-element-property :post-blank context) 0)) - (insert "\n")) - (t (forward-line))) + (unless (bolp) (insert "\n")) + (insert "\n") (org-babel--insert-results-keyword (org-element-property :name context) hash) (point)))))) diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el index caeae04d4..a03df7b40 100644 --- a/testing/lisp/test-ob.el +++ b/testing/lisp/test-ob.el @@ -1107,6 +1107,7 @@ Line 3\" (ert-deftest test-ob/blocks-with-spaces () "Test expansion of blocks followed by blank lines." + ;; Preserve number of blank lines after block. (should (equal "#+BEGIN_SRC emacs-lisp \(+ 1 2) @@ -1119,7 +1120,57 @@ Line 3\" #+END_SRC\n\n\n" (let ((org-babel-next-src-block "RESULTS")) (org-babel-execute-src-block)) - (buffer-string))))) + (buffer-string)))) + ;; Do not add spurious blank lines after results. + (should + (equal + " +- item 1 + + #+begin_src emacs-lisp + 0 + #+end_src + + #+RESULTS: + : 0 + +- item 2" + (org-test-with-temp-text " +- item 1 + + #+begin_src emacs-lisp + 0 + #+end_src + +- item 2" + (org-babel-execute-src-block) + (buffer-string)))) + (should + (equal + " +- item 1 + + #+begin_src emacs-lisp + 1 + #+end_src + + #+RESULTS: + : 1 + +- item 2" + (org-test-with-temp-text " +- item 1 + + #+begin_src emacs-lisp + 1 + #+end_src + + #+RESULTS: + : 1 + +- item 2" + (org-babel-execute-src-block) + (buffer-string))))) (ert-deftest test-ob/results-in-narrowed-buffer () "Test block execution in a narrowed buffer." @@ -1203,7 +1254,6 @@ echo \"$data\" |-----+--------| | 1 | bar | | 2 | baz | - " (org-test-with-temp-text "#+name: input-table