Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-clojure.el
blob72ea77ddf8a0718cea100674f8c42a41645fa4d4
1 ;;; ob-clojure.el --- Babel Functions for Clojure -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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 'cl-lib)
43 (require 'ob)
45 (declare-function cider-current-connection "ext:cider-client" (&optional type))
46 (declare-function cider-current-session "ext:cider-client" ())
47 (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
48 (declare-function nrepl-sync-request:eval "ext:nrepl-client"
49 (input connection session &optional ns))
50 (declare-function org-trim "org" (s &optional keep-lead))
51 (declare-function slime-eval "ext:slime" (sexp &optional package))
53 (defvar org-babel-tangle-lang-exts)
54 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
56 (defvar org-babel-default-header-args:clojure '())
57 (defvar org-babel-header-args:clojure '((package . :any)))
59 (defcustom org-babel-clojure-backend
60 (cond ((featurep 'cider) 'cider)
61 (t 'slime))
62 "Backend used to evaluate Clojure code blocks."
63 :group 'org-babel
64 :type '(choice
65 (const :tag "cider" cider)
66 (const :tag "SLIME" slime)))
68 (defun org-babel-expand-body:clojure (body params)
69 "Expand BODY according to PARAMS, return the expanded body."
70 (let* ((vars (org-babel--get-vars params))
71 (result-params (cdr (assq :result-params params)))
72 (print-level nil) (print-length nil)
73 (body (org-trim
74 (if (null vars) (org-trim body)
75 (concat "(let ["
76 (mapconcat
77 (lambda (var)
78 (format "%S (quote %S)" (car var) (cdr var)))
79 vars "\n ")
80 "]\n" body ")")))))
81 (if (or (member "code" result-params)
82 (member "pp" result-params))
83 (format "(clojure.pprint/pprint (do %s))" body)
84 body)))
86 (defun org-babel-execute:clojure (body params)
87 "Execute a block of Clojure code with Babel."
88 (let ((expanded (org-babel-expand-body:clojure body params))
89 result)
90 (cl-case org-babel-clojure-backend
91 (cider
92 (require 'cider)
93 (let ((result-params (cdr (assq :result-params params))))
94 (setq result
95 (nrepl-dict-get
96 (nrepl-sync-request:eval
97 expanded (cider-current-connection) (cider-current-session))
98 (if (or (member "output" result-params)
99 (member "pp" result-params))
100 "out"
101 "value")))))
102 (slime
103 (require 'slime)
104 (with-temp-buffer
105 (insert expanded)
106 (setq result
107 (slime-eval
108 `(swank:eval-and-grab-output
109 ,(buffer-substring-no-properties (point-min) (point-max)))
110 (cdr (assq :package params)))))))
111 (org-babel-result-cond (cdr (assq :result-params params))
112 result
113 (condition-case nil (org-babel-script-escape result)
114 (error result)))))
116 (provide 'ob-clojure)
118 ;;; ob-clojure.el ends here