Rudolf Adamkovič <rud...@adamkovic.org> writes:

> Ihor Radchenko <yanta...@posteo.net> writes:
>
>> May you create a test for this with expected failure?
>
> Sure!  Here is one:
>
>   (should
>    (equal "{B}"
>           (org-test-with-temp-text
>               "src_lua{return string.match('A {B} C', '%b{}')}"
>             (org-babel-execute-src-block))))
>
> The return value from 'string.match' is the substring "{B}", which Babel
> misjudges to be a list and signals the error:
>
>   (user-error "Inline error: list result cannot be used")

Maybe something like the attached.
(I am a casual lua user, so better double check)

>From bfe0fa3d78cc13b84dec33c230755c9f4693a685 Mon Sep 17 00:00:00 2001
Message-ID: <bfe0fa3d78cc13b84dec33c230755c9f4693a685.1714138810.git.yanta...@posteo.net>
From: Ihor Radchenko <yanta...@posteo.net>
Date: Fri, 26 Apr 2024 16:39:07 +0300
Subject: [PATCH] ob-lua: Fix edge case when output is {string}
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lisp/ob-lua.el (org-babel-lua-wrapper-method): Make sure that string
result is enclosed in "...", so that Org mode does not try to guess
the output type heuristically.
* testing/lisp/test-ob-lua.el (test-ob-lua/types): New test.

Co-authored-by: Rudolf Adamkovič <rud...@adamkovic.org>
---
 lisp/ob-lua.el              | 2 ++
 testing/lisp/test-ob-lua.el | 6 ++++++
 2 files changed, 8 insertions(+)

diff --git a/lisp/ob-lua.el b/lisp/ob-lua.el
index 041abfabc..afe88065d 100644
--- a/lisp/ob-lua.el
+++ b/lisp/ob-lua.el
@@ -282,6 +282,8 @@ (defvar org-babel-lua-wrapper-method
          end
       end
       return result
+   elseif type(it) == 'string' then
+      return '\"' .. it .. '\"'
    else
       return tostring(it)
    end
diff --git a/testing/lisp/test-ob-lua.el b/testing/lisp/test-ob-lua.el
index 0a60c68ca..398ab761d 100644
--- a/testing/lisp/test-ob-lua.el
+++ b/testing/lisp/test-ob-lua.el
@@ -158,6 +158,12 @@ (ert-deftest test-ob-lua/types ()
    (equal "hello world"
           (org-test-with-temp-text "src_lua{return 'hello world'}"
             (org-babel-execute-src-block))))
+  ;; Edge case: string matching list syntax.
+  (should
+   (equal "{B}"
+	  (org-test-with-temp-text
+	      "src_lua{return string.match('A {B} C', '%b{}')}"
+	    (org-babel-execute-src-block))))
   (should
    (equal 0
           (string-match "table: 0x[0-9A-F]+"
-- 
2.44.0

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to