ob-clojure.el 12 KB

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