Using Emacs as Clojure IDE

  1. Open emacs24;

  2. Change CWD to parent folder of project home: M-x cd ~/docs/tmp;

  3. Build a leiningen project: M-! lein new calc-cov (see Run Shell Command in Emacs for other method to run shell command in emacs);

  4. Change CWD to project home: M-x cd calc-cov;

  5. Start nrepl server: M-x nrepl-jack-in;

  6. Open source file in another window: C-x 2 C-x C-f src/.../core.clj;

  7. Change the namespace of the REPL to the namespace of the file you are in: C-c M-n;

  8. Evaluate the whole source file: C-c C-k, evaluate a s-exp before the cursor: C-x C-e;

  9. Open clojure doc of the function under the cursor: C-c C-d

  10. Run test: C-c C-,

Frequently used keyboard shortcuts and commands

  • Toggle focus between nrepl window and clojure source file window: C-c C-z (you should add "(add-to-list 'same-window-buffer-names "nrepl")" to ~/.emacs;

  • Always pretty printing in the REPL: M-x nrepl-toggle-pretty-printing

  • Move over sexp: C-M-b/C-M-f;

  • Interrupt any pending evaluations: C-c C-b;

Evaluate a sexp without evaluate all file

  1. Add this sexp into source file (unnecessary to save source file);

  2. Use following methods to evaluate the sexp:

  3. Move cursor to the sexp for test, C-M-x (or C-c C-c) to evaluate the top-level form (such as a defn expression) under the cursor; If you want insert the evaluation result to current buffer, use C-u C-M-x;

  4. If the the target form is not a "top-level" form, move the cursor to the end of this sexp, C-x C-e to evaluate it, See more shortcuts at nrepl official site;

Then this sexp has been added to the repl, you can use it in repl;

This is very helpful in some circumstances. For example, I want debug function "cell-block" (see p141 in "Clojure Programming"), add

(println "[left mid right] is:") (println [left mid right])

before "(window (map ...)". But You can't evaluate the whole file because there are some lazy sequences and evaluate whole file will cause JVM heap out of memory; So I only evaluate this modified version with C-x C-e, test it in repl. After I figure out how this function works, undo my modification and move on.

Note:

  • In clojure mode with Evil, you have to switch to insert state and put the cursor after the last parenthsis, then evaluate the expression with C-x C-e. If you put the cursor on the last parenthesis in normal state, The result will be wrong.

  • It unnecessary to put your clojure file in a leiningen-created project, which is more flexible than vim-fireplace;

  • The value of expression will be output to the message area (bottom of the emacs window), while the output by "println" will be output to nrepl buffer;

  • Switch cursor between windows: C-x o;

  • Clear repl output: C-c M-o;

  • Indent code blocks intelligently: C-M-q

  • All the keyboard shortcuts will be more convenient if you swap CapsLock key and Escape key, together with Ctrl key and Alt key, see Define Keymap on Ubuntu for detail;

Ref: http://stackoverflow.com/questions/3636364/can-i-clean-the-repl

Ref: Clojure with Emacs

原文地址:https://www.cnblogs.com/darkmatter/p/3606814.html