org.texi: Fix typo
[org-mode/org-tableheadings.git] / lisp / ob-clojure.el
blob21a3ef89f5ce45d441d055ab0b8b03d5e5d3d8ca
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
29 ;; Requirements:
31 ;; - clojure (at least 1.2.0)
32 ;; - clojure-mode
33 ;; - either cider or SLIME
35 ;; For Cider, see https://github.com/clojure-emacs/cider
37 ;; For SLIME, the best way to install these components is by following
38 ;; the directions as set out by Phil Hagelberg (Technomancy) on the
39 ;; web page: http://technomancy.us/126
41 ;;; Code:
42 (require 'ob)
43 (eval-when-compile
44 (require 'cl))
46 (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
47 (declare-function nrepl-sync-request:eval "ext:nrepl-client" (input &optional ns session))
48 (declare-function slime-eval "ext:slime" (sexp &optional package))
50 (defvar org-babel-tangle-lang-exts)
51 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
53 (defvar org-babel-default-header-args:clojure '())
54 (defvar org-babel-header-args:clojure '((package . :any)))
56 (defcustom org-babel-clojure-backend
57 (cond ((featurep 'cider) 'cider)
58 (t 'slime))
59 "Backend used to evaluate Clojure code blocks."
60 :group 'org-babel
61 :type '(choice
62 (const :tag "cider" cider)
63 (const :tag "SLIME" slime)))
65 (defun org-babel-expand-body:clojure (body params)
66 "Expand BODY according to PARAMS, return the expanded body."
67 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
68 (result-params (cdr (assoc :result-params params)))
69 (print-level nil) (print-length nil)
70 (body (org-babel-trim
71 (if (> (length vars) 0)
72 (concat "(let ["
73 (mapconcat
74 (lambda (var)
75 (format "%S (quote %S)" (car var) (cdr var)))
76 vars "\n ")
77 "]\n" body ")")
78 body))))
79 (if (or (member "code" result-params)
80 (member "pp" result-params))
81 (format "(clojure.pprint/pprint (do %s))" body)
82 body)))
84 (defun org-babel-execute:clojure (body params)
85 "Execute a block of Clojure code with Babel."
86 (let ((expanded (org-babel-expand-body:clojure body params))
87 result)
88 (case org-babel-clojure-backend
89 (cider
90 (require 'cider)
91 (let ((result-params (cdr (assoc :result-params params))))
92 (setq result
93 (nrepl-dict-get
94 (nrepl-sync-request:eval expanded)
95 (if (or (member "output" result-params)
96 (member "pp" result-params))
97 "out"
98 "value")))))
99 (slime
100 (require 'slime)
101 (with-temp-buffer
102 (insert expanded)
103 (setq result
104 (slime-eval
105 `(swank:eval-and-grab-output
106 ,(buffer-substring-no-properties (point-min) (point-max)))
107 (cdr (assoc :package params)))))))
108 (org-babel-result-cond (cdr (assoc :result-params params))
109 result
110 (condition-case nil (org-babel-script-escape result)
111 (error result)))))
113 (provide 'ob-clojure)
115 ;;; ob-clojure.el ends here