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/>.
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.
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
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."
59 :type
'(choice (const "node")
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
)))
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
84 ;; return the value or the output
85 (if (string= result-type
"value")
86 (format org-babel-js-function-wrapper full-body
)
89 (format "%s %s" org-babel-js-cmd
90 (org-babel-process-file-name script-file
)) "")))
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
)
97 (indium-eval full-body
))
100 (let ((session (org-babel-prep-session:js
101 (cdr (assq :session params
)) params
)))
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."
116 (if (and (stringp results
)
117 (string-prefix-p "[" results
)
118 (string-suffix-p "]" results
))
121 (replace-regexp-in-string
122 "\\[" "(" (replace-regexp-in-string
123 "\\]" ")" (replace-regexp-in-string
125 (replace-regexp-in-string
126 "'" "\"" 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."
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
)))
142 (org-babel-comint-in-buffer session
143 (goto-char (point-max))
144 (dolist (var var-lines
)
146 (comint-send-input nil t
)
147 (org-babel-comint-wait-for-output session
)
149 (goto-char (point-max)))))
152 (defun org-babel-variable-assignments:js
(params)
153 "Return list of Javascript statements assigning the block's variables."
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."
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
))
171 ;; start skewer REPL.
175 ((string= "*Javascript REPL*" session
)
177 (let ((session-buffer "*Javascript REPL*"))
178 (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer
))
179 (comint-check-proc session-buffer
))
181 (call-interactively 'run-js
)
184 ((string= "mozrepl" org-babel-js-cmd
)
186 (let ((session-buffer (save-window-excursion
188 (rename-buffer session
)
190 (if (org-babel-comint-buffer-livep session-buffer
)
191 (progn (sit-for .25) session-buffer
)
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\""))))
203 ;;; ob-js.el ends here