;;; ob-js.el --- org-babel functions for Javascript
-;; Copyright (C) 2010 Free Software Foundation
+;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
;; Author: Eric Schulte
;; Keywords: literate programming, reproducible research, js
;; Homepage: http://orgmode.org
-;; Version: 7.7
-;;; License:
+;; This file is part of GNU Emacs.
-;; This program is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
-;;
-;; This program is distributed in the hope that it will be useful,
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
-;;
+
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; - a non-browser javascript engine such as node.js http://nodejs.org/
;; or mozrepl http://wiki.github.com/bard/mozrepl/
-;;
+;;
;; - for session based evaluation mozrepl and moz.el are required see
;; http://wiki.github.com/bard/mozrepl/emacs-integration for
;; configuration instructions
;;; Code:
(require 'ob)
-(require 'ob-ref)
-(require 'ob-comint)
-(require 'ob-eval)
(eval-when-compile (require 'cl))
(declare-function run-mozilla "ext:moz" (arg))
(defcustom org-babel-js-cmd "node"
"Name of command used to evaluate js blocks."
:group 'org-babel
+ :version "24.1"
:type 'string)
(defvar org-babel-js-function-wrapper
(let* ((org-babel-js-cmd (or (cdr (assoc :cmd params)) org-babel-js-cmd))
(result-type (cdr (assoc :result-type params)))
(full-body (org-babel-expand-body:generic
- body params (org-babel-variable-assignments:js params))))
- (org-babel-js-read
- (if (not (string= (cdr (assoc :session params)) "none"))
- ;; session evaluation
- (let ((session (org-babel-prep-session:js
- (cdr (assoc :session params)) params)))
- (nth 1
- (org-babel-comint-with-output
- (session (format "%S" org-babel-js-eoe) t body)
- (mapc
- (lambda (line)
- (insert (org-babel-chomp line)) (comint-send-input nil t))
- (list body (format "%S" org-babel-js-eoe))))))
- ;; external evaluation
- (let ((script-file (org-babel-temp-file "js-script-")))
- (with-temp-file script-file
- (insert
- ;; return the value or the output
- (if (string= result-type "value")
- (format org-babel-js-function-wrapper full-body)
- full-body)))
- (org-babel-eval
- (format "%s %s" org-babel-js-cmd
- (org-babel-process-file-name script-file)) ""))))))
+ body params (org-babel-variable-assignments:js params)))
+ (result (if (not (string= (cdr (assoc :session params)) "none"))
+ ;; session evaluation
+ (let ((session (org-babel-prep-session:js
+ (cdr (assoc :session params)) params)))
+ (nth 1
+ (org-babel-comint-with-output
+ (session (format "%S" org-babel-js-eoe) t body)
+ (mapc
+ (lambda (line)
+ (insert (org-babel-chomp line))
+ (comint-send-input nil t))
+ (list body (format "%S" org-babel-js-eoe))))))
+ ;; external evaluation
+ (let ((script-file (org-babel-temp-file "js-script-")))
+ (with-temp-file script-file
+ (insert
+ ;; return the value or the output
+ (if (string= result-type "value")
+ (format org-babel-js-function-wrapper full-body)
+ full-body)))
+ (org-babel-eval
+ (format "%s %s" org-babel-js-cmd
+ (org-babel-process-file-name script-file)) "")))))
+ (org-babel-result-cond (cdr (assoc :result-params params))
+ result (org-babel-js-read result))))
(defun org-babel-js-read (results)
"Convert RESULTS into an appropriate elisp value.
session))
(defun org-babel-variable-assignments:js (params)
- "Return list of Javascript statements assigning the block's variables"
+ "Return list of Javascript statements assigning the block's variables."
(mapcar
(lambda (pair) (format "var %s=%s;"
(car pair) (org-babel-js-var-to-js (cdr pair))))
(sit-for .5)
(org-babel-js-initiate-session session))))
((string= "node" org-babel-js-cmd )
- (error "session evaluation with node.js is not supported"))
+ (error "Session evaluation with node.js is not supported"))
(t
- (error "sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
+ (error "Sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
(provide 'ob-js)