all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* patch: ob-clojure improvements
@ 2020-06-20  6:55 Ag Ibragimov
  2020-06-20  8:23 ` stardiviner
  2020-10-09 21:17 ` joseph.corneli.orgmode--- via General discussions about Org-mode.
  0 siblings, 2 replies; 12+ messages in thread
From: Ag Ibragimov @ 2020-06-20  6:55 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 604 bytes --]

Hi everyone, here's my attempt to add clojure CLI and babashka support for ob-clojure.el

- Adds a header parameter to override org-babel-clojure-backend
- Adds :args param (right now only used for clojure-cli)

I have tested it with these minimal cases:

#+begin_src clojure :backend clj-cli :args "-Sdeps '{:deps {inflections {:mvn/version \"0.13.2\"}}}'"
  (use 'inflections.core)
  (plural "word")
#+end_src

#+begin_src clojure :backend babashka :results output
  (range 10)
#+end_src

Please let me know what you think. Any advice is appreciated, since I have never contributed before. Thank you.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: ob-clojure-improvements.patch --]
[-- Type: text/x-patch, Size: 3538 bytes --]

diff --git a/lisp/ob-clojure.el b/lisp/ob-clojure.el
index 299a326e4..4e79a2c24 100644
--- a/lisp/ob-clojure.el
+++ b/lisp/ob-clojure.el
@@ -61,7 +61,13 @@ (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
 (add-to-list 'org-babel-tangle-lang-exts '("clojurescript" . "cljs"))
 
 (defvar org-babel-default-header-args:clojure '())
-(defvar org-babel-header-args:clojure '((ns . :any) (package . :any)))
+
+(defvar org-babel-header-args:clojure
+  '((ns . :any)
+    (package . :any)
+    (backend . ((inf-clojure cider slime clj-cli babashka)))
+    (args . :any)))
+
 (defvar org-babel-default-header-args:clojurescript '())
 (defvar org-babel-header-args:clojurescript '((package . :any)))
 
@@ -224,25 +230,47 @@ (defun ob-clojure-eval-with-slime (expanded params)
        ,(buffer-substring-no-properties (point-min) (point-max)))
      (cdr (assq :package params)))))
 
+(defun ob-clojure-eval-with-babashka (expanded params)
+  "Evaluate EXPANDED code block with PARAMS using babashka."
+  (let ((exe (executable-find "bb")))
+    (unless exe (user-error "babashka CLI (bb) not found."))
+    (org-babel-execute:shell
+     (format "%s -e %S" exe expanded) params)))
+
+(defun ob-clojure-eval-with-clj-cli (expanded params)
+  "Evaluate EXPANDED code block with PARAMS using clojure-cli."
+  (let* ((args (cdr (assq :args params)))
+	 (exe (executable-find "clj")))
+    (unless exe (user-error "clj CLI tool not found."))
+    (org-babel-execute:shell
+     (format "%s %s -e %S" exe args expanded) params)))
+
 (defun org-babel-execute:clojure (body params)
   "Execute a block of Clojure code with Babel."
-  (unless org-babel-clojure-backend
-    (user-error "You need to customize org-babel-clojure-backend"))
-  (let* ((expanded (org-babel-expand-body:clojure body params))
-	 (result-params (cdr (assq :result-params params)))
-	 result)
-    (setq result
-	  (cond
-	   ((eq org-babel-clojure-backend 'inf-clojure)
-	    (ob-clojure-eval-with-inf-clojure expanded params))
-	   ((eq org-babel-clojure-backend 'cider)
-	    (ob-clojure-eval-with-cider expanded params))
-	   ((eq org-babel-clojure-backend 'slime)
-	    (ob-clojure-eval-with-slime expanded params))))
-    (org-babel-result-cond result-params
-      result
-      (condition-case nil (org-babel-script-escape result)
-	(error result)))))
+  (let* ((backend-override (cdr (assq :backend params)))
+	 (org-babel-clojure-backend
+	  (cond (backend-override (intern backend-override))
+		(org-babel-clojure-backend org-babel-clojure-backend)
+		(t (user-error "You need to customize org-babel-clojure-backend")))))
+    (let* ((expanded (org-babel-expand-body:clojure body params))
+	   (result-params (cdr (assq :result-params params)))
+	   result)
+      (setq result
+	    (cond
+	     ((eq org-babel-clojure-backend 'inf-clojure)
+	      (ob-clojure-eval-with-inf-clojure expanded params))
+	     ((eq org-babel-clojure-backend 'cider)
+	      (ob-clojure-eval-with-cider expanded params))
+	     ((eq org-babel-clojure-backend 'slime)
+	      (ob-clojure-eval-with-slime expanded params))
+	     ((eq org-babel-clojure-backend 'babashka)
+	      (ob-clojure-eval-with-babashka expanded params))
+	     ((eq org-babel-clojure-backend 'clj-cli)
+	      (ob-clojure-eval-with-clj-cli expanded params))))
+      (org-babel-result-cond result-params
+	result
+	(condition-case nil (org-babel-script-escape result)
+	  (error result))))))
 
 (defun org-babel-execute:clojurescript (body params)
   "Evaluate BODY with PARAMS as ClojureScript code."

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-02-03 12:07 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-20  6:55 patch: ob-clojure improvements Ag Ibragimov
2020-06-20  8:23 ` stardiviner
2020-07-02 22:43   ` agzam.ibragimov
2020-07-03  3:10     ` stardiviner
2020-07-03  3:53       ` Tim Cross
2021-02-02 12:11         ` Christopher Miles
2021-02-02 19:52           ` Tim Cross
2021-02-03  0:13             ` Christopher Miles
2021-02-03  6:36               ` Tim Cross
2021-02-03 11:59                 ` Christopher Miles
2020-09-04 15:24     ` Bastien
2020-10-09 21:17 ` joseph.corneli.orgmode--- via General discussions about Org-mode.

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.