1 ;;; ob-lua.el --- Org Babel functions for Lua evaluation -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014, 2016-2019 Free Software Foundation, Inc.
5 ;; Authors: Dieter Schoen
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; for session support, lua-mode is needed.
26 ;; lua-mode is not part of GNU Emacs/orgmode, but can be obtained
27 ;; from marmalade or melpa.
28 ;; The source repository is here:
29 ;; https://github.com/immerrr/lua-mode
31 ;; However, sessions are not yet working.
33 ;; Org-Babel support for evaluating lua source code.
40 (declare-function lua-shell
"ext:lua-mode" (&optional argprompt
))
41 (declare-function lua-toggle-shells
"ext:lua-mode" (arg))
42 (declare-function run-lua
"ext:lua" (cmd &optional dedicated show
))
44 (defvar org-babel-tangle-lang-exts
)
45 (add-to-list 'org-babel-tangle-lang-exts
'("lua" .
"lua"))
47 (defvar org-babel-default-header-args
:lua
'())
49 (defcustom org-babel-lua-command
"lua"
50 "Name of the command for executing Lua code."
52 :package-version
'(Org .
"8.3")
56 (defcustom org-babel-lua-mode
'lua-mode
57 "Preferred lua mode for use in running lua interactively.
58 This will typically be 'lua-mode."
61 :package-version
'(Org .
"8.3")
64 (defcustom org-babel-lua-hline-to
"None"
65 "Replace hlines in incoming tables with this when translating to lua."
68 :package-version
'(Org .
"8.3")
71 (defcustom org-babel-lua-None-to
'hline
72 "Replace 'None' in lua tables with this before returning."
75 :package-version
'(Org .
"8.3")
78 (defun org-babel-execute:lua
(body params
)
79 "Execute a block of Lua code with Babel.
80 This function is called by `org-babel-execute-src-block'."
81 (let* ((session (org-babel-lua-initiate-session
82 (cdr (assq :session params
))))
83 (result-params (cdr (assq :result-params params
)))
84 (result-type (cdr (assq :result-type params
)))
85 (return-val (when (and (eq result-type
'value
) (not session
))
86 (cdr (assq :return params
))))
87 (preamble (cdr (assq :preamble params
)))
89 (org-babel-expand-body:generic
90 (concat body
(if return-val
(format "\nreturn %s" return-val
) ""))
91 params
(org-babel-variable-assignments:lua params
)))
92 (result (org-babel-lua-evaluate
93 session full-body result-type result-params preamble
)))
94 (org-babel-reassemble-table
96 (org-babel-pick-name (cdr (assq :colname-names params
))
97 (cdr (assq :colnames params
)))
98 (org-babel-pick-name (cdr (assq :rowname-names params
))
99 (cdr (assq :rownames params
))))))
101 (defun org-babel-prep-session:lua
(session params
)
102 "Prepare SESSION according to the header arguments in PARAMS.
103 VARS contains resolved variable references"
104 (let* ((session (org-babel-lua-initiate-session session
))
106 (org-babel-variable-assignments:lua params
)))
107 (org-babel-comint-in-buffer session
109 (end-of-line 1) (insert var
) (comint-send-input)
110 (org-babel-comint-wait-for-output session
)) var-lines
))
113 (defun org-babel-load-session:lua
(session body params
)
114 "Load BODY into SESSION."
115 (save-window-excursion
116 (let ((buffer (org-babel-prep-session:lua session params
)))
117 (with-current-buffer buffer
118 (goto-char (process-mark (get-buffer-process (current-buffer))))
119 (insert (org-babel-chomp body
)))
124 (defun org-babel-variable-assignments:lua
(params)
125 "Return a list of Lua statements assigning the block's variables."
130 (org-babel-lua-var-to-lua (cdr pair
))))
131 (org-babel--get-vars params
)))
133 (defun org-babel-lua-var-to-lua (var)
134 "Convert an elisp value to a lua variable.
135 Convert an elisp value, VAR, into a string of lua source code
136 specifying a variable of the same value."
138 (if (and (= 1 (length var
)) (not (listp (car var
))))
139 (org-babel-lua-var-to-lua (car var
))
142 (not (listp (car var
))))
144 (substring-no-properties (car var
))
146 (org-babel-lua-var-to-lua (cdr var
)))
147 (concat "{" (mapconcat #'org-babel-lua-var-to-lua var
", ") "}")))
149 org-babel-lua-hline-to
151 (if (and (stringp var
) (string-match "[\n\r]" var
)) "[=[%s]=]" "%S")
152 (if (stringp var
) (substring-no-properties var
) var
)))))
154 (defun org-babel-lua-table-or-string (results)
155 "Convert RESULTS into an appropriate elisp value.
156 If the results look like a list or tuple, then convert them into an
157 Emacs-lisp table, otherwise return the results as a string."
158 (let ((res (org-babel-script-escape results
)))
160 (mapcar (lambda (el) (if (eq el
'None
)
161 org-babel-lua-None-to el
))
165 (defvar org-babel-lua-buffers
'((:default .
"*Lua*")))
167 (defun org-babel-lua-session-buffer (session)
168 "Return the buffer associated with SESSION."
169 (cdr (assoc session org-babel-lua-buffers
)))
171 (defun org-babel-lua-with-earmuffs (session)
172 (let ((name (if (stringp session
) session
(format "%s" session
))))
173 (if (and (string= "*" (substring name
0 1))
174 (string= "*" (substring name
(- (length name
) 1))))
176 (format "*%s*" name
))))
178 (defun org-babel-lua-without-earmuffs (session)
179 (let ((name (if (stringp session
) session
(format "%s" session
))))
180 (if (and (string= "*" (substring name
0 1))
181 (string= "*" (substring name
(- (length name
) 1))))
182 (substring name
1 (- (length name
) 1))
185 (defvar lua-default-interpreter
)
186 (defvar lua-which-bufname
)
187 (defvar lua-shell-buffer-name
)
188 (defun org-babel-lua-initiate-session-by-key (&optional session
)
189 "Initiate a lua session.
190 If there is not a current inferior-process-buffer in SESSION
191 then create. Return the initialized session."
192 ;; (require org-babel-lua-mode)
193 (save-window-excursion
194 (let* ((session (if session
(intern session
) :default
))
195 (lua-buffer (org-babel-lua-session-buffer session
))
196 ;; (cmd (if (member system-type '(cygwin windows-nt ms-dos))
197 ;; (concat org-babel-lua-command " -i")
198 ;; org-babel-lua-command))
201 ((and (eq 'lua-mode org-babel-lua-mode
)
202 (fboundp 'lua-start-process
)) ; lua-mode.el
203 ;; Make sure that lua-which-bufname is initialized, as otherwise
204 ;; it will be overwritten the first time a Lua buffer is
206 ;;(lua-toggle-shells lua-default-interpreter)
207 ;; `lua-shell' creates a buffer whose name is the value of
208 ;; `lua-which-bufname' with '*'s at the beginning and end
209 (let* ((bufname (if (and lua-buffer
(buffer-live-p lua-buffer
))
210 (replace-regexp-in-string ;; zap surrounding *
211 "^\\*\\([^*]+\\)\\*$" "\\1" (buffer-name lua-buffer
))
212 (concat "Lua-" (symbol-name session
))))
213 (lua-which-bufname bufname
))
215 (setq lua-buffer
(org-babel-lua-with-earmuffs bufname
))))
217 (error "No function available for running an inferior Lua")))
218 (setq org-babel-lua-buffers
219 (cons (cons session lua-buffer
)
220 (assq-delete-all session org-babel-lua-buffers
)))
223 (defun org-babel-lua-initiate-session (&optional session _params
)
224 "Create a session named SESSION according to PARAMS."
225 (unless (string= session
"none")
226 (error "Sessions currently not supported, work in progress")
227 (org-babel-lua-session-buffer
228 (org-babel-lua-initiate-session-by-key session
))))
230 (defvar org-babel-lua-eoe-indicator
"--eoe"
231 "A string to indicate that evaluation has completed.")
233 (defvar org-babel-lua-wrapper-method
239 fd=io.open(\"%s\", \"w\")
242 (defvar org-babel-lua-pp-wrapper-method
245 function t2s(t, indent)
246 if indent == nil then
249 if type(t) == \"table\" then
251 for k,v in pairs(t) do
252 if type(v) == \"table\" then
253 ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \\n\" ..
254 t2s(v, indent .. \" \")
256 ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \" ..
257 t2s(v, indent .. \" \") .. \"\\n\"
271 fd=io.open(\"%s\", \"w\")
272 fd:write(t2s(main()))
275 (defun org-babel-lua-evaluate
276 (session body
&optional result-type result-params preamble
)
277 "Evaluate BODY as Lua code."
279 (org-babel-lua-evaluate-session
280 session body result-type result-params
)
281 (org-babel-lua-evaluate-external-process
282 body result-type result-params preamble
)))
284 (defun org-babel-lua-evaluate-external-process
285 (body &optional result-type result-params preamble
)
286 "Evaluate BODY in external lua process.
287 If RESULT-TYPE equals 'output then return standard output as a
288 string. If RESULT-TYPE equals 'value then return the value of the
289 last statement in BODY, as elisp."
292 (`output
(org-babel-eval org-babel-lua-command
293 (concat preamble
(and preamble
"\n")
295 (`value
(let ((tmp-file (org-babel-temp-file "lua-")))
297 org-babel-lua-command
299 preamble
(and preamble
"\n")
301 (if (member "pp" result-params
)
302 org-babel-lua-pp-wrapper-method
303 org-babel-lua-wrapper-method
)
305 (lambda (line) (format "\t%s" line
))
307 (org-remove-indentation
310 (org-babel-process-file-name tmp-file
'noquote
))))
311 (org-babel-eval-read-file tmp-file
))))))
312 (org-babel-result-cond result-params
314 (org-babel-lua-table-or-string (org-trim raw
)))))
316 (defun org-babel-lua-evaluate-session
317 (session body
&optional result-type result-params
)
318 "Pass BODY to the Lua process in SESSION.
319 If RESULT-TYPE equals 'output then return standard output as a
320 string. If RESULT-TYPE equals 'value then return the value of the
321 last statement in BODY, as elisp."
322 (let* ((send-wait (lambda () (comint-send-input nil t
) (sleep-for 0 5)))
327 (lambda (statement) (insert statement
) (funcall send-wait
))
331 function t2s(t, indent)
332 if indent == nil then
335 if type(t) == \"table\" then
337 for k,v in pairs(t) do
338 if type(v) == \"table\" then
339 ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \\n\" ..
340 t2s(v, indent .. \" \")
342 ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \" ..
343 t2s(v, indent .. \" \") .. \"\\n\"
352 (concat "fd:write(_))
354 (org-babel-process-file-name tmp-file
'noquote
)))
355 (list (format "fd=io.open(\"%s\", \"w\")
358 (org-babel-process-file-name tmp-file
360 (input-body (lambda (body)
361 (mapc (lambda (line) (insert line
) (funcall send-wait
))
362 (split-string body
"[\r\n]"))
363 (funcall send-wait
)))
370 (org-babel-comint-with-output
371 (session org-babel-lua-eoe-indicator t body
)
372 (funcall input-body body
)
373 (funcall send-wait
) (funcall send-wait
)
374 (insert org-babel-lua-eoe-indicator
)
378 (let ((tmp-file (org-babel-temp-file "lua-")))
379 (org-babel-comint-with-output
380 (session org-babel-lua-eoe-indicator nil body
)
381 (let ((comint-process-echoes nil
))
382 (funcall input-body body
)
383 (funcall dump-last-value tmp-file
384 (member "pp" result-params
))
385 (funcall send-wait
) (funcall send-wait
)
386 (insert org-babel-lua-eoe-indicator
)
387 (funcall send-wait
)))
388 (org-babel-eval-read-file tmp-file
))))))
389 (unless (string= (substring org-babel-lua-eoe-indicator
1 -
1) results
)
390 (org-babel-result-cond result-params
392 (org-babel-lua-table-or-string results
)))))
394 (defun org-babel-lua-read-string (string)
395 "Strip 's from around Lua string."
396 (org-unbracket-string "'" "'" string
))
402 ;;; ob-lua.el ends here