Merge from trunk.
[bpt/emacs.git] / lisp / org / ob-clojure.el
1 ;;; ob-clojure.el --- org-babel functions for clojure evaluation
2
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
5 ;; Author: Joel Boehland
6 ;; Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.7
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;;; support for evaluating clojure code, relies on slime for all eval
29
30 ;;; Requirements:
31
32 ;;; - clojure (at least 1.2.0)
33 ;;; - clojure-mode
34 ;;; - slime
35 ;;; - swank-clojure
36
37 ;;; By far, the best way to install these components is by following
38 ;;; the directions as set out by Phil Hagelberg (Technomancy) on the
39 ;;; web page: http://technomancy.us/126
40
41 ;;; Code:
42 (require 'ob)
43
44 (declare-function slime-eval "ext:slime" (sexp &optional package))
45
46 (defvar org-babel-tangle-lang-exts)
47 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
48
49 (defvar org-babel-default-header-args:clojure '())
50 (defvar org-babel-header-arg-names:clojure '(package))
51
52 (defun org-babel-expand-body:clojure (body params)
53 "Expand BODY according to PARAMS, return the expanded body."
54 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
55 (result-params (cdr (assoc :result-params params)))
56 (print-level nil) (print-length nil)
57 (body (org-babel-trim
58 (if (> (length vars) 0)
59 (concat "(let ["
60 (mapconcat
61 (lambda (var)
62 (format "%S (quote %S)" (car var) (cdr var)))
63 vars "\n ")
64 "]\n" body ")")
65 body))))
66 (cond ((or (member "code" result-params) (member "pp" result-params))
67 (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] "
68 "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch "
69 "(clojure.pprint/pprint (do %s) org-mode-print-catcher) "
70 "(str org-mode-print-catcher)))")
71 (if (member "code" result-params) "code" "simple") body))
72 ;; if (:results output), collect printed output
73 ((member "output" result-params)
74 (format "(clojure.core/with-out-str %s)" body))
75 (t body))))
76
77 (defun org-babel-execute:clojure (body params)
78 "Execute a block of Clojure code with Babel."
79 (require 'slime) (require 'swank-clojure)
80 (with-temp-buffer
81 (insert (org-babel-expand-body:clojure body params))
82 ((lambda (result)
83 (let ((result-params (cdr (assoc :result-params params))))
84 (if (or (member "scalar" result-params)
85 (member "verbatim" result-params))
86 result
87 (condition-case nil (org-babel-script-escape result)
88 (error result)))))
89 (slime-eval
90 `(swank:interactive-eval-region
91 ,(buffer-substring-no-properties (point-min) (point-max)))
92 (cdr (assoc :package params))))))
93
94 (provide 'ob-clojure)
95
96
97
98 ;;; ob-clojure.el ends here