branch: externals/org
commit fbfd065ee134ff7708114f16745a858039cac9df
Author: Ihor Radchenko <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    ob-scheme: Implement conversion from Scheme to Elisp cons cells
    
    * lisp/ob-scheme.el (org-babel-scheme--table-or-string): Consider
    improper lists and cons cells.
    * testing/lisp/test-ob-scheme.el (test-ob-scheme/list-conversion): Add
    new test.
    
    Reported-by: [email protected]
    Link: 
https://orgmode.org/list/[email protected]
---
 lisp/ob-scheme.el              | 20 +++++++++++++++++++-
 testing/lisp/test-ob-scheme.el | 25 +++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-scheme.el b/lisp/ob-scheme.el
index 084d7b75ef..5a8c1dcd87 100644
--- a/lisp/ob-scheme.el
+++ b/lisp/ob-scheme.el
@@ -233,12 +233,30 @@ is true; otherwise returns the last value."
 If the results look like a list or tuple, then convert them into an
 Emacs-lisp table, otherwise return the results as a string."
   (let ((res (and results (org-babel-script-escape results))))
-    (cond ((listp res)
+    (cond ((proper-list-p res)
            (mapcar (lambda (el)
                     (if (or (null el) (eq el 'null))
                         org-babel-scheme-null-to
                       el))
                    res))
+          ((consp res) ; improper list ending with cons cell
+           (cl-labels ((maybe-convert (el)
+                         (if (or (null el) (eq el 'null))
+                             org-babel-scheme-null-to
+                           el)))
+             (let* ((converted (cons (maybe-convert (car res)) nil))
+                    (tail converted))
+               (setq res (cdr res))
+               (while res
+                 (if (not (consp res))
+                     ;; end of cons
+                     (progn
+                       (setcdr tail res)
+                       (setq res nil))
+                   (setcdr tail (list (maybe-convert (car res))))
+                   (setq tail (cdr tail))
+                   (setq res (cdr res))))
+               converted)))
          (t res))))
 
 (defun org-babel-execute:scheme (body params)
diff --git a/testing/lisp/test-ob-scheme.el b/testing/lisp/test-ob-scheme.el
index 0fb79ad532..ccd34dcb84 100644
--- a/testing/lisp/test-ob-scheme.el
+++ b/testing/lisp/test-ob-scheme.el
@@ -64,6 +64,31 @@
            (buffer-substring-no-properties (line-beginning-position 2)
                                            (point-max))))))
 
+(ert-deftest test-ob-scheme/list-conversion ()
+  "Test list conversion from Scheme to Elisp."
+  (should
+   (equal ": (1 hline 3)"
+         (org-test-with-temp-text "#+begin_src scheme\n'(1 null 3)\n#+end_src"
+           (org-babel-execute-maybe)
+           (let ((case-fold-search t)) (search-forward "#+results"))
+           (buffer-substring-no-properties (line-beginning-position 2)
+                                           (point-max)))))
+  (should
+   (equal ": (hline . 3)\n"
+         (org-test-with-temp-text "#+begin_src scheme\n'(null . 3)\n#+end_src"
+           (org-babel-execute-maybe)
+           (let ((case-fold-search t)) (search-forward "#+results"))
+           (buffer-substring-no-properties (line-beginning-position 2)
+                                           (point-max)))))
+  (should
+   (equal "| 1 | nil | 3 |\n"
+          (let ((org-babel-scheme-null-to nil))
+           (org-test-with-temp-text "#+begin_src scheme\n'(1 null 
3)\n#+end_src"
+             (org-babel-execute-maybe)
+             (let ((case-fold-search t)) (search-forward "#+results"))
+             (buffer-substring-no-properties (line-beginning-position 2)
+                                             (point-max)))))))
+
 (ert-deftest test-ob-scheme/prologue ()
   "Test :prologue parameter."
   (should

Reply via email to