ob-clojure.el 12 KB

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