org-babel-clojure.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. (replace-regexp-in-string
  91. "\\[" "(" (replace-regexp-in-string
  92. "\\]" ")" (replace-regexp-in-string
  93. ", " " " (replace-regexp-in-string
  94. "'" "\"" results)))))
  95. results)))
  96. (defun org-babel-clojure-var-to-clojure (var)
  97. "Convert an elisp var into a string of clojure source code
  98. specifying a var of the same value."
  99. (if (listp var)
  100. (format "'%s" var)
  101. (format "%S" var)))
  102. (defun org-babel-clojure-build-full-form (body vars)
  103. "Construct a clojure let form with vars as the let vars"
  104. (let ((vars-forms (mapconcat ;; define any variables
  105. (lambda (pair)
  106. (format "%s %s" (car pair) (org-babel-clojure-var-to-clojure (cdr pair))))
  107. vars "\n ")))
  108. (format "(let [%s]\n %s)" vars-forms (org-babel-trim body))))
  109. (defun org-babel-prep-session:clojure (session params)
  110. "Prepare SESSION according to the header arguments specified in PARAMS."
  111. (let* ((session-buf (org-babel-clojure-initiate-session session))
  112. (vars (org-babel-ref-variables params))
  113. (var-lines (mapcar ;; define any top level session variables
  114. (lambda (pair)
  115. (format "(def %s %s)\n" (car pair)
  116. (org-babel-clojure-var-to-clojure (cdr pair))))
  117. vars)))
  118. session-buf))
  119. (defun org-babel-load-session:clojure (session body params)
  120. "Load BODY into SESSION."
  121. (save-window-excursion
  122. (let ((buffer (org-babel-prep-session:clojure session params)))
  123. (with-current-buffer buffer
  124. (goto-char (point-max))
  125. (insert (org-babel-chomp body)))
  126. buffer)))
  127. (defvar org-babel-clojure-buffers '())
  128. (defvar org-babel-clojure-pending-sessions '())
  129. (defun org-babel-clojure-session-buffer (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. 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. (when (stringp session) (setq session (intern session)))
  174. (setq org-babel-clojure-buffers
  175. (cons (cons session repl-buffer)
  176. (assq-delete-all session org-babel-clojure-buffers))))
  177. (defun org-babel-clojure-repl-buffer-pred ()
  178. "Predicate used to test whether the passed in buffer is an active slime-clojure repl buffer"
  179. (and (buffer-live-p (current-buffer)) (eq major-mode 'slime-repl-mode)))
  180. (defun org-babel-clojure-bind-session-to-repl (session)
  181. (interactive "sEnter session name: ")
  182. (let ((repl-bufs (slime-filter-buffers 'org-babel-clojure-repl-buffer-pred)))
  183. (unless repl-bufs (error "No existing slime-clojure repl buffers exist"))
  184. (let ((repl-buf (read-buffer "Choose slime-clojure repl: " repl-bufs t)))
  185. (org-babel-clojure-bind-session-to-repl-buffer session repl-buf))))
  186. (defun org-babel-clojure-evaluate-external-process (buffer body &optional result-type)
  187. "Evaluate the body in an external process."
  188. (save-excursion
  189. (let ((cmd (format "%s -" (mapconcat #'identity (org-babel-clojure-babel-clojure-cmd) " "))))
  190. (case result-type
  191. (output
  192. (with-temp-buffer
  193. (insert body)
  194. (org-babel-shell-command-on-region cmd (point-min) (point-max) 'current-buffer 'replace)
  195. (buffer-string)))
  196. (value
  197. (let* ((tmp-file (make-temp-file "org-babel-clojure-results-")) exit-code
  198. (stderr
  199. (with-temp-buffer
  200. (insert
  201. (format org-babel-clojure-wrapper-method body tmp-file tmp-file))
  202. (setq exit-code
  203. (org-babel-shell-command-on-region (point-min) (point-max) cmd nil 'replace (current-buffer)))
  204. (buffer-string))))
  205. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  206. (org-babel-clojure-table-or-string
  207. (with-temp-buffer
  208. (insert-file-contents (org-babel-maybe-remote-file tmp-file)) (buffer-string)))))))))
  209. (defun org-babel-clojure-evaluate-session (buffer body &optional result-type)
  210. "Evaluate the body in the context of a clojure session"
  211. (let ((raw nil)
  212. (results nil))
  213. (save-window-excursion
  214. (set-buffer buffer)
  215. (setq raw (org-babel-clojure-slime-eval-sync body))
  216. (setq results (reverse (mapcar #'org-babel-trim raw)))
  217. (case result-type
  218. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  219. (value (org-babel-clojure-table-or-string (car results)))))))
  220. (defun org-babel-clojure-evaluate (buffer body &optional result-type)
  221. "Pass BODY to the Clojure process in BUFFER. If RESULT-TYPE equals
  222. 'output then return a list of the outputs of the statements in
  223. BODY, if RESULT-TYPE equals 'value then return the value of the
  224. last statement in BODY, as elisp."
  225. (if session
  226. (org-babel-clojure-evaluate-session buffer body result-type)
  227. (org-babel-clojure-evaluate-external-process buffer body result-type)))
  228. (defun org-babel-execute:clojure (body params)
  229. "Execute a block of Clojure code with org-babel."
  230. (let* ((processed-params (org-babel-process-params params))
  231. (vars (second processed-params))
  232. (body (org-babel-clojure-build-full-form body vars))
  233. (session (org-babel-clojure-initiate-session (first processed-params))))
  234. (org-babel-clojure-evaluate session body (fourth processed-params))))
  235. (provide 'org-babel-clojure)