Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure code hotreloading feature

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?

like image 444
Rustem Avatar asked Jun 20 '26 10:06

Rustem


1 Answers

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.

like image 109
tanzoniteblack Avatar answered Jun 22 '26 02:06

tanzoniteblack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!