ob-clojure.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2018 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. (declare-function cider-current-connection "ext:cider-client" (&optional type))
  32. (declare-function cider-current-ns "ext:cider-client" ())
  33. (declare-function nrepl--merge "ext:nrepl-client" (dict1 dict2))
  34. (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
  35. (declare-function nrepl-dict-put "ext:nrepl-client" (dict key value))
  36. (declare-function nrepl-request:eval "ext:nrepl-client"
  37. (input callback connection &optional session ns line column additional-params))
  38. (declare-function nrepl-sync-request:eval "ext:nrepl-client"
  39. (input connection session &optional ns))
  40. (declare-function org-trim "org" (s &optional keep-lead))
  41. (declare-function slime-eval "ext:slime" (sexp &optional package))
  42. (defvar nrepl-sync-request-timeout)
  43. (defvar org-babel-tangle-lang-exts)
  44. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  45. (defvar org-babel-default-header-args:clojure '())
  46. (defvar org-babel-header-args:clojure '((package . :any)))
  47. (defcustom org-babel-clojure-sync-nrepl-timeout 10
  48. "Timeout value, in seconds, of a Clojure sync call.
  49. If the value is nil, timeout is disabled."
  50. :group 'org-babel
  51. :type 'integer
  52. :version "26.1"
  53. :package-version '(Org . "9.1")
  54. :safe #'wholenump)
  55. (defcustom org-babel-clojure-backend
  56. (cond ((featurep 'cider) 'cider)
  57. (t 'slime))
  58. "Backend used to evaluate Clojure code blocks."
  59. :group 'org-babel
  60. :type '(choice
  61. (const :tag "cider" cider)
  62. (const :tag "SLIME" slime)))
  63. (defun org-babel-expand-body:clojure (body params)
  64. "Expand BODY according to PARAMS, return the expanded body."
  65. (let* ((vars (org-babel--get-vars params))
  66. (result-params (cdr (assq :result-params params)))
  67. (print-level nil) (print-length nil)
  68. (body (org-trim
  69. (if (null vars) (org-trim body)
  70. (concat "(let ["
  71. (mapconcat
  72. (lambda (var)
  73. (format "%S (quote %S)" (car var) (cdr var)))
  74. vars "\n ")
  75. "]\n" body ")")))))
  76. (if (or (member "code" result-params)
  77. (member "pp" result-params))
  78. (format "(clojure.pprint/pprint (do %s))" body)
  79. body)))
  80. (defun org-babel-execute:clojure (body params)
  81. "Execute a block of Clojure code with Babel.
  82. The underlying process performed by the code block can be output
  83. using the :show-process parameter."
  84. (let ((expanded (org-babel-expand-body:clojure body params))
  85. (response (list 'dict))
  86. result)
  87. (cl-case org-babel-clojure-backend
  88. (cider
  89. (require 'cider)
  90. (let ((result-params (cdr (assq :result-params params)))
  91. (show (cdr (assq :show-process params))))
  92. (if (member show '(nil "no"))
  93. ;; Run code without showing the process.
  94. (progn
  95. (setq response
  96. (let ((nrepl-sync-request-timeout
  97. org-babel-clojure-sync-nrepl-timeout))
  98. (nrepl-sync-request:eval expanded
  99. (cider-current-connection)
  100. (cider-current-ns))))
  101. (setq result
  102. (concat
  103. (nrepl-dict-get response
  104. (if (or (member "output" result-params)
  105. (member "pp" result-params))
  106. "out"
  107. "value"))
  108. (nrepl-dict-get response "ex")
  109. (nrepl-dict-get response "root-ex")
  110. (nrepl-dict-get response "err"))))
  111. ;; Show the process in an output buffer/window.
  112. (let ((process-buffer (switch-to-buffer-other-window
  113. "*Clojure Show Process Sub Buffer*"))
  114. status)
  115. ;; Run the Clojure code in nREPL.
  116. (nrepl-request:eval
  117. expanded
  118. (lambda (resp)
  119. (when (member "out" resp)
  120. ;; Print the output of the nREPL in the output buffer.
  121. (princ (nrepl-dict-get resp "out") process-buffer))
  122. (when (member "ex" resp)
  123. ;; In case there is an exception, then add it to the
  124. ;; output buffer as well.
  125. (princ (nrepl-dict-get resp "ex") process-buffer)
  126. (princ (nrepl-dict-get resp "root-ex") process-buffer))
  127. (when (member "err" resp)
  128. ;; In case there is an error, then add it to the
  129. ;; output buffer as well.
  130. (princ (nrepl-dict-get resp "err") process-buffer))
  131. (nrepl--merge response resp)
  132. ;; Update the status of the nREPL output session.
  133. (setq status (nrepl-dict-get response "status")))
  134. (cider-current-connection)
  135. (cider-current-ns))
  136. ;; Wait until the nREPL code finished to be processed.
  137. (while (not (member "done" status))
  138. (nrepl-dict-put response "status" (remove "need-input" status))
  139. (accept-process-output nil 0.01)
  140. (redisplay))
  141. ;; Delete the show buffer & window when the processing is
  142. ;; finalized.
  143. (mapc #'delete-window
  144. (get-buffer-window-list process-buffer nil t))
  145. (kill-buffer process-buffer)
  146. ;; Put the output or the value in the result section of
  147. ;; the code block.
  148. (setq result
  149. (concat
  150. (nrepl-dict-get response
  151. (if (or (member "output" result-params)
  152. (member "pp" result-params))
  153. "out"
  154. "value"))
  155. (nrepl-dict-get response "ex")
  156. (nrepl-dict-get response "root-ex")
  157. (nrepl-dict-get response "err")))))))
  158. (slime
  159. (require 'slime)
  160. (with-temp-buffer
  161. (insert expanded)
  162. (setq result
  163. (slime-eval
  164. `(swank:eval-and-grab-output
  165. ,(buffer-substring-no-properties (point-min) (point-max)))
  166. (cdr (assq :package params)))))))
  167. (org-babel-result-cond (cdr (assq :result-params params))
  168. result
  169. (condition-case nil (org-babel-script-escape result)
  170. (error result)))))
  171. (provide 'ob-clojure)
  172. ;;; ob-clojure.el ends here