Merge branch 'maint'
[org-mode.git] / lisp / ob-clojure.el
blobbc2bbc0d09548188c94ad422f8a0101c48bb9e20
1 ;;; ob-clojure.el --- org-babel functions for clojure evaluation
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Joel Boehland
6 ;; Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; support for evaluating clojure code, relies on slime for all eval
29 ;;; Requirements:
31 ;;; - clojure (at least 1.2.0)
32 ;;; - clojure-mode
33 ;;; - slime
35 ;;; By far, the best way to install these components is by following
36 ;;; the directions as set out by Phil Hagelberg (Technomancy) on the
37 ;;; web page: http://technomancy.us/126
39 ;;; Code:
40 (require 'ob)
42 (declare-function slime-eval "ext:slime" (sexp &optional package))
44 (defvar org-babel-tangle-lang-exts)
45 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
47 (defvar org-babel-default-header-args:clojure '())
48 (defvar org-babel-header-args:clojure '((package . :any)))
50 (defun org-babel-expand-body:clojure (body params)
51 "Expand BODY according to PARAMS, return the expanded body."
52 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
53 (result-params (cdr (assoc :result-params params)))
54 (print-level nil) (print-length nil)
55 (body (org-babel-trim
56 (if (> (length vars) 0)
57 (concat "(let ["
58 (mapconcat
59 (lambda (var)
60 (format "%S (quote %S)" (car var) (cdr var)))
61 vars "\n ")
62 "]\n" body ")")
63 body))))
64 (cond ((or (member "code" result-params) (member "pp" result-params))
65 (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] "
66 "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch "
67 "(clojure.pprint/pprint (do %s) org-mode-print-catcher) "
68 "(str org-mode-print-catcher)))")
69 (if (member "code" result-params) "code" "simple") body))
70 ;; if (:results output), collect printed output
71 ((member "output" result-params)
72 (format "(clojure.core/with-out-str %s)" body))
73 (t body))))
75 (defun org-babel-execute:clojure (body params)
76 "Execute a block of Clojure code with Babel."
77 (require 'slime)
78 (with-temp-buffer
79 (insert (org-babel-expand-body:clojure body params))
80 ((lambda (result)
81 (let ((result-params (cdr (assoc :result-params params))))
82 (org-babel-result-cond result-params
83 result
84 (condition-case nil (org-babel-script-escape result)
85 (error result)))))
86 (slime-eval
87 `(swank:eval-and-grab-output
88 ,(buffer-substring-no-properties (point-min) (point-max)))
89 (cdr (assoc :package params))))))
91 (provide 'ob-clojure)
95 ;;; ob-clojure.el ends here