ob-clojure.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ;;; ob-clojure.el --- org-babel functions for clojure evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  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 <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; ob support for evaluating clojure code
  20. ;;; Requirements:
  21. ;;; A working clojure install. This also implies a working java executable
  22. ;;; clojure-mode
  23. ;;; slime
  24. ;;; swank-clojure
  25. ;;; By far, 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 'ob)
  30. (require 'cl)
  31. (require 'slime)
  32. (require 'swank-clojure)
  33. (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
  34. (defvar org-babel-clojure-wrapper-method
  35. "
  36. (defn spit
  37. [f content]
  38. (with-open [#^java.io.PrintWriter w
  39. (java.io.PrintWriter.
  40. (java.io.BufferedWriter.
  41. (java.io.OutputStreamWriter.
  42. (java.io.FileOutputStream.
  43. (java.io.File. f)))))]
  44. (.print w content)))
  45. (defn main
  46. []
  47. %s)
  48. (spit \"%s\" (str (main)))") ;;" <-- syntax highlighting is messed without this double quote
  49. ;;taken mostly from clojure-test-mode.el
  50. (defun org-babel-clojure-clojure-slime-eval (string &optional handler)
  51. "Evaluate a STRING of clojure code using `slime-eval-async'."
  52. (slime-eval-async `(swank:eval-and-grab-output ,string)
  53. (or handler #'identity)))
  54. (defun org-babel-clojure-slime-eval-sync (string)
  55. "Evaluate a STRING of clojure code using `slime-eval'."
  56. (slime-eval `(swank:eval-and-grab-output ,string)))
  57. ;;taken from swank-clojure.el
  58. (defun org-babel-clojure-babel-clojure-cmd ()
  59. "Create the command to start clojure according to current settings."
  60. (if (and (not swank-clojure-binary) (not swank-clojure-classpath))
  61. (error "You must specifiy either a `swank-clojure-binary' or a `swank-clojure-jar-path'")
  62. (if swank-clojure-binary
  63. (if (listp swank-clojure-binary)
  64. swank-clojure-binary
  65. (list swank-clojure-binary))
  66. (delete-if
  67. 'null
  68. (append
  69. (list swank-clojure-java-path)
  70. swank-clojure-extra-vm-args
  71. (list
  72. (when swank-clojure-library-paths
  73. (concat "-Djava.library.path="
  74. (swank-clojure-concat-paths swank-clojure-library-paths)))
  75. "-classpath"
  76. (swank-clojure-concat-paths
  77. (append
  78. swank-clojure-classpath
  79. swank-clojure-extra-classpaths))
  80. "clojure.main"))))))
  81. (defun org-babel-clojure-table-or-string (results)
  82. "If RESULTS looks like a table, then convert them into an
  83. Emacs-lisp table, otherwise return the results as a string."
  84. (org-babel-read
  85. (if (string-match "^\\[.+\\]$" results)
  86. (org-babel-read
  87. (concat "'"
  88. (replace-regexp-in-string
  89. "\\[" "(" (replace-regexp-in-string
  90. "\\]" ")" (replace-regexp-in-string
  91. ", " " " (replace-regexp-in-string
  92. "'" "\"" results))))))
  93. results)))
  94. (defun org-babel-clojure-var-to-clojure (var)
  95. "Convert an elisp var into a string of clojure source code
  96. specifying a var of the same value."
  97. (if (listp var)
  98. (format "'%s" var)
  99. (format "%S" var)))
  100. (defun org-babel-clojure-build-full-form (body vars)
  101. "Construct a clojure let form with vars as the let vars."
  102. (let ((vars-forms (mapconcat ;; define any variables
  103. (lambda (pair)
  104. (format "%s %s" (car pair) (org-babel-clojure-var-to-clojure (cdr pair))))
  105. vars "\n "))
  106. (body (org-babel-trim body)))
  107. (if (> (length vars-forms) 0) (format "(let [%s]\n %s)" vars-forms body) body)))
  108. (defun org-babel-prep-session:clojure (session params)
  109. "Prepare SESSION according to the header arguments specified in PARAMS."
  110. (let* ((session-buf (org-babel-clojure-initiate-session session))
  111. (vars (org-babel-ref-variables params))
  112. (var-lines (mapcar ;; define any top level session variables
  113. (lambda (pair)
  114. (format "(def %s %s)\n" (car pair)
  115. (org-babel-clojure-var-to-clojure (cdr pair))))
  116. vars)))
  117. session-buf))
  118. (defun org-babel-load-session:clojure (session body params)
  119. "Load BODY into SESSION."
  120. (save-window-excursion
  121. (let ((buffer (org-babel-prep-session:clojure session params)))
  122. (with-current-buffer buffer
  123. (goto-char (point-max))
  124. (insert (org-babel-chomp body)))
  125. buffer)))
  126. (defvar org-babel-clojure-buffers '())
  127. (defvar org-babel-clojure-pending-sessions '())
  128. (defun org-babel-clojure-session-buffer (session)
  129. "Return the buffer associated with SESSION."
  130. (cdr (assoc session org-babel-clojure-buffers)))
  131. (defun org-babel-clojure-initiate-session-by-key (&optional session)
  132. "If there is not a current inferior-process-buffer in SESSION
  133. then create one. Return the initialized session."
  134. (save-window-excursion
  135. (let* ((session (if session
  136. (if (stringp session) (intern session)
  137. session)
  138. :default))
  139. (clojure-buffer (org-babel-clojure-session-buffer session)))
  140. (unless (and clojure-buffer (buffer-live-p clojure-buffer))
  141. (setq org-babel-clojure-buffers (assq-delete-all session org-babel-clojure-buffers))
  142. (push session org-babel-clojure-pending-sessions)
  143. (slime)
  144. ;; we are waiting to finish setting up which will be done in
  145. ;; org-babel-clojure-session-connected-hook below.
  146. (let ((timeout 9))
  147. (while (and (not (org-babel-clojure-session-buffer session))
  148. (< 0 timeout))
  149. (message "Waiting for clojure repl for session: %s ... %i" session timeout)
  150. (sit-for 1)
  151. (decf timeout)))
  152. (setq org-babel-clojure-pending-sessions
  153. (remove session org-babel-clojure-pending-sessions))
  154. (unless (org-babel-clojure-session-buffer session)
  155. (error "Couldn't create slime clojure process"))
  156. (setq clojure-buffer (org-babel-clojure-session-buffer session)))
  157. session)))
  158. (defun org-babel-clojure-initiate-session (&optional session params)
  159. "Return the slime-clojure repl buffer bound to this session
  160. or nil if \"none\" is specified."
  161. (unless (and (stringp session) (string= session "none"))
  162. (org-babel-clojure-session-buffer (org-babel-clojure-initiate-session-by-key session))))
  163. (defun org-babel-clojure-session-connected-hook ()
  164. "Finish setting up the bindings of org-babel session to a slime-clojure repl."
  165. (let ((pending-session (pop org-babel-clojure-pending-sessions)))
  166. (when pending-session
  167. (save-excursion
  168. (switch-to-buffer (slime-output-buffer))
  169. (rename-buffer (if (stringp pending-session) pending-session (symbol-name pending-session)))
  170. (org-babel-clojure-bind-session-to-repl-buffer pending-session (slime-output-buffer))))))
  171. (add-hook 'slime-connected-hook 'org-babel-clojure-session-connected-hook)
  172. (defun org-babel-clojure-bind-session-to-repl-buffer (session repl-buffer)
  173. "Associate SESSION with REPL-BUFFER."
  174. (when (stringp session) (setq session (intern session)))
  175. (setq org-babel-clojure-buffers
  176. (cons (cons session repl-buffer)
  177. (assq-delete-all session org-babel-clojure-buffers))))
  178. (defun org-babel-clojure-repl-buffer-pred ()
  179. "Test whether the current buffer is an active slime-clojure
  180. repl buffer."
  181. (and (buffer-live-p (current-buffer)) (eq major-mode 'slime-repl-mode)))
  182. (defun org-babel-clojure-bind-session-to-repl (session)
  183. "Bind SESSION to a clojure repl."
  184. (interactive "sEnter session name: ")
  185. (let ((repl-bufs (slime-filter-buffers 'org-babel-clojure-repl-buffer-pred)))
  186. (unless repl-bufs (error "No existing slime-clojure repl buffers exist"))
  187. (let ((repl-buf (read-buffer "Choose slime-clojure repl: " repl-bufs t)))
  188. (org-babel-clojure-bind-session-to-repl-buffer session repl-buf))))
  189. (defun org-babel-clojure-evaluate-external-process (buffer body &optional result-type)
  190. "Evaluate the body in an external process."
  191. (save-excursion
  192. (let ((cmd (format "%s -" (mapconcat #'identity (org-babel-clojure-babel-clojure-cmd) " "))))
  193. (case result-type
  194. (output
  195. (with-temp-buffer
  196. (insert body)
  197. (org-babel-shell-command-on-region cmd (point-min) (point-max) 'current-buffer 'replace)
  198. (buffer-string)))
  199. (value
  200. (let* ((tmp-file (make-temp-file "org-babel-clojure-results-")) exit-code
  201. (stderr
  202. (with-temp-buffer
  203. (insert
  204. (format org-babel-clojure-wrapper-method body tmp-file tmp-file))
  205. (setq exit-code
  206. (org-babel-shell-command-on-region (point-min) (point-max) cmd nil 'replace (current-buffer)))
  207. (buffer-string))))
  208. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  209. (org-babel-clojure-table-or-string
  210. (with-temp-buffer
  211. (insert-file-contents (org-babel-maybe-remote-file tmp-file)) (buffer-string)))))))))
  212. (defun org-babel-clojure-evaluate-session (buffer body &optional result-type)
  213. "Evaluate the body in the context of a clojure session."
  214. (let ((raw nil)
  215. (results nil))
  216. (save-window-excursion
  217. (set-buffer buffer)
  218. (setq raw (org-babel-clojure-slime-eval-sync body))
  219. (setq results (reverse (mapcar #'org-babel-trim raw)))
  220. (case result-type
  221. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  222. (value (org-babel-clojure-table-or-string (car results)))))))
  223. (defun org-babel-clojure-evaluate (buffer body &optional result-type)
  224. "Pass BODY to the Clojure process in BUFFER. If RESULT-TYPE equals
  225. 'output then return a list of the outputs of the statements in
  226. BODY, if RESULT-TYPE equals 'value then return the value of the
  227. last statement in BODY, as elisp."
  228. (if session
  229. (org-babel-clojure-evaluate-session buffer body result-type)
  230. (org-babel-clojure-evaluate-external-process buffer body result-type)))
  231. (defun org-babel-expand-body:clojure (body params &optional processed-params)
  232. "Expand BODY according to PARAMS, return the expanded body."
  233. (org-babel-clojure-build-full-form
  234. body (second (or processed-params (org-babel-process-params params)))))
  235. (defun org-babel-execute:clojure (body params)
  236. "Execute a block of Clojure code with org-babel."
  237. (let* ((processed-params (org-babel-process-params params))
  238. (body (org-babel-expand-body:clojure body params processed-params))
  239. (session (org-babel-clojure-initiate-session (first processed-params))))
  240. (org-babel-reassemble-table
  241. (org-babel-clojure-evaluate session body (fourth processed-params))
  242. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  243. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  244. (provide 'ob-clojure)
  245. ;; arch-tag: a43b33f2-653e-46b1-ac56-2805cf05b7d1
  246. ;;; ob-clojure.el ends here