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