Browse Source

ob-core: Fix results removal without blank line between source and results

* lisp/ob-core.el (org-babel-remove-result): Delete blank lines before
  results.
(org-babel-result-end): Use `org-element-at-point'.
* testing/lisp/test-ob.el (test-ob-verify-result-and-removed-result):
  Remove duplicate.
* testing/lisp/test-ob.el (test-ob/org-babel-remove-result--no-blank-line):
  New test.
* testing/lisp/test-ob.el (test-ob/results-in-narrowed-buffer): Small
  refactoring.

Reported-by: Ken Mankoff <mankoff@gmail.com>
<http://lists.gnu.org/archive/html/emacs-orgmode/2017-11/msg00338.html>
Nicolas Goaziou 7 years ago
parent
commit
1d8126385c
2 changed files with 38 additions and 43 deletions
  1. 14 20
      lisp/ob-core.el
  2. 24 23
      testing/lisp/test-ob.el

+ 14 - 20
lisp/ob-core.el

@@ -2416,8 +2416,11 @@ INFO may provide the values of these header arguments (in the
         (goto-char location)
 	(when (looking-at (concat org-babel-result-regexp ".*$"))
 	  (delete-region
-	   (if keep-keyword (1+ (match-end 0)) (1- (match-beginning 0)))
-	   (progn (forward-line 1) (org-babel-result-end))))))))
+	   (if keep-keyword (line-beginning-position 2)
+	     (save-excursion
+	       (skip-chars-backward " \r\t\n")
+	       (line-beginning-position 2)))
+	   (progn (forward-line) (org-babel-result-end))))))))
 
 (defun org-babel-remove-inline-result (&optional datum)
   "Remove the result of the current inline-src-block or babel call.
@@ -2454,24 +2457,15 @@ in the buffer."
 
 (defun org-babel-result-end ()
   "Return the point at the end of the current set of results."
-  (save-excursion
-    (cond
-     ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
-     ((org-at-item-p) (let* ((struct (org-list-struct))
-			     (prvs (org-list-prevs-alist struct)))
-			(org-list-get-list-end (point-at-bol) struct prvs)))
-     ((let ((case-fold-search t)) (looking-at "^\\([ \t]*\\):results:"))
-      (progn (re-search-forward (concat "^" (match-string 1) ":END:"))
-	     (forward-char 1) (point)))
-     (t
-      (let ((case-fold-search t))
-	(if (looking-at (concat "[ \t]*#\\+begin_\\([^ \t\n\r]+\\)"))
-	    (progn (re-search-forward (concat "[ \t]*#\\+end_" (match-string 1))
-				      nil t)
-		   (forward-char 1))
-	  (while (looking-at "[ \t]*\\(: \\|:$\\|\\[\\[\\)")
-	    (forward-line 1))))
-      (point)))))
+  (cond ((looking-at-p "^[ \t]*$") (point)) ;no result
+	((looking-at-p (format "^[ \t]*%s[ \t]*$" org-bracket-link-regexp))
+	 (line-beginning-position 2))
+	(t (save-excursion
+	     (goto-char
+	      (min (point-max)		;for narrowed buffers
+		   (org-element-property :end (org-element-at-point))))
+	     (skip-chars-backward " \r\t\n")
+	     (line-beginning-position 2)))))
 
 (defun org-babel-result-to-file (result &optional description)
   "Convert RESULT into an `org-mode' link with optional DESCRIPTION.

+ 24 - 23
testing/lisp/test-ob.el

@@ -889,27 +889,6 @@ x
 		       (buffer-substring-no-properties
 			(point-at-bol) (point-at-eol)))))))
 
-(defun test-ob-verify-result-and-removed-result (result buffer-text)
-  "Test helper function to test `org-babel-remove-result'.
-A temp buffer is populated with BUFFER-TEXT, the first block is executed,
-and the result of execution is verified against RESULT.
-
-The block is actually executed /twice/ to ensure result
-replacement happens correctly."
-  (org-test-with-temp-text
-      buffer-text
-    (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
-    (should (re-search-forward "\\#\\+results:" nil t))
-    (forward-line)
-    (should (string= result
-		     (buffer-substring-no-properties
-		      (point-at-bol)
-		      (- (point-max) 16))))
-    (org-babel-previous-src-block) (org-babel-remove-result)
-    (should (string= buffer-text
-		     (buffer-substring-no-properties
-		      (point-min) (point-max))))))
-
 (ert-deftest test-ob/org-babel-remove-result--results-default ()
   "Test `org-babel-remove-result' with default :results."
   (mapcar (lambda (language)
@@ -1114,6 +1093,27 @@ Line 3\"
 
 * next heading"))
 
+(ert-deftest test-ob/org-babel-remove-result--no-blank-line ()
+  "Test `org-babel-remove-result' without blank line between code and results."
+  (should
+   (equal "
+#+begin_src emacs-lisp
+  (+ 1 1)
+#+end_src
+#+results:
+: 2
+* next heading"
+	  (org-test-with-temp-text
+	      "
+<point>#+begin_src emacs-lisp
+  (+ 1 1)
+#+end_src
+#+results:
+: 2
+* next heading"
+	    (org-babel-execute-maybe)
+	    (buffer-string)))))
+
 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
   (org-test-with-temp-text "Block two has a space after the name.
 
@@ -1251,9 +1251,10 @@ Line 3\"
 
 #+RESULTS: test
 : 4
-
+<point>
 Paragraph"
-      (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
+      (narrow-to-region (point-min) (point))
+      (goto-char (point-min))
       (let ((org-babel-results-keyword "RESULTS"))
 	(org-babel-execute-src-block))
       (org-trim (buffer-string)))))