update packages
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
;;; ob-lua.el --- Org Babel functions for Lua evaluation -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 2014, 2016-2023 Free Software Foundation, Inc.
|
||||
;; Copyright (C) 2014, 2016-2025 Free Software Foundation, Inc.
|
||||
|
||||
;; Authors: Dieter Schoen
|
||||
;; Keywords: literate programming, reproducible research
|
||||
@@ -23,12 +23,14 @@
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;; Org-Babel support for evaluating lua source code.
|
||||
;; Org-Babel support for evaluating Lua source code.
|
||||
|
||||
;; Requirements:
|
||||
;; for session support, lua-mode is needed.
|
||||
;;
|
||||
;; lua-mode is not part of GNU Emacs/orgmode, but can be obtained
|
||||
;; from marmalade or melpa.
|
||||
;; from NonGNU ELPA (see `M-x list-packages').
|
||||
;;
|
||||
;; The source repository is here:
|
||||
;; https://github.com/immerrr/lua-mode
|
||||
|
||||
@@ -68,21 +70,27 @@ This will typically be `lua-mode'."
|
||||
:type 'symbol)
|
||||
|
||||
(defcustom org-babel-lua-hline-to "None"
|
||||
"Replace hlines in incoming tables with this when translating to lua."
|
||||
"Replace hlines in incoming tables with this when translating to Lua."
|
||||
:group 'org-babel
|
||||
:version "26.1"
|
||||
:package-version '(Org . "8.3")
|
||||
:type 'string)
|
||||
|
||||
(defcustom org-babel-lua-None-to 'hline
|
||||
"Replace `None' in lua tables with this before returning."
|
||||
"Replace `None' in Lua tables with this before returning."
|
||||
:group 'org-babel
|
||||
:version "26.1"
|
||||
:package-version '(Org . "8.3")
|
||||
:type 'symbol)
|
||||
|
||||
(defcustom org-babel-lua-multiple-values-separator ", "
|
||||
"Separate multiple values with this string."
|
||||
:group 'org-babel
|
||||
:package-version '(Org . "9.7")
|
||||
:type 'string)
|
||||
|
||||
(defun org-babel-execute:lua (body params)
|
||||
"Execute a block of Lua code with Babel.
|
||||
"Execute Lua BODY according to PARAMS.
|
||||
This function is called by `org-babel-execute-src-block'."
|
||||
(let* ((session (org-babel-lua-initiate-session
|
||||
(cdr (assq :session params))))
|
||||
@@ -129,7 +137,8 @@ VARS contains resolved variable references."
|
||||
;; helper functions
|
||||
|
||||
(defun org-babel-variable-assignments:lua (params)
|
||||
"Return a list of Lua statements assigning the block's variables."
|
||||
"Return a list of Lua statements assigning the block's variables.
|
||||
The variable definitions are defining in PARAMS."
|
||||
(mapcar
|
||||
(lambda (pair)
|
||||
(format "%s=%s"
|
||||
@@ -138,8 +147,8 @@ VARS contains resolved variable references."
|
||||
(org-babel--get-vars params)))
|
||||
|
||||
(defun org-babel-lua-var-to-lua (var)
|
||||
"Convert an elisp value to a lua variable.
|
||||
Convert an elisp value, VAR, into a string of lua source code
|
||||
"Convert an Emacs Lisp value to a Lua variable.
|
||||
Convert an Emacs Lisp value, VAR, into a string of Lua source code
|
||||
specifying a variable of the same value."
|
||||
(if (listp var)
|
||||
(if (and (= 1 (length var)) (not (listp (car var))))
|
||||
@@ -176,13 +185,20 @@ Emacs-lisp table, otherwise return the results as a string."
|
||||
(cdr (assoc session org-babel-lua-buffers)))
|
||||
|
||||
(defun org-babel-lua-with-earmuffs (session)
|
||||
"Return buffer name for SESSION, as *SESSION*."
|
||||
(let ((name (if (stringp session) session (format "%s" session))))
|
||||
(if (and (string= "*" (substring name 0 1))
|
||||
(string= "*" (substring name (- (length name) 1))))
|
||||
name
|
||||
(format "*%s*" name))))
|
||||
|
||||
(defun org-babel-session-buffer:lua (session &optional _)
|
||||
"Return session buffer name for SESSION."
|
||||
(or (org-babel-lua-session-buffer session)
|
||||
(org-babel-lua-with-earmuffs session)))
|
||||
|
||||
(defun org-babel-lua-without-earmuffs (session)
|
||||
"Remove stars around *SESSION*, leaving SESSION."
|
||||
(let ((name (if (stringp session) session (format "%s" session))))
|
||||
(if (and (string= "*" (substring name 0 1))
|
||||
(string= "*" (substring name (- (length name) 1))))
|
||||
@@ -193,7 +209,7 @@ Emacs-lisp table, otherwise return the results as a string."
|
||||
(defvar lua-which-bufname)
|
||||
(defvar lua-shell-buffer-name)
|
||||
(defun org-babel-lua-initiate-session-by-key (&optional session)
|
||||
"Initiate a lua session.
|
||||
"Initiate a Lua session.
|
||||
If there is not a current inferior-process-buffer in SESSION
|
||||
then create. Return the initialized session."
|
||||
;; (require org-babel-lua-mode)
|
||||
@@ -243,45 +259,55 @@ 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)
|
||||
"Evaluate BODY as Lua code."
|
||||
"Evaluate BODY in SESSION as Lua code.
|
||||
RESULT-TYPE and RESULT-PARAMS are passed to
|
||||
`org-babel-lua-evaluate-session' or
|
||||
`org-babel-lua-evaluate-external-process'.
|
||||
PREAMBLE is passed to `org-babel-lua-evaluate-external-process'."
|
||||
(if session
|
||||
(org-babel-lua-evaluate-session
|
||||
session body result-type result-params)
|
||||
@@ -290,10 +316,12 @@ fd:close()")
|
||||
|
||||
(defun org-babel-lua-evaluate-external-process
|
||||
(body &optional result-type result-params preamble)
|
||||
"Evaluate BODY in external lua process.
|
||||
"Evaluate BODY in external Lua process.
|
||||
If RESULT-TYPE equals `output' then return standard output as a
|
||||
string. If RESULT-TYPE equals `value' then return the value of the
|
||||
last statement in BODY, as elisp."
|
||||
last statement in BODY, as Emacs Lisp.
|
||||
RESULT-PARAMS list all the :result header arg parameters.
|
||||
PREAMBLE string is appended to BODY."
|
||||
(let ((raw
|
||||
(pcase result-type
|
||||
(`output (org-babel-eval org-babel-lua-command
|
||||
@@ -305,15 +333,17 @@ last statement in BODY, as elisp."
|
||||
(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
|
||||
@@ -325,8 +355,8 @@ last statement in BODY, as elisp."
|
||||
"Pass BODY to the Lua process in SESSION.
|
||||
If RESULT-TYPE equals `output' then return standard output as a
|
||||
string. If RESULT-TYPE equals `value' then return the value of the
|
||||
last statement in BODY, as elisp."
|
||||
(let* ((send-wait (lambda () (comint-send-input nil t) (sleep-for 0 5)))
|
||||
last statement in BODY, as Emacs Lisp."
|
||||
(let* ((send-wait (lambda () (comint-send-input nil t) (sleep-for 0.005)))
|
||||
(dump-last-value
|
||||
(lambda
|
||||
(tmp-file pp)
|
||||
@@ -399,7 +429,7 @@ fd:close()"
|
||||
(org-babel-lua-table-or-string results)))))
|
||||
|
||||
(defun org-babel-lua-read-string (string)
|
||||
"Strip single quotes from around Lua string."
|
||||
"Strip single quotes from around Lua STRING."
|
||||
(org-unbracket-string "'" "'" string))
|
||||
|
||||
(provide 'ob-lua)
|
||||
|
||||
Reference in New Issue
Block a user