Merge branch 'maint'
[org-mode.git] / lisp / ob-clojure.el
bloba3e6cbe8f21810aedf46cf224328f37cc22911a1
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 nrepl-send-string-sync "ext:nrepl-client" (input &optional ns session))
52 (declare-function nrepl-current-connection-buffer "ext:nrepl" ())
53 (declare-function nrepl-eval "ext:nrepl" (body))
54 (declare-function slime-eval "ext:slime" (sexp &optional package))
56 (defvar org-babel-tangle-lang-exts)
57 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
59 (defvar org-babel-default-header-args:clojure '())
60 (defvar org-babel-header-args:clojure '((package . :any)))
62 (defcustom org-babel-clojure-backend
63 (cond ((featurep 'cider) 'cider)
64 ((featurep 'nrepl) 'nrepl)
65 (t 'slime))
66 "Backend used to evaluate Clojure code blocks."
67 :group 'org-babel
68 :type '(choice
69 (const :tag "cider" cider)
70 (const :tag "nrepl" nrepl)
71 (const :tag "SLIME" slime)))
73 (defun org-babel-expand-body:clojure (body params)
74 "Expand BODY according to PARAMS, return the expanded body."
75 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
76 (result-params (cdr (assoc :result-params params)))
77 (print-level nil) (print-length nil)
78 (body (org-babel-trim
79 (if (> (length vars) 0)
80 (concat "(let ["
81 (mapconcat
82 (lambda (var)
83 (format "%S (quote %S)" (car var) (cdr var)))
84 vars "\n ")
85 "]\n" body ")")
86 body))))
87 (if (or (member "code" result-params)
88 (member "pp" result-params))
89 (format "(clojure.pprint/pprint (do %s))" body)
90 body)))
92 (defun org-babel-execute:clojure (body params)
93 "Execute a block of Clojure code with Babel."
94 (let ((expanded (org-babel-expand-body:clojure body params))
95 result)
96 (case org-babel-clojure-backend
97 (cider
98 (require 'cider)
99 (let ((result-params (cdr (assoc :result-params params))))
100 (setq result
101 (plist-get
102 (nrepl-send-string-sync expanded)
103 (if (or (member "output" result-params)
104 (member "pp" result-params))
105 :stdout
106 :value)))))
107 (nrepl
108 (require 'nrepl)
109 (setq result
110 (if (nrepl-current-connection-buffer)
111 (let* ((result (nrepl-eval expanded))
112 (s (plist-get result :stdout))
113 (r (plist-get result :value)))
114 (if s (concat s "\n" r) r))
115 (error "nREPL not connected! Use M-x nrepl-jack-in RET"))))
116 (slime
117 (require 'slime)
118 (with-temp-buffer
119 (insert expanded)
120 (setq result
121 (slime-eval
122 `(swank:eval-and-grab-output
123 ,(buffer-substring-no-properties (point-min) (point-max)))
124 (cdr (assoc :package params)))))))
125 (org-babel-result-cond (cdr (assoc :result-params params))
126 result
127 (condition-case nil (org-babel-script-escape result)
128 (error result)))))
130 (provide 'ob-clojure)
132 ;;; ob-clojure.el ends here