3
votes

I'm trying to setup my environment based on EMACS+CIDER for clojure development, btw I got stuck with functionality of hot-reloading I've seen in this video at 6:20-6:25 timing. http://www.parens-of-the-dead.com/e1.html

Actually, author is not using feature like RING/wrap-reload, but his code reloads in REPL after each buffer saving in emacs. I guess I have same libraries on local machine (cider, cider-nrepl, clj-refactor, clojure-mode), but my local installation is not working as shown, it requires calling (reset) function every time in REPL.

P.S. I can roughly get same behaviour by this code in EMACS, but I don't want to use hacks like that:

(defun my-clojure-reset ()
  "Reload clojure once file were saved."
  (when
      (s-ends-with? "clj" (buffer-file-name))
    (cider-interactive-eval "(do (ns user) (reset))")))

(add-hook 'after-save-hook 'my-clojure-reset)

Any suggestions how to implement code reloading on file saved event in EMACS+CIDER?

1
Have you added the reloaded.repl code as per 5m27s in the video? (see github.com/weavejester/reloaded.repl) - Mark Fisher
@MarkFisher, sure - AFAIK it provides (reset) function, but is not monitoring file changes - what I'm looking for. - Rustem

1 Answers

5
votes

You can do this kind of behavior with the follow emacs-lisp in your start up file:

(add-hook 'cider-mode-hook
          (lambda ()
            (add-hook 'after-save-hook 'cider-load-buffer nil 'make-it-local)))

This adds a hook on only buffers running cider-mode (i.e. clojure files when you've done cider-jack-in) which after saving, will call cider-load-buffer, which will evaluate the contents of your file in the cider connection. The 'make-it-local value just makes it so this hook only gets applied to local buffers, otherwise this hook would load for all modes after you opened something in cider-mode the first time.