summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2017-11-25 21:07:52 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2017-11-25 21:07:52 +0100
commit1d8126385cf979cfaade0e6a82040884bd6af56b (patch)
treea98202b89ef1514c24a8de6be85301606d70c9fa
parented06b159c328d6b7781527725a49b8833ed18bfc (diff)
downloadorg-mode-1d8126385cf979cfaade0e6a82040884bd6af56b.tar.gz
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>
-rw-r--r--lisp/ob-core.el34
-rw-r--r--testing/lisp/test-ob.el47
2 files changed, 38 insertions, 43 deletions
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index c7c0384..106cfbd 100644
--- a/lisp/ob-core.el
+++ b/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.
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index cc73946..9d9453a 100644
--- a/testing/lisp/test-ob.el
+++ b/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)))))