ob: Fix block evaluation in a narrowed buffer

* lisp/ob.el (org-babel-where-is-src-block-result): Insert new results
  keyword in current narrowed part of buffer, if necessary. Small
  refactoring.
(org-babel-insert-result): Do not widen buffer when new results have
to be inserted.  Therefore, results inserted after the last block of
a narrowed buffer still belong to the narrowed part of the buffer.
* testing/lisp/test-ob.el: Add tests.
* testing/lisp/test-ob-exp.el: Move test to test-ob.el
This commit is contained in:
Nicolas Goaziou 2012-10-24 17:23:20 +02:00
parent ccc98ebc2d
commit 2f2a80fe06
3 changed files with 237 additions and 160 deletions

View File

@ -1723,6 +1723,7 @@ following the source block."
(head (unless on-lob-line (org-babel-where-is-src-block-head)))
found beg end)
(when head (goto-char head))
(org-with-wide-buffer
(setq
found ;; was there a result (before we potentially insert one)
(or
@ -1759,18 +1760,13 @@ following the source block."
(forward-line 1)
(delete-region
end (org-babel-result-end)))
(setq end nil)))))))))
(if (and insert end)
(progn
(setq end nil))))))))))
(if (not (and insert end)) found
(goto-char end)
(unless beg
(if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
(insert (concat
(if indent
(mapconcat
(lambda (el) " ")
(org-number-sequence 1 indent) "")
"")
(when (wholenump indent) (make-string indent ? ))
"#+" org-babel-results-keyword
(when hash (concat "["hash"]"))
":"
@ -1778,8 +1774,7 @@ following the source block."
(unless beg (insert "\n") (backward-char))
(beginning-of-line 0)
(if hash (org-babel-hide-hash))
(point))
found))))
(point)))))
(defvar org-block-regexp)
(defun org-babel-read-result ()
@ -1888,7 +1883,6 @@ code ---- the results are extracted in the syntax of the source
inside of a #+BEGIN_SRC block with the source-code
language set appropriately. Note this relies on the
optional LANG argument."
(save-restriction (widen)
(if (stringp result)
(progn
(setq result (org-no-properties result))
@ -1915,6 +1909,14 @@ code ---- the results are extracted in the syntax of the source
t info hash indent)))
(results-switches
(cdr (assoc :results_switches (nth 2 info))))
(visible-beg (copy-marker (point-min)))
(visible-end (copy-marker (point-max)))
;; When results exist outside of the current visible
;; region of the buffer, be sure to widen buffer to
;; update them.
(outside-scope-p (and existing-result
(or (> visible-beg existing-result)
(<= visible-end existing-result))))
beg end)
(when (and (stringp result) ; ensure results end in a newline
(not inlinep)
@ -1922,6 +1924,9 @@ code ---- the results are extracted in the syntax of the source
(not (or (string-equal (substring result -1) "\n")
(string-equal (substring result -1) "\r"))))
(setq result (concat result "\n")))
(unwind-protect
(progn
(when outside-scope-p (widen))
(if (not existing-result)
(setq beg (or inlinep (point)))
(goto-char existing-result)
@ -2007,12 +2012,15 @@ code ---- the results are extracted in the syntax of the source
;; in this case `table-align' does the work for us
(not (and (listp result)
(member "append" result-params))))
(indent-rigidly beg end indent))))
(indent-rigidly beg end indent))
(if (null result)
(if (member "value" result-params)
(message "Code block returned no value.")
(message "Code block produced no output."))
(message "Code block evaluation complete.")))))
(message "Code block evaluation complete.")))
(when outside-scope-p (narrow-to-region visible-beg visible-end))
(set-marker visible-beg nil)
(set-marker visible-end nil))))))
(defun org-babel-remove-result (&optional info)
"Remove the result of the current source block."

View File

@ -273,17 +273,6 @@ elements in the final html."
(should (string-match (regexp-quote (format nil "%S" '(:foo :bar)))
ascii)))))
(ert-deftest ob-exp/blocks-with-spaces ()
"Test expansion of blocks followed by blank lines."
(should
(equal "#+RESULTS:\n: 3\n\n\n"
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp :exports results
\(+ 1 2)
#+END_SRC\n\n\n"
(let ((org-current-export-file (current-buffer)))
(org-export-blocks-preprocess)
(buffer-string))))))
(provide 'test-ob-exp)

View File

@ -1033,6 +1033,86 @@ Line 3\"
(move-beginning-of-line 0)
(should (looking-at (format ": %d" num))))))
(ert-deftest test-ob/blocks-with-spaces ()
"Test expansion of blocks followed by blank lines."
(should
(equal "#+BEGIN_SRC emacs-lisp
\(+ 1 2)
#+END_SRC
#+RESULTS:
: 3\n\n\n"
(org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
\(+ 1 2)
#+END_SRC\n\n\n"
(progn (org-babel-execute-src-block)
(buffer-string))))))
(ert-deftest test-ob/results-in-narrowed-buffer ()
"Test block execution in a narrowed buffer."
;; If results don't exist, they should be inserted in visible part
;; of the buffer.
(should
(equal
"#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
(org-test-with-temp-text
"#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
(progn
(narrow-to-region (point) (save-excursion (forward-line 3) (point)))
(org-babel-execute-src-block)
(org-trim (buffer-string))))))
(should
(equal
"#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
(org-test-with-temp-text
"#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
(progn
(narrow-to-region (point) (save-excursion (forward-line 4) (point)))
(org-babel-execute-src-block)
(org-trim (buffer-string))))))
;; Results in visible part of buffer, should be updated here.
(should
(equal
"#+NAME: test
#+BEGIN_SRC emacs-lisp
\(+ 1 2)
#+END_SRC
#+RESULTS: test
: 3"
(org-test-with-temp-text
"#+NAME: test
#+BEGIN_SRC emacs-lisp
\(+ 1 2)
#+END_SRC
#+RESULTS: test
: 4
Paragraph"
(progn
(narrow-to-region (point) (save-excursion (forward-line 7) (point)))
(org-babel-execute-src-block)
(org-trim (buffer-string))))))
;; Results in invisible part of buffer, should be updated there.
(org-test-with-temp-text
"#+NAME: test
#+BEGIN_SRC emacs-lisp
\(+ 1 2)
#+END_SRC
#+RESULTS: test
: 4
Paragraph"
(progn
(narrow-to-region (point) (save-excursion (forward-line 4) (point)))
(org-babel-execute-src-block)
(should-not (re-search-forward "^#\\+RESULTS:" nil t))
(widen)
(should (should (re-search-forward "^: 3" nil t))))))
(provide 'test-ob)
;;; test-ob ends here