ob-js.el 5.8 KB

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