Search code examples
emacsclojureleiningenread-eval-print-loopcider

How to clear the REPL in cider-mode?


I am not meaning cleaning up the text output of REPL; I mean cleaning up all evaluated results in REPL. During developing, repeatedly C-c C-q and C-c M-j is low efficiency.


UPDATE

There may be some bad debug behaviour of mine. I am not sure how other people develop progs with CIDER, but I really need the functionality mentioned above. I guess other developers also encounter same problems as mine.

For example, at the top of a clojure prog unit, I use declare to declare a function foo, which is used by another function bar, and foo is implemented after bar. Then, I C-c C-k, etc, and the prog goes well. Later, I removed the forward declaration of foo occasionally. What does happen? The prog still goes well. REALLY? Then, I commit my whole job and terminate the CIDER REPL session happily.

Disaster on morning: Symbol foo not found!

That's my story. So, nobody has ever encountered similar problems?


Solution

  • Try the (refresh) function in the clojure.tools.namespace.repl namespace:

    The refresh function will scan all the directories on the classpath for Clojure source files, read their ns declarations, build a graph of their dependencies, and load them in dependency order.

    https://github.com/clojure/tools.namespace#reloading-code-usage

    It doesn't seem to remove the vars declared in the user namespace, which I've typed into the REPL, but it does:

    ...unload (remove) the namespaces that changed to clear out any old definitions.

    We generally add that plus a few other useful things to the user namespace, so it's loaded into the REPL on startup:

    (ns user
      (:require [clojure.tools.namespace.repl :refer [refresh]]
                [clojure.repl :refer [doc source]]
                [clojure.pprint :refer [pprint pp]]
                [midje.repl :as midje]
                [clojure.stacktrace :as st]))
    

    To keep that code separate from your main and test sources, put that in a file at <project root>/dev/user.clj, then add the following to your lein project.clj file:

    :profiles {:dev {:source-paths ["dev"]}}
    

    (p.s. although it's not the question you want answered, for those seeing this answer and wanting to clear the text in the Cider REPL, it's C-c M-o (https://github.com/clojure-emacs/cider)