ob-clojure.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland, Eric Schulte, Oleh Krehel, Frederick Giasson
  4. ;; Maintainer: Daniel Kraus <daniel@kraus.my>
  5. ;;
  6. ;; Keywords: literate programming, reproducible research
  7. ;; URL: https://orgmode.org
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Support for evaluating clojure code
  21. ;; Requirements:
  22. ;; - clojure (at least 1.2.0)
  23. ;; - clojure-mode
  24. ;; - inf-clojure, cider or SLIME
  25. ;; For clojure-mode, see https://github.com/clojure-emacs/clojure-mode
  26. ;; For cider, see https://github.com/clojure-emacs/cider
  27. ;; For inf-clojure, see https://github.com/clojure-emacs/cider
  28. ;; For babashka, see https://github.com/babashka/babashka
  29. ;; For nbb, see https://github.com/babashka/nbb
  30. ;; For SLIME, the best way to install these components is by following
  31. ;; the directions as set out by Phil Hagelberg (Technomancy) on the
  32. ;; web page: https://technomancy.us/126
  33. ;;; Code:
  34. (require 'org-macs)
  35. (org-assert-version)
  36. (require 'ob)
  37. (declare-function cider-current-connection "ext:cider-client" (&optional type))
  38. (declare-function cider-current-ns "ext:cider-client" ())
  39. (declare-function inf-clojure "ext:inf-clojure" (cmd))
  40. (declare-function inf-clojure-cmd "ext:inf-clojure" (project-type))
  41. (declare-function inf-clojure-eval-string "ext:inf-clojure" (code))
  42. (declare-function inf-clojure-project-type "ext:inf-clojure" ())
  43. (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
  44. (declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection &optional ns tooling))
  45. (declare-function sesman-start-session "ext:sesman" (system))
  46. (declare-function slime-eval "ext:slime" (sexp &optional package))
  47. (defvar cider-buffer-ns)
  48. (defvar org-babel-tangle-lang-exts)
  49. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  50. (add-to-list 'org-babel-tangle-lang-exts '("clojurescript" . "cljs"))
  51. (defvar org-babel-default-header-args:clojure '())
  52. (defvar org-babel-header-args:clojure '((ns . :any) (package . :any)))
  53. (defvar org-babel-default-header-args:clojurescript '())
  54. (defvar org-babel-header-args:clojurescript '((package . :any)))
  55. (defcustom org-babel-clojure-backend nil
  56. "Backend used to evaluate Clojure code blocks."
  57. :group 'org-babel
  58. :type '(choice
  59. (const :tag "inf-clojure" inf-clojure)
  60. (const :tag "cider" cider)
  61. (const :tag "slime" slime)
  62. (const :tag "babashka" babashka)
  63. (const :tag "nbb" nbb)
  64. (const :tag "Not configured yet" nil)))
  65. (defcustom org-babel-clojure-default-ns "user"
  66. "Default Clojure namespace for source block when finding ns failed."
  67. :type 'string
  68. :group 'org-babel)
  69. (defcustom ob-clojure-babashka-command (executable-find "bb")
  70. "Path to the babashka executable."
  71. :type 'file
  72. :group 'org-babel)
  73. (defcustom ob-clojure-nbb-command (executable-find "nbb")
  74. "Path to the nbb executable."
  75. :type 'file
  76. :group 'org-babel)
  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. (if (eq org-babel-clojure-backend 'cider)
  82. (or cider-buffer-ns
  83. (let ((repl-buf (cider-current-connection)))
  84. (and repl-buf (buffer-local-value
  85. 'cider-buffer-ns repl-buf))))
  86. org-babel-clojure-default-ns)))
  87. (result-params (cdr (assq :result-params params)))
  88. (print-level nil)
  89. (print-length nil)
  90. ;; Remove comments, they break (let [...] ...) bindings
  91. (body (replace-regexp-in-string "^[ ]*;+.*$" "" body))
  92. (body (org-trim
  93. (concat
  94. ;; Source block specified namespace :ns.
  95. (and (cdr (assq :ns params)) (format "(ns %s)\n" ns))
  96. ;; Variables binding.
  97. (if (null vars) (org-trim body)
  98. (format "(let [%s]\n%s)"
  99. (mapconcat
  100. (lambda (var)
  101. (format "%S %S" (car var) (cdr var)))
  102. vars
  103. "\n ")
  104. body))))))
  105. (if (or (member "code" result-params)
  106. (member "pp" result-params))
  107. (format "(clojure.pprint/pprint (do %s))" body)
  108. body)))
  109. (defvar ob-clojure-inf-clojure-filter-out)
  110. (defvar ob-clojure-inf-clojure-tmp-output)
  111. (defun ob-clojure-inf-clojure-output (s)
  112. "Store a trimmed version of S in a variable and return S."
  113. (let ((s0 (org-trim
  114. (replace-regexp-in-string
  115. ob-clojure-inf-clojure-filter-out "" s))))
  116. (push s0 ob-clojure-inf-clojure-tmp-output))
  117. s)
  118. (defmacro ob-clojure-with-temp-expanded (expanded params &rest body)
  119. "Run BODY on EXPANDED code block with PARAMS."
  120. (declare (debug (body)) (indent 2))
  121. `(with-temp-buffer
  122. (insert ,expanded)
  123. (goto-char (point-min))
  124. (while (not (looking-at "\\s-*\\'"))
  125. (let* ((beg (point))
  126. (end (progn (forward-sexp) (point)))
  127. (exp (org-babel-expand-body:clojure
  128. (buffer-substring beg end) ,params)))
  129. (sit-for .1)
  130. ,@body))))
  131. (defsubst ob-clojure-string-or-list (l)
  132. "Convert list L into a string or a list of list."
  133. (if (and (listp l) (= (length l) 1))
  134. (car l)
  135. (mapcar #'list l)))
  136. (defvar inf-clojure-buffer)
  137. (defvar comint-prompt-regexp)
  138. (defvar inf-clojure-comint-prompt-regexp)
  139. (defun ob-clojure-eval-with-inf-clojure (expanded params)
  140. "Evaluate EXPANDED code block with PARAMS using inf-clojure."
  141. (condition-case nil (require 'inf-clojure)
  142. (user-error "inf-clojure not available"))
  143. ;; Maybe initiate the inf-clojure session
  144. (unless (and inf-clojure-buffer
  145. (buffer-live-p (get-buffer inf-clojure-buffer)))
  146. (save-window-excursion
  147. (let* ((alias (cdr (assq :alias params)))
  148. (cmd0 (inf-clojure-cmd (inf-clojure-project-type)))
  149. (cmd (if alias (replace-regexp-in-string
  150. "clojure" (format "clojure -A%s" alias)
  151. cmd0)
  152. cmd0)))
  153. (setq comint-prompt-regexp inf-clojure-comint-prompt-regexp)
  154. (funcall-interactively #'inf-clojure cmd)
  155. (goto-char (point-max))))
  156. (sit-for 1))
  157. ;; Now evaluate the code
  158. (setq ob-clojure-inf-clojure-filter-out
  159. (concat "^nil\\|nil$\\|\\s-*"
  160. (or (cdr (assq :ns params))
  161. org-babel-clojure-default-ns)
  162. "=>\\s-*"))
  163. (add-hook 'comint-preoutput-filter-functions
  164. #'ob-clojure-inf-clojure-output)
  165. (setq ob-clojure-inf-clojure-tmp-output nil)
  166. (ob-clojure-with-temp-expanded expanded nil
  167. (inf-clojure-eval-string exp))
  168. (sit-for .5)
  169. (remove-hook 'comint-preoutput-filter-functions
  170. #'ob-clojure-inf-clojure-output)
  171. ;; And return the result
  172. (ob-clojure-string-or-list
  173. (delete nil
  174. (mapcar
  175. (lambda (s)
  176. (unless (or (equal "" s)
  177. (string-match-p "^Clojure" s))
  178. s))
  179. (reverse ob-clojure-inf-clojure-tmp-output)))))
  180. (defun ob-clojure-eval-with-cider (expanded params)
  181. "Evaluate EXPANDED code block with PARAMS using cider."
  182. (condition-case nil (require 'cider)
  183. (user-error "cider not available"))
  184. (let ((connection (cider-current-connection (cdr (assq :target params))))
  185. (result-params (cdr (assq :result-params params)))
  186. result0)
  187. (unless connection (sesman-start-session 'CIDER))
  188. (if (not connection)
  189. ;; Display in the result instead of using `user-error'
  190. (setq result0 "Please reevaluate when nREPL is connected")
  191. (ob-clojure-with-temp-expanded expanded params
  192. (let ((response (nrepl-sync-request:eval exp connection)))
  193. (push (or (nrepl-dict-get response "root-ex")
  194. (nrepl-dict-get response "ex")
  195. (nrepl-dict-get
  196. response (if (or (member "output" result-params)
  197. (member "pp" result-params))
  198. "out"
  199. "value")))
  200. result0)))
  201. (ob-clojure-string-or-list
  202. (reverse (delete "" (mapcar (lambda (r)
  203. (replace-regexp-in-string "nil" "" r))
  204. result0)))))))
  205. (defun ob-clojure-eval-with-slime (expanded params)
  206. "Evaluate EXPANDED code block with PARAMS using slime."
  207. (condition-case nil (require 'slime)
  208. (user-error "slime not available"))
  209. (with-temp-buffer
  210. (insert expanded)
  211. (slime-eval
  212. `(swank:eval-and-grab-output
  213. ,(buffer-substring-no-properties (point-min) (point-max)))
  214. (cdr (assq :package params)))))
  215. (defun ob-clojure-eval-with-babashka (bb expanded)
  216. "Evaluate EXPANDED code block using BB (babashka or nbb)."
  217. (let ((script-file (org-babel-temp-file "clojure-bb-script-" ".clj")))
  218. (with-temp-file script-file
  219. (insert expanded))
  220. (org-babel-eval
  221. (format "%s %s" bb (org-babel-process-file-name script-file))
  222. "")))
  223. (defun org-babel-execute:clojure (body params)
  224. "Execute a block of Clojure code with Babel."
  225. (unless org-babel-clojure-backend
  226. (user-error "You need to customize org-babel-clojure-backend"))
  227. (let* ((expanded (org-babel-expand-body:clojure body params))
  228. (result-params (cdr (assq :result-params params)))
  229. result)
  230. (setq result
  231. (cond
  232. ((eq org-babel-clojure-backend 'inf-clojure)
  233. (ob-clojure-eval-with-inf-clojure expanded params))
  234. ((eq org-babel-clojure-backend 'babashka)
  235. (ob-clojure-eval-with-babashka ob-clojure-babashka-command expanded))
  236. ((eq org-babel-clojure-backend 'nbb)
  237. (ob-clojure-eval-with-babashka ob-clojure-nbb-command expanded))
  238. ((eq org-babel-clojure-backend 'cider)
  239. (ob-clojure-eval-with-cider expanded params))
  240. ((eq org-babel-clojure-backend 'slime)
  241. (ob-clojure-eval-with-slime expanded params))))
  242. (org-babel-result-cond result-params
  243. result
  244. (condition-case nil (org-babel-script-escape result)
  245. (error result)))))
  246. (defun org-babel-execute:clojurescript (body params)
  247. "Evaluate BODY with PARAMS as ClojureScript code."
  248. (org-babel-execute:clojure body (cons '(:target . "cljs") params)))
  249. (provide 'ob-clojure)
  250. ;;; ob-clojure.el ends here