all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Add 'backend' header arg to clojure code blocks
@ 2022-10-08 10:24 Chris Clark
  2022-10-08 12:43 ` Christopher M. Miles
  2022-10-09  7:42 ` Ihor Radchenko
  0 siblings, 2 replies; 9+ messages in thread
From: Chris Clark @ 2022-10-08 10:24 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 479 bytes --]

This is my first attempt to contribute to org-mode; please let me know if
I'm doing something wrong.

Attached is a patch to incorporate some of the changes that were originally
presented by Ag Ibragimov here:
https://list.orgmode.org/m2y2oimdjf.fsf@gmail.com/. I've been using these
changes locally for a long time, and I'm hoping others might benefit!

For example:

   #+header: :backend babashka
   #+begin_src clojure
     (range 3)
   #+end_src

   #+RESULTS:
   : (0 1 2)

[-- Attachment #1.2: Type: text/html, Size: 722 bytes --]

[-- Attachment #2: 0001-ob-clojure.el-Add-a-backend-header-arg-to-clojure-co.patch --]
[-- Type: application/octet-stream, Size: 3619 bytes --]

From cc72dcac3075be76e3edcfee75c887272e48698c Mon Sep 17 00:00:00 2001
From: Chris Clark <cfclrk@gmail.com>
Date: Sat, 8 Oct 2022 05:41:20 -0400
Subject: [PATCH] ob-clojure.el: Add a 'backend' header arg to clojure code
 blocks

* ob-clojure.el (org-babel-header-args:clojure,
org-babel-execute:clojure): Add a 'backend' header arg that can
override the configured `org-babel-clojure-backend`.
---
 lisp/ob-clojure.el | 53 ++++++++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/lisp/ob-clojure.el b/lisp/ob-clojure.el
index dc72eb049..24a5ab34a 100644
--- a/lisp/ob-clojure.el
+++ b/lisp/ob-clojure.el
@@ -68,7 +68,10 @@
 (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 babashka nbb)))))
 (defvar org-babel-default-header-args:clojurescript '())
 (defvar org-babel-header-args:clojurescript '((package . :any)))
 
@@ -253,28 +256,32 @@
      "")))
 
 (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 'babashka)
-	    (ob-clojure-eval-with-babashka ob-clojure-babashka-command expanded))
-           ((eq org-babel-clojure-backend 'nbb)
-	    (ob-clojure-eval-with-babashka ob-clojure-nbb-command expanded))
-	   ((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)))))
+  "Execute the BODY block of Clojure code with PARAMS using Babel."
+  (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 'babashka)
+	      (ob-clojure-eval-with-babashka ob-clojure-babashka-command expanded))
+             ((eq org-babel-clojure-backend 'nbb)
+	      (ob-clojure-eval-with-babashka ob-clojure-nbb-command expanded))
+	     ((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))))))
 
 (defun org-babel-execute:clojurescript (body params)
   "Evaluate BODY with PARAMS as ClojureScript code."
-- 
2.38.0


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

end of thread, other threads:[~2022-10-13  2:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-08 10:24 Add 'backend' header arg to clojure code blocks Chris Clark
2022-10-08 12:43 ` Christopher M. Miles
2022-10-09  7:42 ` Ihor Radchenko
2022-10-09 15:34   ` Chris Clark
2022-10-11  2:55     ` Ihor Radchenko
2022-10-12  0:40       ` [PATCH] Fix void variable error `cider-buffer-ns` when specifying :backend header argument Christopher M. Miles
     [not found]       ` <63460e17.050a0220.1ff7f.4bdeSMTPIN_ADDED_BROKEN@mx.google.com>
2022-10-12  6:04         ` Ihor Radchenko
2022-10-12  9:29           ` [PATCH] (v2) " Christopher M. Miles
     [not found]           ` <6346892e.050a0220.a2480.498fSMTPIN_ADDED_BROKEN@mx.google.com>
2022-10-13  2:56             ` Ihor Radchenko

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.