emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Daniel Kraus <daniel@kraus.my>
To: emacs-orgmode@gnu.org
Subject: [PATCH] ob-clojure.el: Add support for babashka and nbb backend
Date: Sun, 14 Nov 2021 16:28:01 +0100	[thread overview]
Message-ID: <87bl2mycxq.fsf@kraus.my> (raw)

* lisp/ob-clojure.el: Add support for babashka and nbb backend.
---
This adds support to ob-clojure for babashka (https://github.com/babashka/babashka)
and nbb (node version of babashka).
It doesn't use `params` as I'm not really sure what they're used for and
if they're important for evaluation.

I'm also new to org development and the git email workflow so any feedback
to the code or the email patch etc is appreciated.

 lisp/ob-clojure.el | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/lisp/ob-clojure.el b/lisp/ob-clojure.el
index 3b995d94c..dc42daa5c 100644
--- a/lisp/ob-clojure.el
+++ b/lisp/ob-clojure.el
@@ -36,6 +36,8 @@
 ;; For clojure-mode, see https://github.com/clojure-emacs/clojure-mode
 ;; For cider, see https://github.com/clojure-emacs/cider
 ;; For inf-clojure, see https://github.com/clojure-emacs/cider
+;; For babashka, see https://github.com/babashka/babashka
+;; For nbb, see https://github.com/babashka/nbb
 
 ;; For SLIME, the best way to install these components is by following
 ;; the directions as set out by Phil Hagelberg (Technomancy) on the
@@ -73,6 +75,8 @@
 	  (const :tag "inf-clojure" inf-clojure)
 	  (const :tag "cider" cider)
 	  (const :tag "slime" slime)
+	  (const :tag "babashka" babashka)
+	  (const :tag "nbb" nbb)
 	  (const :tag "Not configured yet" nil)))
 
 (defcustom org-babel-clojure-default-ns "user"
@@ -80,6 +84,16 @@
   :type 'string
   :group 'org-babel)
 
+(defcustom ob-clojure-babashka-command (executable-find "bb")
+  "Path to the babashka executable."
+  :type 'file
+  :group 'org-babel)
+
+(defcustom ob-clojure-nbb-command (executable-find "nbb")
+  "Path to the nbb executable."
+  :type 'file
+  :group 'org-babel)
+
 (defun org-babel-expand-body:clojure (body params)
   "Expand BODY according to PARAMS, return the expanded body."
   (let* ((vars (org-babel--get-vars params))
@@ -225,6 +239,16 @@
        ,(buffer-substring-no-properties (point-min) (point-max)))
      (cdr (assq :package params)))))
 
+(defun ob-clojure-escape-quotes (str-val)
+  "Escape quotes for STR-VAL."
+  (replace-regexp-in-string "\"" "\\\"" str-val 'FIXEDCASE 'LITERAL))
+
+(defun ob-clojure-eval-with-babashka (bb expanded)
+  "Evaluate EXPANDED code block using BB (babashka or nbb)."
+  (let ((escaped (ob-clojure-escape-quotes expanded)))
+    (shell-command-to-string
+     (concat bb " -e \"" escaped "\""))))
+
 (defun org-babel-execute:clojure (body params)
   "Execute a block of Clojure code with Babel."
   (unless org-babel-clojure-backend
@@ -236,6 +260,10 @@
 	  (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)
-- 
2.33.1




             reply	other threads:[~2021-11-14 15:31 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-14 15:28 Daniel Kraus [this message]
2021-11-14 16:25 ` [PATCH] ob-clojure.el: Add support for babashka and nbb backend Max Nikulin
2021-11-14 16:30   ` Daniel Kraus
2021-11-15 14:33     ` Max Nikulin
2021-11-15 16:05       ` Daniel Kraus
2021-11-17 16:12         ` Max Nikulin
2021-11-20 10:18           ` Daniel Kraus
2021-12-22 22:34             ` Daniel Kraus
2022-01-31  7:58         ` Daniel Kraus
2022-02-02 15:58           ` Max Nikulin
2022-06-11  7:45           ` Ihor Radchenko
2022-09-25  3:10         ` Bastien
2022-09-25 13:37           ` Christopher M. Miles
2022-09-25 13:43           ` Christopher M. Miles
     [not found]           ` <20969.6847757854$1664113375@news.gmane.org>
2022-09-25 13:48             ` Bastien
2022-09-26 16:02           ` Daniel Kraus
2022-09-27  2:31             ` Ihor Radchenko
2022-09-27 19:39             ` Bastien
2022-09-28  1:16               ` [WORG] Document in more detail about what maintainers do? (was: [PATCH] ob-clojure.el: Add support for babashka and nbb backend) Ihor Radchenko
2022-09-28  6:56                 ` [WORG] Document in more detail about what maintainers do? Bastien
2022-09-29  7:06                   ` Ihor Radchenko
2022-10-29  6:28                     ` Ihor Radchenko
2022-10-29  8:06                       ` Bastien Guerry

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=87bl2mycxq.fsf@kraus.my \
    --to=daniel@kraus.my \
    --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).