Merge branch 'master' of code.orgmode.org:bzg/org-mode
[org-mode/org-tableheadings.git] / lisp / ob-js.el
blobf18aa9da63db7c37d157ceb77c77e152e5ede90b
1 ;;; ob-js.el --- Babel Functions for Javascript -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2018 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, js
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/>.
24 ;;; Commentary:
26 ;; Now working with SBCL for both session and external evaluation.
28 ;; This certainly isn't optimally robust, but it seems to be working
29 ;; for the basic use cases.
31 ;;; Requirements:
33 ;; - a non-browser javascript engine such as node.js http://nodejs.org/
34 ;; or mozrepl http://wiki.github.com/bard/mozrepl/
36 ;; - for session based evaluation mozrepl and moz.el are required see
37 ;; http://wiki.github.com/bard/mozrepl/emacs-integration for
38 ;; configuration instructions
40 ;;; Code:
41 (require 'ob)
43 (declare-function run-mozilla "ext:moz" (arg))
44 (declare-function httpd-start "simple-httpd" ())
45 (declare-function run-skewer "skewer-mode" ())
46 (declare-function indium-run-node "indium-nodejs" (command))
47 (declare-function indium-eval "indium-interaction" (string &optional callback))
49 (defvar org-babel-default-header-args:js '()
50 "Default header arguments for js code blocks.")
52 (defvar org-babel-js-eoe "org-babel-js-eoe"
53 "String to indicate that evaluation has completed.")
55 (defcustom org-babel-js-cmd "node"
56 "Name of command used to evaluate js blocks."
57 :group 'org-babel
58 :version "24.1"
59 :type '(choice (const "node")
60 (const "mozrepl")
61 (const "skewer-mode")
62 (const "indium")
63 (const "js-comint"))
64 :safe #'stringp)
66 (defvar org-babel-js-function-wrapper
67 "require('sys').print(require('sys').inspect(function(){\n%s\n}()));"
68 "Javascript code to print value of body.")
70 (defun org-babel-execute:js (body params)
71 "Execute a block of Javascript code with org-babel.
72 This function is called by `org-babel-execute-src-block'"
73 (let* ((org-babel-js-cmd (or (cdr (assq :cmd params)) org-babel-js-cmd))
74 (session (cdr (assq :session params)))
75 (result-type (cdr (assq :result-type params)))
76 (full-body (org-babel-expand-body:generic
77 body params (org-babel-variable-assignments:js params)))
78 (result (cond
79 ;; no session specified, external evaluation
80 ((string= session "none")
81 (let ((script-file (org-babel-temp-file "js-script-")))
82 (with-temp-file script-file
83 (insert
84 ;; return the value or the output
85 (if (string= result-type "value")
86 (format org-babel-js-function-wrapper full-body)
87 full-body)))
88 (org-babel-eval
89 (format "%s %s" org-babel-js-cmd
90 (org-babel-process-file-name script-file)) "")))
91 ;; Indium Node REPL
92 ;; separate case because Indium REPL is not inherited from comint-mode
93 ((string= session "*JS REPL*")
94 (require 'indium-repl)
95 (unless (get-buffer session)
96 (indium-run-node))
97 (indium-eval full-body))
98 ;; session evaluation
100 (let ((session (org-babel-prep-session:js
101 (cdr (assq :session params)) params)))
102 (nth 1
103 (org-babel-comint-with-output
104 (session (format "%S" org-babel-js-eoe) t body)
105 (dolist (code (list body (format "%S" org-babel-js-eoe)))
106 (insert (org-babel-chomp code))
107 (comint-send-input nil t)))))))))
108 (org-babel-result-cond (cdr (assq :result-params params))
109 result (org-babel-js-read result))))
111 (defun org-babel-js-read (results)
112 "Convert RESULTS into an appropriate elisp value.
113 If RESULTS look like a table, then convert them into an
114 Emacs-lisp table, otherwise return the results as a string."
115 (org-babel-read
116 (if (and (stringp results)
117 (string-prefix-p "[" results)
118 (string-suffix-p "]" results))
119 (org-babel-read
120 (concat "'"
121 (replace-regexp-in-string
122 "\\[" "(" (replace-regexp-in-string
123 "\\]" ")" (replace-regexp-in-string
124 ",[[:space:]]" " "
125 (replace-regexp-in-string
126 "'" "\"" results))))))
127 results)))
129 (defun org-babel-js-var-to-js (var)
130 "Convert VAR into a js variable.
131 Convert an elisp value into a string of js source code
132 specifying a variable of the same value."
133 (if (listp var)
134 (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
135 (replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
137 (defun org-babel-prep-session:js (session params)
138 "Prepare SESSION according to the header arguments specified in PARAMS."
139 (let* ((session (org-babel-js-initiate-session session))
140 (var-lines (org-babel-variable-assignments:js params)))
141 (when session
142 (org-babel-comint-in-buffer session
143 (goto-char (point-max))
144 (dolist (var var-lines)
145 (insert var)
146 (comint-send-input nil t)
147 (org-babel-comint-wait-for-output session)
148 (sit-for .1)
149 (goto-char (point-max)))))
150 session))
152 (defun org-babel-variable-assignments:js (params)
153 "Return list of Javascript statements assigning the block's variables."
154 (mapcar
155 (lambda (pair) (format "var %s=%s;"
156 (car pair) (org-babel-js-var-to-js (cdr pair))))
157 (org-babel--get-vars params)))
159 (defun org-babel-js-initiate-session (&optional session)
160 "If there is not a current inferior-process-buffer in SESSION
161 then create. Return the initialized session."
162 (cond
163 ((string= session "none")
164 (warn "Session evaluation of ob-js is not supported"))
165 ((string= "*skewer-repl*" session)
166 (require 'skewer-repl)
167 (let ((session-buffer (get-buffer "*skewer-repl*")))
168 (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer))
169 (comint-check-proc session-buffer))
170 session-buffer
171 ;; start skewer REPL.
172 (httpd-start)
173 (run-skewer)
174 session-buffer)))
175 ((string= "*Javascript REPL*" session)
176 (require 'js-comint)
177 (let ((session-buffer "*Javascript REPL*"))
178 (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer))
179 (comint-check-proc session-buffer))
180 session-buffer
181 (call-interactively 'run-js)
182 (sit-for .5)
183 session-buffer)))
184 ((string= "mozrepl" org-babel-js-cmd)
185 (require 'moz)
186 (let ((session-buffer (save-window-excursion
187 (run-mozilla nil)
188 (rename-buffer session)
189 (current-buffer))))
190 (if (org-babel-comint-buffer-livep session-buffer)
191 (progn (sit-for .25) session-buffer)
192 (sit-for .5)
193 (org-babel-js-initiate-session session))))
194 ((string= "node" org-babel-js-cmd )
195 (error "Session evaluation with node.js is not supported"))
197 (error "Sessions are only supported with mozrepl add \":cmd mozrepl\""))))
199 (provide 'ob-js)
203 ;;; ob-js.el ends here