1 ;;; ob-js.el --- org-babel functions for Javascript
3 ;; Copyright (C) 2010-2011 Free Software Foundation
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, js
7 ;; Homepage: http://orgmode.org
11 ;; This program 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, or (at your option)
16 ;; This program 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Now working with SBCL for both session and external evaluation.
30 ;; This certainly isn't optimally robust, but it seems to be working
31 ;; for the basic use cases.
35 ;; - a non-browser javascript engine such as node.js http://nodejs.org/
36 ;; or mozrepl http://wiki.github.com/bard/mozrepl/
38 ;; - for session based evaluation mozrepl and moz.el are required see
39 ;; http://wiki.github.com/bard/mozrepl/emacs-integration for
40 ;; configuration instructions
47 (eval-when-compile (require 'cl
))
49 (declare-function run-mozilla
"ext:moz" (arg))
51 (defvar org-babel-default-header-args
:js
'()
52 "Default header arguments for js code blocks.")
54 (defvar org-babel-js-eoe
"org-babel-js-eoe"
55 "String to indicate that evaluation has completed.")
57 (defcustom org-babel-js-cmd
"node"
58 "Name of command used to evaluate js blocks."
62 (defvar org-babel-js-function-wrapper
63 "require('sys').print(require('sys').inspect(function(){%s}()));"
64 "Javascript code to print value of body.")
66 (defun org-babel-execute:js
(body params
)
67 "Execute a block of Javascript code with org-babel.
68 This function is called by `org-babel-execute-src-block'"
69 (let* ((org-babel-js-cmd (or (cdr (assoc :cmd params
)) org-babel-js-cmd
))
70 (result-type (cdr (assoc :result-type params
)))
71 (full-body (org-babel-expand-body:generic
72 body params
(org-babel-variable-assignments:js params
))))
74 (if (not (string= (cdr (assoc :session params
)) "none"))
76 (let ((session (org-babel-prep-session:js
77 (cdr (assoc :session params
)) params
)))
79 (org-babel-comint-with-output
80 (session (format "%S" org-babel-js-eoe
) t body
)
83 (insert (org-babel-chomp line
)) (comint-send-input nil t
))
84 (list body
(format "%S" org-babel-js-eoe
))))))
85 ;; external evaluation
86 (let ((script-file (org-babel-temp-file "js-script-")))
87 (with-temp-file script-file
89 ;; return the value or the output
90 (if (string= result-type
"value")
91 (format org-babel-js-function-wrapper full-body
)
94 (format "%s %s" org-babel-js-cmd
95 (org-babel-process-file-name script-file
)) ""))))))
97 (defun org-babel-js-read (results)
98 "Convert RESULTS into an appropriate elisp value.
99 If RESULTS look like a table, then convert them into an
100 Emacs-lisp table, otherwise return the results as a string."
102 (if (and (stringp results
) (string-match "^\\[.+\\]$" results
))
105 (replace-regexp-in-string
106 "\\[" "(" (replace-regexp-in-string
107 "\\]" ")" (replace-regexp-in-string
108 ", " " " (replace-regexp-in-string
109 "'" "\"" results
))))))
112 (defun org-babel-js-var-to-js (var)
113 "Convert VAR into a js variable.
114 Convert an elisp value into a string of js source code
115 specifying a variable of the same value."
117 (concat "[" (mapconcat #'org-babel-js-var-to-js var
", ") "]")
120 (defun org-babel-prep-session:js
(session params
)
121 "Prepare SESSION according to the header arguments specified in PARAMS."
122 (let* ((session (org-babel-js-initiate-session session
))
123 (var-lines (org-babel-variable-assignments:js params
)))
125 (org-babel-comint-in-buffer session
126 (sit-for .5) (goto-char (point-max))
128 (insert var
) (comint-send-input nil t
)
129 (org-babel-comint-wait-for-output session
)
130 (sit-for .1) (goto-char (point-max))) var-lines
)))
133 (defun org-babel-variable-assignments:js
(params)
134 "Return list of Javascript statements assigning the block's variables"
136 (lambda (pair) (format "var %s=%s;"
137 (car pair
) (org-babel-js-var-to-js (cdr pair
))))
138 (mapcar #'cdr
(org-babel-get-header params
:var
))))
140 (defun org-babel-js-initiate-session (&optional session
)
141 "If there is not a current inferior-process-buffer in SESSION
142 then create. Return the initialized session."
143 (unless (string= session
"none")
145 ((string= "mozrepl" org-babel-js-cmd
)
147 (let ((session-buffer (save-window-excursion
149 (rename-buffer session
)
151 (if (org-babel-comint-buffer-livep session-buffer
)
152 (progn (sit-for .25) session-buffer
)
154 (org-babel-js-initiate-session session
))))
155 ((string= "node" org-babel-js-cmd
)
156 (error "session evaluation with node.js is not supported"))
158 (error "sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
164 ;;; ob-js.el ends here