]> code.delx.au - gnu-emacs/blob - lisp/org/ob-js.el
Merge from emacs-23
[gnu-emacs] / lisp / org / ob-js.el
1 ;;; ob-js.el --- org-babel functions for Javascript
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, js
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.3
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Now working with SBCL for both session and external evaluation.
28 ;;
29 ;; This certainly isn't optimally robust, but it seems to be working
30 ;; for the basic use cases.
31
32 ;;; Requirements:
33
34 ;; - a non-browser javascript engine such as node.js http://nodejs.org/
35 ;; or mozrepl http://wiki.github.com/bard/mozrepl/
36 ;;
37 ;; - for session based evaluation mozrepl and moz.el are required see
38 ;; http://wiki.github.com/bard/mozrepl/emacs-integration for
39 ;; configuration instructions
40
41 ;;; Code:
42 (require 'ob)
43 (require 'ob-ref)
44 (require 'ob-comint)
45 (require 'ob-eval)
46 (eval-when-compile (require 'cl))
47
48 (declare-function run-mozilla "ext:moz" (arg))
49
50 (defvar org-babel-default-header-args:js '()
51 "Default header arguments for js code blocks.")
52
53 (defvar org-babel-js-eoe "org-babel-js-eoe"
54 "String to indicate that evaluation has completed.")
55
56 (defcustom org-babel-js-cmd "node"
57 "Name of command used to evaluate js blocks."
58 :group 'org-babel
59 :type 'string)
60
61 (defvar org-babel-js-function-wrapper
62 "require('sys').print(require('sys').inspect(function(){%s}()));"
63 "Javascript code to print value of body.")
64
65 (defun org-babel-execute:js (body params)
66 "Execute a block of Javascript code with org-babel.
67 This function is called by `org-babel-execute-src-block'"
68 (let* ((org-babel-js-cmd (or (cdr (assoc :cmd params)) org-babel-js-cmd))
69 (result-type (cdr (assoc :result-type params)))
70 (full-body (org-babel-expand-body:generic
71 body params (org-babel-variable-assignments:js params))))
72 (org-babel-js-read
73 (if (not (string= (cdr (assoc :session params)) "none"))
74 ;; session evaluation
75 (let ((session (org-babel-prep-session:js
76 (cdr (assoc :session params)) params)))
77 (nth 1
78 (org-babel-comint-with-output
79 (session (format "%S" org-babel-js-eoe) t body)
80 (mapc
81 (lambda (line)
82 (insert (org-babel-chomp line)) (comint-send-input nil t))
83 (list body (format "%S" org-babel-js-eoe))))))
84 ;; external evaluation
85 (let ((script-file (org-babel-temp-file "js-script-")))
86 (with-temp-file script-file
87 (insert
88 ;; return the value or the output
89 (if (string= result-type "value")
90 (format org-babel-js-function-wrapper full-body)
91 full-body)))
92 (org-babel-eval
93 (format "%s %s" org-babel-js-cmd
94 (org-babel-process-file-name script-file)) ""))))))
95
96 (defun org-babel-js-read (results)
97 "Convert RESULTS into an appropriate elisp value.
98 If RESULTS look like a table, then convert them into an
99 Emacs-lisp table, otherwise return the results as a string."
100 (org-babel-read
101 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
102 (org-babel-read
103 (concat "'"
104 (replace-regexp-in-string
105 "\\[" "(" (replace-regexp-in-string
106 "\\]" ")" (replace-regexp-in-string
107 ", " " " (replace-regexp-in-string
108 "'" "\"" results))))))
109 results)))
110
111 (defun org-babel-js-var-to-js (var)
112 "Convert VAR into a js variable.
113 Convert an elisp value into a string of js source code
114 specifying a variable of the same value."
115 (if (listp var)
116 (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
117 (format "%S" var)))
118
119 (defun org-babel-prep-session:js (session params)
120 "Prepare SESSION according to the header arguments specified in PARAMS."
121 (let* ((session (org-babel-js-initiate-session session))
122 (var-lines (org-babel-variable-assignments:js params)))
123 (when session
124 (org-babel-comint-in-buffer session
125 (sit-for .5) (goto-char (point-max))
126 (mapc (lambda (var)
127 (insert var) (comint-send-input nil t)
128 (org-babel-comint-wait-for-output session)
129 (sit-for .1) (goto-char (point-max))) var-lines)))
130 session))
131
132 (defun org-babel-variable-assignments:js (params)
133 "Return list of Javascript statements assigning the block's variables"
134 (mapcar
135 (lambda (pair) (format "var %s=%s;"
136 (car pair) (org-babel-js-var-to-js (cdr pair))))
137 (mapcar #'cdr (org-babel-get-header params :var))))
138
139 (defun org-babel-js-initiate-session (&optional session)
140 "If there is not a current inferior-process-buffer in SESSION
141 then create. Return the initialized session."
142 (unless (string= session "none")
143 (cond
144 ((string= "mozrepl" org-babel-js-cmd)
145 (require 'moz)
146 (let ((session-buffer (save-window-excursion
147 (run-mozilla nil)
148 (rename-buffer session)
149 (current-buffer))))
150 (if (org-babel-comint-buffer-livep session-buffer)
151 (progn (sit-for .25) session-buffer)
152 (sit-for .5)
153 (org-babel-js-initiate-session session))))
154 ((string= "node" org-babel-js-cmd )
155 (error "session evaluation with node.js is not supported"))
156 (t
157 (error "sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
158
159 (provide 'ob-js)
160
161 ;; arch-tag: 84401fb3-b8d9-4bb6-9a90-cbe2d103d494
162
163 ;;; ob-js.el ends here