Merge branch 'maint'
[org-mode.git] / lisp / ob-clojure.el
blob36f655f53ef69ff2b6498a4d5f8f12e933f17b1b
1 ;;; ob-clojure.el --- org-babel functions for clojure evaluation
3 ;; Copyright (C) 2009-2014 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.el for all eval.
30 ;; Requirements:
32 ;; - clojure (at least 1.2.0)
33 ;; - clojure-mode
34 ;; - either cider or nrepl.el or SLIME
36 ;; For cider, see https://github.com/clojure-emacs/cider
38 ;; For SLIME, 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 ;; For nREPL:
43 ;; get clojure with https://github.com/technomancy/leiningen
44 ;; get nrepl from MELPA (clojure-mode is a dependency).
46 ;;; Code:
47 (require 'ob)
48 (eval-when-compile
49 (require 'cl))
51 (declare-function cider-current-ns "ext:cider-interaction" ())
52 (declare-function cider-get-raw-value "ext:cider-client" (eval-result))
53 (declare-function cider-eval-sync "ext:cider-client" (input &optional ns session))
54 (declare-function nrepl-send-string-sync "ext:nrepl-client" (input &optional ns session))
55 (declare-function nrepl-current-tooling-session "ext:nrepl-client" ())
57 (declare-function nrepl-current-connection-buffer "ext:nrepl" ())
58 (declare-function nrepl-eval "ext:nrepl" (body))
60 (declare-function slime-eval "ext:slime" (sexp &optional package))
62 (defvar org-babel-tangle-lang-exts)
63 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
65 (defvar org-babel-default-header-args:clojure '())
66 (defvar org-babel-header-args:clojure '((package . :any)))
68 (defcustom org-babel-clojure-backend
69 (cond ((featurep 'cider) 'cider)
70 ((featurep 'nrepl) 'nrepl)
71 (t 'slime))
72 "Backend used to evaluate Clojure code blocks."
73 :group 'org-babel
74 :type '(choice
75 (const :tag "cider" cider)
76 (const :tag "nrepl" nrepl)
77 (const :tag "SLIME" slime)))
79 (defun org-babel-expand-body:clojure (body params)
80 "Expand BODY according to PARAMS, return the expanded body."
81 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
82 (result-params (cdr (assoc :result-params params)))
83 (print-level nil) (print-length nil)
84 (body (org-babel-trim
85 (if (> (length vars) 0)
86 (concat "(let ["
87 (mapconcat
88 (lambda (var)
89 (format "%S (quote %S)" (car var) (cdr var)))
90 vars "\n ")
91 "]\n" body ")")
92 body))))
93 (cond ((or (member "code" result-params) (member "pp" result-params))
94 (format (concat "(let [org-mode-print-catcher (java.io.StringWriter.)] "
95 "(clojure.pprint/with-pprint-dispatch clojure.pprint/%s-dispatch "
96 "(clojure.pprint/pprint (do %s) org-mode-print-catcher) "
97 "(str org-mode-print-catcher)))")
98 (if (member "code" result-params) "code" "simple") body))
99 ;; if (:results output), collect printed output
100 ((member "output" result-params)
101 (format "(clojure.core/with-out-str %s)" body))
102 (t body))))
104 (defun org-babel-execute:clojure (body params)
105 "Execute a block of Clojure code with Babel."
106 (let ((expanded (org-babel-expand-body:clojure body params))
107 result)
108 (case org-babel-clojure-backend
109 (cider
110 (require 'cider)
111 (setq result
112 (or (cider-get-raw-value
113 (cider-eval-sync
114 expanded
115 (cider-current-ns)
116 (nrepl-current-tooling-session)))
117 (error "nREPL not connected! Use M-x cider-jack-in RET"))))
118 (nrepl
119 (require 'nrepl)
120 (setq result
121 (if (nrepl-current-connection-buffer)
122 (let* ((result (nrepl-eval expanded))
123 (s (plist-get result :stdout))
124 (r (plist-get result :value)))
125 (if s (concat s "\n" r) r))
126 (error "nREPL not connected! Use M-x nrepl-jack-in RET"))))
127 (slime
128 (require 'slime)
129 (with-temp-buffer
130 (insert expanded)
131 (setq result
132 (slime-eval
133 `(swank:eval-and-grab-output
134 ,(buffer-substring-no-properties (point-min) (point-max)))
135 (cdr (assoc :package params)))))))
136 (org-babel-result-cond (cdr (assoc :result-params params))
137 result
138 (condition-case nil (org-babel-script-escape result)
139 (error result)))))
141 (provide 'ob-clojure)
143 ;;; ob-clojure.el ends here