emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ag Ibragimov <agzam.ibragimov@gmail.com>
To: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: patch: ob-clojure improvements
Date: Fri, 19 Jun 2020 23:55:48 -0700	[thread overview]
Message-ID: <m2y2oimdjf.fsf@gmail.com> (raw)

[-- 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."

             reply	other threads:[~2020-06-20  6:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-20  6:55 Ag Ibragimov [this message]
2020-06-20  8:23 ` patch: ob-clojure improvements 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.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2y2oimdjf.fsf@gmail.com \
    --to=agzam.ibragimov@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).