ob-clojure.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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
  53. '((ns . :any)
  54. (package . :any)
  55. (backend . ((inf-clojure cider slime babashka nbb)))))
  56. (defvar org-babel-default-header-args:clojurescript '())
  57. (defvar org-babel-header-args:clojurescript '((package . :any)))
  58. (defcustom org-babel-clojure-backend nil
  59. "Backend used to evaluate Clojure code blocks."
  60. :group 'org-babel
  61. :type '(choice
  62. (const :tag "inf-clojure" inf-clojure)
  63. (const :tag "cider" cider)
  64. (const :tag "slime" slime)
  65. (const :tag "babashka" babashka)
  66. (const :tag "nbb" nbb)
  67. (const :tag "Not configured yet" nil)))
  68. (defcustom org-babel-clojure-default-ns "user"
  69. "Default Clojure namespace for source block when finding ns failed."
  70. :type 'string
  71. :group 'org-babel)
  72. (defcustom ob-clojure-babashka-command (executable-find "bb")
  73. "Path to the babashka executable."
  74. :type 'file
  75. :group 'org-babel)
  76. (defcustom ob-clojure-nbb-command (executable-find "nbb")
  77. "Path to the nbb executable."
  78. :type 'file
  79. :group 'org-babel)
  80. (defun org-babel-expand-body:clojure (body params)
  81. "Expand BODY according to PARAMS, return the expanded body."
  82. (let* ((vars (org-babel--get-vars params))
  83. (backend-override (cdr (assq :backend params)))
  84. (org-babel-clojure-backend
  85. (cond
  86. (backend-override (intern backend-override))
  87. (org-babel-clojure-backend org-babel-clojure-backend)
  88. (t (user-error "You need to customize `org-babel-clojure-backend'
  89. or set the `:backend' header argument"))))
  90. (ns (or (cdr (assq :ns params))
  91. (if (eq org-babel-clojure-backend 'cider)
  92. (or cider-buffer-ns
  93. (let ((repl-buf (cider-current-connection)))
  94. (and repl-buf (buffer-local-value
  95. 'cider-buffer-ns repl-buf))))
  96. org-babel-clojure-default-ns)))
  97. (result-params (cdr (assq :result-params params)))
  98. (print-level nil)
  99. (print-length nil)
  100. ;; Remove comments, they break (let [...] ...) bindings
  101. (body (replace-regexp-in-string "^[ ]*;+.*$" "" body))
  102. (body (org-trim
  103. (concat
  104. ;; Source block specified namespace :ns.
  105. (and (cdr (assq :ns params)) (format "(ns %s)\n" ns))
  106. ;; Variables binding.
  107. (if (null vars) (org-trim body)
  108. (format "(let [%s]\n%s)"
  109. (mapconcat
  110. (lambda (var)
  111. (format "%S %S" (car var) (cdr var)))
  112. vars
  113. "\n ")
  114. body))))))
  115. (if (or (member "code" result-params)
  116. (member "pp" result-params))
  117. (format "(clojure.pprint/pprint (do %s))" body)
  118. body)))
  119. (defvar ob-clojure-inf-clojure-filter-out)
  120. (defvar ob-clojure-inf-clojure-tmp-output)
  121. (defun ob-clojure-inf-clojure-output (s)
  122. "Store a trimmed version of S in a variable and return S."
  123. (let ((s0 (org-trim
  124. (replace-regexp-in-string
  125. ob-clojure-inf-clojure-filter-out "" s))))
  126. (push s0 ob-clojure-inf-clojure-tmp-output))
  127. s)
  128. (defmacro ob-clojure-with-temp-expanded (expanded params &rest body)
  129. "Run BODY on EXPANDED code block with PARAMS."
  130. (declare (debug (body)) (indent 2))
  131. `(with-temp-buffer
  132. (insert ,expanded)
  133. (goto-char (point-min))
  134. (while (not (looking-at "\\s-*\\'"))
  135. (let* ((beg (point))
  136. (end (progn (forward-sexp) (point)))
  137. (exp (org-babel-expand-body:clojure
  138. (buffer-substring beg end) ,params)))
  139. (sit-for .1)
  140. ,@body))))
  141. (defsubst ob-clojure-string-or-list (l)
  142. "Convert list L into a string or a list of list."
  143. (if (and (listp l) (= (length l) 1))
  144. (car l)
  145. (mapcar #'list l)))
  146. (defvar inf-clojure-buffer)
  147. (defvar comint-prompt-regexp)
  148. (defvar inf-clojure-comint-prompt-regexp)
  149. (defun ob-clojure-eval-with-inf-clojure (expanded params)
  150. "Evaluate EXPANDED code block with PARAMS using inf-clojure."
  151. (condition-case nil (require 'inf-clojure)
  152. (user-error "inf-clojure not available"))
  153. ;; Maybe initiate the inf-clojure session
  154. (unless (and inf-clojure-buffer
  155. (buffer-live-p (get-buffer inf-clojure-buffer)))
  156. (save-window-excursion
  157. (let* ((alias (cdr (assq :alias params)))
  158. (cmd0 (inf-clojure-cmd (inf-clojure-project-type)))
  159. (cmd (if alias (replace-regexp-in-string
  160. "clojure" (format "clojure -A%s" alias)
  161. cmd0)
  162. cmd0)))
  163. (setq comint-prompt-regexp inf-clojure-comint-prompt-regexp)
  164. (funcall-interactively #'inf-clojure cmd)
  165. (goto-char (point-max))))
  166. (sit-for 1))
  167. ;; Now evaluate the code
  168. (setq ob-clojure-inf-clojure-filter-out
  169. (concat "^nil\\|nil$\\|\\s-*"
  170. (or (cdr (assq :ns params))
  171. org-babel-clojure-default-ns)
  172. "=>\\s-*"))
  173. (add-hook 'comint-preoutput-filter-functions
  174. #'ob-clojure-inf-clojure-output)
  175. (setq ob-clojure-inf-clojure-tmp-output nil)
  176. (ob-clojure-with-temp-expanded expanded nil
  177. (inf-clojure-eval-string exp))
  178. (sit-for .5)
  179. (remove-hook 'comint-preoutput-filter-functions
  180. #'ob-clojure-inf-clojure-output)
  181. ;; And return the result
  182. (ob-clojure-string-or-list
  183. (delete nil
  184. (mapcar
  185. (lambda (s)
  186. (unless (or (equal "" s)
  187. (string-match-p "^Clojure" s))
  188. s))
  189. (reverse ob-clojure-inf-clojure-tmp-output)))))
  190. (defun ob-clojure-eval-with-cider (expanded params)
  191. "Evaluate EXPANDED code block with PARAMS using cider."
  192. (condition-case nil (require 'cider)
  193. (user-error "cider not available"))
  194. (let ((connection (cider-current-connection (cdr (assq :target params))))
  195. (result-params (cdr (assq :result-params params)))
  196. result0)
  197. (unless connection (sesman-start-session 'CIDER))
  198. (if (not connection)
  199. ;; Display in the result instead of using `user-error'
  200. (setq result0 "Please reevaluate when nREPL is connected")
  201. (ob-clojure-with-temp-expanded expanded params
  202. (let ((response (nrepl-sync-request:eval exp connection)))
  203. (push (or (nrepl-dict-get response "root-ex")
  204. (nrepl-dict-get response "ex")
  205. (nrepl-dict-get
  206. response (if (or (member "output" result-params)
  207. (member "pp" result-params))
  208. "out"
  209. "value")))
  210. result0)))
  211. (ob-clojure-string-or-list
  212. (reverse (delete "" (mapcar (lambda (r)
  213. (replace-regexp-in-string "nil" "" r))
  214. result0)))))))
  215. (defun ob-clojure-eval-with-slime (expanded params)
  216. "Evaluate EXPANDED code block with PARAMS using slime."
  217. (condition-case nil (require 'slime)
  218. (user-error "slime not available"))
  219. (with-temp-buffer
  220. (insert expanded)
  221. (slime-eval
  222. `(swank:eval-and-grab-output
  223. ,(buffer-substring-no-properties (point-min) (point-max)))
  224. (cdr (assq :package params)))))
  225. (defun ob-clojure-eval-with-babashka (bb expanded)
  226. "Evaluate EXPANDED code block using BB (babashka or nbb)."
  227. (let ((script-file (org-babel-temp-file "clojure-bb-script-" ".clj")))
  228. (with-temp-file script-file
  229. (insert expanded))
  230. (org-babel-eval
  231. (format "%s %s" bb (org-babel-process-file-name script-file))
  232. "")))
  233. (defun org-babel-execute:clojure (body params)
  234. "Execute the BODY block of Clojure code with PARAMS using Babel."
  235. (let* ((backend-override (cdr (assq :backend params)))
  236. (org-babel-clojure-backend
  237. (cond
  238. (backend-override (intern backend-override))
  239. (org-babel-clojure-backend org-babel-clojure-backend)
  240. (t (user-error "You need to customize `org-babel-clojure-backend'
  241. or set the `:backend' header argument")))))
  242. (let* ((expanded (org-babel-expand-body:clojure body params))
  243. (result-params (cdr (assq :result-params params)))
  244. result)
  245. (setq result
  246. (cond
  247. ((eq org-babel-clojure-backend 'inf-clojure)
  248. (ob-clojure-eval-with-inf-clojure expanded params))
  249. ((eq org-babel-clojure-backend 'babashka)
  250. (ob-clojure-eval-with-babashka ob-clojure-babashka-command expanded))
  251. ((eq org-babel-clojure-backend 'nbb)
  252. (ob-clojure-eval-with-babashka ob-clojure-nbb-command expanded))
  253. ((eq org-babel-clojure-backend 'cider)
  254. (ob-clojure-eval-with-cider expanded params))
  255. ((eq org-babel-clojure-backend 'slime)
  256. (ob-clojure-eval-with-slime expanded params))))
  257. (org-babel-result-cond result-params
  258. result
  259. (condition-case nil (org-babel-script-escape result)
  260. (error result))))))
  261. (defun org-babel-execute:clojurescript (body params)
  262. "Evaluate BODY with PARAMS as ClojureScript code."
  263. (org-babel-execute:clojure body (cons '(:target . "cljs") params)))
  264. (provide 'ob-clojure)
  265. ;;; ob-clojure.el ends here