ob-js.el 6.1 KB

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