ob-clojure.el 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2019 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland, Eric Schulte, Oleh Krehel, Frederick Giasson
  4. ;;
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Support for evaluating clojure code
  20. ;; Requirements:
  21. ;; - clojure (at least 1.2.0)
  22. ;; - clojure-mode
  23. ;; - either cider or SLIME
  24. ;; For Cider, see https://github.com/clojure-emacs/cider
  25. ;; For SLIME, the best way to install these components is by following
  26. ;; the directions as set out by Phil Hagelberg (Technomancy) on the
  27. ;; web page: http://technomancy.us/126
  28. ;;; Code:
  29. (require 'cl-lib)
  30. (require 'ob)
  31. (require 'org-macs)
  32. (declare-function cider-current-connection "ext:cider-client" (&optional type))
  33. (declare-function cider-current-ns "ext:cider-client" ())
  34. (declare-function nrepl--merge "ext:nrepl-client" (dict1 dict2))
  35. (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
  36. (declare-function nrepl-dict-put "ext:nrepl-client" (dict key value))
  37. (declare-function nrepl-request:eval "ext:nrepl-client" (input callback connection &optional ns line column additional-params tooling))
  38. (declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection &optional ns tooling))
  39. (declare-function slime-eval "ext:slime" (sexp &optional package))
  40. (defvar nrepl-sync-request-timeout)
  41. (defvar cider-buffer-ns)
  42. (defvar org-babel-tangle-lang-exts)
  43. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  44. (defvar org-babel-default-header-args:clojure '())
  45. (defvar org-babel-header-args:clojure '((package . :any)))
  46. (defcustom org-babel-clojure-sync-nrepl-timeout 10
  47. "Timeout value, in seconds, of a Clojure sync call.
  48. If the value is nil, timeout is disabled."
  49. :group 'org-babel
  50. :type 'integer
  51. :version "26.1"
  52. :package-version '(Org . "9.1")
  53. :safe #'wholenump)
  54. (defcustom org-babel-clojure-backend
  55. (cond ((featurep 'cider) 'cider)
  56. (t 'slime))
  57. "Backend used to evaluate Clojure code blocks."
  58. :group 'org-babel
  59. :type '(choice
  60. (const :tag "cider" cider)
  61. (const :tag "SLIME" slime)))
  62. (defcustom org-babel-clojure-default-ns "user"
  63. "Default Clojure namespace for source block when finding ns failed."
  64. :type 'string
  65. :group 'org-babel)
  66. (defun org-babel-clojure-cider-current-ns ()
  67. "Like `cider-current-ns' except `cider-find-ns'."
  68. (or cider-buffer-ns
  69. (let ((repl-buf (cider-current-connection)))
  70. (and repl-buf (buffer-local-value 'cider-buffer-ns repl-buf)))
  71. org-babel-clojure-default-ns))
  72. (defun org-babel-expand-body:clojure (body params)
  73. "Expand BODY according to PARAMS, return the expanded body."
  74. (let* ((vars (org-babel--get-vars params))
  75. (ns (or (cdr (assq :ns params))
  76. (org-babel-clojure-cider-current-ns)))
  77. (result-params (cdr (assq :result-params params)))
  78. (print-level nil)
  79. (print-length nil)
  80. (body
  81. (org-trim
  82. (format "(ns %s)\n%s"
  83. ;; Source block specified namespace :ns.
  84. ns
  85. ;; Variables binding.
  86. (if (null vars) (org-trim body)
  87. (format "(let [%s]\n%s)"
  88. (mapconcat
  89. (lambda (var)
  90. (format "%S (quote %S)" (car var) (cdr var)))
  91. vars
  92. "\n ")
  93. body))))))
  94. (if (or (member "code" result-params)
  95. (member "pp" result-params))
  96. (format "(clojure.pprint/pprint (do %s))" body)
  97. body)))
  98. (defun org-babel-execute:clojure (body params)
  99. "Execute a block of Clojure code with Babel.
  100. The underlying process performed by the code block can be output
  101. using the :show-process parameter."
  102. (let* ((expanded (org-babel-expand-body:clojure body params))
  103. (response (list 'dict))
  104. result)
  105. (cl-case org-babel-clojure-backend
  106. (cider
  107. (require 'cider)
  108. (let ((result-params (cdr (assq :result-params params)))
  109. (show (cdr (assq :show-process params))))
  110. (if (member show '(nil "no"))
  111. ;; Run code without showing the process.
  112. (progn
  113. (setq response
  114. (let ((nrepl-sync-request-timeout
  115. org-babel-clojure-sync-nrepl-timeout))
  116. (nrepl-sync-request:eval expanded
  117. (cider-current-connection))))
  118. (setq result
  119. (concat
  120. (nrepl-dict-get response
  121. (if (or (member "output" result-params)
  122. (member "pp" result-params))
  123. "out"
  124. "value"))
  125. (nrepl-dict-get response "ex")
  126. (nrepl-dict-get response "root-ex")
  127. (nrepl-dict-get response "err"))))
  128. ;; Show the process in an output buffer/window.
  129. (let ((process-buffer (switch-to-buffer-other-window
  130. "*Clojure Show Process Sub Buffer*"))
  131. status)
  132. ;; Run the Clojure code in nREPL.
  133. (nrepl-request:eval
  134. expanded
  135. (lambda (resp)
  136. (when (member "out" resp)
  137. ;; Print the output of the nREPL in the output buffer.
  138. (princ (nrepl-dict-get resp "out") process-buffer))
  139. (when (member "ex" resp)
  140. ;; In case there is an exception, then add it to the
  141. ;; output buffer as well.
  142. (princ (nrepl-dict-get resp "ex") process-buffer)
  143. (princ (nrepl-dict-get resp "root-ex") process-buffer))
  144. (when (member "err" resp)
  145. ;; In case there is an error, then add it to the
  146. ;; output buffer as well.
  147. (princ (nrepl-dict-get resp "err") process-buffer))
  148. (nrepl--merge response resp)
  149. ;; Update the status of the nREPL output session.
  150. (setq status (nrepl-dict-get response "status")))
  151. (cider-current-connection))
  152. ;; Wait until the nREPL code finished to be processed.
  153. (while (not (member "done" status))
  154. (nrepl-dict-put response "status" (remove "need-input" status))
  155. (accept-process-output nil 0.01)
  156. (redisplay))
  157. ;; Delete the show buffer & window when the processing is
  158. ;; finalized.
  159. (mapc #'delete-window
  160. (get-buffer-window-list process-buffer nil t))
  161. (kill-buffer process-buffer)
  162. ;; Put the output or the value in the result section of
  163. ;; the code block.
  164. (setq result
  165. (concat
  166. (nrepl-dict-get response
  167. (if (or (member "output" result-params)
  168. (member "pp" result-params))
  169. "out"
  170. "value"))
  171. (nrepl-dict-get response "ex")
  172. (nrepl-dict-get response "root-ex")
  173. (nrepl-dict-get response "err")))))))
  174. (slime
  175. (require 'slime)
  176. (with-temp-buffer
  177. (insert expanded)
  178. (setq result
  179. (slime-eval
  180. `(swank:eval-and-grab-output
  181. ,(buffer-substring-no-properties (point-min) (point-max)))
  182. (cdr (assq :package params)))))))
  183. (org-babel-result-cond (cdr (assq :result-params params))
  184. result
  185. (condition-case nil (org-babel-script-escape result)
  186. (error result)))))
  187. (provide 'ob-clojure)
  188. ;;; ob-clojure.el ends here