org-babel-clojure.el 11 KB

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