lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / ob-js.el
blob7b83c3e694adf30b34603899981dc1e2ccfb0001
1 ;;; ob-js.el --- Babel Functions for Javascript -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2019 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 "ext:simple-httpd" ())
45 (declare-function run-skewer "ext:skewer-mode" ())
46 (declare-function skewer-repl "ext:skewer-repl" ())
47 (declare-function indium-run-node "ext:indium-nodejs" (command))
48 (declare-function indium-eval "ext:indium-interaction" (string &optional callback))
50 (defvar org-babel-default-header-args:js '()
51 "Default header arguments for js code blocks.")
53 (defvar org-babel-js-eoe "org-babel-js-eoe"
54 "String to indicate that evaluation has completed.")
56 (defcustom org-babel-js-cmd "node"
57 "Name of command used to evaluate js blocks."
58 :group 'org-babel
59 :version "24.1"
60 :type '(choice (const "node")
61 (const "mozrepl")
62 (const "skewer-mode")
63 (const "indium")
64 (const "js-comint"))
65 :safe #'stringp)
67 (defvar org-babel-js-function-wrapper
68 "require('sys').print(require('sys').inspect(function(){\n%s\n}()));"
69 "Javascript code to print value of body.")
71 (defun org-babel-execute:js (body params)
72 "Execute a block of Javascript code with org-babel.
73 This function is called by `org-babel-execute-src-block'"
74 (let* ((org-babel-js-cmd (or (cdr (assq :cmd params)) org-babel-js-cmd))
75 (session (cdr (assq :session params)))
76 (result-type (cdr (assq :result-type params)))
77 (full-body (org-babel-expand-body:generic
78 body params (org-babel-variable-assignments:js params)))
79 (result (cond
80 ;; no session specified, external evaluation
81 ((string= session "none")
82 (let ((script-file (org-babel-temp-file "js-script-")))
83 (with-temp-file script-file
84 (insert
85 ;; return the value or the output
86 (if (string= result-type "value")
87 (format org-babel-js-function-wrapper full-body)
88 full-body)))
89 (org-babel-eval
90 (format "%s %s" org-babel-js-cmd
91 (org-babel-process-file-name script-file)) "")))
92 ;; Indium Node REPL. Separate case because Indium
93 ;; REPL is not inherited from Comint mode.
94 ((string= session "*JS REPL*")
95 (require 'indium-repl)
96 (unless (get-buffer session)
97 (indium-run-node org-babel-js-cmd))
98 (indium-eval full-body))
99 ;; session evaluation
101 (let ((session (org-babel-prep-session:js
102 (cdr (assq :session params)) params)))
103 (nth 1
104 (org-babel-comint-with-output
105 (session (format "%S" org-babel-js-eoe) t body)
106 (dolist (code (list body (format "%S" org-babel-js-eoe)))
107 (insert (org-babel-chomp code))
108 (comint-send-input nil t)))))))))
109 (org-babel-result-cond (cdr (assq :result-params params))
110 result (org-babel-js-read result))))
112 (defun org-babel-js-read (results)
113 "Convert RESULTS into an appropriate elisp value.
114 If RESULTS look like a table, then convert them into an
115 Emacs-lisp table, otherwise return the results as a string."
116 (org-babel-read
117 (if (and (stringp results)
118 (string-prefix-p "[" results)
119 (string-suffix-p "]" results))
120 (org-babel-read
121 (concat "'"
122 (replace-regexp-in-string
123 "\\[" "(" (replace-regexp-in-string
124 "\\]" ")" (replace-regexp-in-string
125 ",[[:space:]]" " "
126 (replace-regexp-in-string
127 "'" "\"" results))))))
128 results)))
130 (defun org-babel-js-var-to-js (var)
131 "Convert VAR into a js variable.
132 Convert an elisp value into a string of js source code
133 specifying a variable of the same value."
134 (if (listp var)
135 (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
136 (replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
138 (defun org-babel-prep-session:js (session params)
139 "Prepare SESSION according to the header arguments specified in PARAMS."
140 (let* ((session (org-babel-js-initiate-session session))
141 (var-lines (org-babel-variable-assignments:js params)))
142 (when session
143 (org-babel-comint-in-buffer session
144 (goto-char (point-max))
145 (dolist (var var-lines)
146 (insert var)
147 (comint-send-input nil t)
148 (org-babel-comint-wait-for-output session)
149 (sit-for .1)
150 (goto-char (point-max)))))
151 session))
153 (defun org-babel-variable-assignments:js (params)
154 "Return list of Javascript statements assigning the block's variables."
155 (mapcar
156 (lambda (pair) (format "var %s=%s;"
157 (car pair) (org-babel-js-var-to-js (cdr pair))))
158 (org-babel--get-vars params)))
160 (defun org-babel-js-initiate-session (&optional session _params)
161 "If there is not a current inferior-process-buffer in `SESSION'
162 then create. Return the initialized session."
163 (cond
164 ((string= session "none")
165 (warn "Session evaluation of ob-js is not supported"))
166 ((string= "*skewer-repl*" session)
167 (require 'skewer-repl)
168 (let ((session-buffer (get-buffer "*skewer-repl*")))
169 (if (and session-buffer
170 (org-babel-comint-buffer-livep (get-buffer session-buffer))
171 (comint-check-proc session-buffer))
172 session-buffer
173 ;; start skewer REPL.
174 (httpd-start)
175 (run-skewer)
176 (skewer-repl)
177 session-buffer)))
178 ((string= "*Javascript REPL*" session)
179 (require 'js-comint)
180 (let ((session-buffer "*Javascript REPL*"))
181 (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer))
182 (comint-check-proc session-buffer))
183 session-buffer
184 (call-interactively 'run-js)
185 (sit-for .5)
186 session-buffer)))
187 ((string= "mozrepl" org-babel-js-cmd)
188 (require 'moz)
189 (let ((session-buffer (save-window-excursion
190 (run-mozilla nil)
191 (rename-buffer session)
192 (current-buffer))))
193 (if (org-babel-comint-buffer-livep session-buffer)
194 (progn (sit-for .25) session-buffer)
195 (sit-for .5)
196 (org-babel-js-initiate-session session))))
197 ((string= "node" org-babel-js-cmd )
198 (error "Session evaluation with node.js is not supported"))
200 (error "Sessions are only supported with mozrepl add \":cmd mozrepl\""))))
202 (provide 'ob-js)
206 ;;; ob-js.el ends here