Browse Source

ob-clojure nrepl customization variable

  - ensure body is expanded for n-repl as well
  - declare n-repl functions
  - remove require ob-tangle from ob-clojure

* lisp/ob-clojure.el (org-babel-clojure-backend): Customization
  variable to select clojure backend
  (nrepl-eval): Declared function.
  (nrepl-current-connection-buffer): Declared function.
  (org-babel-execute:clojure): Execution now uses customized backend.
Eric Schulte 4 years ago
parent
commit
ee6c792ec7
1 changed files with 32 additions and 27 deletions
  1. 32 27
      lisp/ob-clojure.el

+ 32 - 27
lisp/ob-clojure.el

@@ -43,9 +43,10 @@
 
 ;;; Code:
 (require 'ob)
-(require 'ob-tangle)
 
 (declare-function slime-eval "ext:slime" (sexp &optional package))
+(declare-function nrepl-current-connection-buffer "ext:nrepl" ())
+(declare-function nrepl-eval "ext:nrepl" (body))
 
 (defvar org-babel-tangle-lang-exts)
 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
@@ -53,6 +54,11 @@
 (defvar org-babel-default-header-args:clojure '())
 (defvar org-babel-header-args:clojure '((package . :any)))
 
+(defcustom org-babel-clojure-backend 'nrepl
+  "Backend used to evaluate Clojure code blocks."
+  :group 'org-babel
+  :type 'symbol)
+
 (defun org-babel-expand-body:clojure (body params)
   "Expand BODY according to PARAMS, return the expanded body."
   (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
@@ -78,33 +84,32 @@
 	   (format "(clojure.core/with-out-str %s)" body))
 	  (t body))))
 
-(defun org-babel--execute-clojure-slime (body params)
+(defun org-babel-execute:clojure (body params)
   "Execute a block of Clojure code with Babel."
-  (require 'slime)
-  (with-temp-buffer
-    (insert (org-babel-expand-body:clojure body params))
-    ((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))))))
-
-(defun org-babel--execute-clojure-nrepl (body params)
-  "Execute a block of Clojure code with Babel and nREPL."
-  (require 'nrepl)
-  (if (nrepl-current-connection-buffer)
-      (let* ((result (nrepl-eval body))
-             (s (plist-get result :stdout))
-             (r (plist-get result :value)))
-        (if s (concat s "\n" r) r))
-    (error "nREPL not connected! Use M-x nrepl-jack-in.")))
-
-(defalias 'org-babel-execute:clojure 'org-babel--execute-clojure-nrepl)
+  (let ((expanded (org-babel-expand-body:clojure body params)))
+    (case org-babel-clojure-backend
+      (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))))))
+      (nrepl
+       (require 'nrepl)
+       (if (nrepl-current-connection-buffer)
+	   (let* ((result (nrepl-eval expanded))
+		  (s (plist-get result :stdout))
+		  (r (plist-get result :value)))
+	     (if s (concat s "\n" r) r))
+	 (error "nREPL not connected! Use M-x nrepl-jack-in."))))))
 
 (provide 'ob-clojure)