summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2020-04-07 01:42:35 +0200
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2020-04-07 01:42:35 +0200
commit06fe9d6b05ddc613fe6f7f4de11b4893b2015567 (patch)
treecb07f85bc3af426663e03e784db6863bf99aeda9
parent0a7e382c27b95ff14d3a4fd797649b76b66d3138 (diff)
downloadorg-mode-06fe9d6b05ddc613fe6f7f4de11b4893b2015567.tar.gz
ob: Improve handling of :wrap header
* lisp/ob-core.el (org-babel-insert-result): Improve handling of :wrap header. * testing/lisp/test-ob.el (test-ob/inline-src_blk-wrap): New test. (test-ob/preserve-comma-escape): Add test.
-rw-r--r--lisp/ob-core.el55
-rw-r--r--testing/lisp/test-ob.el68
2 files changed, 117 insertions, 6 deletions
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 93bf312..b43e63e 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2373,13 +2373,58 @@ INFO may provide the values of these header arguments (in the
(org-babel-chomp result "\n"))))
(t (goto-char beg) (insert result)))
(setq end (copy-marker (point) t))
- ;; possibly wrap result
+ ;; Possibly wrap result.
(cond
((assq :wrap (nth 2 info))
- (let ((name (or (cdr (assq :wrap (nth 2 info))) "results")))
- (funcall wrap (concat "#+begin_" name)
- (concat "#+end_" (car (split-string name)))
- nil nil (concat "{{{results(@@" name ":") "@@)}}}")))
+ (let* ((full (or (cdr (assq :wrap (nth 2 info))) "results"))
+ (split (split-string full))
+ (type (car split))
+ (opening-line (concat "#+begin_" full))
+ (closing-line (concat "#+end_" type)))
+ (cond
+ ;; Escape contents from "export" wrap. Wrap
+ ;; inline results within an export snippet with
+ ;; appropriate value.
+ ((eq t (compare-strings type nil nil "export" nil nil t))
+ (let ((backend (pcase split
+ (`(,_) "none")
+ (`(,_ ,b . ,_) b))))
+ (funcall wrap
+ opening-line closing-line
+ nil nil
+ (format "{{{results(@@%s:"
+ backend) "@@)}}}")))
+ ;; Escape contents from "example" wrap. Mark
+ ;; inline results as verbatim.
+ ((eq t (compare-strings type nil nil "example" nil nil t))
+ (funcall wrap
+ opening-line closing-line
+ nil nil
+ "{{{results(=" "=)}}}"))
+ ;; Escape contents from "src" wrap. Mark
+ ;; inline results as inline source code.
+ ((eq t (compare-strings type nil nil "src" nil nil t))
+ (let ((inline-open
+ (pcase split
+ (`(,_)
+ "{{{results(src_none{")
+ (`(,_ ,language)
+ (format "{{{results(src_%s{" language))
+ (`(,_ ,language . ,rest)
+ (let ((r (mapconcat #'identity rest " ")))
+ (format "{{{results(src_%s[%s]{"
+ language r))))))
+ (funcall wrap
+ opening-line closing-line
+ nil nil
+ inline-open "})}}}")))
+ ;; Do not escape contents in non-verbatim
+ ;; blocks. Return plain inline results.
+ (t
+ (funcall wrap
+ opening-line closing-line
+ t nil
+ "{{{results(" ")}}}")))))
((member "html" result-params)
(funcall wrap "#+begin_export html" "#+end_export" nil nil
"{{{results(@@html:" "@@)}}}"))
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index 8d1a2a7..c02bcd1 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -465,6 +465,47 @@ at the beginning of a line."
(buffer-substring-no-properties
(point-min) (point-max)))))))
+(ert-deftest test-ob/inline-src_blk-wrap ()
+ (let ((org-babel-inline-result-wrap "=%s="))
+ ;; Export: use export snippet.
+ (should
+ (string-match-p
+ "@@foo:1@@"
+ (org-test-with-temp-text "src_emacs-lisp[:wrap export foo]{1}"
+ (org-babel-execute-maybe)
+ (buffer-string))))
+ (should
+ (string-match-p
+ "src_foo{1}"
+ (org-test-with-temp-text "src_emacs-lisp[:wrap src foo]{1}"
+ (org-babel-execute-maybe)
+ (buffer-string))))
+ (should
+ (string-match-p
+ "src_foo\\[parameter\\]{1}"
+ (org-test-with-temp-text "src_emacs-lisp[:wrap src foo parameter]{1}"
+ (org-babel-execute-maybe)
+ (buffer-string))))
+ (should
+ (string-match-p
+ "=1="
+ (org-test-with-temp-text "src_emacs-lisp[:wrap example]{1}"
+ (org-babel-execute-maybe)
+ (buffer-string))))
+ ;; Anything else is ignored.
+ (should
+ (string-match-p
+ "{{{results(1)}}}"
+ (org-test-with-temp-text "src_emacs-lisp[:wrap foo]{1}"
+ (org-babel-execute-maybe)
+ (buffer-string))))
+ (should
+ (string-match-p
+ "{{{results(a\\\\,b)}}}"
+ (org-test-with-temp-text "src_emacs-lisp[:wrap foo]{\"a,b\"}"
+ (org-babel-execute-maybe)
+ (buffer-string))))))
+
(ert-deftest test-ob/combining-scalar-and-raw-result-types ()
(org-test-with-temp-text-in-file "
@@ -1603,7 +1644,32 @@ line 1
(org-babel-execute-src-block)
(let ((case-fold-search t)) (search-forward "result" nil t))
(downcase (buffer-substring-no-properties (line-beginning-position 2)
- (point-max)))))))
+ (point-max))))))
+ (should
+ (string-match-p
+ ",#"
+ (org-test-with-temp-text "#+begin_src emacs-lisp :wrap export foo
+\"#+keyword: value\"
+#+end_src"
+ (org-babel-execute-src-block)
+ (buffer-string))))
+ (should
+ (string-match-p
+ ",#"
+ (org-test-with-temp-text "#+begin_src emacs-lisp :wrap src foo
+\"#+keyword: value\"
+#+end_src"
+ (org-babel-execute-src-block)
+ (buffer-string))))
+ ;; Do not comma-escape when the block is not verbatim.
+ (should-not
+ (string-match-p
+ ",#"
+ (org-test-with-temp-text "#+begin_src emacs-lisp :wrap special
+\"#+keyword: value\"
+#+end_src"
+ (org-babel-execute-src-block)
+ (buffer-string)))))
(ert-deftest test-ob/safe-header-args ()
"Detect safe and unsafe header args."