Merge branch 'maint'
[org-mode.git] / lisp / ob-clojure.el
blob255fe8d31a52145115aeaf2d0b568e41210b65fc
1 ;;; ob-clojure.el --- org-babel functions for clojure evaluation
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Joel Boehland, Eric Schulte, Oleh Krehel
6 ;;
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 either on slime or
28 ;;; on nrepl for all eval
30 ;;; Requirements:
32 ;;; - clojure (at least 1.2.0)
33 ;;; - clojure-mode
34 ;;; - either slime or nrepl
36 ;;; For SLIME-way, the best way to install these components is by
37 ;;; following the directions as set out by Phil Hagelberg (Technomancy)
38 ;;; on the web page: http://technomancy.us/126
40 ;;; For nREPL-way:
41 ;;; get clojure is with https://github.com/technomancy/leiningen
42 ;;; get nrepl from MELPA (clojure-mode is a dependency).
44 ;;; Code:
45 (require 'ob)
47 (declare-function slime-eval "ext:slime" (sexp &optional package))
48 (declare-function nrepl-current-connection-buffer "ext:nrepl" ())
49 (declare-function nrepl-eval "ext:nrepl" (body))
51 (defvar org-babel-tangle-lang-exts)
52 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
54 (defvar org-babel-default-header-args:clojure '())
55 (defvar org-babel-header-args:clojure '((package . :any)))
57 (defcustom org-babel-clojure-backend 'nrepl
58 "Backend used to evaluate Clojure code blocks."
59 :group 'org-babel
60 :type 'symbol)
62 (defun org-babel-expand-body:clojure (body params)
63 "Expand BODY according to PARAMS, return the expanded body."
64 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
65 (result-params (cdr (assoc :result-params params)))
66 (print-level nil) (print-length nil)
67 (body (org-babel-trim
68 (if (> (length vars) 0)
69 (concat "(let ["
70 (mapconcat
71 (lambda (var)
72 (format "%S (quote %S)" (car var) (cdr var)))
73 vars "\n ")
74 "]\n" body ")")
75 body))))
76 (cond ((or (member "code" result-params) (member "pp" result-params))
77 (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] "
78 "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch "
79 "(clojure.pprint/pprint (do %s) org-mode-print-catcher) "
80 "(str org-mode-print-catcher)))")
81 (if (member "code" result-params) "code" "simple") body))
82 ;; if (:results output), collect printed output
83 ((member "output" result-params)
84 (format "(clojure.core/with-out-str %s)" body))
85 (t body))))
87 (defun org-babel-execute:clojure (body params)
88 "Execute a block of Clojure code with Babel."
89 (let ((expanded (org-babel-expand-body:clojure body params)))
90 (case org-babel-clojure-backend
91 (slime
92 (require 'slime)
93 (with-temp-buffer
94 (insert expanded)
95 ((lambda (result)
96 (let ((result-params (cdr (assoc :result-params params))))
97 (org-babel-result-cond result-params
98 result
99 (condition-case nil (org-babel-script-escape result)
100 (error result)))))
101 (slime-eval
102 `(swank:eval-and-grab-output
103 ,(buffer-substring-no-properties (point-min) (point-max)))
104 (cdr (assoc :package params))))))
105 (nrepl
106 (require 'nrepl)
107 (if (nrepl-current-connection-buffer)
108 (let* ((result (nrepl-eval expanded))
109 (s (plist-get result :stdout))
110 (r (plist-get result :value)))
111 (if s (concat s "\n" r) r))
112 (error "nREPL not connected! Use M-x nrepl-jack-in."))))))
114 (provide 'ob-clojure)
118 ;;; ob-clojure.el ends here