ob-clojure.el: Add support for the cider backend

* ob-clojure.el (org-babel-clojure-backend): Add customization
options.
(org-babel-execute:clojure): Add support for cider.
This commit is contained in:
Bastien Guerry 2013-11-04 11:30:34 +01:00
parent 538a087cdd
commit dbc39fcef5
1 changed files with 50 additions and 35 deletions

View File

@ -24,30 +24,37 @@
;;; Commentary: ;;; Commentary:
;;; support for evaluating clojure code, relies either on slime or ;; Support for evaluating clojure code, relies either on Slime or
;;; on nrepl for all eval ;; on Nrepl.el for all eval.
;;; Requirements: ;; Requirements:
;;; - clojure (at least 1.2.0) ;; - clojure (at least 1.2.0)
;;; - clojure-mode ;; - clojure-mode
;;; - either slime or nrepl ;; - either cider or nrepl.el or SLIME
;;; For SLIME-way, the best way to install these components is by ;; For cider, see https://github.com/clojure-emacs/cider
;;; following the directions as set out by Phil Hagelberg (Technomancy)
;;; on the web page: http://technomancy.us/126
;;; For nREPL-way: ;; For SLIME, the best way to install these components is by following
;;; get clojure is with https://github.com/technomancy/leiningen ;; the directions as set out by Phil Hagelberg (Technomancy) on the
;;; get nrepl from MELPA (clojure-mode is a dependency). ;; web page: http://technomancy.us/126
;; For nREPL:
;; get clojure with https://github.com/technomancy/leiningen
;; get nrepl from MELPA (clojure-mode is a dependency).
;;; Code: ;;; Code:
(require 'ob) (require 'ob)
(declare-function slime-eval "ext:slime" (sexp &optional package)) (declare-function cider-current-ns "ext:cider-interaction" ())
(declare-function nrepl-send-string-sync "ext:nrepl-client" (input &optional ns session))
(declare-function nrepl-current-tooling-session "ext:nrepl-client" ())
(declare-function nrepl-current-connection-buffer "ext:nrepl" ()) (declare-function nrepl-current-connection-buffer "ext:nrepl" ())
(declare-function nrepl-eval "ext:nrepl" (body)) (declare-function nrepl-eval "ext:nrepl" (body))
(declare-function slime-eval "ext:slime" (sexp &optional package))
(defvar org-babel-tangle-lang-exts) (defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj")) (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
@ -57,7 +64,10 @@
(defcustom org-babel-clojure-backend 'nrepl (defcustom org-babel-clojure-backend 'nrepl
"Backend used to evaluate Clojure code blocks." "Backend used to evaluate Clojure code blocks."
:group 'org-babel :group 'org-babel
:type 'symbol) :type '(choice
(const :tag "cider" cider)
(const :tag "nrepl" nrepl)
(const :tag "SLIME" slime)))
(defun org-babel-expand-body:clojure (body params) (defun org-babel-expand-body:clojure (body params)
"Expand BODY according to PARAMS, return the expanded body." "Expand BODY according to PARAMS, return the expanded body."
@ -88,31 +98,36 @@
"Execute a block of Clojure code with Babel." "Execute a block of Clojure code with Babel."
(let ((expanded (org-babel-expand-body:clojure body params))) (let ((expanded (org-babel-expand-body:clojure body params)))
(case org-babel-clojure-backend (case org-babel-clojure-backend
(slime (cider
(require 'slime) (require 'cider)
(with-temp-buffer (or (nth 1 (nrepl-send-string-sync
(insert expanded) (format "(clojure.pprint/pprint %s)" expanded)
((lambda (result) (cider-current-ns)
(let ((result-params (cdr (assoc :result-params params)))) (nrepl-current-tooling-session)))
(org-babel-result-cond result-params (error "nREPL not connected! Use M-x cider-jack-in RET")))
result
(condition-case nil (org-babel-script-escape result)
(error result)))))
(slime-eval
`(swank:eval-and-grab-output
,(buffer-substring-no-properties (point-min) (point-max)))
(cdr (assoc :package params))))))
(nrepl (nrepl
(require 'nrepl) (require 'nrepl)
(if (nrepl-current-connection-buffer) (if (nrepl-current-connection-buffer)
(let* ((result (nrepl-eval expanded)) (let* ((result (nrepl-eval expanded))
(s (plist-get result :stdout)) (s (plist-get result :stdout))
(r (plist-get result :value))) (r (plist-get result :value)))
(if s (concat s "\n" r) r)) (if s (concat s "\n" r) r))
(error "nREPL not connected! Use M-x nrepl-jack-in.")))))) (error "nREPL not connected! Use M-x nrepl-jack-in RET")))
(slime
(require 'slime)
(with-temp-buffer
(insert expanded)
((lambda (result)
(let ((result-params (cdr (assoc :result-params params))))
(org-babel-result-cond result-params
result
(condition-case nil (org-babel-script-escape result)
(error result)))))
(slime-eval
`(swank:eval-and-grab-output
,(buffer-substring-no-properties (point-min) (point-max)))
(cdr (assoc :package params)))))))))
(provide 'ob-clojure) (provide 'ob-clojure)
;;; ob-clojure.el ends here ;;; ob-clojure.el ends here