From: "Rudolf Adamkovič" <rudolf@adamkovic.org>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: [PATCH] ob-lua: Support all types and multiple values in results
Date: Thu, 02 May 2024 18:02:34 +0200 [thread overview]
Message-ID: <m27cgcnqw5.fsf@adamkovic.org> (raw)
In-Reply-To: <m2frv0nr3m.fsf@adamkovic.org>
[-- Attachment #1: Type: text/plain, Size: 157 bytes --]
Rudolf Adamkovič <rudolf@adamkovic.org> writes:
> I am attaching a V2 of the patch that:
Oops, I forgot to attach the patch. Here it is. :)
Rudy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-lua-Quote-list-like-strings-sort-tables-and-parse.patch --]
[-- Type: text/x-patch, Size: 14832 bytes --]
From b4d61bf511df74094fbcdf13c8c0c2b57b1e4a22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= <rudolf@adamkovic.org>
Date: Mon, 29 Apr 2024 21:42:04 +0200
Subject: [PATCH] ob-lua: Quote list-like strings, sort tables, and parse bare
returns
* lisp/ob-lua.el (org-babel-lua-wrapper-method): 3 changes: (1) Quote
strings with list-like content to prevent Org Babel from incorrectly
guessing their type, (2) sort pretty-printed non-sequential tables to
make them reproducible, and (3) handle implicit nils produced by
'return' used with no arguments.
* testing/lisp/test-ob-lua.el (test-ob-lua/result/nil):
(test-ob-lua/result/nil/explicit):
(test-ob-lua/result/boolean):
(test-ob-lua/results/number/integer):
(test-ob-lua/results/number/integer/negative):
(test-ob-lua/results/number/integer/multiple):
(test-ob-lua/results/number/real):
(test-ob-lua/results/number/real/multiple):
(test-ob-lua/results/number/infinity):
(test-ob-lua/results/string/single-quotes):
(test-ob-lua/results/string/double-quotes):
(test-ob-lua/results/string/multiple):
(test-ob-lua/results/string/list-like):
(test-ob-lua/results/string/list-like/brackets):
(test-ob-lua/results/string/list-like/curlies):
(test-ob-lua/results/string/list-like/standard-output):
(test-ob-lua/result/table):
(test-ob-lua/result/table/pretty-print):
(test-ob-lua/result/table/pretty-print/sorted):
(test-ob-lua/results/value-separator): New tests.
---
lisp/ob-lua.el | 49 +++++--
testing/lisp/test-ob-lua.el | 278 ++++++++++++++++++++++++++++++++----
2 files changed, 284 insertions(+), 43 deletions(-)
diff --git a/lisp/ob-lua.el b/lisp/ob-lua.el
index 041abfabc..19950f2cb 100644
--- a/lisp/ob-lua.el
+++ b/lisp/ob-lua.el
@@ -254,33 +254,40 @@ then create. Return the initialized session."
(defvar org-babel-lua-wrapper-method
"
function main()
-%s
+ %s
end
function dump(it, indent)
if indent == nil then
indent = ''
end
+
if type(it) == 'table' and %s then
- local count = 0
- for _ in pairs(it) do
- count = count + 1
- end
local result = ''
+
if #indent ~= 0 then
result = result .. '\\n'
end
- for key, value in pairs(it) do
+
+ local keys = {}
+ for key in pairs(it) do
+ table.insert(keys, key)
+ end
+
+ table.sort(keys)
+
+ for index, key in pairs(keys) do
+ local value = it[key]
result = result
.. indent
.. dump(key)
.. ' = '
.. dump(value, indent .. ' ')
- count = count - 1
- if count ~= 0 then
+ if index ~= #keys then
result = result .. '\\n'
end
end
+
return result
else
return tostring(it)
@@ -288,11 +295,27 @@ function dump(it, indent)
end
function combine(...)
- local result = {}
- for index = 1, select('#', ...) do
- result[index] = dump(select(index, ...))
- end
- return table.concat(result, '%s')
+ local quotes = '\"'
+ local result = {}
+
+ for index = 1, select('#', ...) do
+ result[index] = dump(select(index, ...))
+ end
+
+ if #result == 0 then
+ return dump(nil)
+ end
+
+ if #result == 1 then
+ local value = result[1]
+ if string.find(value, '[%%(%%[{]') == 1 then
+ return quotes .. value .. quotes
+ else
+ return value
+ end
+ end
+
+ return quotes .. table.concat(result, '%s') .. quotes
end
output = io.open('%s', 'w')
diff --git a/testing/lisp/test-ob-lua.el b/testing/lisp/test-ob-lua.el
index 0a60c68ca..a0a3b178c 100644
--- a/testing/lisp/test-ob-lua.el
+++ b/testing/lisp/test-ob-lua.el
@@ -136,45 +136,263 @@ return x
(org-babel-next-src-block)
(org-babel-execute-src-block)))))
-(ert-deftest test-ob-lua/types ()
- "Test returning different types."
+(ert-deftest test-ob-lua/result/nil ()
+ "Test returning nothing."
(should
- (equal "nil"
- (org-test-with-temp-text "src_lua{return nil}"
- (org-babel-execute-src-block))))
+ (equal
+ "src_lua{return} {{{results(=nil=)}}}"
+ (org-test-with-temp-text "src_lua{return}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/result/nil/explicit ()
+ "Test returning nothing explicitly."
+ (should
+ (equal
+ "src_lua{return nil} {{{results(=nil=)}}}"
+ (org-test-with-temp-text "src_lua{return nil}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/result/boolean ()
+ "Test returning the boolean values true and false."
+ (should
+ (equal
+ "src_lua{return true} {{{results(=true=)}}}"
+ (org-test-with-temp-text "src_lua{return true}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max)))))
+ (should
+ (equal
+ "src_lua{return false} {{{results(=false=)}}}"
+ (org-test-with-temp-text "src_lua{return false}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/number/integer ()
+ "Test returning integers."
+ (should
+ (equal
+ "src_lua{return 1} {{{results(=1=)}}}"
+ (org-test-with-temp-text "src_lua{return 1}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/number/integer/negative ()
+ "Test returning negative integers."
(should
- (equal "true"
- (org-test-with-temp-text "src_lua{return true}"
- (org-babel-execute-src-block))))
+ (equal
+ "src_lua{return -1} {{{results(=-1=)}}}"
+ (org-test-with-temp-text "src_lua{return -1}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/number/integer/multiple ()
+ "Test returning multiple integers at once."
+ (should
+ (equal
+ "src_lua{return 1, 2, 3} {{{results(=1, 2, 3=)}}}"
+ (org-test-with-temp-text "src_lua{return 1, 2, 3}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/number/real ()
+ "Test returning real numbers."
+ (should
+ (equal
+ "src_lua{return 1.5} {{{results(=1.5=)}}}"
+ (org-test-with-temp-text "src_lua{return 1.5}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/number/real/multiple ()
+ "Test returning multiple real numbers at once."
(should
- (equal "false"
- (org-test-with-temp-text "src_lua{return false}"
- (org-babel-execute-src-block))))
+ (equal
+ "src_lua{return 1.5, 2.5, 3.5} {{{results(=1.5, 2.5, 3.5=)}}}"
+ (org-test-with-temp-text "src_lua{return 1.5, 2.5, 3.5}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/number/infinity ()
+ "Test returning the infinity."
(should
- (equal 1
- (org-test-with-temp-text "src_lua{return 1}"
- (org-babel-execute-src-block))))
+ (equal
+ "src_lua{return 1 / 0} {{{results(=inf=)}}}"
+ (org-test-with-temp-text "src_lua{return 1 / 0}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/string/single-quotes ()
+ "Test returning strings in single quotes."
(should
- (equal "hello world"
- (org-test-with-temp-text "src_lua{return 'hello world'}"
- (org-babel-execute-src-block))))
+ (equal
+ "src_lua{return 'hello world'} {{{results(=hello world=)}}}"
+ (org-test-with-temp-text "src_lua{return 'hello world'}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/string/double-quotes ()
+ "Test returning strings in double quotes."
(should
- (equal 0
- (string-match "table: 0x[0-9A-F]+"
- (org-test-with-temp-text "src_lua{return {}}"
- (org-babel-execute-src-block))))))
+ (equal
+ "src_lua{return \"hello world\"} {{{results(=hello world=)}}}"
+ (org-test-with-temp-text "src_lua{return \"hello world\"}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
-(ert-deftest test-ob-lua/multiple-values ()
- "Test returning multiple values."
+(ert-deftest test-ob-lua/results/string/multiple ()
+ "Test returning multiple strings at once."
(should
- (equal "1, 2, 3"
- (org-test-with-temp-text "src_lua{return 1, 2, 3}"
- (org-babel-execute-src-block))))
+ (equal
+ "src_lua{return 'a', 'b'} {{{results(=a, b=)}}}"
+ (org-test-with-temp-text "src_lua{return 'a', 'b'}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/string/list-like ()
+ "Test returning strings that look like \"(...)\" lists."
+ (should
+ (equal
+ (concat "src_lua{return string.match('A (B) C', '%b()')}"
+ " {{{results(=(B)=)}}}")
+ (org-test-with-temp-text
+ "src_lua{return string.match('A (B) C', '%b()')}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/string/list-like/brackets ()
+ "Test returning strings that look like \"[...]\" lists."
+ (should
+ (equal
+ (concat "src_lua{return string.match('A [B] C', '%b[]')}"
+ " {{{results(=[B]=)}}}")
+ (org-test-with-temp-text
+ "src_lua{return string.match('A [B] C', '%b[]')}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/string/list-like/curlies ()
+ "Test returning strings that look like \"{...}\" lists."
+ (should
+ (equal
+ (concat "src_lua{return string.match('A {B} C', '%b{}')}"
+ " {{{results(={B}=)}}}")
+ (org-test-with-temp-text
+ "src_lua{return string.match('A {B} C', '%b{}')}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/string/list-like/standard-output ()
+ "Test returning strings that look like lists to standard output."
+ (should
+ (equal
+ (concat "src_lua[:results output]"
+ "{print(string.match('A (B) C', '%b()'))}"
+ " {{{results(=(B)=)}}}")
+ (org-test-with-temp-text
+ (concat "src_lua[:results output]"
+ "{print(string.match('A (B) C', '%b()'))}")
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/result/table ()
+ "Test returning table references."
+ (should
+ (equal
+ 0
+ (string-match
+ "src_lua{return {}} {{{results(=table: 0x[0-9A-F]+=)}}}"
+ (org-test-with-temp-text "src_lua{return {}}"
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max)))))))
+
+(ert-deftest test-ob-lua/result/table/pretty-print ()
+ "Test returning and pretty-printing sequential tables."
+ (should
+ (equal (string-join
+ '("#+BEGIN_SRC lua :results pp"
+ "return {10, {20, 30, {40, 50}, 60}, 70}"
+ "#+END_SRC"
+ ""
+ "#+RESULTS:"
+ ": 1 = 10"
+ ": 2 = " ; FIXME Trailing space.
+ ": 1 = 20"
+ ": 2 = 30"
+ ": 3 = " ; FIXME Trailing space.
+ ": 1 = 40"
+ ": 2 = 50"
+ ": 4 = 60"
+ ": 3 = 70"
+ "")
+ "\n")
+ (org-test-with-temp-text
+ (string-join
+ '("#+BEGIN_SRC lua :results pp"
+ "return {10, {20, 30, {40, 50}, 60}, 70}<point>"
+ "#+END_SRC")
+ "\n")
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/result/table/pretty-print/sorted ()
+ "Test returning and pretty-printing non-sequential tables."
+ (should
+ (equal (string-join
+ '("#+BEGIN_SRC lua :results pp"
+ "return {b = 20, c = 30, a = 10}"
+ "#+END_SRC"
+ ""
+ "#+RESULTS:"
+ ;; NOTE The keys are sorted alphabetically.
+ ": a = 10"
+ ": b = 20"
+ ": c = 30"
+ "")
+ "\n")
+ (org-test-with-temp-text
+ (string-join
+ '("#+BEGIN_SRC lua :results pp"
+ "return {b = 20, c = 30, a = 10}"
+ "#+END_SRC")
+ "\n")
+ (org-babel-execute-src-block)
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
+
+(ert-deftest test-ob-lua/results/value-separator ()
+ "Test customizing the separator of multiple values."
+ ;; TODO Once Org Babel supports returning lists from inline blocks,
+ ;; instead of trapping with the user error: "Inline error: list
+ ;; result cannot be used", use those for multiple values.
(should
- (equal "1|2|3"
- (let ((org-babel-lua-multiple-values-separator "|"))
- (org-test-with-temp-text "src_lua{return 1, 2, 3}"
- (org-babel-execute-src-block))))))
+ (equal
+ "src_lua{return 1, 2, 3} {{{results(=1\t2\t3=)}}}"
+ (org-test-with-temp-text "src_lua{return 1, 2, 3}"
+ (let ((org-babel-lua-multiple-values-separator "\t"))
+ (org-babel-execute-src-block))
+ (buffer-substring-no-properties (point-min)
+ (point-max))))))
(provide 'test-ob-lua)
--
2.39.3 (Apple Git-146)
[-- Attachment #3: Type: text/plain, Size: 259 bytes --]
--
“Those who cannot remember the past are condemned to repeat it.”
--- George Santayana, Life of Reason: Reason in Common Sense, 1905
Rudolf Adamkovič <rudolf@adamkovic.org> [he/him]
Studenohorská 25, 84103 Bratislava, Slovakia, European Union
next prev parent reply other threads:[~2024-05-02 16:03 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-23 16:44 [PATCH] ob-lua: Support all types and multiple values in results Rudolf Adamkovič
2024-04-23 17:05 ` Rudolf Adamkovič
2024-04-24 14:20 ` Ihor Radchenko
2024-04-24 13:05 ` Ihor Radchenko
2024-04-24 15:01 ` Rudolf Adamkovič
2024-04-26 13:40 ` Ihor Radchenko
2024-04-27 10:57 ` Max Nikulin
2024-04-27 16:33 ` Rudolf Adamkovič
2024-04-28 12:36 ` Ihor Radchenko
2024-04-29 15:57 ` Rudolf Adamkovič
2024-04-29 20:26 ` Rudolf Adamkovič
2024-04-30 10:24 ` Ihor Radchenko
2024-05-02 15:57 ` Rudolf Adamkovič
2024-05-02 15:58 ` Rudolf Adamkovič
2024-05-02 16:02 ` Rudolf Adamkovič [this message]
2024-05-01 11:05 ` Max Nikulin
2024-05-02 16:00 ` Rudolf Adamkovič
2024-05-02 16:41 ` Ihor Radchenko
2024-06-09 20:54 ` Rudolf Adamkovič
2024-06-14 17:41 ` Rudolf Adamkovič
2024-06-14 17:48 ` Rudolf Adamkovič
2024-06-14 17:58 ` Ihor Radchenko
2024-06-14 20:13 ` Rudolf Adamkovič
2024-06-14 20:18 ` Ihor Radchenko
2024-06-14 20:37 ` Rudolf Adamkovič
2024-06-14 21:20 ` Rudolf Adamkovič
2024-06-15 13:08 ` Ihor Radchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m27cgcnqw5.fsf@adamkovic.org \
--to=rudolf@adamkovic.org \
--cc=emacs-orgmode@gnu.org \
--cc=yantar92@posteo.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).