ob-js.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. (defvar org-babel-default-header-args:js '()
  33. "Default header arguments for js code blocks.")
  34. (defvar org-babel-js-eoe "org-babel-js-eoe"
  35. "String to indicate that evaluation has completed.")
  36. (defcustom org-babel-js-cmd "node"
  37. "Name of command used to evaluate js blocks."
  38. :group 'org-babel
  39. :version "24.1"
  40. :type 'string)
  41. (defvar org-babel-js-function-wrapper
  42. "require('sys').print(require('sys').inspect(function(){\n%s\n}()));"
  43. "Javascript code to print value of body.")
  44. (defun org-babel-execute:js (body params)
  45. "Execute a block of Javascript code with org-babel.
  46. This function is called by `org-babel-execute-src-block'"
  47. (let* ((org-babel-js-cmd (or (cdr (assq :cmd params)) org-babel-js-cmd))
  48. (result-type (cdr (assq :result-type params)))
  49. (full-body (org-babel-expand-body:generic
  50. body params (org-babel-variable-assignments:js params)))
  51. (result (if (not (string= (cdr (assq :session params)) "none"))
  52. ;; session evaluation
  53. (let ((session (org-babel-prep-session:js
  54. (cdr (assq :session params)) params)))
  55. (nth 1
  56. (org-babel-comint-with-output
  57. (session (format "%S" org-babel-js-eoe) t body)
  58. (mapc
  59. (lambda (line)
  60. (insert (org-babel-chomp line))
  61. (comint-send-input nil t))
  62. (list body (format "%S" org-babel-js-eoe))))))
  63. ;; external evaluation
  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. (org-babel-result-cond (cdr (assq :result-params params))
  75. result (org-babel-js-read result))))
  76. (defun org-babel-js-read (results)
  77. "Convert RESULTS into an appropriate elisp value.
  78. If RESULTS look like a table, then convert them into an
  79. Emacs-lisp table, otherwise return the results as a string."
  80. (org-babel-read
  81. (if (and (stringp results)
  82. (string-prefix-p "[" results)
  83. (string-suffix-p "]" results))
  84. (org-babel-read
  85. (concat "'"
  86. (replace-regexp-in-string
  87. "\\[" "(" (replace-regexp-in-string
  88. "\\]" ")" (replace-regexp-in-string
  89. ",[[:space:]]" " "
  90. (replace-regexp-in-string
  91. "'" "\"" results))))))
  92. results)))
  93. (defun org-babel-js-var-to-js (var)
  94. "Convert VAR into a js variable.
  95. Convert an elisp value into a string of js source code
  96. specifying a variable of the same value."
  97. (if (listp var)
  98. (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
  99. (replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
  100. (defun org-babel-prep-session:js (session params)
  101. "Prepare SESSION according to the header arguments specified in PARAMS."
  102. (let* ((session (org-babel-js-initiate-session session))
  103. (var-lines (org-babel-variable-assignments:js params)))
  104. (when session
  105. (org-babel-comint-in-buffer session
  106. (sit-for .5) (goto-char (point-max))
  107. (mapc (lambda (var)
  108. (insert var) (comint-send-input nil t)
  109. (org-babel-comint-wait-for-output session)
  110. (sit-for .1) (goto-char (point-max))) var-lines)))
  111. session))
  112. (defun org-babel-variable-assignments:js (params)
  113. "Return list of Javascript statements assigning the block's variables."
  114. (mapcar
  115. (lambda (pair) (format "var %s=%s;"
  116. (car pair) (org-babel-js-var-to-js (cdr pair))))
  117. (org-babel--get-vars params)))
  118. (defun org-babel-js-initiate-session (&optional session)
  119. "If there is not a current inferior-process-buffer in SESSION
  120. then create. Return the initialized session."
  121. (unless (string= session "none")
  122. (cond
  123. ((string= "mozrepl" org-babel-js-cmd)
  124. (require 'moz)
  125. (let ((session-buffer (save-window-excursion
  126. (run-mozilla nil)
  127. (rename-buffer session)
  128. (current-buffer))))
  129. (if (org-babel-comint-buffer-livep session-buffer)
  130. (progn (sit-for .25) session-buffer)
  131. (sit-for .5)
  132. (org-babel-js-initiate-session session))))
  133. ((string= "node" org-babel-js-cmd )
  134. (error "Session evaluation with node.js is not supported"))
  135. (t
  136. (error "Sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
  137. (provide 'ob-js)
  138. ;;; ob-js.el ends here