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