Most of Clojure hackers agree that starting with Clojure can be frustrating. This has been discussed in google groups and elsewhere. Almost all hackers agree that
Clojure programmers spend most of their time in the REPL.
I find many different opinion about how to simplify this process. There are people
just complain about how hard it is while there are people outright defy and
provide all sorts of differing approaches, which adds to a newbie's confusion.
I actually created a project called ClojureW to help newbie get started easily and
quickly just to get a taste of Clojure (I blogged about where and how to get it in
my earlier blogs). This post is about a nice trick that I found to help with Clojure
hacking. In your .emacs file just put following:
(defun eshell-execute-selection () "Insert text of current selection or clipboard in eshell and execute." (interactive) (require 'eshell) (let ((command (or (buffer-substring (mark) (point)) x-last-selected-text-clipboard))) (let ((buf (current-buffer))) (unless (get-buffer eshell-buffer-name) (eshell)) (display-buffer eshell-buffer-name t) (switch-to-buffer-other-window eshell-buffer-name) (end-of-buffer) (eshell-kill-input) (insert command) (eshell-send-input) (end-of-buffer) (switch-to-buffer-other-window buf))))
and map it to a shortcut key of your choice like this:
(global-set-key [f10] 'eshell-execute-selection)
Then start a Clojure REPL in eshell and type anything in a scratch buffer. You can
select any text and hit F10 to evaluate. One excellent side effect of this hack is
that you do not need to mess with SLIME or any other language specific set up. It
works with any command line tools you already familiar with. You can execute shell
scripts, Ruby scripts, and GNU Smalltalk scripts. If you have any frequently used
commands, it will be perfect to keep them in a file and just select and hit F10.
I found this approach very helpful when I need to experiment with Clojure or Java
libs or platforms such as Java Packages and Incanter(In case, if you are not heard
it is a R like statistical charting platform written in Clojure).
I hope someone finds this useful and Please share if you have handy hacks that ease the pain of hacking. Enjoy!
Disclaimer: I did not write the whole elisp code. I just modified it to work with any selection or clipboard content. I believe I found this elisp snippet somewhere in Emacswiki, a fantastic place to "steal" snippets to hack with.