ob-js.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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: 7.5
  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-execute:js (body params)
  53. "Execute a block of Javascript code with org-babel.
  54. This function is called by `org-babel-execute-src-block'"
  55. (let* ((org-babel-js-cmd (or (cdr (assoc :cmd params)) org-babel-js-cmd))
  56. (result-type (cdr (assoc :result-type params)))
  57. (full-body (org-babel-expand-body:generic
  58. body params (org-babel-variable-assignments:js params))))
  59. (org-babel-js-read
  60. (if (not (string= (cdr (assoc :session params)) "none"))
  61. ;; session evaluation
  62. (let ((session (org-babel-prep-session:js
  63. (cdr (assoc :session params)) params)))
  64. (nth 1
  65. (org-babel-comint-with-output
  66. (session (format "%S" org-babel-js-eoe) t body)
  67. (mapc
  68. (lambda (line)
  69. (insert (org-babel-chomp line)) (comint-send-input nil t))
  70. (list body (format "%S" org-babel-js-eoe))))))
  71. ;; external evaluation
  72. (let ((script-file (org-babel-temp-file "js-script-")))
  73. (with-temp-file script-file
  74. (insert
  75. ;; return the value or the output
  76. (if (string= result-type "value")
  77. (format org-babel-js-function-wrapper full-body)
  78. full-body)))
  79. (org-babel-eval
  80. (format "%s %s" org-babel-js-cmd
  81. (org-babel-process-file-name script-file)) ""))))))
  82. (defun org-babel-js-read (results)
  83. "Convert RESULTS into an appropriate elisp value.
  84. If RESULTS look like a table, then convert them into an
  85. Emacs-lisp table, otherwise return the results as a string."
  86. (org-babel-read
  87. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  88. (org-babel-read
  89. (concat "'"
  90. (replace-regexp-in-string
  91. "\\[" "(" (replace-regexp-in-string
  92. "\\]" ")" (replace-regexp-in-string
  93. ", " " " (replace-regexp-in-string
  94. "'" "\"" results))))))
  95. results)))
  96. (defun org-babel-js-var-to-js (var)
  97. "Convert VAR into a js variable.
  98. Convert an elisp value into a string of js source code
  99. specifying a variable of the same value."
  100. (if (listp var)
  101. (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
  102. (format "%S" var)))
  103. (defun org-babel-prep-session:js (session params)
  104. "Prepare SESSION according to the header arguments specified in PARAMS."
  105. (let* ((session (org-babel-js-initiate-session session))
  106. (var-lines (org-babel-variable-assignments:js params)))
  107. (when session
  108. (org-babel-comint-in-buffer session
  109. (sit-for .5) (goto-char (point-max))
  110. (mapc (lambda (var)
  111. (insert var) (comint-send-input nil t)
  112. (org-babel-comint-wait-for-output session)
  113. (sit-for .1) (goto-char (point-max))) var-lines)))
  114. session))
  115. (defun org-babel-variable-assignments:js (params)
  116. "Return list of Javascript statements assigning the block's variables"
  117. (mapcar
  118. (lambda (pair) (format "var %s=%s;"
  119. (car pair) (org-babel-js-var-to-js (cdr pair))))
  120. (mapcar #'cdr (org-babel-get-header params :var))))
  121. (defun org-babel-js-initiate-session (&optional session)
  122. "If there is not a current inferior-process-buffer in SESSION
  123. then create. Return the initialized session."
  124. (unless (string= session "none")
  125. (cond
  126. ((string= "mozrepl" org-babel-js-cmd)
  127. (require 'moz)
  128. (let ((session-buffer (save-window-excursion
  129. (run-mozilla nil)
  130. (rename-buffer session)
  131. (current-buffer))))
  132. (if (org-babel-comint-buffer-livep session-buffer)
  133. (progn (sit-for .25) session-buffer)
  134. (sit-for .5)
  135. (org-babel-js-initiate-session session))))
  136. ((string= "node" org-babel-js-cmd )
  137. (error "session evaluation with node.js is not supported"))
  138. (t
  139. (error "sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
  140. (provide 'ob-js)
  141. ;; arch-tag: 84401fb3-b8d9-4bb6-9a90-cbe2d103d494
  142. ;;; ob-js.el ends here