ob-js.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. :safe #'stringp)
  49. (defvar org-babel-js-function-wrapper
  50. "require('sys').print(require('sys').inspect(function(){\n%s\n}()));"
  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 (assq :cmd params)) org-babel-js-cmd))
  56. (session (cdr (assq :session params)))
  57. (result-type (cdr (assq :result-type params)))
  58. (full-body (org-babel-expand-body:generic
  59. body params (org-babel-variable-assignments:js params)))
  60. (result (cond
  61. ;; no session specified, external evaluation
  62. ((string= session "none")
  63. (let ((script-file (org-babel-temp-file "js-script-")))
  64. (with-temp-file script-file
  65. (insert
  66. ;; return the value or the output
  67. (if (string= result-type "value")
  68. (format org-babel-js-function-wrapper full-body)
  69. full-body)))
  70. (org-babel-eval
  71. (format "%s %s" org-babel-js-cmd
  72. (org-babel-process-file-name script-file)) "")))
  73. ;; Indium Node REPL
  74. ;; separate case because Indium REPL is not inherited from comint-mode
  75. ((string= session "*JS REPL*")
  76. (require 'indium-repl)
  77. (unless (get-buffer session)
  78. (indium-run-node))
  79. (indium-eval full-body))
  80. ;; session evaluation
  81. (t
  82. (let ((session (org-babel-prep-session:js
  83. (cdr (assq :session params)) params)))
  84. (nth 1
  85. (org-babel-comint-with-output
  86. (session (format "%S" org-babel-js-eoe) t body)
  87. (dolist (code (list body (format "%S" org-babel-js-eoe)))
  88. (insert (org-babel-chomp code))
  89. (comint-send-input nil t)))))))))
  90. (org-babel-result-cond (cdr (assq :result-params params))
  91. result (org-babel-js-read result))))
  92. (defun org-babel-js-read (results)
  93. "Convert RESULTS into an appropriate elisp value.
  94. If RESULTS look like a table, then convert them into an
  95. Emacs-lisp table, otherwise return the results as a string."
  96. (org-babel-read
  97. (if (and (stringp results)
  98. (string-prefix-p "[" results)
  99. (string-suffix-p "]" results))
  100. (org-babel-read
  101. (concat "'"
  102. (replace-regexp-in-string
  103. "\\[" "(" (replace-regexp-in-string
  104. "\\]" ")" (replace-regexp-in-string
  105. ",[[:space:]]" " "
  106. (replace-regexp-in-string
  107. "'" "\"" results))))))
  108. results)))
  109. (defun org-babel-js-var-to-js (var)
  110. "Convert VAR into a js variable.
  111. Convert an elisp value into a string of js source code
  112. specifying a variable of the same value."
  113. (if (listp var)
  114. (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
  115. (replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
  116. (defun org-babel-prep-session:js (session params)
  117. "Prepare SESSION according to the header arguments specified in PARAMS."
  118. (let* ((session (org-babel-js-initiate-session session))
  119. (var-lines (org-babel-variable-assignments:js params)))
  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-variable-assignments:js (params)
  129. "Return list of Javascript statements assigning the block's variables."
  130. (mapcar
  131. (lambda (pair) (format "var %s=%s;"
  132. (car pair) (org-babel-js-var-to-js (cdr pair))))
  133. (org-babel--get-vars params)))
  134. (defun org-babel-js-initiate-session (&optional session)
  135. "If there is not a current inferior-process-buffer in SESSION
  136. then create. Return the initialized session."
  137. (cond
  138. ((string= session "none")
  139. (warn "Session evaluation of ob-js is not supported"))
  140. ((string= "*skewer-repl*" session)
  141. (require 'skewer-repl)
  142. (let ((session-buffer (get-buffer "*skewer-repl*")))
  143. (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer))
  144. (comint-check-proc session-buffer))
  145. session-buffer
  146. ;; start skewer REPL.
  147. (httpd-start)
  148. (run-skewer)
  149. session-buffer)))
  150. ((string= "mozrepl" org-babel-js-cmd)
  151. (require 'moz)
  152. (let ((session-buffer (save-window-excursion
  153. (run-mozilla nil)
  154. (rename-buffer session)
  155. (current-buffer))))
  156. (if (org-babel-comint-buffer-livep session-buffer)
  157. (progn (sit-for .25) session-buffer)
  158. (sit-for .5)
  159. (org-babel-js-initiate-session session))))
  160. ((string= "node" org-babel-js-cmd )
  161. (error "Session evaluation with node.js is not supported"))
  162. (t
  163. (error "Sessions are only supported with mozrepl add \":cmd mozrepl\""))))
  164. (provide 'ob-js)
  165. ;;; ob-js.el ends here