org-sbe: Fix when the macro is expanded outside Org file

* lisp/ob-table.el (org-sbe): Do not try to execute code block during
macro expansion.
* testing/lisp/test-ob-table.el (test-ob-table/sbe): Uncomment the
test.  Update the expected return value.
This commit is contained in:
Ihor Radchenko 2022-11-23 17:26:55 +08:00
parent bd384d3267
commit 89af7b11a4
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 38 additions and 40 deletions

View File

@ -112,44 +112,43 @@ as shown in the example below.
;; ensure that all cells prefixed with $'s are strings
(cons (car var)
(delq nil (mapcar
(lambda (el)
(if (eq '$ el)
(prog1 nil (setq quote t))
(prog1
(cond
(quote (format "\"%s\"" el))
((stringp el) (org-no-properties el))
(t el))
(setq quote nil))))
(cdr var)))))
(lambda (el)
(if (eq '$ el)
(prog1 nil (setq quote t))
(prog1
(cond
(quote (format "\"%s\"" el))
((stringp el) (org-no-properties el))
(t el))
(setq quote nil))))
(cdr var)))))
variables)))
(unless (stringp source-block)
(setq source-block (symbol-name source-block)))
(let ((result
(if (and source-block (> (length source-block) 0))
(let ((params
;; FIXME: Why `eval'?!?!?
(eval `(org-babel-parse-header-arguments
(concat
":var results="
,source-block
"[" ,header-args "]"
"("
(mapconcat
(lambda (var-spec)
(if (> (length (cdr var-spec)) 1)
(format "%S='%S"
(car var-spec)
(mapcar #'read (cdr var-spec)))
(format "%S=%s"
(car var-spec) (cadr var-spec))))
',variables ", ")
")")))))
(org-babel-execute-src-block
nil (list "emacs-lisp" "results" params)
'((:results . "silent"))))
"")))
(org-trim (if (stringp result) result (format "%S" result)))))))
`(let ((result
(if ,(and source-block (> (length source-block) 0))
(let ((params
',(org-babel-parse-header-arguments
(concat
":var results="
source-block
"[" header-args "]"
"("
(mapconcat
(lambda (var-spec)
(if (> (length (cdr var-spec)) 1)
(format "%S='%S"
(car var-spec)
(mapcar #'read (cdr var-spec)))
(format "%S=%s"
(car var-spec) (cadr var-spec))))
variables ", ")
")"))))
(org-babel-execute-src-block
nil (list "emacs-lisp" "results" params)
'((:results . "silent"))))
"")))
(org-trim (if (stringp result) result (format "%S" result)))))))
(provide 'ob-table)

View File

@ -24,11 +24,10 @@
;;; Code:
;; TODO Test Broken (wrong-type-argument number-or-marker-p "2.0")
;; (ert-deftest test-ob-table/sbe ()
;; "Test that `sbe' can be used to call code blocks from inside tables."
;; (org-test-at-id "6d2ff4ce-4489-4e2a-9c65-e3f71f77d975"
;; (should (= 2 (sbe take-sqrt (n "4"))))))
(ert-deftest test-ob-table/sbe ()
"Test that `sbe' can be used to call code blocks from inside tables."
(org-test-at-id "6d2ff4ce-4489-4e2a-9c65-e3f71f77d975"
(should (equal "2.0" (org-sbe take-sqrt (n "4"))))))
(provide 'test-ob-table)