`org-export-htmlize-output-type' docstring patch
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-js.el
blob05f8ea41754d57939111494c65a245ff8f0d3b3b
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: 7.4
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-execute:js (body params)
68 "Execute a block of Javascript code with org-babel.
69 This function is called by `org-babel-execute-src-block'"
70 (let* ((org-babel-js-cmd (or (cdr (assoc :cmd params)) org-babel-js-cmd))
71 (result-type (cdr (assoc :result-type params)))
72 (full-body (org-babel-expand-body:generic
73 body params (org-babel-variable-assignments:js params))))
74 (org-babel-js-read
75 (if (not (string= (cdr (assoc :session params)) "none"))
76 ;; session evaluation
77 (let ((session (org-babel-prep-session:js
78 (cdr (assoc :session params)) params)))
79 (nth 1
80 (org-babel-comint-with-output
81 (session (format "%S" org-babel-js-eoe) t body)
82 (mapc
83 (lambda (line)
84 (insert (org-babel-chomp line)) (comint-send-input nil t))
85 (list body (format "%S" org-babel-js-eoe))))))
86 ;; external evaluation
87 (let ((script-file (org-babel-temp-file "js-script-")))
88 (with-temp-file script-file
89 (insert
90 ;; return the value or the output
91 (if (string= result-type "value")
92 (format org-babel-js-function-wrapper full-body)
93 full-body)))
94 (org-babel-eval
95 (format "%s %s" org-babel-js-cmd
96 (org-babel-process-file-name script-file)) ""))))))
98 (defun org-babel-js-read (results)
99 "Convert RESULTS into an appropriate elisp value.
100 If RESULTS look like a table, then convert them into an
101 Emacs-lisp table, otherwise return the results as a string."
102 (org-babel-read
103 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
104 (org-babel-read
105 (concat "'"
106 (replace-regexp-in-string
107 "\\[" "(" (replace-regexp-in-string
108 "\\]" ")" (replace-regexp-in-string
109 ", " " " (replace-regexp-in-string
110 "'" "\"" results))))))
111 results)))
113 (defun org-babel-js-var-to-js (var)
114 "Convert VAR into a js variable.
115 Convert an elisp value into a string of js source code
116 specifying a variable of the same value."
117 (if (listp var)
118 (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
119 (format "%S" var)))
121 (defun org-babel-prep-session:js (session params)
122 "Prepare SESSION according to the header arguments specified in PARAMS."
123 (let* ((session (org-babel-js-initiate-session session))
124 (var-lines (org-babel-variable-assignments:js params)))
125 (when session
126 (org-babel-comint-in-buffer session
127 (sit-for .5) (goto-char (point-max))
128 (mapc (lambda (var)
129 (insert var) (comint-send-input nil t)
130 (org-babel-comint-wait-for-output session)
131 (sit-for .1) (goto-char (point-max))) var-lines)))
132 session))
134 (defun org-babel-variable-assignments:js (params)
135 "Return list of Javascript statements assigning the block's variables"
136 (mapcar
137 (lambda (pair) (format "var %s=%s;"
138 (car pair) (org-babel-js-var-to-js (cdr pair))))
139 (mapcar #'cdr (org-babel-get-header params :var))))
141 (defun org-babel-js-initiate-session (&optional session)
142 "If there is not a current inferior-process-buffer in SESSION
143 then create. Return the initialized session."
144 (unless (string= session "none")
145 (cond
146 ((string= "mozrepl" org-babel-js-cmd)
147 (require 'moz)
148 (let ((session-buffer (save-window-excursion
149 (run-mozilla nil)
150 (rename-buffer session)
151 (current-buffer))))
152 (if (org-babel-comint-buffer-livep session-buffer)
153 (progn (sit-for .25) session-buffer)
154 (sit-for .5)
155 (org-babel-js-initiate-session session))))
156 ((string= "node" org-babel-js-cmd )
157 (error "session evaluation with node.js is not supported"))
159 (error "sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
161 (provide 'ob-js)
163 ;; arch-tag: 84401fb3-b8d9-4bb6-9a90-cbe2d103d494
165 ;;; ob-js.el ends here