Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload Clojure files in emacs

I am just starting out learning Clojure and Emacs. I have got Clojure Box for windows running and I would like to be able to write code in a buffer then run it in the REPL without haveing to call

(use 'example.code)

all the time. I know about C-c C-k but it doesn't reload the namespace. If i use

(in-ns 'example.code)

to change namespace in the repl it works. What is the right way to do this?

like image 960
Lewis Jubb Avatar asked Jul 13 '10 11:07

Lewis Jubb


1 Answers

in-ns is one of the right ways.

The way which feels most "right" to me is to (require '[example.code :as ec]) and work in the user namespace at the REPL; that way my throwaway experimental state stays in user and ec/foo is convenient enough to me (and it makes it obvious where foo is supposed to come from). You can always say (require :reload-all 'example.code) (same works with use) to force recompilation.

Also, here's a function to remove (from the current namespace) all mappings pulled in from a given namespace with use:

(defn unuse [ns]
  (doseq [[n v] (ns-refers *ns*)]
    (if (= (.. v ns name) ns)
      (ns-unmap *ns* n))))

On top of that you could build

(defn reuse [ns]
  (unuse ns)
  (remove-ns ns)
  (use :reload-all ns))

and say (reuse 'example.code) to get something close to a fresh start with your namespace. (Note that 1.2 new features such as deftype & defrecord introduce some complexities... In particular, unuse has no effect on imported class -- this includes records and deftype-created types. :reload-all still causes the deftype et al. forms to be recompiled, but I remember hitting weird cases where this didn't seem to be enough... Possibly my error, possibly some arcane aspect of these features I haven't yet fully explored.)

like image 108
Michał Marczyk Avatar answered Oct 19 '22 14:10

Michał Marczyk