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