ob-js.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. ;;; ob-js.el --- Babel Functions for Javascript -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2018 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, js
  5. ;; Homepage: https://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 <https://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. (declare-function run-mozilla "ext:moz" (arg))
  32. (declare-function httpd-start "simple-httpd" ())
  33. (declare-function run-skewer "skewer-mode" ())
  34. (declare-function indium-run-node "indium-nodejs" (command))
  35. (declare-function indium-eval "indium-interaction" (string &optional callback))
  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 '(choice (const "node")
  45. (const "mozrepl")
  46. (const "skewer-mode")
  47. (const "indium")
  48. (const "js-comint"))
  49. :safe #'stringp)
  50. (defvar org-babel-js-function-wrapper
  51. "require('sys').print(require('sys').inspect(function(){\n%s\n}()));"
  52. "Javascript code to print value of body.")
  53. (defun org-babel-execute:js (body params)
  54. "Execute a block of Javascript code with org-babel.
  55. This function is called by `org-babel-execute-src-block'"
  56. (let* ((org-babel-js-cmd (or (cdr (assq :cmd params)) org-babel-js-cmd))
  57. (session (cdr (assq :session params)))
  58. (result-type (cdr (assq :result-type params)))
  59. (full-body (org-babel-expand-body:generic
  60. body params (org-babel-variable-assignments:js params)))
  61. (result (cond
  62. ;; no session specified, external evaluation
  63. ((string= session "none")
  64. (let ((script-file (org-babel-temp-file "js-script-")))
  65. (with-temp-file script-file
  66. (insert
  67. ;; return the value or the output
  68. (if (string= result-type "value")
  69. (format org-babel-js-function-wrapper full-body)
  70. full-body)))
  71. (org-babel-eval
  72. (format "%s %s" org-babel-js-cmd
  73. (org-babel-process-file-name script-file)) "")))
  74. ;; Indium Node REPL
  75. ;; separate case because Indium REPL is not inherited from comint-mode
  76. ((string= session "*JS REPL*")
  77. (require 'indium-repl)
  78. (unless (get-buffer session)
  79. (indium-run-node))
  80. (indium-eval full-body))
  81. ;; session evaluation
  82. (t
  83. (let ((session (org-babel-prep-session:js
  84. (cdr (assq :session params)) params)))
  85. (nth 1
  86. (org-babel-comint-with-output
  87. (session (format "%S" org-babel-js-eoe) t body)
  88. (dolist (code (list body (format "%S" org-babel-js-eoe)))
  89. (insert (org-babel-chomp code))
  90. (comint-send-input nil t)))))))))
  91. (org-babel-result-cond (cdr (assq :result-params params))
  92. result (org-babel-js-read result))))
  93. (defun org-babel-js-read (results)
  94. "Convert RESULTS into an appropriate elisp value.
  95. If RESULTS look like a table, then convert them into an
  96. Emacs-lisp table, otherwise return the results as a string."
  97. (org-babel-read
  98. (if (and (stringp results)
  99. (string-prefix-p "[" results)
  100. (string-suffix-p "]" results))
  101. (org-babel-read
  102. (concat "'"
  103. (replace-regexp-in-string
  104. "\\[" "(" (replace-regexp-in-string
  105. "\\]" ")" (replace-regexp-in-string
  106. ",[[:space:]]" " "
  107. (replace-regexp-in-string
  108. "'" "\"" results))))))
  109. results)))
  110. (defun org-babel-js-var-to-js (var)
  111. "Convert VAR into a js variable.
  112. Convert an elisp value into a string of js source code
  113. specifying a variable of the same value."
  114. (if (listp var)
  115. (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
  116. (replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
  117. (defun org-babel-prep-session:js (session params)
  118. "Prepare SESSION according to the header arguments specified in PARAMS."
  119. (let* ((session (org-babel-js-initiate-session session))
  120. (var-lines (org-babel-variable-assignments:js params)))
  121. (when session
  122. (org-babel-comint-in-buffer session
  123. (goto-char (point-max))
  124. (dolist (var var-lines)
  125. (insert var)
  126. (comint-send-input nil t)
  127. (org-babel-comint-wait-for-output session)
  128. (sit-for .1)
  129. (goto-char (point-max)))))
  130. session))
  131. (defun org-babel-variable-assignments:js (params)
  132. "Return list of Javascript statements assigning the block's variables."
  133. (mapcar
  134. (lambda (pair) (format "var %s=%s;"
  135. (car pair) (org-babel-js-var-to-js (cdr pair))))
  136. (org-babel--get-vars params)))
  137. (defun org-babel-js-initiate-session (&optional session)
  138. "If there is not a current inferior-process-buffer in SESSION
  139. then create. Return the initialized session."
  140. (cond
  141. ((string= session "none")
  142. (warn "Session evaluation of ob-js is not supported"))
  143. ((string= "*skewer-repl*" session)
  144. (require 'skewer-repl)
  145. (let ((session-buffer (get-buffer "*skewer-repl*")))
  146. (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer))
  147. (comint-check-proc session-buffer))
  148. session-buffer
  149. ;; start skewer REPL.
  150. (httpd-start)
  151. (run-skewer)
  152. session-buffer)))
  153. ((string= "*Javascript REPL*" session)
  154. (require 'js-comint)
  155. (let ((session-buffer "*Javascript REPL*"))
  156. (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer))
  157. (comint-check-proc session-buffer))
  158. session-buffer
  159. (call-interactively 'run-js)
  160. (sit-for .5)
  161. session-buffer)))
  162. ((string= "mozrepl" org-babel-js-cmd)
  163. (require 'moz)
  164. (let ((session-buffer (save-window-excursion
  165. (run-mozilla nil)
  166. (rename-buffer session)
  167. (current-buffer))))
  168. (if (org-babel-comint-buffer-livep session-buffer)
  169. (progn (sit-for .25) session-buffer)
  170. (sit-for .5)
  171. (org-babel-js-initiate-session session))))
  172. ((string= "node" org-babel-js-cmd )
  173. (error "Session evaluation with node.js is not supported"))
  174. (t
  175. (error "Sessions are only supported with mozrepl add \":cmd mozrepl\""))))
  176. (provide 'ob-js)
  177. ;;; ob-js.el ends here