From 326d51913a0d69fa42c67814f11f2c844acc0076 Mon Sep 17 00:00:00 2001 From: Nicolas Berthier Date: Fri, 1 Aug 2014 11:28:05 +0200 Subject: [PATCH 3/3] ob-core: Preserve inline-ness of source blocks when inserting results * lisp/ob-core.el (org-babel-insert-result): Preserve inline-ness of source blocks. * testing/lisp/test-ob-exp.el: Update newly passing tests. --- lisp/ob-core.el | 36 ++++++++++++++++++++++++------------ testing/lisp/test-ob-exp.el | 19 +++++-------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index c5beb60..50a13c2 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -2094,9 +2094,11 @@ drawer -- results are added directly to the Org-mode file as with \"raw\", but are wrapped in a RESULTS drawer, allowing them to later be replaced or removed automatically. -org ----- results are added inside of a \"#+BEGIN_SRC org\" block. - They are not comma-escaped when inserted, but Org syntax - here will be discarded when exporting the file. +org ----- results are added inside of a \"src_org{}\" or \"#+BEGIN_SRC + org\" block depending on whether the current source block is + inline or not. They are not comma-escaped when inserted, + but Org syntax here will be discarded when exporting the + file. html ---- results are added inside of a #+BEGIN_HTML block. This is a good option if you code block will output html @@ -2108,8 +2110,9 @@ latex --- results are added inside of a #+BEGIN_LATEX block. code ---- the results are extracted in the syntax of the source code of the language being evaluated and are added - inside of a #+BEGIN_SRC block with the source-code - language set appropriately. Note this relies on the + inside of a source block with the source-code language + set appropriately. Also, source block inlining is + preserved in this case. Note this relies on the optional LANG argument." (if (stringp result) (progn @@ -2171,12 +2174,15 @@ code ---- the results are extracted in the syntax of the source ((member "prepend" result-params)))) ; already there (setq results-switches (if results-switches (concat " " results-switches) "")) - (let ((wrap (lambda (start finish &optional no-escape) - (goto-char end) (insert (concat finish "\n")) - (goto-char beg) (insert (concat start "\n")) + (let ((wrap (lambda (start finish &optional no-escape no-newlines) + (goto-char end) + (insert (concat finish (unless no-newlines "\n"))) + (goto-char beg) + (insert (concat start (unless no-newlines "\n"))) (unless no-escape (org-escape-code-in-region (min (point) end) end)) - (goto-char end) (goto-char (point-at-eol)) + (goto-char end) + (unless no-newlines (goto-char (point-at-eol))) (setq end (point-marker)))) (proper-list-p (lambda (it) (and (listp it) (null (cdr (last it))))))) ;; insert results based on type @@ -2224,10 +2230,16 @@ code ---- the results are extracted in the syntax of the source (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX")) ((member "org" result-params) (goto-char beg) (if (org-at-table-p) (org-cycle)) - (funcall wrap "#+BEGIN_SRC org" "#+END_SRC")) + (if inlinep + (funcall wrap "src_org{" "}" nil t) + (funcall wrap "#+BEGIN_SRC org" "#+END_SRC"))) ((member "code" result-params) - (funcall wrap (format "#+BEGIN_SRC %s%s" (or lang "none") results-switches) - "#+END_SRC")) + (let ((lang (or lang "none"))) + (if inlinep + (funcall wrap (format "src_%s[%s]{" lang results-switches) + "}" nil t) + (funcall wrap (format "#+BEGIN_SRC %s%s" lang results-switches) + "#+END_SRC")))) ((member "raw" result-params) (goto-char beg) (if (org-at-table-p) (org-cycle))) ((or (member "drawer" result-params) diff --git a/testing/lisp/test-ob-exp.el b/testing/lisp/test-ob-exp.el index e319bd2..456f794 100644 --- a/testing/lisp/test-ob-exp.el +++ b/testing/lisp/test-ob-exp.el @@ -249,14 +249,10 @@ Here is one that is also evaluated: src_sh[]{echo 4} =4=") (org-narrow-to-subtree) (org-test-with-expanded-babel-code (buffer-string)))))) -(ert-deftest ob-exp/exports-inline-code-double-eval-bug () - "Failing for now as the result actually is -`#+BEGIN_SRC emacs-lisp\n2#+END_SRC\n'. - -Based on default header arguments for inline code blocks (:exports +(ert-deftest ob-exp/exports-inline-code-double-eval () + "Based on default header arguments for inline code blocks (:exports results), the resulting code block `src_emacs-lisp{2}' should also be evaluated." - :expected-result :failed (should (string-match "\\`=2=$" (org-test-with-temp-text @@ -264,11 +260,9 @@ evaluated." (org-export-execute-babel-code) (buffer-string))))) -(ert-deftest ob-exp/exports-inline-code-eval-code-once-bug () +(ert-deftest ob-exp/exports-inline-code-eval-code-once () "Ibid above, except that the resulting inline code block should not -be evaluated. Result for now is -`#+BEGIN_SRC emacs-lisp :exports code\n2#+END_SRC\n'" - :expected-result :failed +be evaluated." (should (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{2}$" (org-test-with-temp-text @@ -277,10 +271,7 @@ be evaluated. Result for now is (org-export-execute-babel-code) (buffer-string))))) -(ert-deftest ob-exp/exports-inline-code-double-eval-exports-both-bug () - "Failing for now as the result actually is -`src_emacs-lisp[]{(+ 1 1)} #+BEGIN_SRC emacs-lisp\n2#+END_SRC\n'." - :expected-result :failed +(ert-deftest ob-exp/exports-inline-code-double-eval-exports-both () (should (string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} " "src_emacs-lisp\\(?:\\[]\\)?{2}$") -- 2.0.0.rc4