From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms1.migadu.com with LMTPS id cBk6GcXlJ2aOtAAA62LTzQ:P1 (envelope-from ) for ; Tue, 23 Apr 2024 18:45:57 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:58f0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id cBk6GcXlJ2aOtAAA62LTzQ (envelope-from ) for ; Tue, 23 Apr 2024 18:45:57 +0200 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=adamkovic.org header.s=fm1 header.b=AXgXYi2L; dkim=pass header.d=messagingengine.com header.s=fm3 header.b=jd+dS8Bu; dmarc=pass (policy=none) header.from=adamkovic.org; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1713890757; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=nd04n2pdwGzAWlzugIYgI93CXzKxcZGa5MqqDhaiV4w=; b=MFFM3nvyty1QFWSEdHAwHssuvxDe+gm8GVS63DrGZs5Y8eVJfEOoJETzFdCGpfwV6HUKDb ytwSoVHlj9oTmCmpXJ2ewwC19oD4F0Mo50GFyfm4fl2qwpjMWs7ISI0Zu+vqeJ4d47jB2Q t5VZZ32nPuFbn02DVUej6xBBrFTZfR/wa9gk0AFXrx4/8PbxaRAIm76WZGDE+FuMSgexMc FtHcBBayY7RzhcdGxlyNys9rUGYsMRkyI5T0FGCs73JEovyM14GTUw1r5y+qGaMpty7JBB G7/d/yZHOuTkwhifR9SjcHvyuvdwUkfC2HmMKvKal7CFXKNoCa8BZDougEj5CQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=adamkovic.org header.s=fm1 header.b=AXgXYi2L; dkim=pass header.d=messagingengine.com header.s=fm3 header.b=jd+dS8Bu; dmarc=pass (policy=none) header.from=adamkovic.org; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1713890757; a=rsa-sha256; cv=none; b=f44jxrcZ2qGdELK8AJnZz0Cu/lyQSk2CgE5iNLyBkUQ4aefjeK/87Z4SUHOlhnQUPjrx38 6FLFC5XHiVqtAFYSYtTrgBnqwX+Wl+bvyox6bHLAZvdJC+hzf4Ur/S7JTEknac2KZkADfJ YaTZ4+kg1jeZ5ukT3l74Do6Zzz5Zma4ome/ncIse/KShwK0v7nYaI0bHysDDJMpRIyRJe5 DfhjXcorgh5/yX4l9SjpgtmAtoRb/G0X4+I5J1QLBrAbmPSqi7J99tJs8SVAg11XB4aW0K q7WuXdvOUp3WG+SyHIzjxgzG46BNl9FH2RAqNUr7+a1NfA5SIHQVi74tu8RWBQ== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 2A95DD3DE for ; Tue, 23 Apr 2024 18:45:56 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rzJGd-0000Nz-Qz; Tue, 23 Apr 2024 12:45:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rzJGb-0000Ld-EX for emacs-orgmode@gnu.org; Tue, 23 Apr 2024 12:45:13 -0400 Received: from fhigh1-smtp.messagingengine.com ([103.168.172.152]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rzJGY-0003Ew-1S for emacs-orgmode@gnu.org; Tue, 23 Apr 2024 12:45:13 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.nyi.internal (Postfix) with ESMTP id C9D6311400AB; Tue, 23 Apr 2024 12:45:06 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 23 Apr 2024 12:45:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adamkovic.org; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to; s=fm1; t=1713890706; x=1713977106; bh=nd04n2pdwG zAWlzugIYgI93CXzKxcZGa5MqqDhaiV4w=; b=AXgXYi2LCzf6Tto+yHWjFpD2Dh pv78OhTVVDZCsii3MecZhEHAA776+5fjbIMLmzPubSBI9vOq0FZN+Z4VSGNmLMxL +SNC/nYlshpAI2ij05++aO6nPY8nhY03vIAQ88/u/261EbeIjburHZsHqBOEZYgy 6FMRf37PzOK6+OLj4fh5p4K29w8kjsnFSzYQfy0ayBuyDwC3JYEdnRPgN1ygIeyX NM/JWaFYrEs2OKtEEA6kdhP9WuG3F1RwamgxAHezdUw/Hy6MyKhzdUPygeMzUg3O Vasf6PwPs/fiw6yi7kvY4G9iJmsloc4LTcu/dimdnZqybf4XWO10nrdn6llA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1713890706; x=1713977106; bh=nd04n2pdwGzAWlzugIYgI93CXzKx cZGa5MqqDhaiV4w=; b=jd+dS8BuXZbAptF/Z9Tlp5DrjJt84A2Hmy/ZeEXV6HkA NZ0+edCHdCfwoQjym1hhOYiN+vvjHI8mM4hPBSCBW/Ll321gPswt+1uAN1OSVOcR +eRS893JLC4bPwxQkRBcCxWgQSTreJqdJWGWnjaBcZDWzDxxgOaVyTmn/FfxjK3J L+H/jgVuI+5WWHeEcZV5splYtAdeEMRToXeks9/vnReLDlPBaZwWdgTWNsg5Au0d NdaEDM3WeuLxTKAiy47bvNEVetGY3OHRVIO6eeFFLaSC9i5rQjVYfxiezs/hKv3K XwJ8DxMVfQulgDZBut1gchRufMyDR/I+5L8i+dXGKw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeluddguddthecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvvefufffkofgggfestdekre dtredtjeenucfhrhhomheptfhuugholhhfucetuggrmhhkohhvihgtuceorhhuugholhhf segruggrmhhkohhvihgtrdhorhhgqeenucggtffrrghtthgvrhhnpeduteffkefhffevfe eulefgjeejtefgleetveeljeekleffteeuveevveeuveeugfenucffohhmrghinheprhgv thhurhhnshdrohhrghdptghhrghrrggtthgvrhhsrdhorhhgpdhorhhgqdgsrggsvghlqd hluhgrqdifrhgrphhpvghrqdhmvghthhhougdrohhrghdpohhrghdqsggrsggvlhdqlhhu rgdqmhhulhhtihhplhgvqdhvrghluhgvshdqshgvphgrrhgrthhorhdrohhrghenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehruhguohhlfhes rggurghmkhhovhhitgdrohhrgh X-ME-Proxy: Feedback-ID: i88214938:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 23 Apr 2024 12:45:05 -0400 (EDT) From: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= To: emacs-orgmode@gnu.org Cc: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= Subject: [PATCH] ob-lua: Support all types and multiple values in results Date: Tue, 23 Apr 2024 18:44:58 +0200 Message-ID: <20240423164458.33702-1-rudolf@adamkovic.org> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=103.168.172.152; envelope-from=rudolf@adamkovic.org; helo=fhigh1-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -9.06 X-Migadu-Scanner: mx11.migadu.com X-Spam-Score: -9.06 X-Migadu-Queue-Id: 2A95DD3DE X-TUID: vFoJtWk21WU+ * etc/ORG-NEWS (New and changed options): Describe the new option 'org-babel-lua-multiple-values-separator'. (New features): Describe the main change, as per the title of this commit message. * lisp/ob-lua.el (org-babel-lua-multiple-values-separator): Enable the user to customize the string that separates the individual values in multi-valued returns. (org-babel-lua-wrapper-method): Support all Lua types and multi-valued returns. Further, do not pretty-print tables with one or more extraneous newline characters. (org-babel-lua-pp-wrapper-method): Remove in favor of the new, more general 'org-babel-lua-wrapper-method'. (org-babel-lua-evaluate-external-process): Adapt for the new 'org-babel-lua-wrapper-method'. * testing/lisp/test-ob-lua.el (test-ob-lua/colnames-yes-header-argument-pp): (test-ob-lua/colnames-nil-header-argument): (test-ob-lua/colnames-no-header-argument): Stop expecting extraneous newlines, now that the pretty printer does not output them. (test-ob-lua/types): Test nil, boolean, number, string, and table results. (test-ob-lua/multiple-values): Test multi-valued results. --- etc/ORG-NEWS | 17 +++++++++ lisp/ob-lua.el | 73 ++++++++++++++++++++++--------------- testing/lisp/test-ob-lua.el | 48 ++++++++++++++++++++++-- 3 files changed, 105 insertions(+), 33 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index fc2ff9e00..696f46e53 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -637,6 +637,11 @@ link when storing any type of external link type in an Org file, not just =id:= links. ** New and changed options +*** ~org-babel-lua-multiple-values-separator~ + +The string that separates the values of multi-valued results returned +from Lua code blocks. + *** =.avif= images are now recognized in ~org-html-inline-image-rules~ In =ox-html=, =.avif= image links are now inlined by default. @@ -1012,6 +1017,18 @@ The option can be customized either by 2. by setting the file local keyword =LATEX_FOOTNOTE_COMMAND= ** New features +*** =ob-lua=: Support all types and multiple values in results + +Lua code blocks can now return values of any type and can also return +multiple values. Previously, values of certain types were incorrectly +converted to the empty string =""=, which broke HTML export for inline +code blocks, and multiple values were incorrectly concatenated, where +~return 1, 2, 3~ was evaluated as =123=. + +Multiple values are comma-separated by default, so that they work well +with inline code blocks. To change the string used as the separator, +customize ~org-babel-lua-multiple-values-separator~. + *** ~org-paste-subtree~ now handles =C-u= and =C-u C-u= prefix arguments specially With =C-u= prefix argument, force inserting a sibling heading below. diff --git a/lisp/ob-lua.el b/lisp/ob-lua.el index b241fccdc..92a1b3344 100644 --- a/lisp/ob-lua.el +++ b/lisp/ob-lua.el @@ -81,6 +81,13 @@ This will typically be `lua-mode'." :package-version '(Org . "8.3") :type 'symbol) +(defcustom org-babel-lua-multiple-values-separator ", " + "Separate multiple values with this string." + :group 'org-babel + :version "30.0" + :package-version '(Org . "9.7") + :type 'string) + (defun org-babel-execute:lua (body params) "Execute Lua BODY according to PARAMS. This function is called by `org-babel-execute-src-block'." @@ -251,41 +258,47 @@ function main() %s end -fd=io.open(\"%s\", \"w\") -fd:write( main() ) -fd:close()") -(defvar org-babel-lua-pp-wrapper-method - " --- table to string -function t2s(t, indent) +function dump(it, indent) if indent == nil then - indent = \"\" + indent = '' end - if type(t) == \"table\" then - ts = \"\" - for k,v in pairs(t) do - if type(v) == \"table\" then - ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \\n\" .. - t2s(v, indent .. \" \") - else - ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \" .. - t2s(v, indent .. \" \") .. \"\\n\" + 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 + result = result + .. indent + .. dump(key) + .. ' = ' + .. dump(value, indent .. ' ') + count = count - 1 + if count ~= 0 then + result = result .. '\\n' end end - return ts + return result else - return tostring(t) + return tostring(it) end end - -function main() -%s +function combine(...) + local result = {} + for index = 1, select('#', ...) do + result[index] = dump(select(index, ...)) + end + return table.concat(result, '%s') end -fd=io.open(\"%s\", \"w\") -fd:write(t2s(main())) -fd:close()") +output = io.open('%s', 'w') +output:write(combine(main())) +output:close()") (defun org-babel-lua-evaluate (session body &optional result-type result-params preamble) @@ -319,15 +332,17 @@ PREAMBLE string is appended to BODY." (concat preamble (and preamble "\n") (format - (if (member "pp" result-params) - org-babel-lua-pp-wrapper-method - org-babel-lua-wrapper-method) + org-babel-lua-wrapper-method (mapconcat (lambda (line) (format "\t%s" line)) (split-string (org-remove-indentation (org-trim body)) - "[\r\n]") "\n") + "[\r\n]") + "\n") + (if (member "pp" result-params) + "true" "false") + org-babel-lua-multiple-values-separator (org-babel-process-file-name tmp-file 'noquote)))) (org-babel-eval-read-file tmp-file)))))) (org-babel-result-cond result-params diff --git a/testing/lisp/test-ob-lua.el b/testing/lisp/test-ob-lua.el index f30e65bb3..0a60c68ca 100644 --- a/testing/lisp/test-ob-lua.el +++ b/testing/lisp/test-ob-lua.el @@ -77,9 +77,9 @@ return x[1] (ert-deftest test-ob-lua/colnames-yes-header-argument-pp () - "Test table passing with `colnames' header and pp option." + "Test table passing with `colnames' header and `pp' option." (should - (equal "a = 12\nb = 13\n" + (equal "a = 12\nb = 13" (org-test-with-temp-text "#+name: eg | col | val | @@ -99,7 +99,7 @@ return x (ert-deftest test-ob-lua/colnames-nil-header-argument () "Test table with `colnames' set to `nil'." (should - (equal "1 = a\n2 = b\n" + (equal "1 = a\n2 = b" (org-test-with-temp-text "#+name: eg | col | @@ -119,7 +119,7 @@ return x (ert-deftest test-ob-lua/colnames-no-header-argument () "Test table passing without `colnames'." (should - (equal "1 = col\n2 = a\n3 = b\n" + (equal "1 = col\n2 = a\n3 = b" (org-test-with-temp-text "#+name: eg | col | @@ -136,6 +136,46 @@ return x (org-babel-next-src-block) (org-babel-execute-src-block))))) +(ert-deftest test-ob-lua/types () + "Test returning different types." + (should + (equal "nil" + (org-test-with-temp-text "src_lua{return nil}" + (org-babel-execute-src-block)))) + (should + (equal "true" + (org-test-with-temp-text "src_lua{return true}" + (org-babel-execute-src-block)))) + (should + (equal "false" + (org-test-with-temp-text "src_lua{return false}" + (org-babel-execute-src-block)))) + (should + (equal 1 + (org-test-with-temp-text "src_lua{return 1}" + (org-babel-execute-src-block)))) + (should + (equal "hello world" + (org-test-with-temp-text "src_lua{return 'hello world'}" + (org-babel-execute-src-block)))) + (should + (equal 0 + (string-match "table: 0x[0-9A-F]+" + (org-test-with-temp-text "src_lua{return {}}" + (org-babel-execute-src-block)))))) + +(ert-deftest test-ob-lua/multiple-values () + "Test returning multiple values." + (should + (equal "1, 2, 3" + (org-test-with-temp-text "src_lua{return 1, 2, 3}" + (org-babel-execute-src-block)))) + (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)))))) + (provide 'test-ob-lua) ;;; test-ob-lua.el ends here -- 2.44.0