소스 검색

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.
Nicolas Goaziou 8 년 전
부모
커밋
2116162fe6
2개의 변경된 파일64개의 추가작업 그리고 9개의 파일을 삭제
  1. 12 7
      lisp/ob-core.el
  2. 52 2
      testing/lisp/test-ob.el

+ 12 - 7
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))))))

+ 52 - 2
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<point>
+  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<point>
+  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