ob-js.el 7.2 KB

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