1 ;;; ob-lua.el --- Org Babel functions for Lua evaluation -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014, 2016-2017 Free Software Foundation, Inc.
5 ;; Authors: Dieter Schoen
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://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.
39 (declare-function org-remove-indentation
"org" (code &optional n
))
40 (declare-function org-trim
"org" (s &optional keep-lead
))
41 (declare-function lua-shell
"ext:lua-mode" (&optional argprompt
))
42 (declare-function lua-toggle-shells
"ext:lua-mode" (arg))
43 (declare-function run-lua
"ext:lua" (cmd &optional dedicated show
))
45 (defvar org-babel-tangle-lang-exts
)
46 (add-to-list 'org-babel-tangle-lang-exts
'("lua" .
"lua"))
48 (defvar org-babel-default-header-args
:lua
'())
50 (defcustom org-babel-lua-command
"lua"
51 "Name of the command for executing Lua code."
53 :package-version
'(Org .
"8.3")
57 (defcustom org-babel-lua-mode
'lua-mode
58 "Preferred lua mode for use in running lua interactively.
59 This will typically be 'lua-mode."
62 :package-version
'(Org .
"8.3")
65 (defcustom org-babel-lua-hline-to
"None"
66 "Replace hlines in incoming tables with this when translating to lua."
69 :package-version
'(Org .
"8.3")
72 (defcustom org-babel-lua-None-to
'hline
73 "Replace 'None' in lua tables with this before returning."
76 :package-version
'(Org .
"8.3")
79 (defun org-babel-execute:lua
(body params
)
80 "Execute a block of Lua code with Babel.
81 This function is called by `org-babel-execute-src-block'."
82 (let* ((session (org-babel-lua-initiate-session
83 (cdr (assq :session params
))))
84 (result-params (cdr (assq :result-params params
)))
85 (result-type (cdr (assq :result-type params
)))
86 (return-val (when (and (eq result-type
'value
) (not session
))
87 (cdr (assq :return params
))))
88 (preamble (cdr (assq :preamble params
)))
90 (org-babel-expand-body:generic
91 (concat body
(if return-val
(format "\nreturn %s" return-val
) ""))
92 params
(org-babel-variable-assignments:lua params
)))
93 (result (org-babel-lua-evaluate
94 session full-body result-type result-params preamble
)))
95 (org-babel-reassemble-table
97 (org-babel-pick-name (cdr (assq :colname-names params
))
98 (cdr (assq :colnames params
)))
99 (org-babel-pick-name (cdr (assq :rowname-names params
))
100 (cdr (assq :rownames params
))))))
102 (defun org-babel-prep-session:lua
(session params
)
103 "Prepare SESSION according to the header arguments in PARAMS.
104 VARS contains resolved variable references"
105 (let* ((session (org-babel-lua-initiate-session session
))
107 (org-babel-variable-assignments:lua params
)))
108 (org-babel-comint-in-buffer session
110 (end-of-line 1) (insert var
) (comint-send-input)
111 (org-babel-comint-wait-for-output session
)) var-lines
))
114 (defun org-babel-load-session:lua
(session body params
)
115 "Load BODY into SESSION."
116 (save-window-excursion
117 (let ((buffer (org-babel-prep-session:lua session params
)))
118 (with-current-buffer buffer
119 (goto-char (process-mark (get-buffer-process (current-buffer))))
120 (insert (org-babel-chomp body
)))
125 (defun org-babel-variable-assignments:lua
(params)
126 "Return a list of Lua statements assigning the block's variables."
131 (org-babel-lua-var-to-lua (cdr pair
))))
132 (org-babel--get-vars params
)))
134 (defun org-babel-lua-var-to-lua (var)
135 "Convert an elisp value to a lua variable.
136 Convert an elisp value, VAR, into a string of lua source code
137 specifying a variable of the same value."
139 (if (and (= 1 (length var
)) (not (listp (car var
))))
140 (org-babel-lua-var-to-lua (car var
))
143 (not (listp (car var
))))
145 (substring-no-properties (car var
))
147 (org-babel-lua-var-to-lua (cdr var
)))
148 (concat "{" (mapconcat #'org-babel-lua-var-to-lua var
", ") "}")))
150 org-babel-lua-hline-to
152 (if (and (stringp var
) (string-match "[\n\r]" var
)) "\"\"%S\"\"" "%S")
153 (if (stringp var
) (substring-no-properties var
) var
)))))
155 (defun org-babel-lua-table-or-string (results)
156 "Convert RESULTS into an appropriate elisp value.
157 If the results look like a list or tuple, then convert them into an
158 Emacs-lisp table, otherwise return the results as a string."
159 (let ((res (org-babel-script-escape results
)))
161 (mapcar (lambda (el) (if (eq el
'None
)
162 org-babel-lua-None-to el
))
166 (defvar org-babel-lua-buffers
'((:default .
"*Lua*")))
168 (defun org-babel-lua-session-buffer (session)
169 "Return the buffer associated with SESSION."
170 (cdr (assoc session org-babel-lua-buffers
)))
172 (defun org-babel-lua-with-earmuffs (session)
173 (let ((name (if (stringp session
) session
(format "%s" session
))))
174 (if (and (string= "*" (substring name
0 1))
175 (string= "*" (substring name
(- (length name
) 1))))
177 (format "*%s*" name
))))
179 (defun org-babel-lua-without-earmuffs (session)
180 (let ((name (if (stringp session
) session
(format "%s" session
))))
181 (if (and (string= "*" (substring name
0 1))
182 (string= "*" (substring name
(- (length name
) 1))))
183 (substring name
1 (- (length name
) 1))
186 (defvar lua-default-interpreter
)
187 (defvar lua-which-bufname
)
188 (defvar lua-shell-buffer-name
)
189 (defun org-babel-lua-initiate-session-by-key (&optional session
)
190 "Initiate a lua session.
191 If there is not a current inferior-process-buffer in SESSION
192 then create. Return the initialized session."
193 ;; (require org-babel-lua-mode)
194 (save-window-excursion
195 (let* ((session (if session
(intern session
) :default
))
196 (lua-buffer (org-babel-lua-session-buffer session
))
197 ;; (cmd (if (member system-type '(cygwin windows-nt ms-dos))
198 ;; (concat org-babel-lua-command " -i")
199 ;; org-babel-lua-command))
202 ((and (eq 'lua-mode org-babel-lua-mode
)
203 (fboundp 'lua-start-process
)) ; lua-mode.el
204 ;; Make sure that lua-which-bufname is initialized, as otherwise
205 ;; it will be overwritten the first time a Lua buffer is
207 ;;(lua-toggle-shells lua-default-interpreter)
208 ;; `lua-shell' creates a buffer whose name is the value of
209 ;; `lua-which-bufname' with '*'s at the beginning and end
210 (let* ((bufname (if (and lua-buffer
(buffer-live-p lua-buffer
))
211 (replace-regexp-in-string ;; zap surrounding *
212 "^\\*\\([^*]+\\)\\*$" "\\1" (buffer-name lua-buffer
))
213 (concat "Lua-" (symbol-name session
))))
214 (lua-which-bufname bufname
))
216 (setq lua-buffer
(org-babel-lua-with-earmuffs bufname
))))
218 (error "No function available for running an inferior Lua")))
219 (setq org-babel-lua-buffers
220 (cons (cons session lua-buffer
)
221 (assq-delete-all session org-babel-lua-buffers
)))
224 (defun org-babel-lua-initiate-session (&optional session _params
)
225 "Create a session named SESSION according to PARAMS."
226 (unless (string= session
"none")
227 (error "Sessions currently not supported, work in progress")
228 (org-babel-lua-session-buffer
229 (org-babel-lua-initiate-session-by-key session
))))
231 (defvar org-babel-lua-eoe-indicator
"--eoe"
232 "A string to indicate that evaluation has completed.")
234 (defvar org-babel-lua-wrapper-method
240 fd=io.open(\"%s\", \"w\")
243 (defvar org-babel-lua-pp-wrapper-method
246 function t2s(t, indent)
247 if indent == nil then
250 if type(t) == \"table\" then
252 for k,v in pairs(t) do
253 if type(v) == \"table\" then
254 ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \\n\" ..
255 t2s(v, indent .. \" \")
257 ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \" ..
258 t2s(v, indent .. \" \") .. \"\\n\"
272 fd=io.open(\"%s\", \"w\")
273 fd:write(t2s(main()))
276 (defun org-babel-lua-evaluate
277 (session body
&optional result-type result-params preamble
)
278 "Evaluate BODY as Lua code."
280 (org-babel-lua-evaluate-session
281 session body result-type result-params
)
282 (org-babel-lua-evaluate-external-process
283 body result-type result-params preamble
)))
285 (defun org-babel-lua-evaluate-external-process
286 (body &optional result-type result-params preamble
)
287 "Evaluate BODY in external lua process.
288 If RESULT-TYPE equals 'output then return standard output as a
289 string. If RESULT-TYPE equals 'value then return the value of the
290 last statement in BODY, as elisp."
293 (`output
(org-babel-eval org-babel-lua-command
294 (concat (if preamble
(concat preamble
"\n"))
296 (`value
(let ((tmp-file (org-babel-temp-file "lua-")))
298 org-babel-lua-command
300 (if preamble
(concat preamble
"\n") "")
302 (if (member "pp" result-params
)
303 org-babel-lua-pp-wrapper-method
304 org-babel-lua-wrapper-method
)
306 (lambda (line) (format "\t%s" line
))
308 (org-remove-indentation
311 (org-babel-process-file-name tmp-file
'noquote
))))
312 (org-babel-eval-read-file tmp-file
))))))
313 (org-babel-result-cond result-params
315 (org-babel-lua-table-or-string (org-trim raw
)))))
317 (defun org-babel-lua-evaluate-session
318 (session body
&optional result-type result-params
)
319 "Pass BODY to the Lua process in SESSION.
320 If RESULT-TYPE equals 'output then return standard output as a
321 string. If RESULT-TYPE equals 'value then return the value of the
322 last statement in BODY, as elisp."
323 (let* ((send-wait (lambda () (comint-send-input nil t
) (sleep-for 0 5)))
328 (lambda (statement) (insert statement
) (funcall send-wait
))
332 function t2s(t, indent)
333 if indent == nil then
336 if type(t) == \"table\" then
338 for k,v in pairs(t) do
339 if type(v) == \"table\" then
340 ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \\n\" ..
341 t2s(v, indent .. \" \")
343 ts = ts .. indent .. t2s(k,indent .. \" \") .. \" = \" ..
344 t2s(v, indent .. \" \") .. \"\\n\"
353 (concat "fd:write(_))
355 (org-babel-process-file-name tmp-file
'noquote
)))
356 (list (format "fd=io.open(\"%s\", \"w\")
359 (org-babel-process-file-name tmp-file
361 (input-body (lambda (body)
362 (mapc (lambda (line) (insert line
) (funcall send-wait
))
363 (split-string body
"[\r\n]"))
364 (funcall send-wait
)))
371 (org-babel-comint-with-output
372 (session org-babel-lua-eoe-indicator t body
)
373 (funcall input-body body
)
374 (funcall send-wait
) (funcall send-wait
)
375 (insert org-babel-lua-eoe-indicator
)
379 (let ((tmp-file (org-babel-temp-file "lua-")))
380 (org-babel-comint-with-output
381 (session org-babel-lua-eoe-indicator nil body
)
382 (let ((comint-process-echoes nil
))
383 (funcall input-body body
)
384 (funcall dump-last-value tmp-file
385 (member "pp" result-params
))
386 (funcall send-wait
) (funcall send-wait
)
387 (insert org-babel-lua-eoe-indicator
)
388 (funcall send-wait
)))
389 (org-babel-eval-read-file tmp-file
))))))
390 (unless (string= (substring org-babel-lua-eoe-indicator
1 -
1) results
)
391 (org-babel-result-cond result-params
393 (org-babel-lua-table-or-string results
)))))
395 (defun org-babel-lua-read-string (string)
396 "Strip 's from around Lua string."
397 (org-unbracket-string "'" "'" string
))
403 ;;; ob-lua.el ends here