ob-js.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ;;; ob-js.el --- org-babel functions for Javascript
  2. ;; Copyright (C) 2010-2013 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. (eval-when-compile (require 'cl))
  32. (declare-function run-mozilla "ext:moz" (arg))
  33. (defvar org-babel-default-header-args:js '()
  34. "Default header arguments for js code blocks.")
  35. (defvar org-babel-js-eoe "org-babel-js-eoe"
  36. "String to indicate that evaluation has completed.")
  37. (defcustom org-babel-js-cmd "node"
  38. "Name of command used to evaluate js blocks."
  39. :group 'org-babel
  40. :version "24.1"
  41. :type 'string)
  42. (defvar org-babel-js-function-wrapper
  43. "require('sys').print(require('sys').inspect(function(){%s}()));"
  44. "Javascript code to print value of body.")
  45. (defun org-babel-execute:js (body params)
  46. "Execute a block of Javascript code with org-babel.
  47. This function is called by `org-babel-execute-src-block'"
  48. (let* ((org-babel-js-cmd (or (cdr (assoc :cmd params)) org-babel-js-cmd))
  49. (result-type (cdr (assoc :result-type params)))
  50. (full-body (org-babel-expand-body:generic
  51. body params (org-babel-variable-assignments:js params)))
  52. (result (if (not (string= (cdr (assoc :session params)) "none"))
  53. ;; session evaluation
  54. (let ((session (org-babel-prep-session:js
  55. (cdr (assoc :session params)) params)))
  56. (nth 1
  57. (org-babel-comint-with-output
  58. (session (format "%S" org-babel-js-eoe) t body)
  59. (mapc
  60. (lambda (line)
  61. (insert (org-babel-chomp line))
  62. (comint-send-input nil t))
  63. (list body (format "%S" org-babel-js-eoe))))))
  64. ;; external evaluation
  65. (let ((script-file (org-babel-temp-file "js-script-")))
  66. (with-temp-file script-file
  67. (insert
  68. ;; return the value or the output
  69. (if (string= result-type "value")
  70. (format org-babel-js-function-wrapper full-body)
  71. full-body)))
  72. (org-babel-eval
  73. (format "%s %s" org-babel-js-cmd
  74. (org-babel-process-file-name script-file)) "")))))
  75. (org-babel-result-cond (cdr (assoc :result-params params))
  76. result (org-babel-js-read result))))
  77. (defun org-babel-js-read (results)
  78. "Convert RESULTS into an appropriate elisp value.
  79. If RESULTS look like a table, then convert them into an
  80. Emacs-lisp table, otherwise return the results as a string."
  81. (org-babel-read
  82. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  83. (org-babel-read
  84. (concat "'"
  85. (replace-regexp-in-string
  86. "\\[" "(" (replace-regexp-in-string
  87. "\\]" ")" (replace-regexp-in-string
  88. ", " " " (replace-regexp-in-string
  89. "'" "\"" results))))))
  90. results)))
  91. (defun org-babel-js-var-to-js (var)
  92. "Convert VAR into a js variable.
  93. Convert an elisp value into a string of js source code
  94. specifying a variable of the same value."
  95. (if (listp var)
  96. (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
  97. (format "%S" var)))
  98. (defun org-babel-prep-session:js (session params)
  99. "Prepare SESSION according to the header arguments specified in PARAMS."
  100. (let* ((session (org-babel-js-initiate-session session))
  101. (var-lines (org-babel-variable-assignments:js params)))
  102. (when session
  103. (org-babel-comint-in-buffer session
  104. (sit-for .5) (goto-char (point-max))
  105. (mapc (lambda (var)
  106. (insert var) (comint-send-input nil t)
  107. (org-babel-comint-wait-for-output session)
  108. (sit-for .1) (goto-char (point-max))) var-lines)))
  109. session))
  110. (defun org-babel-variable-assignments:js (params)
  111. "Return list of Javascript statements assigning the block's variables."
  112. (mapcar
  113. (lambda (pair) (format "var %s=%s;"
  114. (car pair) (org-babel-js-var-to-js (cdr pair))))
  115. (mapcar #'cdr (org-babel-get-header params :var))))
  116. (defun org-babel-js-initiate-session (&optional session)
  117. "If there is not a current inferior-process-buffer in SESSION
  118. then create. Return the initialized session."
  119. (unless (string= session "none")
  120. (cond
  121. ((string= "mozrepl" org-babel-js-cmd)
  122. (require 'moz)
  123. (let ((session-buffer (save-window-excursion
  124. (run-mozilla nil)
  125. (rename-buffer session)
  126. (current-buffer))))
  127. (if (org-babel-comint-buffer-livep session-buffer)
  128. (progn (sit-for .25) session-buffer)
  129. (sit-for .5)
  130. (org-babel-js-initiate-session session))))
  131. ((string= "node" org-babel-js-cmd )
  132. (error "Session evaluation with node.js is not supported"))
  133. (t
  134. (error "Sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
  135. (provide 'ob-js)
  136. ;;; ob-js.el ends here