Add support for running clojure blocks in org-mode via babashka

This commit is contained in:
Jeremy Dormitzer 2020-04-28 17:37:12 -04:00
parent df593f7cbc
commit 5f9dbe53ce

View File

@ -3224,15 +3224,6 @@ Clj-refactor adds magical refactoring abilities:
:hook ((clojure-mode . clj-refactor-setup))) :hook ((clojure-mode . clj-refactor-setup)))
#+END_SRC #+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: Integrate with cljfmt, the Clojure code formatter:
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun cljfmt () (defun cljfmt ()
@ -3262,6 +3253,71 @@ Integrate with cljfmt, the Clojure code formatter:
(general-def clojure-mode-map "C-M-\\" #'cljfmt) (general-def clojure-mode-map "C-M-\\" #'cljfmt)
#+END_SRC #+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 * Scheme
Tell emacs about file extensions which should activate scheme-mode: Tell emacs about file extensions which should activate scheme-mode:
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp