Skip to content

Instantly share code, notes, and snippets.

@mvarela
Last active March 18, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvarela/08663bafd54c60d478b26b69a207453a to your computer and use it in GitHub Desktop.
Save mvarela/08663bafd54c60d478b26b69a207453a to your computer and use it in GitHub Desktop.
;; This snippet allows to add a ":ns" option to clojure source blocks, which
;; will correctly set the cider-buffer-ns variable, so that the block in quesiton, as well
;; as subsequent blocks, are executed in the desired namespace, instead of "user" (this happens
;; because of how CIDER handles the namespaces in non-clojure files (such as org files)
;; - example usage
;;
;;
;; The namespace is created, and the rest of the block is evaluated in it.
;;
;; #+BEGIN_SRC clojure :results output
;; (ns my-namespace.core)
;; (prn *ns*)
;; #+END_SRC
;; #+RESULTS:
;; : #namespace[my-namespace.core]
;;
;;
;; This below, however, is probably not what you want, but it's the default
;; behavior in ob-clojure.el when using CIDER as a backend
;;
;; #+BEGIN_SRC clojure :results value
;; (def greeting "hi there!")
;; #+END_SRC
;;
;; #+RESULTS:
;; : #'user/greeting
;;
;;
;; Setting the :ns option will fix this. The block below and those coming after it
;; will be evaluated within the desired namespace
;;
;; #+BEGIN_SRC clojure :results value :ns my-namespace.core
;; (def greeting "hi there from my-namespace.core!")
;; #+END_SRC
;;
;; #+RESULTS:
;; : #'my-namespace.core/greeting
(require 'ob-clojure)
(defun ob-clojure-set-ns (body params)
(let ((ns (cdr (assq :ns params))))
(when ns
(setq-local cider-buffer-ns ns))))
(advice-add #'org-babel-execute:clojure :before #'ob-clojure-set-ns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment