Skip to content

Commit

Permalink
ob-clojure.el: Add support for babashka and nbb backend
Browse files Browse the repository at this point in the history
* lisp/ob-clojure.el: Add support for babashka and nbb backend.
  • Loading branch information
dakra authored and bzg committed Sep 25, 2022
1 parent e8a797e commit 764642f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lisp/ob-clojure.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,13 +79,25 @@
(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"
"Default Clojure namespace for source block when finding ns failed."
: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))
Expand Down Expand Up @@ -229,6 +243,15 @@
,(buffer-substring-no-properties (point-min) (point-max)))
(cdr (assq :package params)))))

(defun ob-clojure-eval-with-babashka (bb expanded)
"Evaluate EXPANDED code block using BB (babashka or nbb)."
(let ((script-file (org-babel-temp-file "clojure-bb-script-" ".clj")))
(with-temp-file script-file
(insert expanded))
(org-babel-eval
(format "%s %s" bb (org-babel-process-file-name script-file))
"")))

(defun org-babel-execute:clojure (body params)
"Execute a block of Clojure code with Babel."
(unless org-babel-clojure-backend
Expand All @@ -240,6 +263,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)
Expand Down

0 comments on commit 764642f

Please sign in to comment.