From 5f9dbe53cea2b37fc89cc49f858f98387da99576 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Tue, 28 Apr 2020 17:37:12 -0400 Subject: [PATCH] Add support for running clojure blocks in org-mode via babashka --- emacs/init.org | 74 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/emacs/init.org b/emacs/init.org index 3a6ede4..a2b931c 100755 --- a/emacs/init.org +++ b/emacs/init.org @@ -3224,15 +3224,6 @@ Clj-refactor adds magical refactoring abilities: :hook ((clojure-mode . clj-refactor-setup))) #+END_SRC - -Enable Org-mode Clojure evaluation: -#+BEGIN_SRC emacs-lisp - (add-hook 'org-mode-hook - (lambda () - (require 'ob-clojure) - (setq org-babel-clojure-backend 'cider))) -#+END_SRC - Integrate with cljfmt, the Clojure code formatter: #+BEGIN_SRC emacs-lisp (defun cljfmt () @@ -3262,6 +3253,71 @@ Integrate with cljfmt, the Clojure code formatter: (general-def clojure-mode-map "C-M-\\" #'cljfmt) #+END_SRC +Add support for running Org-mode Clojure source blocks with [[https://github.com/borkdude/babashka][Babashka]]: +#+BEGIN_SRC emacs-lisp + (with-eval-after-load 'ob-clojure + (defcustom org-babel-clojure-backend nil + "Backend used to evaluate Clojure code blocks." + :group 'org-babel + :type '(choice + (const :tag "inf-clojure" inf-clojure) + (const :tag "cider" cider) + (const :tag "slime" slime) + (const :tag "bb" bb) + (const :tag "Not configured yet" nil))) + + (defun elisp->clj (in) + (cond + ((listp in) (concat "[" (s-join " " (mapcar #'elisp->clj in)) "]")) + (t (format "%s" in)))) + + (defun ob-clojure-eval-with-bb (expanded params) + "Evaluate EXPANDED code block with PARAMS using babashka." + (unless (executable-find "bb") + (user-error "Babashka not installed")) + (let* ((stdin (let ((stdin (cdr (assq :stdin params)))) + (when stdin + (elisp->clj + (org-babel-ref-resolve stdin))))) + (input (cdr (assq :input params))) + (file (make-temp-file "ob-clojure-bb" nil nil expanded)) + (command (concat (when stdin (format "echo %s | " (shell-quote-argument stdin))) + (format "bb %s -f %s" + (cond + ((equal input "edn") "") + ((equal input "text") "-i") + (t "")) + (shell-quote-argument file)))) + (result (shell-command-to-string command))) + (s-trim result))) + + (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)) + ((eq org-babel-clojure-backend 'bb) + (ob-clojure-eval-with-bb expanded params)))) + (org-babel-result-cond result-params + result + (condition-case nil (org-babel-script-escape result) + (error result))))) + + (customize-set-variable 'org-babel-clojure-backend 'bb)) + + (add-hook 'org-mode-hook (lambda () (require 'ob-clojure))) +#+END_SRC + * Scheme Tell emacs about file extensions which should activate scheme-mode: #+BEGIN_SRC emacs-lisp