ob-clojure.el 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-jack-in "ext:cider" (&optional prompt-project cljs-too))
  33. (declare-function cider-current-connection "ext:cider-client" (&optional type))
  34. (declare-function cider-current-ns "ext:cider-client" ())
  35. (declare-function cider-repls "ext:cider-connection" (&optional type ensure))
  36. (declare-function nrepl--merge "ext:nrepl-client" (dict1 dict2))
  37. (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
  38. (declare-function nrepl-dict-put "ext:nrepl-client" (dict key value))
  39. (declare-function nrepl-request:eval "ext:nrepl-client" (input callback connection &optional ns line column additional-params tooling))
  40. (declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection &optional ns tooling))
  41. (declare-function slime-eval "ext:slime" (sexp &optional package))
  42. (defvar nrepl-sync-request-timeout)
  43. (defvar cider-buffer-ns)
  44. (defvar sesman-system)
  45. (defvar cider-version)
  46. (defvar org-babel-tangle-lang-exts)
  47. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  48. (defvar org-babel-default-header-args:clojure '())
  49. (defvar org-babel-header-args:clojure '((ns . :any)
  50. (package . :any)))
  51. (defcustom org-babel-clojure-sync-nrepl-timeout 10
  52. "Timeout value, in seconds, of a Clojure sync call.
  53. If the value is nil, timeout is disabled."
  54. :group 'org-babel
  55. :type 'integer
  56. :version "26.1"
  57. :package-version '(Org . "9.1")
  58. :safe #'wholenump)
  59. (defcustom org-babel-clojure-backend
  60. (cond ((featurep 'cider) 'cider)
  61. (t 'slime))
  62. "Backend used to evaluate Clojure code blocks."
  63. :group 'org-babel
  64. :type '(choice
  65. (const :tag "cider" cider)
  66. (const :tag "SLIME" slime)))
  67. (defcustom org-babel-clojure-default-ns "user"
  68. "Default Clojure namespace for source block when finding ns failed."
  69. :type 'string
  70. :group 'org-babel)
  71. (defun org-babel-clojure-cider-current-ns ()
  72. "Like `cider-current-ns' except `cider-find-ns'."
  73. (or cider-buffer-ns
  74. (let ((repl-buf (cider-current-connection)))
  75. (and repl-buf (buffer-local-value 'cider-buffer-ns repl-buf)))
  76. org-babel-clojure-default-ns))
  77. (defun org-babel-expand-body:clojure (body params)
  78. "Expand BODY according to PARAMS, return the expanded body."
  79. (let* ((vars (org-babel--get-vars params))
  80. (ns (or (cdr (assq :ns params))
  81. (org-babel-clojure-cider-current-ns)))
  82. (result-params (cdr (assq :result-params params)))
  83. (print-level nil)
  84. (print-length nil)
  85. (body (org-trim
  86. (concat
  87. ;; Source block specified namespace :ns.
  88. (and (cdr (assq :ns params)) (format "(ns %s)\n" ns))
  89. ;; Variables binding.
  90. (if (null vars) (org-trim body)
  91. (format "(let [%s]\n%s)"
  92. (mapconcat
  93. (lambda (var)
  94. (format "%S (quote %S)" (car var) (cdr var)))
  95. vars
  96. "\n ")
  97. body))))))
  98. (if (or (member "code" result-params)
  99. (member "pp" result-params))
  100. (format "(clojure.pprint/pprint (do %s))" body)
  101. body)))
  102. (defun org-babel-execute:clojure (body params)
  103. "Execute a block of Clojure code with Babel.
  104. The underlying process performed by the code block can be output
  105. using the :show-process parameter."
  106. (let* ((expanded (org-babel-expand-body:clojure body params))
  107. (response (list 'dict))
  108. result)
  109. (cl-case org-babel-clojure-backend
  110. (cider
  111. (require 'cider)
  112. (let ((result-params (cdr (assq :result-params params)))
  113. (show (cdr (assq :show-process params))))
  114. (if (member show '(nil "no"))
  115. ;; Run code without showing the process.
  116. (progn
  117. (setq response
  118. (let ((nrepl-sync-request-timeout
  119. org-babel-clojure-sync-nrepl-timeout))
  120. (nrepl-sync-request:eval expanded
  121. (cider-current-connection))))
  122. (setq result
  123. (concat
  124. (nrepl-dict-get response
  125. (if (or (member "output" result-params)
  126. (member "pp" result-params))
  127. "out"
  128. "value"))
  129. (nrepl-dict-get response "ex")
  130. (nrepl-dict-get response "root-ex")
  131. (nrepl-dict-get response "err"))))
  132. ;; Show the process in an output buffer/window.
  133. (let ((process-buffer (switch-to-buffer-other-window
  134. "*Clojure Show Process Sub Buffer*"))
  135. status)
  136. ;; Run the Clojure code in nREPL.
  137. (nrepl-request:eval
  138. expanded
  139. (lambda (resp)
  140. (when (member "out" resp)
  141. ;; Print the output of the nREPL in the output buffer.
  142. (princ (nrepl-dict-get resp "out") process-buffer))
  143. (when (member "ex" resp)
  144. ;; In case there is an exception, then add it to the
  145. ;; output buffer as well.
  146. (princ (nrepl-dict-get resp "ex") process-buffer)
  147. (princ (nrepl-dict-get resp "root-ex") process-buffer))
  148. (when (member "err" resp)
  149. ;; In case there is an error, then add it to the
  150. ;; output buffer as well.
  151. (princ (nrepl-dict-get resp "err") process-buffer))
  152. (nrepl--merge response resp)
  153. ;; Update the status of the nREPL output session.
  154. (setq status (nrepl-dict-get response "status")))
  155. (cider-current-connection))
  156. ;; Wait until the nREPL code finished to be processed.
  157. (while (not (member "done" status))
  158. (nrepl-dict-put response "status" (remove "need-input" status))
  159. (accept-process-output nil 0.01)
  160. (redisplay))
  161. ;; Delete the show buffer & window when the processing is
  162. ;; finalized.
  163. (mapc #'delete-window
  164. (get-buffer-window-list process-buffer nil t))
  165. (kill-buffer process-buffer)
  166. ;; Put the output or the value in the result section of
  167. ;; the code block.
  168. (setq result
  169. (concat
  170. (nrepl-dict-get response
  171. (if (or (member "output" result-params)
  172. (member "pp" result-params))
  173. "out"
  174. "value"))
  175. (nrepl-dict-get response "ex")
  176. (nrepl-dict-get response "root-ex")
  177. (nrepl-dict-get response "err")))))))
  178. (slime
  179. (require 'slime)
  180. (with-temp-buffer
  181. (insert expanded)
  182. (setq result
  183. (slime-eval
  184. `(swank:eval-and-grab-output
  185. ,(buffer-substring-no-properties (point-min) (point-max)))
  186. (cdr (assq :package params)))))))
  187. (org-babel-result-cond (cdr (assq :result-params params))
  188. result
  189. (condition-case nil (org-babel-script-escape result)
  190. (error result)))))
  191. (defun org-babel-clojure-initiate-session (&optional session _params)
  192. "Initiate a session named SESSION according to PARAMS."
  193. (when (and session (not (string= session "none")))
  194. (save-window-excursion
  195. (cond
  196. ((org-babel-comint-buffer-livep session) nil)
  197. ;; CIDER jack-in to the Clojure project directory.
  198. ((eq org-babel-clojure-backend 'cider)
  199. (require 'cider)
  200. (let ((session-buffer
  201. (save-window-excursion
  202. (if (version< cider-version "0.18.0")
  203. ;; Older CIDER (without sesman) still need to use
  204. ;; old way.
  205. (cider-jack-in nil) ;jack-in without project
  206. ;; New CIDER (with sesman to manage sessions).
  207. (unless (cider-repls)
  208. (let ((sesman-system 'CIDER))
  209. (call-interactively 'sesman-link-with-directory))))
  210. (current-buffer))))
  211. (when (org-babel-comint-buffer-livep session-buffer)
  212. (sit-for .25)
  213. session-buffer)))
  214. ((eq org-babel-clojure-backend 'slime)
  215. (error "Session evaluation with SLIME is not supported"))
  216. (t
  217. (error "Session initiate failed")))
  218. (get-buffer session))))
  219. (defun org-babel-prep-session:clojure (session params)
  220. "Prepare SESSION according to the header arguments specified in PARAMS."
  221. (let ((session (org-babel-clojure-initiate-session session))
  222. (var-lines (org-babel-variable-assignments:clojure params)))
  223. (when session
  224. (org-babel-comint-in-buffer session
  225. (dolist (var var-lines)
  226. (insert var)
  227. (comint-send-input nil t)
  228. (org-babel-comint-wait-for-output session)
  229. (sit-for .1)
  230. (goto-char (point-max)))))
  231. session))
  232. (defun org-babel-clojure-var-to-clojure (var)
  233. "Convert src block's VAR to Clojure variable."
  234. (cond
  235. ((listp var)
  236. (replace-regexp-in-string "(" "'(" var))
  237. ((stringp var)
  238. ;; Wrap Babel passed-in header argument value with quotes in Clojure.
  239. (format "\"%s\"" var))
  240. (t
  241. (format "%S" var))))
  242. (defun org-babel-variable-assignments:clojure (params)
  243. "Return a list of Clojure statements assigning the block's variables in PARAMS."
  244. (mapcar
  245. (lambda (pair)
  246. (format "(def %s %s)"
  247. (car pair)
  248. (org-babel-clojure-var-to-clojure (cdr pair))))
  249. (org-babel--get-vars params)))
  250. (provide 'ob-clojure)
  251. ;;; ob-clojure.el ends here