org-protocol default template should be nil
[org-mode.git] / lisp / ob-js.el
bloba59a134d139d73ec9dd5960c61069576c960bd6d
1 ;;; ob-js.el --- org-babel functions for Javascript
3 ;; Copyright (C) 2010 Free Software Foundation
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, js
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Now working with SBCL for both session and external evaluation.
31 ;; This certainly isn't optimally robust, but it seems to be working
32 ;; for the basic use cases.
34 ;;; Requirements:
36 ;; - a non-browser javascript engine such as node.js http://nodejs.org/
37 ;; or mozrepl http://wiki.github.com/bard/mozrepl/
38 ;;
39 ;; - for session based evaluation mozrepl and moz.el are required see
40 ;; http://wiki.github.com/bard/mozrepl/emacs-integration for
41 ;; configuration instructions
43 ;;; Code:
44 (require 'ob)
45 (require 'ob-ref)
46 (require 'ob-comint)
47 (require 'ob-eval)
48 (eval-when-compile (require 'cl))
50 (declare-function run-mozilla "ext:moz" (arg))
52 (defvar org-babel-default-header-args:js '()
53 "Default header arguments for js code blocks.")
55 (defvar org-babel-js-eoe "org-babel-js-eoe"
56 "String to indicate that evaluation has completed.")
58 (defcustom org-babel-js-cmd "node"
59 "Name of command used to evaluate js blocks."
60 :group 'org-babel
61 :type 'string)
63 (defvar org-babel-js-function-wrapper
64 "require('sys').print(require('sys').inspect(function(){%s}()));"
65 "Javascript code to print value of body.")
67 (defun org-babel-expand-body:js (body params &optional processed-params)
68 "Expand BODY according to PARAMS, return the expanded body."
69 (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
70 (concat
71 (mapconcat ;; define any variables
72 (lambda (pair) (format "var %s=%s;"
73 (car pair) (org-babel-js-var-to-js (cdr pair))))
74 vars "\n") "\n" body "\n")))
76 (defun org-babel-execute:js (body params)
77 "Execute a block of Javascript code with org-babel.
78 This function is called by `org-babel-execute-src-block'"
79 (let* ((processed-params (org-babel-process-params params))
80 (org-babel-js-cmd (or (cdr (assoc :cmd params)) org-babel-js-cmd))
81 (result-type (nth 3 processed-params))
82 (full-body (org-babel-expand-body:js body params processed-params)))
83 (org-babel-js-read
84 (if (not (string= (nth 0 processed-params) "none"))
85 ;; session evaluation
86 (let ((session (org-babel-prep-session:js
87 (nth 0 processed-params) params)))
88 (nth 1
89 (org-babel-comint-with-output
90 (session (format "%S" org-babel-js-eoe) t body)
91 (mapc
92 (lambda (line)
93 (insert (org-babel-chomp line)) (comint-send-input nil t))
94 (list body (format "%S" org-babel-js-eoe))))))
95 ;; external evaluation
96 (let ((script-file (org-babel-temp-file "js-script-")))
97 (with-temp-file script-file
98 (insert
99 ;; return the value or the output
100 (if (string= result-type "value")
101 (format org-babel-js-function-wrapper full-body)
102 full-body)))
103 (org-babel-eval (format "%s %s" org-babel-js-cmd script-file) ""))))))
105 (defun org-babel-js-read (results)
106 "Convert RESULTS into an appropriate elisp value.
107 If RESULTS look like a table, then convert them into an
108 Emacs-lisp table, otherwise return the results as a string."
109 (org-babel-read
110 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
111 (org-babel-read
112 (concat "'"
113 (replace-regexp-in-string
114 "\\[" "(" (replace-regexp-in-string
115 "\\]" ")" (replace-regexp-in-string
116 ", " " " (replace-regexp-in-string
117 "'" "\"" results))))))
118 results)))
120 (defun org-babel-js-var-to-js (var)
121 "Convert VAR into a js variable.
122 Convert an elisp value into a string of js source code
123 specifying a variable of the same value."
124 (if (listp var)
125 (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
126 (format "%S" var)))
128 (defun org-babel-prep-session:js (session params)
129 "Prepare SESSION according to the header arguments specified in PARAMS."
130 (let* ((session (org-babel-js-initiate-session session))
131 (vars (org-babel-ref-variables params))
132 (var-lines
133 (mapcar
134 (lambda (pair) (format "var %s=%s;"
135 (car pair) (org-babel-js-var-to-js (cdr pair))))
136 vars)))
137 (when session
138 (org-babel-comint-in-buffer session
139 (sit-for .5) (goto-char (point-max))
140 (mapc (lambda (var)
141 (insert var) (comint-send-input nil t)
142 (org-babel-comint-wait-for-output session)
143 (sit-for .1) (goto-char (point-max))) var-lines)))
144 session))
146 (defun org-babel-js-initiate-session (&optional session)
147 "If there is not a current inferior-process-buffer in SESSION
148 then create. Return the initialized session."
149 (unless (string= session "none")
150 (cond
151 ((string= "mozrepl" org-babel-js-cmd)
152 (require 'moz)
153 (let ((session-buffer (save-window-excursion
154 (run-mozilla nil)
155 (rename-buffer session)
156 (current-buffer))))
157 (if (org-babel-comint-buffer-livep session-buffer)
158 (progn (sit-for .25) session-buffer)
159 (sit-for .5)
160 (org-babel-js-initiate-session session))))
161 ((string= "node" org-babel-js-cmd )
162 (error "session evaluation with node.js is not supported"))
164 (error "sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
166 (provide 'ob-js)
168 ;; arch-tag: 84401fb3-b8d9-4bb6-9a90-cbe2d103d494
170 ;;; ob-js.el ends here