ob-js.el 5.7 KB

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