ob-js.el 7.3 KB

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